Expand shadow parity coverage in persistence and ws tests
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
89e2c73f1f
commit
fc14fdb569
2 files changed
+169
No files matched your search
@@ -5,7 +5,11 @@ import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import type { ProviderEvent } from "@t3tools/contracts";
|
||||
import { LiveStoreStateMirror } from "./livestore/liveStoreEngine";
|
||||
import { diffStateSnapshots } from "./livestore/parity";
|
||||
import { PersistenceService } from "./persistenceService";
|
||||
import { LegacyStateSyncEngine } from "./stateSyncEngineLegacy";
|
||||
import { ShadowStateSyncEngine } from "./stateSyncEngineShadow";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
const ISO_BASE_MS = Date.parse("2026-02-19T00:00:00.000Z");
|
||||
@@ -20,6 +24,20 @@ function iso(offsetMs = 0): string {
|
||||
return new Date(ISO_BASE_MS + offsetMs).toISOString();
|
||||
}
|
||||
|
||||
async function waitForMirrorParity(
|
||||
readExpected: () => ReturnType<LegacyStateSyncEngine["loadSnapshot"]>,
|
||||
readMirror: () => ReturnType<LiveStoreStateMirror["debugReadSnapshot"]>,
|
||||
): Promise<void> {
|
||||
const maxAttempts = 40;
|
||||
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
||||
if (diffStateSnapshots(readExpected(), readMirror()).length === 0) {
|
||||
return;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 15));
|
||||
}
|
||||
expect(diffStateSnapshots(readExpected(), readMirror())).toEqual([]);
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tempDirs.splice(0, tempDirs.length)) {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
@@ -446,4 +464,78 @@ describe("PersistenceService", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("maintains shadow mirror parity across checkpoint revert effects", async () => {
|
||||
const stateDir = makeTempDir("t3code-persistence-shadow-parity-state-");
|
||||
const projectDir = makeTempDir("t3code-persistence-shadow-parity-project-");
|
||||
const service = new PersistenceService({
|
||||
dbPath: path.join(stateDir, "state.sqlite"),
|
||||
});
|
||||
const legacy = new LegacyStateSyncEngine({ persistenceService: service });
|
||||
const mirror = new LiveStoreStateMirror({ storeId: "persistence-shadow-parity-test" });
|
||||
const shadow = new ShadowStateSyncEngine({
|
||||
delegate: legacy,
|
||||
mirror,
|
||||
});
|
||||
|
||||
try {
|
||||
const project = shadow.addProject({ cwd: projectDir }).project;
|
||||
const thread = shadow.createThread({
|
||||
projectId: project.id,
|
||||
title: "checkpoint parity thread",
|
||||
model: "gpt-5.3-codex",
|
||||
}).thread;
|
||||
service.bindSessionToThread("sess-shadow", thread.id, "runtime-thread-shadow");
|
||||
|
||||
service.persistUserMessageForTurn({
|
||||
sessionId: "sess-shadow",
|
||||
clientMessageId: "msg-1",
|
||||
clientMessageText: "first",
|
||||
input: "first",
|
||||
attachments: [],
|
||||
});
|
||||
service.persistUserMessageForTurn({
|
||||
sessionId: "sess-shadow",
|
||||
clientMessageId: "msg-2",
|
||||
clientMessageText: "second",
|
||||
input: "second",
|
||||
attachments: [],
|
||||
});
|
||||
|
||||
service.persistTurnDiffSummaryFromCheckpoint({
|
||||
sessionId: "sess-shadow",
|
||||
runtimeThreadId: "runtime-thread-shadow",
|
||||
turnId: "turn-1",
|
||||
checkpointTurnCount: 1,
|
||||
completedAt: iso(100),
|
||||
diff: "",
|
||||
});
|
||||
service.persistTurnDiffSummaryFromCheckpoint({
|
||||
sessionId: "sess-shadow",
|
||||
runtimeThreadId: "runtime-thread-shadow",
|
||||
turnId: "turn-2",
|
||||
checkpointTurnCount: 2,
|
||||
completedAt: iso(200),
|
||||
diff: "",
|
||||
});
|
||||
|
||||
shadow.applyCheckpointRevert({
|
||||
sessionId: "sess-shadow",
|
||||
runtimeThreadId: "runtime-thread-shadow",
|
||||
turnCount: 1,
|
||||
messageCount: 1,
|
||||
});
|
||||
|
||||
await waitForMirrorParity(() => legacy.loadSnapshot(), () => mirror.debugReadSnapshot());
|
||||
|
||||
const mirrored = mirror.debugReadSnapshot();
|
||||
expect(mirrored.threads[0]?.messages.map((message) => message.id)).toEqual(["msg-1"]);
|
||||
expect(mirrored.threads[0]?.turnDiffSummaries.map((summary) => summary.turnId)).toEqual([
|
||||
"turn-1",
|
||||
]);
|
||||
expect(diffStateSnapshots(legacy.loadSnapshot(), mirrored)).toEqual([]);
|
||||
} finally {
|
||||
shadow.close();
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
@@ -44,6 +44,10 @@ import type { TerminalManager } from "./terminalManager";
|
||||
import { PersistenceService } from "./persistenceService";
|
||||
import { ProviderManager } from "./providerManager";
|
||||
import type { ApplyCheckpointRevertInput, StateSyncEngine } from "./stateSyncEngine";
|
||||
import { LegacyStateSyncEngine } from "./stateSyncEngineLegacy";
|
||||
import { ShadowStateSyncEngine } from "./stateSyncEngineShadow";
|
||||
import { LiveStoreStateMirror } from "./livestore/liveStoreEngine";
|
||||
import { diffStateSnapshots } from "./livestore/parity";
|
||||
|
||||
interface PendingMessages {
|
||||
queue: unknown[];
|
||||
@@ -383,6 +387,22 @@ async function waitForPush(ws: WebSocket, channel: string): Promise<WsPush> {
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForCondition(
|
||||
predicate: () => boolean,
|
||||
options: { timeoutMs?: number; intervalMs?: number } = {},
|
||||
): Promise<void> {
|
||||
const timeoutMs = options.timeoutMs ?? 1500;
|
||||
const intervalMs = options.intervalMs ?? 20;
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
if (predicate()) {
|
||||
return;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
||||
}
|
||||
expect(predicate()).toBe(true);
|
||||
}
|
||||
|
||||
const DEFAULT_RESOLVED_KEYBINDINGS = compileKeybindings([...DEFAULT_KEYBINDINGS]);
|
||||
|
||||
function mergeWithDefaultsForTest(custom: KeybindingsConfig): ResolvedKeybindingsConfig {
|
||||
@@ -970,6 +990,63 @@ describe("WebSocket Server", () => {
|
||||
expect(pushed.data).toEqual(mirroredStateEvent);
|
||||
});
|
||||
|
||||
it("keeps a shadow sync engine mirror in parity for websocket mutations", async () => {
|
||||
const stateDir = makeTempDir("t3code-ws-shadow-engine-state-");
|
||||
const projectCwd = makeTempDir("t3code-ws-shadow-engine-project-");
|
||||
const persistenceService = new PersistenceService({
|
||||
dbPath: path.join(stateDir, "state.sqlite"),
|
||||
legacyProjectsJsonPath: path.join(stateDir, "projects.json"),
|
||||
});
|
||||
const legacy = new LegacyStateSyncEngine({ persistenceService });
|
||||
const mirror = new LiveStoreStateMirror({ storeId: "ws-shadow-engine-parity-test" });
|
||||
const shadow = new ShadowStateSyncEngine({
|
||||
delegate: legacy,
|
||||
mirror,
|
||||
});
|
||||
|
||||
try {
|
||||
server = createTestServer({
|
||||
cwd: "/test",
|
||||
stateSyncEngine: shadow,
|
||||
});
|
||||
await server.start();
|
||||
const addr = server.httpServer.address();
|
||||
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
|
||||
|
||||
const ws = await connectWs(port);
|
||||
connections.push(ws);
|
||||
await waitForMessage(ws);
|
||||
|
||||
const addedProject = await sendRequest(ws, WS_METHODS.projectsAdd, { cwd: projectCwd });
|
||||
expect(addedProject.error).toBeUndefined();
|
||||
const projectId = (addedProject.result as { project: { id: string } }).project.id;
|
||||
|
||||
const createdThread = await sendRequest(ws, WS_METHODS.threadsCreate, {
|
||||
projectId,
|
||||
title: "Shadow parity thread",
|
||||
model: "gpt-5.3-codex",
|
||||
});
|
||||
expect(createdThread.error).toBeUndefined();
|
||||
const threadId = (createdThread.result as { thread: { id: string } }).thread.id;
|
||||
|
||||
const updatedThread = await sendRequest(ws, WS_METHODS.threadsUpdateTitle, {
|
||||
threadId,
|
||||
title: "Shadow parity thread updated",
|
||||
});
|
||||
expect(updatedThread.error).toBeUndefined();
|
||||
|
||||
await waitForCondition(() => {
|
||||
const expected = legacy.loadSnapshot();
|
||||
const mirrored = mirror.debugReadSnapshot();
|
||||
return diffStateSnapshots(expected, mirrored).length === 0;
|
||||
});
|
||||
|
||||
expect(diffStateSnapshots(legacy.loadSnapshot(), mirror.debugReadSnapshot())).toEqual([]);
|
||||
} finally {
|
||||
shadow.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("broadcasts ordered state.event pushes", async () => {
|
||||
const stateDir = makeTempDir("t3code-ws-state-events-");
|
||||
const projectCwd = makeTempDir("t3code-ws-state-events-project-");
|
||||
|
||||
Reference in new issue
Block a user