| 1 | import { test } from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | |
| 4 | import { MAX_HOPS, chainFor, deliveries, delivery, handOffPlace, handOffRefusal } 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 group direct message, a person who mentions some of its agents wakes only those", () => { |
| 17 | assert.deepEqual(deliveries({ author: "user:u", hops: 0, channelKind: "dm", agents: [ship, docs], mentioned: ["docs"] }), [{ agent_id: "a2", hops: 0 }]); |
| 18 | // Mentioning someone who isn't in it is no mention of its agents: all of them answer. |
| 19 | assert.deepEqual(deliveries({ author: "user:u", hops: 0, channelKind: "dm", agents: [ship, docs], mentioned: ["mike"] }), [ |
| 20 | { agent_id: "a1", hops: 0 }, |
| 21 | { agent_id: "a2", hops: 0 }, |
| 22 | ]); |
| 23 | }); |
| 24 | |
| 25 | test("in a channel, a person wakes only the agent members they mention", () => { |
| 26 | assert.deepEqual(deliveries({ author: "user:u", hops: 0, channelKind: "channel", agents: [ship, docs], mentioned: [] }), []); |
| 27 | assert.deepEqual( |
| 28 | deliveries({ author: "user:u", hops: 0, channelKind: "channel", agents: [ship, docs], mentioned: ["Ship", "nobody"] }), |
| 29 | [{ agent_id: "a1", hops: 0 }], |
| 30 | ); |
| 31 | }); |
| 32 | |
| 33 | test("an agent's message wakes nobody, even when it @mentions an agent member", () => { |
| 34 | assert.deepEqual(deliveries({ author: "agent:a1", hops: 0, channelKind: "dm", agents: [ship, docs], mentioned: [] }), []); |
| 35 | assert.deepEqual(deliveries({ author: "agent:a1", hops: 0, channelKind: "dm", agents: [ship, docs], mentioned: ["docs"] }), []); |
| 36 | assert.deepEqual(deliveries({ author: "agent:a1", hops: 2, channelKind: "channel", agents: [ship, docs], mentioned: ["docs", "ship"] }), []); |
| 37 | }); |
| 38 | |
| 39 | test("a delivery carries the chain's asker on g1t's own chat, and the wake's hops", () => { |
| 40 | const asker = { username: "ada", role: "member", can_write: false }; |
| 41 | const place = { workspace: "acme", workspace_id: "w1", channel_id: "c1", channel_kind: "channel", channel_name: "ops" }; |
| 42 | assert.deepEqual( |
| 43 | delivery(place, { agent_id: "a2", hops: 3 }, { id: "m1", thread_root: "m0" }, { hops: 2, asked_by: "u1", asker, chain: ["a1"] }), |
| 44 | { ...place, agent_id: "a2", message_id: "m1", thread_root: "m0", asked_by: "u1", asker, chain: ["a1"], hops: 3, surface: "g1t" }, |
| 45 | ); |
| 46 | // An agent that passed no asker back hands on none. |
| 47 | assert.equal(delivery(place, { agent_id: "a2", hops: 1 }, { id: "m1", thread_root: null }, { hops: 0, asked_by: "u1", asker: null, chain: [] }).asker, null); |
| 48 | }); |
| 49 | |
| 50 | test("mentioning @g1t in a channel brings it in once; never into a DM", async () => { |
| 51 | const { addsOrchestrator } = await import("./delivery.ts"); |
| 52 | assert.equal(addsOrchestrator({ channelKind: "channel", mentioned: ["G1T"], orchestratorIsMember: false }), true); |
| 53 | assert.equal(addsOrchestrator({ channelKind: "channel", mentioned: ["g1t"], orchestratorIsMember: true }), false, "already in"); |
| 54 | assert.equal(addsOrchestrator({ channelKind: "channel", mentioned: ["ship"], orchestratorIsMember: false }), false); |
| 55 | assert.equal(addsOrchestrator({ channelKind: "dm", mentioned: ["g1t"], orchestratorIsMember: false }), false); |
| 56 | // Once in, it is woken like any mentioned agent member. |
| 57 | const g1t = { id: "a9", handle: "g1t" }; |
| 58 | assert.deepEqual(deliveries({ author: "user:u", hops: 0, channelKind: "channel", agents: [ship, g1t], mentioned: ["g1t"] }), [{ agent_id: "a9", hops: 0 }]); |
| 59 | }); |
| 60 | |
| 61 | test("a chain is the agents that handled a request, with the poster added", () => { |
| 62 | assert.deepEqual(chainFor(["a9"], "a1"), ["a9", "a1"]); |
| 63 | assert.deepEqual(chainFor("nonsense", "a1"), ["a1"]); |
| 64 | }); |
| 65 | |
| 66 | test("a hand-off goes here when the colleague is in this channel or group DM, else to a group DM", () => { |
| 67 | assert.equal(handOffPlace({ channelKind: "channel", members: 40, colleagueHere: true }), "here"); |
| 68 | assert.equal(handOffPlace({ channelKind: "dm", members: 3, colleagueHere: true }), "here"); |
| 69 | assert.equal(handOffPlace({ channelKind: "channel", members: 40, colleagueHere: false }), "group_dm"); |
| 70 | // The 1:1 DM the user saw: @g1t and the person, Mike not in it. |
| 71 | assert.equal(handOffPlace({ channelKind: "dm", members: 2, colleagueHere: false }), "group_dm"); |
| 72 | }); |
| 73 | |
| 74 | test("a hand-off is refused to itself, to @g1t, back along the chain, and past the hop limit", () => { |
| 75 | const ok = { agent: "a9", colleague: { id: "a1" }, chain: [], hops: 0 }; |
| 76 | assert.equal(handOffRefusal(ok), null); |
| 77 | assert.match(handOffRefusal({ ...ok, colleague: { id: "a9" } })!, /itself/); |
| 78 | assert.match(handOffRefusal({ ...ok, agent: "a1", colleague: { id: "a9", builtin: true } })!, /@g1t/); |
| 79 | assert.match(handOffRefusal({ ...ok, agent: "a2", chain: ["a1", "a2"] })!, /already handled/); |
| 80 | assert.equal(handOffRefusal({ ...ok, hops: MAX_HOPS - 1 }), null); |
| 81 | assert.match(handOffRefusal({ ...ok, hops: MAX_HOPS })!, /too many times/); |
| 82 | }); |