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

handlers.test.ts

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