'use strict'; const assert = require('node:assert/strict'); const fs = require('node:fs/promises'); const {chromium} = require(process.env.PLAYWRIGHT_MODULE || 'playwright'); (async () => { const browser = await chromium.launch({headless: true, ...(process.env.PLAYWRIGHT_EXECUTABLE_PATH ? {executablePath: process.env.PLAYWRIGHT_EXECUTABLE_PATH} : {})}); try { const context = await browser.newContext({viewport: {width: 1440, height: 1000}}); const page = await context.newPage(), errors = [], requests = []; page.on('pageerror', error => errors.push(error.message)); page.on('request', request => requests.push(request.url())); await page.goto(process.env.APP_URL || 'http://127.0.0.1:4186/'); await page.waitForFunction(() => !!window.CyberLocal); const fixture = await page.evaluate(async () => { let a = (await CyberLocal.request('/api/assessments', {method: 'POST', body: {name: 'Protocol synthetic tabletop', client: 'Synthetic only', scope: 'Fictional training discussion only', workflow: 'exercise', content: 'Fictional identity tabletop with no production targets.', authorization: true, roles: ['red', 'blue', 'white', 'black', 'purple']}})).assessment; a = (await CyberLocal.request(`/api/assessments/${a.id}/workbook`, {method: 'PATCH', body: {expected_revision: a.revision, entry: {role: 'blue', owner: 'Synthetic blue owner', status: 'in-progress', evidence: 'Existing synthetic evidence', expected: 'Existing expected note.', observed: 'Existing observed note.', decision: 'Existing decision.'}}})).assessment; return a; }); const saved = () => page.evaluate(async id => (await CyberLocal.request('/api/assessments/' + id)).assessment, fixture.id); await page.reload(); await page.getByRole('button', {name: fixture.name, exact: true}).click(); await page.locator('#tab-missions').click(); assert.equal(await page.locator('#protocol-context').count(), 1, 'Role tasks provides protocol context'); await page.locator('#protocol-context > summary').click(); const topic = page.locator('#protocol-topic'); assert.equal(await topic.locator('option').count(), 5, 'placeholder plus four curated topics'); const requestCount = requests.length; for (const id of ['http', 'tls', 'dns', 'ssh']) { await topic.selectOption(id); assert.equal(await page.locator('#protocol-card').isVisible(), true); const card = await page.locator('#protocol-card').innerText(); assert.ok(card.length > 100, `${id} has substantive local learning content`); assert.ok(await page.locator('#protocol-card a[href^="https://"]').count(), `${id} cites sources`); } await topic.selectOption('tls'); const link = page.locator('#protocol-explorer'); assert.equal(await link.getAttribute('href'), 'https://protocol-explorer-alpha.vercel.app/'); assert.equal(await link.getAttribute('target'), '_blank'); assert.ok((await link.getAttribute('rel')).split(/\s+/).includes('noopener')); assert.ok((await link.getAttribute('rel')).split(/\s+/).includes('noreferrer')); assert.equal(await link.getAttribute('referrerpolicy'), 'no-referrer'); assert.equal(requests.length, requestCount, 'topic selection causes no network requests'); assert.deepEqual(await saved(), fixture, 'learning selection does not mutate assessment'); console.log('PASS all local topics, fixed external link privacy attributes, no selection requests or mutations'); await page.getByRole('button', {name: 'Add TLS prompts to blue workbook', exact: true}).click(); await page.locator('#workbook-dialog').waitFor(); const draft = await page.locator('#workbook-expected').inputValue(); assert.ok(draft.includes('Which peer identities and trust anchors should the client accept?'), 'draft contains original TLS questions'); assert.ok(draft.startsWith('Existing expected note.'), 'append preserves existing expected note'); assert.ok(draft.length > 'Existing expected note.'.length, 'adds protocol prompts to draft'); for (const [key, value] of Object.entries({owner: 'Synthetic blue owner', status: 'in-progress', evidence: 'Existing synthetic evidence', observed: 'Existing observed note.', decision: 'Existing decision.'})) assert.equal(await page.locator('#workbook-' + key).inputValue(), value, `preserves ${key}`); assert.deepEqual(await saved(), fixture, 'opening prompt draft does not save'); page.once('dialog', dialog => dialog.accept()); await page.locator('#workbook-dialog [data-close]').first().click(); await page.locator('#workbook-dialog').waitFor({state: 'hidden'}); assert.deepEqual(await saved(), fixture, 'discarding unsaved prompt draft leaves record unchanged'); await page.getByRole('button', {name: 'Add TLS prompts to blue workbook', exact: true}).click(); assert.equal(await page.locator('#workbook-expected').inputValue(), draft); await page.locator('#workbook-form button[type=submit]').click(); await page.locator('#workbook-dialog').waitFor({state: 'hidden'}); const updated = await saved(); assert.equal(updated.workbook.find(entry => entry.role === 'blue').expected, draft); assert.equal(updated.revision, fixture.revision + 1, 'one explicit workbook save increments revision once'); assert.deepEqual(updated.findings, fixture.findings); assert.equal(updated.scenario_category, fixture.scenario_category); assert.equal(await page.locator('#protocol-topic').inputValue(), 'tls', 'topic survives assessment rerender'); if (!(await page.locator('#protocol-context').evaluate(node => node.open))) await page.locator('#protocol-context > summary').click(); await page.getByRole('button', {name: 'Add TLS prompts to blue workbook', exact: true}).click(); assert.equal(await page.locator('#workbook-expected').inputValue(), draft, 'reapplying same topic avoids duplication'); await page.locator('#workbook-dialog [data-close]').first().click(); const exports = await page.evaluate(async id => ({markdown: await CyberLocal.exportText(id, 'markdown'), backup: await CyberLocal.backupText()}), fixture.id); assert.ok(exports.markdown.includes('Which peer identities and trust anchors should the client accept?'), 'Markdown includes saved TLS prompts'); assert.equal(JSON.parse(exports.backup).records.find(record => record.id === fixture.id).workbook.find(entry => entry.role === 'blue').expected, draft); const roundtrip = await page.evaluate(async backup => { const imported = JSON.parse(backup); imported.records[0].name = 'Restored protocol synthetic tabletop'; const result = await CyberLocal.restoreBackup(JSON.stringify(imported)); const rows = (await CyberLocal.request('/api/assessments')).assessments; const row = rows.find(row => row.id !== JSON.parse(backup).records[0].id); return {result, assessment: (await CyberLocal.request('/api/assessments/' + row.id)).assessment}; }, exports.backup); assert.equal(roundtrip.result.count, 1); assert.equal(roundtrip.assessment.workbook.find(entry => entry.role === 'blue').expected, draft); console.log('PASS draft-only append, discard, explicit save, duplicate avoidance and backup round-trip'); await page.setViewportSize({width: 390, height: 844}); const details = page.locator('#protocol-context'); if (!(await details.evaluate(node => node.open))) await details.locator('summary').click(); await topic.focus(); assert.equal(await topic.evaluate(node => node === document.activeElement), true, 'selector receives keyboard focus'); assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), true, '390px layout does not overflow'); if (process.env.ARTIFACT_DIR) { await fs.mkdir(process.env.ARTIFACT_DIR, {recursive: true}); await page.screenshot({path: process.env.ARTIFACT_DIR + '/protocol-mobile.png', fullPage: true}); } const fullRecord = await page.evaluate(async id => { const a = (await CyberLocal.request('/api/assessments/' + id)).assessment; const entry = {...a.workbook.find(entry => entry.role === 'blue'), expected: 'x'.repeat(1990)}; return (await CyberLocal.request(`/api/assessments/${id}/workbook`, {method: 'PATCH', body: {expected_revision: a.revision, entry}})).assessment; }, fixture.id); await page.reload(); await page.getByRole('button', {name: fixture.name, exact: true}).click(); await page.locator('#tab-missions').click(); await page.locator('#protocol-context > summary').click(); await page.locator('#protocol-topic').selectOption('tls'); await page.getByRole('button', {name: 'Add TLS prompts to blue workbook', exact: true}).click(); assert.match(await page.locator('#notice').innerText(), /2000|2,000/, 'oversized append explains existing field limit'); assert.equal(await page.locator('#workbook-dialog').evaluate(node => node.open), false, 'oversized append opens no partial draft'); assert.deepEqual(await saved(), fullRecord, 'oversized append never truncates or saves'); assert.deepEqual(errors, []); console.log('PASS mobile layout, keyboard focus, append size guard and no browser errors'); } finally { await browser.close(); } })().catch(error => {console.error(error); process.exitCode = 1;});