const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const os = require("node:os"); const path = require("node:path"); const vm = require("node:vm"); async function run() { const root = await fs.mkdtemp(path.join(os.tmpdir(), "time4midi-project-smoke-")); const legacyPath = path.join(root, "show.json"); const newPath = path.join(root, "show.t4m"); const statePath = path.join(root, "last-project.json"); const handlers = new Map(); const source = await fs.readFile(path.join(__dirname, "..", "main.js"), "utf8"); const start = source.indexOf('ipcMain.handle("project:save"'); const end = source.indexOf('ipcMain.handle("audio:pickFile"'); assert(start >= 0 && end > start); const dialog = { showSaveDialog: async () => ({ canceled: false, filePath: legacyPath }), showOpenDialog: async () => ({ canceled: false, filePaths: [newPath] }) }; vm.runInNewContext(source.slice(start, end), { ipcMain: { handle: (name, handler) => handlers.set(name, handler) }, fs, path, dialog, app: { getPath: () => root }, console }); try { await fs.writeFile(legacyPath, '{"name":"old"}', "utf8"); const saved = await handlers.get("project:save")(null, { filePath: legacyPath, data: '{"name":"new"}' }); assert.equal(saved.filePath, newPath); assert.equal(await fs.readFile(legacyPath, "utf8"), '{"name":"old"}'); assert.equal(await fs.readFile(newPath, "utf8"), '{"name":"new"}'); assert.equal((await handlers.get("project:loadRecent")()).filePath, newPath); const loaded = await handlers.get("project:load")(); assert.equal(loaded.filePath, newPath); await fs.unlink(newPath); assert.equal((await handlers.get("project:loadRecent")()).canceled, true); process.stdout.write("project file smoke OK\n"); } finally { await fs.unlink(legacyPath).catch(() => {}); await fs.unlink(newPath).catch(() => {}); await fs.unlink(statePath).catch(() => {}); await fs.rmdir(root); } } run().catch((error) => { process.stderr.write(String(error.stack || error) + "\n"); process.exitCode = 1; });