// The rendered views of the Quarto extension (src/quarto/): a program from the files beside a page, run by the app's // engines and drawn by its views as HTML, under the small document of src/quarto/dom.ts; the CLI pplus.lua calls // (quarto/_extensions/pplus/render.mjs). The gallery that shows them in the app is tests/quarto-gallery.test.mjs. 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 zlib from 'node:zlib'; import { installDocument, SDocument } from '../dist/quarto/dom.js'; import { programHeader, listingText, langOfFile } from '../dist/quarto/program.js'; installDocument(); const { renderStatic, STATIC_VIEWS, stepsPanel } = await import('../dist/quarto/render.js'); const { problemById } = await import('../dist/core/catalog.js'); const ROOT = new URL('..', import.meta.url).pathname; const CB = problemById('count-base'); const head = (call, expect) => `// CALL: ${call}\n${expect ? `// EXPECT: ${expect}\n` : ''}`; const PSEUDO = head(CB.call, 'COUNT_BASE returned 3') + CB.sources.pseudo; test('a program file: its pane, its header lines, and the text a page shows', () => { assert.equal(langOfFile('x/count.pseudo'), 'pseudo'); assert.equal(langOfFile('mean.tex'), 'matho'); assert.equal(langOfFile('demo.mojo'), 'mojo'); assert.equal(langOfFile('notes.txt'), null); const h = programHeader('// CALL: F(1)\n// INPUT: 4\n// INPUT: 5\n// EXPECT: F returned 2\n// STOPS: index\nFUNCTION F...'); assert.deepEqual(h, { call: 'F(1)', input: ['4', '5'], expect: ['F returned 2'], stops: 'index', questions: [] }); assert.deepEqual(programHeader('% CALL: X(1)\n% EXPECT: X returned 1\n').expect, ['X returned 1']); assert.deepEqual(programHeader('# expect-stdout: 3\n# expect-stdout: 4\ndef main():\n pass\n').expect, ['3', '4'], 'a Mojo file\'s corpus header counts as EXPECT'); assert.equal(listingText('// CALL: F(1)\n\nFUNCTION F()\n', 'pseudo'), 'FUNCTION F()'); assert.equal(listingText('# origin: x\n# expect-stdout: 1\n\ndef main():\n # note: mine\n print(1)\n', 'mojo'), 'def main():\n # note: mine\n print(1)'); }); test('the small document builds, finds and writes what the views make', () => { const doc = new SDocument(); const div = doc.createElement('div'); div.className = 'a b'; div.classList.add('c'); div.classList.toggle('a'); div.style.left = '10%'; div.style.setProperty('--x', '1'); div.dataset.stepNo = '4'; const svg = doc.createElementNS('http://www.w3.org/2000/svg', 'svg'); svg.setAttribute('viewBox', '0 0 1 1'); svg.appendChild(doc.createElementNS('http://www.w3.org/2000/svg', 'path')); const p = doc.createElement('p'); p.textContent = 'x < y & "z"'; const sel = doc.createElement('select'); for (const v of ['one', 'two']) { const o = doc.createElement('option'); o.value = v; o.textContent = v; sel.appendChild(o); } sel.value = 'two'; const details = doc.createElement('details'); details.open = true; div.append(svg, p, sel, details); assert.equal(div.outerHTML, '

x < y & "z"

'.replace(' selected=""', '')); assert.equal(div.querySelectorAll('svg path').length, 1, 'the descendant combinator'); assert.equal(div.querySelector('[data-step-no="4"]'), null, 'the root itself is not searched'); assert.equal(div.querySelectorAll('.b, p').length, 1); assert.equal(p.closest('.c'), div); assert.throws(() => div.querySelector('div > p'), /only the descendant combinator/); p.replaceWith(doc.createTextNode('gone')); assert.equal(div.textContent.includes('gone'), true); }); test('run: what the program prints, checked against its EXPECT lines, for each notation', () => { const r = renderStatic({ view: 'run', files: { pseudo: PSEUDO } }); assert.equal(r.lang, 'pseudo'); assert.deepEqual(r.expect, { want: ['COUNT_BASE returned 3'], got: ['COUNT_BASE returned 3'], ok: true }); assert.match(r.html, /prints the 1 line its EXPECT header says/); assert.match(r.markdown, /```\nCOUNT_BASE returned 3\n```/); const wrong = renderStatic({ view: 'run', files: { pseudo: PSEUDO.replace('returned 3', 'returned 9') } }); assert.equal(wrong.expect.ok, false); assert.match(wrong.warnings.join('\n'), /its EXPECT lines say \["COUNT_BASE returned 9"\]/); const lingo = renderStatic({ view: 'run', files: { lingo: CB.sources.lingo }, call: 'COUNT_BASES(\'A\', "GATTACA")' }); assert.deepEqual(lingo.expect, undefined); assert.match(lingo.html, /COUNT_BASES returned 3/); const mojo = renderStatic({ view: 'run', files: { mojo: '# EXPECT: 3\n' + CB.sources.mojo } }); assert.equal(mojo.lang, 'mojo'); assert.ok(mojo.expect.ok); assert.match(mojo.html, /not the compiler/, 'the interpreter says it is not the compiler'); const compiled = renderStatic({ view: 'run', files: { mojo: CB.sources.mojo }, compiled: { stdout: '3\n', stderr: '', status: 0, version: 'Mojo 1.1.0' } }); assert.match(compiled.html, /Mojo 1\.1\.0, on the machine that rendered this page/); const broken = renderStatic({ view: 'run', files: { pseudo: 'FUNCTION F() RETURNS INTEGER\n RETURN Nope\nENDFUNCTION\n' } }); assert.match(broken.html, /pplus-err/); assert.ok(broken.warnings.length); }); test('steps: one row per step of the run, the values after it, and the page drawn at the step asked for', () => { const r = renderStatic({ view: 'steps', files: { pseudo: PSEUDO }, t: 5 }); const json = /