/** Offline R7 captures: fixture-only, OS temporary cache, no network or live operations. */
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 { decodePng, encodePng } from './bench/png'
const root = path.resolve(import.meta.dir, '..')
const out = path.join(root, 'out/r7')
const cache = mkdtempSync(path.join(tmpdir(), 'fmm-r7-'))
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 release = (version: string, dependencies = ['base >= 2.0']) => ({ version, released_at: '2026-10-19T00:00:00Z', sha1: '1'.repeat(40), info_json: { factorio_version: '2.0', dependencies } })
const mods = [
['new-mod', 'Factory Planner', 'Plan production lines and their dependencies across the entire factory.'],
['RateCalculator', 'Rate Calculator with an intentionally very long title to verify progress bar elision', 'Calculate production and consumption rates for selected machines.'],
['bottleneck', 'Bottleneck', 'Show which machines are limiting production and explain the bottleneck.'],
['folder-work', 'My local mod', 'Folder mods can now be updated with the same safe backup process.'],
].map(([name, title, summary]) => ({ name, title, summary, owner: 'Factorio community', downloads_count: 123456, category: 'utilities', latest_release: release(name === 'bottleneck' ? '0.11.8' : '4.0.0'), releases: [release(name === 'bottleneck' ? '0.11.8' : '4.0.0')] }))
const catalogue = save('catalogue.json', { results: mods })
const fixtureData = JSON.parse(readFileSync(path.join(root, 'crates/app/tests/fixtures/r2b.json'), 'utf8'))
fixtureData.rows.forEach((row: { selected?: boolean }) => { row.selected = false })
fixtureData.rows.push({ name: 'folder-work', title: 'My local mod', version: '1.0.0', checked: true, selected: true })
fixtureData.rows.find((row: { name: string }) => row.name === 'RateCalculator').title = 'Rate Calculator with an intentionally very long title to verify progress bar elision'
fixtureData['detail-title'] = 'My local mod'
const fixture = save('fixture.json', fixtureData)
const key = (url: string) => [...Buffer.from(url)].reduce((h, b) => BigInt.asUintN(64, (h ^ BigInt(b)) * 0x100000001b3n), 0xcbf29ce484222325n).toString(16).padStart(16, '0')
mkdirSync(path.join(cache, 'portal-thumbs'), { recursive: true })
const images = Array.from({ length: 8 }, (_, i) => {
const url = `https://assets-mod.factorio.com/r7-fixture-${i}.png`
const data = new Uint8Array(240 * 144 * 4)
for (let y = 0; y < 144; y++) for (let x = 0; x < 240; x++) data.set([60 + i * 20, 60 + (x >> 4) * 7, 40 + (y >> 4) * 13, 255], (y * 240 + x) * 4)
writeFileSync(path.join(cache, `portal-thumbs/${key(url)}.png`), encodePng({ width: 240, height: 144, data }))
return { thumbnail: url, url }
})
const full = save('full.json', { ...mods[0], description: 'Plan production lines with **verified downloads** and safe backups.', images, releases: [release('4.0.0', ['base >= 2.0', 'space-age >= 2.0', 'RateCalculator >= 3.0', 'helper >= 1.0', '? art', '! bottleneck', '(?) hidden'])] })
const gallery = save('gallery.json', { ...mods[0], description: 'Gallery wheel fixture.\n\n' + 'Paragraph below the gallery.\n\n'.repeat(40), images })
const bold = save('bold.json', { ...mods[0], description: readFileSync(path.join(root, 'crates/app/tests/fixtures/alien-biomes-planet-controls.md'), 'utf8'), images: [], releases: [release('4.0.0', [])] })
const dialog = save('plan.json', { title: 'Confirm installation', lines: ['Install RenaiTransportation 2.2.17', 'Optional dependencies (not downloaded): ? space-age >= 1.0.0'], confirm: true })
const common = ['--fixture', fixture, '--size', '1440x945', '--catalogue', catalogue]
const explore = ['--tab', 'explore']
const selected = ['--explore-select', 'new-mod', '--portal-full', full]
const cases: [string, string[]][] = [
['explore-download', [...explore, ...selected]],
['explore-update', [...explore, '--explore-select', 'RateCalculator']],
['explore-remove', [...explore, '--explore-select', 'bottleneck']],
['row-hover', [...explore, ...selected, '--pointer', '500,170']],
['download-hover', [...explore, ...selected, '--pointer', '774,290']],
['update-hover', [...explore, '--explore-select', 'RateCalculator', '--pointer', '774,354']],
['remove-hover', [...explore, '--explore-select', 'bottleneck', '--pointer', '774,162']],
['progress', [...explore, ...selected, '--download-progress', 'new-mod=0.46']],
['dependencies', [...explore, ...selected]],
['bold', [...explore, '--explore-select', 'new-mod', '--portal-full', bold]],
['gallery', [...explore, '--explore-select', 'new-mod', '--portal-full', gallery]],
['gallery-gap-wheel', [...explore, '--explore-select', 'new-mod', '--portal-full', gallery, '--wheel', '991,520,0,-120']],
['gallery-scrollbar-wheel', [...explore, '--explore-select', 'new-mod', '--portal-full', gallery, '--wheel', '991,665,0,-120']],
['updates-progress', ['--tab', 'updates', '--portal-updates', catalogue, '--download-progress', 'RateCalculator=0.46', '--download-progress', 'folder-work=0.73']],
['explore-batch', [...explore, ...selected, '--download-progress', 'RateCalculator=0.46', '--download-progress', 'folder-work=0.73']],
['remove-confirm', [...explore, '--remove-confirm', 'bottleneck']],
['updates-folder', ['--tab', 'updates', '--portal-updates', catalogue, '--updates-select', 'folder-work', '--explore-action-state', 'folder']],
['space-age-confirm', [...explore, '--confirm-plan', dialog]],
['manage', ['--tab', 'manage', '--catalogue', path.join(cache, 'absent.json')]],
]
let log = ''
function capture(exe: string, name: string, args: string[], baseline = false) {
const flags = baseline ? ['--fixture', 'crates/app/tests/fixtures/r2b.json', '--size', '1440x945'] : common
const result = spawnSync(path.join(root, exe), ['--screenshot', path.join(out, `${name}.png`), ...flags, ...args], { cwd: root, env: { ...process.env, FMM_CACHE_DIR: cache }, windowsHide: true, encoding: 'utf8' })
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)
}
for (const [name, args] of cases) {
if (name.startsWith('gallery-')) {
const png = decodePng(readFileSync(path.join(out, 'gallery.png')))
let first = -1
for (let i = 0; i < png.data.length; i += 4) if (png.data[i] === 60 && png.data[i + 1] === 60 && png.data[i + 2] === 40) { first = i / 4; break }
if (first < 0) throw new Error('Gallery fixture image not found')
const x = first % png.width + 150
const y = Math.floor(first / png.width) + (name.includes('scrollbar') ? 134 : 30)
args[args.indexOf('--wheel') + 1] = [x, y, 0, -120].join(',')
}
capture('target/release/fmm.exe', name, args)
}
const manage = ['--tab', 'manage', '--catalogue', path.join(cache, 'absent.json')]
capture('out/r7/fmm-master.exe', 'manage-master', manage, true)
capture('target/release/fmm.exe', 'manage', manage, true)
const before = decodePng(readFileSync(path.join(out, 'manage-master.png')))
const after = decodePng(readFileSync(path.join(out, 'manage.png')))
if (Buffer.compare(Buffer.from(before.data), Buffer.from(after.data))) throw new Error('Manage pixels differ from master')
console.log('Manage RGBA pixels are identical to master')