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:
commit
b6c6cc8295
24 changed files with 5539 additions and 0 deletions
277
test/client_against_fake.test.ts
Normal file
277
test/client_against_fake.test.ts
Normal file
|
|
@ -0,0 +1,277 @@
|
|||
// The client against the fake hub: hermetic, no hub binary, no
|
||||
// module, no model. Every case here is one a portal gateway has to
|
||||
// survive.
|
||||
|
||||
import assert from 'node:assert/strict';
|
||||
import { after, before, beforeEach, describe, it } from 'node:test';
|
||||
|
||||
import { ChainClient, ChainError, asJson, asText } from '../src/index.js';
|
||||
import { FakeHub } from '../src/fake_hub.js';
|
||||
|
||||
const FLOW = `name: verbrauchsausweis
|
||||
inputs:
|
||||
consumption: { type: json }
|
||||
steps:
|
||||
- id: score
|
||||
uses: geg.verbrauch@^1
|
||||
outputs:
|
||||
report: $steps.score.text
|
||||
`;
|
||||
|
||||
describe('client against the fake hub', () => {
|
||||
let hub: FakeHub;
|
||||
let client: ChainClient;
|
||||
|
||||
before(async () => {
|
||||
hub = new FakeHub();
|
||||
const address = await hub.start();
|
||||
client = new ChainClient({ address, project: 'helloeco', deadlineMs: 5_000 });
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
client.close();
|
||||
await hub.close();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
hub.setFallback({ type: 'succeed', outputs: {} });
|
||||
hub.setHealth('SERVING');
|
||||
});
|
||||
|
||||
it('reports health', async () => {
|
||||
const health = await client.health();
|
||||
assert.equal(health.state, 'SERVING');
|
||||
});
|
||||
|
||||
it('runs a flow and reads its outputs', async () => {
|
||||
hub.succeeds({ report: 'Endenergiebedarf 128 kWh/(m²·a)', data: { json: { class: 'D' } } });
|
||||
const result = await client.submit({
|
||||
flowYaml: FLOW,
|
||||
inputs: { consumption: { json: { kwh: 15400 } } },
|
||||
});
|
||||
assert.equal(asText(result.outputs.report), 'Endenergiebedarf 128 kWh/(m²·a)');
|
||||
assert.deepEqual(asJson(result.outputs.data), { class: 'D' });
|
||||
assert.match(result.invocationId, /^run-/);
|
||||
|
||||
const seen = hub.submits.at(-1);
|
||||
assert.equal(seen?.project, 'helloeco');
|
||||
assert.deepEqual(seen?.inputs.consumption, { kind: 'json', json: { kwh: 15400 } });
|
||||
});
|
||||
|
||||
it('follows a run step by step', async () => {
|
||||
hub.succeeds({ report: 'done' }, ['check', 'score', 'render']);
|
||||
const events = [];
|
||||
for await (const event of client.submitStream({ flowYaml: FLOW })) {
|
||||
events.push(event);
|
||||
}
|
||||
const started = events.filter((e) => e.type === 'stepStarted').map((e) => e.step);
|
||||
assert.deepEqual(started, ['check', 'score', 'render']);
|
||||
const last = events.at(-1);
|
||||
assert.equal(last?.type, 'result');
|
||||
if (last?.type === 'result') {
|
||||
assert.equal(asText(last.result.outputs.report), 'done');
|
||||
}
|
||||
});
|
||||
|
||||
it('classifies an overloaded hub as retryable without blaming the request', async () => {
|
||||
hub.fails('overloaded', 'too many runs in flight');
|
||||
const err = await client.submit({ flowYaml: FLOW }).then(
|
||||
() => undefined,
|
||||
(e: unknown) => e as ChainError,
|
||||
);
|
||||
assert.ok(err instanceof ChainError);
|
||||
assert.equal(err.kind, 'overloaded');
|
||||
assert.equal(err.family, 'overloaded');
|
||||
assert.equal(err.retryable, true);
|
||||
assert.equal(err.reachedHub, true);
|
||||
});
|
||||
|
||||
it('classifies a refused request as not retryable', async () => {
|
||||
hub.fails('flow_rejected', "missing value '$inputs.consumption'");
|
||||
const err = (await client.submit({ flowYaml: FLOW }).catch((e) => e)) as ChainError;
|
||||
assert.equal(err.kind, 'flow_rejected');
|
||||
assert.equal(err.retryable, false);
|
||||
});
|
||||
|
||||
it('carries the class out of a stream too', async () => {
|
||||
hub.fails('step_failed', "step 'render' failed: template has no page size");
|
||||
const err = await (async () => {
|
||||
try {
|
||||
for await (const _event of client.submitStream({ flowYaml: FLOW })) {
|
||||
// drain
|
||||
}
|
||||
return undefined;
|
||||
} catch (e) {
|
||||
return e as ChainError;
|
||||
}
|
||||
})();
|
||||
assert.ok(err instanceof ChainError);
|
||||
assert.equal(err.kind, 'step_failed');
|
||||
assert.equal(err.family, 'failed');
|
||||
assert.equal(err.step, 'render');
|
||||
});
|
||||
|
||||
it('returns the same run for the same idempotency key', async () => {
|
||||
hub.succeeds({ report: 'first' });
|
||||
hub.succeeds({ report: 'second' });
|
||||
const first = await client.submit({ flowYaml: FLOW, idempotencyKey: 'stripe-evt-1' });
|
||||
const again = await client.submit({ flowYaml: FLOW, idempotencyKey: 'stripe-evt-1' });
|
||||
assert.equal(again.invocationId, first.invocationId);
|
||||
assert.equal(asText(again.outputs.report), 'first');
|
||||
// The second submit must not have consumed the next scripted run.
|
||||
const third = await client.submit({ flowYaml: FLOW, idempotencyKey: 'stripe-evt-2' });
|
||||
assert.equal(asText(third.outputs.report), 'second');
|
||||
assert.notEqual(third.invocationId, first.invocationId);
|
||||
});
|
||||
|
||||
it('waits for an approval, then finishes with the reviewer on record', async () => {
|
||||
hub.waitsForApproval({
|
||||
prompt: 'Registriernummer 4711 vergeben und Ausweis zustellen?',
|
||||
stepId: 'freigabe',
|
||||
outputs: { report: 'zugestellt' },
|
||||
});
|
||||
const run = client.submit({ flowYaml: FLOW });
|
||||
|
||||
const pending = await waitFor(async () => {
|
||||
const list = await client.listApprovals({ project: 'helloeco' });
|
||||
return list.length > 0 ? list : undefined;
|
||||
});
|
||||
assert.equal(pending[0]?.prompt, 'Registriernummer 4711 vergeben und Ausweis zustellen?');
|
||||
assert.equal(pending[0]?.stepId, 'freigabe');
|
||||
|
||||
await client.decideApproval({
|
||||
approvalId: pending[0]!.approvalId,
|
||||
approve: true,
|
||||
reviewer: 'aussteller@example.org',
|
||||
});
|
||||
|
||||
const result = await run;
|
||||
assert.equal(asText(result.outputs.report), 'zugestellt');
|
||||
assert.equal(hub.decisions.at(-1)?.reviewer, 'aussteller@example.org');
|
||||
});
|
||||
|
||||
it('fails the run when the reviewer declines, with the reason', async () => {
|
||||
hub.waitsForApproval({ prompt: 'Ausweis zustellen?', outputs: {} });
|
||||
// The catch is attached right away: the run rejects while the
|
||||
// test is still waiting for the approval row, and an unhandled
|
||||
// rejection in between would fail the test for the wrong reason.
|
||||
const run = client.submit({ flowYaml: FLOW }).catch((e: unknown) => e as ChainError);
|
||||
const pending = await waitFor(async () => {
|
||||
const list = await client.listApprovals();
|
||||
return list.length > 0 ? list : undefined;
|
||||
});
|
||||
await client.decideApproval({
|
||||
approvalId: pending[0]!.approvalId,
|
||||
approve: false,
|
||||
reviewer: 'aussteller@example.org',
|
||||
reason: 'Verbrauchswerte unplausibel',
|
||||
});
|
||||
const err = (await run) as ChainError;
|
||||
assert.equal(err.kind, 'approval_rejected');
|
||||
assert.match(err.message, /unplausibel/);
|
||||
});
|
||||
|
||||
it('refuses a rejection without a reason', async () => {
|
||||
hub.waitsForApproval({ prompt: 'Ausweis zustellen?', outputs: {} });
|
||||
const run = client.submit({ flowYaml: FLOW }).catch((e: unknown) => e as ChainError);
|
||||
const pending = await waitFor(async () => {
|
||||
const list = await client.listApprovals();
|
||||
return list.length > 0 ? list : undefined;
|
||||
});
|
||||
const err = (await client
|
||||
.decideApproval({ approvalId: pending[0]!.approvalId, approve: false })
|
||||
.catch((e) => e)) as ChainError;
|
||||
assert.equal(err.kind, 'flow_rejected');
|
||||
await client.decideApproval({
|
||||
approvalId: pending[0]!.approvalId,
|
||||
approve: false,
|
||||
reason: 'aufgeräumt',
|
||||
});
|
||||
await run;
|
||||
});
|
||||
|
||||
it('follows a detached run by id', async () => {
|
||||
hub.succeeds({ report: 'spät' });
|
||||
const started = await client.submit({ flowYaml: FLOW, detach: true });
|
||||
assert.deepEqual(started.outputs, {});
|
||||
const status = await client.getStatus(started.invocationId);
|
||||
assert.equal(status.phase, 'SUCCEEDED');
|
||||
const result = await client.getResult(started.invocationId);
|
||||
assert.equal(asText(result.outputs.report), 'spät');
|
||||
});
|
||||
|
||||
it('says not_found for an invocation the hub never had', async () => {
|
||||
const err = (await client.getStatus('run-does-not-exist').catch((e) => e)) as ChainError;
|
||||
assert.equal(err.kind, 'not_found');
|
||||
assert.equal(err.family, 'rejected');
|
||||
});
|
||||
});
|
||||
|
||||
describe('client against a hub that is not there', () => {
|
||||
it('says the hub was never reached, not that a dependency failed', async () => {
|
||||
const hub = new FakeHub();
|
||||
const address = await hub.start();
|
||||
const client = new ChainClient({ address, deadlineMs: 2_000 });
|
||||
await client.health();
|
||||
await hub.stop();
|
||||
|
||||
const err = (await client.health().catch((e) => e)) as ChainError;
|
||||
assert.equal(err.family, 'unreachable');
|
||||
assert.equal(err.reachedHub, false);
|
||||
assert.equal(err.retryable, true);
|
||||
client.close();
|
||||
await hub.close();
|
||||
});
|
||||
});
|
||||
|
||||
describe('a hub restart while a run waits for an approval', () => {
|
||||
it('loses the run, keeps the approval, and says so', async () => {
|
||||
// This is today's platform behaviour, not a wish: run state lives
|
||||
// in memory, approvals live in SQLite. A gateway must therefore
|
||||
// treat "approval still pending after a restart" as a run it has
|
||||
// to start again.
|
||||
const hub = new FakeHub();
|
||||
const address = await hub.start();
|
||||
const client = new ChainClient({ address, project: 'helloeco', deadlineMs: 5_000 });
|
||||
|
||||
hub.waitsForApproval({ prompt: 'Ausweis zustellen?', outputs: { report: 'zugestellt' } });
|
||||
const run = client.submit({ flowYaml: FLOW }).catch((e: unknown) => e as ChainError);
|
||||
const pending = await waitFor(async () => {
|
||||
const list = await client.listApprovals({ project: 'helloeco' });
|
||||
return list.length > 0 ? list : undefined;
|
||||
});
|
||||
const approvalId = pending[0]!.approvalId;
|
||||
|
||||
await hub.restart();
|
||||
|
||||
const err = (await run) as ChainError;
|
||||
assert.equal(err.family, 'unreachable');
|
||||
|
||||
// The decision still lands, and still has nothing to resume.
|
||||
const stillPending = await client.listApprovals({ project: 'helloeco' });
|
||||
assert.equal(stillPending.length, 1);
|
||||
assert.equal(stillPending[0]?.approvalId, approvalId);
|
||||
const decided = await client.decideApproval({
|
||||
approvalId,
|
||||
approve: true,
|
||||
reviewer: 'aussteller@example.org',
|
||||
});
|
||||
assert.equal(decided.status, 'APPROVED');
|
||||
const gone = (await client.getStatus(pending[0]!.flowExecution).catch((e) => e)) as ChainError;
|
||||
assert.equal(gone.kind, 'not_found');
|
||||
|
||||
client.close();
|
||||
await hub.close();
|
||||
});
|
||||
});
|
||||
|
||||
/** Poll until the producer returns something, or give up. */
|
||||
async function waitFor<T>(produce: () => Promise<T | undefined>, timeoutMs = 3_000): Promise<T> {
|
||||
const until = Date.now() + timeoutMs;
|
||||
for (;;) {
|
||||
const value = await produce();
|
||||
if (value !== undefined) return value;
|
||||
if (Date.now() > until) throw new Error('timed out waiting for the fake hub');
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
}
|
||||
}
|
||||
105
test/errors.test.ts
Normal file
105
test/errors.test.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import assert from 'node:assert/strict';
|
||||
import { describe, it } from 'node:test';
|
||||
|
||||
import { status as GrpcStatus, Metadata } from '@grpc/grpc-js';
|
||||
import type { ServiceError } from '@grpc/grpc-js';
|
||||
|
||||
import { ChainError, chainErrorFrom, kindForCode } from '../src/errors.js';
|
||||
|
||||
function serviceError(code: number, details: string, kind?: string): ServiceError {
|
||||
const metadata = new Metadata();
|
||||
if (kind) metadata.set('chain-error-kind', kind);
|
||||
const err = new Error(details) as ServiceError;
|
||||
err.code = code;
|
||||
err.details = details;
|
||||
err.metadata = metadata;
|
||||
return err;
|
||||
}
|
||||
|
||||
describe('error classification', () => {
|
||||
it('prefers the class the hub sent over the gRPC code', () => {
|
||||
// Both arrive as INVALID_ARGUMENT. Only the class separates
|
||||
// "fix your request" from "the module ran and said no".
|
||||
const rejected = chainErrorFrom(
|
||||
serviceError(GrpcStatus.INVALID_ARGUMENT, "missing value '$inputs.x'", 'flow_rejected'),
|
||||
);
|
||||
const failed = chainErrorFrom(
|
||||
serviceError(GrpcStatus.INVALID_ARGUMENT, "step 'render' failed", 'step_failed'),
|
||||
);
|
||||
assert.equal(rejected.kind, 'flow_rejected');
|
||||
assert.equal(rejected.family, 'rejected');
|
||||
assert.equal(failed.kind, 'step_failed');
|
||||
assert.equal(failed.family, 'failed');
|
||||
assert.equal(failed.step, 'render');
|
||||
});
|
||||
|
||||
it('tells a hub that is not reachable from a dependency that is down', () => {
|
||||
const noHub = chainErrorFrom(serviceError(GrpcStatus.UNAVAILABLE, 'No connection established'));
|
||||
const hubSaysSo = chainErrorFrom(
|
||||
serviceError(GrpcStatus.UNAVAILABLE, "service 'ollama' did not answer", 'unavailable'),
|
||||
);
|
||||
assert.equal(noHub.reachedHub, false);
|
||||
assert.equal(hubSaysSo.reachedHub, true);
|
||||
// Both are retryable, but only one is the hub's own fault.
|
||||
assert.equal(noHub.retryable, true);
|
||||
assert.equal(hubSaysSo.retryable, true);
|
||||
});
|
||||
|
||||
it('reads a cancellation it did not cause as a lost hub', () => {
|
||||
// grpc-js reports CANCELLED when the transport drops a call in
|
||||
// flight. Without a class the hub never produced that status, and
|
||||
// a gateway must not read it as "I cancelled this myself".
|
||||
const err = chainErrorFrom(serviceError(GrpcStatus.CANCELLED, 'Call cancelled'));
|
||||
assert.equal(err.kind, 'unavailable');
|
||||
assert.equal(err.family, 'unreachable');
|
||||
assert.equal(err.reachedHub, false);
|
||||
// A cancellation the hub itself reports keeps its class.
|
||||
const fromHub = chainErrorFrom(
|
||||
serviceError(GrpcStatus.CANCELLED, 'cancelled by the caller', 'cancelled'),
|
||||
);
|
||||
assert.equal(fromHub.kind, 'cancelled');
|
||||
});
|
||||
|
||||
it('keeps an unknown class from a newer hub instead of crashing', () => {
|
||||
const err = chainErrorFrom(
|
||||
serviceError(GrpcStatus.INTERNAL, 'something new', 'quantum_entanglement'),
|
||||
);
|
||||
assert.equal(err.kind, 'unknown');
|
||||
assert.equal(err.family, 'internal');
|
||||
assert.equal(err.reachedHub, true);
|
||||
});
|
||||
|
||||
it('maps every gRPC code a hub can send', () => {
|
||||
assert.equal(kindForCode(GrpcStatus.INVALID_ARGUMENT), 'flow_rejected');
|
||||
assert.equal(kindForCode(GrpcStatus.NOT_FOUND), 'not_found');
|
||||
assert.equal(kindForCode(GrpcStatus.ALREADY_EXISTS), 'conflict');
|
||||
assert.equal(kindForCode(GrpcStatus.FAILED_PRECONDITION), 'precondition');
|
||||
assert.equal(kindForCode(GrpcStatus.UNAVAILABLE), 'unavailable');
|
||||
assert.equal(kindForCode(GrpcStatus.RESOURCE_EXHAUSTED), 'overloaded');
|
||||
assert.equal(kindForCode(GrpcStatus.DEADLINE_EXCEEDED), 'timeout');
|
||||
assert.equal(kindForCode(GrpcStatus.PERMISSION_DENIED), 'forbidden');
|
||||
assert.equal(kindForCode(GrpcStatus.UNAUTHENTICATED), 'unauthenticated');
|
||||
assert.equal(kindForCode(GrpcStatus.CANCELLED), 'cancelled');
|
||||
assert.equal(kindForCode(GrpcStatus.INTERNAL), 'internal');
|
||||
});
|
||||
|
||||
it('only marks the three families a retry can help', () => {
|
||||
const retryable = ['unavailable', 'overloaded', 'timeout'] as const;
|
||||
for (const kind of retryable) {
|
||||
assert.equal(new ChainError('x', { kind, code: 0, reachedHub: true }).retryable, true, kind);
|
||||
}
|
||||
for (const kind of ['flow_rejected', 'step_failed', 'forbidden', 'internal'] as const) {
|
||||
assert.equal(new ChainError('x', { kind, code: 0, reachedHub: true }).retryable, false, kind);
|
||||
}
|
||||
});
|
||||
|
||||
it('logs one line without the payload', () => {
|
||||
const err = new ChainError("step 'render' failed", {
|
||||
kind: 'step_failed',
|
||||
code: 3,
|
||||
reachedHub: true,
|
||||
step: 'render',
|
||||
});
|
||||
assert.equal(String(err), "ChainError[step_failed/failed] step 'render' failed step=render");
|
||||
});
|
||||
});
|
||||
74
test/payload.test.ts
Normal file
74
test/payload.test.ts
Normal 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');
|
||||
});
|
||||
});
|
||||
51
test/proto_copy.test.ts
Normal file
51
test/proto_copy.test.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
// 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`,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
127
test/real_hub.test.ts
Normal file
127
test/real_hub.test.ts
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// The client against a real hub binary.
|
||||
//
|
||||
// This is the skew gate for the TypeScript client: it proves that the
|
||||
// shapes this client encodes are the shapes the hub decodes, which a
|
||||
// fake can never prove. It needs a built `chain` binary and therefore
|
||||
// skips when there is none; CI and release work set `CHAIN_BIN`.
|
||||
//
|
||||
// CHAIN_BIN=../fai_chain/target/debug/chain npm test
|
||||
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawn, type ChildProcess } from 'node:child_process';
|
||||
import { existsSync, mkdtempSync, rmSync } from 'node:fs';
|
||||
import { createServer } from 'node:net';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join, resolve } from 'node:path';
|
||||
import { after, before, describe, it } from 'node:test';
|
||||
|
||||
import { ChainClient, ChainError, asText, protoDir } from '../src/index.js';
|
||||
|
||||
// `protoDir` is found by searching, so it is the one path that holds
|
||||
// whether this runs from `src`, `dist` or a test build.
|
||||
const sdkRoot = resolve(protoDir, '..');
|
||||
const platformRepo = resolve(
|
||||
process.env.CHAIN_PLATFORM_REPO ?? join(sdkRoot, '..', 'fai_chain'),
|
||||
);
|
||||
const hubBinary = resolve(process.env.CHAIN_BIN ?? join(platformRepo, 'target', 'debug', 'chain'));
|
||||
const modulesDir = join(platformRepo, 'modules');
|
||||
const echoWasm = join(modulesDir, 'echo', 'target', 'wasm32-wasip2', 'release', 'echo.wasm');
|
||||
const available = existsSync(hubBinary) && existsSync(echoWasm);
|
||||
|
||||
const HELLO = `name: hello
|
||||
inputs:
|
||||
name: text
|
||||
steps:
|
||||
- id: greet
|
||||
use: debug.echo@^0
|
||||
with:
|
||||
message: "Hello, $inputs.name"
|
||||
outputs:
|
||||
greeting: $greet.echoed
|
||||
`;
|
||||
|
||||
async function freePort(): Promise<number> {
|
||||
return new Promise((resolvePort, reject) => {
|
||||
const server = createServer();
|
||||
server.on('error', reject);
|
||||
server.listen(0, '127.0.0.1', () => {
|
||||
const address = server.address();
|
||||
const port = typeof address === 'object' && address ? address.port : 0;
|
||||
server.close(() => resolvePort(port));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('client against a real hub', { skip: available ? false : `no hub binary at ${hubBinary}` }, () => {
|
||||
let hub: ChildProcess | undefined;
|
||||
let client: ChainClient;
|
||||
let dataDir = '';
|
||||
|
||||
before(async () => {
|
||||
const port = await freePort();
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'chain-ts-e2e-'));
|
||||
hub = spawn(
|
||||
hubBinary,
|
||||
['serve', '--bind', `127.0.0.1:${port}`, '--modules-dir', modulesDir, '--data-dir', join(dataDir, 'chain.db')],
|
||||
{ stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, CHAIN_DATA_DIR: dataDir } },
|
||||
);
|
||||
client = new ChainClient({ address: `127.0.0.1:${port}`, deadlineMs: 20_000 });
|
||||
const until = Date.now() + 30_000;
|
||||
for (;;) {
|
||||
try {
|
||||
const health = await client.health(2_000);
|
||||
if (health.state === 'SERVING') break;
|
||||
} catch {
|
||||
// not up yet
|
||||
}
|
||||
if (Date.now() > until) throw new Error('the hub did not start');
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
}
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
client?.close();
|
||||
hub?.kill('SIGTERM');
|
||||
if (dataDir) rmSync(dataDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('runs the hello flow and reads the output', async () => {
|
||||
const result = await client.submit({ flowYaml: HELLO, inputs: { name: 'Ch∆In' } });
|
||||
assert.equal(asText(result.outputs.greeting), 'Hello, Ch∆In');
|
||||
});
|
||||
|
||||
it('follows the same flow step by step', async () => {
|
||||
const steps: string[] = [];
|
||||
let greeting: string | undefined;
|
||||
for await (const event of client.submitStream({ flowYaml: HELLO, inputs: { name: 'stream' } })) {
|
||||
if (event.type === 'stepStarted') steps.push(event.step);
|
||||
if (event.type === 'result') greeting = asText(event.result.outputs.greeting);
|
||||
}
|
||||
assert.deepEqual(steps, ['greet']);
|
||||
assert.equal(greeting, 'Hello, stream');
|
||||
});
|
||||
|
||||
it('gets the real error class out of a real refusal', async () => {
|
||||
const err = (await client
|
||||
.submit({ flowYaml: 'this is not a flow [' })
|
||||
.catch((e: unknown) => e)) as ChainError;
|
||||
assert.ok(err instanceof ChainError);
|
||||
assert.equal(err.kind, 'flow_rejected');
|
||||
assert.equal(err.reachedHub, true);
|
||||
assert.equal(err.retryable, false);
|
||||
});
|
||||
|
||||
it('refuses a flow whose module is not installed, as not_found', async () => {
|
||||
const err = (await client
|
||||
.submit({
|
||||
flowYaml: `name: nowhere\nsteps:\n - id: x\n use: nothing.at.all/missing@^1\noutputs:\n out: $x.text\n`,
|
||||
})
|
||||
.catch((e: unknown) => e)) as ChainError;
|
||||
assert.ok(err instanceof ChainError);
|
||||
assert.equal(err.reachedHub, true);
|
||||
assert.ok(
|
||||
err.kind === 'not_found' || err.kind === 'flow_rejected',
|
||||
`an unknown capability reads as not_found or flow_rejected, got ${err.kind}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue