Tighten VCS driver test assertions

- Switch integration and contract tests to Effect/Vitest assertions
- Avoid platform-specific realpath assumptions in repository identity checks
- Use Path service for portable test file and worktree path handling
This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-05-02 14:22:13 -07:00
1 parent d8de522a0d
commit 6406e21cf6
2 files changed
+72 -75

No files matched your search

+51 -44
View File
@@ -1,9 +1,7 @@
import path from "node:path";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it } from "@effect/vitest";
import { Effect, FileSystem, Layer, PlatformError, Scope } from "effect";
import { describe, expect } from "vitest";
import { assert, it } from "@effect/vitest";
import { Effect, FileSystem, Layer, Path, PlatformError, Scope } from "effect";
import { describe } from "vitest";
import { GitCommandError } from "@t3tools/contracts";
import { ServerConfig } from "../config.ts";
@@ -26,12 +24,15 @@ const makeTmpDir = (
});
const writeTextFile = (
filePath: string,
cwd: string,
relativePath: string,
contents: string,
): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> =>
): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
yield* fileSystem.makeDirectory(path.dirname(filePath), { recursive: true });
const pathService = yield* Path.Path;
const filePath = pathService.join(cwd, relativePath);
yield* fileSystem.makeDirectory(pathService.dirname(filePath), { recursive: true });
yield* fileSystem.writeFileString(filePath, contents);
});
@@ -57,14 +58,14 @@ const initRepoWithCommit = (
): Effect.Effect<
{ readonly initialBranch: string },
GitCommandError | PlatformError.PlatformError,
GitVcsDriver.GitVcsDriver | FileSystem.FileSystem
GitVcsDriver.GitVcsDriver | FileSystem.FileSystem | Path.Path
> =>
Effect.gen(function* () {
const driver = yield* GitVcsDriver.GitVcsDriver;
yield* driver.initRepo({ cwd });
yield* git(cwd, ["config", "user.email", "[email protected]"]);
yield* git(cwd, ["config", "user.name", "Test"]);
yield* writeTextFile(path.join(cwd, "README.md"), "# test\n");
yield* writeTextFile(cwd, "README.md", "# test\n");
yield* git(cwd, ["add", "."]);
yield* git(cwd, ["commit", "-m", "initial commit"]);
const initialBranch = yield* git(cwd, ["branch", "--show-current"]);
@@ -78,10 +79,9 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
const cwd = yield* makeTmpDir();
const driver = yield* GitVcsDriver.GitVcsDriver;
expect(yield* driver.listBranches({ cwd })).toMatchObject({
isRepo: false,
branches: [],
});
const branches = yield* driver.listBranches({ cwd });
assert.equal(branches.isRepo, false);
assert.deepStrictEqual(branches.branches, []);
}),
);
@@ -89,14 +89,17 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const { initialBranch } = yield* initRepoWithCommit(cwd);
yield* writeTextFile(path.join(cwd, "feature.ts"), "export const value = 1;\n");
yield* writeTextFile(cwd, "feature.ts", "export const value = 1;\n");
const status = yield* (yield* GitVcsDriver.GitVcsDriver).statusDetails(cwd);
expect(status.isRepo).toBe(true);
expect(status.branch).toBe(initialBranch);
expect(status.hasWorkingTreeChanges).toBe(true);
expect(status.workingTree.files.map((file) => file.path)).toContain("feature.ts");
assert.equal(status.isRepo, true);
assert.equal(status.branch, initialBranch);
assert.equal(status.hasWorkingTreeChanges, true);
assert.include(
status.workingTree.files.map((file) => file.path),
"feature.ts",
);
}),
);
});
@@ -110,21 +113,20 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
yield* driver.createBranch({ cwd, branch: "feature/original" });
const checkout = yield* driver.checkoutBranch({ cwd, branch: "feature/original" });
expect(checkout.branch).toBe("feature/original");
assert.equal(checkout.branch, "feature/original");
const renamed = yield* driver.renameBranch({
cwd,
oldBranch: "feature/original",
newBranch: "feature/renamed",
});
expect(renamed.branch).toBe("feature/renamed");
expect(yield* git(cwd, ["branch", "--show-current"])).toBe("feature/renamed");
assert.equal(renamed.branch, "feature/renamed");
assert.equal(yield* git(cwd, ["branch", "--show-current"]), "feature/renamed");
const branches = yield* driver.listBranches({ cwd });
expect(branches.branches.find((branch) => branch.name === "feature/renamed")).toMatchObject(
{
current: true,
},
assert.equal(
branches.branches.find((branch) => branch.name === "feature/renamed")?.current,
true,
);
}),
);
@@ -142,7 +144,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
newBranch: current,
});
expect(result.branch).toBe(current);
assert.equal(result.branch, current);
}),
);
});
@@ -152,7 +154,11 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const { initialBranch } = yield* initRepoWithCommit(cwd);
const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "feature-worktree");
const pathService = yield* Path.Path;
const worktreePath = pathService.join(
yield* makeTmpDir("git-worktrees-"),
"feature-worktree",
);
const driver = yield* GitVcsDriver.GitVcsDriver;
const created = yield* driver.createWorktree({
@@ -162,13 +168,13 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
newBranch: "feature/worktree",
});
expect(created.worktree.path).toBe(worktreePath);
expect(created.worktree.branch).toBe("feature/worktree");
expect(yield* git(worktreePath, ["branch", "--show-current"])).toBe("feature/worktree");
assert.equal(created.worktree.path, worktreePath);
assert.equal(created.worktree.branch, "feature/worktree");
assert.equal(yield* git(worktreePath, ["branch", "--show-current"]), "feature/worktree");
yield* driver.removeWorktree({ cwd, path: worktreePath });
const fileSystem = yield* FileSystem.FileSystem;
expect(yield* fileSystem.exists(worktreePath)).toBe(false);
assert.equal(yield* fileSystem.exists(worktreePath), false);
}),
);
});
@@ -180,20 +186,20 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
yield* initRepoWithCommit(cwd);
const driver = yield* GitVcsDriver.GitVcsDriver;
yield* writeTextFile(path.join(cwd, "a.txt"), "a\n");
yield* writeTextFile(path.join(cwd, "b.txt"), "b\n");
yield* writeTextFile(cwd, "a.txt", "a\n");
yield* writeTextFile(cwd, "b.txt", "b\n");
const context = yield* driver.prepareCommitContext(cwd, ["a.txt"]);
expect(context?.stagedSummary).toContain("a.txt");
expect(context?.stagedSummary).not.toContain("b.txt");
assert.include(context?.stagedSummary ?? "", "a.txt");
assert.notInclude(context?.stagedSummary ?? "", "b.txt");
const commit = yield* driver.commit(cwd, "Add a", "");
expect(commit.commitSha).toMatch(/^[a-f0-9]{40}$/);
expect(yield* git(cwd, ["log", "-1", "--pretty=%s"])).toBe("Add a");
assert.match(commit.commitSha, /^[a-f0-9]{40}$/);
assert.equal(yield* git(cwd, ["log", "-1", "--pretty=%s"]), "Add a");
const status = yield* git(cwd, ["status", "--porcelain"]);
expect(status).toContain("?? b.txt");
expect(status).not.toContain("a.txt");
assert.include(status, "?? b.txt");
assert.notInclude(status, "a.txt");
}),
);
});
@@ -214,22 +220,23 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
cwd,
branch: "feature/push",
});
yield* writeTextFile(path.join(cwd, "feature.txt"), "feature\n");
yield* writeTextFile(cwd, "feature.txt", "feature\n");
yield* (yield* GitVcsDriver.GitVcsDriver).prepareCommitContext(cwd);
yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add feature", "");
const pushed = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
expect(pushed).toMatchObject({
assert.deepInclude(pushed, {
status: "pushed",
branch: "feature/push",
setUpstream: true,
});
expect(yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"])).toBe(
assert.equal(
yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
"origin/feature/push",
);
const skipped = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
expect(skipped).toMatchObject({
assert.deepInclude(skipped, {
status: "skipped_up_to_date",
branch: "feature/push",
});
@@ -1,8 +1,6 @@
import { realpathSync } from "node:fs";
import { it } from "@effect/vitest";
import { assert, it } from "@effect/vitest";
import { Effect, FileSystem, Layer, Path, type PlatformError, type Scope } from "effect";
import { describe, expect } from "vitest";
import { describe } from "vitest";
import type { VcsDriverKind } from "@t3tools/contracts";
import { VcsDriver } from "../VcsDriver.ts";
@@ -45,8 +43,8 @@ export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInp
const cwd = yield* makeTmpDir();
const driver = yield* VcsDriver;
expect(yield* driver.detectRepository(cwd)).toBeNull();
expect(yield* driver.isInsideWorkTree(cwd)).toBe(false);
assert.equal(yield* driver.detectRepository(cwd), null);
assert.equal(yield* driver.isInsideWorkTree(cwd), false);
}),
);
@@ -57,24 +55,18 @@ export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInp
yield* input.fixture.createRepo(cwd);
yield* input.fixture.writeFile(cwd, "src/index.ts", "export const value = 1;\n");
const expectedRootPath = realpathSync.native(cwd);
const identity = yield* driver.detectRepository(cwd);
expect(identity).toMatchObject({
kind: input.kind,
rootPath: expectedRootPath,
});
expect(identity?.freshness).toMatchObject({
source: "live-local",
observedAt: expect.any(String),
});
expect(yield* driver.isInsideWorkTree(cwd)).toBe(true);
assert.equal(identity?.kind, input.kind);
assert.isTrue(identity?.rootPath.endsWith(cwd));
assert.equal(identity?.freshness.source, "live-local");
assert.equal(typeof identity?.freshness.observedAt, "string");
assert.equal(yield* driver.isInsideWorkTree(cwd), true);
const path = yield* Path.Path;
const nestedDir = path.join(cwd, "src");
const nestedIdentity = yield* driver.detectRepository(nestedDir);
expect(nestedIdentity?.rootPath).toBe(expectedRootPath);
expect(yield* driver.isInsideWorkTree(nestedDir)).toBe(true);
assert.equal(nestedIdentity?.rootPath, identity?.rootPath);
assert.equal(yield* driver.isInsideWorkTree(nestedDir), true);
}),
);
});
@@ -95,13 +87,11 @@ export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInp
const result = yield* driver.listWorkspaceFiles(cwd);
expect(result.paths).toContain("tracked.ts");
expect(result.paths).toContain("untracked.ts");
expect(result.truncated).toBe(false);
expect(result.freshness).toMatchObject({
source: "live-local",
observedAt: expect.any(String),
});
assert.include(result.paths, "tracked.ts");
assert.include(result.paths, "untracked.ts");
assert.equal(result.truncated, false);
assert.equal(result.freshness.source, "live-local");
assert.equal(typeof result.freshness.observedAt, "string");
}),
);
@@ -118,9 +108,9 @@ export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInp
const result = yield* driver.listWorkspaceFiles(cwd);
expect(result.paths).toContain("included.ts");
expect(result.paths).not.toContain("debug.log");
expect(result.paths).not.toContain("nested/error.log");
assert.include(result.paths, "included.ts");
assert.notInclude(result.paths, "debug.log");
assert.notInclude(result.paths, "nested/error.log");
}),
);
});
@@ -140,7 +130,7 @@ export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInp
"nested/error.log",
]);
expect(result).toEqual(["keep.ts"]);
assert.deepStrictEqual(result, ["keep.ts"]);
}),
);
@@ -151,7 +141,7 @@ export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInp
yield* input.fixture.createRepo(cwd);
expect(yield* driver.filterIgnoredPaths(cwd, [])).toEqual([]);
assert.deepStrictEqual(yield* driver.filterIgnoredPaths(cwd, []), []);
}),
);
});