Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { DateTime, Effect, Layer, Option } from "effect";
3
4import { GitHubCli } from "./GitHubCli.ts";
5import { GitLabCli } from "./GitLabCli.ts";
6import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
7import { VcsDriverRegistry } from "../vcs/VcsDriverRegistry.ts";
8import type { VcsDriverShape } from "../vcs/VcsDriver.ts";
9
10const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z");
11
12function makeRegistry(input: {
13 readonly remotes: ReadonlyArray<{
14 readonly name: string;
15 readonly url: string;
16 }>;
17}) {
18 const driver = {
19 listRemotes: () =>
20 Effect.succeed({
21 remotes: input.remotes.map((remote) => ({
22 ...remote,
23 pushUrl: Option.none(),
24 isPrimary: remote.name === "origin",
25 })),
26 freshness: {
27 source: "live-local" as const,
28 observedAt: TEST_EPOCH,
29 expiresAt: Option.none(),
30 },
31 }),
32 } satisfies Partial<VcsDriverShape>;
33
34 const registryLayer = Layer.mock(VcsDriverRegistry)({
35 get: () => Effect.succeed(driver as unknown as VcsDriverShape),
36 resolve: () =>
37 Effect.succeed({
38 kind: "git",
39 repository: {
40 kind: "git",
41 rootPath: "/repo",
42 metadataPath: null,
43 freshness: {
44 source: "live-local" as const,
45 observedAt: TEST_EPOCH,
46 expiresAt: Option.none(),
47 },
48 },
49 driver: driver as unknown as VcsDriverShape,
50 }),
51 });
52
53 return SourceControlProviderRegistry.make().pipe(
54 Effect.provide(
55 Layer.mergeAll(registryLayer, Layer.mock(GitHubCli)({}), Layer.mock(GitLabCli)({})),
56 ),
57 );
58}
59
60it.effect("routes GitHub remotes to the GitHub provider", () =>
61 Effect.gen(function* () {
62 const registry = yield* makeRegistry({
63 remotes: [{ name: "origin", url: "git@github.com:pingdotgg/t3code.git" }],
64 });
65
66 const provider = yield* registry.resolve({ cwd: "/repo" });
67
68 assert.strictEqual(provider.kind, "github");
69 }),
70);
71
72it.effect("routes directly by provider kind for remote-first workflows", () =>
73 Effect.gen(function* () {
74 const registry = yield* makeRegistry({
75 remotes: [],
76 });
77
78 const provider = yield* registry.get("github");
79
80 assert.strictEqual(provider.kind, "github");
81 }),
82);
83
84it.effect("routes GitLab remotes to the GitLab provider", () =>
85 Effect.gen(function* () {
86 const registry = yield* makeRegistry({
87 remotes: [{ name: "origin", url: "git@gitlab.com:group/project.git" }],
88 });
89
90 const provider = yield* registry.resolve({ cwd: "/repo" });
91
92 assert.strictEqual(provider.kind, "gitlab");
93 }),
94);
95
96it.effect("falls back to a non-origin remote when origin is not configured", () =>
97 Effect.gen(function* () {
98 const registry = yield* makeRegistry({
99 remotes: [{ name: "upstream", url: "https://dev.azure.com/acme/project/_git/repo" }],
100 });
101
102 const provider = yield* registry.resolve({ cwd: "/repo" });
103
104 assert.strictEqual(provider.kind, "azure-devops");
105 }),
106);