File size: 3,125 Bytes
3c124f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Parity pins between engine/wire.ts and /schema/event.schema.json — the
// cross-language contract. If these fail, one side of the wire drifted.
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { EVENTS } from "./events";
import {
  isWireEvent,
  TEMPLATES,
  toEventDef,
  WIRE_OPTIONAL,
  WIRE_REQUIRED,
  WIRE_SOURCES,
  type WireEvent,
} from "./wire";

const schema = JSON.parse(readFileSync(join(__dirname, "../../../schema/event.schema.json"), "utf8"));

describe("schema parity", () => {
  it("required fields match the JSON schema", () => {
    expect([...WIRE_REQUIRED].sort()).toEqual([...schema.required].sort());
  });

  it("all schema properties are known to the TS side", () => {
    expect(Object.keys(schema.properties).sort()).toEqual([...WIRE_REQUIRED, ...WIRE_OPTIONAL].sort());
  });

  it("source enum matches the JSON schema", () => {
    expect([...WIRE_SOURCES].sort()).toEqual([...schema.properties.source.enum].sort());
  });
});

describe("isWireEvent", () => {
  it("accepts a minimal valid event", () => {
    expect(isWireEvent({ source: "claude", type: "task_completed", title: "Done" })).toBe(true);
  });

  it("rejects unknown sources, empty fields, and non-objects", () => {
    expect(isWireEvent({ source: "slack", type: "x", title: "y" })).toBe(false);
    expect(isWireEvent({ source: "claude", type: "", title: "y" })).toBe(false);
    expect(isWireEvent({ source: "claude", type: "x", title: "" })).toBe(false);
    expect(isWireEvent(null)).toBe(false);
    expect(isWireEvent("claude")).toBe(false);
  });
});

describe("TEMPLATES integrity", () => {
  it("every template id resolves to a deck event (renames can't silently fall through)", () => {
    const ids = new Set(EVENTS.map((e) => e.id));
    for (const id of Object.values(TEMPLATES)) expect(ids).toContain(id);
  });
});

describe("toEventDef", () => {
  const claude: WireEvent = {
    source: "claude",
    type: "task_completed",
    title: "Implement wire schema",
    summary: "3 files changed",
  };

  it("borrows the matching template's scoring", () => {
    const def = toEventDef(claude);
    const template = EVENTS.find((e) => e.id === "claude_done");
    expect(def.base).toEqual(template?.base);
    expect(def.source).toBe("Claude");
    expect(def.lines.plain).toBe("Implement wire schema — 3 files changed");
  });

  it("real events never mutate sim windows and have no flight target", () => {
    const def = toEventDef(claude);
    expect(def.effect).toBe("none");
    expect(def.target).toBeNull();
  });

  it("unknown types get middling fallback scores (never an interrupt)", () => {
    const def = toEventDef({ source: "build", type: "linted", title: "Lint clean" });
    expect(def.base.urgency).toBeLessThan(80);
    expect(def.lines.plain).toBe("Lint clean");
  });

  it("build failures map to the urgent template", () => {
    const def = toEventDef({ source: "build", type: "tests_failed", title: "2 failing" });
    expect(def.base).toEqual(EVENTS.find((e) => e.id === "build_fail")?.base);
  });
});