/** Offline M3 captures from temporary snapshots; no live effects or network. */
import { spawnSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { zipSync, strToU8 } from 'fflate'
import { decodePng } from './bench/png'
const root = path.resolve(import.meta.dir, '..')
const out = path.join(root, 'out/m3')
const cache = mkdtempSync(path.join(tmpdir(), 'fmm-m3-'))
mkdirSync(out, { recursive: true })
writeFileSync(path.join(out, 'capture-cache.txt'), cache)
const save = (name: string, data: unknown) => {
const file = path.join(cache, name)
writeFileSync(file, JSON.stringify(data))
return file
}
const dep = (name: string, kind = 'required', comparator?: string, version?: string) => ({ name, kind, comparator, version })
const log = 'Version: 2.0.0\nDate: 2026-10-20\n Features:\n - Independent enable and disable choices.\n - Installed version selection.\n Bugfixes:\n - Preserve the owner’s pending choices.\n'
const local = (name: string, title: string, version: string, dependencies: ReturnType<typeof dep>[] = [], source = 'folder', changelog = '') => {
const file = path.join(cache, 'user/mods', source === 'zip' ? `${name}_${version}.zip` : `${name}_${version}`)
if (source === 'zip') {
mkdirSync(path.dirname(file), { recursive: true })
writeFileSync(file, zipSync({ [`${name}_${version}/info.json`]: strToU8(JSON.stringify({ name, version })), ...(changelog ? { [`${name}_${version}/changelog.txt`]: strToU8(changelog) } : {}) }))
} else {
mkdirSync(file, { recursive: true })
if (changelog) writeFileSync(path.join(file, 'changelog.txt'), changelog)
}
return { name, title, version, author: 'Fixture author', description: `${title}: offline M3 verification fixture.`, factorioVersion: '2.0', dependencies, source, path: file, thumbnail: null }
}
const builtins = ['base', 'elevated-rails', 'quality', 'space-age'].map(name => local(name, ({ base: 'Base mod', 'elevated-rails': 'Elevated Rails', quality: 'Quality', 'space-age': 'Space Age' })[name]!, '2.0.77', [], 'builtin'))
const library = local('factory-library', 'Factory Library', '1.0.0', [dep('quality')], 'folder', log)
const planner = local('factory-planner', 'Factory Planner', '2.0.0', [dep('factory-library'), dep('space-age')], 'zip', log)
const other = local('other-planner', 'Other Planner', '1.0.0')
const paths = { installDir: path.join(cache, 'install'), userDir: path.join(cache, 'user'), modsDir: path.join(cache, 'user/mods'), modListPath: path.join(cache, 'user/mods/mod-list.json'), gameVersion: '2.0.77' }
const snap = (mods: ReturnType<typeof local>[], active: string[]) => ({ paths, builtin: builtins, mods, scanErrors: [], modList: { mods: [...new Set([...builtins, ...mods].map(m => m.name))].map(name => ({ name, enabled: name === 'base' || active.includes(name) })) } })
const fixture = (name: string, snapshot: ReturnType<typeof snap>, select: string, extra: object = {}) => save(name + '.json', { snapshot, select, ...extra })
const normal = snap([planner, library, other], [])
const on = ['factory-planner', 'factory-library', 'quality', 'space-age']
const missingPlanner = { ...planner, dependencies: [dep('factory-library', 'required', '>=', '2.0'), dep('missing-helper', 'required', '>=', '1.2')] }
const conflictPlanner = { ...planner, dependencies: [dep('other-planner', 'incompatible')] }
const oldPlanner = local('factory-planner', 'Factory Planner', '1.0.0', [dep('factory-library')], 'folder', log.replaceAll('2.0.0', '1.0.0'))
const absentCatalogue = path.join(cache, 'absent.json')
const confirm = save('confirm.json', { title: 'Confirm installation', lines: ['Install missing-helper 1.2.0', 'Updates: factory-library 2.0.0', 'Required dependencies for Factory Planner'], confirm: true })
const cases: [string, string, string[]][] = [
['enabled-only', fixture('enabled', normal, planner.name, { toggles: [planner.name] }), []],
['disabled-only', fixture('disabled', snap([planner, library, other], on), library.name, { toggles: [library.name] }), []],
['missing', fixture('missing', snap([missingPlanner, library, other], on), planner.name), []],
['install-missing-confirm', fixture('missing-confirm', snap([missingPlanner, library, other], on), planner.name), ['--confirm-plan', confirm]],
['incompatible', fixture('conflict', snap([conflictPlanner, library, other], [planner.name, other.name]), other.name), []],
['version-dropdown', fixture('versions', snap([planner, oldPlanner, library, other], on), planner.name, { 'pin-choice': 2 }), ['--pin-dropdown-open', '1']],
['version-pinned', fixture('pinned', snap([planner, oldPlanner, library, other], on), planner.name, { 'pin-choice': 2 }), []],
['local-changelog', fixture('zip-log', snap([planner, library, other], on), planner.name), ['--changelog', 'true']],
['folder-changelog', fixture('folder-log', snap([planner, library, other], on), library.name), ['--changelog', 'true']],
['no-changelog', fixture('no-log', snap([planner, library, other], on), other.name), ['--changelog', 'true']],
]
let captures = ''
function capture(exe: string, name: string, file: string, args: string[]) {
const result = spawnSync(path.join(root, exe), ['--screenshot', path.join(out, name + '.png'), '--fixture', file, '--size', '1440x945', '--catalogue', absentCatalogue, ...args], { cwd: root, env: { ...process.env, FMM_CACHE_DIR: cache }, windowsHide: true, encoding: 'utf8' })
captures += `${name}: ${result.status}\n${result.stdout}${result.stderr}\n`
writeFileSync(path.join(out, 'captures.log'), captures)
if (result.error) throw result.error
if (result.status !== 0) throw new Error(captures)
console.log(name)
}
for (const [name, file, args] of cases) capture('target/release/fmm.exe', name, file, args)
const baseline = path.join(root, 'crates/app/tests/fixtures/r2b.json')
capture('out/m3/fmm-r7.exe', 'manage-r7', baseline, [])
capture('target/release/fmm.exe', 'manage', baseline, [])
const before = decodePng(readFileSync(path.join(out, 'manage-r7.png')))
const after = decodePng(readFileSync(path.join(out, 'manage.png')))
if (Buffer.compare(Buffer.from(before.data), Buffer.from(after.data))) throw new Error('Unchanged Manage fixture differs from R7')
console.log('Unchanged Manage fixture RGBA equals R7')