← back to Homesonspec
apps/workers/src/verify.test.ts
69 lines
import { describe, expect, it } from "vitest";
import { computeFreshness, labelRank, pickDedupeWinner, HOUR_MS } from "./verify";
const now = new Date("2026-07-22T12:00:00Z");
const hoursAgo = (h: number) => new Date(now.getTime() - h * HOUR_MS);
// Standard cadence: fresh <=24h, aging <=72h, review <=168h, stale beyond.
const F = 24, A = 72, S = 168;
describe("computeFreshness", () => {
it("null verification time always needs review", () => {
expect(computeFreshness(null, F, A, S, now)).toBe("REVIEW_REQUIRED");
});
it("recent verification is FRESH", () => {
expect(computeFreshness(hoursAgo(1), F, A, S, now)).toBe("FRESH");
expect(computeFreshness(hoursAgo(24), F, A, S, now)).toBe("FRESH");
});
it("between fresh and aging intervals is AGING", () => {
expect(computeFreshness(hoursAgo(48), F, A, S, now)).toBe("AGING");
});
it("between aging and stale intervals needs review", () => {
expect(computeFreshness(hoursAgo(100), F, A, S, now)).toBe("REVIEW_REQUIRED");
});
it("past the stale interval is STALE", () => {
expect(computeFreshness(hoursAgo(200), F, A, S, now)).toBe("STALE");
});
});
describe("labelRank", () => {
it("orders authority high→low, demo lowest", () => {
expect(labelRank("VERIFIED_FROM_BUILDER")).toBeGreaterThan(labelRank("BUILDER_WEBSITE"));
expect(labelRank("BUILDER_WEBSITE")).toBeGreaterThan(labelRank("BUILDER_SUBMITTED"));
expect(labelRank("DEMONSTRATION")).toBeLessThan(labelRank("NO_LONGER_AVAILABLE"));
});
});
describe("pickDedupeWinner", () => {
const mk = (id: string, label: Parameters<typeof labelRank>[0], vAgoH: number | null, cAgoH: number) => ({
id,
verificationLabel: label,
lastVerifiedAt: vAgoH === null ? null : hoursAgo(vAgoH),
createdAt: hoursAgo(cAgoH),
});
it("prefers the higher-authority label regardless of recency", () => {
const winner = pickDedupeWinner([
mk("a", "BUILDER_WEBSITE", 1, 1),
mk("b", "VERIFIED_FROM_BUILDER", 100, 100),
]);
expect(winner.id).toBe("b");
});
it("breaks a label tie by most-recent verification", () => {
const winner = pickDedupeWinner([
mk("a", "BUILDER_WEBSITE", 50, 10),
mk("b", "BUILDER_WEBSITE", 5, 90),
]);
expect(winner.id).toBe("b");
});
it("falls back to newest record when verification times match", () => {
const winner = pickDedupeWinner([
mk("a", "BUILDER_SUBMITTED", null, 200),
mk("b", "BUILDER_SUBMITTED", null, 2),
]);
expect(winner.id).toBe("b");
});
});