Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
import { describe, it, expect, vi, beforeEach } from "vitest";
const mockGetWakeMessageScopeById = vi.fn();
vi.mock("../../src/db/queries/community/message", () => ({
getWakeMessageScopeById: (...a: unknown[]) => mockGetWakeMessageScopeById(...a),
}));
const mockGetBotWakeContext = vi.fn();
vi.mock("../../src/db/queries/community/bot", () => ({
getBotWakeContext: (...a: unknown[]) => mockGetBotWakeContext(...a),
}));
const mockHasMentionForMessage = vi.fn();
vi.mock("../../src/db/queries/community/mention", () => ({
hasMentionForMessage: (...a: unknown[]) => mockHasMentionForMessage(...a),
}));
const mockInsertBotAuditWakeTrigger = vi.fn();
vi.mock("../../src/db/queries/community/bot-audit-log", () => ({
insertBotAuditWakeTrigger: (...a: unknown[]) => mockInsertBotAuditWakeTrigger(...a),
}));
const mockGetUsersByIds = vi.fn();
vi.mock("../../src/db/queries/user", () => ({
getUsersByIds: (...a: unknown[]) => mockGetUsersByIds(...a),
}));
const mockCanBotReadWakeScope = vi.fn();
vi.mock("../../src/db/queries/community/member", () => ({
canBotReadWakeScope: (...a: unknown[]) => mockCanBotReadWakeScope(...a),
}));
const mockResolveNotificationEligibilityForUsers = vi.fn();
vi.mock("../../src/db/queries/community/notification-eligibility", () => ({
resolveNotificationEligibilityForUsers: (...a: unknown[]) =>
mockResolveNotificationEligibilityForUsers(...a),
}));
const mockResolveUnreadNoticeChannel = vi.fn();
vi.mock("../../src/db/queries/community/agent-inbox", () => ({
resolveUnreadNoticeChannel: (...a: unknown[]) => mockResolveUnreadNoticeChannel(...a),
}));
import { dispatchOneUnreadWake } from "../../src/community/wake-dispatch";
import type { Database } from "../../src/db/index";
const fakeDb = {} as Database;
const MESSAGE_CHANNEL = {
id: "msg_1",
seq: 7,
authorId: "u_human",
channelId: "ch_1",
dmConversationId: null,
};
const BOT_READY = {
state: "ready" as const,
botUserId: "bot_1",
name: "zoe",
discriminator: "0042",
machineId: "machine_1",
runtime: "claude",
ownerUserId: "owner_1",
};
function seedReady() {
mockGetWakeMessageScopeById.mockResolvedValue(MESSAGE_CHANNEL);
mockGetBotWakeContext.mockResolvedValue(BOT_READY);
mockCanBotReadWakeScope.mockResolvedValue(true);
mockResolveNotificationEligibilityForUsers.mockResolvedValue(new Map([
["bot_1", { currentLevel: "all", hasAttention: false, isUnread: true, isReadable: true }],
]));
mockResolveUnreadNoticeChannel.mockResolvedValue("/srv_1/general");
mockGetUsersByIds.mockResolvedValue([{ id: "u_human", name: "gustavo", discriminator: "0042" }]);
mockHasMentionForMessage.mockResolvedValue(false);
mockInsertBotAuditWakeTrigger.mockResolvedValue({ id: "evt_1", createdAt: "2026-07-23T00:00:00.000Z" });
}
function makeEnv(fetchImpl: (url: string, init?: RequestInit) => Promise<Response>) {
return { WS_DO_WORKER: { fetch: vi.fn(fetchImpl) } as unknown as Fetcher };
}
/**
* `dispatchOneUnreadWake` is the ONE per-candidate function both
* `src/queue-worker`'s real queue consumer and `src/web`'s dev-only inline
* stand-in call — these tests exercise it through its real
* `buildUnreadWakeCommand`/`sendWakeToMachine` wiring end-to-end (query
* modules mocked, `fetch` mocked), so neither caller needs its own
* "what does a wake candidate resolve to" test coverage.
*/
describe("dispatchOneUnreadWake", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("resolves to { outcome: 'attempted' } when a socket write is attempted", async () => {
seedReady();
const env = makeEnv(async () => new Response(JSON.stringify({ attempted: 1 }), { status: 200 }));
const result = await dispatchOneUnreadWake(fakeDb, env, { messageId: "msg_1", botUserId: "bot_1" });
expect(result).toEqual({ outcome: "attempted" });
});
it("resolves to { outcome: 'attempted_nowhere', machineId } when no socket write is attempted", async () => {
seedReady();
const env = makeEnv(async () => new Response(JSON.stringify({ attempted: 0 }), { status: 200 }));
const result = await dispatchOneUnreadWake(fakeDb, env, { messageId: "msg_1", botUserId: "bot_1" });
expect(result).toEqual({ outcome: "attempted_nowhere", machineId: "machine_1" });
});
it("resolves to { outcome: 'skip', reason } without ever calling fetch when the rebuild is a skip", async () => {
mockGetWakeMessageScopeById.mockResolvedValue(null);
const fetchMock = vi.fn();
const env = makeEnv(fetchMock);
const result = await dispatchOneUnreadWake(fakeDb, env, { messageId: "msg_gone", botUserId: "bot_1" });
expect(result).toEqual({ outcome: "skip", reason: "message_missing" });
expect(fetchMock).not.toHaveBeenCalled();
});
it("propagates a throw from buildUnreadWakeCommand (caller decides retry vs log)", async () => {
mockGetWakeMessageScopeById.mockRejectedValue(new Error("D1_ERROR: query failed"));
const env = makeEnv(async () => new Response("{}", { status: 200 }));
await expect(
dispatchOneUnreadWake(fakeDb, env, { messageId: "msg_1", botUserId: "bot_1" }),
).rejects.toThrow("D1_ERROR");
});
it("propagates a throw from sendWakeToMachine (transient ws-do failure)", async () => {
seedReady();
const env = makeEnv(async () => new Response("boom", { status: 500 }));
await expect(
dispatchOneUnreadWake(fakeDb, env, { messageId: "msg_1", botUserId: "bot_1" }),
).rejects.toThrow();
});
});