|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import test from "node:test" |
| 3 | +import type { PluginConfig } from "../lib/config" |
| 4 | +import { |
| 5 | + createChatMessageHandler, |
| 6 | + createChatMessageTransformHandler, |
| 7 | + createCommandExecuteHandler, |
| 8 | + createTextCompleteHandler, |
| 9 | +} from "../lib/hooks" |
| 10 | +import { Logger } from "../lib/logger" |
| 11 | +import { createSessionState, type WithParts } from "../lib/state" |
| 12 | + |
| 13 | +function buildConfig(permission: "allow" | "ask" | "deny" = "allow"): PluginConfig { |
| 14 | + return { |
| 15 | + enabled: true, |
| 16 | + debug: false, |
| 17 | + pruneNotification: "off", |
| 18 | + pruneNotificationType: "chat", |
| 19 | + commands: { |
| 20 | + enabled: true, |
| 21 | + protectedTools: [], |
| 22 | + }, |
| 23 | + manualMode: { |
| 24 | + enabled: false, |
| 25 | + automaticStrategies: true, |
| 26 | + }, |
| 27 | + turnProtection: { |
| 28 | + enabled: false, |
| 29 | + turns: 4, |
| 30 | + }, |
| 31 | + experimental: { |
| 32 | + allowSubAgents: false, |
| 33 | + customPrompts: false, |
| 34 | + }, |
| 35 | + protectedFilePatterns: [], |
| 36 | + compress: { |
| 37 | + mode: "message", |
| 38 | + permission, |
| 39 | + showCompression: false, |
| 40 | + maxContextLimit: 150000, |
| 41 | + minContextLimit: 50000, |
| 42 | + nudgeFrequency: 5, |
| 43 | + iterationNudgeThreshold: 15, |
| 44 | + nudgeForce: "soft", |
| 45 | + protectedTools: ["task"], |
| 46 | + protectUserMessages: false, |
| 47 | + }, |
| 48 | + strategies: { |
| 49 | + deduplication: { |
| 50 | + enabled: true, |
| 51 | + protectedTools: [], |
| 52 | + }, |
| 53 | + purgeErrors: { |
| 54 | + enabled: true, |
| 55 | + turns: 4, |
| 56 | + protectedTools: [], |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function buildMessage(id: string, role: "user" | "assistant", text: string): WithParts { |
| 63 | + return { |
| 64 | + info: { |
| 65 | + id, |
| 66 | + role, |
| 67 | + sessionID: "session-1", |
| 68 | + agent: "assistant", |
| 69 | + time: { created: 1 }, |
| 70 | + } as WithParts["info"], |
| 71 | + parts: [ |
| 72 | + { |
| 73 | + id: `${id}-part`, |
| 74 | + messageID: id, |
| 75 | + sessionID: "session-1", |
| 76 | + type: "text", |
| 77 | + text, |
| 78 | + }, |
| 79 | + ], |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +test("chat message transform strips hallucinated tags even when compress is denied", async () => { |
| 84 | + const state = createSessionState() |
| 85 | + const logger = new Logger(false) |
| 86 | + const config = buildConfig("deny") |
| 87 | + const handler = createChatMessageTransformHandler( |
| 88 | + { session: { get: async () => ({}) } } as any, |
| 89 | + state, |
| 90 | + logger, |
| 91 | + config, |
| 92 | + { |
| 93 | + reload() {}, |
| 94 | + getRuntimePrompts() { |
| 95 | + return {} as any |
| 96 | + }, |
| 97 | + } as any, |
| 98 | + { global: undefined, agents: {} }, |
| 99 | + ) |
| 100 | + const output = { |
| 101 | + messages: [buildMessage("assistant-1", "assistant", "alpha <dcp>beta</dcp> omega")], |
| 102 | + } |
| 103 | + |
| 104 | + await handler({}, output) |
| 105 | + |
| 106 | + assert.equal(output.messages[0]?.parts[0]?.type, "text") |
| 107 | + assert.equal((output.messages[0]?.parts[0] as any).text, "alpha omega") |
| 108 | +}) |
| 109 | + |
| 110 | +test("command execute exits after effective permission resolves to deny", async () => { |
| 111 | + let sessionMessagesCalls = 0 |
| 112 | + const output = { parts: [] as any[] } |
| 113 | + const handler = createCommandExecuteHandler( |
| 114 | + { |
| 115 | + session: { |
| 116 | + messages: async () => { |
| 117 | + sessionMessagesCalls += 1 |
| 118 | + return { data: [] } |
| 119 | + }, |
| 120 | + }, |
| 121 | + } as any, |
| 122 | + createSessionState(), |
| 123 | + new Logger(false), |
| 124 | + buildConfig("deny"), |
| 125 | + "/tmp", |
| 126 | + { global: undefined, agents: {} }, |
| 127 | + ) |
| 128 | + |
| 129 | + await handler({ command: "dcp", sessionID: "session-1", arguments: "context" }, output) |
| 130 | + |
| 131 | + assert.equal(sessionMessagesCalls, 1) |
| 132 | + assert.deepEqual(output.parts, []) |
| 133 | +}) |
| 134 | + |
| 135 | +test("chat message hook caches variant even when effective permission is denied", async () => { |
| 136 | + const state = createSessionState() |
| 137 | + const handler = createChatMessageHandler(state, new Logger(false), buildConfig("allow"), { |
| 138 | + global: { "*": "deny" }, |
| 139 | + agents: {}, |
| 140 | + }) |
| 141 | + |
| 142 | + await handler({ sessionID: "session-1", variant: "danger", agent: "assistant" }, {}) |
| 143 | + |
| 144 | + assert.equal(state.variant, "danger") |
| 145 | +}) |
| 146 | + |
| 147 | +test("text complete strips hallucinated metadata tags", async () => { |
| 148 | + const output = { text: "alpha <dcp>beta</dcp> omega" } |
| 149 | + const handler = createTextCompleteHandler() |
| 150 | + |
| 151 | + await handler({ sessionID: "session-1", messageID: "message-1", partID: "part-1" }, output) |
| 152 | + |
| 153 | + assert.equal(output.text, "alpha omega") |
| 154 | +}) |
0 commit comments