From c777aae03dd44d2cdc01080ff87a62492440f247 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Feb 2026 12:47:11 +0000 Subject: [PATCH] Cover mirror bootstrap replay against real legacy state Co-authored-by: Julius Marminge --- .../src/livestore/mirrorBootstrap.test.ts | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/apps/server/src/livestore/mirrorBootstrap.test.ts b/apps/server/src/livestore/mirrorBootstrap.test.ts index 384e9b8873..1a87698029 100644 --- a/apps/server/src/livestore/mirrorBootstrap.test.ts +++ b/apps/server/src/livestore/mirrorBootstrap.test.ts @@ -1,6 +1,28 @@ -import { describe, expect, it, vi } from "vitest"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +import { afterEach, describe, expect, it, vi } from "vitest"; import { createLogger } from "../logger"; +import { PersistenceService } from "../persistenceService"; +import { LegacyStateSyncEngine } from "../stateSyncEngineLegacy"; import { bootstrapMirrorFromCatchUp } from "./mirrorBootstrap"; +import { LiveStoreStateMirror } from "./liveStoreEngine"; +import { diffStateSnapshots } from "./parity"; + +const tempDirs: string[] = []; + +function makeTempDir(prefix: string): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); + tempDirs.push(dir); + return dir; +} + +afterEach(() => { + for (const dir of tempDirs.splice(0, tempDirs.length)) { + fs.rmSync(dir, { recursive: true, force: true }); + } +}); describe("bootstrapMirrorFromCatchUp", () => { it("mirrors catch-up history into the mirror sink", async () => { @@ -141,4 +163,46 @@ describe("bootstrapMirrorFromCatchUp", () => { complete: false, }); }); + + it("replays persisted legacy catch-up history into a real LiveStore mirror", async () => { + const stateDir = makeTempDir("t3code-bootstrap-replay-state-"); + const projectDir = makeTempDir("t3code-bootstrap-replay-project-"); + const service = new PersistenceService({ + dbPath: path.join(stateDir, "state.sqlite"), + }); + const legacy = new LegacyStateSyncEngine({ persistenceService: service }); + const mirror = new LiveStoreStateMirror({ storeId: "bootstrap-replay-parity-test" }); + + try { + const project = legacy.addProject({ cwd: projectDir }).project; + const thread = legacy.createThread({ + projectId: project.id, + title: "Bootstrap replay parity thread", + }).thread; + service.bindSessionToThread("bootstrap-replay-session", thread.id, "runtime-thread-bootstrap"); + service.persistUserMessageForTurn({ + sessionId: "bootstrap-replay-session", + clientMessageId: "bootstrap-message-1", + clientMessageText: "bootstrap replay parity", + input: "bootstrap replay parity", + attachments: [], + }); + + const expectedSnapshot = legacy.loadSnapshot(); + const bootstrapResult = await bootstrapMirrorFromCatchUp({ + source: legacy, + mirror, + logger: createLogger("mirror-bootstrap-test"), + failOnError: true, + }); + + expect(bootstrapResult.complete).toBe(true); + expect(bootstrapResult.lastStateSeq).toBe(expectedSnapshot.lastStateSeq); + expect(bootstrapResult.mirroredCount).toBeGreaterThan(0); + expect(diffStateSnapshots(expectedSnapshot, mirror.debugReadSnapshot())).toEqual([]); + } finally { + await mirror.dispose(); + service.close(); + } + }); });