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

TanStackSourceSourceControlProvider.test.ts

132 lines4.9 KB

1import { assert, describe, it } from "@effect/vitest";
2import { GitCommandError } from "@t3tools/contracts";
3import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7​
8import * as TanStackSourceApi from "./TanStackSourceApi.ts";
9import * as TanStackSourceSourceControlProvider from "./TanStackSourceSourceControlProvider.ts";
10​
11const REMOTE = "https://source.tanstack.com/@acme/web.git";
12const OWNER_KEY = "branch.pr-42.t3TanstackSourcePull";
13​
14/** Pull request #42 of acme/web, opened from a fork, so it checks out as `pr-42`. */
15const PULL = {
16 number: 42,
17 title: "Fix the thing",
18 state: "open",
19 draft: false,
20 author: "bob",
21 head: "fix-thing",
22 createdAt: 1_000,
23 body: null,
24 base: "main",
25 headRepo: "bob/web",
26 headSha: "c".repeat(40),
27 baseSha: "b".repeat(40),
28 mergeCommitSha: null,
29 mergedAt: null,
30 mergedBy: null,
31 mergeable: null,
32 changedFiles: null,
33};
34​
35function checkout(options: {
36 readonly recordedOwner: string | null;
37 readonly isAncestor: boolean;
38}) {
39 const git: Array<string> = [];
40 const config = new Map<string, string>(
41 options.recordedOwner === null ? [] : [[OWNER_KEY, options.recordedOwner]],
42 );
43 const api = TanStackSourceApi.TanStackSourceApi.of({
44 request: (input) =>
45 input.path === "/repos/acme/web/pulls/42"
46 ? Effect.succeed(JSON.stringify(PULL))
47 : Effect.die(`unexpected ${input.path}`),
48 response: () => Effect.die("unexpected response read"),
49 });
50 const host = TestSourceControlHost.layer({
51 git: {
52 resolvePrimaryRemoteName: () => Effect.succeed("origin"),
53 ensureRemote: () => Effect.succeed("origin"),
54 readConfigValue: (_cwd, key) =>
55 Effect.succeed(key === "remote.origin.url" ? REMOTE : (config.get(key) ?? null)),
56 writeConfigValue: (input) => Effect.sync(() => void config.set(input.key, input.value)),
57 listLocalBranchNames: () => Effect.succeed(["main", "pr-42"]),
58 createBranch: (input) => Effect.sync(() => void git.push(`create ${input.branch}`)),
59 switchRef: (input) =>
60 Effect.sync(() => {
61 git.push(`switch ${input.refName}`);
62 return { refName: input.refName };
63 }),
64 execute: (input) =>
65 input.args[0] === "merge-base" && !options.isAncestor
66 ? Effect.fail(
67 new GitCommandError({
68 operation: input.operation,
69 command: "git",
70 cwd: input.cwd,
71 argumentCount: input.args.length,
72 exitCode: 1,
73 detail: "not an ancestor",
74 }),
75 )
76 : Effect.sync(() => {
77 git.push(input.args.join(" "));
78 return {
79 exitCode: 0,
80 stdout: "",
81 stderr: "",
82 stdoutTruncated: false,
83 stderrTruncated: false,
84 } as never;
85 }),
86 },
87 });
88 const run = TanStackSourceSourceControlProvider.make.pipe(
89 Effect.flatMap((provider) => provider.checkoutChangeRequest({ cwd: "/repo", reference: "42" })),
90 Effect.provideService(TanStackSourceApi.TanStackSourceApi, api),
91 Effect.provide(Layer.merge(host, FileSystem.layerNoop({}))),
92 );
93 return { run, git, config };
95​
96describe("TanStackSourceSourceControlProvider.checkoutChangeRequest", () => {
97 it.effect(
98 "refuses a pr-<n> branch T3 did not make for this pull request, and leaves it alone",
99 () => {
100 // An unrelated branch at the pull request's base is an ancestor of its head.
101 const { run, git, config } = checkout({ recordedOwner: null, isAncestor: true });
102 return Effect.gen(function* () {
103 const error = yield* Effect.flip(run);
104 assert.include(error.detail, "pr-42 was not made for pull request #42");
105 assert.isFalse(
106 git.some((command) => command.startsWith("checkout") || command.startsWith("switch")),
107 );
108 assert.isFalse(config.has(OWNER_KEY));
109 });
110 },
111 );
112​
113 it.effect("moves the branch it made for the pull request up to the new head", () => {
114 const { run, git } = checkout({ recordedOwner: "acme/web#42", isAncestor: true });
115 return Effect.gen(function* () {
116 yield* run;
117 assert.deepStrictEqual(
118 git.filter((command) => /^(checkout|switch|create) /u.test(command)),
119 ["checkout --no-track -B pr-42 refs/remotes/origin/pull/42 --", "switch pr-42"],
120 );
121 });
122 });
123​
124 it.effect("keeps local commits on its own branch rather than moving it", () => {
125 const { run, git } = checkout({ recordedOwner: "acme/web#42", isAncestor: false });
126 return Effect.gen(function* () {
127 const error = yield* Effect.flip(run);
128 assert.include(error.detail, "has commits pull request #42 does not");
129 assert.isFalse(git.some((command) => command.startsWith("checkout")));
130 });
131 });
132});