import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; const object = value => !!value && typeof value === "object" && !Array.isArray(value); const type = value => Array.isArray(value) ? "array" : value === null ? "null" : typeof value; export const reportGuiMethods = ["test_cheatsheet_dialog_creation", "test_hover_tooltips"]; export async function verifyReportGui(page, native, evidenceRoot, viewport, suffix = "") { const observations = { nativeType: type(native), docsType: type(native?.apiDocumentation), fixtureType: type(native?.preflight?.fixture), viewport }; const failPreflight = message => { const error = new Error(message); error.classification = "preflight_failed"; error.observations = observations; throw error; }; if (!object(native) || !object(native.apiDocumentation) || !object(native.preflight) || !object(native.preflight.fixture)) failPreflight("Native documentation/fixture shape is unavailable"); const docs = native.apiDocumentation; observations.clausesType = type(docs.clauses); observations.functionsType = type(docs.functions); if (!Array.isArray(docs.clauses) || !docs.clauses.every(v => typeof v === "string") || !object(docs.functions) || !Object.values(docs.functions).every(items => Array.isArray(items) && items.every(fn => object(fn) && [fn.name, fn.signature, fn.description].every(v => typeof v === "string") && (fn.snippet === undefined || typeof fn.snippet === "string")))) failPreflight("Native SQL documentation types are invalid"); const fixture = native.preflight.fixture; if (!Array.isArray(fixture.objects) || !fixture.objects.every(v => object(v) && typeof v.typeId === "string" && typeof v.name === "string") || typeof fixture.sheetType !== "string") failPreflight("Native fixture types are invalid"); if (native.preflight.passed !== true || native.resourcesReleased !== true || native.guiUp?.[1] !== false || fixture.sheetType !== "Spreadsheet::Sheet" || fixture.objects.length !== 7) failPreflight("Native readiness or official seven-object fixture failed"); if (typeof native.documentCountBefore !== "number" || typeof native.documentCountAfter !== "number" || native.documentCountBefore !== native.documentCountAfter) failPreflight("Native document resources were not released"); const layout = await page.evaluate(() => { const app = document.querySelector("#app"), editor = document.querySelector("#bim-sql-editor"), help = document.querySelector("#bim-sql-help"); const box = app?.getBoundingClientRect(); return { appType: app?.constructor.name, editorType: editor?.constructor.name, helpType: help?.constructor.name, editable: editor instanceof HTMLElement ? editor.contentEditable : null, helpDisabled: help instanceof HTMLButtonElement ? help.disabled : null, overflow: document.documentElement.scrollWidth > innerWidth || document.documentElement.scrollHeight > innerHeight, inViewport: !!box && box.width > 0 && box.height > 0 && box.left >= 0 && box.top >= 0 && box.right <= innerWidth && box.bottom <= innerHeight }; }); observations.layout = layout; if (layout.editorType !== "HTMLDivElement" || layout.helpType !== "HTMLButtonElement" || layout.editable !== "true" || layout.helpDisabled !== false || layout.overflow || !layout.inViewport) failPreflight("Web command/editor or viewport prerequisites failed"); const docsShape = { clauseCount: docs.clauses.length, categories: Object.keys(docs.functions), functionCount: Object.values(docs.functions).reduce((n, items) => n + items.length, 0) }; fs.writeFileSync(path.join(evidenceRoot, `${viewport.name}${suffix}-preflight.json`), JSON.stringify({ observations, docsShape, passed: true }, null, 2) + "\n"); const tests = []; const run = async (name, task) => { const checks = []; const check = (name, actual, expected) => { assert.deepEqual(actual, expected, name); checks.push({ name, passed: true }); }; try { await task(check); tests.push({ name, passed: true, checks }); } catch (error) { tests.push({ name, passed: false, checks, error: String(error.message).slice(0,1000) }); } }; const help = page.locator("#bim-sql-help"), dialog = page.locator("#bim-sql-cheatsheet"), editor = page.locator("#bim-sql-editor"), tooltip = page.locator("#bim-sql-tooltip"); await run(reportGuiMethods[0], async check => { await help.click(); await dialog.waitFor({ state: "visible" }); const displayed = await dialog.evaluate(el => ({ elementType: el.constructor.name, openType: typeof el.open, open: el.open, title: el.querySelector("h1")?.textContent, clauses: el.querySelector(":scope > code")?.textContent, categories: [...el.querySelectorAll("h3")].map(node => node.textContent), functions: [...el.querySelectorAll("li[data-function]")].map(node => ({ name: node.getAttribute("data-function"), signature: node.querySelector("code")?.textContent, description: node.querySelector("div")?.textContent, snippet: node.querySelector("pre")?.textContent ?? null })), bounds: (() => { const b=el.getBoundingClientRect(); return { inViewport: b.left >= 0 && b.top >= 0 && b.right <= innerWidth && b.bottom <= innerHeight, horizontalOverflow: el.scrollWidth > el.clientWidth }; })() })); observations.dialogTypes = { element: displayed.elementType, open: displayed.openType, functions: type(displayed.functions) }; assert.equal(displayed.elementType, "HTMLDialogElement"); assert.equal(displayed.openType, "boolean"); assert.ok(Array.isArray(displayed.functions)); check("real Web dialog opens", displayed.open, true); check("desktop dialog title", displayed.title, "BIM SQL Cheatsheet"); check("all native clauses rendered", displayed.clauses, [...docs.clauses].sort().join(", ")); check("all native categories rendered", displayed.categories, Object.keys(docs.functions).sort()); const expectedFunctions = Object.keys(docs.functions).sort().flatMap(category => [...docs.functions[category]].sort((a,b) => a.name.localeCompare(b.name)).map(fn => ({ name:fn.name, signature:fn.signature, description:fn.description, snippet:fn.snippet || null }))); check("native signatures/descriptions/snippets rendered", displayed.functions, expectedFunctions); check("dialog fits viewport", displayed.bounds, { inViewport:true, horizontalOverflow:false }); await page.screenshot({ path:path.join(evidenceRoot, `${viewport.name}${suffix}-arch-report-gui-dialog.png`) }); await page.locator("#bim-sql-cheatsheet-ok").click(); check("OK closes dialog", await dialog.isVisible(), false); await help.click(); await page.keyboard.press("Escape"); check("Escape closes dialog", await dialog.isVisible(), false); check("focus returns to opener", await help.evaluate(el => document.activeElement === el), true); }); if (await dialog.isVisible()) await page.keyboard.press("Escape"); await run(reportGuiMethods[1], async check => { const convertDoc = Object.entries(docs.functions).flatMap(([category, list]) => list.map(fn => ({...fn, category}))).find(fn => fn.name === "CONVERT"); assert.ok(convertDoc && typeof convertDoc.signature === "string"); const word = name => editor.locator(`[data-word="${name}"]`).first(); await word("CONVERT").hover(); await tooltip.waitFor({ state:"visible" }); const text = await tooltip.textContent(); observations.tooltipType = typeof text; assert.equal(typeof text,"string"); check("official CONVERT signature", text.includes("CONVERT(quantity, 'unit')"), true); check("official Utility category", text.includes("Utility"), true); check("native description is displayed", text.includes(convertDoc.description), true); await page.screenshot({ path:path.join(evidenceRoot, `${viewport.name}${suffix}-arch-report-gui-tooltip.png`) }); await word("SELECT").hover(); check("official SELECT clause tooltip", await tooltip.textContent(), "SQL Clause"); await word("document".toUpperCase()).hover(); check("unknown word hides stale tooltip", await tooltip.isVisible(), false); await editor.evaluate(el => { el.__sqlEvents = []; for (const event of ["mouseover", "mouseleave", "focusin", "focusout", "input", "click"]) el.addEventListener(event, e => { el.__sqlEvents.push({event:e.type,target:e.target?.dataset?.word ?? e.target?.id,active:document.activeElement?.dataset?.word ?? document.activeElement?.id}); if(el.__sqlEvents.length>12)el.__sqlEvents.shift(); }); }); await editor.fill("select convert(Area, 'm^2') FROM document"); check("query stays editable", await editor.textContent(), "select convert(Area, 'm^2') FROM document"); if (viewport.name === "mobile") await word("CONVERT").tap(); else await word("CONVERT").focus(); observations.lowercaseHelp = await editor.evaluate(el => ({ events:el.__sqlEvents, active:document.activeElement?.dataset?.word ?? document.activeElement?.id, text:document.querySelector("#bim-sql-tooltip")?.textContent, hidden:document.querySelector("#bim-sql-tooltip")?.hidden })); const lowercaseText = await tooltip.textContent(); assert.equal(typeof lowercaseText, "string"); check("lowercase word supports touch/keyboard help", lowercaseText.includes(convertDoc.signature), true); await page.keyboard.press("Escape"); check("Escape dismisses tooltip", await tooltip.isVisible(), false); }); return { tests, observations, docsShape, passed:tests.length===2 && tests.every(test => test.passed) }; }