|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { html, nothing, render, type LitElement } from "lit"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import type { ExecApprovalRequest } from "../app/exec-approval.ts"; |
| 6 | +import { i18n } from "../i18n/index.ts"; |
| 7 | +import { getRenderedModalDialog, installDialogPolyfill } from "../test-helpers/modal-dialog.ts"; |
| 8 | +import "./exec-approval.ts"; |
| 9 | + |
| 10 | +let container: HTMLDivElement; |
| 11 | +let restoreDialogPolyfill: () => void; |
| 12 | + |
| 13 | +function createExecRequest(overrides: Partial<ExecApprovalRequest> = {}): ExecApprovalRequest { |
| 14 | + return { |
| 15 | + id: "approval-1", |
| 16 | + kind: "exec", |
| 17 | + request: { |
| 18 | + command: "echo hello", |
| 19 | + ask: "on-request", |
| 20 | + }, |
| 21 | + createdAtMs: Date.now() - 1_000, |
| 22 | + expiresAtMs: Date.now() + 60_000, |
| 23 | + ...overrides, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +async function renderApproval(request: ExecApprovalRequest) { |
| 28 | + render( |
| 29 | + html`<openclaw-exec-approval |
| 30 | + .props=${{ |
| 31 | + queue: [request], |
| 32 | + busy: false, |
| 33 | + error: null, |
| 34 | + onDecision: vi.fn(), |
| 35 | + }} |
| 36 | + ></openclaw-exec-approval>`, |
| 37 | + container, |
| 38 | + ); |
| 39 | + const approval = container.querySelector<LitElement>("openclaw-exec-approval"); |
| 40 | + if (!approval) { |
| 41 | + throw new Error("Expected exec approval"); |
| 42 | + } |
| 43 | + await approval.updateComplete; |
| 44 | +} |
| 45 | + |
| 46 | +describe("openclaw-exec-approval", () => { |
| 47 | + beforeEach(async () => { |
| 48 | + restoreDialogPolyfill = installDialogPolyfill(); |
| 49 | + await i18n.setLocale("en"); |
| 50 | + container = document.createElement("div"); |
| 51 | + document.body.append(container); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(async () => { |
| 55 | + render(nothing, container); |
| 56 | + container.remove(); |
| 57 | + await i18n.setLocale("en"); |
| 58 | + restoreDialogPolyfill(); |
| 59 | + vi.restoreAllMocks(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("uses neutral unavailable copy for exec allow-always decisions", async () => { |
| 63 | + await renderApproval( |
| 64 | + createExecRequest({ |
| 65 | + request: { |
| 66 | + command: "echo hello", |
| 67 | + ask: "always", |
| 68 | + allowedDecisions: ["allow-once", "deny"], |
| 69 | + }, |
| 70 | + }), |
| 71 | + ); |
| 72 | + |
| 73 | + await getRenderedModalDialog(container); |
| 74 | + |
| 75 | + expect( |
| 76 | + Array.from(container.querySelectorAll(".exec-approval-actions button")).map((button) => |
| 77 | + button.textContent?.trim(), |
| 78 | + ), |
| 79 | + ).toEqual(["Allow once", "Deny"]); |
| 80 | + expect(container.querySelector(".exec-approval-warning")?.textContent?.trim()).toBe( |
| 81 | + "Allow Always is unavailable for this command.", |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it("does not show exec unavailable copy for restricted plugin approvals", async () => { |
| 86 | + await renderApproval( |
| 87 | + createExecRequest({ |
| 88 | + id: "plugin-approval-1", |
| 89 | + kind: "plugin", |
| 90 | + request: { |
| 91 | + command: "Plugin approval", |
| 92 | + allowedDecisions: ["allow-once", "deny"], |
| 93 | + }, |
| 94 | + pluginTitle: "Plugin approval", |
| 95 | + }), |
| 96 | + ); |
| 97 | + |
| 98 | + await getRenderedModalDialog(container); |
| 99 | + |
| 100 | + expect( |
| 101 | + Array.from(container.querySelectorAll(".exec-approval-actions button")).map((button) => |
| 102 | + button.textContent?.trim(), |
| 103 | + ), |
| 104 | + ).toEqual(["Allow once", "Deny"]); |
| 105 | + expect(container.querySelector(".exec-approval-warning")).toBeNull(); |
| 106 | + }); |
| 107 | +}); |
0 commit comments