chain-client-sdk-ts/test/proto_copy.test.ts
Dr. Stefan Flemming b6c6cc8295 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>
2026-10-27 00:59:57 +01:00

51 lines
1.9 KiB
TypeScript

// The copies must stay verbatim. Two guards, because they catch
// different mistakes:
//
// * the recorded hashes catch an edit inside this repo, with no
// platform checkout in sight (hermetic);
// * the comparison against the platform repo catches a contract
// that moved on without the copies, and skips when the checkout
// is not there.
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { existsSync, readFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { describe, it } from 'node:test';
import { protoDir, protoFiles } from '../src/proto.js';
function sha256(path: string): string {
return createHash('sha256').update(readFileSync(path)).digest('hex');
}
describe('the chain.v1 copies', () => {
it('match their recorded hashes', () => {
const recorded = new Map<string, string>();
for (const line of readFileSync(join(protoDir, 'CHECKSUMS'), 'utf8').split('\n')) {
const m = /^([0-9a-f]{64})\s+(\S+)$/.exec(line.trim());
if (m) recorded.set(m[2] as string, m[1] as string);
}
assert.equal(recorded.size, protoFiles.length, 'CHECKSUMS lists every file');
for (const file of protoFiles) {
assert.equal(sha256(join(protoDir, file)), recorded.get(file), `${file} was edited`);
}
});
it('match the platform repo, when it is checked out here', (t) => {
const platform = resolve(
process.env.CHAIN_PLATFORM_REPO ?? join(protoDir, '..', '..', 'fai_chain'),
);
if (!existsSync(join(platform, 'proto', 'chain', 'v1', 'hub.proto'))) {
t.skip(`no platform checkout at ${platform}`);
return;
}
for (const file of protoFiles) {
assert.equal(
sha256(join(protoDir, file)),
sha256(join(platform, 'proto', file)),
`${file} drifted from the platform repo: run scripts/sync-proto.mjs`,
);
}
});
});