From 89e2c73f1f12ae137f80eae01bf84f41130b5724 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Feb 2026 10:05:46 +0000 Subject: [PATCH] Add shadow engine parity integration coverage Co-authored-by: Julius Marminge --- apps/server/src/stateSyncEngineShadow.test.ts | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/apps/server/src/stateSyncEngineShadow.test.ts b/apps/server/src/stateSyncEngineShadow.test.ts index 2f3f6939be..4fc45e9abf 100644 --- a/apps/server/src/stateSyncEngineShadow.test.ts +++ b/apps/server/src/stateSyncEngineShadow.test.ts @@ -4,6 +4,8 @@ import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import type { StateEvent } from "@t3tools/contracts"; +import { diffStateSnapshots } from "./livestore/parity"; +import { LiveStoreStateMirror } from "./livestore/liveStoreEngine"; import { PersistenceService } from "./persistenceService"; import { LegacyStateSyncEngine } from "./stateSyncEngineLegacy"; import type { StateEventMirror } from "./stateSyncEngineShadow"; @@ -17,6 +19,21 @@ function makeTempDir(prefix: string): string { return dir; } +async function waitForParity( + getExpected: () => ReturnType, + getMirrored: () => ReturnType, +): Promise { + const maxAttempts = 30; + for (let attempt = 0; attempt < maxAttempts; attempt += 1) { + if (diffStateSnapshots(getExpected(), getMirrored()).length === 0) { + return; + } + await new Promise((resolve) => setTimeout(resolve, 15)); + } + + expect(diffStateSnapshots(getExpected(), getMirrored())).toEqual([]); +} + class CapturingMirror implements StateEventMirror { readonly events: StateEvent[] = []; disposeCalls = 0; @@ -125,4 +142,39 @@ describe("ShadowStateSyncEngine", () => { service.close(); } }); + + it("keeps LiveStore mirror snapshot in parity with delegate writes", async () => { + const stateDir = makeTempDir("t3code-shadow-parity-state-"); + const projectDir = makeTempDir("t3code-shadow-parity-project-"); + const service = new PersistenceService({ + dbPath: path.join(stateDir, "state.sqlite"), + }); + const legacy = new LegacyStateSyncEngine({ persistenceService: service }); + const mirror = new LiveStoreStateMirror({ storeId: "shadow-parity-engine-test" }); + const shadow = new ShadowStateSyncEngine({ + delegate: legacy, + mirror, + }); + + try { + const project = shadow.addProject({ cwd: projectDir }).project; + const thread = shadow.createThread({ + projectId: project.id, + title: "Parity thread", + }).thread; + service.bindSessionToThread("parity-session", thread.id, "runtime-thread-parity"); + service.persistUserMessageForTurn({ + sessionId: "parity-session", + clientMessageId: "parity-message-1", + clientMessageText: "hello parity", + input: "hello parity", + attachments: [], + }); + + await waitForParity(() => legacy.loadSnapshot(), () => mirror.debugReadSnapshot()); + } finally { + shadow.close(); + service.close(); + } + }); });