diff --git a/public/assets/app.css b/public/assets/app.css index db076ec..9b87fa4 100644 --- a/public/assets/app.css +++ b/public/assets/app.css @@ -182,3 +182,8 @@ html[data-theme="light"] .pulse-dim { color: var(--text-muted); } html[data-theme="light"] .pulse-running { box-shadow: none; } + +/* The `hidden` attribute must win over component display rules such as + .lt-form-group { display: flex }, or hidden sections still show (both + Quick Command worker pickers were visible in either mode). */ +[hidden] { display: none !important; } diff --git a/public/assets/pages/quick.css b/public/assets/pages/quick.css index 7e4c706..5b18915 100644 --- a/public/assets/pages/quick.css +++ b/public/assets/pages/quick.css @@ -83,3 +83,38 @@ color: var(--text-muted); font-size: 0.85em; } + +/* Results under the form, one card per worker (#5). */ +.qc-run-summary { + font-size: 0.75rem; + color: var(--text-secondary); + margin-bottom: 0.5rem; +} +.qc-run { + border: 1px solid var(--border-dim, var(--border-color)); + background: var(--bg-primary); + padding: 0.6rem 0.75rem; + margin-bottom: 0.6rem; + min-width: 0; +} +.qc-run-head { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 0.4rem 0.75rem; +} +.qc-run-time { color: var(--text-muted); font-size: 0.75rem; } +.qc-run-link { margin-left: auto; font-size: 0.75rem; } +.qc-run-body pre { + max-height: 24rem; + overflow: auto; + white-space: pre-wrap; + overflow-wrap: anywhere; + margin: 0.5rem 0 0; +} +.qc-out-err { + color: var(--accent-red); + border-left-color: var(--accent-red); + text-shadow: none; +} +.qc-out-note { color: var(--text-muted); font-size: 0.72rem; margin-top: 0.4rem; } diff --git a/public/assets/pages/quick.js b/public/assets/pages/quick.js index a56b228..9ebabfc 100644 --- a/public/assets/pages/quick.js +++ b/public/assets/pages/quick.js @@ -166,65 +166,117 @@ /* ------------------------------------------------------------------- Result rendering ------------------------------------------------------------------- */ - function renderSingleSuccess(executionId) { - const wrap = document.getElementById('qc-result'); - if (!wrap) return; - wrap.innerHTML = - '
' + - '✓' + - '
' + - '
Command sent successfully
' + - '
Execution ID: ' + esc(executionId) + ' | ' + - 'view in Executions' + - '
' + - '
' + - '
'; + /* Results show here as they arrive, per worker, instead of only in Executions (#5). + A card per run: "running" until its command_result comes in over the live + connection, or a poll finds it finished if that event was missed. */ + const OUTPUT_MAX = 100000; // characters shown per stream + const _runs = new Map(); // execution id -> { card, startedAt, timer } + const _early = new Map(); // results that beat the POST response + + function forgetRuns() { + _runs.forEach(r => clearTimeout(r.timer)); + _runs.clear(); } - function renderSingleFailure(message) { + function renderRuns(entries, summary) { + forgetRuns(); const wrap = document.getElementById('qc-result'); if (!wrap) return; - wrap.innerHTML = - '
' + - '✕' + - '
' + - '
Command failed
' + - '
' + esc(message) + '
' + - '
' + - '
'; + const head = summary ? '
' + esc(summary) + '
' : ''; + wrap.innerHTML = head + entries.map(runCard).join(''); + entries.forEach((e, i) => { + if (!e.executionId) return; + const run = { card: document.getElementById('qc-run-' + i), startedAt: Date.now(), timer: null }; + _runs.set(e.executionId, run); + if (_early.has(e.executionId)) { showResult(e.executionId, _early.get(e.executionId)); return; } + run.timer = setTimeout(() => pollRun(e.executionId, 0), 5000); + }); } - function renderMultiResult(results, successCount, failCount) { - const wrap = document.getElementById('qc-result'); - if (!wrap) return; - const rows = results.map(r => - '' + - '' + esc(r.worker) + '' + - '' + - (r.success - ? '✓ Sent (' + esc(String(r.executionId || '').slice(0, 8)) + '…)' - : '✕ ' + esc(r.error) + '') + - '' + - '' - ).join(''); - wrap.innerHTML = - '
' + - '
' + - '
Multi-worker execution complete
' + - '
Success: ' + successCount + '  |  Failed: ' + failCount + '
' + - '
' + - '
' + - '' + - '' + - '' + rows + '' + - '
WorkerResult
'; + function runCard(e, i) { + const id = e.executionId; + const worker = esc(e.worker); + if (!id) { + return '
' + + '' + worker + '' + + '✕ Not sent
' + + '
' + esc(e.error || 'Failed to send') + '
'; + } + return '
' + + '' + worker + '' + + 'Running…' + + '' + + 'view in Executions
' + + '
'; + } + + function clip(text) { + const t = String(text || ''); + if (t.length <= OUTPUT_MAX) return esc(t); + return esc(t.slice(0, OUTPUT_MAX)) + '\n[' + (t.length - OUTPUT_MAX).toLocaleString() + + ' more characters: see the full output in Executions]'; + } + + function failureLabel(r) { + if (r.timed_out) return 'Timed out'; + if (r.signal) return 'Killed (' + r.signal + ')'; + if (typeof r.exit_code === 'number') return 'Failed (exit ' + r.exit_code + ')'; + return 'Failed'; + } + + function showResult(executionId, r) { + const run = _runs.get(executionId); + if (!run || !run.card) return; + clearTimeout(run.timer); + _runs.delete(executionId); + _early.delete(executionId); + const status = run.card.querySelector('.qc-run-status'); + const ok = Boolean(r.success); + const label = ok ? 'Completed' : failureLabel(r); + status.className = 'lt-status qc-run-status ' + (ok ? 'lt-status-completed' : 'lt-status-failed'); + status.textContent = (ok ? '\u2713 ' : '\u2715 ') + label; + const ms = typeof r.duration === 'number' ? r.duration : Date.now() - run.startedAt; + run.card.querySelector('.qc-run-time').textContent = ms < 1000 ? ms + ' ms' : (ms / 1000).toFixed(1) + ' s'; + const parts = []; + if (r.stdout) parts.push('
' + clip(r.stdout) + '
'); + if (r.stderr) parts.push('
' + clip(r.stderr) + '
'); + if (!r.success && r.error && !r.stderr) parts.push('
' + esc(r.error) + '
'); + if (!parts.length) parts.push('
No output
'); + const body = run.card.querySelector('.qc-run-body'); + body.innerHTML = parts.join(''); + } + + // Fallback for a result event that never arrived (e.g. the live connection dropped). + async function pollRun(executionId, tries) { + const run = _runs.get(executionId); + if (!run) return; + try { + const ex = await Pulse.api.get('/api/executions/' + encodeURIComponent(executionId)); + if (ex && ex.status !== 'running') { + const logs = Array.isArray(ex.logs) ? ex.logs : []; + const r = logs.filter(l => l.action === 'command_result').pop(); + showResult(executionId, r || { success: ex.status === 'completed', error: 'No result recorded (' + ex.status + ')' }); + return; + } + } catch (e) { /* try again */ } + if (tries < 180 && _runs.has(executionId)) run.timer = setTimeout(() => pollRun(executionId, tries + 1), 5000); + } + + function onCommandResult(data) { + if (!data || !data.execution_id) return false; + if (_runs.has(data.execution_id)) { showResult(data.execution_id, data); return true; } + // Can arrive before the POST that started it returns: keep it briefly. + _early.set(data.execution_id, data); + if (_early.size > 100) _early.delete(_early.keys().next().value); + return false; } function renderExecuting(count) { + forgetRuns(); const wrap = document.getElementById('qc-result'); if (!wrap) return; - wrap.innerHTML = '
Executing' + - (count > 1 ? ' on ' + count + ' worker(s)' : '') + '…
'; + wrap.innerHTML = '
Sending' + + (count > 1 ? ' to ' + count + ' workers' : '') + '…
'; } /* ------------------------------------------------------------------- @@ -253,10 +305,9 @@ try { const data = await Pulse.api.post('/api/workers/' + encodeURIComponent(workerId) + '/command', { command }); addToHistory(command, workerName); - renderSingleSuccess(data && data.execution_id); - Pulse.beep('success'); + renderRuns([{ worker: workerName, executionId: data && data.execution_id }]); } catch (e) { - renderSingleFailure(e.message || 'Failed to execute command'); + renderRuns([{ worker: workerName, error: e.message || 'Failed to execute command' }]); Pulse.beep('error'); } return; @@ -283,10 +334,10 @@ const successCount = results.filter(r => r.success).length; const failCount = results.length - successCount; addToHistory(command, workerIds.length + ' workers'); - renderMultiResult(results, successCount, failCount); - if (failCount === 0) Pulse.beep('success'); - else if (successCount > 0) Pulse.beep('info'); - else Pulse.beep('error'); + renderRuns(results, failCount === 0 + ? 'Sent to ' + successCount + ' workers' + : 'Sent to ' + successCount + ' of ' + results.length + ' workers; ' + failCount + ' could not be reached'); + if (failCount > 0) Pulse.beep(successCount > 0 ? 'info' : 'error'); } /* ------------------------------------------------------------------- @@ -375,11 +426,12 @@ return loadWorkers().then(() => { consumeRerun(); }); } - function onEvent(type) { + function onEvent(type, data) { if (type === 'worker_update') { loadWorkers(); return true; } + if (type === 'command_result') return onCommandResult(data); return false; } diff --git a/server.js b/server.js index 6fa1676..0589275 100644 --- a/server.js +++ b/server.js @@ -630,6 +630,7 @@ wss.on('connection', (ws, req) => { stdout: stdout, stderr: stderr, ...failure, + duration: duration, is_automated: isAutomated, }); diff --git a/tests/ui/pages.ui.js b/tests/ui/pages.ui.js index a7f8452..c8b190c 100644 --- a/tests/ui/pages.ui.js +++ b/tests/ui/pages.ui.js @@ -89,6 +89,7 @@ test('empty states say what to do next (#33)', async ({ page }) => { await page.keyboard.press('Escape'); await page.goto('/quick'); + await page.locator('#qc-mode-multi').check(); // the checkbox list is the multi-worker picker await expect(page.locator('#qc-worker-list').getByRole('link', { name: 'Workers' })).toHaveAttribute('href', '/workers'); await page.goto('/workflows'); diff --git a/tests/ui/quick-command.ui.js b/tests/ui/quick-command.ui.js index 88174cd..b8f5697 100644 --- a/tests/ui/quick-command.ui.js +++ b/tests/ui/quick-command.ui.js @@ -13,8 +13,11 @@ test('a quick command runs on a worker and its output shows in Executions', asyn await page.locator('#qc-command').fill(`echo ${marker}`); await page.locator('#qc-execute-btn').click(); + // The output shows right under the form (#5), with a link to the full record. const result = page.locator('#qc-result'); - await expect(result).toContainText('Command sent successfully'); + await expect(result.locator('.qc-run-status')).toHaveText(/Completed/); + await expect(result.locator('pre.qc-out')).toHaveText(marker); + await expect(result).toContainText('node-02'); await result.getByRole('link', { name: 'view in Executions' }).click(); await expect(page).toHaveURL(/\/executions/); @@ -34,7 +37,10 @@ test('a failing command is reported as failed with its stderr', async ({ page }) await page.locator('#qc-command').fill(`echo ${marker} >&2; exit 3`); await page.locator('#qc-command').press('Control+Enter'); // the documented shortcut - await page.locator('#qc-result').getByRole('link', { name: 'view in Executions' }).click(); + const result = page.locator('#qc-result'); + await expect(result.locator('.qc-run-status')).toHaveText(/Failed \(exit 3\)/); + await expect(result.locator('pre.qc-out-err')).toHaveText(marker); + await result.getByRole('link', { name: 'view in Executions' }).click(); const detail = page.locator('#ex-detail-body'); await expect(detail).toContainText(marker); await expect(detail).toContainText(/failed/i); @@ -44,13 +50,19 @@ test('multi-worker mode sends the command to every selected worker', async ({ pa await page.goto('/quick'); await page.locator('label[for="qc-mode-multi"], #qc-mode-multi').first().click(); await page.getByRole('button', { name: /select all/i }).click(); - await page.locator('#qc-command').fill(`echo ${token()}`); + await page.locator('#qc-command').fill('echo "$WORKER_NAME says hi"'); await page.locator('#qc-execute-btn').click(); + // One card per worker, each with its own output (#5). const result = page.locator('#qc-result'); - await expect(result).toContainText('Success: 3'); - await expect(result).toContainText('Failed: 0'); - await expect(result.locator('tbody tr')).toHaveCount(3); + await expect(result).toContainText('Sent to 3 workers'); + const runs = result.locator('.qc-run'); + await expect(runs).toHaveCount(3); + for (const n of ['node-01', 'node-02', 'node-03']) { + const card = runs.filter({ hasText: n }); + await expect(card.locator('.qc-run-status')).toHaveText(/Completed/); + await expect(card.locator('pre.qc-out')).toHaveText(`${n} says hi`); + } }); test('Executions updates live when a command starts elsewhere', async ({ page, request }) => { @@ -89,7 +101,7 @@ test('Quick Command remembers the mode and workers after a reload (#31)', async await boxes.filter({ hasText: 'node-03' }).locator('input').check(); await page.locator('#qc-command').fill('true'); await page.locator('#qc-execute-btn').click(); - await expect(page.locator('#qc-result')).toContainText(/2/); + await expect(page.locator('#qc-result')).toContainText('Sent to 2 workers'); await page.reload(); await expect(page.locator('#qc-mode-multi')).toBeChecked(); @@ -103,7 +115,7 @@ test('Quick Command remembers the mode and workers after a reload (#31)', async await page.locator('#qc-worker').selectOption(node3); await page.locator('#qc-command').fill('true'); // the reload emptied it await page.locator('#qc-execute-btn').click(); - await expect(page.locator('#qc-result')).toContainText('Command sent successfully'); + await expect(page.locator('#qc-result .qc-run-status')).toHaveText(/Completed/); await page.reload(); await expect(page.locator('#qc-mode-single')).toBeChecked(); await expect(page.locator('#qc-worker option:checked')).toContainText('node-03'); @@ -129,3 +141,28 @@ test('a live update keeps the rows brought in with Load More (#17)', async ({ pa await expect(rows.first()).toContainText(marker, { timeout: 10_000 }); await expect(rows).toHaveCount(shown); }); + +test('the result still shows when the live update is missed (#5)', async ({ page }) => { + // No live connection at all: the page has to find the result by polling. + await page.routeWebSocket(/.*/, () => { /* never connects to the server */ }); + const marker = token(); + await page.goto('/quick'); + await page.locator('#qc-worker').selectOption({ label: 'node-03 (online)' }); + await page.locator('#qc-command').fill(`echo ${marker}`); + await page.locator('#qc-execute-btn').click(); + + const result = page.locator('#qc-result'); + await expect(result.locator('.qc-run-status')).toHaveText(/Running/); + await expect(result.locator('pre.qc-out')).toHaveText(marker, { timeout: 15_000 }); + await expect(result.locator('.qc-run-status')).toHaveText(/Completed/); +}); + +test('only the worker picker for the chosen mode is shown', async ({ page }) => { + await page.goto('/quick'); + await page.locator('#qc-mode-single').check(); + await expect(page.locator('#qc-single-mode')).toBeVisible(); + await expect(page.locator('#qc-multi-mode')).toBeHidden(); + await page.locator('#qc-mode-multi').check(); + await expect(page.locator('#qc-multi-mode')).toBeVisible(); + await expect(page.locator('#qc-single-mode')).toBeHidden(); +});