|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +import type { ProviderEvent } from "@t3tools/contracts"; |
| 8 | +import { PersistenceService } from "./persistenceService"; |
| 9 | + |
| 10 | +const tempDirs: string[] = []; |
| 11 | + |
| 12 | +function makeTempDir(prefix: string): string { |
| 13 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 14 | + tempDirs.push(dir); |
| 15 | + return dir; |
| 16 | +} |
| 17 | + |
| 18 | +function iso(offsetMs = 0): string { |
| 19 | + return new Date(Date.now() + offsetMs).toISOString(); |
| 20 | +} |
| 21 | + |
| 22 | +afterEach(() => { |
| 23 | + for (const dir of tempDirs.splice(0, tempDirs.length)) { |
| 24 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | +describe("PersistenceService", () => { |
| 29 | + it("persists projects/threads/messages and serves bootstrap + catch-up", () => { |
| 30 | + const stateDir = makeTempDir("t3code-persistence-state-"); |
| 31 | + const projectDir = makeTempDir("t3code-persistence-project-"); |
| 32 | + const service = new PersistenceService({ |
| 33 | + dbPath: path.join(stateDir, "state.sqlite"), |
| 34 | + }); |
| 35 | + |
| 36 | + const addedProject = service.addProject({ cwd: projectDir }); |
| 37 | + expect(addedProject.created).toBe(true); |
| 38 | + |
| 39 | + const createdThread = service.createThread({ |
| 40 | + projectId: addedProject.project.id, |
| 41 | + title: "Thread 1", |
| 42 | + model: "gpt-5.3-codex", |
| 43 | + }).thread; |
| 44 | + |
| 45 | + service.bindSessionToThread("sess-1", createdThread.id, "runtime-thread-1"); |
| 46 | + service.persistUserMessageForTurn({ |
| 47 | + sessionId: "sess-1", |
| 48 | + clientMessageId: "msg-user-1", |
| 49 | + clientMessageText: "hello world", |
| 50 | + input: "hello world", |
| 51 | + attachments: [], |
| 52 | + }); |
| 53 | + |
| 54 | + const baseEvent: Omit<ProviderEvent, "id" | "createdAt" | "method" | "payload"> = { |
| 55 | + kind: "notification", |
| 56 | + provider: "codex", |
| 57 | + sessionId: "sess-1", |
| 58 | + }; |
| 59 | + |
| 60 | + service.ingestProviderEvent({ |
| 61 | + ...baseEvent, |
| 62 | + id: "evt-1", |
| 63 | + createdAt: iso(10), |
| 64 | + method: "item/started", |
| 65 | + payload: { |
| 66 | + item: { |
| 67 | + type: "agentMessage", |
| 68 | + id: "assistant-msg-1", |
| 69 | + text: "", |
| 70 | + }, |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + service.ingestProviderEvent({ |
| 75 | + ...baseEvent, |
| 76 | + id: "evt-2", |
| 77 | + createdAt: iso(20), |
| 78 | + method: "item/agentMessage/delta", |
| 79 | + itemId: "assistant-msg-1", |
| 80 | + textDelta: "hi", |
| 81 | + payload: { |
| 82 | + itemId: "assistant-msg-1", |
| 83 | + delta: "hi", |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + service.ingestProviderEvent({ |
| 88 | + ...baseEvent, |
| 89 | + id: "evt-3", |
| 90 | + createdAt: iso(30), |
| 91 | + method: "item/completed", |
| 92 | + payload: { |
| 93 | + item: { |
| 94 | + type: "agentMessage", |
| 95 | + id: "assistant-msg-1", |
| 96 | + text: "hi there", |
| 97 | + }, |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + service.ingestProviderEvent({ |
| 102 | + ...baseEvent, |
| 103 | + id: "evt-4", |
| 104 | + createdAt: iso(40), |
| 105 | + method: "turn/completed", |
| 106 | + turnId: "turn-1", |
| 107 | + payload: { |
| 108 | + turn: { |
| 109 | + id: "turn-1", |
| 110 | + status: "completed", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }); |
| 114 | + |
| 115 | + const snapshot = service.loadSnapshot(); |
| 116 | + expect(snapshot.projects).toHaveLength(1); |
| 117 | + expect(snapshot.threads).toHaveLength(1); |
| 118 | + const thread = snapshot.threads[0]; |
| 119 | + expect(thread?.messages.map((message) => message.id)).toEqual([ |
| 120 | + "msg-user-1", |
| 121 | + "assistant-msg-1", |
| 122 | + ]); |
| 123 | + expect(thread?.messages[1]?.text).toBe("hi there"); |
| 124 | + expect(thread?.turnDiffSummaries[0]?.turnId).toBe("turn-1"); |
| 125 | + |
| 126 | + const catchUp = service.catchUp({ afterSeq: 0 }); |
| 127 | + expect(catchUp.events.length).toBeGreaterThan(0); |
| 128 | + expect(catchUp.events.every((event, index, events) => event.seq === events[index]?.seq)).toBe( |
| 129 | + true, |
| 130 | + ); |
| 131 | + expect(catchUp.events.some((event) => event.eventType === "project.upsert")).toBe(true); |
| 132 | + expect(catchUp.events.some((event) => event.eventType === "thread.upsert")).toBe(true); |
| 133 | + expect(catchUp.events.some((event) => event.eventType === "message.upsert")).toBe(true); |
| 134 | + |
| 135 | + service.close(); |
| 136 | + }); |
| 137 | + |
| 138 | + it("stores turn diff summaries parsed from checkpoint diffs", () => { |
| 139 | + const stateDir = makeTempDir("t3code-persistence-diff-state-"); |
| 140 | + const projectDir = makeTempDir("t3code-persistence-diff-project-"); |
| 141 | + const service = new PersistenceService({ |
| 142 | + dbPath: path.join(stateDir, "state.sqlite"), |
| 143 | + }); |
| 144 | + |
| 145 | + const project = service.addProject({ cwd: projectDir }).project; |
| 146 | + const thread = service.createThread({ |
| 147 | + projectId: project.id, |
| 148 | + title: "Thread 1", |
| 149 | + model: "gpt-5.3-codex", |
| 150 | + }).thread; |
| 151 | + service.bindSessionToThread("sess-1", thread.id, "runtime-thread-1"); |
| 152 | + |
| 153 | + service.persistTurnDiffSummaryFromCheckpoint({ |
| 154 | + sessionId: "sess-1", |
| 155 | + runtimeThreadId: "runtime-thread-1", |
| 156 | + turnId: "turn-1", |
| 157 | + checkpointTurnCount: 1, |
| 158 | + completedAt: "2026-02-19T00:00:00.000Z", |
| 159 | + status: "completed", |
| 160 | + diff: [ |
| 161 | + "diff --git a/src/app.ts b/src/app.ts", |
| 162 | + "index 1111111..2222222 100644", |
| 163 | + "--- a/src/app.ts", |
| 164 | + "+++ b/src/app.ts", |
| 165 | + "@@ -1,1 +1,2 @@", |
| 166 | + "-console.log('a')", |
| 167 | + "+console.log('b')", |
| 168 | + "+console.log('c')", |
| 169 | + ].join("\n"), |
| 170 | + }); |
| 171 | + |
| 172 | + const snapshot = service.loadSnapshot(); |
| 173 | + const summary = snapshot.threads[0]?.turnDiffSummaries[0]; |
| 174 | + expect(summary?.turnId).toBe("turn-1"); |
| 175 | + expect(summary?.checkpointTurnCount).toBe(1); |
| 176 | + expect(summary?.files[0]?.path).toBe("src/app.ts"); |
| 177 | + expect(summary?.files[0]?.additions).toBe(2); |
| 178 | + expect(summary?.files[0]?.deletions).toBe(1); |
| 179 | + |
| 180 | + service.close(); |
| 181 | + }); |
| 182 | + |
| 183 | + it("imports legacy renderer state once and reports idempotent re-imports", () => { |
| 184 | + const stateDir = makeTempDir("t3code-persistence-legacy-state-"); |
| 185 | + const projectDir = makeTempDir("t3code-persistence-legacy-project-"); |
| 186 | + const service = new PersistenceService({ |
| 187 | + dbPath: path.join(stateDir, "state.sqlite"), |
| 188 | + }); |
| 189 | + |
| 190 | + const firstImport = service.importLegacyRendererState({ |
| 191 | + projects: [ |
| 192 | + { |
| 193 | + id: "project-legacy-1", |
| 194 | + name: "legacy", |
| 195 | + cwd: projectDir, |
| 196 | + scripts: [], |
| 197 | + }, |
| 198 | + ], |
| 199 | + threads: [ |
| 200 | + { |
| 201 | + id: "thread-legacy-1", |
| 202 | + projectId: "project-legacy-1", |
| 203 | + title: "Imported", |
| 204 | + model: "gpt-5.3-codex", |
| 205 | + createdAt: "2026-02-19T00:00:00.000Z", |
| 206 | + messages: [ |
| 207 | + { |
| 208 | + id: "msg-legacy-1", |
| 209 | + role: "user", |
| 210 | + text: "legacy message", |
| 211 | + createdAt: "2026-02-19T00:00:01.000Z", |
| 212 | + streaming: false, |
| 213 | + }, |
| 214 | + ], |
| 215 | + }, |
| 216 | + ], |
| 217 | + }); |
| 218 | + expect(firstImport).toEqual({ |
| 219 | + imported: true, |
| 220 | + alreadyImported: false, |
| 221 | + }); |
| 222 | + |
| 223 | + const secondImport = service.importLegacyRendererState({ |
| 224 | + projects: [], |
| 225 | + threads: [], |
| 226 | + }); |
| 227 | + expect(secondImport).toEqual({ |
| 228 | + imported: false, |
| 229 | + alreadyImported: true, |
| 230 | + }); |
| 231 | + |
| 232 | + const snapshot = service.loadSnapshot(); |
| 233 | + expect(snapshot.projects).toHaveLength(1); |
| 234 | + expect(snapshot.threads).toHaveLength(1); |
| 235 | + expect(snapshot.threads[0]?.messages).toHaveLength(1); |
| 236 | + |
| 237 | + service.close(); |
| 238 | + }); |
| 239 | +}); |
0 commit comments