Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

test/property.test.ts

Raw
import { existsSync, readFileSync, rmSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import { assertProperty, fc } from "./property.ts";

const regressionDir = join(
	dirname(fileURLToPath(import.meta.url)),
	"regressions",
);

describe("seeded property runner", () => {
	it("passes a true property without recording anything", () => {
		assertProperty(
			import.meta.url,
			"addition commutes",
			fc.property(fc.integer(), fc.integer(), (a, b) => a + b === b + a),
		);
	});

	it("prints a replay seed and stores the minimal counterexample on failure", () => {
		const file = join(regressionDir, "demo-failure.json");
		rmSync(file, { force: true });
		expect(() =>
			assertProperty(
				import.meta.url,
				"demo failure",
				fc.property(fc.integer({ min: 0 }), (n) => n < 1000),
				{ numRuns: 5000 },
			),
		).toThrow(/seed=\d+.*counterexample=\[1000\]/);
		expect(existsSync(file)).toBe(true);
		const record = JSON.parse(readFileSync(file, "utf8"));
		expect(typeof record.seed).toBe("number");
		// fast-check shrinks to the smallest violating value.
		expect(record.counterexample).toEqual([1000]);
		rmSync(file, { force: true });
	});
});