v0.2.0: Git-native architecture (#1)
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
import { execSync } from "node:child_process";
|
||||
import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import * as fs from "node:fs";
|
||||
import { GitContext } from "../core/git-context.js";
|
||||
|
||||
function createTempRepo(): string {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-test-"));
|
||||
execSync("git init", { cwd: dir, stdio: "pipe" });
|
||||
execSync('git config user.email "test@test.com"', { cwd: dir, stdio: "pipe" });
|
||||
execSync('git config user.name "Test"', { cwd: dir, stdio: "pipe" });
|
||||
return dir;
|
||||
}
|
||||
|
||||
function commit(repoDir: string, message: string): void {
|
||||
const filePath = path.join(repoDir, `file-${Date.now()}.txt`);
|
||||
fs.writeFileSync(filePath, "test", "utf-8");
|
||||
execSync(`git add "${filePath}"`, { cwd: repoDir, stdio: "pipe" });
|
||||
execSync(`git commit -m "${message.replace(/"/g, '\\"')}"`, { cwd: repoDir, stdio: "pipe" });
|
||||
}
|
||||
|
||||
function cleanup(dir: string): void {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
describe("GitContext", () => {
|
||||
let repoDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
repoDir = createTempRepo();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup(repoDir);
|
||||
});
|
||||
|
||||
describe("isGitRepo", () => {
|
||||
it("returns true for a git repo", () => {
|
||||
const ctx = new GitContext(repoDir);
|
||||
expect(ctx.isGitRepo()).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for non-git directory", () => {
|
||||
const nonGit = fs.mkdtempSync(path.join(os.tmpdir(), "ci-nongit-"));
|
||||
const ctx = new GitContext(nonGit);
|
||||
expect(ctx.isGitRepo()).toBe(false);
|
||||
cleanup(nonGit);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getCurrentBranch", () => {
|
||||
it("returns current branch name", () => {
|
||||
const ctx = new GitContext(repoDir);
|
||||
commit(repoDir, "initial");
|
||||
expect(ctx.getCurrentBranch()).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getRecentCommits", () => {
|
||||
it("returns parsed commits with ci blocks", () => {
|
||||
commit(repoDir, `docs(init): initialize project
|
||||
|
||||
---ci---
|
||||
phase: 0
|
||||
milestone: v1.0
|
||||
status: specify
|
||||
---/ci---
|
||||
|
||||
Initial commit`);
|
||||
|
||||
const ctx = new GitContext(repoDir);
|
||||
const commits = ctx.getRecentCommits(5);
|
||||
|
||||
expect(commits.length).toBeGreaterThanOrEqual(1);
|
||||
const ciCommit = commits.find((c) => c.ci !== null);
|
||||
expect(ciCommit).toBeDefined();
|
||||
expect(ciCommit!.ci!.phase).toBe(0);
|
||||
expect(ciCommit!.ci!.milestone).toBe("v1.0");
|
||||
});
|
||||
|
||||
it("returns commits without ci blocks as null ci", () => {
|
||||
commit(repoDir, "feat: some feature");
|
||||
|
||||
const ctx = new GitContext(repoDir);
|
||||
const commits = ctx.getRecentCommits(5);
|
||||
|
||||
expect(commits.length).toBeGreaterThanOrEqual(1);
|
||||
expect(commits[0].ci).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("reconstructState", () => {
|
||||
it("reconstructs state from latest ci commit", () => {
|
||||
commit(repoDir, `docs(init): initialize project
|
||||
|
||||
---ci---
|
||||
phase: 1
|
||||
milestone: v1.0
|
||||
status: execute
|
||||
---/ci---`);
|
||||
|
||||
const ctx = new GitContext(repoDir);
|
||||
const state = ctx.reconstructState();
|
||||
|
||||
expect(state.currentPhase).toBe(1);
|
||||
expect(state.currentMilestone).toBe("v1.0");
|
||||
expect(state.currentStage).toBe("execute");
|
||||
});
|
||||
|
||||
it("returns default state without ci commits", () => {
|
||||
commit(repoDir, "feat: some regular feature");
|
||||
|
||||
const ctx = new GitContext(repoDir);
|
||||
const state = ctx.reconstructState();
|
||||
|
||||
expect(state.currentPhase).toBe(0);
|
||||
expect(state.currentStage).toBe("specify");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getDecisionsFromCommits", () => {
|
||||
it("extracts decisions from parsed commits", () => {
|
||||
commit(repoDir, `feat(P01): add auth
|
||||
|
||||
---ci---
|
||||
phase: 1
|
||||
milestone: v1.0
|
||||
status: execute
|
||||
decisions:
|
||||
- id: D-001
|
||||
decision: Use bcrypt
|
||||
rationale: Industry standard
|
||||
confidence: 0.90
|
||||
alternatives: [argon2]
|
||||
---/ci---`);
|
||||
|
||||
const ctx = new GitContext(repoDir);
|
||||
const commits = ctx.getRecentCommits(5);
|
||||
const decisions = ctx.getDecisionsFromCommits(commits);
|
||||
|
||||
expect(decisions).toHaveLength(1);
|
||||
expect(decisions[0].id).toBe("D-001");
|
||||
expect(decisions[0].decision).toBe("Use bcrypt");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getLessons", () => {
|
||||
it("extracts lessons from commits", () => {
|
||||
commit(repoDir, `docs(P01): complete phase
|
||||
|
||||
---ci---
|
||||
phase: 1
|
||||
milestone: v1.0
|
||||
status: complete
|
||||
lessons:
|
||||
- Never use sync bcrypt
|
||||
- Always check JWT expiry first
|
||||
---/ci---`);
|
||||
|
||||
const ctx = new GitContext(repoDir);
|
||||
const commits = ctx.getRecentCommits(10);
|
||||
const lessons: string[] = [];
|
||||
for (const commit of commits) {
|
||||
if (commit.ci?.lessons) lessons.push(...commit.ci.lessons);
|
||||
}
|
||||
|
||||
expect(lessons).toContain("Never use sync bcrypt");
|
||||
expect(lessons).toContain("Always check JWT expiry first");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getBranches", () => {
|
||||
it("lists branches with type info", () => {
|
||||
commit(repoDir, "initial");
|
||||
execSync("git checkout -b phase/01-auth", { cwd: repoDir, stdio: "pipe" });
|
||||
commit(repoDir, "feat: auth work");
|
||||
execSync("git checkout -b milestone/v1.0-mvp", { cwd: repoDir, stdio: "pipe" });
|
||||
commit(repoDir, "feat: milestone work");
|
||||
|
||||
const ctx = new GitContext(repoDir);
|
||||
const branches = ctx.getBranches();
|
||||
|
||||
const phaseBranches = branches.filter((b) => b.type === "phase");
|
||||
const milestoneBranches = branches.filter((b) => b.type === "milestone");
|
||||
|
||||
expect(phaseBranches.length).toBeGreaterThanOrEqual(1);
|
||||
expect(milestoneBranches.length).toBeGreaterThanOrEqual(1);
|
||||
expect(phaseBranches[0].phaseNumber).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user