// State persistence: the plugin writes under `/plugin-state/rom-manager`, and on first // run migrates an operator config authored in the pre-0.2.1 `/rom-manager` location — // the write path moved when the runner was de-privileged, and a missed migration would silently // reset the operator's settings. import { afterEach, beforeEach, describe, expect, test } from "bun:test"; import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { loadConfig, saveRawConfig } from "../src/state.js"; let root: string; let saved: string | undefined; beforeEach(() => { saved = process.env.PUNKTFUNK_CONFIG_DIR; root = fs.mkdtempSync(path.join(os.tmpdir(), "rm-state-")); process.env.PUNKTFUNK_CONFIG_DIR = root; }); afterEach(() => { if (saved === undefined) delete process.env.PUNKTFUNK_CONFIG_DIR; else process.env.PUNKTFUNK_CONFIG_DIR = saved; fs.rmSync(root, { recursive: true, force: true }); }); describe("state location + migration", () => { test("persists under plugin-state/rom-manager", () => { saveRawConfig({ roots: [{ dir: "/games", platform: "snes" }] }); expect( fs.existsSync( path.join(root, "plugin-state", "rom-manager", "config.json"), ), ).toBe(true); }); test("migrates a pre-0.2.1 config from /rom-manager on first load", () => { // An operator config authored in the OLD location. const legacy = path.join(root, "rom-manager"); fs.mkdirSync(legacy, { recursive: true }); fs.writeFileSync( path.join(legacy, "config.json"), JSON.stringify({ roots: [{ dir: "/legacy/games", platform: "snes" }] }), ); // First load migrates it — the operator's roots survive rather than resetting to defaults. const cfg = loadConfig(); expect(cfg.roots).toEqual([{ dir: "/legacy/games", platform: "snes" }]); expect( fs.existsSync( path.join(root, "plugin-state", "rom-manager", "config.json"), ), ).toBe(true); }); test("does not clobber an existing new-location config with the legacy one", () => { const legacy = path.join(root, "rom-manager"); fs.mkdirSync(legacy, { recursive: true }); fs.writeFileSync( path.join(legacy, "config.json"), JSON.stringify({ roots: [{ dir: "/old", platform: "snes" }] }), ); saveRawConfig({ roots: [{ dir: "/new", platform: "snes" }] }); // new location already authored expect(loadConfig().roots).toEqual([{ dir: "/new", platform: "snes" }]); }); });