import assert from 'assert'; import { validatePage, PageValidationResult, PageValidationError, } from '../src/service/pageValidator'; // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- /** Wrap inline script(s) in a minimal HTML page. */ function page(body: string, scripts: string | string[] = []): string { const arr = Array.isArray(scripts) ? scripts : [scripts]; const scriptTags = arr.map(s => ``).join('\n'); return `${body}\n${scriptTags}`; } function pageWithIds(ids: string[], scripts: string | string[] = []): string { const divs = ids.map(id => `
`).join('\n'); return page(divs, scripts); } function errorTypes(result: PageValidationResult): string[] { return result.errors.map(e => e.type); } // --------------------------------------------------------------------------- // Layer 1: Syntax Errors (Acorn Parse) // --------------------------------------------------------------------------- describe('pageValidator', () => { describe('Layer 1 — Syntax errors', () => { it('catches unclosed brace', () => { const html = page('', 'const x = {'); const result = validatePage(html); assert.strictEqual(result.valid, false); assert.strictEqual(result.errors.length, 1); assert.strictEqual(result.errors[0].type, 'syntax-error'); assert.ok(result.errors[0].line !== undefined, 'should include line number'); assert.ok(result.errors[0].col !== undefined, 'should include column number'); }); it('catches unexpected token', () => { const html = page('', 'function() {}'); const result = validatePage(html); assert.strictEqual(result.valid, false); assert.ok(result.errors.some(e => e.type === 'syntax-error')); }); it('reports source as inline-script-N', () => { const html = page('', ['const a = 1;', 'const b = {']); const result = validatePage(html); const err = result.errors.find(e => e.type === 'syntax-error')!; assert.ok(err); assert.strictEqual(err.source, 'inline-script-1'); }); it('catches errors in multiple scripts independently', () => { const html = page('', ['const a = {', 'const b = {']); const result = validatePage(html); assert.strictEqual(result.valid, false); assert.strictEqual(result.errors.filter(e => e.type === 'syntax-error').length, 2); }); it('parses module scripts correctly', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('skips empty scripts', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('skips external scripts (with src attribute)', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('allows top-level await', () => { const html = page('', 'const data = await fetch("/api");'); const result = validatePage(html); assert.strictEqual(result.valid, true); }); }); // ----------------------------------------------------------------------- // Injected Scripts — Should Not Produce False Positives // ----------------------------------------------------------------------- describe('Injected scripts — no false positives', () => { it('skips page-bridge script', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('skips page-helpers script', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, true); }); it('skips page-info script', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, true); }); it('skips page-script (legacy)', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, true); }); it('skips synthos-error-capture (legacy)', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, true); }); it('still validates scripts without known IDs', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, false); assert.strictEqual(result.errors.length, 1); assert.strictEqual(result.errors[0].source, 'inline-script-0'); }); }); // ----------------------------------------------------------------------- // Layer 2: DOM Element ID Inventory // ----------------------------------------------------------------------- describe('Layer 2 — Missing element IDs', () => { it('catches getElementById referencing missing ID', () => { const html = pageWithIds(['header'], "document.getElementById('chartArea');"); const result = validatePage(html); assert.strictEqual(result.valid, false); assert.strictEqual(result.errors.length, 1); assert.strictEqual(result.errors[0].type, 'missing-element'); assert.ok(result.errors[0].message.includes('#chartArea')); }); it('catches querySelector referencing missing ID', () => { const html = pageWithIds(['app'], "document.querySelector('#missing');"); const result = validatePage(html); assert.strictEqual(result.valid, false); assert.ok(result.errors.some(e => e.type === 'missing-element' && e.message.includes('#missing'))); }); it('catches querySelectorAll referencing missing ID', () => { const html = pageWithIds([], "document.querySelectorAll('#gone');"); const result = validatePage(html); assert.strictEqual(result.valid, false); assert.ok(result.errors.some(e => e.type === 'missing-element')); }); it('catches jQuery-style $() referencing missing ID', () => { const html = pageWithIds(['content'], "$('#sidebar');"); const result = validatePage(html); assert.strictEqual(result.valid, false); assert.ok(result.errors.some(e => e.type === 'missing-element' && e.message.includes('#sidebar'))); }); it('passes when all referenced IDs exist', () => { const html = pageWithIds( ['chartArea', 'sidebar', 'content'], [ "document.getElementById('chartArea');", "document.querySelector('#sidebar');", "$('#content');", ] ); const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('does NOT flag IDs created by innerHTML string literals', () => { // A script renders a form at runtime via innerHTML, then queries // its own generated IDs. Static inventory would flag #btnSave // and #mapDate as missing — dynamic inventory covers them. const html = pageWithIds(['root'], "document.getElementById('root').innerHTML = '';" + "document.getElementById('btnSave').addEventListener('click', function(){});" + "document.querySelector('#mapDate').value = '';" ); const result = validatePage(html); assert.strictEqual(result.valid, true, 'innerHTML-created IDs should not trigger missing-element errors'); assert.strictEqual(result.errors.length, 0); }); it('does NOT flag IDs created via template literal innerHTML', () => { // Realistic render() pattern using backtick template string. const html = pageWithIds(['qbBody'], 'const rows = ``;' + "document.getElementById('qbBody').innerHTML = rows;" + "document.getElementById('btnSaveAll').onclick = function(){};" + "document.getElementById('filterCategory').onchange = function(){};" ); const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('does NOT flag IDs assigned via .id property', () => { const html = pageWithIds(['root'], "const el = document.createElement('div');" + "el.id = 'dynHeader';" + "document.getElementById('root').appendChild(el);" + "document.getElementById('dynHeader').textContent = 'x';" ); const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('still flags truly missing IDs when script never creates them', () => { // Make sure the widened inventory doesn't swallow every reference. const html = pageWithIds(['root'], "document.getElementById('root').innerHTML = '
';" + "document.getElementById('bogusId').click();" ); const result = validatePage(html); assert.strictEqual(result.valid, false); const missing = result.errors.filter(e => e.type === 'missing-element'); assert.strictEqual(missing.length, 1); assert.ok(missing[0].message.includes('#bogusId')); }); it('does NOT run when Layer 1 found syntax errors', () => { // The script has a syntax error AND references a missing ID. // Layer 2 should be skipped, so only the syntax error appears. const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, false); assert.ok(result.errors.every(e => e.type !== 'missing-element'), 'missing-element errors should not appear when there are syntax errors'); assert.ok(result.errors.some(e => e.type === 'syntax-error')); }); }); // ----------------------------------------------------------------------- // Layer 3: SynthOS API Pattern Checks // ----------------------------------------------------------------------- describe('Layer 3 — SynthOS API checks', () => { it('catches unknown synthos API call', () => { const html = page('', 'await synthos.generate.video();'); const result = validatePage(html); assert.strictEqual(result.valid, false); assert.ok(result.errors.some(e => e.type === 'unknown-api' && e.message.includes('synthos.generate.video') )); }); it('catches multiple unknown API calls', () => { const html = page('', [ 'synthos.ai.think();', 'synthos.render.pdf();', ]); const result = validatePage(html); const unknownApis = result.errors.filter(e => e.type === 'unknown-api'); assert.strictEqual(unknownApis.length, 2); }); it('deduplicates same unknown API call across scripts', () => { const html = page('', [ 'synthos.generate.video();', 'synthos.generate.video();', ]); const result = validatePage(html); const unknownApis = result.errors.filter(e => e.type === 'unknown-api'); assert.strictEqual(unknownApis.length, 1); }); it('deduplicates same unknown API call within one script', () => { const html = page('', ` synthos.generate.video(); synthos.generate.video(); synthos.generate.video(); `); const result = validatePage(html); const unknownApis = result.errors.filter(e => e.type === 'unknown-api'); assert.strictEqual(unknownApis.length, 1); }); it('runs even when there are syntax errors (Layer 3 always runs)', () => { const html = page('', [ 'const x = {', // syntax error 'synthos.generate.video();', // unknown API ]); const result = validatePage(html); assert.ok(result.errors.some(e => e.type === 'syntax-error'), 'should have syntax error'); assert.ok(result.errors.some(e => e.type === 'unknown-api'), 'should have unknown-api error'); }); describe('known methods pass', () => { const knownCalls = [ 'synthos.data.save', 'synthos.data.get', 'synthos.data.list', 'synthos.data.remove', 'synthos.files.upload', 'synthos.files.url', 'synthos.files.list', 'synthos.files.remove', 'synthos.generate.image', 'synthos.generate.completion', 'synthos.script.run', 'synthos.page.list', 'synthos.page.get', 'synthos.page.update', 'synthos.page.remove', 'synthos.page.ask', 'synthos.search.web', 'synthos.connector.call', 'synthos.connector.list', 'synthos.agent.list', 'synthos.agent.send', 'synthos.agent.sendStream', 'synthos.agent.chat.send', 'synthos.agent.chat.sendStream', 'synthos.agent.chat.history', 'synthos.agent.chat.abort', 'synthos.agent.chat.clear', 'synthos.agent.isEnabled', 'synthos.agent.getCapabilities', 'synthos.shell.navigate', 'synthos.shell.submitChat', 'synthos.shell.setDirty', 'synthos.shell.showLoading', 'synthos.shell.hideLoading', 'synthos.shell.focusChat', 'synthos.shell.on', 'synthos.shared.data.save', 'synthos.shared.data.get', 'synthos.shared.files.upload', 'synthos.shared.files.url', ]; for (const method of knownCalls) { it(`${method} → no error`, () => { const html = page('', `await ${method}('test');`); const result = validatePage(html); const unknownApis = result.errors.filter(e => e.type === 'unknown-api'); assert.strictEqual(unknownApis.length, 0, `${method} should be recognized`); }); } }); }); // ----------------------------------------------------------------------- // Clean Pages — Should Pass // ----------------------------------------------------------------------- describe('Clean pages pass validation', () => { it('simple page with valid script', () => { const html = pageWithIds(['app'], "document.getElementById('app').textContent = 'Hello';"); const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('page with no scripts', () => { const html = '

Static page

'; const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('page with known synthos calls and matching IDs', () => { const html = pageWithIds( ['output', 'canvas'], [ "const data = await synthos.data.get('myKey');", "document.getElementById('output').textContent = data;", "const img = await synthos.generate.image('a cat');", ] ); const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('page with only external scripts', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, true); }); it('page mixing external and valid inline scripts', () => { const html = `
`; const result = validatePage(html); assert.strictEqual(result.valid, true); }); }); // ----------------------------------------------------------------------- // Result structure // ----------------------------------------------------------------------- describe('Result structure', () => { it('includes durationMs', () => { const html = page('', 'const x = 1;'); const result = validatePage(html); assert.ok(typeof result.durationMs === 'number'); assert.ok(result.durationMs >= 0); }); it('valid is true when errors is empty', () => { const html = page('', 'const x = 1;'); const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); it('valid is false when errors is non-empty', () => { const html = page('', 'const x = {'); const result = validatePage(html); assert.strictEqual(result.valid, false); assert.ok(result.errors.length > 0); }); }); // ----------------------------------------------------------------------- // Combined scenarios // ----------------------------------------------------------------------- describe('Combined scenarios', () => { it('syntax error + unknown API → both reported (no missing-element)', () => { const html = ` `; const result = validatePage(html); assert.strictEqual(result.valid, false); const types = errorTypes(result); assert.ok(types.includes('syntax-error')); assert.ok(types.includes('unknown-api')); assert.ok(!types.includes('missing-element'), 'Layer 2 should be skipped'); }); it('missing element + unknown API → both reported', () => { const html = pageWithIds(['header'], [ "document.getElementById('footer');", "synthos.fake.method();", ]); const result = validatePage(html); assert.strictEqual(result.valid, false); const types = errorTypes(result); assert.ok(types.includes('missing-element')); assert.ok(types.includes('unknown-api')); }); it('realistic page with bridge, helpers, and user script', () => { const html = `
`; const result = validatePage(html); assert.strictEqual(result.valid, true); assert.strictEqual(result.errors.length, 0); }); }); });