264 lines
19 KiB
JavaScript
264 lines
19 KiB
JavaScript
// The Quarto extension embeds what the playground shows: the whole playground or one view, arranged by the
|
|
// shortcode's options; the app reads those options from the embed's query (applyEmbedParams).
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { applyEmbedParams, emptyState, EMBED_VIEWS, parseRoute } from '../dist/core/state.js';
|
|
|
|
const ROOT = new URL('..', import.meta.url).pathname;
|
|
const EXT = ROOT + 'quarto/_extensions/pplus';
|
|
|
|
test('an embed query arranges the example the way the playground controls do', () => {
|
|
const base = { ...emptyState(), sources: { mojo: 'def main():\n pass', asm: 'ret' }, lang: 'pseudo' };
|
|
const s = applyEmbedParams(base, new URLSearchParams('lang=mojo&panel=compiler&build=default&machine=Zen%204&dock=closed&docktab=output&editor=0.6&side=0.4&dockheight=0.75&t=3'), 'hw');
|
|
assert.equal(s.lang, 'mojo');
|
|
assert.equal(s.view, 'compiler');
|
|
assert.equal(s.build, 'default');
|
|
assert.equal(s.machine, 'Zen 4');
|
|
assert.deepEqual(s.layout, { hw: { dockCollapsed: true, dockTab: 'output', split: 0.6, right: 0.4, dock: 0.75 } }, 'the arrangement is the area\'s');
|
|
assert.equal(s.t, 3);
|
|
assert.equal(base.layout, undefined, 'the state it was given is not changed');
|
|
});
|
|
|
|
test('a malformed embed query changes nothing', () => {
|
|
const base = emptyState();
|
|
const s = applyEmbedParams(base, new URLSearchParams('panes=klingon&area=nowhere&panel=nope&build=O3&editor=2&t=-1&lang=cobol&dock=maybe'));
|
|
assert.equal(s.view, base.view);
|
|
assert.equal(s.lang, base.lang);
|
|
assert.equal(s.build, undefined);
|
|
assert.equal(s.t, undefined);
|
|
assert.deepEqual(s.layout, {});
|
|
});
|
|
|
|
test('the playground and the Mojo views are embeddable routes', () => {
|
|
for (const v of ['playground', 'life', 'compiler']) assert.ok(EMBED_VIEWS.includes(v));
|
|
const r = parseRoute('#/embed/playground/p/demo?panel=life');
|
|
assert.equal(r.kind, 'embed');
|
|
assert.equal(r.view, 'playground');
|
|
assert.equal(r.problem, 'demo');
|
|
assert.equal(r.params.get('panel'), 'life');
|
|
});
|
|
|
|
test('check-pages knows every view the app embeds', () => {
|
|
const text = fs.readFileSync(ROOT + 'scripts/check-pages.mjs', 'utf8');
|
|
const listed = new Set([...(/const EMBED_VIEWS = new Set\(\[([^\]]+)\]\)/.exec(text)?.[1] ?? '').matchAll(/'([^']+)'/g)].map((m) => m[1]));
|
|
assert.deepEqual([...listed].sort(), [...EMBED_VIEWS].sort());
|
|
});
|
|
|
|
// the shortcode itself, run by pandoc's Lua with Quarto's few functions stubbed (skipped without pandoc)
|
|
const pandoc = spawnSync('pandoc', ['--version'], { encoding: 'utf8' });
|
|
const lua = pandoc.status === 0 && /pandoc 3/.test(pandoc.stdout);
|
|
|
|
function shortcode(calls, files) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pplus-qmd-'));
|
|
for (const [f, text] of Object.entries(files)) fs.writeFileSync(path.join(dir, f), text);
|
|
fs.writeFileSync(path.join(dir, 'calls.json'), JSON.stringify(calls));
|
|
const r = spawnSync('pandoc', ['lua', ROOT + 'tests/quarto/harness.lua', EXT, dir, path.join(dir, 'calls.json')], { encoding: 'utf8' });
|
|
assert.equal(r.status, 0, r.stderr);
|
|
return { out: r.stdout.trim().split('\n').map((l) => JSON.parse(l)), warnings: r.stderr };
|
|
}
|
|
const stateOf = (src) => JSON.parse(Buffer.from(/\/~([\w-]+)/.exec(src)[1], 'base64url').toString('utf8'));
|
|
|
|
const MOJO = '# origin: Programming+ demo\n# probe: a recursive function\n# mode: both\n# args: -O0 --debug-level=line-tables\n# expect: pass\n\ndef main():\n # note: this comment is the program\'s\n print(1)\n';
|
|
|
|
test('the shortcode embeds the whole playground from files, arranged as asked', { skip: !lua && 'pandoc 3 is not on the PATH' }, () => {
|
|
const { out } = shortcode([{ mojo: 'demo.mojo', asm: 'demo.asm', setup: 'demo.setup', view: 'playground', area: 'hw', panel: 'compiler', build: 'default', dock: 'open', editor: '0.6', title: 'Demo' }],
|
|
{ 'demo.mojo': MOJO, 'demo.asm': 'main:\n ret', 'demo.setup': '# main() takes no arguments' });
|
|
const [o] = out;
|
|
assert.match(o.src, /^https:\/\/pplus\.scient\.ing\/#\/embed\/playground\/~/);
|
|
assert.equal(o.height, '720', 'the playground gets a taller frame by default');
|
|
const st = stateOf(o.src);
|
|
assert.equal(st.sources.mojo, 'def main():\n # note: this comment is the program\'s\n print(1)', 'the corpus header is left out; a comment in the program is kept');
|
|
assert.equal(st.sources.asm, 'main:\n ret');
|
|
assert.equal(st.setup, '# main() takes no arguments');
|
|
assert.equal(st.view, 'compiler');
|
|
assert.equal(st.build, 'default');
|
|
assert.deepEqual(st.layout, { hw: { dockCollapsed: false, split: 0.6 } });
|
|
assert.equal(st.title, 'Demo');
|
|
assert.match(o.src, /\?area=hw&panel=compiler&build=default&dock=open&editor=0\.6$/);
|
|
});
|
|
|
|
test('the shortcode writes a state the app reads back whole, and lists matho as LaTeX', { skip: !lua && 'pandoc 3 is not on the PATH' }, async () => {
|
|
const { normalize } = await import('../dist/core/state.js');
|
|
const { out } = shortcode([{ pseudo: 'x.pseudo' }, { matho: 'm.tex', show: 'listing', math: '0' }], { 'x.pseudo': 'OUTPUT 1\n', 'm.tex': 'y = x^2\n' });
|
|
const st = stateOf(out[0].src);
|
|
assert.equal(st.v, normalize(st).v, 'the shortcode writes the current state format');
|
|
assert.equal(normalize(st).sources.pseudo, 'OUTPUT 1');
|
|
assert.deepEqual(out[1].codes, [{ lang: 'latex', text: 'y = x^2' }], 'pandoc highlights the listing as LaTeX');
|
|
});
|
|
|
|
test('the shortcode arranges a catalog problem and a state from Share, and refuses an unknown view', { skip: !lua && 'pandoc 3 is not on the PATH' }, () => {
|
|
const { out, warnings } = shortcode([
|
|
{ problem: 'demo', view: 'playground', panel: 'life', machine: 'Zen 4', theme: 'dark' },
|
|
{ state: 'https://pplus.scient.ing/#/bridge/s/~eyJ2IjoyfQ', view: 'life' },
|
|
{ mojo: 'demo.mojo', show: 'listing' },
|
|
{ mojo: 'demo.mojo', view: 'nonsense' },
|
|
{ mojo: 'demo.mojo', view: 'playground', height: 'auto' },
|
|
], { 'demo.mojo': MOJO });
|
|
assert.equal(out[0].src, 'https://pplus.scient.ing/#/embed/playground/p/demo?panel=life&machine=Zen%204&theme=dark');
|
|
assert.equal(out[1].src, 'https://pplus.scient.ing/#/embed/life/~eyJ2IjoyfQ', 'a whole share link is cut down to its state');
|
|
assert.deepEqual(out[2].codes, [{ lang: 'mojo', text: 'def main():\n # note: this comment is the program\'s\n print(1)' }]);
|
|
assert.match(out[3].src, /#\/embed\/trace\//);
|
|
assert.match(warnings, /unknown view="nonsense"/);
|
|
assert.equal(out[4].height, '720');
|
|
assert.match(warnings, /height="auto" does not apply/);
|
|
});
|
|
|
|
/* ---------- the family of shortcodes (quarto/_extensions/pplus/pplus.lua) ---------- */
|
|
|
|
const PSEUDO = '// CALL: COUNT_BASE(\'A\', "GATTACA")\n// EXPECT: COUNT_BASE returned 3\nFUNCTION COUNT_BASE(B : CHAR, T : STRING) RETURNS INTEGER\n DECLARE Total : INTEGER\n Total <- 0\n FOR I <- 0 TO LENGTH(T)\n IF T[I] == B THEN\n Total <- Total + 1\n ENDIF\n NEXT I\n RETURN Total\nENDFUNCTION\n';
|
|
const sc = (name, kw, meta = { 'pplus-cache': 'false' }) => ({ sc: name, kw, meta });
|
|
|
|
test('pplus.lua offers every shortcode the gallery and check-pages know, and no other', { skip: !lua && 'pandoc 3 is not on the PATH' }, async () => {
|
|
const { SHORTCODE_NAMES } = await import('../dist/quarto/shortcodes.js');
|
|
const r = spawnSync('pandoc', ['lua', '-e', `quarto = { doc = { input_file = '' } }; local t = dofile('${EXT}/pplus.lua'); local n = {} for k in pairs(t) do n[#n + 1] = k end table.sort(n) print(table.concat(n, ' '))`], { encoding: 'utf8' });
|
|
assert.equal(r.status, 0, r.stderr);
|
|
assert.deepEqual(r.stdout.trim().split(' '), [...SHORTCODE_NAMES].sort());
|
|
const check = fs.readFileSync(ROOT + 'scripts/check-pages.mjs', 'utf8');
|
|
const hwViews = [...(/const HW_VIEWS = \[([^\]]+)\]/.exec(check)?.[1] ?? '').matchAll(/'([^']+)'/g)].map((m) => m[1]);
|
|
const listed = [...(/const SHORTCODES = new Set\(\[([^\]]+)\]\)/.exec(check)?.[1] ?? '').matchAll(/'([^']+)'/g)].map((m) => m[1]).filter((n) => n.startsWith('pplus'));
|
|
assert.deepEqual([...listed, ...hwViews.map((v) => `pplus-hw-${v}`)].sort(), [...SHORTCODE_NAMES].sort(), 'scripts/check-pages.mjs knows every shortcode');
|
|
const { HW_NAMES } = await import('../dist/quarto/machine.js');
|
|
assert.deepEqual(hwViews, Object.keys(HW_NAMES), 'and every machine view, in the same order as pplus.lua');
|
|
const luaHw = [...(/local HW_VIEWS = \{([^}]+)\}/.exec(fs.readFileSync(EXT + '/pplus.lua', 'utf8'))?.[1] ?? '').matchAll(/"([^"]+)"/g)].map((m) => m[1]);
|
|
assert.deepEqual(luaHw, Object.keys(HW_NAMES));
|
|
const { STATIC_VIEWS } = await import('../dist/quarto/render.js');
|
|
const views = new Set([...(/const STATIC_VIEWS = new Set\(\[([^\]]+)\]\)/.exec(check)?.[1] ?? '').matchAll(/'([^']+)'/g)].map((m) => m[1]));
|
|
for (const v of STATIC_VIEWS) assert.ok(views.has(v), `check-pages knows pplus-view's view="${v}"`);
|
|
});
|
|
|
|
test('a rendered shortcode runs the file while the page renders: no iframe, the view in the page', { skip: !lua && 'pandoc 3 is not on the PATH' }, () => {
|
|
const { out, warnings } = shortcode([
|
|
sc('pplus-run', { pseudo: 'count.pseudo', code: '1', title: 'Count' }),
|
|
sc('pplus-steps', { pseudo: 'count.pseudo', t: '3' }),
|
|
sc('pplus-flow', { pseudo: 'count.pseudo', live: 'click' }),
|
|
sc('pplus-view', { view: 'dataflow', pseudo: 'count.pseudo' }),
|
|
], { 'count.pseudo': PSEUDO });
|
|
const [run, steps, flow, data] = out;
|
|
assert.equal(run.src, undefined, 'no iframe');
|
|
assert.ok(run.divs.includes('pplus-figure pplus-figure-run'));
|
|
assert.match(run.raw.join(''), /class="[^"]*pplus-static pplus-v-run[^"]*"/);
|
|
assert.match(run.raw.join(''), /COUNT_BASE returned 3/);
|
|
assert.match(run.raw.join(''), /pplus-figtitle">Count</);
|
|
assert.deepEqual(run.codes.map((c) => c.lang), ['pseudo'], 'code="1" puts the listing in the figure');
|
|
assert.doesNotMatch(run.codes[0].text, /CALL:|EXPECT:/, 'without its header lines');
|
|
assert.match(steps.raw.join(''), /<script type="application\/json" class="pplus-steps-data">/);
|
|
assert.match(steps.raw.join(''), /data-t="3"/);
|
|
assert.match(flow.raw.join(''), /<svg[^>]*class="flow-svg"/);
|
|
assert.match(flow.raw.join(''), /class="pplus-golive" data-src="https:\/\/pplus\.scient\.ing\/#\/embed\/flow\/~/, 'live="click" carries the live view to put in its place');
|
|
assert.match(data.raw.join(''), /pplus-v-data/);
|
|
assert.doesNotMatch(warnings, /pplus-run|pplus-steps|pplus-flow/);
|
|
});
|
|
|
|
test('a rendered run that does not print its EXPECT lines is reported, and stops the render with pplus-strict', { skip: !lua && 'pandoc 3 is not on the PATH' }, () => {
|
|
const wrong = PSEUDO.replace('returned 3', 'returned 4');
|
|
const { warnings } = shortcode([sc('pplus-run', { pseudo: 'w.pseudo' })], { 'w.pseudo': wrong });
|
|
assert.match(warnings, /pplus-run in index\.qmd: run: printed \["COUNT_BASE returned 3"\], its EXPECT lines say \["COUNT_BASE returned 4"\]/);
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pplus-qmd-'));
|
|
fs.writeFileSync(path.join(dir, 'w.pseudo'), wrong);
|
|
fs.writeFileSync(path.join(dir, 'calls.json'), JSON.stringify([sc('pplus-run', { pseudo: 'w.pseudo' }, { 'pplus-strict': 'true', 'pplus-cache': 'false' })]));
|
|
const r = spawnSync('pandoc', ['lua', ROOT + 'tests/quarto/harness.lua', EXT, dir, path.join(dir, 'calls.json')], { encoding: 'utf8' });
|
|
assert.notEqual(r.status, 0, 'the render stops');
|
|
assert.match(r.stderr, /does not print its EXPECT lines \(pplus-strict is on\)/);
|
|
});
|
|
|
|
test('without an engine a rendered view falls back to the listing and the live view', { skip: !lua && 'pandoc 3 is not on the PATH' }, () => {
|
|
const { out, warnings } = shortcode([sc('pplus-table', { pseudo: 'count.pseudo' }, { 'pplus-engine': 'none' })], { 'count.pseudo': PSEUDO });
|
|
assert.deepEqual(out[0].codes.map((c) => c.lang), ['pseudo']);
|
|
assert.match(out[0].src, /#\/embed\/table\/~/);
|
|
assert.match(warnings, /showing the live view instead/);
|
|
});
|
|
|
|
test('translate writes a tabset of code blocks; listing, tabs and link need no engine', { skip: !lua && 'pandoc 3 is not on the PATH' }, () => {
|
|
const { out } = shortcode([
|
|
sc('pplus-translate', { matho: 'mean.tex' }),
|
|
sc('pplus-listing', { pseudo: 'count.pseudo', numbers: '1' }),
|
|
sc('pplus-tabs', { pseudo: 'count.pseudo', matho: 'mean.tex' }),
|
|
sc('pplus-link', { pseudo: 'count.pseudo', text: 'Open it' }),
|
|
sc('pplus-playground', { pseudo: 'count.pseudo', panel: 'flow' }),
|
|
sc('pplus-embed', { pseudo: 'count.pseudo', view: 'life' }),
|
|
], { 'count.pseudo': PSEUDO, 'mean.tex': '% CALL: X_BAR(3, [1.0, 2.0, 6.0])\n\\bar{x} = \\frac{1}{n} \\sum_{i=0}^{n-1} x_i\n' });
|
|
const [tr, listing, tabs, link, playground, embed] = out;
|
|
assert.ok(tr.divs.some((d) => d.includes('panel-tabset')), 'a tabset (Quarto turns it into tabs)');
|
|
assert.deepEqual(tr.codes.map((c) => c.lang), ['latex', 'lingo', 'pseudo', 'mojo'], 'the file the page gave first, then what was made from it');
|
|
assert.match(tr.codes.find((c) => c.lang === 'pseudo').text, /FUNCTION X_BAR/);
|
|
assert.equal(tr.maths.length, 1, 'the equation typeset too');
|
|
assert.match(tr.raw.join(''), /pplus-tag-generated/);
|
|
assert.deepEqual(listing.codes, [{ lang: 'pseudo', text: PSEUDO.split('\n').slice(2).join('\n').trim() }]);
|
|
assert.ok(tabs.divs.some((d) => d.includes('panel-tabset')));
|
|
assert.match(link.links[0], /^https:\/\/pplus\.scient\.ing\/#\/bridge\/s\/~/);
|
|
assert.match(playground.src, /#\/embed\/playground\/~.*\?panel=flow$/);
|
|
assert.equal(playground.height, '720');
|
|
assert.match(embed.src, /#\/embed\/life\/~/);
|
|
});
|
|
|
|
test('in a format other than HTML a rendered view is Markdown', { skip: !lua && 'pandoc 3 is not on the PATH' }, () => {
|
|
process.env.PPLUS_FORMAT = 'latex';
|
|
try {
|
|
const { out } = shortcode([sc('pplus-run', { pseudo: 'count.pseudo' }), sc('pplus-flow', { pseudo: 'count.pseudo' })], { 'count.pseudo': PSEUDO });
|
|
assert.equal(Object.keys(out[0].raw).length, 0, 'no HTML'); // (an empty Lua table is {} in JSON)
|
|
assert.deepEqual(out[0].codes.map((c) => c.text), ['COUNT_BASE returned 3']);
|
|
assert.ok(out[0].links.length, 'and a link to the app');
|
|
assert.equal(Object.keys(out[1].raw).length, 0);
|
|
} finally { delete process.env.PPLUS_FORMAT; }
|
|
});
|
|
|
|
test('the Lua listing and the renderer\'s leave out the same header lines', { skip: !lua && 'pandoc 3 is not on the PATH' }, async () => {
|
|
const { listingText } = await import('../dist/quarto/program.js');
|
|
const files = { 'a.pseudo': PSEUDO, 'b.mojo': MOJO, 'c.tex': '% CALL: F(1)\n% EXPECT: F returned 1\ny = x\n', 'd.lingo': '// CALL: F(1)\nTo f x:\n The answer is x.\n' };
|
|
const { out } = shortcode([sc('pplus-listing', { pseudo: 'a.pseudo', mojo: 'b.mojo', matho: 'c.tex', lingo: 'd.lingo', math: '0' })], files);
|
|
const byLang = Object.fromEntries(out[0].codes.map((c) => [c.lang, c.text]));
|
|
assert.equal(byLang.pseudo, listingText(PSEUDO, 'pseudo'));
|
|
assert.equal(byLang.mojo, listingText(MOJO, 'mojo'));
|
|
assert.equal(byLang.latex, listingText(files['c.tex'], 'matho'));
|
|
assert.equal(byLang.lingo, listingText(files['d.lingo'], 'lingo'));
|
|
});
|
|
|
|
test('pplus-views.css is what `pixi run quarto-css` writes from styles.css', () => {
|
|
const r = spawnSync(process.execPath, [ROOT + 'scripts/quarto-css.mjs', '--check'], { encoding: 'utf8' });
|
|
assert.equal(r.status, 0, r.stderr || r.stdout);
|
|
});
|
|
|
|
test('the example site\'s programs run as their headers say, and its shortcodes name files that exist', () => {
|
|
const r = spawnSync(process.execPath, [ROOT + 'scripts/check-pages.mjs', ROOT + 'quarto/example'], { encoding: 'utf8', cwd: ROOT });
|
|
assert.equal(r.status, 0, r.stdout + r.stderr);
|
|
assert.match(r.stdout, /0 problems/);
|
|
const page = fs.readFileSync(ROOT + 'quarto/example/index.qmd', 'utf8') + fs.readFileSync(ROOT + 'quarto/example/live.qmd', 'utf8');
|
|
const used = new Set([...page.matchAll(/(?<!\{)\{\{<\s*(pplus(?:-[a-z]+)?)/g)].map((m) => m[1]));
|
|
const page2 = page + fs.readFileSync(ROOT + 'quarto/example/machine.qmd', 'utf8');
|
|
for (const m of page2.matchAll(/(?<!\{)\{\{<\s*(pplus(?:-[a-z]+)*)/g)) used.add(m[1]);
|
|
for (const name of ['pplus-run', 'pplus-steps', 'pplus-table', 'pplus-flow', 'pplus-dataflow', 'pplus-lifetimes', 'pplus-proof', 'pplus-tests', 'pplus-check', 'pplus-translate', 'pplus-machine', 'pplus-view', 'pplus-hw', 'pplus-hw-pipeline', 'pplus-hw-chip', 'pplus-hw-paging', 'pplus-hw-encoding', 'pplus-hw-counters', 'pplus-playground', 'pplus-embed', 'pplus-listing', 'pplus-tabs', 'pplus-link'])
|
|
assert.ok(used.has(name), `the example site uses ${name}`);
|
|
});
|
|
|
|
test('the machine: pplus-hw draws the board over an x86-64 run, and pplus-hw-<view> one card', { skip: !lua && 'pandoc 3 is not on the PATH' }, async () => {
|
|
const { problemById } = await import('../dist/core/catalog.js');
|
|
const p = problemById('asm-two-pointers');
|
|
const files = { 'two.asm': p.sources.asm, 'two.setup': p.setup };
|
|
const { out, warnings } = shortcode([
|
|
sc('pplus-hw', { asm: 'two.asm', setup: 'two.setup', t: '9' }),
|
|
sc('pplus-hw-cache', { asm: 'two.asm', setup: 'two.setup', listing: '0' }),
|
|
sc('pplus-hw', { asm: 'two.asm', setup: 'two.setup', views: 'registers,nonsense', column: 'body' }),
|
|
sc('pplus-hw-registers', { pseudo: 'count.pseudo' }),
|
|
], { ...files, 'count.pseudo': PSEUDO });
|
|
const [board, cache, named, noAsm] = out;
|
|
const html = board.raw.join('');
|
|
assert.ok(board.divs.includes('pplus-figure pplus-figure-hw column-page'), 'the board is page-wide');
|
|
for (const card of ['registers', 'flags', 'address', 'memory', 'stack']) assert.match(html, new RegExp(`data-hw="${card}"`), `the first preset has ${card}`);
|
|
assert.match(html, /pplus-hw-now">Step 9: mov r11, qword ptr/);
|
|
assert.match(html, /<div class="a86 board-a86" data-hw-frame="">\s*<section class="a86-panel/, 'the opening step\'s drawing is in the markup');
|
|
assert.match(html, /<script type="application\/x-pplus-deflate" class="pplus-hw-data">[A-Za-z0-9+/=]+<\/script>/, 'every step, compressed');
|
|
assert.match(html, /href="https:\/\/pplus\.scient\.ing\/#\/hw\/s\/~/, 'open in the playground goes to Hardware');
|
|
const cacheHtml = cache.raw.join('');
|
|
assert.ok(cache.divs.includes('pplus-figure pplus-figure-hw'), 'one card is as wide as the text');
|
|
assert.match(cacheHtml, /data-hw="cache"/);
|
|
assert.doesNotMatch(cacheHtml, /pplus-hw-listing/);
|
|
assert.ok(!named.divs.some((d) => d.includes('column-')));
|
|
assert.match(warnings, /pplus-hw in index\.qmd: hw: no machine view "nonsense"/);
|
|
assert.match(noAsm.raw.join(''), /run an x86-64 file/);
|
|
assert.match(warnings, /pplus-hw-registers in index\.qmd: hw: no x86-64 file/);
|
|
});
|