|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + resolveSessionAgentId: vi.fn(() => "agent-from-key"), |
| 5 | + consumeRestartSentinel: vi.fn(async () => ({ |
| 6 | + payload: { |
| 7 | + sessionKey: "agent:main:main", |
| 8 | + deliveryContext: { |
| 9 | + channel: "whatsapp", |
| 10 | + to: "+15550002", |
| 11 | + accountId: "acct-2", |
| 12 | + }, |
| 13 | + }, |
| 14 | + })), |
| 15 | + formatRestartSentinelMessage: vi.fn(() => "restart message"), |
| 16 | + summarizeRestartSentinel: vi.fn(() => "restart summary"), |
| 17 | + resolveMainSessionKeyFromConfig: vi.fn(() => "agent:main:main"), |
| 18 | + parseSessionThreadInfo: vi.fn(() => ({ baseSessionKey: null, threadId: undefined })), |
| 19 | + loadSessionEntry: vi.fn(() => ({ cfg: {}, entry: {} })), |
| 20 | + resolveAnnounceTargetFromKey: vi.fn(() => null), |
| 21 | + deliveryContextFromSession: vi.fn(() => undefined), |
| 22 | + mergeDeliveryContext: vi.fn((a?: Record<string, unknown>, b?: Record<string, unknown>) => ({ |
| 23 | + ...b, |
| 24 | + ...a, |
| 25 | + })), |
| 26 | + normalizeChannelId: vi.fn((channel: string) => channel), |
| 27 | + resolveOutboundTarget: vi.fn(() => ({ ok: true as const, to: "+15550002" })), |
| 28 | + deliverOutboundPayloads: vi.fn(async () => []), |
| 29 | + enqueueSystemEvent: vi.fn(), |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock("../agents/agent-scope.js", () => ({ |
| 33 | + resolveSessionAgentId: mocks.resolveSessionAgentId, |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock("../infra/restart-sentinel.js", () => ({ |
| 37 | + consumeRestartSentinel: mocks.consumeRestartSentinel, |
| 38 | + formatRestartSentinelMessage: mocks.formatRestartSentinelMessage, |
| 39 | + summarizeRestartSentinel: mocks.summarizeRestartSentinel, |
| 40 | +})); |
| 41 | + |
| 42 | +vi.mock("../config/sessions.js", () => ({ |
| 43 | + resolveMainSessionKeyFromConfig: mocks.resolveMainSessionKeyFromConfig, |
| 44 | +})); |
| 45 | + |
| 46 | +vi.mock("../config/sessions/delivery-info.js", () => ({ |
| 47 | + parseSessionThreadInfo: mocks.parseSessionThreadInfo, |
| 48 | +})); |
| 49 | + |
| 50 | +vi.mock("./session-utils.js", () => ({ |
| 51 | + loadSessionEntry: mocks.loadSessionEntry, |
| 52 | +})); |
| 53 | + |
| 54 | +vi.mock("../agents/tools/sessions-send-helpers.js", () => ({ |
| 55 | + resolveAnnounceTargetFromKey: mocks.resolveAnnounceTargetFromKey, |
| 56 | +})); |
| 57 | + |
| 58 | +vi.mock("../utils/delivery-context.js", () => ({ |
| 59 | + deliveryContextFromSession: mocks.deliveryContextFromSession, |
| 60 | + mergeDeliveryContext: mocks.mergeDeliveryContext, |
| 61 | +})); |
| 62 | + |
| 63 | +vi.mock("../channels/plugins/index.js", () => ({ |
| 64 | + normalizeChannelId: mocks.normalizeChannelId, |
| 65 | +})); |
| 66 | + |
| 67 | +vi.mock("../infra/outbound/targets.js", () => ({ |
| 68 | + resolveOutboundTarget: mocks.resolveOutboundTarget, |
| 69 | +})); |
| 70 | + |
| 71 | +vi.mock("../infra/outbound/deliver.js", () => ({ |
| 72 | + deliverOutboundPayloads: mocks.deliverOutboundPayloads, |
| 73 | +})); |
| 74 | + |
| 75 | +vi.mock("../infra/system-events.js", () => ({ |
| 76 | + enqueueSystemEvent: mocks.enqueueSystemEvent, |
| 77 | +})); |
| 78 | + |
| 79 | +const { scheduleRestartSentinelWake } = await import("./server-restart-sentinel.js"); |
| 80 | + |
| 81 | +describe("scheduleRestartSentinelWake", () => { |
| 82 | + it("forwards session context to outbound delivery", async () => { |
| 83 | + await scheduleRestartSentinelWake({ deps: {} as never }); |
| 84 | + |
| 85 | + expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith( |
| 86 | + expect.objectContaining({ |
| 87 | + channel: "whatsapp", |
| 88 | + to: "+15550002", |
| 89 | + session: { key: "agent:main:main", agentId: "agent-from-key" }, |
| 90 | + }), |
| 91 | + ); |
| 92 | + expect(mocks.enqueueSystemEvent).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments