|
| 1 | +// codex-permission-alias.test.ts — TDD for the Codex permission dashboard alias fix. |
| 2 | +// |
| 3 | +// Bug: Codex emits `pilot.permission.pending` / `pilot.permission.resolved` but |
| 4 | +// the dashboard only handled `permission.requested` / `permission.resolved`. |
| 5 | +// Fix: add named constants for the pilot event types and handle them as aliases |
| 6 | +// in the SSE handleEvent switch. |
| 7 | +// |
| 8 | +// These tests run against pure modules (no browser APIs needed): |
| 9 | +// - constants.js — verifies the new constants are defined with correct values |
| 10 | +// - permission-normalize.js — verifies the pure normalization helper works for |
| 11 | +// both native OpenCode payload shapes and Codex payload shapes |
| 12 | + |
| 13 | +import { describe, expect, test } from "bun:test" |
| 14 | +import { EVENTS } from "../constants.js" |
| 15 | +import { |
| 16 | + normalizePermissionPending, |
| 17 | + normalizePermissionResolved, |
| 18 | +} from "../sse/permission-normalize.js" |
| 19 | + |
| 20 | +// ── Step 1: constants ───────────────────────────────────────────────────────── |
| 21 | + |
| 22 | +describe("constants.js — pilot permission alias event types", () => { |
| 23 | + // FAILING until constants are added: EVENTS.PERMISSION_PENDING_PILOT and |
| 24 | + // EVENTS.PERMISSION_RESOLVED_PILOT must exist with the exact strings that |
| 25 | + // Codex emits. |
| 26 | + |
| 27 | + test("EVENTS.PERMISSION_PENDING_PILOT equals 'pilot.permission.pending'", () => { |
| 28 | + expect(EVENTS.PERMISSION_PENDING_PILOT).toBe("pilot.permission.pending") |
| 29 | + }) |
| 30 | + |
| 31 | + test("EVENTS.PERMISSION_RESOLVED_PILOT equals 'pilot.permission.resolved'", () => { |
| 32 | + expect(EVENTS.PERMISSION_RESOLVED_PILOT).toBe("pilot.permission.resolved") |
| 33 | + }) |
| 34 | + |
| 35 | + test("native PERMISSION_REQUESTED constant is unchanged (no regression)", () => { |
| 36 | + expect(EVENTS.PERMISSION_REQUESTED).toBe("permission.requested") |
| 37 | + }) |
| 38 | + |
| 39 | + test("native PERMISSION_RESOLVED constant is unchanged (no regression)", () => { |
| 40 | + expect(EVENTS.PERMISSION_RESOLVED).toBe("permission.resolved") |
| 41 | + }) |
| 42 | +}) |
| 43 | + |
| 44 | +// ── Step 2: normalization helper ────────────────────────────────────────────── |
| 45 | + |
| 46 | +describe("normalizePermissionPending — Codex payload shape", () => { |
| 47 | + // The exact payload Codex emits in handlers.ts ~line 275: |
| 48 | + // { permissionID, title: `Codex: ${tool_name}`, sessionID, permissionType, metadata } |
| 49 | + const codexEvent = { |
| 50 | + type: "pilot.permission.pending", |
| 51 | + properties: { |
| 52 | + permissionID: "uuid-123", |
| 53 | + title: "Codex: BashTool", |
| 54 | + sessionID: "sess-abc", |
| 55 | + permissionType: "codex-tool", |
| 56 | + metadata: { tool_name: "BashTool", source: "codex-hook" }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + test("normalizePermissionPending exists and handles Codex payload", () => { |
| 61 | + const normalized = normalizePermissionPending(codexEvent) |
| 62 | + expect(normalized.id).toBe("uuid-123") |
| 63 | + expect(normalized.permissionID).toBe("uuid-123") |
| 64 | + expect(normalized.title).toBe("Codex: BashTool") |
| 65 | + expect(normalized.sessionID).toBe("sess-abc") |
| 66 | + expect(normalized.type).toBe("codex-tool") |
| 67 | + expect(normalized.metadata).toEqual({ tool_name: "BashTool", source: "codex-hook" }) |
| 68 | + }) |
| 69 | + |
| 70 | + test("normalizePermissionPending handles native OpenCode payload shape", () => { |
| 71 | + // Native OpenCode events carry the same fields under .properties |
| 72 | + const nativeEvent = { |
| 73 | + type: "permission.requested", |
| 74 | + properties: { |
| 75 | + permissionID: "native-uuid-456", |
| 76 | + title: "Shell: rm -rf", |
| 77 | + sessionID: "sess-native", |
| 78 | + permissionType: "shell", |
| 79 | + pattern: "/tmp/**", |
| 80 | + metadata: { command: "rm -rf /tmp/foo" }, |
| 81 | + }, |
| 82 | + } |
| 83 | + const normalized = normalizePermissionPending(nativeEvent) |
| 84 | + expect(normalized.id).toBe("native-uuid-456") |
| 85 | + expect(normalized.title).toBe("Shell: rm -rf") |
| 86 | + expect(normalized.pattern).toBe("/tmp/**") |
| 87 | + }) |
| 88 | + |
| 89 | + test("normalizePermissionPending handles .data shape (EventSource named-event path)", () => { |
| 90 | + // Named SSE events wrap the payload in .data |
| 91 | + const namedEvent = { |
| 92 | + type: "permission.requested", |
| 93 | + data: { |
| 94 | + permissionID: "evt-data-789", |
| 95 | + title: "Read file", |
| 96 | + sessionID: "sess-data", |
| 97 | + permissionType: "read", |
| 98 | + }, |
| 99 | + } |
| 100 | + const normalized = normalizePermissionPending(namedEvent) |
| 101 | + expect(normalized.id).toBe("evt-data-789") |
| 102 | + expect(normalized.sessionID).toBe("sess-data") |
| 103 | + }) |
| 104 | + |
| 105 | + test("pattern field is undefined (not present) for Codex events — safe default", () => { |
| 106 | + // Codex does NOT send a pattern field. The normalize should produce |
| 107 | + // pattern: undefined (not throw) so the banner gracefully shows no detail. |
| 108 | + const normalized = normalizePermissionPending(codexEvent) |
| 109 | + expect(normalized.pattern).toBeUndefined() |
| 110 | + }) |
| 111 | +}) |
| 112 | + |
| 113 | +describe("normalizePermissionResolved — Codex payload shape", () => { |
| 114 | + const codexResolved = { |
| 115 | + type: "pilot.permission.resolved", |
| 116 | + properties: { |
| 117 | + permissionID: "uuid-123", |
| 118 | + action: "allow", |
| 119 | + source: "remote", |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + test("normalizePermissionResolved exists and handles Codex payload", () => { |
| 124 | + const normalized = normalizePermissionResolved(codexResolved) |
| 125 | + expect(normalized.id).toBe("uuid-123") |
| 126 | + expect(normalized.permissionID).toBe("uuid-123") |
| 127 | + }) |
| 128 | + |
| 129 | + test("normalizePermissionResolved handles client_disconnected reason", () => { |
| 130 | + const disconnectEvent = { |
| 131 | + type: "pilot.permission.resolved", |
| 132 | + properties: { |
| 133 | + permissionID: "uuid-disconnected", |
| 134 | + action: "deny", |
| 135 | + source: "remote", |
| 136 | + reason: "client_disconnected", |
| 137 | + }, |
| 138 | + } |
| 139 | + const normalized = normalizePermissionResolved(disconnectEvent) |
| 140 | + expect(normalized.id).toBe("uuid-disconnected") |
| 141 | + expect(normalized.permissionID).toBe("uuid-disconnected") |
| 142 | + }) |
| 143 | + |
| 144 | + test("normalizePermissionResolved handles timeout reason", () => { |
| 145 | + const timeoutEvent = { |
| 146 | + type: "pilot.permission.resolved", |
| 147 | + properties: { |
| 148 | + permissionID: "uuid-timeout", |
| 149 | + action: "deny", |
| 150 | + source: "remote", |
| 151 | + reason: "timeout", |
| 152 | + }, |
| 153 | + } |
| 154 | + const normalized = normalizePermissionResolved(timeoutEvent) |
| 155 | + expect(normalized.id).toBe("uuid-timeout") |
| 156 | + }) |
| 157 | +}) |
0 commit comments