/** Offline R8 capture matrix. All writable app data is isolated in an OS temporary directory. */
import { spawnSync } from 'node:child_process'
import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { decodePng } from './bench/png'
const root = path.resolve(import.meta.dir, '..')
const out = path.join(root, 'out/r8')
const temp = mkdtempSync(path.join(tmpdir(), 'fmm-r8-'))
const cache = path.join(temp, 'cache')
const metadata = path.join(temp, 'metadata')
const settings = path.join(temp, 'settings')
const data = path.join(temp, 'Factorio')
const install = path.join(temp, 'Steam/steamapps/common/Factorio')
for (const dir of [out, cache, metadata, settings, path.join(cache, 'portal-thumbs'), path.join(data, 'mods'), path.join(install, 'data/base')]) mkdirSync(dir, { 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(settings, 'settings.json'), JSON.stringify({ dataDir: data, installDir: install, verifyDownloads: true, catalogueRefreshHours: 24 }))
writeFileSync(path.join(out, 'capture-data.txt'), temp)
const save = (name: string, value: unknown) => { const file = path.join(temp, name + '.json'); writeFileSync(file, JSON.stringify(value)); return file }
const key = (s: string) => { let h = 0xcbf29ce484222325n; for (const b of Buffer.from(s)) h = BigInt.asUintN(64, (h ^ BigInt(b)) * 0x100000001b3n); return h.toString(16).padStart(16, '0') }
const catalogue = JSON.parse(readFileSync(path.join(root, 'crates/app/tests/fixtures/catalogue-small.json'), 'utf8')) as { results: any[] }
const imageUrl = 'https://assets-mod.factorio.com/assets/r8-gallery.png'
copyFileSync(path.join(root, 'assets/game/icons1x/space-age.png'), path.join(cache, 'portal-thumbs', key(imageUrl) + '.png'))
const mods = catalogue.results.slice(0, 14).map((m, i) => {
const thumbnail = `/assets/r8-${i}.png`
const full = { ...m, thumbnail, description: 'A detailed offline fixture.\n\n## Features\n' + 'Factory production tools and convenient planning. '.repeat(25),
homepage: `https://mods.factorio.com/mod/${m.name}`, tags: ['utility'],
images: [{ thumbnail: imageUrl, url: imageUrl }, { thumbnail: imageUrl, url: imageUrl }],
releases: [{ version: '2.0.0', sha1: '1'.repeat(40), info_json: { factorio_version: '2.0', dependencies: ['base', '? factory-library >= 1.0'] } }],
latest_release: { version: '2.0.0', info_json: { factorio_version: '2.0' } },
changelog: 'Version: 2.0.0\nDate: 2026-10-20\n Features:\n - Offline R8 fixture\n' }
catalogue.results[i] = full
writeFileSync(path.join(metadata, `${m.name}_1.0.0.json`), JSON.stringify(full))
const image = path.join(root, 'assets/game/icons1x', i % 2 ? 'space-age.png' : 'missing-thumbnail.png')
const url = 'https://assets-mod.factorio.com' + thumbnail
copyFileSync(image, path.join(cache, 'portal-thumbs', key(url) + '.png'))
copyFileSync(image, path.join(metadata, key(url) + '.png'))
return { name: m.name, title: m.title, version: '1.0.0', author: m.owner, description: 'Local mod description.', factorioVersion: '2.0', dependencies: [{ name: 'factory-library', kind: 'optional', comparator: '>=', version: '1.0' }], source: 'zip', path: path.join(temp, `${m.name}_1.0.0.zip`), thumbnail: null }
})
const builtin = ['base', 'quality', 'space-age'].map(name => ({ ...mods[0], name, title: name, version: '2.0.77', source: 'builtin', dependencies: [] }))
const snapshot = { paths: { installDir: install, userDir: data, modsDir: path.join(data, 'mods'), modListPath: path.join(data, 'mods/mod-list.json'), gameVersion: '2.0.77' }, builtin, mods, scanErrors: [], modList: { mods: [...builtin, ...mods].map(m => ({ name: m.name, enabled: true })) } }
const fixture = save('fixture', { snapshot, select: mods[0]!.name })
const changed = save('changed', { snapshot, select: mods[0]!.name, toggles: [mods[0]!.name] })
catalogue.results = catalogue.results.slice(0, 14)
const cat = save('catalogue', catalogue)
const full = save('full', catalogue.results[0])
const absent = path.join(temp, 'absent.json')
const folders = save('folders', [data + ' (recommended)', path.join(temp, 'Other Steam Library/Factorio')])
let log = ''
function capture(name: string, args: string[], file = fixture, size = '1440x945', broken = false) {
const exe = path.join(out, broken ? 'fmm-broken.exe' : 'fmm-default.exe')
const result = spawnSync(exe, ['--screenshot', path.join(out, name + '.png'), '--fixture', file, '--size', size, '--catalogue', cat, ...args], {
cwd: root, env: { ...process.env, FACTORIO_DIR: '', FMM_CACHE_DIR: cache, FMM_METADATA_DIR: broken ? path.join(temp, 'empty-metadata') : metadata, FMM_SETTINGS_DIR: settings }, encoding: 'utf8', windowsHide: true,
})
log += `${name}: ${result.status}\n${result.stdout}${result.stderr}\n`
writeFileSync(path.join(out, process.argv.includes('--broken') ? 'captures-broken.log' : 'captures.log'), log)
if (result.error) throw result.error
if (result.status !== 0) throw new Error(log)
console.log(name)
}
if (process.argv.includes('--broken')) {
for (const view of ['rows', 'grid']) capture(`broken-explore-${view}`, ['--tab', 'explore', '--view', view, '--portal-html', 'crates/core/tests/fixtures/search-default.html', '--portal-full', full, '--explore-select', mods[0]!.name], fixture, '1440x945', true)
capture('broken-empty-catalogue', ['--tab', 'explore', '--catalogue', absent, '--portal-html', 'crates/core/tests/fixtures/search-default.html', '--portal-full', full], fixture, '1440x945', true)
capture('broken-updates', ['--tab', 'updates', '--portal-updates', cat], fixture, '1440x945', true)
} else {
for (const tab of ['manage', 'updates']) for (const view of ['table', 'rows', 'grid']) capture(`${tab}-${view}`, ['--tab', tab, '--view', view, '--portal-full', full, '--portal-updates', cat, ...(tab === 'updates' ? ['--updates-select', mods[0]!.name] : [])])
for (const view of ['rows', 'grid']) {
capture(`explore-${view}`, ['--tab', 'explore', '--view', view, '--portal-full', full, '--explore-select', mods[0]!.name])
capture(`explore-placeholder-${view}`, ['--tab', 'explore', '--view', view])
}
capture('lightbox-x', ['--tab', 'explore', '--portal-full', full, '--explore-select', mods[0]!.name, '--lightbox', '0', '--lightbox-zoom', '8'])
capture('mod-list-changed', [], changed)
capture('download-x', ['--tab', 'explore', '--portal-full', full, '--explore-select', mods[0]!.name, '--download-progress', `${mods[0]!.name}=0.46`])
capture('manage-search-1000', ['--search', 'Rate'], fixture, '1000x600')
capture('updates-search-1000', ['--tab', 'updates', '--portal-updates', cat, '--updates-search', 'Rate'], fixture, '1000x600')
capture('settings', ['--settings-open', '1'])
writeFileSync(path.join(settings, 'settings.json'), JSON.stringify({ dataDir: data, installDir: install, verifyDownloads: false }))
capture('settings-no-sha1', ['--settings-open', '1'])
writeFileSync(path.join(settings, 'settings.json'), JSON.stringify({ dataDir: data, installDir: install, verifyDownloads: true }))
capture('settings-1000', ['--settings-open', '1'], fixture, '1000x600')
capture('onboarding', ['--onboarding-open', '1'])
capture('onboarding-multiple', ['--onboarding-open', '1', '--onboarding-folders', folders])
capture('custom-titlebar', ['--pointer', '40,25'])
capture('dependency-focused', ['--tab', 'explore', '--portal-full', full, '--dependency', mods[0]!.name])
capture('gallery-wheel', ['--tab', 'explore', '--portal-full', full, '--explore-select', mods[0]!.name, '--wheel', '1050,500,0,-100'])
const crop = (name: string) => {
const png = decodePng(readFileSync(path.join(out, name + '.png')))
const pixels = Buffer.alloc(600 * 830 * 4)
for (let y = 0; y < 830; y++) pixels.set(png.data.subarray(((y + 48) * png.width + 824) * 4, ((y + 48) * png.width + 1424) * 4), y * 600 * 4)
return pixels
}
let comparison = ''
for (const tab of ['manage', 'updates']) for (const view of ['rows', 'grid']) {
if (Buffer.compare(crop(`${tab}-table`), crop(`${tab}-${view}`))) throw new Error(`${tab} details changed in ${view}`)
comparison += `${tab}: Table = ${view}, exact detail pane RGBA\n`
}
for (const prefix of ['explore', 'explore-placeholder']) {
if (Buffer.compare(crop(`${prefix}-rows`), crop(`${prefix}-grid`))) throw new Error(`${prefix} details changed`)
comparison += `${prefix}: Rows = Grid, exact detail pane RGBA\n`
}
writeFileSync(path.join(out, 'pane-comparisons.txt'), comparison)
}