Audit pass: - Frame rule counts frames since the last move, so every step lasts exactly movesEvery(score) frames, also right after a speed-up - clampTime + MAX_GAP_MS: a hidden tab no longer replays the whole gap - Pong Restart keeps held keys - Fact corrections (mulberry32 output coverage, Haskell IO, uniform) - New fixture cases from a mutation analysis Extensions (all three tracks, shared fixtures, course text): - Turn queue (Exercise 10.6), persisted high score (10.7), replay viewer (10.8), play() end-to-end helper (9.5) - Optional State (4.7) and optional Module 7b, stack-safe IO - Module 12, property-based testing with generators, shrinking, invariants and a model-based oracle - Module 13, Conway's Life with a Click event - JS type checking via jsconfig, JSDoc and types.d.ts - Maintainer tools: scripts/mutate.mjs, scripts/typecheck-js.sh, Playwright end-to-end suite (e2e/, scripts/e2e.sh) Docs: docs/contract.md is the canonical contract; specs and build log under docs/superpowers. Verified: JS 728, Scala 689, F# 691 unit tests; stubs and progression M01-M13 and M07b; 176 end-to-end tests on Chromium and WebKit. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
85 lines
3.3 KiB
JavaScript
85 lines
3.3 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readdirSync, readFileSync } from 'node:fs'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { dirname, join, basename } from 'node:path'
|
|
|
|
const srcDir = join(dirname(fileURLToPath(import.meta.url)), '..', 'src')
|
|
|
|
// Files that are allowed to touch the outside world.
|
|
// The entry files (main.js, life/main.js, pong/main.js and the replay viewer's replay-main.js) are given browser glue.
|
|
const excluded = (file) => ['runtime.js', 'browser.js', 'main.js', 'replay-main.js'].includes(basename(file))
|
|
|
|
const banned = [
|
|
['let', /\blet\b/],
|
|
['var', /\bvar\b/],
|
|
['++', /\+\+/],
|
|
['--', /--/],
|
|
['.push(', /\.push\(/],
|
|
['.pop(', /\.pop\(/],
|
|
['.shift(', /\.shift\(/],
|
|
['.unshift(', /\.unshift\(/],
|
|
['.splice(', /\.splice\(/],
|
|
['.sort(', /\.sort\(/],
|
|
['.reverse(', /\.reverse\(/],
|
|
['Math.random', /\bMath\.random\b/],
|
|
['Date.now', /\bDate\.now\b/],
|
|
['performance.now', /\bperformance\.now\b/],
|
|
['document', /\bdocument\b/],
|
|
['window', /\bwindow\b/],
|
|
['addEventListener', /\baddEventListener\b/],
|
|
]
|
|
|
|
const stripComments = (code) => code.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '')
|
|
const violations = (code) => banned.filter(([, re]) => re.test(stripComments(code))).map(([name]) => name)
|
|
|
|
// Only JavaScript is scanned. src/types.d.ts (the optional type declarations) holds no code that runs, so it is skipped.
|
|
const jsFiles = (dir) =>
|
|
readdirSync(dir, { withFileTypes: true }).flatMap((e) =>
|
|
e.isDirectory() ? jsFiles(join(dir, e.name)) : e.name.endsWith('.js') ? [join(dir, e.name)] : [])
|
|
|
|
banned.forEach(([name]) =>
|
|
test(`purity scanner flags a known-bad sample using ${name}`, () => {
|
|
const samples = {
|
|
let: 'let n = 0',
|
|
var: 'var n = 0',
|
|
'++': 'n++',
|
|
'--': 'n--',
|
|
'.push(': 'xs.push(1)',
|
|
'.pop(': 'xs.pop()',
|
|
'.shift(': 'xs.shift()',
|
|
'.unshift(': 'xs.unshift(1)',
|
|
'.splice(': 'xs.splice(0, 1)',
|
|
'.sort(': 'xs.sort()',
|
|
'.reverse(': 'xs.reverse()',
|
|
'Math.random': 'const r = Math.random()',
|
|
'Date.now': 'const t = Date.now()',
|
|
'performance.now': 'const t = performance.now()',
|
|
document: 'document.title',
|
|
window: 'window.alert(1)',
|
|
addEventListener: 'x.addEventListener("click", f)',
|
|
}
|
|
assert.ok(violations(samples[name]).includes(name))
|
|
}))
|
|
|
|
test('purity scanner ignores banned words inside comments', () => {
|
|
const code = '// let var Math.random()\n/* xs.push(1); document; n++ */\nconst a = 1 // window\n'
|
|
assert.deepEqual(violations(code), [])
|
|
})
|
|
|
|
test('purity scanner accepts ordinary pure code', () =>
|
|
assert.deepEqual(violations('export const f = (xs) => xs.map((x) => x + 1)'), []))
|
|
|
|
jsFiles(srcDir)
|
|
.filter((f) => !excluded(f))
|
|
.forEach((f) =>
|
|
test(`purity: ${f.slice(srcDir.length + 1)} has no banned constructs`, () =>
|
|
assert.deepEqual(violations(readFileSync(f, 'utf8')), [])))
|
|
|
|
test('the purity audit skips .d.ts files', () =>
|
|
assert.deepEqual(jsFiles(srcDir).filter((f) => f.endsWith('.d.ts')), []))
|
|
|
|
// helpers.js is loaded by every test file; importing src/ there would make all tests fail at load time against the stubs.
|
|
test('test/helpers.js imports nothing from src/', () =>
|
|
assert.doesNotMatch(readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'helpers.js'), 'utf8'), /from\s+['"][^'"]*src\//))
|