Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestrationV2BoundedSnapshot.test.ts

142 lines4.9 KB

1import {
2 RunId,
3 ThreadId,
4 TurnItemId,
5 type OrchestrationV2ProjectedTurnItem,
6 type OrchestrationV2ThreadProjection,
7 type OrchestrationV2TurnItem,
8} from "@t3tools/contracts";
9import { describe, expect, it } from "vite-plus/test";
10​
11import {
12 boundedSnapshotProjection,
13 omitLocalVisibleTurnItems,
14 restoreLocalVisibleTurnItems,
15} from "./orchestrationV2BoundedSnapshot.ts";
16​
17const PARENT = ThreadId.make("thread:compact-parent");
18const CHILD = ThreadId.make("thread:compact-child");
19const RUN = RunId.make("run:compact");
20​
21function item(
22 threadId: ThreadId,
23 id: string,
24 ordinal: number,
25 type: "command_execution" | "run_interrupt_request" | "fork" = "command_execution",
26): OrchestrationV2TurnItem {
27 return {
28 id: TurnItemId.make(id),
29 type,
30 threadId,
31 runId: type === "command_execution" ? null : RUN,
32 nodeId: null,
33 providerThreadId: null,
34 providerTurnId: null,
35 nativeItemRef: null,
36 parentItemId: null,
37 ordinal,
38 status: "completed",
39 title: id,
40 input: id,
41 exitCode: 0,
42 startedAt: null,
43 completedAt: null,
44 updatedAt: "2026-10-01T00:00:00.000Z",
45 } as unknown as OrchestrationV2TurnItem;
47​
48function row(
49 visibility: OrchestrationV2ProjectedTurnItem["visibility"],
50 sourceThreadId: ThreadId,
51 value: OrchestrationV2TurnItem,
52): Omit<OrchestrationV2ProjectedTurnItem, "position"> {
53 return { visibility, sourceThreadId, sourceItemId: value.id, item: value };
55​
56function projection(input: {
57 readonly visible: ReadonlyArray<Omit<OrchestrationV2ProjectedTurnItem, "position">>;
58 readonly turnItems: ReadonlyArray<OrchestrationV2TurnItem>;
59}): OrchestrationV2ThreadProjection {
60 return {
61 thread: { id: CHILD },
62 turnItems: [...input.turnItems],
63 visibleTurnItems: input.visible.map((value, position) => ({ ...value, position })),
64 } as unknown as OrchestrationV2ThreadProjection;
66​
67function forkWindow() {
68 const inherited = [item(PARENT, "parent-1", 1), item(PARENT, "parent-2", 2)];
69 const marker = item(PARENT, "fork-marker", 3, "fork");
70 const local = [item(CHILD, "child-1", 1), item(CHILD, "child-2", 2), item(CHILD, "child-3", 3)];
71 const retainedRequest = item(CHILD, "interrupt-request", 0, "run_interrupt_request");
72 return {
73 local,
74 retainedRequest,
75 projection: projection({
76 visible: [
77 ...inherited.map((value) => row("inherited", PARENT, value)),
78 row("synthetic", PARENT, marker),
79 ...local.map((value) => row("local", CHILD, value)),
80 ],
81 turnItems: [...local, retainedRequest],
82 }),
83 };
85​
86describe("compact bounded snapshot turnItems", () => {
87 it("omits only local visible items and restores the exact list and order", () => {
88 const { projection: full, local, retainedRequest } = forkWindow();
89 const compact = omitLocalVisibleTurnItems(full);
90​
91 expect(compact).not.toBeNull();
92 expect(compact!.turnItems).toEqual([retainedRequest]);
93 expect(compact!.visibleTurnItems).toBe(full.visibleTurnItems);
94​
95 const restored = restoreLocalVisibleTurnItems(compact!);
96 expect(restored.turnItems).toEqual([...local, retainedRequest]);
97 expect(restored).toEqual(full);
98 });
99​
100 it("round-trips through JSON as a separately decoded client would see it", () => {
101 const { projection: full } = forkWindow();
102 const wire = JSON.parse(JSON.stringify(omitLocalVisibleTurnItems(full)));
103 expect(
104 boundedSnapshotProjection({ projection: wire, turnItemsOmitLocalVisible: true }),
105 ).toEqual(JSON.parse(JSON.stringify(full)));
106 });
107​
108 it("treats rows sourced from the thread itself as local", () => {
109 const own = item(CHILD, "own", 1);
110 const full = projection({ visible: [row("inherited", CHILD, own)], turnItems: [own] });
111 const compact = omitLocalVisibleTurnItems(full);
112 expect(compact?.turnItems).toEqual([]);
113 expect(restoreLocalVisibleTurnItems(compact!)).toEqual(full);
114 });
115​
116 it("refuses shapes it cannot restore exactly", () => {
117 const [first, second] = [item(CHILD, "a", 1), item(CHILD, "b", 2)];
118 const visible = [row("local", CHILD, first), row("local", CHILD, second)];
119 expect(omitLocalVisibleTurnItems(projection({ visible, turnItems: [second, first] }))).toBe(
120 null,
121 );
122 expect(
123 omitLocalVisibleTurnItems(projection({ visible, turnItems: [{ ...first }, second] })),
124 ).toBe(null);
125 expect(omitLocalVisibleTurnItems(projection({ visible, turnItems: [first] }))).toBe(null);
126 });
127​
128 it("sends nothing compact when no local rows are visible", () => {
129 const inherited = item(PARENT, "parent", 1);
130 const request = item(CHILD, "interrupt-request", 0, "run_interrupt_request");
131 expect(
132 omitLocalVisibleTurnItems(
133 projection({ visible: [row("inherited", PARENT, inherited)], turnItems: [request] }),
134 ),
135 ).toBe(null);
136 });
137​
138 it("leaves unmarked snapshots untouched", () => {
139 const { projection: full } = forkWindow();
140 expect(boundedSnapshotProjection({ projection: full })).toBe(full);
141 });
142});