|
| 1 | +import { vi, describe, it, expect, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +// Mock fs.stat/readFile and resolvePath so the specific filesystem-error |
| 4 | +// messages in getSpecificFsErrorMessage (EISDIR / EACCES / EPERM) and the |
| 5 | +// basic-message fallbacks are reachable without relying on real OS errors. |
| 6 | +vi.mock('node:fs', () => ({ |
| 7 | + promises: { |
| 8 | + stat: vi.fn().mockName('fs.stat'), |
| 9 | + readFile: vi.fn().mockName('fs.readFile'), |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock('../../src/utils/path-utils.js', () => ({ |
| 14 | + resolvePath: vi.fn().mockName('pathUtils.resolvePath'), |
| 15 | + PROJECT_ROOT: '/project-root', |
| 16 | +})); |
| 17 | + |
| 18 | +const isFileStat = (isFile: boolean) => ({ isFile: () => isFile }); |
| 19 | + |
| 20 | +describe('read-content filesystem-error branches', () => { |
| 21 | + let handler: (args: unknown) => Promise<{ content: { text: string }[] }>; |
| 22 | + let fsMock: { stat: ReturnType<typeof vi.fn>; readFile: ReturnType<typeof vi.fn> }; |
| 23 | + let resolvePath: ReturnType<typeof vi.fn>; |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + fsMock = (await import('node:fs')).promises as never; |
| 27 | + resolvePath = (await import('../../src/utils/path-utils.js')).resolvePath as never; |
| 28 | + vi.resetAllMocks(); |
| 29 | + resolvePath.mockImplementation((p: string) => `/project-root/${p}`); |
| 30 | + fsMock.stat.mockResolvedValue(isFileStat(true)); |
| 31 | + const { readContentToolDefinition } = await import('../../src/handlers/read-content.js'); |
| 32 | + handler = readContentToolDefinition.handler; |
| 33 | + }); |
| 34 | + |
| 35 | + const firstError = async (args: unknown) => { |
| 36 | + const res = await handler(args); |
| 37 | + return JSON.parse(res.content[0].text)[0].error as string; |
| 38 | + }; |
| 39 | + |
| 40 | + it('maps EISDIR to a "Path is a directory" message with the relative path', async () => { |
| 41 | + fsMock.stat.mockRejectedValue(Object.assign(new Error('x'), { code: 'EISDIR' })); |
| 42 | + expect(await firstError({ paths: ['somedir'] })).toBe( |
| 43 | + 'Path is a directory, not a file: somedir', |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('maps EACCES to a permission-denied read message', async () => { |
| 48 | + fsMock.stat.mockRejectedValue(Object.assign(new Error('x'), { code: 'EACCES' })); |
| 49 | + expect(await firstError({ paths: ['locked.txt'] })).toBe( |
| 50 | + 'Permission denied reading file: locked.txt', |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('maps EPERM to a permission-denied read message', async () => { |
| 55 | + fsMock.stat.mockRejectedValue(Object.assign(new Error('x'), { code: 'EPERM' })); |
| 56 | + expect(await firstError({ paths: ['locked.txt'] })).toMatch(/Permission denied reading file/); |
| 57 | + }); |
| 58 | + |
| 59 | + it('falls back to a basic filesystem-error message for an unknown code', async () => { |
| 60 | + fsMock.stat.mockRejectedValue(Object.assign(new Error('weird'), { code: 'EBUSY' })); |
| 61 | + expect(await firstError({ paths: ['busy.txt'] })).toBe('Filesystem error: weird'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('falls back to a basic message for a non-object rejection', async () => { |
| 65 | + fsMock.stat.mockRejectedValue('totally not an error object'); |
| 66 | + expect(await firstError({ paths: ['weird.txt'] })).toBe( |
| 67 | + 'Filesystem error: totally not an error object', |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns ENOENT "File not found" with the resolved target path', async () => { |
| 72 | + fsMock.stat.mockRejectedValue(Object.assign(new Error('x'), { code: 'ENOENT' })); |
| 73 | + const err = await firstError({ paths: ['gone.txt'] }); |
| 74 | + expect(err).toMatch(/File not found at resolved path '\/project-root\/gone.txt'/); |
| 75 | + expect(err).toContain("from relative path 'gone.txt'"); |
| 76 | + }); |
| 77 | + |
| 78 | + it('reads the whole file when no line range is given', async () => { |
| 79 | + fsMock.stat.mockResolvedValue(isFileStat(true)); |
| 80 | + fsMock.readFile.mockResolvedValue('full body'); |
| 81 | + const res = await handler({ paths: ['whole.txt'] }); |
| 82 | + const parsed = JSON.parse(res.content[0].text)[0]; |
| 83 | + expect(parsed.content).toBe('full body'); |
| 84 | + expect(parsed.error).toBeUndefined(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments