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 SourceControlProviderKind,
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 Ref from "effect/Ref";
15
16import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
17import {
18 type PullRequestTestThread,
19 v2PullRequestThread,
20} from "../orchestration-v2/testkit/pullRequestFixtures.ts";
21import * as ProjectStore from "../orchestration-v2/ProjectStore.ts";
22import * as ProjectService from "../project/ProjectService.ts";
23import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
24import { refreshPushedPullRequests } from "./refreshPushedPullRequests.ts";
25import * as 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: SourceControlProviderKind.make("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: PullRequestTestThread = {
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 createdAt: "2026-08-01T00:00:00.000Z",
65 updatedAt: "2026-08-20T00:00:00.000Z",
66 archivedAt: null,
67 settledOverride: null,
68 settledAt: null,
69 latestUserMessageAt: "2026-08-20T00:00:00.000Z",
70};
71
72function prResult(pr: GitRunStackedActionResult["pr"]): Pick<GitRunStackedActionResult, "pr"> {
73 return { pr };
74}
75
76const layerDependenciesFor = (
77 dispatch: Orchestrator.OrchestratorV2Shape["dispatch"],
78 threadShell: PullRequestTestThread | null = thread,
79) =>
80 Layer.mergeAll(
81 // Only the tracked repository answers: anywhere else falls back to the project's identity.
82 Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({
83 resolve: (cwd) =>
84 Effect.succeed(
85 cwd === "/work/tracked"
86 ? {
87 ...project.repositoryIdentity!,
88 canonicalKey: "github.acme.test/platform/tracked",
89 locator: {
90 source: "git-remote" as const,
91 remoteName: "origin",
92 remoteUrl: "git@github.acme.test:Platform/Tracked.git",
93 },
94 displayName: "Platform/Tracked",
95 name: "Tracked",
96 }
97 : null,
98 ),
99 }),
100 Layer.mock(ProjectService.ProjectService)({
101 getShell: () => Effect.succeedSome(project),
102 }),
103 Layer.mock(Orchestrator.OrchestratorV2)({
104 getThreadShell: () => Effect.succeed(threadShell ? v2PullRequestThread(threadShell) : null),
105 dispatch,
106 }),
107 );
108
109const recordingDispatch = Effect.fn("recordingDispatch")(function* () {
110 const commands = yield* Ref.make<ReadonlyArray<OrchestrationCommand>>([]);
111 const dispatch: Orchestrator.OrchestratorV2Shape["dispatch"] = (command) =>
112 Ref.update(commands, (recorded) => [...recorded, command]).pipe(
113 Effect.as({ sequence: 1, storedEvents: [] }),
114 );
115 return { commands, dispatch };
116});
117
118describe("createdPullRequestKey", () => {
119 it("reads host and repository from the URL when it is recognisable", () => {
120 expect(
121 createdPullRequestKey(
122 prResult({
123 status: "created",
124 number: 12,
125 url: "https://github.com/Other/Fork/pull/12",
126 }),
127 project.repositoryIdentity,
128 ),
129 ).toEqual({
130 host: "github.com",
131 repository: "other/fork",
132 number: 12,
133 url: "https://github.com/Other/Fork/pull/12",
134 });
135 });
136
137 it("falls back to the project's host and repository for an unreadable URL", () => {
138 expect(
139 createdPullRequestKey(
140 prResult({ status: "opened_existing", number: 3, url: "https://ghe.internal/x/3" }),
141 project.repositoryIdentity,
142 ),
143 ).toEqual({
144 host: "github.acme.test",
145 repository: "platform/api",
146 number: 3,
147 url: "https://ghe.internal/x/3",
148 });
149 expect(
150 createdPullRequestKey(
151 prResult({ status: "created", number: 3, url: "https://ghe.internal/x/3" }),
152 undefined,
153 ),
154 ).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 cwd: "/work/project",
170 commandId,
171 }).pipe(Effect.provide(layerDependenciesFor(dispatch)));
172 // An unreadable URL is keyed by the repository the action ran in, not the project's.
173 yield* linkCreatedPullRequest({
174 threadId: THREAD_ID,
175 result: prResult({ status: "created", number: 7, url: "https://ghe.internal/x/7" }),
176 cwd: "/work/tracked",
177 commandId,
178 }).pipe(Effect.provide(layerDependenciesFor(dispatch)));
179
180 expect(
181 (yield* Ref.get(commands)).map((command) => "repository" in command && command.repository),
182 ).toEqual(["t3tools/t3code", "platform/tracked"]);
183 expect((yield* Ref.get(commands)).slice(0, 1)).toEqual([
184 {
185 type: "thread.pull-request.link",
186 commandId: "server:pr-created-link:test",
187 threadId: THREAD_ID,
188 host: "github.com",
189 repository: "t3tools/t3code",
190 number: 42,
191 url: "https://github.com/t3tools/t3code/pull/42",
192 source: "created",
193 },
194 ]);
195 }),
196 );
197});
198
199it.effect(
200 "refreshes PR readers after a push from a thread or project, but not a local commit",
201 () =>
202 Effect.gen(function* () {
203 const refreshed: Array<[string, boolean | undefined]> = [];
204 const layerDependencies = Layer.mergeAll(
205 Layer.mock(Orchestrator.OrchestratorV2)({
206 getThreadShell: () => Effect.succeed(v2PullRequestThread(thread)),
207 }),
208 Layer.mock(ProjectStore.ProjectStoreV2)({
209 listShells: () => Effect.succeed([project]),
210 }),
211 Layer.mock(PullRequestService.PullRequestService)({
212 refreshAfterTurn: (id, options) =>
213 Effect.sync(() => {
214 refreshed.push([id, options?.pushed]);
215 }),
216 }),
217 );
218 yield* refreshPushedPullRequests(
219 { cwd: "/worktree", threadId: THREAD_ID },
220 { push: { status: "pushed" } },
221 ).pipe(Effect.provide(layerDependencies));
222 yield* refreshPushedPullRequests(
223 { cwd: project.workspaceRoot },
224 { push: { status: "pushed" } },
225 ).pipe(Effect.provide(layerDependencies));
226 yield* refreshPushedPullRequests({ cwd: "/unrelated" }, { push: { status: "pushed" } }).pipe(
227 Effect.provide(layerDependencies),
228 );
229 yield* refreshPushedPullRequests(
230 { cwd: project.workspaceRoot, threadId: THREAD_ID },
231 { push: { status: "skipped_not_requested" } },
232 ).pipe(Effect.provide(layerDependencies));
233 yield* refreshPushedPullRequests(
234 { cwd: "/draft-worktree", projectId: PROJECT_ID },
235 { push: { status: "pushed" } },
236 ).pipe(Effect.provide(layerDependencies));
237 expect(refreshed).toEqual([
238 [PROJECT_ID, true],
239 [PROJECT_ID, true],
240 [PROJECT_ID, true],
241 ]);
242 }),
243);