-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcli.test.ts
More file actions
766 lines (595 loc) · 27 KB
/
cli.test.ts
File metadata and controls
766 lines (595 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/**
* CLI 테스트
*/
import { describe, expect, it, vi } from 'vitest';
import { isDirectExecution, runCli } from '../../src/cli.js';
function createDeps() {
const output: string[] = [];
const errors: string[] = [];
return {
output,
errors,
deps: {
fetchImpl: vi.fn<typeof fetch>(),
writeOut: (message: string) => {
output.push(message);
},
writeErr: (message: string) => {
errors.push(message);
},
getVersion: () => '9.9.9',
nowIso: () => '2026-03-07T00:00:00.000Z',
runCommand: vi.fn<(command: string, args: string[]) => Promise<number>>(),
isInteractiveTerminal: () => false,
runInteractive: vi.fn<(deps: { fetchImpl: typeof fetch }) => Promise<number>>(),
},
};
}
describe('CLI', () => {
it('명령어 없이 TTY 환경이면 인터랙티브 모드를 실행한다', async () => {
const { deps } = createDeps();
deps.isInteractiveTerminal = () => true;
deps.runInteractive = vi.fn().mockResolvedValue(0);
const exitCode = await runCli([], deps);
expect(exitCode).toBe(0);
expect(deps.runInteractive).toHaveBeenCalledTimes(1);
});
it('--non-interactive 옵션이 있으면 TTY에서도 인터랙티브 모드를 실행하지 않는다', async () => {
const { output, deps } = createDeps();
deps.isInteractiveTerminal = () => true;
deps.runInteractive = vi.fn().mockResolvedValue(0);
const exitCode = await runCli(['--non-interactive'], deps);
expect(exitCode).toBe(0);
expect(deps.runInteractive).not.toHaveBeenCalled();
expect(output.join('\n')).toContain('사용법:');
});
it('기본 실행 시 도움말을 출력한다', async () => {
const { output, deps } = createDeps();
const exitCode = await runCli([], deps);
expect(exitCode).toBe(0);
expect(output.join('\n')).toContain('사용법:');
});
it('help <command>는 상세 도움말을 출력한다', async () => {
const { output, deps } = createDeps();
const exitCode = await runCli(['help', 'products'], deps);
expect(exitCode).toBe(0);
expect(output.join('\n')).toContain('명령: products');
expect(output.join('\n')).toContain('옵션: --page, --pageSize');
});
it('help <unknown>은 오류를 반환한다', async () => {
const { errors, deps } = createDeps();
const exitCode = await runCli(['help', 'unknown'], deps);
expect(exitCode).toBe(1);
expect(errors.join('\n')).toContain('도움말을 찾을 수 없는 명령어');
});
it('version 명령은 버전을 출력한다', async () => {
const { output, deps } = createDeps();
const exitCode = await runCli(['version'], deps);
expect(exitCode).toBe(0);
expect(output).toEqual(['9.9.9']);
});
it('url 명령은 MCP URL을 출력한다', async () => {
const { output, deps } = createDeps();
const exitCode = await runCli(['url'], deps);
expect(exitCode).toBe(0);
expect(output).toEqual(['https://mcp.aka.page/mcp']);
});
it('get 명령은 임의 API를 호출한다', async () => {
const { deps, output } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true, data: { products: [] }, meta: { total: 0 } }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['get', '/api/daiso/products', '--q', '수납박스'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith('https://mcp.aka.page/api/daiso/products?q=%EC%88%98%EB%82%A9%EB%B0%95%EC%8A%A4');
expect(output.join('\n')).toContain('요청 성공');
expect(output.join('\n')).toContain('원본 JSON은 --json 옵션으로 확인하세요.');
});
it('get --json은 원본 JSON을 출력하고 쿼리에 json을 추가하지 않는다', async () => {
const { deps, output } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true, data: { value: 1 } }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['get', '/api/daiso/products', '--q', '수납박스', '--json'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith('https://mcp.aka.page/api/daiso/products?q=%EC%88%98%EB%82%A9%EB%B0%95%EC%8A%A4');
expect(output[0]).toContain('"success": true');
});
it('products 명령은 검색어로 제품 API를 호출한다', async () => {
const { deps, output } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({
success: true,
data: { products: [{ name: '수납박스', id: '1', price: 1000 }] },
}),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['products', '수납박스', '--page', '2', '--pageSize', '10'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/daiso/products?q=%EC%88%98%EB%82%A9%EB%B0%95%EC%8A%A4&page=2&pageSize=10',
);
expect(output.join('\n')).toContain('제품 목록: 1건');
});
it('--non-interactive 옵션은 일반 명령 호출 시 쿼리에 포함되지 않는다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['products', '수납박스', '--non-interactive', '--page', '1'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/daiso/products?q=%EC%88%98%EB%82%A9%EB%B0%95%EC%8A%A4&page=1',
);
});
it('product 명령은 제품 상세 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['product', '1034604'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith('https://mcp.aka.page/api/daiso/products/1034604');
});
it('stores 명령은 keyword 없이도 sido로 호출할 수 있다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['stores', '--sido', '서울', '--gugun', '강남구'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/daiso/stores?sido=%EC%84%9C%EC%9A%B8&gugun=%EA%B0%95%EB%82%A8%EA%B5%AC',
);
});
it('stores --help는 상세 도움말을 출력한다', async () => {
const { output, deps } = createDeps();
const exitCode = await runCli(['stores', '--help'], deps);
expect(exitCode).toBe(0);
expect(output.join('\n')).toContain('명령: stores');
expect(output.join('\n')).toContain('옵션: --keyword, --sido, --gugun, --dong, --limit');
});
it('stores 명령은 다이소 키워드 보정으로 재검색한다', async () => {
const { deps, output } = createDeps();
deps.fetchImpl = vi
.fn<typeof fetch>()
.mockResolvedValueOnce({
ok: true,
json: vi.fn().mockResolvedValue({ success: true, data: { stores: [] } }),
} as unknown as Response)
.mockResolvedValueOnce({
ok: true,
json: vi.fn().mockResolvedValue({
success: true,
data: { stores: [{ name: '안산중앙본점', address: '경기 안산', phone: '1522-4400' }] },
}),
} as unknown as Response);
const exitCode = await runCli(['stores', '안산 중앙역'], deps);
expect(exitCode).toBe(0);
expect(deps.fetchImpl).toHaveBeenNthCalledWith(
1,
'https://mcp.aka.page/api/daiso/stores?keyword=%EC%95%88%EC%82%B0+%EC%A4%91%EC%95%99%EC%97%AD',
);
expect(deps.fetchImpl).toHaveBeenNthCalledWith(
2,
'https://mcp.aka.page/api/daiso/stores?keyword=%EC%95%88%EC%82%B0%EC%A4%91%EC%95%99%EC%97%AD',
);
expect(output.join('\n')).toContain('입력 키워드 "안산 중앙역" 대신 "안산중앙역"로 매장을 찾았습니다.');
});
it('inventory 명령은 productId로 재고 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['inventory', '1034604', '--keyword', '강남역'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/daiso/inventory?productId=1034604&keyword=%EA%B0%95%EB%82%A8%EC%97%AD',
);
});
it('display-location 명령은 진열 위치 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['display-location', '1034604', '04515'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/daiso/display-location?productId=1034604&storeCode=04515',
);
});
it('display-location 명령은 인자가 부족하면 실패한다', async () => {
const { errors, deps } = createDeps();
const exitCode = await runCli(['display-location', '1034604'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('productId와 storeCode가 필요합니다');
});
it('cu-stores 명령은 CU 매장 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['cu-stores', '강남', '--limit', '5'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith('https://mcp.aka.page/api/cu/stores?limit=5&keyword=%EA%B0%95%EB%82%A8');
});
it('cu-inventory 명령은 CU 재고 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['cu-inventory', '과자', '--storeKeyword', '강남'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/cu/inventory?keyword=%EA%B3%BC%EC%9E%90&storeKeyword=%EA%B0%95%EB%82%A8',
);
});
it('cu-inventory 명령은 검색어가 없으면 실패한다', async () => {
const { errors, deps } = createDeps();
const exitCode = await runCli(['cu-inventory'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('검색어가 필요합니다');
});
it('lottecinema-theaters 명령은 롯데시네마 주변 지점 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true, data: { theaters: [] }, meta: { total: 0 } }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(
['lottecinema-theaters', '--lat', '37.3154', '--lng', '126.8388', '--limit', '5'],
deps,
);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/lottecinema/theaters?lat=37.3154&lng=126.8388&limit=5',
);
});
it('lottecinema-movies 명령은 롯데시네마 영화/회차 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true, data: { movies: [], showtimes: [] }, meta: { total: 0 } }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['lottecinema-movies', '--playDate', '20260310', '--theaterId', '3012'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/lottecinema/movies?playDate=20260310&theaterId=3012',
);
});
it('lottecinema-seats 명령은 롯데시네마 잔여 좌석 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true, data: { seats: [] }, meta: { total: 0 } }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(
['lottecinema-seats', '--playDate', '20260310', '--theaterId', '3012', '--movieId', '23816', '--limit', '10'],
deps,
);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/lottecinema/seats?playDate=20260310&theaterId=3012&movieId=23816&limit=10',
);
});
it('emart24-stores 명령은 이마트24 매장 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['emart24-stores', '강남', '--service24h', 'true', '--limit', '5'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/emart24/stores?service24h=true&limit=5&keyword=%EA%B0%95%EB%82%A8',
);
});
it('emart24-products 명령은 이마트24 상품 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['emart24-products', '두바이', '--page', '2', '--pageSize', '20'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/emart24/products?keyword=%EB%91%90%EB%B0%94%EC%9D%B4&page=2&pageSize=20',
);
});
it('emart24-products 명령은 검색어가 없으면 실패한다', async () => {
const { errors, deps } = createDeps();
const exitCode = await runCli(['emart24-products'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('검색어가 필요합니다');
});
it('emart24-inventory 명령은 이마트24 재고 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(
['emart24-inventory', '8800244010504', '--bizNoArr', '28339,05015'],
deps,
);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/emart24/inventory?pluCd=8800244010504&bizNoArr=28339%2C05015',
);
});
it('emart24-inventory 명령은 필수 인자가 없으면 실패한다', async () => {
const { errors, deps } = createDeps();
const exitCode = await runCli(['emart24-inventory', '8800244010504'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('pluCd와 --bizNoArr가 필요합니다');
});
it('lottemart-stores 명령은 롯데마트 매장 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['lottemart-stores', '잠실', '--area', '서울', '--limit', '5'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/lottemart/stores?area=%EC%84%9C%EC%9A%B8&limit=5&keyword=%EC%9E%A0%EC%8B%A4',
);
});
it('lottemart-products 명령은 롯데마트 상품 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['lottemart-products', '콜라', '--storeName', '강변점', '--area', '서울'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/lottemart/products?keyword=%EC%BD%9C%EB%9D%BC&storeName=%EA%B0%95%EB%B3%80%EC%A0%90&area=%EC%84%9C%EC%9A%B8',
);
});
it('lottemart-products 명령은 store 정보가 없으면 실패한다', async () => {
const { errors, deps } = createDeps();
const exitCode = await runCli(['lottemart-products', '콜라'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('--storeCode 또는 --storeName이 필요합니다');
});
it('gs25-stores 명령은 GS25 매장 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['gs25-stores', '강남', '--limit', '5'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith('https://mcp.aka.page/api/gs25/stores?limit=5&keyword=%EA%B0%95%EB%82%A8');
});
it('gs25-products 명령은 GS25 상품 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['gs25-products', '오감자', '--limit', '10'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/gs25/products?keyword=%EC%98%A4%EA%B0%90%EC%9E%90&limit=10',
);
});
it('gs25-inventory 명령은 GS25 재고 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['gs25-inventory', '오감자', '--storeKeyword', '강남'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/gs25/inventory?keyword=%EC%98%A4%EA%B0%90%EC%9E%90&storeKeyword=%EA%B0%95%EB%82%A8',
);
});
it('seveneleven-products 명령은 세븐일레븐 상품 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['seveneleven-products', '삼각김밥', '--size', '20'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/seveneleven/products?query=%EC%82%BC%EA%B0%81%EA%B9%80%EB%B0%A5&size=20',
);
});
it('seveneleven-stores 명령은 세븐일레븐 매장 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['seveneleven-stores', '안산 중앙역', '--limit', '10'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/seveneleven/stores?keyword=%EC%95%88%EC%82%B0+%EC%A4%91%EC%95%99%EC%97%AD&limit=10',
);
});
it('seveneleven-popwords 명령은 세븐일레븐 인기 검색어 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(['seveneleven-popwords', '--label', 'home'], deps);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith('https://mcp.aka.page/api/seveneleven/popwords?label=home');
});
it('seveneleven-catalog 명령은 세븐일레븐 카탈로그 API를 호출한다', async () => {
const { deps } = createDeps();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ success: true }),
} as unknown as Response);
deps.fetchImpl = fetchImpl;
const exitCode = await runCli(
['seveneleven-catalog', '--includeIssues', 'true', '--includeExhibition', 'true', '--limit', '10'],
deps,
);
expect(exitCode).toBe(0);
expect(fetchImpl).toHaveBeenCalledWith(
'https://mcp.aka.page/api/seveneleven/catalog?includeIssues=true&includeExhibition=true&limit=10',
);
});
it('health 명령은 서버 상태를 출력한다', async () => {
const { output, deps } = createDeps();
deps.fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ status: 'ok' }),
} as unknown as Response);
const exitCode = await runCli(['health'], deps);
expect(exitCode).toBe(0);
expect(output[0]).toContain('"status": "ok"');
expect(output[0]).toContain('"checkedAt": "2026-03-07T00:00:00.000Z"');
});
it('health 명령은 status 누락 시 unknown으로 출력한다', async () => {
const { output, deps } = createDeps();
deps.fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({}),
} as unknown as Response);
const exitCode = await runCli(['health'], deps);
expect(exitCode).toBe(0);
expect(output[0]).toContain('"status": "unknown"');
});
it('health 명령은 HTTP 오류를 처리한다', async () => {
const { errors, deps } = createDeps();
deps.fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: false,
status: 503,
} as Response);
const exitCode = await runCli(['health'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('HTTP 503');
});
it('get 명령은 경로가 없으면 실패한다', async () => {
const { errors, deps } = createDeps();
const exitCode = await runCli(['get'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('경로가 필요합니다');
});
it('products 명령은 검색어가 없으면 실패한다', async () => {
const { errors, deps } = createDeps();
const exitCode = await runCli(['products'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('검색어가 필요합니다');
});
it('health 명령은 예외를 처리한다', async () => {
const { errors, deps } = createDeps();
deps.fetchImpl = vi.fn<typeof fetch>().mockRejectedValue(new Error('network down'));
const exitCode = await runCli(['health'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('network down');
});
it('health 명령은 문자열 예외를 처리한다', async () => {
const { errors, deps } = createDeps();
deps.fetchImpl = vi.fn<typeof fetch>().mockRejectedValue('network down');
const exitCode = await runCli(['health'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('network down');
});
it('claude 명령은 등록 명령을 출력한다', async () => {
const { output, deps } = createDeps();
const exitCode = await runCli(['claude'], deps);
expect(exitCode).toBe(0);
expect(output).toEqual(['claude mcp add daiso https://mcp.aka.page --transport sse']);
});
it('claude --exec 명령은 실제 실행 함수를 호출한다', async () => {
const { deps } = createDeps();
deps.runCommand = vi.fn().mockResolvedValue(0);
const exitCode = await runCli(['claude', '--exec'], deps);
expect(exitCode).toBe(0);
expect(deps.runCommand).toHaveBeenCalledWith('claude', [
'mcp',
'add',
'daiso',
'https://mcp.aka.page',
'--transport',
'sse',
]);
});
it('claude --exec 명령은 실행 예외를 처리한다', async () => {
const { errors, deps } = createDeps();
deps.runCommand = vi.fn().mockRejectedValue(new Error('not found'));
const exitCode = await runCli(['claude', '--exec'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('not found');
});
it('claude --exec 명령은 문자열 예외를 처리한다', async () => {
const { errors, deps } = createDeps();
deps.runCommand = vi.fn().mockRejectedValue('not found');
const exitCode = await runCli(['claude', '--exec'], deps);
expect(exitCode).toBe(1);
expect(errors[0]).toContain('not found');
});
it('기본 의존성으로도 version 명령이 동작한다', async () => {
const writeOut = vi.fn<(message: string) => void>();
const writeErr = vi.fn<(message: string) => void>();
const exitCode = await runCli(['version'], { writeOut, writeErr });
expect(exitCode).toBe(0);
expect(writeOut).toHaveBeenCalledTimes(1);
expect(writeErr).not.toHaveBeenCalled();
});
it('기본 nowIso 의존성으로 health 명령이 동작한다', async () => {
const writeOut = vi.fn<(message: string) => void>();
const writeErr = vi.fn<(message: string) => void>();
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ status: 'ok' }),
} as unknown as Response);
const exitCode = await runCli(['health'], { fetchImpl, writeOut, writeErr });
expect(exitCode).toBe(0);
expect(writeOut).toHaveBeenCalledTimes(1);
expect(writeErr).not.toHaveBeenCalled();
});
it('알 수 없는 명령은 에러를 반환한다', async () => {
const { errors, deps } = createDeps();
const exitCode = await runCli(['unknown'], deps);
expect(exitCode).toBe(1);
expect(errors.join('\n')).toContain('알 수 없는 명령어');
});
it('직접 실행 여부를 올바르게 판별한다', () => {
expect(isDirectExecution('', 'file:///tmp/test.js')).toBe(false);
expect(isDirectExecution('/tmp/test.js', 'file:///tmp/test.js')).toBe(true);
expect(isDirectExecution('/tmp/other.js', 'file:///tmp/test.js')).toBe(false);
});
});