frontend-app-api: make rootNodeId required in resolveAppGraph

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-18 19:20:12 +02:00
parent 89dc1250d8
commit ee39751fdf
4 changed files with 78 additions and 92 deletions
@@ -36,13 +36,13 @@ export interface CreateAppGraphOptions {
/** @internal */
export function createAppGraph(options: CreateAppGraphOptions): AppGraph {
const appGraph = resolveAppGraph(
'core',
resolveAppNodeSpecs({
features: options.features,
builtinExtensions: options.builtinExtensions,
parameters: readAppExtensionsConfig(options.config),
forbidden: new Set(['core']),
}),
'core',
);
instantiateAppNodeTree(appGraph.root);
return appGraph;
@@ -79,10 +79,9 @@ function makeInstanceWithId<TConfig>(
describe('instantiateAppNodeTree', () => {
it('should instantiate a single node', () => {
const graph = resolveAppGraph(
[{ ...makeSpec(simpleExtension), id: 'root-node' }],
'root-node',
);
const graph = resolveAppGraph('root-node', [
{ ...makeSpec(simpleExtension), id: 'root-node' },
]);
expect(graph.root.instance).not.toBeDefined();
instantiateAppNodeTree(graph.root);
expect(graph.root.instance).toBeDefined();
@@ -94,43 +93,39 @@ describe('instantiateAppNodeTree', () => {
});
it('should not instantiate disabled nodes', () => {
const graph = resolveAppGraph(
[{ ...makeSpec(simpleExtension), id: 'root-node', disabled: true }],
'root-node',
);
const graph = resolveAppGraph('root-node', [
{ ...makeSpec(simpleExtension), id: 'root-node', disabled: true },
]);
expect(graph.root.instance).not.toBeDefined();
instantiateAppNodeTree(graph.root);
expect(graph.root.instance).not.toBeDefined();
});
it('should instantiate a node with attachments', () => {
const graph = resolveAppGraph(
[
{
...makeSpec(
createExtension({
id: 'root-node',
attachTo: { id: 'ignored', input: 'ignored' },
inputs: {
test: createExtensionInput({ test: testDataRef }),
},
output: {
inputMirror: inputMirrorDataRef,
},
factory({ bind, inputs }) {
bind({ inputMirror: inputs });
},
}),
),
},
{
...makeSpec(simpleExtension),
id: 'child-node',
attachTo: { id: 'root-node', input: 'test' },
},
],
'root-node',
);
const graph = resolveAppGraph('root-node', [
{
...makeSpec(
createExtension({
id: 'root-node',
attachTo: { id: 'ignored', input: 'ignored' },
inputs: {
test: createExtensionInput({ test: testDataRef }),
},
output: {
inputMirror: inputMirrorDataRef,
},
factory({ bind, inputs }) {
bind({ inputMirror: inputs });
},
}),
),
},
{
...makeSpec(simpleExtension),
id: 'child-node',
attachTo: { id: 'root-node', input: 'test' },
},
]);
const childNode = graph.nodes.get('child-node');
expect(childNode).toBeDefined();
@@ -151,34 +146,31 @@ describe('instantiateAppNodeTree', () => {
});
it('should not instantiate disabled attachments', () => {
const graph = resolveAppGraph(
[
{
...makeSpec(
createExtension({
id: 'root-node',
attachTo: { id: 'ignored', input: 'ignored' },
inputs: {
test: createExtensionInput({ test: testDataRef }),
},
output: {
inputMirror: inputMirrorDataRef,
},
factory({ bind, inputs }) {
bind({ inputMirror: inputs });
},
}),
),
},
{
...makeSpec(simpleExtension),
id: 'child-node',
attachTo: { id: 'root-node', input: 'test' },
disabled: true,
},
],
'root-node',
);
const graph = resolveAppGraph('root-node', [
{
...makeSpec(
createExtension({
id: 'root-node',
attachTo: { id: 'ignored', input: 'ignored' },
inputs: {
test: createExtensionInput({ test: testDataRef }),
},
output: {
inputMirror: inputMirrorDataRef,
},
factory({ bind, inputs }) {
bind({ inputMirror: inputs });
},
}),
),
},
{
...makeSpec(simpleExtension),
id: 'child-node',
attachTo: { id: 'root-node', input: 'test' },
disabled: true,
},
]);
const childNode = graph.nodes.get('child-node');
expect(childNode).toBeDefined();
@@ -34,13 +34,13 @@ const baseSpec = {
describe('buildAppGraph', () => {
it('should fail to create an empty graph', () => {
expect(() => resolveAppGraph([])).toThrow(
expect(() => resolveAppGraph('core', [])).toThrow(
"No root node with id 'core' found in app graph",
);
});
it('should create a graph with only one node', () => {
const graph = resolveAppGraph([{ ...baseSpec, id: 'core' }]);
const graph = resolveAppGraph('core', [{ ...baseSpec, id: 'core' }]);
expect(graph.root).toEqual({
spec: { ...baseSpec, id: 'core' },
edges: { attachments: new Map() },
@@ -50,18 +50,15 @@ describe('buildAppGraph', () => {
});
it('should create a graph', () => {
const graph = resolveAppGraph(
[
{ ...baseSpec, id: 'a' },
{ ...baseSpec, id: 'b' },
{ ...baseSpec, id: 'c' },
{ ...baseSpec, attachTo: { id: 'b', input: 'x' }, id: 'bx1' },
{ ...baseSpec, attachTo: { id: 'b', input: 'x' }, id: 'bx2' },
{ ...baseSpec, attachTo: { id: 'b', input: 'y' }, id: 'by1' },
{ ...baseSpec, attachTo: { id: 'd', input: 'x' }, id: 'dx1' },
],
'b',
);
const graph = resolveAppGraph('b', [
{ ...baseSpec, id: 'a' },
{ ...baseSpec, id: 'b' },
{ ...baseSpec, id: 'c' },
{ ...baseSpec, attachTo: { id: 'b', input: 'x' }, id: 'bx1' },
{ ...baseSpec, attachTo: { id: 'b', input: 'x' }, id: 'bx2' },
{ ...baseSpec, attachTo: { id: 'b', input: 'y' }, id: 'by1' },
{ ...baseSpec, attachTo: { id: 'd', input: 'x' }, id: 'dx1' },
]);
expect(Array.from(graph.nodes.keys())).toEqual([
'a',
@@ -116,18 +113,15 @@ describe('buildAppGraph', () => {
});
it('should create a graph out of order', () => {
const graph = resolveAppGraph(
[
{ ...baseSpec, attachTo: { id: 'b', input: 'x' }, id: 'bx2' },
{ ...baseSpec, id: 'a' },
{ ...baseSpec, attachTo: { id: 'b', input: 'y' }, id: 'by1' },
{ ...baseSpec, id: 'b' },
{ ...baseSpec, attachTo: { id: 'b', input: 'x' }, id: 'bx1' },
{ ...baseSpec, id: 'c' },
{ ...baseSpec, attachTo: { id: 'd', input: 'x' }, id: 'dx1' },
],
'b',
);
const graph = resolveAppGraph('b', [
{ ...baseSpec, attachTo: { id: 'b', input: 'x' }, id: 'bx2' },
{ ...baseSpec, id: 'a' },
{ ...baseSpec, attachTo: { id: 'b', input: 'y' }, id: 'by1' },
{ ...baseSpec, id: 'b' },
{ ...baseSpec, attachTo: { id: 'b', input: 'x' }, id: 'bx1' },
{ ...baseSpec, id: 'c' },
{ ...baseSpec, attachTo: { id: 'd', input: 'x' }, id: 'dx1' },
]);
expect(Array.from(graph.nodes.keys())).toEqual([
'bx2',
@@ -163,7 +157,7 @@ describe('buildAppGraph', () => {
it('throws an error when duplicated extensions are detected', () => {
expect(() =>
resolveAppGraph([
resolveAppGraph('core', [
{ ...baseSpec, id: 'a' },
{ ...baseSpec, id: 'a' },
]),
@@ -88,8 +88,8 @@ class SerializableAppNode implements AppNode {
* @internal
*/
export function resolveAppGraph(
rootNodeId: string,
specs: AppNodeSpec[],
rootNodeId = 'core',
): AppGraph {
const nodes = new Map<string, SerializableAppNode>();