// Actual UI only: closes exactly one unknown job through the dev/test WebUI as the // visibly simulated test principal. No page.request, fetch injection, API or DB access. // Usage: node close_unknown_job.cjs const fs = require('node:fs'); const path = require('node:path'); const { chromium } = require('../../web/node_modules/playwright'); const root = path.resolve(__dirname, '../..'); const assert = (value, code) => { if (!value) throw new Error(code || 'UI contract mismatch'); }; (async () => { const [operationId, jobId, reason] = process.argv.slice(2); assert(/^[0-9a-f-]{36}$/.test(operationId) && /^[0-9a-f-]{36}$/.test(jobId) && reason && reason.length <= 1024, 'ARGS'); const loginFile = path.join(root, 'credentials/server-browser-login.json'); const loginStat = fs.lstatSync(loginFile); assert(loginStat.isFile() && !loginStat.isSymbolicLink() && loginStat.uid === process.getuid() && (loginStat.mode & 0o777) === 0o600, 'LOGIN_FILE'); const login = JSON.parse(fs.readFileSync(loginFile, 'utf8')); const cache = path.join(root, 'web/node_modules/.cache/cloud-acceptance'); const browserRoot = path.join(root, 'web/node_modules/.cache/playwright-browsers'); const version = fs.readdirSync(browserRoot).find(x => /^chromium-/.test(x)); const candidates = ['chrome-linux/chrome', 'chrome-linux64/chrome'].map(x => path.join(browserRoot, version, x)); const browser = await chromium.launch({ headless: true, executablePath: candidates.find(x => fs.existsSync(x)), env: { ...process.env, HOME: path.join(cache, 'home') } }); try { const page = await browser.newPage({ ignoreHTTPSErrors: false }); await page.goto('https://localhost:18443/'); await page.getByLabel('Benutzername').fill(login.username); await page.getByLabel('Passwort', { exact: true }).fill(login.password); const sessionResponse = 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 sessionResponse).json(); assert(session.testsimulation === true && session.approval_origin === 'dev_test_browser_simulation' && session.principal === 'simulated-testprincipal-a', 'SESSION'); await page.getByRole('status').filter({ hasText: 'SIMULIERT' }).waitFor(); await page.getByRole('link', { name: 'Aufträge', exact: true }).click(); await page.getByRole('button', { name: operationId, exact: true }).click(); await page.getByRole('button', { name: `Job ${jobId} · unknown`, exact: true }).click(); const job = page.locator('article').filter({ has: page.getByRole('heading', { name: `Job ${jobId}`, exact: true }) }); const before = JSON.parse(await job.getByLabel('Vollständige Originaldaten: Versuch, Zustand, Ausgabe, Trunkierung, Nachprüfungen und Ergebnis', { exact: true }).innerText()); assert(before.job_id === jobId && before.state === 'unknown', 'NOT_UNKNOWN'); const region = job.getByRole('region', { name: 'Unknown-Job abschließen', exact: true }); const button = region.getByRole('button', { name: 'SIMULIERT: Unknown-Job ausdrücklich abschließen', exact: true }); assert(await button.isDisabled(), 'BUTTON_NOT_GATED'); await region.getByLabel('Begründung').fill(reason); await region.getByRole('checkbox', { name: 'Ich akzeptiere, dass die Wirkung', exact: false }).check(); const response = page.waitForResponse(r => new URL(r.url()).pathname.endsWith(`/jobs/${jobId}/unknown-closure`)); await button.click(); const closed = await (await response).json(); assert(closed.state === 'closed_unknown' && closed.result.error_code === 'OPERATOR_CLOSED_UNKNOWN' && closed.result.evidence.effect === 'unknown' && closed.result.evidence.retry === false && closed.result.evidence.testsimulation === true && closed.result.evidence.approval_origin === 'dev_test_browser_simulation', 'CLOSURE_RESULT'); console.log(JSON.stringify({ closed: true, job_id: jobId, before_state: before.state, before_revision: before.revision, state: closed.state, revision: closed.revision, closure_id: closed.result.evidence.closure_id, principal: session.principal, approval_origin: session.approval_origin, testsimulation: session.testsimulation })); await page.getByRole('button', { name: 'Abmelden', exact: true }).click(); } finally { await browser.close(); } })().catch(error => { console.log('UNKNOWN_CLOSURE_INCOMPLETE:' + error.message); process.exitCode = 1; });