export async function runBrowserOpfsMirrorSections(context, simulator) { const { expectEvent, expectMissingWasmPath, expectText, near, runSection, withSmokeSimulator, } = context; await runSection("opfs wasm mirror persist file", async () => { const wasmPath = simulator.fs.opfs.resolvePath("programs/browser-smoke.ngc"); const wasmDecoded = new TextDecoder().decode(simulator.fs.module.FS.readFile(wasmPath)); expectText(wasmDecoded, "G21 G90\nG0 X1\n", "OPFS file was not mirrored into the WASM filesystem"); const persistedPath = simulator.fs.opfs.resolvePath("programs/browser-persisted.ngc"); simulator.fs.module.FS.writeFile(persistedPath, new TextEncoder().encode("G21 G90\nG0 X2\n")); const persisted = new TextDecoder().decode(await simulator.fs.opfs.persistFile("programs/browser-persisted.ngc")); expectText(persisted, "G21 G90\nG0 X2\n", "WASM filesystem file was not persisted into OPFS"); const persistedDecoded = new TextDecoder().decode( await simulator.fs.opfs.readFile("programs/browser-persisted.ngc"), ); expectText(persistedDecoded, "G21 G90\nG0 X2\n", "persisted WASM filesystem file was not readable from OPFS"); }); await runSection("opfs persisted wasm mirror parse in new instance", async () => { await withSmokeSimulator(async (restoredMirrorSimulator) => { // LinuxCNC source basis: rs274ngc_pre.cc read_text() reads // program blocks after the OPFS file is mirrored into the WASM FS. const persistedProgram = new TextDecoder().decode( await restoredMirrorSimulator.fs.opfs.readFile("programs/browser-persisted.ngc"), ); expectText(persistedProgram, "G21 G90\nG0 X2\n", "persisted WASM mirror file was not restored from OPFS in a new instance"); const persistedEvents = await restoredMirrorSimulator.parseFile( "programs/browser-persisted.ngc", "linuxcnc", { backend: "linuxcnc-rs274" }, ); expectEvent(persistedEvents, (event) => event.type === "rapid" && near(event.end.x, 2), "persisted WASM mirror file was not parsed through LinuxCNC in a new instance"); }); }); await runSection("opfs copy and move mirror files", async () => { // LinuxCNC source basis: rs274ngc_pre.cc save_parameters() // links the current parameter file to filename + ".bak", then // writes filename + ".new" and renames it over the parameter file. await simulator.fs.opfs.writeFile("parameters/move-main.var", "5161\t1.000000\n"); const copiedParameterBackup = new TextDecoder().decode( await simulator.fs.opfs.copyFile("parameters/move-main.var", "parameters/move-main.var.bak"), ); expectText(copiedParameterBackup, "5161\t1.000000\n", "OPFS copyFile did not return the copied file contents"); const copiedParameterBackupFile = new TextDecoder().decode( await simulator.fs.opfs.readFile("parameters/move-main.var.bak"), ); expectText(copiedParameterBackupFile, "5161\t1.000000\n", "OPFS copyFile did not create the backup file"); const copiedParameterBackupWasmPath = simulator.fs.opfs.resolvePath("parameters/move-main.var.bak"); const copiedParameterBackupWasm = new TextDecoder().decode( simulator.fs.module.FS.readFile(copiedParameterBackupWasmPath), ); expectText(copiedParameterBackupWasm, "5161\t1.000000\n", "OPFS copyFile did not update the WASM mirror destination"); await simulator.fs.opfs.writeFile("parameters/move-main.var.new", "5161\t2.000000\n"); await simulator.fs.opfs.moveFile("parameters/move-main.var.new", "parameters/move-main.var"); const movedParameterFile = new TextDecoder().decode( await simulator.fs.opfs.readFile("parameters/move-main.var"), ); expectText(movedParameterFile, "5161\t2.000000\n", "OPFS moveFile did not replace the destination file"); if (await simulator.fs.opfs.exists("parameters/move-main.var.new")) { throw new Error("OPFS moveFile left the source file behind"); } const movedParameterWasmPath = simulator.fs.opfs.resolvePath("parameters/move-main.var"); const movedParameterWasm = new TextDecoder().decode( simulator.fs.module.FS.readFile(movedParameterWasmPath), ); expectText(movedParameterWasm, "5161\t2.000000\n", "OPFS moveFile did not update the WASM mirror destination"); const movedTemporaryWasmPath = simulator.fs.opfs.resolvePath("parameters/move-main.var.new"); expectMissingWasmPath( () => simulator.fs.module.FS.stat(movedTemporaryWasmPath), "OPFS moveFile left the WASM mirror source behind", ); }); await runSection("opfs write truncates wasm mirror", async () => { await simulator.fs.opfs.writeFile("programs/truncate.ngc", "G21 G90\nG0 X12345\nM30\n"); await simulator.fs.opfs.writeFile("programs/truncate.ngc", "G21\n"); const truncatedProgram = new TextDecoder().decode(await simulator.fs.opfs.readFile("programs/truncate.ngc")); expectText(truncatedProgram, "G21\n", "OPFS writeFile did not truncate stale program bytes"); const truncatedWasmPath = simulator.fs.opfs.resolvePath("programs/truncate.ngc"); const truncatedWasmProgram = new TextDecoder().decode(simulator.fs.module.FS.readFile(truncatedWasmPath)); expectText(truncatedWasmProgram, "G21\n", "OPFS writeFile did not truncate the WASM mirror"); }); }