import assert from 'node:assert/strict'; export default async function runPrivilege({ ipc, input, key, click, check, screenshot, delay, within, hasText }) { let state; ipc('external', 'privilege'); let previousAuthGeometry = ''; state = await check('privilege overlay maps and focuses literal input', s => { if (!s.auth.visible || !s.auth.active || !s.auth.inputFocused || s.auth.width <= 0 || s.auth.height <= 0) return false; const geometry = JSON.stringify([s.auth.width, s.auth.height, s.auth.card, s.auth.shake, s.auth.shakeContentHeight]); const settled = geometry === previousAuthGeometry; previousAuthGeometry = geometry; return settled; }); assert(within(state.auth.card, state.width, state.height)); assert(state.auth.shake.height > 0); assert.equal(state.auth.shake.height, state.auth.shakeContentHeight); assert.equal(state.auth.shake.width, state.auth.card.width - 44); await screenshot('privilege-initial'); key('Tab'); await check('Tab reaches password reveal without focus theft', s => s.auth.revealFocused && !s.auth.inputFocused); key('Tab'); await check('Tab reaches authentication cancel', s => s.auth.cancelFocused); key('Tab'); await check('Tab reaches authentication authorize', s => s.auth.authorizeFocused); key('Tab'); await check('authentication traversal wraps to input', s => s.auth.inputFocused); key('shift+Tab'); await check('reverse Tab reaches authorize', s => s.auth.authorizeFocused); key('shift+Tab'); await check('reverse Tab reaches cancel', s => s.auth.cancelFocused); key('shift+Tab'); await check('reverse Tab reaches reveal', s => s.auth.revealFocused); key('shift+Tab'); await check('reverse Tab returns to input', s => s.auth.inputFocused); click(state.auth.input); input('type', 'fixture-only'); await check('privilege input is present and masked without serializing its text', s => s.auth.hasInput && s.auth.inputMasked); await screenshot('privilege-masked-input'); key('Return'); // The fixture holds each submission until settled, like a slow PAM check. // Checking keeps the submitted length as masked dots in the read-only field // and says checking once, on authorize. const maskedSubmission = rows => rows.some(row => row.text.length === 'fixture-only'.length && new Set(row.text).size === 1); state = await check('privilege Enter routes literal submit and shows it is checking', s => s.auth.submissions === 1 && s.auth.receivedLiteral && s.auth.active && s.auth.checking && s.auth.inputReadOnly && s.auth.inputFocused && !s.auth.authorizeEnabled && s.auth.authorizeLabel === 'checking…' && maskedSubmission(s.auth.text) && !hasText(s.auth.text, 'fixture input') && !hasText(s.auth.text, 'checking password') && !hasText(s.auth.text, 'password rejected')); await screenshot('privilege-checking'); key('Return'); key('Tab'); // Tab lands after the repeated Return, so that Return was handled by then. await check('Enter while checking never submits again', s => s.auth.revealFocused && s.auth.submissions === 1 && s.auth.checking); key('shift+Tab'); await check('reverse Tab returns to the checking input', s => s.auth.inputFocused && s.auth.checking); ipc('external', 'privilege-settle'); state = await check('privilege renders rejection once checked', s => s.auth.submissions === 1 && !s.auth.checking && !s.auth.inputReadOnly && s.auth.inputFocused && s.auth.authorizeEnabled && s.auth.authorizeLabel === 'authorize' && s.auth.active && hasText(s.auth.text, 'password rejected') && !hasText(s.auth.text, 'checking password') && hasText(s.auth.text, 'fixture input')); const failureCard = state.auth.card; const failureHeight = state.auth.shake.height; for (let i = 0; i < 4; i++) { state = await check(`failure shake keeps supplied frame size ${i}`, s => s.auth.shake.height === s.auth.shakeContentHeight && s.auth.shake.height === failureHeight); assert.deepEqual(state.auth.card, failureCard); assert.equal(state.auth.shake.width, failureCard.width - 44); await delay(60); } await screenshot('privilege-rejected'); click(state.auth.reveal); state = await check('pointer reveal toggles without stealing input focus', s => s.auth.revealed && s.auth.revealFocused && !s.auth.inputFocused && hasText(s.auth.text, 'fixture input')); // Reveal is captured with an empty field, never even a synthetic secret. await screenshot('privilege-reveal-empty'); click(state.auth.reveal); await check('pointer hides input again', s => !s.auth.revealed); click(state.auth.input); input('type', 'fixture-only'); key('Return'); await check('a retry replaces the rejection with checking', s => s.auth.submissions === 2 && s.auth.checking && s.auth.authorizeLabel === 'checking…' && maskedSubmission(s.auth.text) && !hasText(s.auth.text, 'password rejected')); ipc('external', 'privilege-settle'); state = await check('second Enter renders fixture authorization success', s => s.auth.submissions === 2 && s.auth.receivedLiteral && s.auth.confirming && !s.auth.active && hasText(s.auth.text, 'authorized')); await screenshot('privilege-authorized'); ipc('external', 'privilege'); state = await check('new fixture request reacquires focus', s => s.auth.active && s.auth.inputFocused && !s.auth.confirming); click(state.auth.cancel); await check('pointer cancel dismisses privilege overlay', s => !s.auth.visible && s.auth.cancellations === 1); ipc('external', 'privilege'); state = await check('privilege reopens after pointer cancellation', s => s.auth.visible && s.auth.inputFocused); click(state.auth.input); input('type', 'fixture-only'); key('Escape'); await check('Escape cancels privilege without submitting', s => !s.auth.visible && s.auth.cancellations === 2 && s.auth.submissions === 2); }