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

ThreadPullRequestService.test.ts

198 lines7.2 KB

1import {
2 ProjectId,
3 ProviderInstanceId,
4 SourceControlProviderKind,
5 ThreadId,
6 type OrchestrationProjectShell,
7 type OrchestrationV2ThreadShell,
8 type OrchestrationV2ServerCommand,
9} from "@t3tools/contracts";
10import { describe, expect, it } from "@effect/vitest";
11import * as Crypto from "effect/Crypto";
12import * as DateTime from "effect/DateTime";
13import * as Deferred from "effect/Deferred";
14import * as Effect from "effect/Effect";
15import * as FileSystem from "effect/FileSystem";
16import * as Layer from "effect/Layer";
17import * as Option from "effect/Option";
18import * as Stream from "effect/Stream";
19​
20import * as GitManager from "../git/GitManager.ts";
21import * as ProjectStore from "./ProjectStore.ts";
22import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
23import * as PullRequestService from "../pullRequest/PullRequestService.ts";
24import * as ServerActivation from "../serverActivation.ts";
25import * as Orchestrator from "./Orchestrator.ts";
26import * as ThreadPullRequestService from "./ThreadPullRequestService.ts";
27​
28describe("ThreadPullRequestServiceV2 project guard", () => {
29 it("rejects a pull-request result when the project root changes before dispatch", () => {
30 const currentProject = Option.some({
31 workspaceRoot: "/workspace/replaced",
32 } satisfies Pick<OrchestrationProjectShell, "workspaceRoot">);
33​
34 expect(
35 ThreadPullRequestService.projectWorkspaceMatchesSnapshot(
36 currentProject,
37 "/workspace/original",
38 ),
39 ).toBe(false);
40 });
41});
42​
43describe("ThreadPullRequestServiceV2 reads", () => {
44 const NOW = DateTime.makeUnsafe("2026-09-20T00:00:00.000Z");
45 const threadShell = (id: string): OrchestrationV2ThreadShell => {
46 const threadId = ThreadId.make(id);
47 return {
48 id: threadId,
49 projectId: ProjectId.make("project-1"),
50 title: id,
51 providerInstanceId: ProviderInstanceId.make("codex"),
52 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
53 runtimeMode: "full-access",
54 interactionMode: "default",
55 branch: null,
56 worktreePath: null,
57 activeProviderThreadId: null,
58 lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null },
59 forkedFrom: null,
60 createdBy: "user",
61 creationSource: "web",
62 activeRunId: null,
63 latestRunId: null,
64 status: "idle",
65 pendingRuntimeRequest: null,
66 latestVisibleMessage: null,
67 latestUserMessageAt: null,
68 hasActionableProposedPlan: false,
69 itemCount: 0,
70 visibleItemCount: 0,
71 createdAt: NOW,
72 updatedAt: NOW,
73 archivedAt: null,
74 settledOverride: null,
75 settledAt: null,
76 lastVisitedAt: null,
77 deletedAt: null,
78 };
79 };
80​
81 it.effect.each([
82 { state: "closed", sameBranch: false },
83 { state: "merged", sameBranch: false },
84 { state: "closed", sameBranch: true },
85 { state: "merged", sameBranch: true },
86 ] as const)(
87 "restores a $state automatic PR only for its own branch (sameBranch=$sameBranch)",
88 ({ state, sameBranch }) =>
89 Effect.scoped(
90 Effect.gen(function* () {
91 const savedReference = {
92 projectId: ProjectId.make("project-1"),
93 repository: "acme/project",
94 number: 42,
95 url: "https://github.com/acme/project/pull/42",
96 };
97 const thread = {
98 ...threadShell("branch-pr-thread"),
99 branch: "main",
100 branchPullRequest: savedReference,
101 linkedPullRequest: savedReference,
102 };
103 const project: OrchestrationProjectShell = {
104 id: thread.projectId,
105 title: "Project",
106 workspaceRoot: "/workspace/project",
107 defaultModelSelection: null,
108 scripts: [],
109 createdAt: "2026-08-01T00:00:00.000Z",
110 updatedAt: "2026-08-01T00:00:00.000Z",
111 };
112 const activation = yield* Deferred.make<void>();
113 const sweepStarted = yield* Deferred.make<void>();
114 const commands: Array<OrchestrationV2ServerCommand> = [];
115 const dependencies = Layer.mergeAll(
116 Layer.mock(Orchestrator.OrchestratorV2)({
117 streamDomainEvents: Stream.never,
118 getShellSnapshot: () =>
119 Deferred.succeed(sweepStarted, undefined).pipe(
120 Effect.as({
121 schemaVersion: 2,
122 snapshotSequence: 1,
123 threads: [thread],
124 archivedThreads: [],
125 }),
126 ),
127 dispatch: (command) =>
128 Effect.sync(() => {
129 commands.push(command);
130 return { sequence: 2, storedEvents: [] };
131 }),
132 }),
133 Layer.mock(ProjectStore.ProjectStoreV2)({
134 listShells: () => Effect.succeed([project]),
135 getShell: () => Effect.succeed(Option.some(project)),
136 }),
137 Layer.mock(GitManager.GitManager)({
138 branchPullRequest: () => Effect.succeed(null),
139 }),
140 Layer.mock(PullRequestService.PullRequestService)({
141 summary: () =>
142 Effect.succeed({
143 ...savedReference,
144 provider: SourceControlProviderKind.make("github"),
145 title: "Previous branch work",
146 state,
147 headBranch: sameBranch ? thread.branch : "feature/previous-work",
148 baseBranch: "release",
149 updatedAt: "2026-09-19T00:00:00.000Z",
150 }),
151 }),
152 Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({
153 resolve: () =>
154 Effect.succeed({
155 canonicalKey: "github.com/acme/project",
156 locator: {
157 source: "git-remote" as const,
158 remoteName: "origin",
159 remoteUrl: "git@github.com:acme/project.git",
160 },
161 provider: "github" as const,
162 owner: "acme",
163 name: "project",
164 }),
165 }),
166 Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
167 Layer.succeed(
168 Crypto.Crypto,
169 Crypto.make({
170 randomBytes: (size) => new Uint8Array(size).fill(1),
171 digest: (_algorithm, data) => Effect.succeed(data),
172 }),
173 ),
174 FileSystem.layerNoop({}),
175 );
176 yield* Effect.gen(function* () {
177 const service = yield* ThreadPullRequestService.make;
178 yield* service.start();
179 yield* Deferred.succeed(activation, undefined);
180 yield* Deferred.await(sweepStarted);
181 yield* service.drain;
182 }).pipe(Effect.provide(dependencies));
183​
184 if (sameBranch) {
185 expect(commands).toEqual([]);
186 } else {
187 expect(commands).toHaveLength(1);
188 expect(commands[0]).toMatchObject({
189 type: "thread.pull-request.sync",
190 threadId: thread.id,
191 branchPullRequest: null,
192 });
193 expect(commands[0]).not.toHaveProperty("linkedPullRequest");
194 }
195 }),
196 ),
197 );
198});