Add inspect subcommand: read-only stats for v2 map files
Reads the map JSON, optionally loads --atlas-dir paths for name resolution, and prints the inspector report to stdout. Missing file, bad JSON, and missing --atlas-dir paths all produce labelled stderr errors with exit 1.
This commit is contained in:
53
tests/inspect.test.js
Normal file
53
tests/inspect.test.js
Normal file
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const path = require('node:path');
|
||||
const { run } = require('../src/cli');
|
||||
|
||||
class Sink {
|
||||
constructor() { this.chunks = []; }
|
||||
write(s) { this.chunks.push(s); }
|
||||
get text() { return this.chunks.join(''); }
|
||||
}
|
||||
|
||||
const FIX_MAP = path.join(__dirname, 'fixtures', 'maps', 'inspect_sample.map.json');
|
||||
const FIX_ATLAS = path.join(__dirname, 'fixtures', 'atlases');
|
||||
|
||||
test('inspect: prints report to stdout, exit 0', () => {
|
||||
const out = new Sink(); const err = new Sink();
|
||||
const code = run(['inspect', FIX_MAP], { out, err });
|
||||
assert.equal(code, 0, err.text);
|
||||
assert.match(out.text, /Map: inspect_sample/);
|
||||
assert.match(out.text, /Roof: 4 cells flagged/);
|
||||
});
|
||||
|
||||
test('inspect: --atlas-dir enables name resolution', () => {
|
||||
const out = new Sink(); const err = new Sink();
|
||||
const code = run(['inspect', FIX_MAP, '--atlas-dir', FIX_ATLAS], { out, err });
|
||||
assert.equal(code, 0);
|
||||
assert.match(out.text, /atlas_a:grass/);
|
||||
});
|
||||
|
||||
test('inspect: missing positional arg prints usage, exit 1', () => {
|
||||
const out = new Sink(); const err = new Sink();
|
||||
const code = run(['inspect'], { out, err });
|
||||
assert.equal(code, 1);
|
||||
assert.match(err.text, /usage: sporel-mapper inspect/);
|
||||
});
|
||||
|
||||
test('inspect: missing map file prints error, exit 1', () => {
|
||||
const out = new Sink(); const err = new Sink();
|
||||
const code = run(['inspect', '/no/such/map.json'], { out, err });
|
||||
assert.equal(code, 1);
|
||||
assert.match(err.text, /error: cannot read/);
|
||||
});
|
||||
|
||||
test('inspect: invalid JSON prints error, exit 1', () => {
|
||||
const out = new Sink(); const err = new Sink();
|
||||
// Use a non-JSON file as bogus input
|
||||
const reallyNotJson = path.join(__dirname, '..', 'src', 'cli.js');
|
||||
const code = run(['inspect', reallyNotJson], { out, err });
|
||||
assert.equal(code, 1);
|
||||
assert.match(err.text, /error/i);
|
||||
});
|
||||
Reference in New Issue
Block a user