- Replace branch-centric git/VCS types and methods with ref-based names - Update server, web, and tests to use the new pluggable integration shape
247 lines
8.7 KiB
TypeScript
247 lines
8.7 KiB
TypeScript
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
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";
|
|
import * as GitVcsDriver from "./GitVcsDriver.ts";
|
|
|
|
const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
|
|
prefix: "t3-git-vcs-driver-test-",
|
|
});
|
|
const TestLayer = GitVcsDriver.layer.pipe(
|
|
Layer.provide(ServerConfigLayer),
|
|
Layer.provideMerge(NodeServices.layer),
|
|
);
|
|
|
|
const makeTmpDir = (
|
|
prefix = "git-vcs-driver-test-",
|
|
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
|
|
Effect.gen(function* () {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
return yield* fileSystem.makeTempDirectoryScoped({ prefix });
|
|
});
|
|
|
|
const writeTextFile = (
|
|
cwd: string,
|
|
relativePath: string,
|
|
contents: string,
|
|
): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem | Path.Path> =>
|
|
Effect.gen(function* () {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const pathService = yield* Path.Path;
|
|
const filePath = pathService.join(cwd, relativePath);
|
|
yield* fileSystem.makeDirectory(pathService.dirname(filePath), { recursive: true });
|
|
yield* fileSystem.writeFileString(filePath, contents);
|
|
});
|
|
|
|
const git = (
|
|
cwd: string,
|
|
args: ReadonlyArray<string>,
|
|
env?: NodeJS.ProcessEnv,
|
|
): Effect.Effect<string, GitCommandError, GitVcsDriver.GitVcsDriver> =>
|
|
Effect.gen(function* () {
|
|
const driver = yield* GitVcsDriver.GitVcsDriver;
|
|
const result = yield* driver.execute({
|
|
operation: "GitVcsDriver.test.git",
|
|
cwd,
|
|
args,
|
|
...(env ? { env } : {}),
|
|
timeoutMs: 10_000,
|
|
});
|
|
return result.stdout.trim();
|
|
});
|
|
|
|
const initRepoWithCommit = (
|
|
cwd: string,
|
|
): Effect.Effect<
|
|
{ readonly initialBranch: string },
|
|
GitCommandError | PlatformError.PlatformError,
|
|
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(cwd, "README.md", "# test\n");
|
|
yield* git(cwd, ["add", "."]);
|
|
yield* git(cwd, ["commit", "-m", "initial commit"]);
|
|
const initialBranch = yield* git(cwd, ["branch", "--show-current"]);
|
|
return { initialBranch };
|
|
});
|
|
|
|
it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
|
|
describe("repository status", () => {
|
|
it.effect("reports non-repository directories without failing", () =>
|
|
Effect.gen(function* () {
|
|
const cwd = yield* makeTmpDir();
|
|
const driver = yield* GitVcsDriver.GitVcsDriver;
|
|
|
|
const refs = yield* driver.listRefs({ cwd });
|
|
assert.equal(refs.isRepo, false);
|
|
assert.deepStrictEqual(refs.refs, []);
|
|
}),
|
|
);
|
|
|
|
it.effect("reports refName and dirty state for a repository", () =>
|
|
Effect.gen(function* () {
|
|
const cwd = yield* makeTmpDir();
|
|
const { initialBranch } = yield* initRepoWithCommit(cwd);
|
|
yield* writeTextFile(cwd, "feature.ts", "export const value = 1;\n");
|
|
|
|
const status = yield* (yield* GitVcsDriver.GitVcsDriver).statusDetails(cwd);
|
|
|
|
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",
|
|
);
|
|
}),
|
|
);
|
|
});
|
|
|
|
describe("refName operations", () => {
|
|
it.effect("creates, checks out, renames, and lists refs", () =>
|
|
Effect.gen(function* () {
|
|
const cwd = yield* makeTmpDir();
|
|
yield* initRepoWithCommit(cwd);
|
|
const driver = yield* GitVcsDriver.GitVcsDriver;
|
|
|
|
yield* driver.createRef({ cwd, refName: "feature/original" });
|
|
const switchRef = yield* driver.switchRef({ cwd, refName: "feature/original" });
|
|
assert.equal(switchRef.refName, "feature/original");
|
|
|
|
const renamed = yield* driver.renameBranch({
|
|
cwd,
|
|
oldBranch: "feature/original",
|
|
newBranch: "feature/renamed",
|
|
});
|
|
assert.equal(renamed.branch, "feature/renamed");
|
|
assert.equal(yield* git(cwd, ["branch", "--show-current"]), "feature/renamed");
|
|
|
|
const refs = yield* driver.listRefs({ cwd });
|
|
assert.equal(
|
|
refs.refs.find((refName) => refName.name === "feature/renamed")?.current,
|
|
true,
|
|
);
|
|
}),
|
|
);
|
|
|
|
it.effect("returns the existing refName when rename source and target match", () =>
|
|
Effect.gen(function* () {
|
|
const cwd = yield* makeTmpDir();
|
|
yield* initRepoWithCommit(cwd);
|
|
const driver = yield* GitVcsDriver.GitVcsDriver;
|
|
|
|
const current = yield* git(cwd, ["branch", "--show-current"]);
|
|
const result = yield* driver.renameBranch({
|
|
cwd,
|
|
oldBranch: current,
|
|
newBranch: current,
|
|
});
|
|
|
|
assert.equal(result.branch, current);
|
|
}),
|
|
);
|
|
});
|
|
|
|
describe("worktree operations", () => {
|
|
it.effect("creates and removes a worktree for a new refName", () =>
|
|
Effect.gen(function* () {
|
|
const cwd = yield* makeTmpDir();
|
|
const { initialBranch } = yield* initRepoWithCommit(cwd);
|
|
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({
|
|
cwd,
|
|
path: worktreePath,
|
|
refName: initialBranch,
|
|
newRefName: "feature/worktree",
|
|
});
|
|
|
|
assert.equal(created.worktree.path, worktreePath);
|
|
assert.equal(created.worktree.refName, "feature/worktree");
|
|
assert.equal(yield* git(worktreePath, ["branch", "--show-current"]), "feature/worktree");
|
|
|
|
yield* driver.removeWorktree({ cwd, path: worktreePath });
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
assert.equal(yield* fileSystem.exists(worktreePath), false);
|
|
}),
|
|
);
|
|
});
|
|
|
|
describe("commit context", () => {
|
|
it.effect("stages selected files and commits only those files", () =>
|
|
Effect.gen(function* () {
|
|
const cwd = yield* makeTmpDir();
|
|
yield* initRepoWithCommit(cwd);
|
|
const driver = yield* GitVcsDriver.GitVcsDriver;
|
|
|
|
yield* writeTextFile(cwd, "a.txt", "a\n");
|
|
yield* writeTextFile(cwd, "b.txt", "b\n");
|
|
|
|
const context = yield* driver.prepareCommitContext(cwd, ["a.txt"]);
|
|
assert.include(context?.stagedSummary ?? "", "a.txt");
|
|
assert.notInclude(context?.stagedSummary ?? "", "b.txt");
|
|
|
|
const commit = yield* driver.commit(cwd, "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"]);
|
|
assert.include(status, "?? b.txt");
|
|
assert.notInclude(status, "a.txt");
|
|
}),
|
|
);
|
|
});
|
|
|
|
describe("remote operations", () => {
|
|
it.effect("pushes with upstream setup and skips when already up to date", () =>
|
|
Effect.gen(function* () {
|
|
const cwd = yield* makeTmpDir();
|
|
const remote = yield* makeTmpDir("git-remote-");
|
|
yield* initRepoWithCommit(cwd);
|
|
yield* git(remote, ["init", "--bare"]);
|
|
yield* git(cwd, ["remote", "add", "origin", remote]);
|
|
yield* (yield* GitVcsDriver.GitVcsDriver).createRef({
|
|
cwd,
|
|
refName: "feature/push",
|
|
});
|
|
yield* (yield* GitVcsDriver.GitVcsDriver).switchRef({
|
|
cwd,
|
|
refName: "feature/push",
|
|
});
|
|
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);
|
|
assert.deepInclude(pushed, {
|
|
status: "pushed",
|
|
branch: "feature/push",
|
|
setUpstream: true,
|
|
});
|
|
assert.equal(
|
|
yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]),
|
|
"origin/feature/push",
|
|
);
|
|
|
|
const skipped = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
|
|
assert.deepInclude(skipped, {
|
|
status: "skipped_up_to_date",
|
|
branch: "feature/push",
|
|
});
|
|
}),
|
|
);
|
|
});
|
|
});
|