From ee39751fdffa80b0a6f0535ea520863887958fcf Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 18 Oct 2023 19:20:12 +0200 Subject: [PATCH] frontend-app-api: make rootNodeId required in resolveAppGraph Signed-off-by: Patrik Oldsberg --- .../src/graph/createAppGraph.ts | 2 +- .../src/graph/instantiateAppNodeTree.test.ts | 118 ++++++++---------- .../src/graph/resolveAppGraph.test.ts | 48 ++++--- .../src/graph/resolveAppGraph.ts | 2 +- 4 files changed, 78 insertions(+), 92 deletions(-) diff --git a/packages/frontend-app-api/src/graph/createAppGraph.ts b/packages/frontend-app-api/src/graph/createAppGraph.ts index cb72fa3cd1..ace8595f53 100644 --- a/packages/frontend-app-api/src/graph/createAppGraph.ts +++ b/packages/frontend-app-api/src/graph/createAppGraph.ts @@ -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; diff --git a/packages/frontend-app-api/src/graph/instantiateAppNodeTree.test.ts b/packages/frontend-app-api/src/graph/instantiateAppNodeTree.test.ts index 7bffc69c7c..21f364dda2 100644 --- a/packages/frontend-app-api/src/graph/instantiateAppNodeTree.test.ts +++ b/packages/frontend-app-api/src/graph/instantiateAppNodeTree.test.ts @@ -79,10 +79,9 @@ function makeInstanceWithId( 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(); diff --git a/packages/frontend-app-api/src/graph/resolveAppGraph.test.ts b/packages/frontend-app-api/src/graph/resolveAppGraph.test.ts index 2d1acbbc04..2668c51863 100644 --- a/packages/frontend-app-api/src/graph/resolveAppGraph.test.ts +++ b/packages/frontend-app-api/src/graph/resolveAppGraph.test.ts @@ -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' }, ]), diff --git a/packages/frontend-app-api/src/graph/resolveAppGraph.ts b/packages/frontend-app-api/src/graph/resolveAppGraph.ts index 18f481fa30..354b4c53ee 100644 --- a/packages/frontend-app-api/src/graph/resolveAppGraph.ts +++ b/packages/frontend-app-api/src/graph/resolveAppGraph.ts @@ -88,8 +88,8 @@ class SerializableAppNode implements AppNode { * @internal */ export function resolveAppGraph( + rootNodeId: string, specs: AppNodeSpec[], - rootNodeId = 'core', ): AppGraph { const nodes = new Map();