feat: 创建可复用断言工具集

This commit is contained in:
2026-06-09 15:12:12 +08:00
parent 4e736998c7
commit 2d5b40379f

64
tests/agent/validators.ts Normal file
View File

@@ -0,0 +1,64 @@
import { expect } from "bun:test";
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import type { ChangeStatus } from "../../src/types.ts";
import { validateConfig } from "../../src/core/config.ts";
import type { RuneConfig } from "../../src/types.ts";
export function assertFileExists(projectDir: string, relativePath: string): void {
expect(existsSync(join(projectDir, relativePath))).toBe(true);
}
export function assertNoFile(projectDir: string, relativePath: string): void {
expect(existsSync(join(projectDir, relativePath))).toBe(false);
}
export function assertDirExists(projectDir: string, relativePath: string): void {
expect(existsSync(join(projectDir, relativePath))).toBe(true);
}
export function assertFileContains(
projectDir: string,
relativePath: string,
expected: string,
): void {
const content = readFileSync(join(projectDir, relativePath), "utf-8");
expect(content).toContain(expected);
}
export function assertDocCreated(projectDir: string, changeName: string, docName: string): void {
assertFileExists(projectDir, `.rune/changes/${changeName}/${docName}.md`);
const content = readFileSync(
join(projectDir, `.rune/changes/${changeName}/${docName}.md`),
"utf-8",
);
expect(content.length).toBeGreaterThan(0);
}
export function assertDocContains(
projectDir: string,
changeName: string,
docName: string,
expected: string,
): void {
assertFileContains(projectDir, `.rune/changes/${changeName}/${docName}.md`, expected);
}
export function assertAllTasksDone(change: ChangeStatus): void {
expect(change.taskProgress).not.toBeNull();
if (change.taskProgress) {
expect(change.taskProgress.completed).toBe(change.taskProgress.total);
}
}
export function assertTaskProgress(change: ChangeStatus, completed: number, total: number): void {
expect(change.taskProgress).toEqual({ completed, total });
}
export function assertConfigValid(config: RuneConfig): void {
expect(() => validateConfig(config)).not.toThrow();
}
export function assertConfigInvalid(config: RuneConfig): void {
expect(() => validateConfig(config)).toThrow();
}