topoSort validated each need with producedBy.has(key), and the visit skipped a dependency whose producer was the node itself. So a step with needs: ['user'] and provides: ['user'], and nothing else providing user, passed the graph check and booted. At request time it read a value nobody had written, where every other missing dependency fails at boot. The check now treats a node's own token as missing unless it is seeded. The issue also allowed "provided by another node", but since CARD-18 (GitHub #35) a token has exactly one producer, so a second node that provides it fails boot on its own; the self-producer is the only producer there can be. A seeded token (req, params) still satisfies the need, because the step reads the seed from the context before its output is merged over it. The visit keeps skipping the node itself for that case, so it is not reported as a cycle. overrides do not exempt the node. They replace a runner after finalizeGraph has run topoSort, and change no needs, so the graph check sees the same node either way. BREAKING CHANGE: an app with a node that needs a token only it provides now fails createApp with "missing dependency: <token> needed by <node> — it provides '<token>' itself". Remove the token from the node's needs, or move the work that produces it into a separate provider or step. Refs: GitHub #43 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
126 lines
5.5 KiB
TypeScript
126 lines
5.5 KiB
TypeScript
import { describe, expect, it, test } from 'vitest';
|
|
import { topoSort, topoLevels, GraphNode, subgraphFor, nearest } from '../src/graph';
|
|
|
|
const node = (name: string, needs: string[], provides: string[]): GraphNode =>
|
|
({ name, needs, provides, origin: 'test' });
|
|
|
|
test('orders nodes so dependencies come first (regardless of input order)', () => {
|
|
const nodes = [
|
|
node('handler', ['user'], []),
|
|
node('auth', ['db'], ['user']),
|
|
node('db', ['config'], ['db']),
|
|
];
|
|
const ordered = topoSort(nodes, ['config']).map((n) => n.name);
|
|
expect(ordered).toEqual(['db', 'auth', 'handler']);
|
|
});
|
|
|
|
test('throws on missing dependency', () => {
|
|
const nodes = [node('handler', ['user'], [])];
|
|
expect(() => topoSort(nodes, [])).toThrow(/missing dependency: user needed by handler/);
|
|
});
|
|
|
|
test('throws on cycle', () => {
|
|
const nodes = [node('a', ['b'], ['a']), node('b', ['a'], ['b'])];
|
|
expect(() => topoSort(nodes, [])).toThrow(/cycle detected/);
|
|
});
|
|
|
|
// CARD-18 (GitHub #35, D9): the check is a rule over the whole node set, not an artefact of the
|
|
// order nodes were registered in — so swapping `provideBuiltins` and `mountPlugins` cannot reopen it.
|
|
describe('one producer per token', () => {
|
|
const builtin: GraphNode = { name: 'logger', needs: [], provides: ['logger'], origin: 'builtin' };
|
|
const plugin: GraphNode = { name: 'fake', needs: [], provides: ['logger'], origin: 'plugin' };
|
|
|
|
it.each([
|
|
['built-in first', [builtin, plugin]],
|
|
['plugin first', [plugin, builtin]],
|
|
])('throws naming both producers and origins (%s)', (_, nodes) => {
|
|
expect(() => topoSort(nodes, [])).toThrow(/token 'logger' is provided twice/);
|
|
expect(() => topoSort(nodes, [])).toThrow(/'logger' \(builtin\)/);
|
|
expect(() => topoSort(nodes, [])).toThrow(/'fake' \(plugin\)/);
|
|
});
|
|
|
|
it('names a mesh origin like any other', () => {
|
|
const local: GraphNode = { name: 'auth', needs: [], provides: ['auth'], origin: 'module:App' };
|
|
const remote: GraphNode = { name: 'auth-remote', needs: [], provides: ['auth'], origin: 'mesh:ws://t' };
|
|
expect(() => topoSort([local, remote], [])).toThrow(
|
|
"token 'auth' is provided twice: by 'auth' (module:App) and by 'auth-remote' (mesh:ws://t)",
|
|
);
|
|
});
|
|
|
|
it('allows a node to list its own token twice', () => {
|
|
expect(topoSort([node('a', [], ['a', 'a'])], []).map((n) => n.name)).toEqual(['a']);
|
|
});
|
|
});
|
|
|
|
// GitHub #43: a node that needs its own token reads a value nobody wrote. With one producer per
|
|
// token, nothing else can provide it either, so only a seed makes the need real.
|
|
describe('self-dependency', () => {
|
|
it('counts as missing when the token is not seeded', () => {
|
|
expect(() => topoSort([node('auth', ['user'], ['user'])], [])).toThrow(
|
|
/missing dependency: user needed by auth — it provides 'user' itself/,
|
|
);
|
|
});
|
|
|
|
it('passes when the token is seeded, since the node reads the seed', () => {
|
|
expect(topoSort([node('normalize', ['params'], ['params'])], ['req', 'params']).map((n) => n.name)).toEqual([
|
|
'normalize',
|
|
]);
|
|
});
|
|
});
|
|
|
|
const N = (name: string, needs: string[] = []) => ({ name, needs, provides: [name], origin: 'm' });
|
|
|
|
describe('subgraphFor', () => {
|
|
const ordered = [N('config'), N('db', ['config']), N('user', ['db', 'req']), N('audit', ['db'])];
|
|
it('returns the transitive closure of needs, in the given order', () => {
|
|
expect(subgraphFor(['user'], ordered).map((n) => n.name)).toEqual(['config', 'db', 'user']);
|
|
});
|
|
it('returns empty for no needs', () => {
|
|
expect(subgraphFor([], ordered)).toEqual([]);
|
|
});
|
|
it('ignores tokens with no producer (seeds/envelope)', () => {
|
|
expect(subgraphFor(['user', 'query', 'req'], ordered).map((n) => n.name)).toEqual(['config', 'db', 'user']);
|
|
});
|
|
it('does not pull in unrelated nodes', () => {
|
|
expect(subgraphFor(['user'], ordered).map((n) => n.name)).not.toContain('audit');
|
|
});
|
|
});
|
|
|
|
describe('topoLevels', () => {
|
|
const levels = (ordered: GraphNode[]): string[][] => topoLevels(ordered).map((l) => l.map((n) => n.name));
|
|
|
|
it('puts nodes that cannot constrain each other in the same level', () => {
|
|
expect(levels([N('a'), N('b'), N('c')])).toEqual([['a', 'b', 'c']]);
|
|
});
|
|
|
|
it('pushes a node past every producer it needs, not just its first', () => {
|
|
// `late` needs a depth-0 and a depth-1 node, so it belongs at 2 — taking the max, not the first.
|
|
const ordered = [N('config'), N('db', ['config']), N('late', ['config', 'db'])];
|
|
expect(levels(ordered)).toEqual([['config'], ['db'], ['late']]);
|
|
});
|
|
|
|
it('ignores needs nothing here produces, so a seed does not push a node down a level', () => {
|
|
expect(levels([N('a'), N('user', ['req', 'params'])])).toEqual([['a', 'user']]);
|
|
});
|
|
|
|
it('leaves no gaps, so every level has something to run', () => {
|
|
const ordered = topoSort([N('c', ['b']), N('a'), N('b', ['a']), N('solo')], []);
|
|
expect(topoLevels(ordered).every((level) => level.length > 0)).toBe(true);
|
|
});
|
|
|
|
it('keeps every node exactly once', () => {
|
|
const ordered = topoSort([N('c', ['b']), N('a'), N('b', ['a']), N('solo')], []);
|
|
expect(topoLevels(ordered).flat().map((n) => n.name).sort()).toEqual(['a', 'b', 'c', 'solo']);
|
|
});
|
|
});
|
|
|
|
describe('nearest', () => {
|
|
it('suggests a close candidate (edit distance <= 2)', () => {
|
|
expect(nearest('usr', ['user', 'config', 'db'])).toBe('user');
|
|
expect(nearest('confgi', ['user', 'config'])).toBe('config');
|
|
});
|
|
it('returns undefined when nothing is close', () => {
|
|
expect(nearest('zzzzzz', ['user', 'db'])).toBeUndefined();
|
|
});
|
|
});
|