70 lines
2.5 KiB
TypeScript
70 lines
2.5 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, beforeAll, describe, it, vi } from "vitest";
|
|
|
|
vi.mock("@toolctrl/ui-mock", async () => import("../packages/ui-mock/src/index.ts"));
|
|
|
|
let uiMockVisual: typeof import("./verify-ui-mock-visual.ts");
|
|
|
|
const fixtures: string[] = [];
|
|
|
|
function createFixture() {
|
|
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "ui-mock-visual-"));
|
|
fixtures.push(rootDir);
|
|
return rootDir;
|
|
}
|
|
|
|
afterEach(() => {
|
|
while (fixtures.length > 0) {
|
|
fs.rmSync(fixtures.pop()!, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
beforeAll(async () => {
|
|
uiMockVisual = await import("./verify-ui-mock-visual.ts");
|
|
});
|
|
|
|
describe("verify-ui-mock-visual", () => {
|
|
it("generates a real hot-swap mock module, page, and snapshot", () => {
|
|
const drill = uiMockVisual.buildUiMockVisualDrill();
|
|
|
|
assert.match(drill.module, /export const mockPage/);
|
|
assert.match(drill.module, /"route": "\/mock\/phase-c\/hot-swap"/);
|
|
assert.match(drill.html, /data-ui-mock-page="phase-c-hot-swap"/);
|
|
assert.match(drill.snapshot, /"pageId": "phase-c-hot-swap"/);
|
|
});
|
|
|
|
it("passes when committed static baselines match the generated drill", () => {
|
|
const rootDir = createFixture();
|
|
uiMockVisual.writeUiMockVisualBaseline(rootDir);
|
|
|
|
const result = uiMockVisual.verifyUiMockVisualBaseline(rootDir);
|
|
const output = uiMockVisual.formatUiMockVisualResult(result);
|
|
|
|
assert.deepEqual(result.failures, []);
|
|
assert.equal(result.passes.length, 7);
|
|
assert.match(output, /^PASS UI mock static artifact baseline verification/);
|
|
assert.match(output, /renders through JSDOM in CI/);
|
|
assert.match(output, /rendered hot-swap navigation has exactly one active route/);
|
|
});
|
|
|
|
it("fails when the HTML visual baseline drifts", () => {
|
|
const rootDir = createFixture();
|
|
uiMockVisual.writeUiMockVisualBaseline(rootDir);
|
|
fs.appendFileSync(path.join(rootDir, "packages/ui-mock/visual-baseline/hot-swap.html"), "\n<!-- drift -->\n", "utf8");
|
|
|
|
const result = uiMockVisual.verifyUiMockVisualBaseline(rootDir);
|
|
|
|
assert.match(result.failures.join("\n"), /hot-swap\.html: static artifact baseline drifted/);
|
|
});
|
|
|
|
it("blocks visual baseline update mode when CI is set", () => {
|
|
assert.throws(
|
|
() => uiMockVisual.assertVisualBaselineUpdateAllowed({ CI: "true" }),
|
|
/Refusing to update UI mock static artifact baselines while CI is set\./,
|
|
);
|
|
});
|
|
});
|