|
1 | 1 | import { beforeEach, describe, expect, it, vi } from "bun:test"; |
2 | 2 | import type { |
3 | 3 | DeleteFileResult, |
| 4 | + FileExistsResult, |
4 | 5 | MkdirResult, |
5 | 6 | MoveFileResult, |
6 | 7 | ReadFileResult, |
@@ -486,6 +487,90 @@ describe('FileHandler', () => { |
486 | 487 | }); |
487 | 488 | }); |
488 | 489 |
|
| 490 | + describe('handleExists - POST /api/exists', () => { |
| 491 | + it('should return true when file exists', async () => { |
| 492 | + const existsData = { |
| 493 | + path: '/tmp/test.txt', |
| 494 | + sessionId: 'session-123' |
| 495 | + }; |
| 496 | + |
| 497 | + (mockFileService.exists as any).mockResolvedValue({ |
| 498 | + success: true, |
| 499 | + data: true |
| 500 | + }); |
| 501 | + |
| 502 | + const request = new Request('http://localhost:3000/api/exists', { |
| 503 | + method: 'POST', |
| 504 | + headers: { 'Content-Type': 'application/json' }, |
| 505 | + body: JSON.stringify(existsData) |
| 506 | + }); |
| 507 | + |
| 508 | + const response = await fileHandler.handle(request, mockContext); |
| 509 | + |
| 510 | + expect(response.status).toBe(200); |
| 511 | + const responseData = await response.json() as FileExistsResult; |
| 512 | + expect(responseData.success).toBe(true); |
| 513 | + expect(responseData.exists).toBe(true); |
| 514 | + expect(responseData.path).toBe('/tmp/test.txt'); |
| 515 | + expect(responseData.timestamp).toBeDefined(); |
| 516 | + |
| 517 | + expect(mockFileService.exists).toHaveBeenCalledWith('/tmp/test.txt', 'session-123'); |
| 518 | + }); |
| 519 | + |
| 520 | + it('should return false when file does not exist', async () => { |
| 521 | + const existsData = { |
| 522 | + path: '/tmp/nonexistent.txt', |
| 523 | + sessionId: 'session-123' |
| 524 | + }; |
| 525 | + |
| 526 | + (mockFileService.exists as any).mockResolvedValue({ |
| 527 | + success: true, |
| 528 | + data: false |
| 529 | + }); |
| 530 | + |
| 531 | + const request = new Request('http://localhost:3000/api/exists', { |
| 532 | + method: 'POST', |
| 533 | + headers: { 'Content-Type': 'application/json' }, |
| 534 | + body: JSON.stringify(existsData) |
| 535 | + }); |
| 536 | + |
| 537 | + const response = await fileHandler.handle(request, mockContext); |
| 538 | + |
| 539 | + expect(response.status).toBe(200); |
| 540 | + const responseData = await response.json() as FileExistsResult; |
| 541 | + expect(responseData.success).toBe(true); |
| 542 | + expect(responseData.exists).toBe(false); |
| 543 | + }); |
| 544 | + |
| 545 | + it('should handle errors when checking file existence', async () => { |
| 546 | + const existsData = { |
| 547 | + path: '/invalid/path', |
| 548 | + sessionId: 'session-123' |
| 549 | + }; |
| 550 | + |
| 551 | + (mockFileService.exists as any).mockResolvedValue({ |
| 552 | + success: false, |
| 553 | + error: { |
| 554 | + message: 'Invalid path', |
| 555 | + code: 'VALIDATION_FAILED', |
| 556 | + httpStatus: 400 |
| 557 | + } |
| 558 | + }); |
| 559 | + |
| 560 | + const request = new Request('http://localhost:3000/api/exists', { |
| 561 | + method: 'POST', |
| 562 | + headers: { 'Content-Type': 'application/json' }, |
| 563 | + body: JSON.stringify(existsData) |
| 564 | + }); |
| 565 | + |
| 566 | + const response = await fileHandler.handle(request, mockContext); |
| 567 | + |
| 568 | + expect(response.status).toBe(400); |
| 569 | + const responseData = await response.json() as ErrorResponse; |
| 570 | + expect(responseData.code).toBe('VALIDATION_FAILED'); |
| 571 | + }); |
| 572 | + }); |
| 573 | + |
489 | 574 | describe('route handling', () => { |
490 | 575 | it('should return 500 for invalid endpoints', async () => { |
491 | 576 | const request = new Request('http://localhost:3000/api/invalid-operation', { |
|
0 commit comments