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

VcsDriverContractHarness.ts

163 lines6.0 KB

1import { assert, it, describe } from "@effect/vitest";
2import {
3 Effect,
4 FileSystem,
5 Layer,
6 Path,
7 type PlatformError,
8 type Scope,
9 DateTime,
10 Option,
11} from "effect";
12​
13import type { VcsDriverKind } from "@t3tools/contracts";
14import * as VcsDriver from "../VcsDriver.ts";
15​
16export interface VcsDriverFixture<R, E> {
17 readonly createRepo: (cwd: string) => Effect.Effect<void, E, R>;
18 readonly writeFile: (
19 cwd: string,
20 relativePath: string,
21 contents: string,
22 ) => Effect.Effect<void, E, R | FileSystem.FileSystem | Path.Path>;
23 readonly trackFile?: (cwd: string, relativePath: string) => Effect.Effect<void, E, R>;
24 readonly commit?: (cwd: string, message: string) => Effect.Effect<void, E, R>;
25 readonly ignorePath: (
26 cwd: string,
27 pattern: string,
28 ) => Effect.Effect<void, E, R | FileSystem.FileSystem | Path.Path>;
30​
31export interface VcsDriverContractSuiteInput<R, E> {
32 readonly name: string;
33 readonly kind: VcsDriverKind;
34 readonly layer: Layer.Layer<
35 VcsDriver.VcsDriver | R | FileSystem.FileSystem | Path.Path,
36 E,
37 never
38 >;
39 readonly fixture: VcsDriverFixture<R, E>;
41​
42export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInput<R, E>) {
43 const makeTmpDir = (
44 prefix = `t3-${input.kind}-vcs-contract-`,
45 ): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
46 Effect.gen(function* () {
47 const fileSystem = yield* FileSystem.FileSystem;
48 return yield* fileSystem.makeTempDirectoryScoped({ prefix });
49 });
50​
51 it.layer(input.layer)(`${input.name} VCS driver contract`, (it) => {
52 describe("repository detection", () => {
53 it.effect("returns null outside a repository", () =>
54 Effect.gen(function* () {
55 const cwd = yield* makeTmpDir();
56 const driver = yield* VcsDriver.VcsDriver;
57​
58 assert.equal(yield* driver.detectRepository(cwd), null);
59 assert.equal(yield* driver.isInsideWorkTree(cwd), false);
60 }),
61 );
62​
63 it.effect("detects repository identity inside a repository and nested directories", () =>
64 Effect.gen(function* () {
65 const cwd = yield* makeTmpDir();
66 const driver = yield* VcsDriver.VcsDriver;
67​
68 yield* input.fixture.createRepo(cwd);
69 yield* input.fixture.writeFile(cwd, "src/index.ts", "export const value = 1;\n");
70 const identity = yield* driver.detectRepository(cwd);
71 assert.equal(identity?.kind, input.kind);
72 assert.isTrue(identity?.rootPath.endsWith(cwd));
73 assert.equal(identity?.freshness.source, "live-local");
74 assert.isTrue(DateTime.isDateTime(identity?.freshness.observedAt));
75 assert.isTrue(Option.isNone(identity?.freshness.expiresAt ?? Option.none()));
76 assert.equal(yield* driver.isInsideWorkTree(cwd), true);
77​
78 const path = yield* Path.Path;
79 const nestedDir = path.join(cwd, "src");
80 const nestedIdentity = yield* driver.detectRepository(nestedDir);
81 assert.equal(nestedIdentity?.rootPath, identity?.rootPath);
82 assert.equal(yield* driver.isInsideWorkTree(nestedDir), true);
83 }),
84 );
85 });
86​
87 describe("workspace files", () => {
88 it.effect("lists tracked and untracked non-ignored files", () =>
89 Effect.gen(function* () {
90 const cwd = yield* makeTmpDir();
91 const driver = yield* VcsDriver.VcsDriver;
92​
93 yield* input.fixture.createRepo(cwd);
94 yield* input.fixture.writeFile(cwd, "tracked.ts", "export const tracked = true;\n");
95 if (input.fixture.trackFile && input.fixture.commit) {
96 yield* input.fixture.trackFile(cwd, "tracked.ts");
97 yield* input.fixture.commit(cwd, "Track file");
98 }
99 yield* input.fixture.writeFile(cwd, "untracked.ts", "export const untracked = true;\n");
100​
101 const result = yield* driver.listWorkspaceFiles(cwd);
102​
103 assert.include(result.paths, "tracked.ts");
104 assert.include(result.paths, "untracked.ts");
105 assert.equal(result.truncated, false);
106 assert.equal(result.freshness.source, "live-local");
107 assert.isTrue(DateTime.isDateTime(result.freshness.observedAt));
108 assert.isTrue(Option.isNone(result.freshness.expiresAt));
109 }),
110 );
111​
112 it.effect("excludes ignored files from workspace listing", () =>
113 Effect.gen(function* () {
114 const cwd = yield* makeTmpDir();
115 const driver = yield* VcsDriver.VcsDriver;
116​
117 yield* input.fixture.createRepo(cwd);
118 yield* input.fixture.ignorePath(cwd, "*.log");
119 yield* input.fixture.writeFile(cwd, "included.ts", "export const included = true;\n");
120 yield* input.fixture.writeFile(cwd, "debug.log", "ignore me\n");
121 yield* input.fixture.writeFile(cwd, "nested/error.log", "ignore me too\n");
122​
123 const result = yield* driver.listWorkspaceFiles(cwd);
124​
125 assert.include(result.paths, "included.ts");
126 assert.notInclude(result.paths, "debug.log");
127 assert.notInclude(result.paths, "nested/error.log");
128 }),
129 );
130 });
131​
132 describe("ignored path filtering", () => {
133 it.effect("filters ignored paths", () =>
134 Effect.gen(function* () {
135 const cwd = yield* makeTmpDir();
136 const driver = yield* VcsDriver.VcsDriver;
137​
138 yield* input.fixture.createRepo(cwd);
139 yield* input.fixture.ignorePath(cwd, "*.log");
140​
141 const result = yield* driver.filterIgnoredPaths(cwd, [
142 "keep.ts",
143 "debug.log",
144 "nested/error.log",
145 ]);
146​
147 assert.deepStrictEqual(result, ["keep.ts"]);
148 }),
149 );
150​
151 it.effect("returns empty input unchanged", () =>
152 Effect.gen(function* () {
153 const cwd = yield* makeTmpDir();
154 const driver = yield* VcsDriver.VcsDriver;
155​
156 yield* input.fixture.createRepo(cwd);
157​
158 assert.deepStrictEqual(yield* driver.filterIgnoredPaths(cwd, []), []);
159 }),
160 );
161 });
162 });