Open History to see the last commit
1import {
2 CommandId,
3 EventId,
4 ProjectId,
5 ThreadId,
6 type OrchestrationEvent,
7 type OrchestrationReadModel,
8 type RepositoryIdentity,
9 type ThreadPullRequestLink,
10 type ThreadPullRequestSnapshot,
11} from "@t3tools/contracts";
12import { expect, it } from "@effect/vitest";
13import * as Effect from "effect/Effect";
14
15import { createEmptyReadModel, projectEvent } from "./projector.ts";
16
17const NOW = "2026-01-01T00:00:00.000Z";
18const LATER = "2026-01-02T00:00:00.000Z";
19const THREAD_ID = ThreadId.make("thread-1");
20const PROJECT_ID = ProjectId.make("project-1");
21
22function makeEvent(input: {
23 readonly sequence: number;
24 readonly type: OrchestrationEvent["type"];
25 readonly payload: unknown;
26}): OrchestrationEvent {
27 return {
28 sequence: input.sequence,
29 eventId: EventId.make(`event-${input.sequence}`),
30 type: input.type,
31 aggregateKind: "thread",
32 aggregateId: THREAD_ID,
33 occurredAt: NOW,
34 commandId: CommandId.make(`command-${input.sequence}`),
35 causationEventId: null,
36 correlationId: null,
37 metadata: {},
38 payload: input.payload as never,
39 } as OrchestrationEvent;
40}
41
42function makeLink(overrides: Partial<ThreadPullRequestLink> = {}): ThreadPullRequestLink {
43 return {
44 host: "github.com",
45 repository: "t3tools/t3code",
46 number: 42,
47 url: "https://github.com/t3tools/t3code/pull/42",
48 source: "manual",
49 linkedAt: NOW,
50 snapshot: null,
51 stack: null,
52 ...overrides,
53 };
54}
55
56const snapshot: ThreadPullRequestSnapshot = {
57 state: "merged",
58 title: "Add links",
59 headBranch: "feat/links",
60 baseBranch: "main",
61 isDraft: false,
62 updatedAt: LATER,
63 syncedAt: LATER,
64};
65
66const createThread = (model: OrchestrationReadModel) =>
67 projectEvent(
68 model,
69 makeEvent({
70 sequence: model.snapshotSequence + 1,
71 type: "thread.created",
72 payload: {
73 threadId: THREAD_ID,
74 projectId: PROJECT_ID,
75 title: "Thread",
76 modelSelection: { provider: "codex", model: "gpt-5.4" },
77 runtimeMode: "full-access",
78 interactionMode: "default",
79 branch: null,
80 worktreePath: null,
81 createdAt: NOW,
82 updatedAt: NOW,
83 },
84 }),
85 );
86
87const createProject = (model: OrchestrationReadModel, repositoryIdentity: RepositoryIdentity) =>
88 projectEvent(model, {
89 ...makeEvent({
90 sequence: model.snapshotSequence + 1,
91 type: "project.created",
92 payload: {
93 projectId: PROJECT_ID,
94 title: "Project",
95 workspaceRoot: "/workspace/project",
96 defaultModelSelection: null,
97 scripts: [],
98 createdAt: NOW,
99 updatedAt: NOW,
100 },
101 }),
102 aggregateKind: "project",
103 aggregateId: PROJECT_ID,
104 }).pipe(
105 Effect.map((next) => ({
106 ...next,
107 projects: next.projects.map((project) =>
108 project.id === PROJECT_ID ? { ...project, repositoryIdentity } : project,
109 ),
110 })),
111 );
112
113it.effect("seeds threads with no pull requests", () =>
114 Effect.gen(function* () {
115 const created = yield* createThread(createEmptyReadModel(NOW));
116 expect(created.threads[0]?.pullRequests).toEqual([]);
117 expect(created.threads[0]?.linkedPullRequest ?? null).toBeNull();
118 }),
119);
120
121it.effect("projects link, sync, and unlink onto the thread", () =>
122 Effect.gen(function* () {
123 const created = yield* createThread(
124 yield* createProject(createEmptyReadModel(NOW), {
125 canonicalKey: "github.com/t3tools/t3code",
126 provider: "github",
127 displayName: "t3tools/t3code",
128 locator: {
129 source: "git-remote",
130 remoteName: "origin",
131 remoteUrl: "https://github.com/t3tools/t3code.git",
132 },
133 }),
134 );
135 const link = makeLink();
136
137 const linked = yield* projectEvent(
138 created,
139 makeEvent({
140 sequence: 2,
141 type: "thread.pull-request-linked",
142 payload: { threadId: THREAD_ID, link, updatedAt: LATER },
143 }),
144 );
145 expect(linked.threads[0]?.pullRequests).toEqual([link]);
146 expect(linked.threads[0]?.updatedAt).toBe(LATER);
147 // The legacy field is derived from the array so old clients keep working.
148 expect(linked.threads[0]?.linkedPullRequest).toEqual({
149 projectId: PROJECT_ID,
150 repository: "t3tools/t3code",
151 number: 42,
152 url: "https://github.com/t3tools/t3code/pull/42",
153 });
154
155 // A second link for the same key replaces in place (used for un-dismiss
156 // and stack tombstones), never duplicates.
157 const relinked = yield* projectEvent(
158 linked,
159 makeEvent({
160 sequence: 3,
161 type: "thread.pull-request-linked",
162 payload: {
163 threadId: THREAD_ID,
164 link: { ...link, host: "GITHUB.COM", source: "agent" },
165 updatedAt: LATER,
166 },
167 }),
168 );
169 expect(relinked.threads[0]?.pullRequests).toHaveLength(1);
170 expect(relinked.threads[0]?.pullRequests[0]?.source).toBe("agent");
171
172 const synced = yield* projectEvent(
173 relinked,
174 makeEvent({
175 sequence: 4,
176 type: "thread.pull-request-synced",
177 payload: {
178 threadId: THREAD_ID,
179 host: "github.com",
180 repository: "t3tools/t3code",
181 number: 42,
182 snapshot,
183 stack: null,
184 updatedAt: LATER,
185 },
186 }),
187 );
188 expect(synced.threads[0]?.pullRequests[0]?.snapshot).toEqual(snapshot);
189
190 const unlinked = yield* projectEvent(
191 synced,
192 makeEvent({
193 sequence: 5,
194 type: "thread.pull-request-unlinked",
195 payload: {
196 threadId: THREAD_ID,
197 host: "github.com",
198 repository: "t3tools/t3code",
199 number: 42,
200 updatedAt: LATER,
201 },
202 }),
203 );
204 expect(unlinked.threads[0]?.pullRequests).toEqual([]);
205 expect(unlinked.threads[0]?.linkedPullRequest).toBeNull();
206 }),
207);
208
209it.effect("ignores a sync for a pull request that is no longer linked", () =>
210 Effect.gen(function* () {
211 const created = yield* createThread(createEmptyReadModel(NOW));
212 const other = makeLink({ number: 7, url: "https://github.com/t3tools/t3code/pull/7" });
213 const linked = yield* projectEvent(
214 created,
215 makeEvent({
216 sequence: 2,
217 type: "thread.pull-request-linked",
218 payload: { threadId: THREAD_ID, link: other, updatedAt: NOW },
219 }),
220 );
221 const synced = yield* projectEvent(
222 linked,
223 makeEvent({
224 sequence: 3,
225 type: "thread.pull-request-synced",
226 payload: {
227 threadId: THREAD_ID,
228 host: "github.com",
229 repository: "t3tools/t3code",
230 number: 42,
231 snapshot,
232 stack: null,
233 updatedAt: LATER,
234 },
235 }),
236 );
237 expect(synced.threads[0]?.pullRequests).toEqual([other]);
238 expect(synced.threads[0]?.updatedAt).toBe(NOW);
239 }),
240);
241
242it.effect("mirrors legacy meta-updated links into pullRequests using the project host", () =>
243 Effect.gen(function* () {
244 const withProject = yield* createProject(createEmptyReadModel(NOW), {
245 canonicalKey: "GitHub.com/t3tools/t3code",
246 provider: "github",
247 displayName: "t3tools/t3code",
248 locator: {
249 source: "git-remote",
250 remoteName: "origin",
251 remoteUrl: "git@github.com:t3tools/t3code.git",
252 },
253 });
254 const created = yield* createThread(withProject);
255 const agentLink = makeLink({
256 number: 7,
257 url: "https://github.com/t3tools/t3code/pull/7",
258 source: "agent",
259 });
260 const withAgentLink = yield* projectEvent(
261 created,
262 makeEvent({
263 sequence: 3,
264 type: "thread.pull-request-linked",
265 payload: { threadId: THREAD_ID, link: agentLink, updatedAt: NOW },
266 }),
267 );
268
269 const legacyLinked = yield* projectEvent(
270 withAgentLink,
271 makeEvent({
272 sequence: 4,
273 type: "thread.meta-updated",
274 payload: {
275 threadId: THREAD_ID,
276 linkedPullRequest: {
277 projectId: PROJECT_ID,
278 repository: "T3Tools/T3Code",
279 number: 42,
280 url: "https://github.com/t3tools/t3code/pull/42",
281 },
282 updatedAt: LATER,
283 },
284 }),
285 );
286 expect(legacyLinked.threads[0]?.pullRequests).toEqual([
287 agentLink,
288 {
289 host: "github.com",
290 repository: "t3tools/t3code",
291 number: 42,
292 url: "https://github.com/t3tools/t3code/pull/42",
293 source: "manual",
294 linkedAt: LATER,
295 snapshot: null,
296 stack: null,
297 },
298 ]);
299 // Two open links read as a stack; the derived field points at the top.
300 expect(legacyLinked.threads[0]?.linkedPullRequest).toEqual({
301 projectId: PROJECT_ID,
302 repository: "t3tools/t3code",
303 number: 42,
304 url: "https://github.com/t3tools/t3code/pull/42",
305 });
306
307 // Null clears only the manual link; the agent's stays.
308 const legacyCleared = yield* projectEvent(
309 legacyLinked,
310 makeEvent({
311 sequence: 5,
312 type: "thread.meta-updated",
313 payload: { threadId: THREAD_ID, linkedPullRequest: null, updatedAt: LATER },
314 }),
315 );
316 expect(legacyCleared.threads[0]?.pullRequests).toEqual([agentLink]);
317 expect(legacyCleared.threads[0]?.linkedPullRequest).toEqual({
318 projectId: PROJECT_ID,
319 repository: "t3tools/t3code",
320 number: 7,
321 url: "https://github.com/t3tools/t3code/pull/7",
322 });
323 }),
324);
325
326it.effect("falls back to the link URL host when the project has no repository identity", () =>
327 Effect.gen(function* () {
328 const created = yield* createThread(createEmptyReadModel(NOW));
329 const legacyLinked = yield* projectEvent(
330 created,
331 makeEvent({
332 sequence: 2,
333 type: "thread.meta-updated",
334 payload: {
335 threadId: THREAD_ID,
336 linkedPullRequest: {
337 projectId: PROJECT_ID,
338 repository: "t3tools/t3code",
339 number: 42,
340 url: "https://GitLab.example.com/t3tools/t3code/-/merge_requests/42",
341 },
342 updatedAt: LATER,
343 },
344 }),
345 );
346 expect(legacyLinked.threads[0]?.pullRequests[0]?.host).toBe("gitlab.example.com");
347 }),
348);
349
350it.effect("leaves pullRequests alone when meta-updated carries no legacy link", () =>
351 Effect.gen(function* () {
352 const created = yield* createThread(createEmptyReadModel(NOW));
353 const link = makeLink();
354 const linked = yield* projectEvent(
355 created,
356 makeEvent({
357 sequence: 2,
358 type: "thread.pull-request-linked",
359 payload: { threadId: THREAD_ID, link, updatedAt: NOW },
360 }),
361 );
362 const retitled = yield* projectEvent(
363 linked,
364 makeEvent({
365 sequence: 3,
366 type: "thread.meta-updated",
367 payload: { threadId: THREAD_ID, title: "Renamed", updatedAt: LATER },
368 }),
369 );
370 expect(retitled.threads[0]?.title).toBe("Renamed");
371 expect(retitled.threads[0]?.pullRequests).toEqual([link]);
372 }),
373);
374
375it.effect("replays Azure legacy selectors as full repository keys", () =>
376 Effect.gen(function* () {
377 const withProject = yield* createProject(createEmptyReadModel(NOW), {
378 canonicalKey: "ssh.dev.azure.com/v3/org-a/project/web",
379 provider: "azure-devops",
380 displayName: "v3/org-a/project/web",
381 name: "web",
382 locator: {
383 source: "git-remote",
384 remoteName: "origin",
385 remoteUrl: "git@ssh.dev.azure.com:v3/org-a/project/web",
386 },
387 });
388 const created = yield* createThread(withProject);
389 const legacy = {
390 projectId: PROJECT_ID,
391 repository: "web",
392 number: 7,
393 url: "https://dev.azure.com/org-a/project/_git/web/pullrequest/7",
394 };
395 const model = yield* projectEvent(
396 created,
397 makeEvent({
398 sequence: 3,
399 type: "thread.meta-updated",
400 payload: { threadId: THREAD_ID, linkedPullRequest: legacy, updatedAt: LATER },
401 }),
402 );
403 expect(model.threads[0]?.pullRequests).toEqual([
404 {
405 host: "dev.azure.com",
406 repository: "org-a/project/_git/web",
407 number: 7,
408 url: legacy.url,
409 source: "manual",
410 linkedAt: LATER,
411 snapshot: null,
412 stack: null,
413 },
414 ]);
415 expect(model.threads[0]?.linkedPullRequest).toEqual(legacy);
416 }),
417);