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

GitHubSourceControlProvider.test.ts

402 lines10.8 KB

1import { assert, it } from "@effect/vitest";
2import * as DateTime from "effect/DateTime";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import { ChildProcessSpawner } from "effect/unstable/process";
7​
8import * as VcsProcess from "../vcs/VcsProcess.ts";
9import * as GitHubCli from "./GitHubCli.ts";
10import { parseGitHubAuthStatus } from "./gitHubAuthStatus.ts";
11import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
12​
13const processResult = (
14 stdout: string,
15 options?: {
16 readonly stderr?: string;
17 readonly exitCode?: ChildProcessSpawner.ExitCode;
18 },
19): VcsProcess.VcsProcessOutput => ({
20 exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
21 stdout,
22 stderr: options?.stderr ?? "",
23 stdoutTruncated: false,
24 stderrTruncated: false,
25});
26​
27function makeProvider(github: Partial<GitHubCli.GitHubCli["Service"]>) {
28 return GitHubSourceControlProvider.make.pipe(
29 Effect.provide(Layer.mock(GitHubCli.GitHubCli)(github)),
30 );
32​
33it.effect("maps GitHub PR summaries into provider-neutral change requests", () =>
34 Effect.gen(function* () {
35 const provider = yield* makeProvider({
36 getPullRequest: () =>
37 Effect.succeed({
38 number: 42,
39 title: "Add GitHub provider",
40 url: "https://github.com/pingdotgg/t3code/pull/42",
41 baseRefName: "main",
42 headRefName: "feature/source-control",
43 state: "open",
44 isCrossRepository: true,
45 headRepositoryNameWithOwner: "fork/t3code",
46 headRepositoryOwnerLogin: "fork",
47 }),
48 });
49​
50 const changeRequest = yield* provider.getChangeRequest({
51 cwd: "/repo",
52 reference: "42",
53 });
54​
55 assert.deepStrictEqual(changeRequest, {
56 provider: "github",
57 number: 42,
58 title: "Add GitHub provider",
59 url: "https://github.com/pingdotgg/t3code/pull/42",
60 baseRefName: "main",
61 headRefName: "feature/source-control",
62 state: "open",
63 closedAt: null,
64 mergedAt: null,
65 updatedAt: Option.none(),
66 isCrossRepository: true,
67 headRepositoryNameWithOwner: "fork/t3code",
68 headRepositoryOwnerLogin: "fork",
69 });
70 }),
71);
72​
73it.effect("adds safe request context while retaining GitHub CLI causes", () =>
74 Effect.gen(function* () {
75 const cause = new GitHubCli.GitHubPullRequestNotFoundError({
76 command: "gh",
77 cwd: "/repo",
78 cause: new Error("raw upstream detail that should remain in the cause"),
79 });
80 const provider = yield* makeProvider({
81 getPullRequest: () => Effect.fail(cause),
82 });
83​
84 const error = yield* provider
85 .getChangeRequest({
86 cwd: "/repo",
87 reference: "https://user:secret@github.com/pingdotgg/t3code/pull/42?token=secret#diff",
88 })
89 .pipe(Effect.flip);
90​
91 assert.deepStrictEqual(
92 {
93 provider: error.provider,
94 operation: error.operation,
95 command: error.command,
96 cwd: error.cwd,
97 reference: error.reference,
98 detail: error.detail,
99 },
100 {
101 provider: "github",
102 operation: "getChangeRequest",
103 command: "gh",
104 cwd: "/repo",
105 reference: "https://github.com/pingdotgg/t3code/pull/42",
106 detail: "Pull request not found. Check the PR number or URL and try again.",
107 },
108 );
109 assert.strictEqual(error.cause, cause);
110 assert.equal(error.message.includes("raw upstream detail"), false);
111 }),
112);
113​
114it.effect("uses gh json listing for non-open change request state queries", () =>
115 Effect.gen(function* () {
116 let executeArgs: ReadonlyArray<string> = [];
117 const provider = yield* makeProvider({
118 execute: (input) => {
119 executeArgs = input.args;
120 return Effect.succeed(
121 processResult(
122 JSON.stringify([
123 {
124 number: 7,
125 title: "Merged work",
126 url: "https://github.com/pingdotgg/t3code/pull/7",
127 baseRefName: "main",
128 headRefName: "feature/merged",
129 state: "merged",
130 mergedAt: "2026-01-01T00:00:00Z",
131 updatedAt: "2026-01-02T00:00:00.000Z",
132 },
133 ]),
134 ),
135 );
136 },
137 });
138​
139 const changeRequests = yield* provider.listChangeRequests({
140 cwd: "/repo",
141 headSelector: "feature/merged",
142 state: "all",
143 limit: 10,
144 });
145​
146 assert.deepStrictEqual(executeArgs, [
147 "pr",
148 "list",
149 "--head",
150 "feature/merged",
151 "--state",
152 "all",
153 "--limit",
154 "10",
155 "--json",
156 "number,title,url,baseRefName,headRefName,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
157 ]);
158 assert.strictEqual(changeRequests[0]?.provider, "github");
159 assert.strictEqual(changeRequests[0]?.state, "merged");
160 assert.strictEqual(changeRequests[0]?.mergedAt, "2026-01-01T00:00:00Z");
161 assert.deepStrictEqual(
162 changeRequests[0]?.updatedAt,
163 Option.some(DateTime.makeUnsafe("2026-01-02T00:00:00.000Z")),
164 );
165 }),
166);
167​
168it.effect("treats empty non-open change request listing output as no results", () =>
169 Effect.gen(function* () {
170 const provider = yield* makeProvider({
171 execute: () => Effect.succeed(processResult("")),
172 });
173​
174 const changeRequests = yield* provider.listChangeRequests({
175 cwd: "/repo",
176 headSelector: "feature/empty",
177 state: "all",
178 limit: 10,
179 });
180​
181 assert.deepStrictEqual(changeRequests, []);
182 }),
183);
184​
185it.effect("creates GitHub PRs through provider-neutral input names", () =>
186 Effect.gen(function* () {
187 let createInput: Parameters<GitHubCli.GitHubCli["Service"]["createPullRequest"]>[0] | null =
188 null;
189 const provider = yield* makeProvider({
190 createPullRequest: (input) => {
191 createInput = input;
192 return Effect.void;
193 },
194 });
195​
196 yield* provider.createChangeRequest({
197 cwd: "/repo",
198 baseRefName: "main",
199 headSelector: "owner:feature/provider",
200 title: "Provider PR",
201 bodyFile: "/tmp/body.md",
202 });
203​
204 assert.deepStrictEqual(createInput, {
205 cwd: "/repo",
206 baseBranch: "main",
207 headSelector: "owner:feature/provider",
208 title: "Provider PR",
209 bodyFile: "/tmp/body.md",
210 });
211 }),
212);
213​
214it("accepts active authenticated GitHub accounts when another account fails", () => {
215 const auth = GitHubSourceControlProvider.discovery.parseAuth(
216 processResult(
217 JSON.stringify({
218 hosts: {
219 "github.com": [
220 {
221 state: "success",
222 active: true,
223 host: "github.com",
224 login: "active-user",
225 tokenSource: "keyring",
226 gitProtocol: "ssh",
227 },
228 {
229 state: "error",
230 active: false,
231 host: "github.com",
232 login: "stale-user",
233 tokenSource: "keyring",
234 gitProtocol: "ssh",
235 error: "The token in keyring is invalid.",
236 },
237 ],
238 },
239 }),
240 ),
241 );
242​
243 assert.deepStrictEqual(
244 {
245 status: auth.status,
246 account: auth.account,
247 host: auth.host,
248 },
249 {
250 status: "authenticated",
251 account: Option.some("active-user"),
252 host: Option.some("github.com"),
253 },
254 );
255});
256​
257it("parses GitHub auth JSON from stdout when stderr has warnings", () => {
258 const auth = GitHubSourceControlProvider.discovery.parseAuth(
259 processResult(
260 JSON.stringify({
261 hosts: {
262 "github.com": [
263 {
264 state: "success",
265 active: true,
266 host: "github.com",
267 login: "active-user",
268 tokenSource: "keyring",
269 gitProtocol: "ssh",
270 },
271 ],
272 },
273 }),
274 { stderr: "warning: ignored diagnostic from gh\n" },
275 ),
276 );
277​
278 assert.deepStrictEqual(
279 {
280 status: auth.status,
281 account: auth.account,
282 host: auth.host,
283 },
284 {
285 status: "authenticated",
286 account: Option.some("active-user"),
287 host: Option.some("github.com"),
288 },
289 );
290});
291​
292it("parses GitHub auth status accounts by host and active state", () => {
293 assert.deepStrictEqual(
294 parseGitHubAuthStatus(
295 JSON.stringify({
296 hosts: {
297 "github.com": [
298 {
299 state: "success",
300 active: true,
301 host: "github.com",
302 login: "active-user",
303 tokenSource: "keyring",
304 gitProtocol: "ssh",
305 },
306 {
307 state: "error",
308 active: false,
309 host: "github.com",
310 login: "stale-user",
311 tokenSource: "keyring",
312 gitProtocol: "ssh",
313 },
314 ],
315 "github.example.test": [
316 {
317 state: "success",
318 active: false,
319 host: "github.example.test",
320 login: "enterprise-user",
321 tokenSource: "keyring",
322 gitProtocol: "ssh",
323 },
324 ],
325 },
326 }),
327 ).accounts,
328 [
329 {
330 host: "github.com",
331 account: "active-user",
332 authenticated: true,
333 active: true,
334 error: null,
335 },
336 {
337 host: "github.com",
338 account: "stale-user",
339 authenticated: false,
340 active: false,
341 error: null,
342 },
343 {
344 host: "github.example.test",
345 account: "enterprise-user",
346 authenticated: true,
347 active: false,
348 error: null,
349 },
350 ],
351 );
352});
353​
354it("reports unauthenticated when GitHub JSON has accounts but none are valid", () => {
355 const auth = GitHubSourceControlProvider.discovery.parseAuth(
356 processResult(
357 JSON.stringify({
358 hosts: {
359 "github.com": [
360 {
361 state: "error",
362 active: true,
363 host: "github.com",
364 login: "stale-user",
365 tokenSource: "keyring",
366 gitProtocol: "ssh",
367 error: "The token in keyring is invalid.",
368 },
369 ],
370 },
371 }),
372 ),
373 );
374​
375 assert.deepStrictEqual(
376 {
377 status: auth.status,
378 host: auth.host,
379 detail: auth.detail,
380 },
381 {
382 status: "unauthenticated",
383 host: Option.some("github.com"),
384 detail: Option.some("The token in keyring is invalid."),
385 },
386 );
387});
388​
389it("reports an update hint instead of unauthenticated when gh predates --json", () => {
390 const auth = GitHubSourceControlProvider.discovery.parseAuth(
391 processResult("", {
392 stderr: "unknown flag: --json\n\nUsage: gh auth status [flags]\n",
393 exitCode: ChildProcessSpawner.ExitCode(1),
394 }),
395 );
396​
397 assert.strictEqual(auth.status, "unknown");
398 assert.match(
399 Option.getOrElse(auth.detail, () => ""),
400 /2\.81\.0/,
401 );
402});