Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as DateTime from "effect/DateTime";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import { ChildProcessSpawner } from "effect/unstable/process";
8import { VcsRepositoryDetectionError } from "@piku/contracts";
9
10import * as ServerConfig from "../config.ts";
11import type * as VcsDriver from "../vcs/VcsDriver.ts";
12import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
13import * as VcsProcess from "../vcs/VcsProcess.ts";
14import * as GitHubCli from "./GitHubCli.ts";
15import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
16
17const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z");
18
19const processOutput = (
20 stdout: string,
21 options?: {
22 readonly stderr?: string;
23 readonly exitCode?: ChildProcessSpawner.ExitCode;
24 },
25): VcsProcess.VcsProcessOutput => ({
26 exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
27 stdout,
28 stderr: options?.stderr ?? "",
29 stdoutTruncated: false,
30 stderrTruncated: false,
31});
32
33function makeRegistry(input: {
34 readonly remotes: ReadonlyArray<{
35 readonly name: string;
36 readonly url: string;
37 }>;
38 readonly process?: Partial<VcsProcess.VcsProcess["Service"]>;
39 readonly resolve?: VcsDriverRegistry.VcsDriverRegistry["Service"]["resolve"];
40}) {
41 const driver = {
42 listRemotes: () =>
43 Effect.succeed({
44 remotes: input.remotes.map((remote) => ({
45 ...remote,
46 pushUrl: Option.none(),
47 isPrimary: remote.name === "origin",
48 })),
49 freshness: {
50 source: "live-local" as const,
51 observedAt: TEST_EPOCH,
52 expiresAt: Option.none(),
53 },
54 }),
55 } satisfies Partial<VcsDriver.VcsDriver["Service"]>;
56
57 const registryLayer = Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
58 get: () => Effect.succeed(driver as unknown as VcsDriver.VcsDriver["Service"]),
59 resolve:
60 input.resolve ??
61 (() =>
62 Effect.succeed({
63 kind: "git",
64 repository: {
65 kind: "git",
66 rootPath: "/repo",
67 metadataPath: null,
68 freshness: {
69 source: "live-local" as const,
70 observedAt: TEST_EPOCH,
71 expiresAt: Option.none(),
72 },
73 },
74 driver: driver as unknown as VcsDriver.VcsDriver["Service"],
75 })),
76 });
77
78 const processLayer = Layer.mock(VcsProcess.VcsProcess)({
79 run: () => Effect.succeed(processOutput("")),
80 ...input.process,
81 });
82
83 return SourceControlProviderRegistry.make.pipe(
84 Effect.provide(
85 Layer.mergeAll(
86 registryLayer,
87 processLayer,
88 Layer.mock(GitHubCli.GitHubCli)({}),
89 ServerConfig.layerTest(process.cwd(), {
90 prefix: "piku-source-control-registry-test-",
91 }).pipe(Layer.provide(NodeServices.layer)),
92 ),
93 ),
94 );
95}
96
97it.effect("routes GitHub remotes to the GitHub provider", () =>
98 Effect.gen(function* () {
99 const registry = yield* makeRegistry({
100 remotes: [{ name: "origin", url: "git@github.com:pingdotgg/pikucode.git" }],
101 });
102
103 const provider = yield* registry.resolve({ cwd: "/repo" });
104
105 assert.strictEqual(provider.kind, "github");
106 }),
107);
108
109it.effect("routes directly by provider kind for remote-first workflows", () =>
110 Effect.gen(function* () {
111 const registry = yield* makeRegistry({
112 remotes: [],
113 });
114
115 const provider = yield* registry.get("github");
116
117 assert.strictEqual(provider.kind, "github");
118 }),
119);
120
121it.effect("includes the request cwd when an unregistered provider is used", () =>
122 Effect.gen(function* () {
123 const registry = yield* makeRegistry({ remotes: [] });
124 const provider = yield* registry.get("unknown");
125
126 const error = yield* provider
127 .getChangeRequest({ cwd: "/repo", reference: "#42" })
128 .pipe(Effect.flip);
129
130 assert.strictEqual(error.provider, "unknown");
131 assert.strictEqual(error.operation, "getChangeRequest");
132 assert.strictEqual(error.cwd, "/repo");
133 assert.strictEqual(error.reference, "#42");
134 }),
135);
136
137it.effect("retains VCS detection failures with structured cwd context", () =>
138 Effect.gen(function* () {
139 const cause = new VcsRepositoryDetectionError({
140 operation: "resolve",
141 cwd: "/repo",
142 detail: "raw VCS detection failure",
143 cause: new Error("raw nested failure"),
144 });
145 const registry = yield* makeRegistry({
146 remotes: [],
147 resolve: () => Effect.fail(cause),
148 });
149
150 const error = yield* registry.resolve({ cwd: "/repo" }).pipe(Effect.flip);
151
152 assert.strictEqual(error.provider, "unknown");
153 assert.strictEqual(error.operation, "detectProvider");
154 assert.strictEqual(error.cwd, "/repo");
155 assert.strictEqual(error.detail, "Failed to detect source control provider.");
156 assert.strictEqual(error.cause, cause);
157 assert.equal(error.message.includes(cause.message), false);
158 }),
159);
160
161it.effect("falls back to a non-origin remote when origin is not configured", () =>
162 Effect.gen(function* () {
163 const registry = yield* makeRegistry({
164 remotes: [{ name: "upstream", url: "git@github.com:pingdotgg/pikucode.git" }],
165 });
166
167 const provider = yield* registry.resolve({ cwd: "/repo" });
168
169 assert.strictEqual(provider.kind, "github");
170 }),
171);