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

linkCreatedPullRequest.test.ts

275 lines9.1 KB

1import {
2 CommandId,
3 ProjectId,
4 ProviderInstanceId,
5 ThreadId,
6 type GitRunStackedActionResult,
7 type OrchestrationV2Command as OrchestrationCommand,
8 type OrchestrationProjectShell,
9 type OrchestrationThreadShell,
10} from "@t3tools/contracts";
11import { describe, expect, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13import * as Layer from "effect/Layer";
14import * as Option from "effect/Option";
15import * as Ref from "effect/Ref";
16​
17import {
18 OrchestratorV2,
19 type OrchestratorV2Shape,
20 OrchestratorDispatchError,
21} from "../orchestration-v2/Orchestrator.ts";
22import { v2PullRequestThread } from "../orchestration-v2/testkit/pullRequestFixtures.ts";
23import { ProjectionSnapshotQuery } from "../orchestration/Services/ProjectionSnapshotQuery.ts";
24import { refreshPushedPullRequests } from "./refreshPushedPullRequests.ts";
25import { PullRequestService } from "../pullRequest/PullRequestService.ts";
26import { createdPullRequestKey, linkCreatedPullRequest } from "./linkCreatedPullRequest.ts";
27​
28const PROJECT_ID = ProjectId.make("project-1");
29const THREAD_ID = ThreadId.make("thread-1");
30const commandId = Effect.succeed(CommandId.make("server:pr-created-link:test"));
31​
32const project: OrchestrationProjectShell = {
33 id: PROJECT_ID,
34 title: "Project",
35 workspaceRoot: "/workspace/project",
36 defaultModelSelection: null,
37 scripts: [],
38 repositoryIdentity: {
39 canonicalKey: "github.acme.test/platform/api",
40 locator: {
41 source: "git-remote",
42 remoteName: "origin",
43 remoteUrl: "git@github.acme.test:Platform/API.git",
44 },
45 provider: "github",
46 displayName: "Platform/API",
47 owner: "Platform",
48 name: "API",
49 },
50 createdAt: "2026-08-01T00:00:00.000Z",
51 updatedAt: "2026-08-01T00:00:00.000Z",
52};
53​
54const thread: OrchestrationThreadShell = {
55 id: THREAD_ID,
56 projectId: PROJECT_ID,
57 title: "Thread",
58 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
59 runtimeMode: "full-access",
60 interactionMode: "default",
61 branch: null,
62 worktreePath: null,
63 pullRequests: [],
64 latestTurn: null,
65 createdAt: "2026-08-01T00:00:00.000Z",
66 updatedAt: "2026-08-20T00:00:00.000Z",
67 archivedAt: null,
68 settledOverride: null,
69 settledAt: null,
70 session: null,
71 latestUserMessageAt: "2026-08-20T00:00:00.000Z",
72 hasPendingApprovals: false,
73 hasPendingUserInput: false,
74 hasActionableProposedPlan: false,
75};
76​
77function prResult(pr: GitRunStackedActionResult["pr"]): Pick<GitRunStackedActionResult, "pr"> {
78 return { pr };
80​
81const makeDependencies = (
82 dispatch: OrchestratorV2Shape["dispatch"],
83 threadShell: OrchestrationThreadShell | null = thread,
84) =>
85 Layer.mergeAll(
86 Layer.mock(ProjectionSnapshotQuery)({
87 getThreadShellById: () => Effect.succeed(Option.fromNullishOr(threadShell)),
88 getProjectShellById: () => Effect.succeedSome(project),
89 }),
90 Layer.mock(OrchestratorV2)({
91 getThreadShell: () => Effect.succeed(threadShell ? v2PullRequestThread(threadShell) : null),
92 dispatch,
93 }),
94 );
95​
96const recordingDispatch = Effect.fn("recordingDispatch")(function* () {
97 const commands = yield* Ref.make<ReadonlyArray<OrchestrationCommand>>([]);
98 const dispatch: OrchestratorV2Shape["dispatch"] = (command) =>
99 Ref.update(commands, (recorded) => [...recorded, command]).pipe(
100 Effect.as({ sequence: 1, storedEvents: [] }),
101 );
102 return { commands, dispatch };
103});
104​
105describe("createdPullRequestKey", () => {
106 it("reads host and repository from the URL when it is recognisable", () => {
107 expect(
108 createdPullRequestKey(
109 prResult({
110 status: "created",
111 number: 12,
112 url: "https://github.com/Other/Fork/pull/12",
113 }),
114 project,
115 ),
116 ).toEqual({
117 host: "github.com",
118 repository: "other/fork",
119 number: 12,
120 url: "https://github.com/Other/Fork/pull/12",
121 });
122 });
123​
124 it("falls back to the project's host and repository for an unreadable URL", () => {
125 expect(
126 createdPullRequestKey(
127 prResult({ status: "opened_existing", number: 3, url: "https://ghe.internal/x/3" }),
128 project,
129 ),
130 ).toEqual({
131 host: "github.acme.test",
132 repository: "platform/api",
133 number: 3,
134 url: "https://ghe.internal/x/3",
135 });
136 expect(
137 createdPullRequestKey(
138 prResult({ status: "created", number: 3, url: "https://ghe.internal/x/3" }),
139 undefined,
140 ),
141 ).toBeNull();
142 });
143​
144 it("yields nothing when no pull request came out of the action", () => {
145 expect(
146 createdPullRequestKey(prResult({ status: "skipped_not_requested" }), project),
147 ).toBeNull();
148 expect(
149 createdPullRequestKey(
150 prResult({ status: "created", url: "https://github.com/a/b/pull/1" }),
151 project,
152 ),
153 ).toBeNull();
154 expect(createdPullRequestKey(prResult({ status: "created", number: 1 }), project)).toBeNull();
155 });
156});
157​
158describe("linkCreatedPullRequest", () => {
159 it.effect("links a created pull request to the thread with source created", () =>
160 Effect.gen(function* () {
161 const { commands, dispatch } = yield* recordingDispatch();
162 yield* linkCreatedPullRequest({
163 threadId: THREAD_ID,
164 result: prResult({
165 status: "created",
166 number: 42,
167 url: "https://github.com/t3tools/t3code/pull/42",
168 }),
169 commandId,
170 }).pipe(Effect.provide(makeDependencies(dispatch)));
171​
172 expect(yield* Ref.get(commands)).toEqual([
173 {
174 type: "thread.pull-request.link",
175 commandId: "server:pr-created-link:test",
176 threadId: THREAD_ID,
177 host: "github.com",
178 repository: "t3tools/t3code",
179 number: 42,
180 url: "https://github.com/t3tools/t3code/pull/42",
181 source: "created",
182 },
183 ]);
184 }),
185 );
186​
187 it.effect("dispatches nothing when the action produced no pull request", () =>
188 Effect.gen(function* () {
189 const { commands, dispatch } = yield* recordingDispatch();
190 const dependencies = makeDependencies(dispatch);
191 yield* linkCreatedPullRequest({
192 threadId: THREAD_ID,
193 result: prResult({ status: "skipped_not_requested" }),
194 commandId,
195 }).pipe(Effect.provide(dependencies));
196 yield* linkCreatedPullRequest({
197 threadId: THREAD_ID,
198 result: prResult({ status: "created", url: "https://github.com/t3tools/t3code/pull/42" }),
199 commandId,
200 }).pipe(Effect.provide(dependencies));
201​
202 expect(yield* Ref.get(commands)).toEqual([]);
203 }),
204 );
205​
206 it.effect("swallows an already-linked rejection and other dispatch failures", () =>
207 Effect.gen(function* () {
208 const rejecting: OrchestratorV2Shape["dispatch"] = (command) =>
209 Effect.fail(
210 new OrchestratorDispatchError({
211 commandId: command.commandId,
212 commandType: command.type,
213 cause: "already linked",
214 }),
215 );
216 const result = prResult({
217 status: "opened_existing",
218 number: 7,
219 url: "https://github.com/t3tools/t3code/pull/7",
220 });
221 yield* linkCreatedPullRequest({ threadId: THREAD_ID, result, commandId }).pipe(
222 Effect.provide(makeDependencies(rejecting)),
223 );
224 yield* linkCreatedPullRequest({ threadId: THREAD_ID, result, commandId }).pipe(
225 Effect.provide(makeDependencies(() => Effect.die(new Error("engine down")))),
226 );
227 // A thread that vanished between the action and the link is not an error either.
228 yield* linkCreatedPullRequest({ threadId: THREAD_ID, result, commandId }).pipe(
229 Effect.provide(makeDependencies(() => Effect.die(new Error("unreachable")), null)),
230 );
231 }),
232 );
233});
234​
235it.effect(
236 "refreshes PR readers after a push from a thread or project, but not a local commit",
237 () =>
238 Effect.gen(function* () {
239 const refreshed: string[] = [];
240 const dependencies = Layer.mergeAll(
241 Layer.mock(OrchestratorV2)({
242 getThreadShell: () => Effect.succeed(v2PullRequestThread(thread)),
243 }),
244 Layer.mock(ProjectionSnapshotQuery)({
245 getProjectShellsWithoutEnrichment: () => Effect.succeed([project]),
246 }),
247 Layer.mock(PullRequestService)({
248 refreshAfterTurn: (id) =>
249 Effect.sync(() => {
250 refreshed.push(id);
251 }),
252 }),
253 );
254 yield* refreshPushedPullRequests(
255 { cwd: "/worktree", threadId: THREAD_ID },
256 { push: { status: "pushed" } },
257 ).pipe(Effect.provide(dependencies));
258 yield* refreshPushedPullRequests(
259 { cwd: project.workspaceRoot },
260 { push: { status: "pushed" } },
261 ).pipe(Effect.provide(dependencies));
262 yield* refreshPushedPullRequests({ cwd: "/unrelated" }, { push: { status: "pushed" } }).pipe(
263 Effect.provide(dependencies),
264 );
265 yield* refreshPushedPullRequests(
266 { cwd: project.workspaceRoot, threadId: THREAD_ID },
267 { push: { status: "skipped_not_requested" } },
268 ).pipe(Effect.provide(dependencies));
269 yield* refreshPushedPullRequests(
270 { cwd: "/draft-worktree", projectId: PROJECT_ID },
271 { push: { status: "pushed" } },
272 ).pipe(Effect.provide(dependencies));
273 expect(refreshed).toEqual([PROJECT_ID, PROJECT_ID, PROJECT_ID]);
274 }),
275);