|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const sendMocks = vi.hoisted(() => ({ |
| 4 | + sendTypingSignal: vi.fn(), |
| 5 | + sendMessageSignal: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock("./send.js", () => ({ |
| 9 | + sendTypingSignal: sendMocks.sendTypingSignal, |
| 10 | + sendMessageSignal: sendMocks.sendMessageSignal, |
| 11 | +})); |
| 12 | + |
| 13 | +const { signalApprovalNativeRuntime } = await import("./approval-handler.runtime.js"); |
| 14 | + |
| 15 | +describe("Signal approval native runtime", () => { |
| 16 | + beforeEach(() => { |
| 17 | + sendMocks.sendTypingSignal.mockReset().mockResolvedValue(true); |
| 18 | + sendMocks.sendMessageSignal.mockReset().mockResolvedValue({ |
| 19 | + messageId: "1700000000000", |
| 20 | + timestamp: 1700000000000, |
| 21 | + receipt: { parts: [] }, |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("uses the live Signal RPC context when delivering approval prompts", async () => { |
| 26 | + const prepared = await signalApprovalNativeRuntime.transport.prepareTarget({ |
| 27 | + plannedTarget: { target: { to: "+15551230000" } }, |
| 28 | + accountId: "default", |
| 29 | + context: { baseUrl: "http://127.0.0.1:18080", account: "+15550001111" }, |
| 30 | + } as never); |
| 31 | + |
| 32 | + expect(prepared?.target).toMatchObject({ |
| 33 | + to: "+15551230000", |
| 34 | + accountId: "default", |
| 35 | + baseUrl: "http://127.0.0.1:18080", |
| 36 | + account: "+15550001111", |
| 37 | + }); |
| 38 | + |
| 39 | + await signalApprovalNativeRuntime.transport.deliverPending({ |
| 40 | + cfg: {}, |
| 41 | + preparedTarget: prepared!.target, |
| 42 | + pendingPayload: { text: "approval", allowedDecisions: ["allow-once"] }, |
| 43 | + } as never); |
| 44 | + |
| 45 | + expect(sendMocks.sendTypingSignal).toHaveBeenCalledWith("+15551230000", { |
| 46 | + cfg: {}, |
| 47 | + accountId: "default", |
| 48 | + baseUrl: "http://127.0.0.1:18080", |
| 49 | + account: "+15550001111", |
| 50 | + }); |
| 51 | + expect(sendMocks.sendMessageSignal).toHaveBeenCalledWith("+15551230000", "approval", { |
| 52 | + cfg: {}, |
| 53 | + accountId: "default", |
| 54 | + baseUrl: "http://127.0.0.1:18080", |
| 55 | + account: "+15550001111", |
| 56 | + textMode: "plain", |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("only renders reaction hints when the Signal target author can be bound", async () => { |
| 61 | + const cfg = { channels: { signal: { allowFrom: ["+15551230000"] } } }; |
| 62 | + const unbound = await signalApprovalNativeRuntime.transport.prepareTarget({ |
| 63 | + plannedTarget: { target: { to: "+15551230000" } }, |
| 64 | + accountId: "default", |
| 65 | + context: { baseUrl: "http://127.0.0.1:18080" }, |
| 66 | + } as never); |
| 67 | + |
| 68 | + await signalApprovalNativeRuntime.transport.deliverPending({ |
| 69 | + cfg, |
| 70 | + preparedTarget: unbound!.target, |
| 71 | + pendingPayload: { |
| 72 | + text: "Exec approval required\nID: exec-1\n\nReply with: /approve exec-1 allow-once|deny", |
| 73 | + allowedDecisions: ["allow-once", "deny"], |
| 74 | + }, |
| 75 | + } as never); |
| 76 | + |
| 77 | + expect(sendMocks.sendMessageSignal).toHaveBeenLastCalledWith( |
| 78 | + "+15551230000", |
| 79 | + expect.not.stringContaining("React with:"), |
| 80 | + expect.any(Object), |
| 81 | + ); |
| 82 | + |
| 83 | + const bound = await signalApprovalNativeRuntime.transport.prepareTarget({ |
| 84 | + plannedTarget: { target: { to: "+15551230000" } }, |
| 85 | + accountId: "default", |
| 86 | + context: { baseUrl: "http://127.0.0.1:18080", account: "+15550001111" }, |
| 87 | + } as never); |
| 88 | + |
| 89 | + await signalApprovalNativeRuntime.transport.deliverPending({ |
| 90 | + cfg, |
| 91 | + preparedTarget: bound!.target, |
| 92 | + pendingPayload: { |
| 93 | + text: "Exec approval required\nID: exec-1\n\nReply with: /approve exec-1 allow-once|deny", |
| 94 | + allowedDecisions: ["allow-once", "deny"], |
| 95 | + }, |
| 96 | + } as never); |
| 97 | + |
| 98 | + expect(sendMocks.sendMessageSignal).toHaveBeenLastCalledWith( |
| 99 | + "+15551230000", |
| 100 | + expect.stringContaining("React with:\n\n👍 Allow Once\n👎 Deny"), |
| 101 | + expect.any(Object), |
| 102 | + ); |
| 103 | + }); |
| 104 | +}); |
0 commit comments