Files
patriothacks/api/_lib/relayAggregate.test.ts

80 lines
3.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { buildRelayAggregate } from "./relayAggregate.ts";
import type { WireEventEntry } from "./eventRelayStore.ts";
function entry(payload: unknown, lamportTs: number): WireEventEntry {
return { payload, authorPubkey: "a".repeat(64), signature: "b".repeat(128), lamportTs };
}
describe("buildRelayAggregate", () => {
it("returns an honest zero-state for an empty relay, not a placeholder", () => {
expect(buildRelayAggregate([])).toEqual({
totalEvents: 0,
population: null,
resources: {},
miniapps: [],
errorEvents: [],
});
});
it("picks the latest seed.population/seed.resource.* value by relay arrival order", () => {
const summary = buildRelayAggregate([
entry({ type: "seed.population", value: 100 }, 0),
entry({ type: "seed.resource.power", resource: "power", value: 10 }, 1),
entry({ type: "seed.population", value: 150 }, 2),
entry({ type: "seed.resource.power", resource: "power", value: 20 }, 3),
]);
expect(summary.totalEvents).toBe(4);
expect(summary.population).toBe(150);
expect(summary.resources).toEqual({ power: 20 });
});
it("tracks each mapped mini-app's highest-lamportTs event as its last activity", () => {
const summary = buildRelayAggregate([
entry({ type: "mentor.match" }, 10),
entry({ type: "mentor.skillReview" }, 3),
entry({ type: "sky.morale.pulse" }, 7),
]);
const mentor = summary.miniapps.find((m) => m.id === "mentor");
expect(mentor).toEqual({ id: "mentor", label: "Mentor", lastEventType: "mentor.match", lastLamportTs: 10 });
const sky = summary.miniapps.find((m) => m.id === "sky");
expect(sky).toEqual({ id: "sky", label: "Sky", lastEventType: "sky.morale.pulse", lastLamportTs: 7 });
});
it("excludes seed/demo/test noise from per-mini-app activity", () => {
const summary = buildRelayAggregate([
entry({ type: "seed.population", value: 1 }, 0),
entry({ type: "seed.demo.loaded", fixtureId: "x" }, 1),
entry({ type: "test.ping" }, 2),
]);
expect(summary.miniapps).toEqual([]);
});
it("flags a .error-suffixed type, an explicit error:true, and a severity/level of error", () => {
const summary = buildRelayAggregate([
entry({ type: "tunnelsafety.hazard.error", message: "boom" }, 0),
entry({ type: "waterwatch.reading.report", error: true, reason: "bad sensor" }, 1),
entry({ type: "healthpost.symptomReport", severity: "error" }, 2),
entry({ type: "sky.morale.pulse" }, 3),
]);
expect(summary.errorEvents).toEqual([
{ type: "tunnelsafety.hazard.error", lamportTs: 0, detail: "boom" },
{ type: "waterwatch.reading.report", lamportTs: 1, detail: "bad sensor" },
{ type: "healthpost.symptomReport", lamportTs: 2, detail: undefined },
]);
});
it("ignores malformed payloads (missing or non-string type) instead of throwing", () => {
const summary = buildRelayAggregate([
entry({ notAType: true }, 0),
entry(null, 1),
entry("just a string", 2),
entry({ type: 42 }, 3),
]);
expect(summary.totalEvents).toBe(4);
expect(summary.population).toBeNull();
expect(summary.miniapps).toEqual([]);
expect(summary.errorEvents).toEqual([]);
});
});