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