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