|
| 1 | +import { execSync } from "child_process"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | + |
| 5 | +const chip32SimDir = "../../../../../chip32-sim"; |
| 6 | + |
| 7 | +const tests = [ |
| 8 | + { |
| 9 | + name: "LoROM", |
| 10 | + filename: "roms/F-Zero.smc", |
| 11 | + expectedCore: 0, |
| 12 | + }, |
| 13 | + { |
| 14 | + name: "HiROM", |
| 15 | + filename: "roms/Chrono Trigger.smc", |
| 16 | + expectedCore: 0, |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "ExHiROM", |
| 20 | + filename: "roms/Tales of Phantasia.smc", |
| 21 | + expectedCore: 0, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "SPC7110", |
| 25 | + filename: "roms/Momotarou Dentetsu Happy (Japan).sfc", |
| 26 | + expectedCore: 1, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "SDD1", |
| 30 | + filename: "roms/Star Ocean (Japan).sfc", |
| 31 | + expectedCore: 1, |
| 32 | + }, |
| 33 | + // BSX isn't properly supported |
| 34 | + // { |
| 35 | + // name: "BSX", |
| 36 | + // filename: "roms/[ikari] Super Bomberman.bs", |
| 37 | + // expectedCore: 1, |
| 38 | + // }, |
| 39 | + { |
| 40 | + name: "PAL LoROM", |
| 41 | + filename: "roms/F-Zero (Europe).sfc", |
| 42 | + expectedCore: 2, |
| 43 | + }, |
| 44 | +]; |
| 45 | + |
| 46 | +const makeDataJson = (testCase) => { |
| 47 | + const filename = path.join(import.meta.dirname, testCase.filename); |
| 48 | + |
| 49 | + const contents = { |
| 50 | + data: { |
| 51 | + magic: "APF_VER_1", |
| 52 | + data_slots: [ |
| 53 | + { |
| 54 | + name: "SMC", |
| 55 | + id: 0, |
| 56 | + filename, |
| 57 | + required: true, |
| 58 | + parameters: "0x109", |
| 59 | + extensions: ["smc", "sfc"], |
| 60 | + address: "0x10000000", |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + return JSON.stringify(contents, null, 2); |
| 67 | +}; |
| 68 | + |
| 69 | +const printChip32Output = (json) => { |
| 70 | + console.log("Chip32 output:"); |
| 71 | + console.log(` Core: ${json.core}`); |
| 72 | + console.log(` File state: ${json.file_state}`); |
| 73 | + console.log(` Logs:`); |
| 74 | + const logs = json.logs.join("\n "); |
| 75 | + console.log(` ${logs}`); |
| 76 | +}; |
| 77 | + |
| 78 | +const runTest = (testCase, testFilePath) => { |
| 79 | + console.log(`Running test: ${testCase.name} (File: ${testCase.filename})`); |
| 80 | + |
| 81 | + try { |
| 82 | + const loaderPinPath = path.join(import.meta.dirname, "../loader.bin"); |
| 83 | + const stdout = execSync( |
| 84 | + `cargo run --quiet -- --data-json "${testFilePath}" --bin "${loaderPinPath}" --json`, |
| 85 | + { encoding: "utf8", cwd: chip32SimDir } |
| 86 | + ); |
| 87 | + |
| 88 | + let jsonOutput; |
| 89 | + try { |
| 90 | + jsonOutput = JSON.parse(stdout); |
| 91 | + } catch (error) { |
| 92 | + console.error(` ❌ Test failed: Invalid JSON output.\n${error}`); |
| 93 | + console.error("Stdout: ", stdout); |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + if (jsonOutput.core !== testCase.expectedCore) { |
| 98 | + console.error( |
| 99 | + ` ❌ Test failed: Expected core ${testCase.expectedCore}, but got ${jsonOutput.core}` |
| 100 | + ); |
| 101 | + printChip32Output(jsonOutput); |
| 102 | + return false; |
| 103 | + } |
| 104 | + if (jsonOutput.file_state === null || jsonOutput.file_state === undefined) { |
| 105 | + console.error(` ❌ Test failed: file_state is null or undefined`); |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + console.log(` ✅ Test passed.\n`); |
| 110 | + return true; |
| 111 | + } catch (error) { |
| 112 | + console.error(` ❌ Test failed: Command exited with code ${error.status}`); |
| 113 | + if (error.stderr) { |
| 114 | + console.error(` Stderr:\n${error.stderr}`); |
| 115 | + } |
| 116 | + try { |
| 117 | + const jsonOutput = JSON.parse(error.stdout.toString()); |
| 118 | + printChip32Output(jsonOutput); |
| 119 | + } catch { |
| 120 | + console.error("Stdout: ", error.stdout.toString()); |
| 121 | + } |
| 122 | + |
| 123 | + console.log("\n"); |
| 124 | + return false; |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +const runAllTests = () => { |
| 129 | + let failCount = 0; |
| 130 | + |
| 131 | + if (!fs.existsSync("tmp")) { |
| 132 | + fs.mkdirSync("tmp"); |
| 133 | + } |
| 134 | + |
| 135 | + for (const testCase of tests) { |
| 136 | + const singleTestFilePath = path.join( |
| 137 | + import.meta.dirname, |
| 138 | + "tmp", |
| 139 | + `test_roms_${testCase.name}.json` |
| 140 | + ); |
| 141 | + fs.writeFileSync(singleTestFilePath, makeDataJson(testCase), "utf8"); |
| 142 | + |
| 143 | + const passed = runTest(testCase, singleTestFilePath); |
| 144 | + if (!passed) { |
| 145 | + failCount += 1; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (failCount === 0) { |
| 150 | + console.log("\nAll tests passed!"); |
| 151 | + return true; |
| 152 | + } else { |
| 153 | + console.error(`\n${failCount} tests failed!`); |
| 154 | + return false; |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | +let success = false; |
| 159 | + |
| 160 | +try { |
| 161 | + success = runAllTests(); |
| 162 | +} finally { |
| 163 | + // fs.rmSync("tmp", { recursive: true, force: true }); |
| 164 | +} |
| 165 | + |
| 166 | +if (!success) { |
| 167 | + process.exit(1); |
| 168 | +} |
0 commit comments