From c0387ff0d34e5c8ce1351f686944a95bf02a1d76 Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Wed, 28 Jan 2026 15:02:40 -0500 Subject: [PATCH] Add unit tests for git persistence --- src/db.test.ts | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/db.test.ts b/src/db.test.ts index 6c1a3d6..8d47507 100644 --- a/src/db.test.ts +++ b/src/db.test.ts @@ -14,6 +14,7 @@ import { initDb, respondToPrompt, updateLastSeen, + updateSessionStats, } from "./db"; beforeEach(() => { @@ -280,3 +281,61 @@ test("getSessionOutput only returns output for specific session", () => { expect(output1[0]?.line).toBe("Session 1 output"); expect(output2[0]?.line).toBe("Session 2 output"); }); + +// Git persistence tests + +test("updateSessionStats persists git branch and files", () => { + const device = createDevice("secret123"); + const session = createSession(device.id); + + updateSessionStats(session.id, { + state: "ready", + prompts: 0, + completions: 0, + tools: 0, + compressions: 0, + thinking_seconds: 0, + work_seconds: 0, + mode: "normal", + model: null, + idle_since: null, + git_branch: "feature/test", + git_files_json: JSON.stringify([ + { path: "src/test.ts", status: "M", additions: 5, deletions: 2 }, + ]), + }); + + const updated = getSession(session.id); + expect(updated?.git_branch).toBe("feature/test"); + + const files = JSON.parse(updated?.git_files_json || "[]"); + expect(files).toHaveLength(1); + expect(files[0].path).toBe("src/test.ts"); + expect(files[0].status).toBe("M"); + expect(files[0].additions).toBe(5); + expect(files[0].deletions).toBe(2); +}); + +test("updateSessionStats handles null git data", () => { + const device = createDevice("secret123"); + const session = createSession(device.id); + + updateSessionStats(session.id, { + state: "ready", + prompts: 1, + completions: 1, + tools: 1, + compressions: 0, + thinking_seconds: 10, + work_seconds: 30, + mode: "normal", + model: "claude-sonnet", + idle_since: Date.now(), + git_branch: null, + git_files_json: null, + }); + + const updated = getSession(session.id); + expect(updated?.git_branch).toBeNull(); + expect(updated?.git_files_json).toBeNull(); +});