|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Qwen |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Broadcast delivery-outcome contract for send_message(to: "*") — #10072. |
| 9 | + * |
| 10 | + * Uses the real TeamManager (via TeamCoordinationHarness) so a delivery |
| 11 | + * rejects the same way it does in production: a recipient terminates |
| 12 | + * between the member snapshot and the send, its per-agent queue is |
| 13 | + * dropped, and sendMessage refuses the delivery. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 17 | +import { SendMessageTool } from './send-message.js'; |
| 18 | +import { BackgroundTaskRegistry } from '../agents/background-tasks.js'; |
| 19 | +import type { ApprovalMode, Config } from '../config/config.js'; |
| 20 | +import type { TeamManager } from '../agents/team/TeamManager.js'; |
| 21 | +import { TeamCoordinationHarness } from '../agents/team/test-utils/coordination-harness.js'; |
| 22 | + |
| 23 | +// Mock Storage so all file I/O uses the harness's temp dir. |
| 24 | +vi.mock('../config/storage.js', async (importOriginal) => { |
| 25 | + const original = |
| 26 | + await importOriginal<typeof import('../config/storage.js')>(); |
| 27 | + let mockGlobalDir = ''; |
| 28 | + return { |
| 29 | + ...original, |
| 30 | + Storage: { |
| 31 | + ...original.Storage, |
| 32 | + getGlobalQwenDir: () => mockGlobalDir, |
| 33 | + __setMockGlobalDir: (dir: string) => { |
| 34 | + mockGlobalDir = dir; |
| 35 | + }, |
| 36 | + }, |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +import { Storage } from '../config/storage.js'; |
| 41 | + |
| 42 | +function setMockDir(dir: string): void { |
| 43 | + ( |
| 44 | + Storage as unknown as { |
| 45 | + __setMockGlobalDir: (d: string) => void; |
| 46 | + } |
| 47 | + ).__setMockGlobalDir(dir); |
| 48 | +} |
| 49 | + |
| 50 | +function makeConfig(teamManager: TeamManager): Config { |
| 51 | + return { |
| 52 | + getTeamManager: () => teamManager, |
| 53 | + getBackgroundTaskRegistry: () => new BackgroundTaskRegistry(), |
| 54 | + getApprovalMode: () => 'default' as ApprovalMode, |
| 55 | + } as unknown as Config; |
| 56 | +} |
| 57 | + |
| 58 | +describe('SendMessageTool — broadcast delivery outcomes (#10072)', () => { |
| 59 | + let harness: TeamCoordinationHarness | undefined; |
| 60 | + |
| 61 | + afterEach(async () => { |
| 62 | + await harness?.cleanup(); |
| 63 | + harness = undefined; |
| 64 | + }); |
| 65 | + |
| 66 | + async function createHarness(): Promise<TeamCoordinationHarness> { |
| 67 | + const h = await TeamCoordinationHarness.create(); |
| 68 | + setMockDir(h.tmpDir); |
| 69 | + harness = h; |
| 70 | + return h; |
| 71 | + } |
| 72 | + |
| 73 | + function broadcastInvocation(h: TeamCoordinationHarness) { |
| 74 | + const tool = new SendMessageTool(makeConfig(h.teamManager)); |
| 75 | + return tool.build({ to: '*', message: 'sync for everyone' }); |
| 76 | + } |
| 77 | + |
| 78 | + it('does not claim complete success when a delivery is rejected', async () => { |
| 79 | + const h = await createHarness(); |
| 80 | + await h.spawnTeammate('alice'); |
| 81 | + const bob = await h.spawnTeammate('bob'); |
| 82 | + |
| 83 | + // bob terminates between the member snapshot and the send: its |
| 84 | + // queue is dropped, so its delivery rejects while alice's lands. |
| 85 | + await bob.shutdown(); |
| 86 | + |
| 87 | + const result = await broadcastInvocation(h).execute( |
| 88 | + new AbortController().signal, |
| 89 | + ); |
| 90 | + |
| 91 | + expect(result.error).toBeUndefined(); |
| 92 | + // Must not claim that every teammate received the message… |
| 93 | + expect(result.llmContent).not.toBe('Message broadcast to all teammates.'); |
| 94 | + // …and must name the recipient that was not reached. |
| 95 | + expect(String(result.llmContent)).toContain('bob'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('still reports complete success when every delivery lands', async () => { |
| 99 | + const h = await createHarness(); |
| 100 | + const alice = await h.spawnTeammate('alice'); |
| 101 | + await h.spawnTeammate('bob'); |
| 102 | + |
| 103 | + const result = await broadcastInvocation(h).execute( |
| 104 | + new AbortController().signal, |
| 105 | + ); |
| 106 | + |
| 107 | + expect(result.error).toBeUndefined(); |
| 108 | + expect(result.llmContent).toBe('Message broadcast to all teammates.'); |
| 109 | + await h.waitForMessages('alice', 1); |
| 110 | + await h.waitForMessages('bob', 1); |
| 111 | + expect(alice.getReceivedMessages()).toHaveLength(1); |
| 112 | + }); |
| 113 | + |
| 114 | + it('reports failure when no delivery lands', async () => { |
| 115 | + const h = await createHarness(); |
| 116 | + const alice = await h.spawnTeammate('alice'); |
| 117 | + const bob = await h.spawnTeammate('bob'); |
| 118 | + await alice.shutdown(); |
| 119 | + await bob.shutdown(); |
| 120 | + |
| 121 | + const result = await broadcastInvocation(h).execute( |
| 122 | + new AbortController().signal, |
| 123 | + ); |
| 124 | + |
| 125 | + expect(result.error).toBeDefined(); |
| 126 | + expect(result.llmContent).not.toBe('Message broadcast to all teammates.'); |
| 127 | + expect(String(result.llmContent)).toContain('alice'); |
| 128 | + expect(String(result.llmContent)).toContain('bob'); |
| 129 | + }); |
| 130 | +}); |
0 commit comments