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