/** * @file sessionChange.test.ts * @description Unit tests for `sessionChangeMatcher`: fires on a new session * and on a change of the keyed columns, never on a re-broadcast that moved none. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, expect, it } from "vitest"; import { sessionChangeMatcher } from "../sessionChange"; import type { WSMessage } from "../types"; const msg = (type: string, data: unknown) => ({ type, data, timestamp: "" }) as WSMessage; describe("sessionChangeMatcher", () => { it("fires on a new session and on a state change, not on a same-state re-broadcast", () => { const m = sessionChangeMatcher(); expect(m(msg("session_created", { id: "a", status: "active" }))).toBe(true); expect(m(msg("session_updated", { id: "a", status: "active", name: "x" }))).toBe(false); expect( m(msg("session_updated", { id: "a", status: "active", awaiting_input_since: "t" })) ).toBe(true); expect(m(msg("session_updated", { id: "a", status: "completed" }))).toBe(true); expect(m(msg("session_updated", { id: "a", status: "completed", archived_at: "t" }))).toBe( true ); }); it("takes an unseen or unkeyable session as a change, and ignores other types", () => { const m = sessionChangeMatcher(); expect(m(msg("session_updated", { id: "b", status: "active" }))).toBe(true); expect(m(msg("session_updated", null))).toBe(true); expect(m(msg("new_event", { session_id: "b" }))).toBe(false); }); });