Files
pplus-web/tests/proveclock.test.mjs
T
alexm 75c1d466da
Build, test, deploy / site (push) Successful in 7m38s
A proved routine is not said to have run out of time
In a container running the tests on two processors, the Quarto
gallery's proved example was rendered with the warning that time ran
out: the generated inputs that cross-check a proof had been cut short by
the 3-second clock, with every claim already proved. core 941af6b counts
that only where a claim is still open. The tests that hold what a proof
finds give it time to spare, and the browser-side request builder takes
prove-seconds as the Lua filter does.
2026-11-13 07:33:35 -05:00

92 lines
4.8 KiB
JavaScript

// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// The time a proof may take is the reader's to set (src/core/proveclock.ts): the Prove view says when the clock, not
// the proof, stopped the checker, offers more time, and proves again; a page rendered with Quarto takes its time
// from the shortcode or the site, and warns its author when it ran out.
import test from 'node:test';
import assert from 'node:assert/strict';
import { installDocument } from '../dist/quarto/dom.js';
installDocument();
const { PROVE_CLOCKS, DEFAULT_PROVE_CLOCK, loadProveClock, saveProveClock, longerProveClock, clockWords } = await import('../dist/core/proveclock.js');
const { Session } = await import('../dist/core/session.js');
const { problemById, stateForProblem } = await import('../dist/core/catalog.js');
const { proofView } = await import('../dist/ui/views/panels.js');
const { renderStatic } = await import('../dist/quarto/render.js');
const memory = () => { const m = new Map(); return { getItem: (k) => (m.has(k) ? m.get(k) : null), setItem: (k, v) => { m.set(k, String(v)); }, removeItem: (k) => { m.delete(k); }, m }; };
const hooks = (live) => ({ reveal() {}, fix() {}, live });
const draw = (session, live) => { const root = document.createElement('div'); proofView(session, root, hooks(live)); return root; };
const proved = () => new Session(stateForProblem(problemById('count-base-proved')), { storageId: null });
test('the setting is one of the listed times, 3 seconds unless the reader chose another, and kept under a pplus name', () => {
assert.equal(DEFAULT_PROVE_CLOCK, 3000);
assert.deepEqual(PROVE_CLOCKS.map((c) => c.ms), [3000, 10000, 30000, 120000]);
const store = memory();
assert.equal(loadProveClock(store), 3000);
assert.equal(saveProveClock(30000, store), 30000);
assert.deepEqual([...store.m.keys()], ['pplus.proveclock']);
assert.equal(loadProveClock(store), 30000);
// anything that is not a listed time is the default, stored or asked for
store.setItem('pplus.proveclock', '99999999');
assert.equal(loadProveClock(store), 3000);
assert.equal(saveProveClock(7, store), 3000);
assert.equal(store.m.size, 0, 'the default is kept by keeping nothing');
assert.equal(loadProveClock(null), 3000);
assert.equal(longerProveClock(3000), 10000);
assert.equal(longerProveClock(120000), null);
assert.equal(clockWords(120000), '2 minutes');
assert.equal(clockWords(1500), '1.5 seconds');
});
test('a proof the clock stopped says so, offers the next longer time, and is proved again with it', () => {
const s = proved();
assert.equal(s.proofClock(), 3000);
// a clock that has already run out, which is what 3 seconds are on a slow or busy device
s.proofBudgetMs = -1;
const cut = s.proof().report;
assert.ok(cut.clock.stopped.length >= 1, JSON.stringify(cut.clock));
assert.equal(cut.counts.broken, 0);
const view = draw(s, true);
const notice = view.querySelector('.proof-timeout');
assert.match(notice.textContent, /Time ran out on .*shown open, not disproved/);
assert.match(notice.querySelector('button').textContent, /^Prove again with 3 seconds$/);
assert.ok(view.querySelector('select#proof-clock'), 'and the setting itself is in the view');
// the setter proves again and tells the views
let told = 0;
s.onChange((what) => { if (what === 'run') told++; });
s.setProofClock(10000);
assert.equal(s.proofClock(), 10000);
assert.equal(told, 1);
assert.equal(s.proof().report.clock.budgetMs, 10000, 'proved again, with the new time');
assert.equal(draw(s, true).querySelector('select#proof-clock').value, '10000');
// what enough time gives is held with no clock at all, so this does not depend on how fast or busy the machine is
s.proofBudgetMs = Infinity;
const whole = s.proof().report;
assert.deepEqual(whole.clock.stopped, []);
assert.ok(whole.counts.proved > cut.counts.proved);
assert.equal(draw(s, true).querySelector('.proof-timeout'), null);
s.proofBudgetMs = 10000;
s.setProofClock(10000);
assert.equal(told, 1, 'the same time again is no change');
s.setProofClock(3000);
});
test('a view that is not live (a page rendered with Quarto) says the time ran out and offers no control', () => {
const s = proved();
s.proofBudgetMs = -1;
const view = draw(s, false);
assert.match(view.querySelector('.proof-timeout').textContent, /Time ran out/);
assert.equal(view.querySelector('button.btn'), null);
assert.equal(view.querySelector('select'), null);
});
test('a rendered proof takes its time from the request, and has no warning when the time was enough', () => {
const p = problemById('count-base-proved');
const files = { pseudo: p.sources.pseudo, speco: p.sources.speco };
const r = renderStatic({ view: 'proof', files, proveSeconds: 600 });
assert.match(r.html, /[1-9]\d* proved/);
assert.doesNotMatch(r.html, /Time ran out/);
assert.deepEqual(r.warnings, []);
});