feat: TypeScript client for Node, with a scriptable fake hub

A service in front of a hub needs four things: start a flow, follow
it, read what a detached run produced, and decide approvals. This is
that surface, plus the part a gateway cannot test without: a fake hub
that speaks the same service from the same .proto files and can be
scripted into every failure a real hub produces.

Errors carry the hub's own class (chain-error-kind), a coarse family,
and reachedHub, which separates a refused connection from a hub that
reports a dependency as down. Both arrive as gRPC UNAVAILABLE, and a
gateway that shows 'service not reachable' for the second one lies to
its users.

The .proto files are carried verbatim and loaded at runtime, so no
generated code can drift from the contract; two guards hold the
copies in place, and a skew suite runs the same client against a real
hub binary.

32 hermetic tests plus 4 against a real hub.

Signed-off-by: Dr. Stefan Flemming <sf@flemming.it>
This commit is contained in:
Dr. Stefan Flemming 2026-10-27 00:59:57 +01:00
commit b6c6cc8295
24 changed files with 5539 additions and 0 deletions

74
test/payload.test.ts Normal file
View file

@ -0,0 +1,74 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { asBytes, asJson, asText, payloadFromWire, payloadToWire } from '../src/payload.js';
describe('payload conversion', () => {
it('sends a string as text', () => {
assert.deepEqual(payloadToWire('hello'), { text: 'hello' });
assert.equal(asText(payloadFromWire({ text: 'hello' })), 'hello');
});
it('carries a nested object through google.protobuf.Struct', () => {
const input = {
json: {
owner: { name: 'Haus 12', built: 1974 },
rooms: [{ area: 24.5 }, { area: 18 }],
heated: true,
note: null,
},
};
const wire = payloadToWire(input);
const back = asJson(payloadFromWire(wire));
assert.deepEqual(back, input.json);
});
it('treats a plain object as json, because that is what a caller means', () => {
const wire = payloadToWire({ consumption: 120 });
assert.deepEqual(asJson(payloadFromWire(wire)), { consumption: 120 });
});
it('refuses a bare array, so the kind stays explicit', () => {
assert.throws(() => payloadToWire([1, 2, 3]), /not a payload/);
});
it('refuses a json payload that is not an object', () => {
assert.throws(() => payloadToWire({ json: 'text' }), /Struct/);
});
it('round-trips bytes with their media type', () => {
const data = Buffer.from([1, 2, 3, 4]);
const wire = payloadToWire({ bytes: data, mimeType: 'application/pdf' });
const out = payloadFromWire(wire);
assert.equal(out.kind, 'bytes');
assert.deepEqual(asBytes(out), data);
assert.equal(out.kind === 'bytes' ? out.mimeType : '', 'application/pdf');
});
it('round-trips a file reference with its hash', () => {
const wire = payloadToWire({
file: {
uri: 'file:///var/lib/portal/42.jpg',
mimeType: 'image/jpeg',
sizeBytes: 2048,
sha256: 'ab'.repeat(32),
},
});
const out = payloadFromWire(wire);
assert.equal(out.kind, 'file');
if (out.kind === 'file') {
assert.equal(out.sizeBytes, 2048);
assert.equal(out.sha256, 'ab'.repeat(32));
}
});
it('reads the kind the loader names, not the first key it finds', () => {
const out = payloadFromWire({ kind: 'text', text: 'the answer', json: { fields: {} } });
assert.equal(out.kind, 'text');
});
it('answers empty for a payload that carries nothing', () => {
assert.equal(payloadFromWire(undefined).kind, 'empty');
assert.equal(payloadFromWire({}).kind, 'empty');
});
});