144 lines
5.0 KiB
TypeScript
144 lines
5.0 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { db } from "./db.ts";
|
|
import { init } from "./identity.ts";
|
|
import { readAll } from "./eventlog.ts";
|
|
import {
|
|
aggregateMoraleCounts,
|
|
extractMoralePulses,
|
|
isSkyMoralePulsePayload,
|
|
latestMoralePulse,
|
|
postMoralePulse,
|
|
SkyMoraleValidationError,
|
|
} from "./skyMorale.ts";
|
|
|
|
beforeEach(async () => {
|
|
indexedDB.deleteDatabase(db.name);
|
|
db.close();
|
|
await db.open();
|
|
await init();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe("isSkyMoralePulsePayload", () => {
|
|
it("accepts a well-formed morale pulse payload, with or without a note", () => {
|
|
expect(
|
|
isSkyMoralePulsePayload({ type: "sky.morale.pulse", mood: "good", postedAt: 1 }),
|
|
).toBe(true);
|
|
expect(
|
|
isSkyMoralePulsePayload({
|
|
type: "sky.morale.pulse",
|
|
mood: "great",
|
|
note: "feeling good",
|
|
postedAt: 1,
|
|
}),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("rejects a malformed mood value, a different event type, or a non-string note", () => {
|
|
expect(isSkyMoralePulsePayload({ type: "sky.morale.pulse", mood: "ecstatic", postedAt: 1 })).toBe(
|
|
false,
|
|
);
|
|
expect(isSkyMoralePulsePayload({ type: "sky.memoryWall.entry", mood: "good", postedAt: 1 })).toBe(
|
|
false,
|
|
);
|
|
expect(
|
|
isSkyMoralePulsePayload({ type: "sky.morale.pulse", mood: "good", note: 7, postedAt: 1 }),
|
|
).toBe(false);
|
|
expect(isSkyMoralePulsePayload(null)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("postMoralePulse", () => {
|
|
it("rejects a malformed mood value before append() is ever called", async () => {
|
|
const appendSpy = vi.fn();
|
|
|
|
await expect(
|
|
postMoralePulse({ mood: "ecstatic" as never }, { append: appendSpy }),
|
|
).rejects.toBeInstanceOf(SkyMoraleValidationError);
|
|
|
|
expect(appendSpy).not.toHaveBeenCalled();
|
|
expect(await readAll()).toHaveLength(0);
|
|
});
|
|
|
|
it("signs and appends a valid pulse to the shared Event Log, readable back verbatim", async () => {
|
|
await postMoralePulse({ mood: "great", note: "sunrise lifted the whole floor" }, { now: () => 1000 });
|
|
|
|
const entries = await readAll();
|
|
const pulses = extractMoralePulses(entries);
|
|
|
|
expect(pulses).toHaveLength(1);
|
|
expect(pulses[0].mood).toBe("great");
|
|
expect(pulses[0].note).toBe("sunrise lifted the whole floor");
|
|
expect(pulses[0].postedAt).toBe(1000);
|
|
expect(pulses[0].authorAccountId).toMatch(/^[0-9a-f]+$/);
|
|
});
|
|
|
|
it("trims a note and omits it entirely when it is blank", async () => {
|
|
await postMoralePulse({ mood: "okay", note: " " }, { now: () => 1000 });
|
|
const pulses = extractMoralePulses(await readAll());
|
|
|
|
expect(pulses[0].note).toBeUndefined();
|
|
});
|
|
|
|
it("ignores entries belonging to other event types during extraction", async () => {
|
|
await postMoralePulse({ mood: "low" }, { now: () => 2000 });
|
|
const entries = await readAll();
|
|
const mixed = [
|
|
...entries,
|
|
{ ...entries[0], id: 999, payload: { type: "wallet.issue", account: "a", amount: 1 } },
|
|
];
|
|
|
|
expect(extractMoralePulses(mixed)).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe("latestMoralePulse + aggregateMoraleCounts", () => {
|
|
it("is correct on a cold replay: latest by Lamport order and an accurate per-mood tally", async () => {
|
|
await postMoralePulse({ mood: "good" }, { now: () => 1 });
|
|
await postMoralePulse({ mood: "rough" }, { now: () => 2 });
|
|
await postMoralePulse({ mood: "good" }, { now: () => 3 });
|
|
|
|
const pulses = extractMoralePulses(await readAll());
|
|
|
|
expect(latestMoralePulse(pulses)?.mood).toBe("good");
|
|
expect(latestMoralePulse(pulses)?.postedAt).toBe(3);
|
|
|
|
const counts = aggregateMoraleCounts(pulses);
|
|
expect(counts).toEqual({ great: 0, good: 2, okay: 0, low: 0, rough: 1 });
|
|
});
|
|
|
|
it("returns undefined for the latest pulse when none have been posted", () => {
|
|
expect(latestMoralePulse([])).toBeUndefined();
|
|
expect(aggregateMoraleCounts([])).toEqual({ great: 0, good: 0, okay: 0, low: 0, rough: 0 });
|
|
});
|
|
});
|
|
|
|
describe("postMoralePulse backend sync wiring (plan todo 3)", () => {
|
|
it("observably calls trySyncWithBackend() after the write, without delaying or altering the local-first append", async () => {
|
|
const fetchMock = vi.fn().mockRejectedValue(new Error("ECONNREFUSED"));
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
|
|
const stored = await postMoralePulse({ mood: "good" }, { now: () => 5000 });
|
|
|
|
expect(stored.payload).toMatchObject({ type: "sky.morale.pulse", mood: "good" });
|
|
expect(await readAll()).toHaveLength(1);
|
|
|
|
// Fire-and-forget: give the un-awaited sync call's microtask a turn.
|
|
await Promise.resolve();
|
|
await Promise.resolve();
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
expect(fetchMock.mock.calls[0][0]).toBe("/api/sync");
|
|
});
|
|
|
|
it("never throws into the caller when the relay is unreachable - the write still resolves normally", async () => {
|
|
vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new Error("network down")));
|
|
|
|
await expect(postMoralePulse({ mood: "low" })).resolves.toMatchObject({
|
|
payload: { type: "sky.morale.pulse", mood: "low" },
|
|
});
|
|
});
|
|
});
|