Add encode subcommand

Wraps gid.encodeGid, prints the packed-u32 as decimal on stdout,
emits usage on stderr on bad arity, and a labelled error on
out-of-range or non-integer input.
This commit is contained in:
Axel Meyer
2026-05-23 13:46:03 +02:00
parent b1743ad19f
commit 7316ebc7dd
3 changed files with 87 additions and 1 deletions

54
tests/encode.test.js Normal file
View File

@@ -0,0 +1,54 @@
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const { run } = require('../src/cli');
class Sink {
constructor() { this.chunks = []; }
write(s) { this.chunks.push(s); }
get text() { return this.chunks.join(''); }
}
test('encode: (0,12,1) prints 196 with trailing newline, exit 0', () => {
const out = new Sink(); const err = new Sink();
const code = run(['encode', '0', '12', '1'], { out, err });
assert.equal(code, 0);
assert.equal(out.text, '196\n');
assert.equal(err.text, '');
});
test('encode: rotation defaults to 0', () => {
const out = new Sink(); const err = new Sink();
const code = run(['encode', '0', '12'], { out, err });
assert.equal(code, 0);
assert.equal(out.text, '192\n');
});
test('encode: no args prints usage to stderr, exit 1', () => {
const out = new Sink(); const err = new Sink();
const code = run(['encode'], { out, err });
assert.equal(code, 1);
assert.match(err.text, /usage: sporel-mapper encode/);
});
test('encode: too many args prints usage, exit 1', () => {
const out = new Sink(); const err = new Sink();
const code = run(['encode', '0', '0', '0', '0'], { out, err });
assert.equal(code, 1);
assert.match(err.text, /usage/);
});
test('encode: out-of-range tile prints error, exit 1', () => {
const out = new Sink(); const err = new Sink();
const code = run(['encode', '0', '99999999', '0'], { out, err });
assert.equal(code, 1);
assert.match(err.text, /error: tile/);
});
test('encode: non-integer arg prints error, exit 1', () => {
const out = new Sink(); const err = new Sink();
const code = run(['encode', '0', 'abc', '0'], { out, err });
assert.equal(code, 1);
assert.match(err.text, /error/i);
});