diff --git a/bun.lock b/bun.lock index 43550fc..5b79f49 100644 --- a/bun.lock +++ b/bun.lock @@ -1,6 +1,5 @@ { "lockfileVersion": 1, - "configVersion": 0, "workspaces": { "": { "dependencies": { diff --git a/tests/api.test.ts b/tests/api.test.ts new file mode 100644 index 0000000..be7d033 --- /dev/null +++ b/tests/api.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, test, mock, afterEach, beforeEach, spyOn } from 'bun:test'; +import fs from 'fs'; +import { loadSession } from '../src/api'; + +describe('api.ts loadSession', () => { + let originalConsoleError: any; + let existsSyncSpy: any; + let readFileSyncSpy: any; + + beforeEach(() => { + originalConsoleError = console.error; + console.error = mock(() => {}); + + // Use spyOn to mock specific fs methods + existsSyncSpy = spyOn(fs, 'existsSync').mockReturnValue(true); + readFileSyncSpy = spyOn(fs, 'readFileSync').mockReturnValue('{ "invalid": json '); + }); + + afterEach(() => { + console.error = originalConsoleError; + existsSyncSpy.mockRestore(); + readFileSyncSpy.mockRestore(); + }); + + test('loadSession throws error on JSON parse failure', async () => { + // Assert that the Promise rejects with a SyntaxError (JSON parse error) + await expect(loadSession()).rejects.toThrow(SyntaxError); + // Verify that console.error was called + expect(console.error).toHaveBeenCalled(); + }); +});