Open History to see the last commit
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 );
31}
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 updatedAt: Option.none(),
64 isCrossRepository: true,
65 headRepositoryNameWithOwner: "fork/t3code",
66 headRepositoryOwnerLogin: "fork",
67 });
68 }),
69);
70
71it.effect("adds safe request context while retaining GitHub CLI causes", () =>
72 Effect.gen(function* () {
73 const cause = new GitHubCli.GitHubPullRequestNotFoundError({
74 command: "gh",
75 cwd: "/repo",
76 cause: new Error("raw upstream detail that should remain in the cause"),
77 });
78 const provider = yield* makeProvider({
79 getPullRequest: () => Effect.fail(cause),
80 });
81
82 const error = yield* provider
83 .getChangeRequest({
84 cwd: "/repo",
85 reference: "https://user:secret@github.com/pingdotgg/t3code/pull/42?token=secret#diff",
86 })
87 .pipe(Effect.flip);
88
89 assert.deepStrictEqual(
90 {
91 provider: error.provider,
92 operation: error.operation,
93 command: error.command,
94 cwd: error.cwd,
95 reference: error.reference,
96 detail: error.detail,
97 },
98 {
99 provider: "github",
100 operation: "getChangeRequest",
101 command: "gh",
102 cwd: "/repo",
103 reference: "https://github.com/pingdotgg/t3code/pull/42",
104 detail: "Pull request not found. Check the PR number or URL and try again.",
105 },
106 );
107 assert.strictEqual(error.cause, cause);
108 assert.equal(error.message.includes("raw upstream detail"), false);
109 }),
110);
111
112it.effect("uses gh json listing for non-open change request state queries", () =>
113 Effect.gen(function* () {
114 let executeArgs: ReadonlyArray<string> = [];
115 const provider = yield* makeProvider({
116 execute: (input) => {
117 executeArgs = input.args;
118 return Effect.succeed(
119 processResult(
120 JSON.stringify([
121 {
122 number: 7,
123 title: "Merged work",
124 url: "https://github.com/pingdotgg/t3code/pull/7",
125 baseRefName: "main",
126 headRefName: "feature/merged",
127 state: "merged",
128 updatedAt: "2026-01-02T00:00:00.000Z",
129 },
130 ]),
131 ),
132 );
133 },
134 });
135
136 const changeRequests = yield* provider.listChangeRequests({
137 cwd: "/repo",
138 headSelector: "feature/merged",
139 state: "all",
140 limit: 10,
141 });
142
143 assert.deepStrictEqual(executeArgs, [
144 "pr",
145 "list",
146 "--head",
147 "feature/merged",
148 "--state",
149 "all",
150 "--limit",
151 "10",
152 "--json",
153 "number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
154 ]);
155 assert.strictEqual(changeRequests[0]?.provider, "github");
156 assert.strictEqual(changeRequests[0]?.state, "merged");
157 assert.deepStrictEqual(
158 changeRequests[0]?.updatedAt,
159 Option.some(DateTime.makeUnsafe("2026-01-02T00:00:00.000Z")),
160 );
161 }),
162);
163
164it.effect("treats empty non-open change request listing output as no results", () =>
165 Effect.gen(function* () {
166 const provider = yield* makeProvider({
167 execute: () => Effect.succeed(processResult("")),
168 });
169
170 const changeRequests = yield* provider.listChangeRequests({
171 cwd: "/repo",
172 headSelector: "feature/empty",
173 state: "all",
174 limit: 10,
175 });
176
177 assert.deepStrictEqual(changeRequests, []);
178 }),
179);
180
181it.effect("drops transport ports from GitHub Enterprise auth targets", () =>
182 Effect.gen(function* () {
183 const listInputs: Array<Parameters<GitHubCli.GitHubCli["Service"]["listOpenPullRequests"]>[0]> =
184 [];
185 const provider = yield* makeProvider({
186 listOpenPullRequests: (input) => {
187 listInputs.push(input);
188 return Effect.succeed([]);
189 },
190 });
191
192 for (const remoteUrl of [
193 "https://github.example:8443/owner/repo.git",
194 "ssh://git@github.example:2222/owner/repo.git",
195 ]) {
196 yield* provider.listChangeRequests({
197 cwd: "/repo",
198 context: {
199 provider: {
200 kind: "github",
201 name: "GitHub Enterprise",
202 baseUrl: "https://github.example",
203 },
204 remoteName: "origin",
205 remoteUrl,
206 },
207 headSelector: "feature/accounts",
208 state: "open",
209 limit: 10,
210 });
211 }
212
213 assert.deepStrictEqual(
214 listInputs.map(({ host, repositories }) => ({ host, repositories })),
215 [
216 { host: "github.example", repositories: ["owner/repo"] },
217 { host: "github.example", repositories: ["owner/repo"] },
218 ],
219 );
220 }),
221);
222
223it.effect("creates GitHub PRs through provider-neutral input names", () =>
224 Effect.gen(function* () {
225 let createInput: Parameters<GitHubCli.GitHubCli["Service"]["createPullRequest"]>[0] | null =
226 null;
227 const provider = yield* makeProvider({
228 createPullRequest: (input) => {
229 createInput = input;
230 return Effect.void;
231 },
232 });
233
234 yield* provider.createChangeRequest({
235 cwd: "/repo",
236 baseRefName: "main",
237 headSelector: "owner:feature/provider",
238 title: "Provider PR",
239 bodyFile: "/tmp/body.md",
240 });
241
242 assert.deepStrictEqual(createInput, {
243 cwd: "/repo",
244 baseBranch: "main",
245 headSelector: "owner:feature/provider",
246 title: "Provider PR",
247 bodyFile: "/tmp/body.md",
248 });
249 }),
250);
251
252it("accepts active authenticated GitHub accounts when another account fails", () => {
253 const auth = GitHubSourceControlProvider.discovery.parseAuth(
254 processResult(
255 JSON.stringify({
256 hosts: {
257 "github.com": [
258 {
259 state: "success",
260 active: true,
261 host: "github.com",
262 login: "active-user",
263 tokenSource: "keyring",
264 gitProtocol: "ssh",
265 },
266 {
267 state: "error",
268 active: false,
269 host: "github.com",
270 login: "stale-user",
271 tokenSource: "keyring",
272 gitProtocol: "ssh",
273 error: "The token in keyring is invalid.",
274 },
275 ],
276 },
277 }),
278 ),
279 );
280
281 assert.deepStrictEqual(
282 {
283 status: auth.status,
284 account: auth.account,
285 host: auth.host,
286 },
287 {
288 status: "authenticated",
289 account: Option.some("active-user"),
290 host: Option.some("github.com"),
291 },
292 );
293});
294
295it("parses GitHub auth JSON from stdout when stderr has warnings", () => {
296 const auth = GitHubSourceControlProvider.discovery.parseAuth(
297 processResult(
298 JSON.stringify({
299 hosts: {
300 "github.com": [
301 {
302 state: "success",
303 active: true,
304 host: "github.com",
305 login: "active-user",
306 tokenSource: "keyring",
307 gitProtocol: "ssh",
308 },
309 ],
310 },
311 }),
312 { stderr: "warning: ignored diagnostic from gh\n" },
313 ),
314 );
315
316 assert.deepStrictEqual(
317 {
318 status: auth.status,
319 account: auth.account,
320 host: auth.host,
321 },
322 {
323 status: "authenticated",
324 account: Option.some("active-user"),
325 host: Option.some("github.com"),
326 },
327 );
328});
329
330it("parses GitHub auth status accounts by host and active state", () => {
331 assert.deepStrictEqual(
332 parseGitHubAuthStatus(
333 JSON.stringify({
334 hosts: {
335 "github.com": [
336 {
337 state: "success",
338 active: true,
339 host: "github.com",
340 login: "active-user",
341 tokenSource: "keyring",
342 gitProtocol: "ssh",
343 },
344 {
345 state: "error",
346 active: false,
347 host: "github.com",
348 login: "stale-user",
349 tokenSource: "keyring",
350 gitProtocol: "ssh",
351 },
352 ],
353 "github.example.test": [
354 {
355 state: "success",
356 active: false,
357 host: "github.example.test",
358 login: "enterprise-user",
359 tokenSource: "keyring",
360 gitProtocol: "ssh",
361 },
362 ],
363 },
364 }),
365 ).accounts,
366 [
367 {
368 host: "github.com",
369 account: "active-user",
370 authenticated: true,
371 active: true,
372 tokenSource: "keyring",
373 error: null,
374 },
375 {
376 host: "github.com",
377 account: "stale-user",
378 authenticated: false,
379 active: false,
380 tokenSource: "keyring",
381 error: null,
382 },
383 {
384 host: "github.example.test",
385 account: "enterprise-user",
386 authenticated: true,
387 active: false,
388 tokenSource: "keyring",
389 error: null,
390 },
391 ],
392 );
393});
394
395it("keeps duplicate GitHub logins distinct by token source", () => {
396 const auth = GitHubSourceControlProvider.discovery.parseAuth(
397 processResult(
398 JSON.stringify({
399 hosts: {
400 "github.com": [
401 {
402 state: "success",
403 active: true,
404 host: "github.com",
405 login: "DominicVonk",
406 tokenSource: "GITHUB_TOKEN",
407 },
408 {
409 state: "success",
410 active: false,
411 host: "github.com",
412 login: "DominicVonk",
413 tokenSource: "keyring",
414 },
415 ],
416 },
417 }),
418 ),
419 );
420
421 assert.deepStrictEqual(auth.githubAccounts, [
422 {
423 host: "github.com",
424 login: "DominicVonk",
425 tokenSource: "GITHUB_TOKEN",
426 active: true,
427 },
428 {
429 host: "github.com",
430 login: "DominicVonk",
431 tokenSource: "keyring",
432 active: false,
433 },
434 ]);
435});
436
437it("reports unauthenticated when GitHub JSON has accounts but none are valid", () => {
438 const auth = GitHubSourceControlProvider.discovery.parseAuth(
439 processResult(
440 JSON.stringify({
441 hosts: {
442 "github.com": [
443 {
444 state: "error",
445 active: true,
446 host: "github.com",
447 login: "stale-user",
448 tokenSource: "keyring",
449 gitProtocol: "ssh",
450 error: "The token in keyring is invalid.",
451 },
452 ],
453 },
454 }),
455 ),
456 );
457
458 assert.deepStrictEqual(
459 {
460 status: auth.status,
461 host: auth.host,
462 detail: auth.detail,
463 },
464 {
465 status: "unauthenticated",
466 host: Option.some("github.com"),
467 detail: Option.some("The token in keyring is invalid."),
468 },
469 );
470});