Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
4import type { OrchestrationShellSnapshot, OrchestrationShellStreamEvent } from "@t3tools/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 settledOverride: null,
40 settledAt: null,
41 pullRequests: [],
42 latestUserMessageAt: null,
43 hasPendingApprovals: false,
44 hasPendingUserInput: false,
45 hasActionableProposedPlan: false,
46 session: null,
47} as const;
48
49describe("applyShellStreamEvent", () => {
50 it("ignores stale project upserts without mutating the snapshot", () => {
51 const snapshotWithProject: OrchestrationShellSnapshot = {
52 ...baseSnapshot,
53 snapshotSequence: 4,
54 projects: [stubProject],
55 };
56
57 for (const sequence of [3, 4]) {
58 const next = applyShellStreamEvent(snapshotWithProject, {
59 kind: "project-upserted",
60 sequence,
61 project: { ...stubProject, title: "Stale Title" },
62 });
63
64 expect(next).toBe(snapshotWithProject);
65 expect(next.snapshotSequence).toBe(4);
66 expect(next.projects[0]?.title).toBe("Test Project");
67 }
68 });
69
70 describe("project-upserted", () => {
71 it("adds a new project", () => {
72 const event: OrchestrationShellStreamEvent = {
73 kind: "project-upserted",
74 sequence: 1,
75 project: stubProject,
76 };
77
78 const next = applyShellStreamEvent(baseSnapshot, event);
79
80 expect(next.projects).toHaveLength(1);
81 expect(next.projects[0]?.id).toBe("project-1");
82 expect(next.snapshotSequence).toBe(1);
83 });
84
85 it("updates an existing project", () => {
86 const snapshotWithProject: OrchestrationShellSnapshot = {
87 ...baseSnapshot,
88 projects: [stubProject],
89 };
90
91 const updatedProject = { ...stubProject, title: "Updated Title" };
92 const event: OrchestrationShellStreamEvent = {
93 kind: "project-upserted",
94 sequence: 2,
95 project: updatedProject,
96 };
97
98 const next = applyShellStreamEvent(snapshotWithProject, event);
99
100 expect(next.projects).toHaveLength(1);
101 expect(next.projects[0]?.title).toBe("Updated Title");
102 expect(next.snapshotSequence).toBe(2);
103 });
104 });
105
106 describe("project-removed", () => {
107 it("removes a project by id", () => {
108 const snapshotWithProject: OrchestrationShellSnapshot = {
109 ...baseSnapshot,
110 projects: [stubProject],
111 };
112
113 const event: OrchestrationShellStreamEvent = {
114 kind: "project-removed",
115 sequence: 3,
116 projectId: ProjectId.make("project-1"),
117 };
118
119 const next = applyShellStreamEvent(snapshotWithProject, event);
120
121 expect(next.projects).toHaveLength(0);
122 expect(next.snapshotSequence).toBe(3);
123 });
124 });
125
126 describe("thread-upserted", () => {
127 it("adds a new thread", () => {
128 const event: OrchestrationShellStreamEvent = {
129 kind: "thread-upserted",
130 sequence: 4,
131 thread: stubThread,
132 };
133
134 const next = applyShellStreamEvent(baseSnapshot, event);
135
136 expect(next.threads).toHaveLength(1);
137 expect(next.threads[0]?.id).toBe("thread-1");
138 expect(next.snapshotSequence).toBe(4);
139 });
140
141 it("updates an existing thread", () => {
142 const snapshotWithThread: OrchestrationShellSnapshot = {
143 ...baseSnapshot,
144 threads: [stubThread],
145 };
146
147 const updatedThread = { ...stubThread, title: "Updated Thread" };
148 const event: OrchestrationShellStreamEvent = {
149 kind: "thread-upserted",
150 sequence: 5,
151 thread: updatedThread,
152 };
153
154 const next = applyShellStreamEvent(snapshotWithThread, event);
155
156 expect(next.threads).toHaveLength(1);
157 expect(next.threads[0]?.title).toBe("Updated Thread");
158 });
159 });
160
161 describe("thread-removed", () => {
162 it("removes a thread by id", () => {
163 const snapshotWithThread: OrchestrationShellSnapshot = {
164 ...baseSnapshot,
165 threads: [stubThread],
166 };
167
168 const event: OrchestrationShellStreamEvent = {
169 kind: "thread-removed",
170 sequence: 6,
171 threadId: ThreadId.make("thread-1"),
172 };
173
174 const next = applyShellStreamEvent(snapshotWithThread, event);
175
176 expect(next.threads).toHaveLength(0);
177 expect(next.snapshotSequence).toBe(6);
178 });
179 });
180
181 it("returns original snapshot for unrecognized event kinds", () => {
182 const unknownEvent = { kind: "unknown-future-event", sequence: 99 } as any;
183 const next = applyShellStreamEvent(baseSnapshot, unknownEvent);
184 expect(next).toBe(baseSnapshot);
185 });
186});