LLMON-Attention-Visualization / test_flow_static.cjs
abedaniels's picture
feat: make the landing page just the header and the about card
ee13518
Raw History Blame Contribute Delete
4.3 kB
// Offline checks: production components render, routes are focused, layer indices stay real.
// These do not replace the browser layout and interaction suites.
process.env.NODE_ENV = 'production';
const assert = require('node:assert/strict');
const { build } = require('esbuild');
const { readFile } = require('node:fs/promises');
const React = require('react');
const { renderToStaticMarkup } = require('react-dom/server');
async function compile(contents) {
const result = await build({ stdin: { contents, loader: 'jsx', resolveDir: __dirname },
bundle: true, write: false, platform: 'node', format: 'cjs', packages: 'external' });
const module = { exports: {} };
new Function('require', 'module', 'exports', result.outputFiles[0].text)(require, module, module.exports);
return module.exports;
}
(async () => {
const { chooseInspectionLayer } = await import('./frontend/flow-utils.mjs');
const forty = Array.from({ length: 40 }, (_, i) => i);
assert.equal(chooseInspectionLayer(forty), 31);
for (const previous of [0, 12, 39]) assert.equal(chooseInspectionLayer(forty, previous), previous);
assert.equal(chooseInspectionLayer([0, 1, 2, 3], 31), 3);
assert.equal(chooseInspectionLayer([0, 5, 39]), 39);
assert.equal(chooseInspectionLayer([0]), 0);
assert.equal(chooseInspectionLayer([]), null);
// An absent or invalid previous selection must never turn into an unsupported layer.
for (const available of [forty, [0, 1], [2, 5], [31], [39]]) {
for (const previous of [null, 0, 31, 39, 400]) assert(available.includes(chooseInspectionLayer(available, previous)));
}
// Load Carbon in its server environment before supplying the page's width.
require('@carbon/react');
global.window = { innerWidth: 1280 };
for (const file of ['index.html', 'how-it-works/index.html']) {
const html = await readFile(`frontend/${file}`, 'utf8');
const source = html.match(/<script type="text\/babel">([\s\S]*?)<\/script>/)[1];
const appSource = source.slice(0, source.lastIndexOf(file === 'index.html' ? ' const root = ReactDOM.createRoot' : ' ReactDOM.createRoot'));
const { App } = await compile(`import React from 'react'; import IBMHeader from './frontend/Header.jsx';
import { readPalettePreference, savePalettePreference } from './frontend/data-utils.js';\n${appSource}\nexport { App };`);
for (const width of [1280, 320]) {
window.innerWidth = width;
const rendered = renderToStaticMarkup(React.createElement(App));
assert(!/experiment_[13]|Layer Focus|Failure Diagnosis|Mamba|hatched|IFR|DAS|LEAK/.test(rendered));
assert(!rendered.includes('Generation Flow 2'));
assert(rendered.includes('Generation Flow') && rendered.includes('How it works'));
if (file === 'index.html') {
// The landing is the header plus the about card; its two page links
// are the about card's view cards and the header actions.
const links = [...rendered.matchAll(/href="(\.\/(?:how-it-works|experiment_2)\/index\.html[^"]*)"/g)].map(m => m[1]);
assert.deepEqual(
[...new Set(links.map(l => (l.includes('?') ? l.slice(0, l.indexOf('?')) : l)))].sort(),
['./experiment_2/index.html', './how-it-works/index.html']);
assert(!rendered.includes('More options') ? true : rendered.includes('How it works'));
} else {
assert(rendered.includes('Yes, the model sees the selection rule too.'));
assert(rendered.includes('Curve weights are not model measurements.'));
assert(rendered.includes('32nd layer'));
}
}
}
const { ExampleContext } = await compile("export { ExampleContext } from './frontend/Guidance.jsx';");
const custom = renderToStaticMarkup(React.createElement(ExampleContext, {
exampleId: 0, customPrompt: '<|open|>instr<|close|><script>unsafe</script>', showSelection: true,
}));
assert(custom.includes('Uploaded example'));
assert(!custom.includes('Expected answer:'));
assert(!custom.includes('<script>'));
assert(custom.includes('&lt;script&gt;unsafe&lt;/script&gt;'));
console.log('PASS: real layer selection; landing/guide render at desktop/mobile widths; uploaded prompt escaping and no borrowed reference answer.');
})().catch(error => { console.error(error); process.exitCode = 1; });