@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 190 lines 6.98 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off
2 import * as NodeFS from "node:fs";
3 import * as NodeOS from "node:os";
4 import * as NodePath from "node:path";
5
6 import type { GitCommandError } from "@t3tools/contracts";
7
8 import { assert, expect, it } from "@effect/vitest";
9 import * as Effect from "effect/Effect";
10 import * as FileSystem from "effect/FileSystem";
11 import * as Layer from "effect/Layer";
12 import * as Option from "effect/Option";
13 import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
14 import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
15
16 import * as GitLabCli from "./GitLabCli.ts";
17 import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
18
19 it.effect("lists a nested-group fork's merge requests by source branch and names its project", () =>
20 Effect.gen(function* () {
21 const branch = "feature/NestedGroups";
22 const output = JSON.stringify([
23 {
24 iid: 2,
25 title: "Another subgroup's fork",
26 web_url: "https://gitlab.com/Group/Upstream/Repository/-/merge_requests/2",
27 target_branch: "main",
28 source_branch: branch,
29 state: "opened",
30 updated_at: "2026-04-08T15:00:00Z",
31 source_project_id: 102,
32 target_project_id: 100,
33 source_project: { path_with_namespace: "Group/Other/Fork" },
34 },
35 {
36 iid: 1,
37 title: "This subgroup's fork",
38 web_url: "https://gitlab.com/Group/Upstream/Repository/-/merge_requests/1",
39 target_branch: "main",
40 source_branch: branch,
41 state: "opened",
42 updated_at: "2026-04-07T15:00:00Z",
43 source_project_id: 101,
44 target_project_id: 100,
45 source_project: { path_with_namespace: "Group/Subgroup/Fork" },
46 },
47 ]);
48 const calls: SourceControlHost.SourceControlProcessInput[] = [];
49 const provider = yield* GitLabSourceControlProvider.make.pipe(
50 Effect.provide(
51 GitLabCli.layer.pipe(
52 Layer.provideMerge(
53 TestSourceControlHost.layer({
54 process: {
55 run: (input) =>
56 Effect.sync(() => {
57 calls.push(input);
58 return TestSourceControlHost.processOutput(output);
59 }),
60 },
61 }),
62 ),
63 Layer.provide(FileSystem.layerNoop({})),
64 ),
65 ),
66 );
67
68 for (const headSelector of [branch, `Group/Subgroup:${branch}`]) {
69 const changeRequests = yield* provider.listChangeRequests({
70 cwd: "/repo",
71 headSelector,
72 state: "all",
73 limit: 20,
74 });
75 expect(changeRequests).toMatchObject([
76 {
77 number: 2,
78 headRefName: branch,
79 isCrossRepository: true,
80 headRepositoryNameWithOwner: "Group/Other/Fork",
81 },
82 {
83 number: 1,
84 url: "https://gitlab.com/Group/Upstream/Repository/-/merge_requests/1",
85 headRefName: branch,
86 isCrossRepository: true,
87 headRepositoryNameWithOwner: "Group/Subgroup/Fork",
88 },
89 ]);
90 }
91 assert.lengthOf(calls, 2);
92 for (const call of calls) {
93 expect(call.command).toBe("glab");
94 expect(call.args).toEqual([
95 "mr",
96 "list",
97 "--source-branch",
98 branch,
99 "--all",
100 "--per-page",
101 "20",
102 "--output",
103 "json",
104 ]);
105 }
106 }),
107 );
108
109 const withRepository = <A, E>(
110 test: (input: {
111 readonly git: (...args: string[]) => Effect.Effect<string, GitCommandError>;
112 readonly write: (path: string, contents: string) => Effect.Effect<void>;
113 readonly readTemplate: (treeish?: string) => Effect.Effect<Option.Option<string>>;
114 }) => Effect.Effect<A, E>,
115 ) =>
116 Effect.gen(function* () {
117 const host = yield* SourceControlHost.SourceControlHost;
118 const cwd = yield* Effect.acquireRelease(
119 Effect.sync(() => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-gitlab-template-"))),
120 (directory) => Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true })),
121 );
122 const git = (...args: string[]) =>
123 host.git
124 .execute({ operation: "GitLabTemplate.test", cwd, args })
125 .pipe(Effect.map((result) => result.stdout.trim()));
126 const write = (path: string, contents: string) =>
127 Effect.sync(() => {
128 NodeFS.mkdirSync(NodePath.dirname(NodePath.join(cwd, path)), { recursive: true });
129 NodeFS.writeFileSync(NodePath.join(cwd, path), contents);
130 });
131 yield* git("init", "--initial-branch=main");
132 yield* git("config", "user.email", "test@example.com");
133 yield* git("config", "user.name", "Test User");
134 yield* write("README.md", "hello\n");
135 yield* git("add", "README.md");
136 yield* git("commit", "-m", "Initial commit");
137 const provider = yield* GitLabSourceControlProvider.make.pipe(
138 Effect.provide(Layer.mock(GitLabCli.GitLabCli)({})),
139 );
140 return yield* test({
141 git,
142 write,
143 readTemplate: (treeish = "HEAD") => provider.readChangeRequestTemplate!({ cwd, treeish }),
144 });
145 }).pipe(Effect.scoped, Effect.provide(TestSourceControlHost.layerWithRealGit()));
146
147 const DEFAULT_TEMPLATE = ".gitlab/merge_request_templates/Default.md";
148
149 it.effect("ignores unavailable or non-regular GitLab default templates", () =>
150 withRepository(({ git, write, readTemplate }) =>
151 Effect.gen(function* () {
152 expect(yield* readTemplate()).toEqual(Option.none());
153
154 yield* write(".gitlab/merge_request_templates/Bugfix.md", "Not the default");
155 yield* git("add", ".gitlab");
156 yield* git("commit", "-m", "Add a selectable template");
157 expect(yield* readTemplate()).toEqual(Option.none());
158
159 yield* write(DEFAULT_TEMPLATE, " \n\n");
160 yield* git("add", ".gitlab");
161 yield* git("commit", "-m", "Add a blank default");
162 expect(yield* readTemplate()).toEqual(Option.none());
163
164 // Index modes exercise committed symlinks and submodules even on hosts without symlinks.
165 const blob = yield* git("rev-parse", "HEAD:README.md");
166 yield* git("update-index", "--cacheinfo", "120000", blob, DEFAULT_TEMPLATE);
167 yield* git("commit", "-m", "Replace default with a symlink");
168 expect(yield* readTemplate()).toEqual(Option.none());
169
170 const commit = yield* git("rev-parse", "HEAD");
171 yield* git("update-index", "--cacheinfo", "160000", commit, DEFAULT_TEMPLATE);
172 yield* git("commit", "-m", "Replace default with a submodule");
173 expect(yield* readTemplate()).toEqual(Option.none());
174 expect(yield* readTemplate("missing-base")).toEqual(Option.none());
175 }),
176 ),
177 );
178
179 it.effect("bounds GitLab template content and marks truncation", () =>
180 withRepository(({ git, write, readTemplate }) =>
181 Effect.gen(function* () {
182 const prefix = "a".repeat(8_000);
183 yield* write(DEFAULT_TEMPLATE, prefix + "EXCLUDED_SUFFIX");
184 yield* git("add", ".gitlab");
185 yield* git("commit", "-m", "Add a large template");
186
187 expect(yield* readTemplate()).toEqual(Option.some(prefix + "\n\n[truncated]"));
188 }),
189 ),
190 );