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

shellReducer.test.ts

183 lines5.3 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { ProjectId, ProviderInstanceId, ThreadId } from "@v12/contracts";
4import type { OrchestrationShellSnapshot, OrchestrationShellStreamEvent } from "@v12/contracts";
5​
6import { applyShellStreamEvent } from "./shellReducer.ts";
7​
8const baseSnapshot: OrchestrationShellSnapshot = {
9 snapshotSequence: 0,
10 projects: [],
11 threads: [],
12 updatedAt: "2026-04-01T00:00:00.000Z",
13};
14​
15const stubProject = {
16 id: ProjectId.make("project-1"),
17 title: "Test Project",
18 workspaceRoot: "/workspace/test",
19 repositoryIdentity: null,
20 defaultModelSelection: null,
21 scripts: [],
22 createdAt: "2026-04-01T00:00:00.000Z",
23 updatedAt: "2026-04-01T00:00:00.000Z",
24} as const;
25​
26const stubThread = {
27 id: ThreadId.make("thread-1"),
28 projectId: ProjectId.make("project-1"),
29 title: "Test Thread",
30 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
31 runtimeMode: "full-access" as const,
32 interactionMode: "default" as const,
33 branch: null,
34 worktreePath: null,
35 latestTurn: null,
36 createdAt: "2026-04-01T00:00:00.000Z",
37 updatedAt: "2026-04-01T00:00:00.000Z",
38 archivedAt: null,
39 latestUserMessageAt: null,
40 hasPendingApprovals: false,
41 hasPendingUserInput: false,
42 hasActionableProposedPlan: false,
43 session: null,
44} as const;
45​
46describe("applyShellStreamEvent", () => {
47 it("ignores stale project upserts without mutating the snapshot", () => {
48 const snapshotWithProject: OrchestrationShellSnapshot = {
49 ...baseSnapshot,
50 snapshotSequence: 4,
51 projects: [stubProject],
52 };
53​
54 for (const sequence of [3, 4]) {
55 const next = applyShellStreamEvent(snapshotWithProject, {
56 kind: "project-upserted",
57 sequence,
58 project: { ...stubProject, title: "Stale Title" },
59 });
60​
61 expect(next).toBe(snapshotWithProject);
62 expect(next.snapshotSequence).toBe(4);
63 expect(next.projects[0]?.title).toBe("Test Project");
64 }
65 });
66​
67 describe("project-upserted", () => {
68 it("adds a new project", () => {
69 const event: OrchestrationShellStreamEvent = {
70 kind: "project-upserted",
71 sequence: 1,
72 project: stubProject,
73 };
74​
75 const next = applyShellStreamEvent(baseSnapshot, event);
76​
77 expect(next.projects).toHaveLength(1);
78 expect(next.projects[0]?.id).toBe("project-1");
79 expect(next.snapshotSequence).toBe(1);
80 });
81​
82 it("updates an existing project", () => {
83 const snapshotWithProject: OrchestrationShellSnapshot = {
84 ...baseSnapshot,
85 projects: [stubProject],
86 };
87​
88 const updatedProject = { ...stubProject, title: "Updated Title" };
89 const event: OrchestrationShellStreamEvent = {
90 kind: "project-upserted",
91 sequence: 2,
92 project: updatedProject,
93 };
94​
95 const next = applyShellStreamEvent(snapshotWithProject, event);
96​
97 expect(next.projects).toHaveLength(1);
98 expect(next.projects[0]?.title).toBe("Updated Title");
99 expect(next.snapshotSequence).toBe(2);
100 });
101 });
102​
103 describe("project-removed", () => {
104 it("removes a project by id", () => {
105 const snapshotWithProject: OrchestrationShellSnapshot = {
106 ...baseSnapshot,
107 projects: [stubProject],
108 };
109​
110 const event: OrchestrationShellStreamEvent = {
111 kind: "project-removed",
112 sequence: 3,
113 projectId: ProjectId.make("project-1"),
114 };
115​
116 const next = applyShellStreamEvent(snapshotWithProject, event);
117​
118 expect(next.projects).toHaveLength(0);
119 expect(next.snapshotSequence).toBe(3);
120 });
121 });
122​
123 describe("thread-upserted", () => {
124 it("adds a new thread", () => {
125 const event: OrchestrationShellStreamEvent = {
126 kind: "thread-upserted",
127 sequence: 4,
128 thread: stubThread,
129 };
130​
131 const next = applyShellStreamEvent(baseSnapshot, event);
132​
133 expect(next.threads).toHaveLength(1);
134 expect(next.threads[0]?.id).toBe("thread-1");
135 expect(next.snapshotSequence).toBe(4);
136 });
137​
138 it("updates an existing thread", () => {
139 const snapshotWithThread: OrchestrationShellSnapshot = {
140 ...baseSnapshot,
141 threads: [stubThread],
142 };
143​
144 const updatedThread = { ...stubThread, title: "Updated Thread" };
145 const event: OrchestrationShellStreamEvent = {
146 kind: "thread-upserted",
147 sequence: 5,
148 thread: updatedThread,
149 };
150​
151 const next = applyShellStreamEvent(snapshotWithThread, event);
152​
153 expect(next.threads).toHaveLength(1);
154 expect(next.threads[0]?.title).toBe("Updated Thread");
155 });
156 });
157​
158 describe("thread-removed", () => {
159 it("removes a thread by id", () => {
160 const snapshotWithThread: OrchestrationShellSnapshot = {
161 ...baseSnapshot,
162 threads: [stubThread],
163 };
164​
165 const event: OrchestrationShellStreamEvent = {
166 kind: "thread-removed",
167 sequence: 6,
168 threadId: ThreadId.make("thread-1"),
169 };
170​
171 const next = applyShellStreamEvent(snapshotWithThread, event);
172​
173 expect(next.threads).toHaveLength(0);
174 expect(next.snapshotSequence).toBe(6);
175 });
176 });
177​
178 it("returns original snapshot for unrecognized event kinds", () => {
179 const unknownEvent = { kind: "unknown-future-event", sequence: 99 } as any;
180 const next = applyShellStreamEvent(baseSnapshot, unknownEvent);
181 expect(next).toBe(baseSnapshot);
182 });
183});