| 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 }), |
| 54 | { ...place, agent_id: "a2", message_id: "m1", thread_root: "m0", asked_by: "u1", asker, 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 }).asker, null); |
| 58 | }); |