|
| 1 | +import type { AgentAwarenessState } from "@t3tools/shared/agentAwareness"; |
| 2 | +import { describe, expect, it } from "vite-plus/test"; |
| 3 | + |
| 4 | +import { |
| 5 | + attentionKey, |
| 6 | + attentionKindForEdge, |
| 7 | + buildAttentionPayload, |
| 8 | + createAttentionEdgeTracker, |
| 9 | +} from "./attention.ts"; |
| 10 | + |
| 11 | +describe("attentionKindForEdge", () => { |
| 12 | + it("announces waiting phases on entry regardless of the previous phase", () => { |
| 13 | + expect(attentionKindForEdge("running", "waiting_for_approval")).toBe("waiting_for_approval"); |
| 14 | + expect(attentionKindForEdge("completed", "waiting_for_input")).toBe("waiting_for_input"); |
| 15 | + }); |
| 16 | + |
| 17 | + it("announces completed only out of running", () => { |
| 18 | + expect(attentionKindForEdge("running", "completed")).toBe("completed"); |
| 19 | + expect(attentionKindForEdge("starting", "completed")).toBeNull(); |
| 20 | + expect(attentionKindForEdge(null, "completed")).toBeNull(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("stays silent for non-attention phases", () => { |
| 24 | + expect(attentionKindForEdge("waiting_for_input", "running")).toBeNull(); |
| 25 | + expect(attentionKindForEdge("running", "starting")).toBeNull(); |
| 26 | + expect(attentionKindForEdge("running", "failed")).toBeNull(); |
| 27 | + expect(attentionKindForEdge("running", "stale")).toBeNull(); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("createAttentionEdgeTracker", () => { |
| 32 | + it("never fires on a thread's first observation", () => { |
| 33 | + const tracker = createAttentionEdgeTracker(); |
| 34 | + expect(tracker.observe("t1", "waiting_for_input")).toBeNull(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("fires completed only on running -> completed", () => { |
| 38 | + const tracker = createAttentionEdgeTracker(); |
| 39 | + expect(tracker.observe("t1", "running")).toBeNull(); // first-seen |
| 40 | + expect(tracker.observe("t1", "completed")).toBe("completed"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("does not fire completed when the thread was never seen running", () => { |
| 44 | + const tracker = createAttentionEdgeTracker(); |
| 45 | + expect(tracker.observe("t1", "starting")).toBeNull(); // first-seen |
| 46 | + expect(tracker.observe("t1", "completed")).toBeNull(); // starting -> completed |
| 47 | + }); |
| 48 | + |
| 49 | + it("fires waiting_for_input on entry after a prior observation", () => { |
| 50 | + const tracker = createAttentionEdgeTracker(); |
| 51 | + expect(tracker.observe("t1", "running")).toBeNull(); |
| 52 | + expect(tracker.observe("t1", "waiting_for_input")).toBe("waiting_for_input"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("does not re-fire while the phase is unchanged", () => { |
| 56 | + const tracker = createAttentionEdgeTracker(); |
| 57 | + tracker.observe("t1", "running"); |
| 58 | + expect(tracker.observe("t1", "waiting_for_approval")).toBe("waiting_for_approval"); |
| 59 | + expect(tracker.observe("t1", "waiting_for_approval")).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("treats a null phase as an observation that never fires", () => { |
| 63 | + const tracker = createAttentionEdgeTracker(); |
| 64 | + tracker.observe("t1", "running"); |
| 65 | + expect(tracker.observe("t1", null)).toBeNull(); |
| 66 | + // Coming back to completed from a null observation is not a running->completed edge. |
| 67 | + expect(tracker.observe("t1", "completed")).toBeNull(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("tracks threads independently", () => { |
| 71 | + const tracker = createAttentionEdgeTracker(); |
| 72 | + tracker.observe("t1", "running"); |
| 73 | + expect(tracker.observe("t2", "completed")).toBeNull(); // t2 first-seen |
| 74 | + expect(tracker.observe("t1", "completed")).toBe("completed"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("re-arms first-seen after forget", () => { |
| 78 | + const tracker = createAttentionEdgeTracker(); |
| 79 | + tracker.observe("t1", "running"); |
| 80 | + tracker.forget("t1"); |
| 81 | + // First observation again -> no fire even though it's a completed value. |
| 82 | + expect(tracker.observe("t1", "completed")).toBeNull(); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("buildAttentionPayload", () => { |
| 87 | + it("maps an awareness state + edge into the service-worker payload", () => { |
| 88 | + const state = { |
| 89 | + environmentId: "env-1", |
| 90 | + threadId: "thread-1", |
| 91 | + projectTitle: "My Project", |
| 92 | + threadTitle: "Fix the bug", |
| 93 | + phase: "waiting_for_input", |
| 94 | + headline: "Waiting for input", |
| 95 | + modelTitle: "claude", |
| 96 | + updatedAt: "2026-07-27T00:00:00.000Z", |
| 97 | + deepLink: "/threads/env-1/thread-1", |
| 98 | + } as unknown as AgentAwarenessState; |
| 99 | + |
| 100 | + expect(buildAttentionPayload(state, "waiting_for_input")).toEqual({ |
| 101 | + title: "Waiting for input", |
| 102 | + body: "My Project · Fix the bug", |
| 103 | + key: "env-1::thread-1", |
| 104 | + environmentId: "env-1", |
| 105 | + threadId: "thread-1", |
| 106 | + kind: "waiting_for_input", |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it("keys by environment + thread", () => { |
| 111 | + expect(attentionKey("env-1", "thread-1")).toBe("env-1::thread-1"); |
| 112 | + }); |
| 113 | +}); |
0 commit comments