| 1 | import { test } from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | |
| 4 | import { MAX_HOPS, deliveries, delivery } from "./delivery.ts"; |
| 5 | |
| 6 | const ship = { id: "a1", handle: "ship" }; |
| 7 | const docs = { id: "a2", handle: "docs" }; |
| 8 | |
| 9 | test("a person's direct message wakes every agent in it, at hop 0", () => { |
| 10 | assert.deepEqual(deliveries({ author: "user:u", hops: 3, channelKind: "dm", agents: [ship, docs], mentioned: [] }), [ |
| 11 | { agent_id: "a1", hops: 0 }, |
| 12 | { agent_id: "a2", hops: 0 }, |
| 13 | ]); |
| 14 | }); |
| 15 | |
| 16 | test("in a channel, a person wakes only the agent members they mention", () => { |
| 17 | assert.deepEqual(deliveries({ author: "user:u", hops: 0, channelKind: "channel", agents: [ship, docs], mentioned: [] }), []); |
| 18 | assert.deepEqual( |
| 19 | deliveries({ author: "user:u", hops: 0, channelKind: "channel", agents: [ship, docs], mentioned: ["Ship", "nobody"] }), |
| 20 | [{ agent_id: "a1", hops: 0 }], |
| 21 | ); |
| 22 | }); |
| 23 | |
| 24 | test("an agent wakes other agents only by mentioning them, one hop further", () => { |
| 25 | // In a direct message with two agents, an agent's reply wakes nobody unless addressed. |
| 26 | assert.deepEqual(deliveries({ author: "agent:a1", hops: 0, channelKind: "dm", agents: [ship, docs], mentioned: [] }), []); |
| 27 | assert.deepEqual( |
| 28 | deliveries({ author: "agent:a1", hops: 2, channelKind: "channel", agents: [ship, docs], mentioned: ["docs"] }), |
| 29 | [{ agent_id: "a2", hops: 3 }], |
| 30 | ); |
| 31 | }); |
| 32 | |
| 33 | test("an agent is never handed its own message", () => { |
| 34 | assert.deepEqual( |
| 35 | deliveries({ author: "agent:a1", hops: 0, channelKind: "channel", agents: [ship, docs], mentioned: ["ship"] }), |
| 36 | [], |
| 37 | ); |
| 38 | }); |
| 39 | |
| 40 | test("a chain stops at the hop limit", () => { |
| 41 | const at = (hops: number) => |
| 42 | deliveries({ author: "agent:a1", hops, channelKind: "channel", agents: [ship, docs], mentioned: ["docs"] }); |
| 43 | assert.equal(MAX_HOPS, 6); |
| 44 | assert.deepEqual(at(MAX_HOPS - 1), [{ agent_id: "a2", hops: MAX_HOPS }]); |
| 45 | assert.deepEqual(at(MAX_HOPS), []); |
| 46 | assert.deepEqual(at(50), []); |
| 47 | }); |
| 48 | |
| 49 | test("a delivery carries the chain's asker on g1t's own chat, and the wake's hops", () => { |
| 50 | const asker = { username: "ada", role: "member", can_write: false }; |
| 51 | const place = { workspace: "acme", workspace_id: "w1", channel_id: "c1", channel_kind: "channel", channel_name: "ops" }; |
| 52 | assert.deepEqual( |
| 53 | delivery(place, { agent_id: "a2", hops: 3 }, { id: "m1", thread_root: "m0" }, { hops: 2, asked_by: "u1", asker, chain: ["a1"] }), |
| 54 | { ...place, agent_id: "a2", message_id: "m1", thread_root: "m0", asked_by: "u1", asker, chain: ["a1"], hops: 3, surface: "g1t" }, |
| 55 | ); |
| 56 | // An agent that passed no asker back hands on none. |
| 57 | assert.equal(delivery(place, { agent_id: "a2", hops: 1 }, { id: "m1", thread_root: null }, { hops: 0, asked_by: "u1", asker: null, chain: [] }).asker, null); |
| 58 | }); |
| 59 | |
| 60 | test("mentioning @g1t in a channel brings it in once; never into a DM", async () => { |
| 61 | const { addsOrchestrator } = await import("./delivery.ts"); |
| 62 | assert.equal(addsOrchestrator({ channelKind: "channel", mentioned: ["G1T"], orchestratorIsMember: false }), true); |
| 63 | assert.equal(addsOrchestrator({ channelKind: "channel", mentioned: ["g1t"], orchestratorIsMember: true }), false, "already in"); |
| 64 | assert.equal(addsOrchestrator({ channelKind: "channel", mentioned: ["ship"], orchestratorIsMember: false }), false); |
| 65 | assert.equal(addsOrchestrator({ channelKind: "dm", mentioned: ["g1t"], orchestratorIsMember: false }), false); |
| 66 | // Once in, it is woken like any mentioned agent member. |
| 67 | const g1t = { id: "a9", handle: "g1t" }; |
| 68 | assert.deepEqual(deliveries({ author: "user:u", hops: 0, channelKind: "channel", agents: [ship, g1t], mentioned: ["g1t"] }), [{ agent_id: "a9", hops: 0 }]); |
| 69 | }); |
| 70 | |
| 71 | test("no ping-pong: an agent's message never goes back to the agent that sent it the work", async () => { |
| 72 | const { chainFor, sender } = await import("./delivery.ts"); |
| 73 | const g1t = { id: "a9", handle: "g1t" }; |
| 74 | // g1t handed the work to ship; ship's reply mentions g1t and docs. |
| 75 | const chain = chainFor(["a9"], "a1"); |
| 76 | assert.deepEqual(chain, ["a9", "a1"]); |
| 77 | assert.equal(sender(chain), "a9"); |
| 78 | assert.deepEqual( |
| 79 | deliveries({ author: "agent:a1", hops: 1, channelKind: "channel", agents: [ship, docs, g1t], mentioned: ["g1t", "docs"], notTo: sender(chain) }), |
| 80 | [{ agent_id: "a2", hops: 2 }], |
| 81 | ); |
| 82 | assert.deepEqual(chainFor("nonsense", "a1"), ["a1"]); |
| 83 | assert.equal(sender(["a1"]), null, "a person sent it"); |
| 84 | }); |
| 85 | |
| 86 | test("the hop limit holds along a chain of hand-offs", () => { |
| 87 | assert.deepEqual(deliveries({ author: "agent:a1", hops: MAX_HOPS - 1, channelKind: "channel", agents: [docs], mentioned: ["docs"] }), [{ agent_id: "a2", hops: MAX_HOPS }]); |
| 88 | assert.deepEqual(deliveries({ author: "agent:a1", hops: MAX_HOPS, channelKind: "channel", agents: [docs], mentioned: ["docs"] }), []); |
| 89 | }); |