|
| 1 | +// Qqbot tests cover native approval presentation behavior. |
| 2 | +import type { |
| 3 | + ExecApprovalPendingView, |
| 4 | + PluginApprovalPendingView, |
| 5 | +} from "openclaw/plugin-sdk/approval-handler-runtime"; |
| 6 | +import { resolveExecApprovalCommandDisplay } from "openclaw/plugin-sdk/approval-runtime"; |
| 7 | +import { describe, expect, it } from "vitest"; |
| 8 | +import type { InlineKeyboard } from "../../engine/types.js"; |
| 9 | +import { qqbotApprovalNativeRuntime } from "./handler-runtime.js"; |
| 10 | + |
| 11 | +type QQBotPendingPayload = { |
| 12 | + text: string; |
| 13 | + keyboard: InlineKeyboard; |
| 14 | +}; |
| 15 | + |
| 16 | +function createExecView(commandText: string): ExecApprovalPendingView { |
| 17 | + return { |
| 18 | + approvalId: "approval-1", |
| 19 | + approvalKind: "exec", |
| 20 | + phase: "pending", |
| 21 | + title: "Exec Approval Required", |
| 22 | + metadata: [], |
| 23 | + commandText, |
| 24 | + commandPreview: "short preview", |
| 25 | + actions: [ |
| 26 | + { |
| 27 | + decision: "allow-once", |
| 28 | + label: "Allow Once", |
| 29 | + command: "/approve approval-1 allow-once", |
| 30 | + style: "success", |
| 31 | + }, |
| 32 | + { |
| 33 | + decision: "deny", |
| 34 | + label: "Deny", |
| 35 | + command: "/approve approval-1 deny", |
| 36 | + style: "danger", |
| 37 | + }, |
| 38 | + ], |
| 39 | + expiresAtMs: Date.now() + 60_000, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +function createPluginView(expiresAtMs: number): PluginApprovalPendingView { |
| 44 | + return { |
| 45 | + approvalId: "plugin:approval-1", |
| 46 | + approvalKind: "plugin", |
| 47 | + phase: "pending", |
| 48 | + title: "Install plugin", |
| 49 | + description: "Approve the requested plugin", |
| 50 | + metadata: [], |
| 51 | + pluginId: "example-plugin", |
| 52 | + toolName: "plugin.install", |
| 53 | + agentId: "main", |
| 54 | + severity: "critical", |
| 55 | + actions: [ |
| 56 | + { |
| 57 | + decision: "allow-once", |
| 58 | + label: "Allow Once", |
| 59 | + command: "/approve plugin:approval-1 allow-once", |
| 60 | + style: "success", |
| 61 | + }, |
| 62 | + { |
| 63 | + decision: "deny", |
| 64 | + label: "Deny", |
| 65 | + command: "/approve plugin:approval-1 deny", |
| 66 | + style: "danger", |
| 67 | + }, |
| 68 | + ], |
| 69 | + expiresAtMs, |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +describe("qqbotApprovalNativeRuntime", () => { |
| 74 | + it("renders the sanitized primary command with callback buttons", async () => { |
| 75 | + const secret = `ghp_${"a".repeat(36)}`; |
| 76 | + const rawCommand = `printf '${secret}\u200b'\n你好😀`; |
| 77 | + const commandText = resolveExecApprovalCommandDisplay({ command: rawCommand }).commandText; |
| 78 | + const view = createExecView(commandText); |
| 79 | + view.cwd = "/tmp\n"; |
| 80 | + view.agentId = "agent```fake"; |
| 81 | + const payload = (await qqbotApprovalNativeRuntime.presentation.buildPendingPayload({ |
| 82 | + cfg: {} as never, |
| 83 | + accountId: "default", |
| 84 | + context: {}, |
| 85 | + request: { |
| 86 | + id: "approval-1", |
| 87 | + request: { command: rawCommand, commandPreview: "short preview" }, |
| 88 | + createdAtMs: Date.now(), |
| 89 | + expiresAtMs: view.expiresAtMs, |
| 90 | + }, |
| 91 | + approvalKind: "exec", |
| 92 | + nowMs: Date.now(), |
| 93 | + view, |
| 94 | + })) as QQBotPendingPayload; |
| 95 | + |
| 96 | + expect(commandText).not.toContain(secret); |
| 97 | + expect(commandText).toContain("\\u{200B}"); |
| 98 | + expect(commandText).toContain("\\u{A}"); |
| 99 | + expect(commandText).toContain("你好😀"); |
| 100 | + expect(payload.text.replace(/[↩\n]/g, "")).toContain(commandText); |
| 101 | + expect(payload.text).not.toContain(secret); |
| 102 | + expect(payload.text).not.toContain("short preview"); |
| 103 | + expect(payload.text).not.toContain("/tmp\n![fake]"); |
| 104 | + expect(payload.text).toContain("📁 目录:\n```\n/tmp\\u{A}\n```"); |
| 105 | + expect(payload.text).toContain("🤖 Agent:\n````\nagent```fake\n````"); |
| 106 | + expect(payload.keyboard.content.rows[0]?.buttons.map((button) => button.action.data)).toEqual([ |
| 107 | + "approve:approval-1:allow-once", |
| 108 | + "approve:approval-1:deny", |
| 109 | + ]); |
| 110 | + }); |
| 111 | + |
| 112 | + it("renders a plugin approval's actual remaining lifetime", async () => { |
| 113 | + const nowMs = 1_000_000; |
| 114 | + const view = createPluginView(nowMs + 600_000); |
| 115 | + const payload = (await qqbotApprovalNativeRuntime.presentation.buildPendingPayload({ |
| 116 | + cfg: {} as never, |
| 117 | + accountId: "default", |
| 118 | + context: {}, |
| 119 | + request: { |
| 120 | + id: view.approvalId, |
| 121 | + request: { |
| 122 | + title: "stale raw title", |
| 123 | + description: "stale raw description", |
| 124 | + severity: "info", |
| 125 | + }, |
| 126 | + createdAtMs: nowMs, |
| 127 | + expiresAtMs: view.expiresAtMs, |
| 128 | + }, |
| 129 | + approvalKind: "plugin", |
| 130 | + nowMs, |
| 131 | + view, |
| 132 | + })) as QQBotPendingPayload; |
| 133 | + |
| 134 | + expect(payload.text).toContain("🔴 审批请求"); |
| 135 | + expect(payload.text).toContain("📋 Install plugin"); |
| 136 | + expect(payload.text).toContain("📝 Approve the requested plugin"); |
| 137 | + expect(payload.text).not.toContain("stale raw"); |
| 138 | + expect(payload.text).toContain("⏱️ 超时: 600 秒"); |
| 139 | + expect(payload.keyboard.content.rows[0]?.buttons.map((button) => button.action.data)).toEqual([ |
| 140 | + "approve:plugin:approval-1:allow-once", |
| 141 | + "approve:plugin:approval-1:deny", |
| 142 | + ]); |
| 143 | + }); |
| 144 | +}); |
0 commit comments