import { describe, expect, test } from "bun:test"; import { buildEventHandleInvalidated, eventHandleInvalidatedSchema, } from "./core-handle-invalidation.js"; import { createGeneration } from "./v3-generation.js"; const validMessage = { t: "getuserfeedback:event:handleInvalidated", gen: createGeneration(3), instanceId: "instance-a", handleKind: "flow", handleId: "handle-a", reasonCode: "CLOSED", source: "core", at: 1_723_000_000_000, } as const; describe("core handle invalidation", () => { test("accepts a complete invalidation message", () => { expect(eventHandleInvalidatedSchema.safeParse(validMessage).success).toBe( true, ); }); test("accepts optional routing and diagnostic fields", () => { expect( eventHandleInvalidatedSchema.safeParse({ ...validMessage, viewId: "view-a", reasonMessage: "flow closed", relatedRequestId: "request-a", }).success, ).toBe(true); }); test("builds the canonical core-to-host envelope", () => { expect( buildEventHandleInvalidated({ gen: 3, instanceId: "instance-a", handleKind: "flow", handleId: "handle-a", reasonCode: "CLOSED", source: "core", at: 1_723_000_000_000, }), ).toEqual(validMessage); }); test("rejects invalid lifecycle values and blank identifiers", () => { for (const message of [ { ...validMessage, handleId: " " }, { ...validMessage, reasonCode: "EXPIRED" }, { ...validMessage, source: "react-native" }, { ...validMessage, gen: -1 }, ]) { expect(eventHandleInvalidatedSchema.safeParse(message).success).toBe( false, ); } }); });