From 59225e9d9664fa612572998c4f9491a4a917bd32 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 5 Mar 2026 04:21:08 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20missing=20tests=20for=20lo?= =?UTF-8?q?adSession=20parsing=20failure=20in=20src/api.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Mocked fs.existsSync to return true - Mocked fs.readFileSync to return an invalid JSON string - Tested that loadSession throws a JSON parse error (SyntaxError) - Verified that console.error is called during the failure - Handled mock restoration correctly using spyOn to avoid interfering with other tests Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> --- bun.lock | 1 - tests/api.test.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/api.test.ts 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(); + }); +});