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