|
1 | 1 | // Event routing: channel selection, sender filtering (self/control/allowlist), |
2 | | -// reactions, deduped delivery-failure captures, external events, and media. |
| 2 | +// reactions, deduped delivery-failure captures, sender agent identities, |
| 3 | +// external events, and media. |
3 | 4 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
4 | 5 | import type { ResolvedConfig } from "../../src/config.js"; |
5 | 6 | import { defaultGatewayConfig } from "../../src/config.js"; |
@@ -228,6 +229,148 @@ describe("dispatchEvent delivery failures", () => { |
228 | 229 | expect.stringContaining("+15551112222"), |
229 | 230 | ); |
230 | 231 | }); |
| 232 | + |
| 233 | + it("acks text.delivery_unconfirmed without waking the agent", async () => { |
| 234 | + // Carrier uncertainty, not a failure: a capture would prompt a resend of |
| 235 | + // a message that usually landed. |
| 236 | + const deps = makeDeps(); |
| 237 | + const ok = await dispatchEvent( |
| 238 | + deps, |
| 239 | + event("text.delivery_unconfirmed", { |
| 240 | + text_message: { |
| 241 | + id: "msg-100", |
| 242 | + remote_phone_number: "+15551112222", |
| 243 | + error_code: "delivery_unconfirmed", |
| 244 | + }, |
| 245 | + }), |
| 246 | + ); |
| 247 | + |
| 248 | + expect(ok).toBe(true); |
| 249 | + expect(deps.sessions.runCapture).not.toHaveBeenCalled(); |
| 250 | + expect(deps.sessions.handleInbound).not.toHaveBeenCalled(); |
| 251 | + }); |
| 252 | +}); |
| 253 | + |
| 254 | +describe("dispatchEvent sender agent identity", () => { |
| 255 | + // A backend-resolved peer agent on the event, keyed like the webhook payload. |
| 256 | + const identity = { id: "agent-42", agent_handle: "atlas-agent", display_name: "Atlas" }; |
| 257 | + |
| 258 | + function noContactDeps(over: Partial<DispatchDeps> = {}): DispatchDeps { |
| 259 | + return makeDeps({ |
| 260 | + contacts: { resolve: vi.fn(async () => ({})), chatKeyFor: vi.fn(() => "ck") }, |
| 261 | + ...over, |
| 262 | + }); |
| 263 | + } |
| 264 | + |
| 265 | + function sms(agentIdentities: unknown[]): VerifiedEvent { |
| 266 | + return event("text.received", { |
| 267 | + text_message: { |
| 268 | + id: "tm-9", |
| 269 | + remote_phone_number: "+15551112222", |
| 270 | + text: "hey from another agent", |
| 271 | + conversation_id: "sms-conv-9", |
| 272 | + media: null, |
| 273 | + }, |
| 274 | + contacts: [], |
| 275 | + agent_identities: agentIdentities, |
| 276 | + }); |
| 277 | + } |
| 278 | + |
| 279 | + function mail(agentIdentities: unknown[]): VerifiedEvent { |
| 280 | + return event("message.received", { |
| 281 | + message: { id: "m-9", from_address: "atlas@agents.inkbox.ai", body: "coordinating" }, |
| 282 | + contacts: [], |
| 283 | + agent_identities: agentIdentities, |
| 284 | + }); |
| 285 | + } |
| 286 | + |
| 287 | + it("attaches the single resolved identity of a contactless SMS sender", async () => { |
| 288 | + const deps = noContactDeps(); |
| 289 | + await dispatchEvent(deps, sms([identity])); |
| 290 | + |
| 291 | + expect(deps.sessions.handleInbound).toHaveBeenCalledWith( |
| 292 | + expect.objectContaining({ |
| 293 | + senderAgent: { id: "agent-42", handle: "atlas-agent", displayName: "Atlas" }, |
| 294 | + }), |
| 295 | + ); |
| 296 | + }); |
| 297 | + |
| 298 | + it("omits the identity when the sender resolves to a contact", async () => { |
| 299 | + const deps = makeDeps(); |
| 300 | + await dispatchEvent(deps, sms([identity])); |
| 301 | + |
| 302 | + const msg = vi.mocked(deps.sessions.handleInbound).mock.calls[0][0]; |
| 303 | + expect(msg.contactId).toBe("c1"); |
| 304 | + expect(msg.senderAgent).toBeUndefined(); |
| 305 | + }); |
| 306 | + |
| 307 | + it("omits the identity when several resolve (group of agents)", async () => { |
| 308 | + const deps = noContactDeps(); |
| 309 | + await dispatchEvent( |
| 310 | + deps, |
| 311 | + sms([identity, { id: "agent-43", agent_handle: "nova-agent", display_name: "Nova" }]), |
| 312 | + ); |
| 313 | + |
| 314 | + const msg = vi.mocked(deps.sessions.handleInbound).mock.calls[0][0]; |
| 315 | + expect(msg.senderAgent).toBeUndefined(); |
| 316 | + expect(msg.group?.participantCount).toBe(2); |
| 317 | + }); |
| 318 | + |
| 319 | + it("omits an identity entry that carries no id", async () => { |
| 320 | + const deps = noContactDeps(); |
| 321 | + await dispatchEvent(deps, sms([{ agent_handle: "no-id-agent" }])); |
| 322 | + |
| 323 | + expect(vi.mocked(deps.sessions.handleInbound).mock.calls[0][0].senderAgent).toBeUndefined(); |
| 324 | + }); |
| 325 | + |
| 326 | + it("trusts a mail identity only from the from bucket matching the sender", async () => { |
| 327 | + const deps = noContactDeps(); |
| 328 | + await dispatchEvent( |
| 329 | + deps, |
| 330 | + mail([{ ...identity, bucket: "from", address: "Atlas@agents.inkbox.ai" }]), |
| 331 | + ); |
| 332 | + |
| 333 | + expect(deps.sessions.handleInbound).toHaveBeenCalledWith( |
| 334 | + expect.objectContaining({ |
| 335 | + senderAgent: { id: "agent-42", handle: "atlas-agent", displayName: "Atlas" }, |
| 336 | + }), |
| 337 | + ); |
| 338 | + }); |
| 339 | + |
| 340 | + it("ignores a mail identity resolved for a recipient bucket", async () => { |
| 341 | + const deps = noContactDeps(); |
| 342 | + await dispatchEvent( |
| 343 | + deps, |
| 344 | + mail([{ ...identity, bucket: "to", address: "me@agents.inkbox.ai" }]), |
| 345 | + ); |
| 346 | + |
| 347 | + expect(vi.mocked(deps.sessions.handleInbound).mock.calls[0][0].senderAgent).toBeUndefined(); |
| 348 | + }); |
| 349 | + |
| 350 | + it("names a contactless reaction sender by their identity", async () => { |
| 351 | + const deps = noContactDeps(); |
| 352 | + await dispatchEvent( |
| 353 | + deps, |
| 354 | + event("imessage.reaction_received", { |
| 355 | + reaction: { |
| 356 | + id: "rx-9", |
| 357 | + conversation_id: "conv-9", |
| 358 | + remote_number: "+15551112222", |
| 359 | + reaction: "loved", |
| 360 | + target_message_id: "im-9", |
| 361 | + }, |
| 362 | + contacts: [], |
| 363 | + agent_identities: [identity], |
| 364 | + }), |
| 365 | + ); |
| 366 | + |
| 367 | + expect(deps.sessions.handleInbound).toHaveBeenCalledWith( |
| 368 | + expect.objectContaining({ |
| 369 | + senderAgent: { id: "agent-42", handle: "atlas-agent", displayName: "Atlas" }, |
| 370 | + text: expect.stringContaining("Atlas reacted with a 'loved' tapback"), |
| 371 | + }), |
| 372 | + ); |
| 373 | + }); |
231 | 374 | }); |
232 | 375 |
|
233 | 376 | describe("dispatchEvent external providers", () => { |
|
0 commit comments