Files
functional-programming-snake/js/test/helpers.js
T

26 lines
1.1 KiB
JavaScript

import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import { states, classicActions, render } from '../src/app.js'
import { map as mapStream } from '../src/stream.js'
import { map as mapIO, randomSeed } from '../src/io.js'
const here = dirname(fileURLToPath(import.meta.url))
/** Load fixtures/<name>.json as { function, cases: [{ name, args, expected }] }. */
export const loadFixture = (name) =>
JSON.parse(readFileSync(join(here, '..', '..', 'fixtures', `${name}.json`), 'utf8'))
/** Deep-freeze for tests (independent of the student's deepFreeze). Recurses into children even of already-frozen objects. */
export const freeze = (x, seen = new WeakSet()) => {
if (x !== null && typeof x === 'object' && !seen.has(x)) {
seen.add(x)
Object.freeze(x)
Object.values(x).forEach((v) => freeze(v, seen))
}
return x
}
/** The m09 program built from classicActions (ticks every 150 ms). The shipped `main` switches to speedActions in m10. */
export const classicMain = mapIO(randomSeed, (seed) => mapStream(states(seed, classicActions), render))