Files
oh-my-pi/packages/react-edit-benchmark/test/verify.test.ts
T
can1357 e22d123009 refactor(fs): migrated remaining sync file operations to async
- Converted readdirSync, readFileSync, and statSync to async readdir, readFile, stat across skills and agent discovery.
- Made scanDirectoryForSkills async and refactored custom directory scanning to use Promise.all for concurrent processing.
- Updated agent discovery to use fs/promises for async file reading and refactored helper patterns.
- Added AgentParsingError exception class for better error handling during agent parsing.
- Added filesystem error type guards (isEnoent, isEacces, isPerm, etc.) to pi-utils for safe error checking.
- Added color manipulation utilities to pi-utils for accessibility features.
- Added color-blind mode setting to settings manager.
- Migrated plugins, settings, and config modules from sync to async file operations.
- Updated error handling to use new pi-utils type guards for type-safe checking.
2026-01-24 03:18:03 +01:00

110 lines
3.6 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { TempDir } from "@oh-my-pi/pi-utils";
import { verifyExpectedFiles } from "../verify";
async function createTempDirs(): Promise<{
root: string;
expectedDir: string;
actualDir: string;
cleanup: () => Promise<void>;
}> {
const tempDir = await TempDir.create("@reach-benchmark-verify-");
const expectedDir = tempDir.join("expected");
const actualDir = tempDir.join("actual");
await fs.mkdir(expectedDir, { recursive: true });
await fs.mkdir(actualDir, { recursive: true });
return {
root: tempDir.absolute(),
expectedDir,
actualDir,
cleanup: async () => {
await tempDir.remove();
},
};
}
describe("verifyExpectedFiles", () => {
it("reports missing files", async () => {
const { expectedDir, actualDir, cleanup } = await createTempDirs();
try {
await Bun.write(path.join(expectedDir, "index.ts"), "export const value = 1;\n");
const result = await verifyExpectedFiles(expectedDir, actualDir);
expect(result.success).toBe(false);
expect(result.error).toContain("Missing files: index.ts");
} finally {
await cleanup();
}
});
it("reports unexpected files", async () => {
const { expectedDir, actualDir, cleanup } = await createTempDirs();
try {
await Bun.write(path.join(expectedDir, "index.ts"), "export const value = 1;\n");
await Bun.write(path.join(actualDir, "index.ts"), "export const value = 1;\n");
await Bun.write(path.join(actualDir, "extra.ts"), "export const extra = true;\n");
const result = await verifyExpectedFiles(expectedDir, actualDir);
expect(result.success).toBe(false);
expect(result.error).toContain("Unexpected files: extra.ts");
} finally {
await cleanup();
}
});
it("fails with diff output when formatted content differs", async () => {
const { expectedDir, actualDir, cleanup } = await createTempDirs();
try {
await Bun.write(path.join(expectedDir, "index.ts"), "const value = 1;\n");
await Bun.write(path.join(actualDir, "index.ts"), "const value = 2;\n");
const result = await verifyExpectedFiles(expectedDir, actualDir);
expect(result.success).toBe(false);
expect(result.diff).toContain("-const value = 1;");
expect(result.diff).toContain("+const value = 2;");
expect(result.diffStats?.linesChanged).toBeGreaterThan(0);
expect(result.diffStats?.charsChanged).toBeGreaterThan(0);
} finally {
await cleanup();
}
});
it("succeeds when formatted content matches despite whitespace differences", async () => {
const { expectedDir, actualDir, cleanup } = await createTempDirs();
try {
const expected = "function test() {\n return 1;\n}\n";
const actual = "function test(){\nreturn 1;\n}\n";
await Bun.write(path.join(expectedDir, "index.ts"), expected);
await Bun.write(path.join(actualDir, "index.ts"), actual);
const result = await verifyExpectedFiles(expectedDir, actualDir);
const actualAfter = await Bun.file(path.join(actualDir, "index.ts")).text();
expect(result.success).toBe(true);
expect(result.formattedEquivalent).toBe(true);
expect(actualAfter).toBe(actual);
} finally {
await cleanup();
}
});
it("normalizes line endings before comparison", async () => {
const { expectedDir, actualDir, cleanup } = await createTempDirs();
try {
await Bun.write(path.join(expectedDir, "index.ts"), "export const value = 1;\r\n");
await Bun.write(path.join(actualDir, "index.ts"), "export const value = 1;\n");
const result = await verifyExpectedFiles(expectedDir, actualDir);
expect(result.success).toBe(true);
} finally {
await cleanup();
}
});
});