|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | +const YAML = require('../js/vendor/yaml/browser/dist/index.js'); |
| 8 | +const { getConfig, getArenaId, getArenaName } = require('../js/arena-configs.js'); |
| 9 | +const PatEncoder = require('../js/pat-encoder.js'); |
| 10 | +const parserModule = require('../js/pat-parser.js'); |
| 11 | +const PatParser = parserModule.default || parserModule; |
| 12 | + |
| 13 | +const ROOT = path.resolve(__dirname, '..'); |
| 14 | +let passed = 0; |
| 15 | +let failed = 0; |
| 16 | + |
| 17 | +function check(label, condition, detail = '') { |
| 18 | + if (condition) { |
| 19 | + passed++; |
| 20 | + console.log(` PASS ${label}${detail ? ` — ${detail}` : ''}`); |
| 21 | + } else { |
| 22 | + failed++; |
| 23 | + console.error(` FAIL ${label}${detail ? ` — ${detail}` : ''}`); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function readYaml(relativePath) { |
| 28 | + return YAML.parse(fs.readFileSync(path.join(ROOT, relativePath), 'utf8')); |
| 29 | +} |
| 30 | + |
| 31 | +function sharedRigFields(rig) { |
| 32 | + return { |
| 33 | + format_version: rig.format_version, |
| 34 | + controller: rig.controller, |
| 35 | + plugins: rig.plugins, |
| 36 | + io: rig.io |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +function strictRoundTrip(rows, arenaId) { |
| 41 | + const cols = 10; |
| 42 | + const pixelRows = rows * 20; |
| 43 | + const pixelCols = cols * 20; |
| 44 | + const frame = new Uint8Array(pixelRows * pixelCols); |
| 45 | + frame[0] = 1; |
| 46 | + frame[pixelCols - 1] = 2; |
| 47 | + frame[(pixelRows - 1) * pixelCols] = 3; |
| 48 | + frame[frame.length - 1] = 4; |
| 49 | + const encoded = PatEncoder.encode({ |
| 50 | + generation: 'G6', |
| 51 | + gs_val: 16, |
| 52 | + numFrames: 1, |
| 53 | + rowCount: rows, |
| 54 | + colCount: cols, |
| 55 | + pixelRows, |
| 56 | + pixelCols, |
| 57 | + frames: [frame], |
| 58 | + stretchValues: [128], |
| 59 | + arena_id: arenaId, |
| 60 | + observer_id: 0 |
| 61 | + }); |
| 62 | + const parsed = PatParser.parsePatFile(encoded, { strict: true }); |
| 63 | + return ( |
| 64 | + parsed.rowCount === rows && |
| 65 | + parsed.colCount === cols && |
| 66 | + parsed.arena_id === arenaId && |
| 67 | + Buffer.compare(Buffer.from(parsed.frames[0]), Buffer.from(frame)) === 0 |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +console.log('=== G6 tall arena + rig proposal ==='); |
| 72 | + |
| 73 | +const base = readYaml('configs/rigs/cshl_g6_2x10.yaml'); |
| 74 | +const index = JSON.parse(fs.readFileSync(path.join(ROOT, 'configs/rigs/index.json'), 'utf8')); |
| 75 | + |
| 76 | +for (const rows of [3, 4]) { |
| 77 | + const arenaName = `G6_${rows}x10`; |
| 78 | + const rigName = `g6_${rows}x10`; |
| 79 | + const rigPath = `configs/rigs/${rigName}.yaml`; |
| 80 | + const expectedArenaId = rows + 2; // 3x10 -> 5; 4x10 -> 6 |
| 81 | + const rig = readYaml(rigPath); |
| 82 | + const config = getConfig(arenaName); |
| 83 | + const entry = index.rigs.find((candidate) => candidate.name === rigName); |
| 84 | + |
| 85 | + check(`${arenaName}: registered`, Boolean(config)); |
| 86 | + check(`${arenaName}: geometry`, config?.arena?.num_rows === rows && config?.arena?.num_cols === 10); |
| 87 | + check(`${arenaName}: full grid`, config?.arena?.columns_installed === null); |
| 88 | + check(`${arenaName}: ID`, getArenaId('G6', arenaName) === expectedArenaId, String(expectedArenaId)); |
| 89 | + check(`${arenaName}: ID round-trip`, getArenaName('G6', expectedArenaId) === arenaName); |
| 90 | + check(`${rigName}: arena reference`, rig.arena === arenaName); |
| 91 | + check( |
| 92 | + `${rigName}: CSHL capability parity`, |
| 93 | + JSON.stringify(sharedRigFields(rig)) === JSON.stringify(sharedRigFields(base)) |
| 94 | + ); |
| 95 | + check(`${rigName}: index entry`, Boolean(entry)); |
| 96 | + check(`${rigName}: index arena`, entry?.arena === arenaName); |
| 97 | + check(`${rigName}: index path`, entry?.path === `./${rigPath}`); |
| 98 | + check(`${arenaName}: strict pattern round-trip`, strictRoundTrip(rows, expectedArenaId)); |
| 99 | +} |
| 100 | + |
| 101 | +console.log(`\n${passed} passed, ${failed} failed`); |
| 102 | +if (failed) process.exit(1); |
0 commit comments