Add snapshot parity utility for LiveStore migration

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Cursor AgentandJulius Marminge committed 2026-02-20 09:32:02 +00:00
1 parent 262f57d84a
commit 8d0af1f9de
2 files changed
+245

No files matched your search

+149
View File
@@ -0,0 +1,149 @@
import { describe, expect, it } from "vitest";
import type { StateBootstrapResult } from "@t3tools/contracts";
import { diffStateSnapshots, isStateSnapshotInParity } from "./parity";
function makeSnapshot(overrides: Partial<StateBootstrapResult> = {}): StateBootstrapResult {
return {
projects: [],
threads: [],
lastStateSeq: 0,
...overrides,
};
}
describe("diffStateSnapshots", () => {
it("treats snapshots with different ordering as parity-equal", () => {
const projectA = {
id: "project-a",
cwd: "/workspace/a",
name: "A",
scripts: [],
createdAt: "2026-02-20T00:00:00.000Z",
updatedAt: "2026-02-20T00:00:00.000Z",
};
const projectB = {
id: "project-b",
cwd: "/workspace/b",
name: "B",
scripts: [],
createdAt: "2026-02-20T00:00:00.000Z",
updatedAt: "2026-02-20T00:00:00.000Z",
};
const threadA = {
id: "thread-a",
codexThreadId: null,
projectId: "project-a",
title: "Thread A",
model: "gpt-5.3-codex",
terminalOpen: false,
terminalHeight: 280,
terminalIds: ["default"],
runningTerminalIds: [],
activeTerminalId: "default",
terminalGroups: [{ id: "group-default", terminalIds: ["default"] }],
activeTerminalGroupId: "group-default",
createdAt: "2026-02-20T00:00:00.000Z",
updatedAt: "2026-02-20T00:00:00.000Z",
branch: null,
worktreePath: null,
turnDiffSummaries: [
{
turnId: "turn-2",
completedAt: "2026-02-20T00:00:02.000Z",
files: [],
},
{
turnId: "turn-1",
completedAt: "2026-02-20T00:00:01.000Z",
files: [],
},
],
messages: [
{
id: "msg-2",
threadId: "thread-a",
role: "assistant" as const,
text: "hello",
createdAt: "2026-02-20T00:00:02.000Z",
updatedAt: "2026-02-20T00:00:02.000Z",
streaming: false,
},
{
id: "msg-1",
threadId: "thread-a",
role: "user" as const,
text: "hi",
createdAt: "2026-02-20T00:00:01.000Z",
updatedAt: "2026-02-20T00:00:01.000Z",
streaming: false,
},
],
};
const expected = makeSnapshot({
projects: [projectA, projectB],
threads: [threadA],
lastStateSeq: 10,
});
const actual = makeSnapshot({
projects: [projectB, projectA],
threads: [
{
...threadA,
turnDiffSummaries: [...threadA.turnDiffSummaries].toReversed(),
messages: [...threadA.messages].toReversed(),
},
],
lastStateSeq: 10,
});
expect(diffStateSnapshots(expected, actual)).toEqual([]);
expect(isStateSnapshotInParity(expected, actual)).toBe(true);
});
it("reports path-qualified diffs when snapshots diverge", () => {
const expected = makeSnapshot({
projects: [
{
id: "project-a",
cwd: "/workspace/a",
name: "Project A",
scripts: [],
createdAt: "2026-02-20T00:00:00.000Z",
updatedAt: "2026-02-20T00:00:00.000Z",
},
],
lastStateSeq: 5,
});
const actual = makeSnapshot({
projects: [
{
id: "project-a",
cwd: "/workspace/a",
name: "Project A renamed",
scripts: [],
createdAt: "2026-02-20T00:00:00.000Z",
updatedAt: "2026-02-20T00:00:00.000Z",
},
],
lastStateSeq: 7,
});
const diffs = diffStateSnapshots(expected, actual);
expect(diffs).toEqual(
expect.arrayContaining([
expect.objectContaining({
path: "$.lastStateSeq",
expected: 5,
actual: 7,
}),
expect.objectContaining({
path: "$.projects[0].name",
expected: "Project A",
actual: "Project A renamed",
}),
]),
);
expect(isStateSnapshotInParity(expected, actual)).toBe(false);
});
});
+96
View File
@@ -0,0 +1,96 @@
import type { StateBootstrapResult } from "@t3tools/contracts";
export interface SnapshotParityDiff {
path: string;
expected: unknown;
actual: unknown;
}
function sortById<T extends { id: string }>(items: readonly T[]): T[] {
return [...items].sort((a, b) => a.id.localeCompare(b.id));
}
function normalizeSnapshot(snapshot: StateBootstrapResult): StateBootstrapResult {
return {
...snapshot,
projects: sortById(snapshot.projects),
threads: sortById(snapshot.threads).map((thread) => ({
...thread,
messages: [...thread.messages].sort((a, b) => {
if (a.id === b.id) {
return a.createdAt.localeCompare(b.createdAt);
}
return a.id.localeCompare(b.id);
}),
turnDiffSummaries: [...thread.turnDiffSummaries].sort((a, b) =>
a.turnId.localeCompare(b.turnId),
),
})),
};
}
function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function collectDiffs(
expected: unknown,
actual: unknown,
path: string,
diffs: SnapshotParityDiff[],
): void {
if (Object.is(expected, actual)) {
return;
}
if (Array.isArray(expected) || Array.isArray(actual)) {
if (!Array.isArray(expected) || !Array.isArray(actual)) {
diffs.push({ path, expected, actual });
return;
}
if (expected.length !== actual.length) {
diffs.push({
path: `${path}.length`,
expected: expected.length,
actual: actual.length,
});
return;
}
for (let index = 0; index < expected.length; index += 1) {
collectDiffs(expected[index], actual[index], `${path}[${index}]`, diffs);
}
return;
}
if (isRecord(expected) || isRecord(actual)) {
if (!isRecord(expected) || !isRecord(actual)) {
diffs.push({ path, expected, actual });
return;
}
const keys = new Set([...Object.keys(expected), ...Object.keys(actual)]);
for (const key of [...keys].sort((a, b) => a.localeCompare(b))) {
collectDiffs(expected[key], actual[key], `${path}.${key}`, diffs);
}
return;
}
diffs.push({ path, expected, actual });
}
export function diffStateSnapshots(
expectedSnapshot: StateBootstrapResult,
actualSnapshot: StateBootstrapResult,
): SnapshotParityDiff[] {
const expected = normalizeSnapshot(expectedSnapshot);
const actual = normalizeSnapshot(actualSnapshot);
const diffs: SnapshotParityDiff[] = [];
collectDiffs(expected, actual, "$", diffs);
return diffs;
}
export function isStateSnapshotInParity(
expectedSnapshot: StateBootstrapResult,
actualSnapshot: StateBootstrapResult,
): boolean {
return diffStateSnapshots(expectedSnapshot, actualSnapshot).length === 0;
}