// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 // 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. Without pandoc 3 on the PATH these // are skipped on a developer's machine. Under CI (the CI variable set, as Gitea and GitHub Actions set it) they are // not skipped: they run and fail, because a skip in CI is how a gap hides (eleven of these were skipped in CI for // weeks, until pixi brought pandoc to the runner). pixi run test brings pandoc. const pandoc = spawnSync('pandoc', ['--version'], { encoding: 'utf8' }); const lua = pandoc.status === 0 && /pandoc 3/.test(pandoc.stdout); const NO_PANDOC = !lua && !process.env.CI && 'pandoc 3 is not on the PATH'; const needPandoc = () => assert.ok(lua, 'pandoc 3 is not on the PATH, and CI must run the Quarto tests: run them as pixi run test, which brings the pinned pandoc'); 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)); needPandoc(); 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: NO_PANDOC }, () => { 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: NO_PANDOC }, 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: NO_PANDOC }, () => { 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: NO_PANDOC }, async () => { const { SHORTCODE_NAMES } = await import('../dist/quarto/shortcodes.js'); needPandoc(); 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: NO_PANDOC }, () => { 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 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(''), /