// Final single Cloud VM reboot: actual dev/test UI only, never API approval. const fs = require('node:fs'); const path = require('node:path'); const crypto = require('node:crypto'); const { isDeepStrictEqual: equal } = require('node:util'); const { chromium } = require('../../web/node_modules/playwright'); const root = path.resolve(__dirname, '../..'); const reason = 'Explicit final Linux VM dev/test acceptance; reconnect within 180 seconds of the central start reservation'; const assert = value => { if (!value) throw new Error('Final reboot UI contract mismatch'); }; function readPrivate(file) { const s = fs.lstatSync(file); assert(s.isFile() && !s.isSymbolicLink() && s.uid === process.getuid() && (s.mode & 0o777) === 0o600 && s.size < 1048576); return JSON.parse(fs.readFileSync(file, 'utf8')); } function checkPlan(plan, expected) { assert(plan.jobs.length === 1 && plan.resolved_targets.length === 1 && crypto.createHash('sha256').update(Buffer.from(plan.plan_payload_bytes)).digest('hex') === plan.plan_hash); const p = plan.jobs[0].payload; assert(p.tenant_id === expected.tenant_id && p.agent_id === expected.agent_id && p.asset_id === expected.asset_id && p.execution_identity === 'root' && p.max_runtime_seconds === 240 && p.max_output_bytes === 65536); assert(equal(p.action, { action: 'reboot', reason, reconnect_timeout_seconds: 180, dependency_group: 'single-test-vm' })); assert(Date.parse(p.start_before) > Date.now()); } (async () => { const file = path.resolve(process.argv[2]); const expected = readPrivate(file); assert(expected.profile === 'dev/test' && expected.case === 'reboot_last' && ['prepare', 'approve'].includes(expected.phase) && expected.expected_principal === 'simulated-testprincipal-a'); const login = readPrivate(path.join(root, 'credentials/server-browser-login.json')); const browsers = path.join(root, 'web/node_modules/.cache/playwright-browsers'); const version = fs.readdirSync(browsers).find(x => /^chromium-/.test(x)); const executablePath = ['chrome-linux/chrome', 'chrome-linux64/chrome'].map(x => path.join(browsers, version, x)).find(x => fs.existsSync(x)); const browser = await chromium.launch({ headless: true, executablePath, env: { ...process.env, HOME: path.join(root, 'web/node_modules/.cache/cloud-acceptance/home') } }); try { const page = await browser.newPage({ ignoreHTTPSErrors: false, timezoneId: 'UTC' }); page.setDefaultTimeout(15000); let approvals = 0; page.on('request', r => { if (new URL(r.url()).pathname.endsWith('/authorizations')) approvals++; }); await page.goto('https://localhost:18443/'); await page.getByLabel('Benutzername').fill(login.username); await page.getByLabel('Passwort', { exact: true }).fill(login.password); const response = page.waitForResponse(r => new URL(r.url()).pathname === '/auth/session' && r.status() === 200); await page.getByRole('button', { name: 'Anmelden', exact: true }).click(); const session = await (await response).json(); assert(session.testsimulation === true && session.approval_origin === 'dev_test_browser_simulation' && session.principal === expected.expected_principal); await page.getByRole('status').filter({ hasText: 'SIMULIERT' }).waitFor(); assert(await page.getByLabel('Mandant', { exact: true }).inputValue() === expected.tenant_id); const detail = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Unveränderlicher Plan / Genehmigungsdetail', exact: true }) }); if (expected.phase === 'prepare') { const request = expected.request; assert(request.action === 'reboot' && request.reason === reason && equal(request.targets.asset_ids, [expected.asset_id]) && equal(request.rollout, { max_parallel: 1, halt_after_failures: 1 }) && equal(request.parameters, { reason, dependency_group: 'single-test-vm', reconnect_timeout_seconds: 180, execution_identity: 'root', timeout_seconds: 240, max_output_bytes: 65536 })); await page.getByRole('checkbox', { name: 'Ziel ' + expected.asset_id, exact: true }).check(); await page.getByLabel('Aktion', { exact: true }).selectOption('reboot'); await page.getByLabel('Aktionsparameter', { exact: true }).fill(JSON.stringify(request.parameters)); await page.getByLabel('Ausführungsidentität', { exact: true }).selectOption('root'); await page.getByLabel('Laufzeitlimit (s)', { exact: true }).fill('240'); await page.getByLabel('Ausgabelimit (Bytes)', { exact: true }).fill('65536'); await page.getByLabel('Frühester Start (lokale Zeit)', { exact: true }).fill(new Date(request.schedule.not_before).toISOString().slice(0, 16)); await page.getByLabel('Startfrist / Ablauf (lokale Zeit)', { exact: true }).fill(new Date(request.schedule.start_before).toISOString().slice(0, 16)); await page.getByLabel('Maximal parallel', { exact: true }).fill('1'); await page.getByLabel('Stopp nach Fehlern', { exact: true }).fill('1'); await page.getByLabel('Begründung', { exact: true }).fill(reason); await page.getByRole('button', { name: 'Plan erstellen · noch keine Ausführung' }).click(); await detail.locator('pre').waitFor(); const plan = JSON.parse(await detail.locator('pre').innerText()); checkPlan(plan, expected); await detail.getByRole('button', { name: 'Antrag absenden · pending_approval' }).click(); const operation = page.locator('section').filter({ has: page.getByRole('heading', { name: /^Antrag \/ Ausführung / }) }); await operation.locator('pre').first().filter({ hasText: '"job_count": 0' }).waitFor(); const pending = JSON.parse(await operation.locator('pre').first().innerText()); assert(pending.approval_state === 'pending_approval' && pending.execution_state === null && approvals === 0); assert(path.dirname(path.resolve(expected.output_file)) === path.dirname(file)); fs.writeFileSync(expected.output_file, JSON.stringify({ plan, operation: pending }), { flag: 'wx', mode: 0o600 }); } else { await page.getByRole('button', { name: expected.operation_id, exact: true }).click(); const operation = page.locator('section').filter({ has: page.getByRole('heading', { name: 'Antrag / Ausführung ' + expected.operation_id, exact: true }) }); const pending = JSON.parse(await operation.locator('pre').first().innerText()); assert(pending.approval_state === 'pending_approval' && pending.job_count === 0 && pending.execution_state === null); await operation.getByRole('button', { name: 'Unveränderlichen Plan zur Prüfung öffnen', exact: true }).click(); const plan = JSON.parse(await detail.locator('pre').innerText()); checkPlan(plan, expected); const { approval_state: currentState, ...actual } = plan; const { approval_state: previousState, ...prepared } = expected.plan; assert(currentState === 'pending_approval' && previousState === null && equal(actual, prepared) && equal(plan.jobs[0], expected.job)); const approve = detail.getByRole('button', { name: 'SIMULIERT: Diesen festen Plan ausdrücklich genehmigen', exact: true }); assert(await approve.isDisabled()); await detail.getByRole('checkbox', { name: 'Ich habe sämtliche festen Ziele', exact: false }).check(); await approve.click(); await operation.locator('p.warning').filter({ hasText: 'Antrag: approved' }).waitFor(); assert(approvals === 1); } await page.getByRole('button', { name: 'Abmelden', exact: true }).click(); } finally { await browser.close(); } })().catch(error => { const line = error.stack?.match(/cloud_reboot_acceptance\.cjs:(\d+):\d+/)?.[1] || 'unknown'; console.log('CLOUD_REBOOT_UI_INCOMPLETE:' + error.constructor.name + ':source_line=' + line); process.exitCode = 1; });