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

VcsDriverRegistry.test.ts

86 lines3.0 KB

1import { assert, it, describe } from "@effect/vitest";
2import { Effect, Layer } from "effect";
3import { ChildProcessSpawner } from "effect/unstable/process";
4​
5import * as VcsProcess from "./VcsProcess.ts";
6import * as VcsProjectConfig from "./VcsProjectConfig.ts";
7import * as VcsDriverRegistry from "./VcsDriverRegistry.ts";
8​
9const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
10 exitCode: ChildProcessSpawner.ExitCode(0),
11 stdout,
12 stderr: "",
13 stdoutTruncated: false,
14 stderrTruncated: false,
15});
16​
17describe("VcsDriverRegistry", () => {
18 it.effect("routes directly by VCS driver kind for non-repository workflows", () => {
19 const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make()).pipe(
20 Layer.provide(
21 Layer.mock(VcsProjectConfig.VcsProjectConfig)({
22 resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
23 }),
24 ),
25 Layer.provide(
26 Layer.mock(VcsProcess.VcsProcess)({
27 run: () => Effect.succeed(processOutput("")),
28 }),
29 ),
30 );
31​
32 return Effect.gen(function* () {
33 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
34 const driver = yield* registry.get("git");
35​
36 assert.strictEqual(driver.capabilities.kind, "git");
37 }).pipe(Effect.provide(layer));
38 });
39​
40 it.effect("caches repository detection for repeated resolves in the same cwd and kind", () => {
41 const calls: VcsProcess.VcsProcessInput[] = [];
42 const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make()).pipe(
43 Layer.provide(
44 Layer.mock(VcsProjectConfig.VcsProjectConfig)({
45 resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
46 }),
47 ),
48 Layer.provide(
49 Layer.mock(VcsProcess.VcsProcess)({
50 run: (input) =>
51 Effect.sync(() => {
52 calls.push(input);
53 const command = input.args.join(" ");
54 if (command === "rev-parse --is-inside-work-tree") {
55 return processOutput("true\n");
56 }
57 if (command === "rev-parse --show-toplevel") {
58 return processOutput("/repo\n");
59 }
60 if (command === "rev-parse --git-common-dir") {
61 return processOutput("/repo/.git\n");
62 }
63 return processOutput("");
64 }),
65 }),
66 ),
67 );
68​
69 return Effect.gen(function* () {
70 const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
71 const first = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
72 const second = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
73​
74 assert.equal(first.repository.rootPath, "/repo");
75 assert.equal(second.repository.rootPath, "/repo");
76 assert.deepStrictEqual(
77 calls.map((call) => call.args.join(" ")),
78 [
79 "rev-parse --is-inside-work-tree",
80 "rev-parse --show-toplevel",
81 "rev-parse --git-common-dir",
82 ],
83 );
84 }).pipe(Effect.provide(layer));
85 });
86});