fuzzyworld

explodorama / files / test / reportReplay.test.ts

reportReplay.test.ts

Blame: the commit that last changed each line · file · history

351348d1 2026-11-04 Pavel Safronov1import { describe, expect, it } from 'vitest'
2import { spawnSync } from 'node:child_process'
3import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
4import { tmpdir } from 'node:os'
5import { join } from 'node:path'
6
7const script = new URL('../tools/report-replay.mjs', import.meta.url).pathname
8const mail = (record: object, qp = false) => {
9 const line = `Run record: ${JSON.stringify(record)}`
10 // Quoted-printable: = as =3D, a soft line break inside the record.
11 return qp ? `Content-Transfer-Encoding: quoted-printable\n\n${line.replace(/=/g, '=3D').replace(/(.{40})/g, '$1=\n')}\n` : `${line}\n`
12}
13const run = (body: string) => {
14 const dir = mkdtempSync(join(tmpdir(), 'report-replay-'))
15 const file = join(dir, 'mail.txt')
16 writeFileSync(file, body)
17 const r = spawnSync('node', [script, file], { encoding: 'utf8' })
18 rmSync(dir, { recursive: true })
19 return r
20}
21
22describe('tools/report-replay.mjs (Q103)', () => {
23 it('reads a record out of quoted-printable mail text and prints the replay address', () => {
24 const r = run(mail({ simVersion: 43, seed: 123456789, startLevel: 1, startCharge: null, options: { dev: false, immortal: false }, actions: [{ id: 'wait' }, { id: 'wait' }] }, true))
25 try {
26 expect(r.status).toBe(0)
27 expect(r.stdout).toContain('?replay=seed123456789-v43-2')
28 } finally {
29 const made = new URL('../replays/seed123456789-v43-2.json', import.meta.url)
30 if (existsSync(made)) rmSync(made)
31 }
32 })
33
34 it('refuses a record whose simVersion could steer the file name out of replays/', () => {
35 const r = run(mail({ simVersion: '/../../../../tmp/evil', seed: 1, actions: [] }))
36 expect(r.status).not.toBe(0)
37 expect(existsSync('/tmp/evil-0.json')).toBe(false)
38 expect(r.stderr).toContain('not a run record')
39 })
40
41 it('refuses --rules that is not a commit hash', () => {
42 const dir = mkdtempSync(join(tmpdir(), 'report-replay-'))
43 const file = join(dir, 'mail.txt')
44 writeFileSync(file, mail({ simVersion: 43, seed: 7, actions: [] }))
45 const r = spawnSync('node', [script, '--rules', '../../x', file], { encoding: 'utf8' })
46 rmSync(dir, { recursive: true })
47 expect(r.status).not.toBe(0)
48 expect(r.stderr).toContain('commit hash')
49 rmSync(new URL('../replays/seed7-v43-0.json', import.meta.url), { force: true })
50 })
51})