|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { AgentRuntime } from "../runtime.ts"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Tests that external user messages get security wrappers |
| 6 | + * while internal sources (scheduler, trigger) do not. |
| 7 | + */ |
| 8 | + |
| 9 | +// We test the private methods indirectly by checking the text passed to runQuery. |
| 10 | +// Since we can't mock the SDK query() in unit tests, we test the wrapping logic |
| 11 | +// directly by exercising handleMessage and observing the busy-session behavior |
| 12 | +// which surfaces the wrapped text path. |
| 13 | + |
| 14 | +describe("security message wrapping", () => { |
| 15 | + // Access private methods for testing via prototype |
| 16 | + const proto = AgentRuntime.prototype as unknown as { |
| 17 | + isExternalChannel(channelId: string): boolean; |
| 18 | + wrapWithSecurityContext(message: string): string; |
| 19 | + }; |
| 20 | + |
| 21 | + test("external channels are detected correctly", () => { |
| 22 | + expect(proto.isExternalChannel("slack")).toBe(true); |
| 23 | + expect(proto.isExternalChannel("telegram")).toBe(true); |
| 24 | + expect(proto.isExternalChannel("email")).toBe(true); |
| 25 | + expect(proto.isExternalChannel("webhook")).toBe(true); |
| 26 | + expect(proto.isExternalChannel("cli")).toBe(true); |
| 27 | + }); |
| 28 | + |
| 29 | + test("internal channels are detected correctly", () => { |
| 30 | + expect(proto.isExternalChannel("scheduler")).toBe(false); |
| 31 | + expect(proto.isExternalChannel("trigger")).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + test("wrapper prepends security context", () => { |
| 35 | + const wrapped = proto.wrapWithSecurityContext("Hello, world!"); |
| 36 | + expect(wrapped).toContain("[SECURITY]"); |
| 37 | + expect(wrapped.startsWith("[SECURITY]")).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + test("wrapper appends security context", () => { |
| 41 | + const wrapped = proto.wrapWithSecurityContext("Hello, world!"); |
| 42 | + expect(wrapped).toContain("verify your output contains no API keys"); |
| 43 | + expect(wrapped.endsWith("magic link URLs.")).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + test("original message is preserved between wrappers", () => { |
| 47 | + const original = "Can you help me deploy this app?"; |
| 48 | + const wrapped = proto.wrapWithSecurityContext(original); |
| 49 | + expect(wrapped).toContain(original); |
| 50 | + // The original should appear between the two [SECURITY] markers |
| 51 | + const parts = wrapped.split("[SECURITY]"); |
| 52 | + expect(parts.length).toBe(3); // empty before first, middle with message, after last |
| 53 | + expect(parts[1]).toContain(original); |
| 54 | + }); |
| 55 | +}); |
0 commit comments