import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { spawnSync } from 'node:child_process'; import { afterEach, describe, expect, it } from 'vitest'; import { VERSION_FILES, versionProblems } from '../../../scripts/check-version.mjs'; /** The version check Fabrica's Deploy reads (fabrica-release.md F10). */ const repo = fileURLToPath(new URL('../../../', import.meta.url)); const temps: string[] = []; afterEach(() => { for (const t of temps.splice(0)) rmSync(t, { recursive: true, force: true }); }); function fakeRepo(versions: Record): string { const root = mkdtempSync(join(tmpdir(), 'rosea-version-')); temps.push(root); for (const file of VERSION_FILES) { const v = versions[file] ?? '0.1.0'; mkdirSync(dirname(join(root, file)), { recursive: true }); const text = file.endsWith('.toml') ? `[package]\nname = "x"\nversion = "${v}"\n` : JSON.stringify({ name: 'x', version: v }); writeFileSync(join(root, file), text); } return root; } describe('check-version', { timeout: 30_000 }, () => { it('agrees when every file carries the version', () => { expect(versionProblems(fakeRepo({}), '0.1.0')).toEqual([]); }); it('names each file that differs, in the words Fabrica parses', () => { const root = fakeRepo({ 'apps/rosea-tauri/src-tauri/Cargo.toml': '0.0.9', 'apps/rosea/package.json': '0.2.0' }); expect(versionProblems(root, '0.1.0')).toEqual([ 'apps/rosea/package.json says 0.2.0, the tag says 0.1.0', 'apps/rosea-tauri/src-tauri/Cargo.toml says 0.0.9, the tag says 0.1.0' ]); }); it('refuses without a version', () => { expect(versionProblems(fakeRepo({}), undefined)).toEqual(['usage: node scripts/check-version.mjs ']); }); it('covers the file the installer and the APK take their version from', () => { const conf = JSON.parse(readFileSync(join(repo, 'apps/rosea-tauri/src-tauri/tauri.conf.json'), 'utf8')) as { version: string }; // tauri.conf.json names the game's package.json relative to itself. const named = join('apps/rosea-tauri/src-tauri', conf.version).replace(/\\/g, '/'); expect(VERSION_FILES).toContain(named); }); it('this repository agrees with itself, and the CLI says so (a v prefix is the tag, not the version)', () => { const version = (JSON.parse(readFileSync(join(repo, 'package.json'), 'utf8')) as { version: string }).version; expect(versionProblems(repo, version)).toEqual([]); const ok = spawnSync(process.execPath, [join(repo, 'scripts/check-version.mjs'), `v${version}`], { encoding: 'utf8' }); expect(ok.status).toBe(0); expect(ok.stdout).toContain(`version ${version} everywhere`); const bad = spawnSync(process.execPath, [join(repo, 'scripts/check-version.mjs'), '9.9.9'], { encoding: 'utf8' }); expect(bad.status).toBe(1); expect(bad.stderr).toContain(`package.json says ${version}, the tag says 9.9.9`); }); });