|
| 1 | +import { afterEach, expect, test } from "bun:test"; |
| 2 | +import type { AgentProfileStore } from "../../server/src/agents/profile-store"; |
| 3 | +import type { AuditStore } from "../../server/src/audit"; |
| 4 | +import type { IntentRouter } from "../../server/src/routing/classify"; |
| 5 | +import { createRoutingRoutes } from "../../server/src/routing/routes"; |
| 6 | +import { routeMessage } from "../src/lib/channels/route"; |
| 7 | + |
| 8 | +/** |
| 9 | + * A long message still finds its coworker, and the trail still says how. |
| 10 | + * |
| 11 | + * `POST /api/route` refuses a message over 10,000 characters, so the model prompt it builds stays |
| 12 | + * bounded. The composer has no such limit: a pasted email thread or log is one message. Both callers |
| 13 | + * of `routeMessage` carry on past a failed routing on purpose, so the refusal said nothing on |
| 14 | + * screen. The home composer sent the message to the default coworker instead of the one it is for, |
| 15 | + * and a coworker the person chose was started without its `channel.routed` row. |
| 16 | + * |
| 17 | + * The route itself answers here, not a stub of it, so the cap these hold against is the server's own. |
| 18 | + */ |
| 19 | + |
| 20 | +const originalFetch = globalThis.fetch; |
| 21 | +afterEach(() => { |
| 22 | + globalThis.fetch = originalFetch; |
| 23 | +}); |
| 24 | + |
| 25 | +const ROSTER = [ |
| 26 | + { |
| 27 | + id: "general-assistant", |
| 28 | + name: "General Assistant", |
| 29 | + roleDescription: "everyday work", |
| 30 | + visibility: "public", |
| 31 | + }, |
| 32 | + { |
| 33 | + id: "risk-analyst", |
| 34 | + name: "Risk Analyst", |
| 35 | + roleDescription: "regulatory and compliance questions", |
| 36 | + visibility: "public", |
| 37 | + }, |
| 38 | +]; |
| 39 | + |
| 40 | +function serve() { |
| 41 | + /** What the router was asked to read, so the text it saw is an assertion. */ |
| 42 | + const asked: string[] = []; |
| 43 | + const written: { eventType: string; payload: Record<string, unknown> }[] = []; |
| 44 | + |
| 45 | + const asActor: Parameters<typeof createRoutingRoutes>[2] = async ( |
| 46 | + context, |
| 47 | + next, |
| 48 | + ) => { |
| 49 | + context.set("actor", { |
| 50 | + id: "u1", |
| 51 | + email: "person@openbot.test", |
| 52 | + role: "user", |
| 53 | + }); |
| 54 | + await next(); |
| 55 | + }; |
| 56 | + const store = { list: async () => ROSTER } as unknown as AgentProfileStore; |
| 57 | + const router = { |
| 58 | + route: async (text: string) => { |
| 59 | + asked.push(text); |
| 60 | + return { |
| 61 | + agentId: "risk-analyst", |
| 62 | + name: "Risk Analyst", |
| 63 | + reason: "matches what it is for", |
| 64 | + fallback: false, |
| 65 | + undecided: null, |
| 66 | + }; |
| 67 | + }, |
| 68 | + } as unknown as IntentRouter; |
| 69 | + const auditStore = { |
| 70 | + insert: async (event: { |
| 71 | + eventType: string; |
| 72 | + payload: Record<string, unknown>; |
| 73 | + }) => { |
| 74 | + written.push(event); |
| 75 | + }, |
| 76 | + } as unknown as AuditStore; |
| 77 | + |
| 78 | + const routes = createRoutingRoutes(store, router, asActor, auditStore); |
| 79 | + globalThis.fetch = Object.assign( |
| 80 | + async ( |
| 81 | + path: Parameters<typeof fetch>[0], |
| 82 | + init?: Parameters<typeof fetch>[1], |
| 83 | + ) => { |
| 84 | + if (path !== "/api/route") throw new Error(`unexpected ${String(path)}`); |
| 85 | + return routes.request("http://openbot.test/", init); |
| 86 | + }, |
| 87 | + { preconnect: originalFetch.preconnect }, |
| 88 | + ); |
| 89 | + return { asked, written }; |
| 90 | +} |
| 91 | + |
| 92 | +test("a message longer than the route reads is routed to the coworker it is for, and recorded", async () => { |
| 93 | + const { asked, written } = serve(); |
| 94 | + |
| 95 | + const decision = await routeMessage( |
| 96 | + `Which of these clauses breach the policy?\n${"clause ".repeat(4_000)}`, |
| 97 | + ); |
| 98 | + |
| 99 | + expect(decision.agentId).toBe("risk-analyst"); |
| 100 | + expect(asked).toHaveLength(1); |
| 101 | + expect(asked[0]?.startsWith("Which of these clauses")).toBe(true); |
| 102 | + expect(written.map((row) => row.eventType)).toEqual(["channel.routed"]); |
| 103 | +}); |
| 104 | + |
| 105 | +test("a long message to a coworker the person chose is recorded as their choice", async () => { |
| 106 | + const { asked, written } = serve(); |
| 107 | + |
| 108 | + const decision = await routeMessage("x".repeat(25_000), "risk-analyst"); |
| 109 | + |
| 110 | + expect(decision).toMatchObject({ |
| 111 | + agentId: "risk-analyst", |
| 112 | + viaMention: true, |
| 113 | + }); |
| 114 | + expect(asked).toEqual([]); |
| 115 | + expect(written).toHaveLength(1); |
| 116 | + expect(written[0]?.payload).toMatchObject({ |
| 117 | + chosen: "risk-analyst", |
| 118 | + viaMention: true, |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +test("the opening is cut between characters, not through an emoji", async () => { |
| 123 | + const { asked } = serve(); |
| 124 | + |
| 125 | + // The emoji's two halves straddle the 10,000th unit. |
| 126 | + await routeMessage(`${"a".repeat(9_999)}😀${"b".repeat(50)}`); |
| 127 | + |
| 128 | + expect(asked).toEqual(["a".repeat(9_999)]); |
| 129 | +}); |
| 130 | + |
| 131 | +test("a message that fits is sent as it was written", async () => { |
| 132 | + const { asked } = serve(); |
| 133 | + |
| 134 | + await routeMessage("Is this contract compliant? 😀"); |
| 135 | + |
| 136 | + expect(asked).toEqual(["Is this contract compliant? 😀"]); |
| 137 | +}); |
0 commit comments