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

handlers.test.ts

772 lines27.6 KB

1import {
2 EnvironmentId,
3 ProjectId,
4 ProviderInstanceId,
5 ThreadId,
6 type OrchestrationV2ServerCommand as OrchestrationCommand,
7 PullRequestOperationError,
8 type OrchestrationProjectShell,
9 type PullRequestRef,
10 type PullRequestReviewGuideResult,
11 type ThreadPullRequestLink,
12 SourceControlProviderKind,
13} from "@t3tools/contracts";
14import { describe, expect, it } from "@effect/vitest";
15import * as SecureRandom from "effect/crypto/SecureRandom";
16import * as Effect from "effect/Effect";
17import * as Layer from "effect/Layer";
18import * as Option from "effect/Option";
19import * as Ref from "effect/Ref";
20import * as Stream from "effect/Stream";
21import type { Tool } from "effect/ai";
22​
23import * as Orchestrator from "../../../orchestration-v2/Orchestrator.ts";
24import {
25 type PullRequestTestThread,
26 v2PullRequestThread,
27} from "../../../orchestration-v2/testkit/pullRequestFixtures.ts";
28import * as ProjectService from "../../../project/ProjectService.ts";
29import * as PullRequestReviewGuideService from "../../../pullRequest/PullRequestReviewGuideService.ts";
30import * as McpInvocationContext from "../../McpInvocationContext.ts";
31import * as McpToolAccess from "../../McpToolAccess.ts";
32import * as McpToolAccessTestkit from "../../McpToolAccess.testkit.ts";
33import { listThreadPullRequests } from "./handlers.ts";
34import * as PullRequestsHandlers from "./handlers.ts";
35import { CHILD_PULL_REQUEST_REPORT_NEXT, PullRequestsToolkit } from "./tools.ts";
36​
37const PROJECT_ID = ProjectId.make("project-1");
38const THREAD_ID = ThreadId.make("thread-1");
39​
40const testCrypto = SecureRandom.make((size) => new Uint8Array(size).fill(7));
41​
42const invocation = (
43 capabilities: ReadonlyArray<McpInvocationContext.McpCapability>,
44): McpInvocationContext.McpInvocationScope => ({
45 environmentId: EnvironmentId.make("environment-1"),
46 requestNamespace: "provider-session-1",
47 thread: {
48 threadId: THREAD_ID,
49 providerSessionId: "provider-session-1",
50 providerInstanceId: ProviderInstanceId.make("codex"),
51 },
52 client: undefined,
53 capabilities: new Set(capabilities),
54 issuedAt: 1,
55});
56​
57function makeProject(
58 repositoryIdentity: OrchestrationProjectShell["repositoryIdentity"] = {
59 canonicalKey: "github.com/t3tools/t3code",
60 locator: {
61 source: "git-remote",
62 remoteName: "origin",
63 remoteUrl: "git@github.com:T3Tools/T3Code.git",
64 },
65 provider: SourceControlProviderKind.make("github"),
66 displayName: "T3Tools/T3Code",
67 owner: "T3Tools",
68 name: "T3Code",
69 },
70): OrchestrationProjectShell {
71 return {
72 id: PROJECT_ID,
73 title: "Project",
74 workspaceRoot: "/workspace/project",
75 defaultModelSelection: null,
76 scripts: [],
77 repositoryIdentity,
78 createdAt: "2026-08-01T00:00:00.000Z",
79 updatedAt: "2026-08-01T00:00:00.000Z",
80 };
82​
83function makeThread(pullRequests: ReadonlyArray<ThreadPullRequestLink>): PullRequestTestThread {
84 return {
85 id: THREAD_ID,
86 projectId: PROJECT_ID,
87 title: "Thread",
88 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
89 runtimeMode: "full-access",
90 interactionMode: "default",
91 branch: null,
92 worktreePath: null,
93 pullRequests,
94 createdAt: "2026-08-01T00:00:00.000Z",
95 updatedAt: "2026-08-20T00:00:00.000Z",
96 archivedAt: null,
97 settledOverride: null,
98 settledAt: null,
99 latestUserMessageAt: "2026-08-20T00:00:00.000Z",
100 };
102​
103function makeLink(
104 number: number,
105 overrides: Partial<ThreadPullRequestLink> & {
106 readonly headBranch?: string;
107 readonly baseBranch?: string;
108 } = {},
109): ThreadPullRequestLink {
110 const { headBranch, baseBranch, ...rest } = overrides;
111 return {
112 host: "github.com",
113 repository: "t3tools/t3code",
114 number,
115 url: `https://github.com/t3tools/t3code/pull/${number}`,
116 source: "manual",
117 linkedAt: "2026-08-10T00:00:00.000Z",
118 snapshot:
119 headBranch === undefined
120 ? null
121 : {
122 state: "open",
123 title: `PR ${number}`,
124 headBranch,
125 baseBranch: baseBranch ?? "main",
126 isDraft: false,
127 updatedAt: null,
128 syncedAt: "2026-08-27T00:00:00.000Z",
129 },
130 stack: null,
131 ...rest,
132 };
134​
135interface HarnessOptions {
136 readonly thread?: PullRequestTestThread | null;
137 readonly project?: OrchestrationProjectShell | null;
138 readonly reject?: (command: OrchestrationCommand) => string | null;
139 readonly projects?: ReadonlyArray<OrchestrationProjectShell>;
140 readonly generate?: (
141 input: PullRequestRef,
142 ) => Effect.Effect<
143 PullRequestReviewGuideResult & { started: boolean },
144 PullRequestOperationError
145 >;
147​
148const NO_GUIDE: PullRequestReviewGuideResult = { guide: null, run: null, currentHeadSha: "head-1" };
149​
150const makeHarness = Effect.fn("makePullRequestsToolkitHarness")(function* (
151 options: HarnessOptions = {},
152) {
153 const commands = yield* Ref.make<ReadonlyArray<OrchestrationCommand>>([]);
154 const guideRequests = yield* Ref.make<ReadonlyArray<PullRequestRef>>([]);
155 const thread = options.thread === undefined ? makeThread([]) : options.thread;
156 const project = options.project === undefined ? makeProject() : options.project;
157 const dispatch: Orchestrator.OrchestratorV2Shape["dispatch"] = (command) =>
158 Effect.gen(function* () {
159 const rejection = options.reject?.(command) ?? null;
160 if (rejection !== null)
161 return yield* new Orchestrator.OrchestratorDispatchError({
162 commandId: command.commandId,
163 commandType: command.type,
164 cause: rejection,
165 });
166 yield* Ref.update(commands, (recorded) => [...recorded, command]);
167 return { sequence: 1, storedEvents: [] };
168 });
169 const layerDependencies = Layer.mergeAll(
170 Layer.mock(ProjectService.ProjectService)({
171 getShell: () => Effect.succeed(Option.fromNullishOr(project)),
172 listShells: () => Effect.succeed(options.projects ?? (project ? [project] : [])),
173 }),
174 Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({
175 get: () => Effect.succeed(NO_GUIDE),
176 generate: (input) =>
177 Ref.update(guideRequests, (recorded) => [...recorded, input]).pipe(
178 Effect.andThen(
179 options.generate?.(input) ??
180 Effect.succeed({
181 ...NO_GUIDE,
182 run: {
183 _tag: "Running" as const,
184 headSha: "head-1",
185 startedAt: "2026-10-11T00:00:00.000Z",
186 },
187 started: true,
188 }),
189 ),
190 ),
191 }),
192 Layer.mock(Orchestrator.OrchestratorV2)({
193 getThreadShell: (id) =>
194 Effect.succeed(id === THREAD_ID && thread ? v2PullRequestThread(thread) : null),
195 dispatch,
196 }),
197 Layer.succeed(SecureRandom.SecureRandom, testCrypto),
198 McpToolAccessTestkit.liveThreadsLayer,
199 );
200 const toolkit = yield* PullRequestsToolkit.pipe(
201 Effect.provide(
202 McpToolAccess.HandlersLayer.layer(PullRequestsHandlers.layer).pipe(
203 Layer.provide(layerDependencies),
204 ),
205 ),
206 );
207 const call = <Name extends keyof typeof PullRequestsToolkit.tools>(
208 name: Name,
209 params: Parameters<typeof toolkit.handle<Name>>[1],
210 capabilities: ReadonlyArray<McpInvocationContext.McpCapability> = ["pull-requests"],
211 ) =>
212 toolkit.handle(name, params).pipe(
213 Stream.unwrap,
214 Stream.runCollect,
215 Effect.map(
216 (chunk) => chunk.at(-1)!.result as Tool.Success<(typeof PullRequestsToolkit.tools)[Name]>,
217 ),
218 Effect.provideService(McpInvocationContext.McpInvocationContext, invocation(capabilities)),
219 Effect.provide(layerDependencies),
220 );
221 return { commands, guideRequests, call };
222});
223​
224describe("pull request toolkit handlers", () => {
225 it.effect("refuses a credential without the pull-requests capability", () =>
226 Effect.gen(function* () {
227 const harness = yield* makeHarness();
228 const error = yield* harness
229 .call("list_thread_pull_requests", {}, ["preview"])
230 .pipe(Effect.flip);
231 expect(error).toMatchObject({
232 _tag: "McpCapabilityUnavailableError",
233 capability: "pull-requests",
234 threadId: THREAD_ID,
235 });
236 expect(yield* Ref.get(harness.commands)).toEqual([]);
237 }),
238 );
239​
240 it.effect("links by URL with source agent on the token's thread", () =>
241 Effect.gen(function* () {
242 const harness = yield* makeHarness();
243 const result = yield* harness.call("link_pull_request", {
244 url: "https://github.com/T3Tools/T3Code/pull/123/files",
245 });
246 expect(result).toEqual({
247 host: "github.com",
248 repository: "t3tools/t3code",
249 number: 123,
250 url: "https://github.com/T3Tools/T3Code/pull/123/files",
251 alreadyLinked: false,
252 });
253 expect(yield* Ref.get(harness.commands)).toMatchObject([
254 {
255 type: "thread.pull-request.link",
256 threadId: THREAD_ID,
257 host: "github.com",
258 repository: "t3tools/t3code",
259 number: 123,
260 source: "agent",
261 },
262 ]);
263 }),
264 );
265​
266 it.effect("watching an unlinked pull request links it first", () =>
267 Effect.gen(function* () {
268 const harness = yield* makeHarness();
269 const result = yield* harness.call("watch_pull_request", {
270 url: "https://github.com/t3tools/t3code/pull/9",
271 });
272 expect(result).toMatchObject({ number: 9, watching: false, wasWatching: false });
273 expect(yield* Ref.get(harness.commands)).toMatchObject([
274 {
275 type: "thread.pull-request.watch",
276 number: 9,
277 watching: true,
278 link: { url: "https://github.com/t3tools/t3code/pull/9", source: "agent" },
279 },
280 ]);
281 }),
282 );
283​
284 it.effect("refuses to watch a merged pull request and stops an existing watch", () =>
285 Effect.gen(function* () {
286 const watch = {
287 startedAt: "2026-08-20T00:00:00.000Z",
288 headSha: null,
289 failedChecks: [],
290 passed: false,
291 passedChecks: [],
292 remarksThrough: "2026-08-20T00:00:00.000Z",
293 remarkIds: [],
294 conflicting: false,
295 wakes: 0,
296 };
297 const merged = makeLink(1, { headBranch: "done" });
298 const harness = yield* makeHarness({
299 thread: makeThread([
300 { ...merged, snapshot: merged.snapshot && { ...merged.snapshot, state: "merged" } },
301 makeLink(2, { headBranch: "idle" }),
302 makeLink(3, { headBranch: "watched", watch }),
303 ]),
304 });
305 const error = yield* harness
306 .call("watch_pull_request", { repository: "t3tools/t3code", number: 1 })
307 .pipe(Effect.flip);
308 expect(error).toMatchObject({ _tag: "PullRequestNotOpenError", state: "merged" });
309 expect(
310 yield* harness.call("unwatch_pull_request", { repository: "t3tools/t3code", number: 3 }),
311 ).toMatchObject({ wasWatching: true });
312 expect(yield* Ref.get(harness.commands)).toMatchObject([
313 { type: "thread.pull-request.watch", number: 3, watching: false },
314 ]);
315 }),
316 );
317​
318 it.effect("watches a pull request saved as closed, since it may have reopened", () =>
319 Effect.gen(function* () {
320 const closed = makeLink(1, { headBranch: "closed" });
321 const harness = yield* makeHarness({
322 thread: makeThread([
323 { ...closed, snapshot: closed.snapshot && { ...closed.snapshot, state: "closed" } },
324 ]),
325 });
326 yield* harness.call("watch_pull_request", { repository: "t3tools/t3code", number: 1 });
327 expect(yield* Ref.get(harness.commands)).toMatchObject([
328 { type: "thread.pull-request.watch", number: 1, watching: true },
329 ]);
330 }),
331 );
332​
333 it.effect("refuses a watch from a subagent thread, whose parent owns the pull request", () =>
334 Effect.gen(function* () {
335 const harness = yield* makeHarness({
336 thread: {
337 ...makeThread([makeLink(1, { headBranch: "feature" })]),
338 lineage: {
339 rootThreadId: ThreadId.make("parent"),
340 parentThreadId: ThreadId.make("parent"),
341 relationshipToParent: "subagent",
342 },
343 },
344 });
345 const error = yield* harness
346 .call("watch_pull_request", { repository: "t3tools/t3code", number: 1 })
347 .pipe(Effect.flip);
348 expect(error).toMatchObject({ _tag: "PullRequestWatchFromSubagentError" });
349 expect(yield* Ref.get(harness.commands)).toEqual([]);
350 }),
351 );
352​
353 it.effect("asks a subagent thread to end with its PR report after it links", () =>
354 Effect.gen(function* () {
355 const harness = yield* makeHarness({
356 thread: {
357 ...makeThread([]),
358 lineage: {
359 rootThreadId: ThreadId.make("parent"),
360 parentThreadId: ThreadId.make("parent"),
361 relationshipToParent: "subagent",
362 },
363 },
364 });
365 const result = yield* harness.call("link_pull_request", {
366 url: "https://github.com/t3tools/t3code/pull/124",
367 });
368 expect(result).toMatchObject({ number: 124, next: CHILD_PULL_REQUEST_REPORT_NEXT });
369 }),
370 );
371​
372 it.effect("canonicalizes GitCafe links independently of the thread's project host", () =>
373 Effect.gen(function* () {
374 for (const host of ["git.cafe", "staging.git.cafe"]) {
375 for (const project of [makeProject(), makeProject(null)]) {
376 for (const target of [
377 { host, repository: "PingDotGG/T3Code", number: 21353 },
378 { url: `https://${host}/PingDotGG/T3Code/pull/21353` },
379 { url: `https://${host}/PingDotGG/T3Code/pulls/21353` },
380 ]) {
381 const harness = yield* makeHarness({ project });
382 const result = yield* harness.call("link_pull_request", target);
383 const url = `https://${host}/pingdotgg/t3code/pulls/21353`;
384 expect(result).toMatchObject({
385 host,
386 repository: "pingdotgg/t3code",
387 number: 21353,
388 url,
389 });
390 expect(yield* Ref.get(harness.commands)).toMatchObject([
391 {
392 type: "thread.pull-request.link",
393 host,
394 repository: "pingdotgg/t3code",
395 number: 21353,
396 url,
397 },
398 ]);
399 }
400 }
401 }
402 }),
403 );
404​
405 it.effect("builds the URL in the project host's own shape", () =>
406 Effect.gen(function* () {
407 const harness = yield* makeHarness({
408 project: makeProject({
409 canonicalKey: "gitlab.com/group/sub/project",
410 locator: {
411 source: "git-remote",
412 remoteName: "origin",
413 remoteUrl: "git@gitlab.com:group/sub/project.git",
414 },
415 provider: SourceControlProviderKind.make("gitlab"),
416 displayName: "group/sub/project",
417 }),
418 });
419 const result = yield* harness.call("link_pull_request", {
420 repository: "group/sub/project",
421 number: 42,
422 });
423 expect(result.url).toBe("https://gitlab.com/group/sub/project/-/merge_requests/42");
424 expect(result.host).toBe("gitlab.com");
425 const other = yield* harness.call("link_pull_request", {
426 host: "gitlab.example.test",
427 repository: "team/repo",
428 number: 7,
429 });
430 expect(other.url).toBe("https://gitlab.example.test/team/repo/-/merge_requests/7");
431 const onGitHub = yield* harness.call("link_pull_request", {
432 host: "github.com",
433 repository: "acme/web",
434 number: 9,
435 });
436 expect(onGitHub.url).toBe("https://github.com/acme/web/pull/9");
437 }),
438 );
439​
440 it.effect("links a URL but not a bare number when the project's host is unknown", () =>
441 Effect.gen(function* () {
442 const harness = yield* makeHarness({
443 project: makeProject({
444 canonicalKey: "scm.example.test/tfs/collection/project/_git/repo",
445 locator: {
446 source: "git-remote",
447 remoteName: "origin",
448 remoteUrl: "http://dummy:secret@scm.example.test:8080/tfs/Collection/Project/_git/Repo",
449 },
450 provider: SourceControlProviderKind.make("unknown"),
451 displayName: "tfs/collection/project/_git/repo",
452 }),
453 });
454 const error = yield* harness
455 .call("link_pull_request", { repository: "tfs/Collection/Project/_git/Repo", number: 124 })
456 .pipe(Effect.flip);
457 expect(error).toMatchObject({ _tag: "PullRequestUrlUnavailableError" });
458​
459 const supplied = "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/123";
460 const fromUrl = yield* harness.call("link_pull_request", { url: supplied });
461 expect(fromUrl).toEqual({
462 host: "scm.example.test",
463 repository: "tfs/collection/project/_git/repo",
464 number: 123,
465 url: supplied,
466 alreadyLinked: false,
467 });
468 expect(yield* Ref.get(harness.commands)).toMatchObject([
469 { type: "thread.pull-request.link", number: 123, url: supplied },
470 ]);
471 }),
472 );
473​
474 it.effect("treats a duplicate link as alreadyLinked rather than an error", () =>
475 Effect.gen(function* () {
476 const harness = yield* makeHarness({
477 thread: makeThread([makeLink(123)]),
478 reject: (command) =>
479 command.type === "thread.pull-request.link" ? "already linked" : null,
480 });
481 const result = yield* harness.call("link_pull_request", {
482 url: "https://github.com/t3tools/t3code/pull/123",
483 });
484 expect(result.alreadyLinked).toBe(true);
485 }),
486 );
487​
488 it.effect("unlinks a linked pull request and reports a missing one as wasLinked=false", () =>
489 Effect.gen(function* () {
490 const harness = yield* makeHarness({
491 thread: makeThread([makeLink(5)]),
492 reject: (command) =>
493 command.type === "thread.pull-request.unlink" && command.number !== 5
494 ? "not linked"
495 : null,
496 });
497 const linked = yield* harness.call("unlink_pull_request", {
498 repository: "t3tools/t3code",
499 number: 5,
500 });
501 expect(linked).toEqual({
502 host: "github.com",
503 repository: "t3tools/t3code",
504 number: 5,
505 wasLinked: true,
506 });
507 const missing = yield* harness.call("unlink_pull_request", {
508 url: "https://github.com/t3tools/t3code/pull/9",
509 });
510 expect(missing.wasLinked).toBe(false);
511 expect(yield* Ref.get(harness.commands)).toMatchObject([
512 { type: "thread.pull-request.unlink", number: 5 },
513 ]);
514 }),
515 );
516​
517 it.effect("reports a dismissed stack member as not linked and leaves it dismissed", () =>
518 Effect.gen(function* () {
519 const harness = yield* makeHarness({
520 thread: makeThread([makeLink(9, { source: "stack-dismissed" })]),
521 });
522 const result = yield* harness.call("unlink_pull_request", {
523 repository: "t3tools/t3code",
524 number: 9,
525 });
526 expect(result.wasLinked).toBe(false);
527 expect(yield* Ref.get(harness.commands)).toEqual([]);
528 }),
529 );
530​
531 it.effect("links several pull requests in one call", () =>
532 Effect.gen(function* () {
533 const harness = yield* makeHarness({
534 thread: makeThread([makeLink(1), { ...makeLink(2), source: "stack-dismissed" }]),
535 });
536 const url = (number: number) => `https://github.com/t3tools/t3code/pull/${number}`;
537 const result = yield* harness.call("link_pull_requests", {
538 urls: [url(1), url(2), url(3), url(3), "https://example.com/not-a-pr"],
539 });
540 expect(result).toEqual({
541 linked: [{ host: "github.com", repository: "t3tools/t3code", numbers: [2, 3] }],
542 alreadyLinked: [{ host: "github.com", repository: "t3tools/t3code", numbers: [1] }],
543 linkedCount: 3,
544 invalidUrls: ["https://example.com/not-a-pr"],
545 });
546 expect(yield* Ref.get(harness.commands)).toMatchObject([
547 { type: "thread.pull-request.link", number: 2, source: "agent" },
548 { type: "thread.pull-request.link", number: 3, source: "agent" },
549 ]);
550 }),
551 );
552​
553 it.effect("unlinks every visible pull request in the given states in one call", () =>
554 Effect.gen(function* () {
555 const withState = (number: number, state: "open" | "closed" | "merged") => {
556 const link = makeLink(number, { headBranch: `branch-${number}` });
557 return { ...link, snapshot: { ...link.snapshot!, state } };
558 };
559 const harness = yield* makeHarness({
560 thread: makeThread([
561 withState(1, "merged"),
562 withState(2, "open"),
563 withState(3, "closed"),
564 makeLink(4),
565 { ...withState(5, "merged"), source: "stack-dismissed" },
566 ]),
567 });
568 const result = yield* harness.call("unlink_pull_requests", {
569 states: ["closed", "merged"],
570 });
571 expect(result).toEqual({
572 unlinked: [
573 { host: "github.com", repository: "t3tools/t3code", state: "merged", numbers: [1] },
574 { host: "github.com", repository: "t3tools/t3code", state: "closed", numbers: [3] },
575 ],
576 unlinkedCount: 2,
577 remaining: 2,
578 });
579 expect(yield* Ref.get(harness.commands)).toMatchObject([
580 { type: "thread.pull-request.unlink", number: 1 },
581 { type: "thread.pull-request.unlink", number: 3 },
582 ]);
583 }),
584 );
585​
586 it("reports an older Forgejo link's HTTP port when listing thread links", () => {
587 const result = listThreadPullRequests(
588 makeThread([
589 makeLink(42, {
590 host: "forge.example",
591 url: "http://forge.example:3000/t3tools/t3code/pulls/42",
592 }),
593 ]),
594 );
595 expect(result.pullRequests[0]?.host).toBe("forge.example:3000");
596 });
597​
598 it.effect("lists visible links with host state and derived chain order", () =>
599 Effect.gen(function* () {
600 const harness = yield* makeHarness({
601 thread: makeThread([
602 makeLink(3, { headBranch: "feat-c", baseBranch: "feat-b", source: "agent" }),
603 makeLink(1, { headBranch: "feat-a", baseBranch: "main", source: "created" }),
604 makeLink(2, { headBranch: "feat-b", baseBranch: "feat-a", source: "agent" }),
605 makeLink(9, { source: "stack-dismissed" }),
606 makeLink(10),
607 ]),
608 });
609 const result = yield* harness.call("list_thread_pull_requests", {});
610 expect(result.pullRequests.map((entry) => entry.number)).toEqual([3, 1, 2, 10]);
611 expect(result.pullRequests[0]).toEqual({
612 host: "github.com",
613 repository: "t3tools/t3code",
614 number: 3,
615 url: "https://github.com/t3tools/t3code/pull/3",
616 source: "agent",
617 watching: false,
618 state: "open",
619 title: "PR 3",
620 headBranch: "feat-c",
621 baseBranch: "feat-b",
622 isDraft: false,
623 stack: { kind: "derived", position: 3, size: 3 },
624 });
625 expect(result.pullRequests[3]).toMatchObject({
626 number: 10,
627 state: null,
628 title: null,
629 headBranch: null,
630 stack: null,
631 });
632 expect(result.chains).toEqual([
633 { kind: "derived", numbers: [1, 2, 3] },
634 { kind: "derived", numbers: [10] },
635 ]);
636 }),
637 );
638});
639​
640describe("listThreadPullRequests", () => {
641 it("reports a native stack position for each member", () => {
642 const stack = {
643 kind: "native" as const,
644 id: "stack-1",
645 number: 1,
646 url: "https://github.com/t3tools/t3code/stack/1",
647 base: "main",
648 layers: [
649 { number: 1, headBranch: "a", state: "open" as const },
650 { number: 2, headBranch: "b", state: "open" as const },
651 ],
652 };
653 const result = listThreadPullRequests({
654 pullRequests: [
655 makeLink(2, { stack, source: "stack" }),
656 makeLink(1, { stack, source: "created" }),
657 ],
658 });
659 expect(result.pullRequests.map((entry) => [entry.number, entry.stack])).toEqual([
660 [2, { kind: "native", position: 2, size: 2 }],
661 [1, { kind: "native", position: 1, size: 2 }],
662 ]);
663 expect(result.chains).toEqual([{ kind: "native", numbers: [1, 2] }]);
664 });
665​
666 it("gives pull requests built on the same one the same position", () => {
667 const result = listThreadPullRequests({
668 pullRequests: [
669 makeLink(1, { headBranch: "feat-a", baseBranch: "main" }),
670 makeLink(2, { headBranch: "feat-b", baseBranch: "feat-a" }),
671 makeLink(3, { headBranch: "feat-c", baseBranch: "feat-a" }),
672 ],
673 });
674 expect(result.pullRequests.map((entry) => [entry.number, entry.stack])).toEqual([
675 [1, { kind: "derived", position: 1, size: 3 }],
676 [2, { kind: "derived", position: 2, size: 3 }],
677 [3, { kind: "derived", position: 2, size: 3 }],
678 ]);
679 expect(result.chains).toEqual([{ kind: "derived", numbers: [1, 2, 3] }]);
680 });
681});
682​
683describe("pull request guide tools", () => {
684 it.effect(
685 "writes guides through the project whose checkout is each pull request's repository",
686 () =>
687 Effect.gen(function* () {
688 const other = {
689 ...makeProject({
690 canonicalKey: "github.com/acme/web",
691 locator: {
692 source: "git-remote",
693 remoteName: "origin",
694 remoteUrl: "git@github.com:acme/web.git",
695 },
696 provider: SourceControlProviderKind.make("github"),
697 displayName: "acme/web",
698 owner: "acme",
699 name: "web",
700 }),
701 id: ProjectId.make("project-web"),
702 };
703 const harness = yield* makeHarness({ projects: [makeProject(), other] });
704 const result = yield* harness.call("write_pull_request_guides", {
705 pullRequests: [
706 { url: "https://github.com/acme/web/pull/7" },
707 { repository: "t3tools/t3code", number: 12 },
708 { url: "https://github.com/someone/else/pull/1" },
709 ],
710 });
711 expect(
712 result.guides.map((guide) => [guide.repository, guide.number, guide.outcome]),
713 ).toEqual([
714 ["acme/web", 7, "started"],
715 ["t3tools/t3code", 12, "started"],
716 ["someone/else", 1, "failed"],
717 ]);
718 expect(result.guides[2]?.detail).toContain("No project in this environment");
719 expect(
720 (yield* Ref.get(harness.guideRequests)).map((ref) => [ref.projectId, ref.number]),
721 ).toEqual([
722 ["project-web", 7],
723 [PROJECT_ID, 12],
724 ]);
725 }),
726 );
727​
728 it.effect("reports an unreadable entry on its own and still writes the rest of the batch", () =>
729 Effect.gen(function* () {
730 const harness = yield* makeHarness();
731 const result = yield* harness.call("write_pull_request_guides", {
732 pullRequests: [
733 { url: "https://example.com/not-a-pull-request" },
734 { repository: "t3tools/t3code" },
735 { repository: "t3tools/t3code", number: 5 },
736 ],
737 });
738 expect(result.guides.map((guide) => guide.outcome)).toEqual(["failed", "failed", "started"]);
739 expect(result.guides[0]?.detail).toContain("not a recognised pull request URL");
740 expect(result.guides[1]?.detail).toContain("Pass either url");
741 expect((yield* Ref.get(harness.guideRequests)).map((ref) => ref.number)).toEqual([5]);
742 }),
743 );
744​
745 it.effect("finds an Azure DevOps checkout for a pull request named by its full path", () =>
746 Effect.gen(function* () {
747 const azure = {
748 ...makeProject({
749 canonicalKey: "dev.azure.com/org/project/_git/web",
750 locator: {
751 source: "git-remote",
752 remoteName: "origin",
753 remoteUrl: "https://dev.azure.com/org/project/_git/web",
754 },
755 provider: SourceControlProviderKind.make("azure-devops"),
756 displayName: "org/project/_git/web",
757 owner: "org",
758 name: "web",
759 }),
760 id: ProjectId.make("project-azure"),
761 };
762 const harness = yield* makeHarness({ projects: [makeProject(), azure] });
763 const result = yield* harness.call("write_pull_request_guides", {
764 pullRequests: [{ host: "dev.azure.com", repository: "org/project/_git/web", number: 9 }],
765 });
766 expect(result.guides.map((guide) => guide.outcome)).toEqual(["started"]);
767 expect((yield* Ref.get(harness.guideRequests)).map((ref) => ref.projectId)).toEqual([
768 "project-azure",
769 ]);
770 }),
771 );
772});