/** M8 fixture-only headless Rust captures. No requests use the live transport. */
import { spawnSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
const root = path.resolve(import.meta.dir, '..')
const out = path.join(root, 'out/m8')
const temp = mkdtempSync(path.join(tmpdir(), 'fmm-m8-'))
for (const dir of [out, path.join(temp, 'settings'), path.join(temp, 'cache'), path.join(temp, 'metadata')]) mkdirSync(dir, { recursive: true })
const data = path.join(temp, 'Factorio')
const install = path.join(temp, 'install')
mkdirSync(path.join(data, 'mods'), { recursive: true })
mkdirSync(path.join(install, 'data/base'), { recursive: true })
writeFileSync(path.join(data, 'mods/mod-list.json'), '{"mods":[]}')
writeFileSync(path.join(install, 'data/base/info.json'), '{"version":"2.0.77"}')
writeFileSync(path.join(temp, 'settings/settings.json'), JSON.stringify({ dataDir: data, installDir: install }))
const generated = path.join(temp, 'images')
const prepare = spawnSync('bun', ['tools/msvc.ts', 'cargo', 'run', '-p', 'fmm_core', '--example', 'm8_images', '--', generated], { cwd: root, encoding: 'utf8', windowsHide: true, timeout: 600_000 })
if (prepare.error) throw prepare.error
if (prepare.status !== 0) throw new Error(prepare.stdout + prepare.stderr)
const save = (name: string, data: unknown) => {
const file = path.join(temp, name + '.json')
writeFileSync(file, JSON.stringify(data))
return file
}
const fixture = save('fixture', { rows: [], 'detail-title': 'Offline inline images' })
const skewer = JSON.parse(readFileSync(path.join(root, 'crates/app/tests/fixtures/skewer-ariabot-full.json'), 'utf8')) as { name: string, description: string }
const manifest: Record<string, string> = {}
const urls = [...skewer.description.matchAll(/!\[.*?\]\((https:\/\/[^\s)]+)/g)].map(m => m[1]!)
urls.forEach((url, i) => { manifest[url] = path.join(generated, `aria-${i}.gif`) })
const mixed = {
name: 'm8-mixed', title: 'Inline images: mixed formats', owner: 'Offline fixture',
summary: 'Natural sizing, wrapping and failures',
description: 'Small PNG (40 × 24, natural size):\n\n\n\nOver-wide JPEG (1600 × 400, shrunk):\n\n\n\nWebP, failed image, and linked badge:\n\n  [](https://example.com/donate)',
releases: [{ version: '1.0.0', info_json: { factorio_version: '2.0' } }],
}
for (const file of ['small.png', 'wide.jpg', 'test.webp', 'badge.png']) manifest['https://example.com/' + file] = path.join(generated, file)
const images = save('images', manifest)
const mixedFull = save('mixed-full', mixed)
const catalogue = save('catalogue', { results: [skewer, mixed] })
let log = ''
function capture(name: string, full: string, mod: string, size: string, args: string[] = []) {
const result = spawnSync(path.join(root, 'target/release/fmm.exe'), [
'--screenshot', path.join(out, name + '.png'), '--fixture', fixture, '--size', size,
'--catalogue', catalogue, '--tab', 'explore', '--portal-full', full, '--explore-select', mod,
'--description-images', images, ...args,
], { cwd: root, env: { ...process.env, FACTORIO_DIR: '', FMM_CACHE_DIR: path.join(temp, 'cache'), FMM_SETTINGS_DIR: path.join(temp, 'settings'), FMM_METADATA_DIR: path.join(temp, 'metadata') }, encoding: 'utf8', windowsHide: true, timeout: 600_000 })
log += `${name}: ${result.status}\n${result.stdout}${result.stderr}\n`
writeFileSync(path.join(out, 'captures.log'), log)
if (result.error) throw result.error
if (result.status !== 0) throw new Error(log)
console.log(name)
}
const skewerFull = path.join(root, 'crates/app/tests/fixtures/skewer-ariabot-full.json')
for (const [size, suffix] of [['1000x600', 'minimum'], ['1600x1000', 'wide']] as const) {
for (const frame of [0, 1]) capture(`m8-skewer-${suffix}-frame-${frame}`, skewerFull, skewer.name, size, ['--description-frame', String(frame), '--detail-scroll', suffix === 'minimum' ? '420' : '0'])
if (readFileSync(path.join(out, `m8-skewer-${suffix}-frame-0.png`)).equals(readFileSync(path.join(out, `m8-skewer-${suffix}-frame-1.png`)))) throw new Error('GIF capture frames are identical')
capture(`m8-mixed-${suffix}`, mixedFull, mixed.name, size, ['--description-frame', '0', '--detail-scroll', suffix === 'minimum' ? '320' : '160'])
capture(`m8-settings-${suffix}`, mixedFull, mixed.name, size, ['--settings-open', '1', '--description-frame', '0'])
capture(`m8-loading-${suffix}`, skewerFull, skewer.name, size, ['--description-loading', '1', '--description-frame', '0', '--detail-scroll', suffix === 'minimum' ? '420' : '0'])
}
console.log(`Captures: ${out}\nIsolated fixtures: ${temp}`)