Open History to see the last commit
1import { assert, describe, expect, it, vi } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4
5import { VcsRepositoryDetectionError } from "@t3tools/contracts";
6
7import * as GitManager from "./GitManager.ts";
8import * as GitWorkflowService from "./GitWorkflowService.ts";
9import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
10import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
11
12function makeLayer(input: {
13 readonly detect: VcsDriverRegistry.VcsDriverRegistry["Service"]["detect"];
14}) {
15 return GitWorkflowService.layer.pipe(
16 Layer.provide(
17 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
18 detect: input.detect,
19 }),
20 ),
21 Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})),
22 Layer.provide(Layer.mock(GitManager.GitManager)({})),
23 );
24}
25
26describe("GitWorkflowService", () => {
27 it.effect("returns an empty local status when no VCS repository is detected", () =>
28 Effect.gen(function* () {
29 const workflow = yield* GitWorkflowService.GitWorkflowService;
30 const status = yield* workflow.localStatus({ cwd: "/not-a-repo" });
31
32 assert.deepStrictEqual(status, {
33 isRepo: false,
34 hasPrimaryRemote: false,
35 isDefaultRef: false,
36 refName: null,
37 hasWorkingTreeChanges: false,
38 workingTree: {
39 files: [],
40 insertions: 0,
41 deletions: 0,
42 },
43 });
44 }).pipe(
45 Effect.provide(
46 makeLayer({
47 detect: () => Effect.succeed(null),
48 }),
49 ),
50 ),
51 );
52
53 it.effect("returns an empty full status when no VCS repository is detected", () =>
54 Effect.gen(function* () {
55 const workflow = yield* GitWorkflowService.GitWorkflowService;
56 const status = yield* workflow.status({ cwd: "/not-a-repo" });
57
58 assert.deepStrictEqual(status, {
59 isRepo: false,
60 hasPrimaryRemote: false,
61 isDefaultRef: false,
62 refName: null,
63 hasWorkingTreeChanges: false,
64 workingTree: {
65 files: [],
66 insertions: 0,
67 deletions: 0,
68 },
69 hasUpstream: false,
70 aheadCount: 0,
71 behindCount: 0,
72 aheadOfDefaultCount: 0,
73 pr: null,
74 });
75 }).pipe(
76 Effect.provide(
77 makeLayer({
78 detect: () => Effect.succeed(null),
79 }),
80 ),
81 ),
82 );
83
84 it.effect("does not call GitManager status methods when no VCS repository is detected", () => {
85 const localStatus = vi.fn();
86 const remoteStatus = vi.fn();
87 const status = vi.fn();
88
89 const testLayer = GitWorkflowService.layer.pipe(
90 Layer.provide(
91 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
92 detect: () => Effect.succeed(null),
93 }),
94 ),
95 Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})),
96 Layer.provide(
97 Layer.mock(GitManager.GitManager)({
98 localStatus,
99 remoteStatus,
100 status,
101 }),
102 ),
103 );
104
105 return Effect.gen(function* () {
106 const workflow = yield* GitWorkflowService.GitWorkflowService;
107 yield* workflow.localStatus({ cwd: "/not-a-repo" });
108 yield* workflow.remoteStatus({ cwd: "/not-a-repo" });
109 yield* workflow.status({ cwd: "/not-a-repo" });
110
111 assert.equal(localStatus.mock.calls.length, 0);
112 assert.equal(remoteStatus.mock.calls.length, 0);
113 assert.equal(status.mock.calls.length, 0);
114 }).pipe(Effect.provide(testLayer));
115 });
116
117 it.effect("returns an empty ref list when no VCS repository is detected", () =>
118 Effect.gen(function* () {
119 const workflow = yield* GitWorkflowService.GitWorkflowService;
120 const refs = yield* workflow.listRefs({ cwd: "/not-a-repo" });
121
122 assert.deepStrictEqual(refs, {
123 refs: [],
124 isRepo: false,
125 hasPrimaryRemote: false,
126 nextCursor: null,
127 totalCount: 0,
128 });
129 }).pipe(
130 Effect.provide(
131 makeLayer({
132 detect: () => Effect.succeed(null),
133 }),
134 ),
135 ),
136 );
137
138 it.effect("structures workflow detection failures without exposing upstream details", () => {
139 const cause = new VcsRepositoryDetectionError({
140 operation: "VcsDriverRegistry.detect",
141 cwd: "/repo",
142 detail: "upstream detail must stay in the cause chain",
143 });
144
145 return Effect.gen(function* () {
146 const workflow = yield* GitWorkflowService.GitWorkflowService;
147 const error = yield* workflow.status({ cwd: "/repo" }).pipe(Effect.flip);
148
149 expect(error).toMatchObject({
150 _tag: "GitManagerError",
151 operation: "GitWorkflowService.status",
152 cwd: "/repo",
153 detail: "Failed to detect a VCS repository for this Git workflow.",
154 });
155 expect(error.message).not.toContain(cause.detail);
156 }).pipe(
157 Effect.provide(
158 makeLayer({
159 detect: () => Effect.fail(cause),
160 }),
161 ),
162 );
163 });
164
165 it.effect("structures command detection failures without exposing upstream details", () => {
166 const cause = new VcsRepositoryDetectionError({
167 operation: "VcsDriverRegistry.detect",
168 cwd: "/repo",
169 detail: "upstream command detail must stay in the cause chain",
170 });
171
172 return Effect.gen(function* () {
173 const workflow = yield* GitWorkflowService.GitWorkflowService;
174 const error = yield* workflow.listRefs({ cwd: "/repo" }).pipe(Effect.flip);
175
176 expect(error).toMatchObject({
177 _tag: "GitCommandError",
178 operation: "GitWorkflowService.listRefs",
179 command: "vcs-route",
180 cwd: "/repo",
181 detail: "Failed to detect a VCS repository for this Git command.",
182 });
183 expect(error.message).not.toContain(cause.detail);
184 }).pipe(
185 Effect.provide(
186 makeLayer({
187 detect: () => Effect.fail(cause),
188 }),
189 ),
190 );
191 });
192});