import { ContextNode, EndpointNode, ListNode, loadNode, MapNode, ReferenceNode, ValueNode, InstanceNode, FilterNode, SortNode, SliceNode, AggregateNode, RelationNode, RelationNodeDef, SpecialsNode } from "./node"; import {loadUniverseYaml} from "./universe"; import {universeDef} from "./universe.test"; test('Serialization of valueNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; const valueDef = valueNode.getDefinition()!; expect(valueDef.type).toEqual('value'); expect(valueDef.value).toBeTruthy(); expect(valueDef.value?.field).toEqual('id'); expect(valueDef.value?.format).toEqual('hex'); const node2 = loadNode(valueDef, universe); expect(node2.getType()).toEqual('value'); expect(node2 instanceof ValueNode).toBeTruthy(); const valueNode2 = node2 as ValueNode; expect(valueNode2.field).toEqual('id'); expect(valueNode2.format).toEqual('hex'); }); test('Serialization of instanceNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const instanceNode = new InstanceNode(); instanceNode.format = 'string'; instanceNode.switches = [ {value: 'a', type: 'image'} ]; const valueDef = instanceNode.getDefinition()!; expect(valueDef.type).toEqual('instance'); expect(valueDef.instance).toBeTruthy(); expect(valueDef.instance?.format).toEqual('string'); const node2 = loadNode(valueDef, universe); expect(node2.getType()).toEqual('instance'); expect(node2 instanceof InstanceNode).toBeTruthy(); const instanceNode2 = node2 as InstanceNode; expect(instanceNode2.format).toEqual('string'); expect(instanceNode2.switches).toHaveLength(1); expect(instanceNode2.switches![0].value).toEqual('a'); expect(instanceNode2.switches![0].type).toEqual('image'); }); test('Serialization of endpointNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const endpointNode = new EndpointNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; endpointNode.type = 'put'; endpointNode.node = valueNode; endpointNode.name = 'entry1'; endpointNode.context = { environment: { type: 'primitive', model: universe.getModel('model'), }, value: undefined, }; endpointNode.parameters = [{name: 'p1', description: '', interface: {type: 'primitive', primitive: {value: 'string'}}}]; endpointNode.interface = {type: 'primitive', primitive: {value: 'integer'}}; const entryDef = endpointNode.getDefinition()!; expect(entryDef.type).toEqual('endpoint'); expect(entryDef.endpoint?.type).toEqual('put'); expect(entryDef.endpoint?.name).toEqual('entry1'); expect(entryDef.endpoint?.context.environment.model).toEqual('model'); expect(entryDef.endpoint?.node.type).toEqual('value'); expect(entryDef.endpoint?.parameters).toHaveLength(1); expect(entryDef.endpoint?.interface?.type).toEqual('primitive'); const node2 = loadNode(entryDef, universe); expect(node2.getType()).toEqual('endpoint'); expect(node2 instanceof EndpointNode).toBeTruthy(); const entryValue2 = node2 as EndpointNode; expect(entryValue2.type).toEqual('put'); expect(entryValue2.context?.environment.model).toEqual(universe.getModel('model')); expect(entryValue2.parameters).toHaveLength(1); expect(entryValue2.interface?.type).toEqual('primitive'); }); test('Serialization of referenceNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const entryNode = new EndpointNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; entryNode.node = valueNode; entryNode.name = 'entry1'; entryNode.context = { environment: { type: 'primitive', model: universe.getModel('model'), }, value: undefined, }; universe.endpoints.map.set(entryNode.name, entryNode); const referenceNode = new ReferenceNode(); referenceNode.node = entryNode; const referenceDef = referenceNode.getDefinition()!; expect(referenceDef.type).toEqual('reference'); expect(referenceDef.reference).toBeTruthy(); expect(referenceDef.reference?.name).toEqual('entry1'); const node2 = loadNode(referenceDef, universe); expect(node2.getType()).toEqual('reference'); expect(node2 instanceof ReferenceNode).toBeTruthy(); const referenceNode2 = node2 as ReferenceNode; expect(referenceNode2.node).toEqual(entryNode); }); test('Serialization of relationNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const relationNode = new RelationNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; relationNode.node = valueNode; relationNode.type = 'primitive'; relationNode.reverse = true; relationNode.relation = universe.getModel('image#format')!.getIdentifier(); const relationDef = relationNode.getDefinition()!; expect(relationDef.type).toEqual('relation'); expect(relationDef.relation?.type).toEqual('primitive'); expect(relationDef.relation?.reverse).toEqual(true); expect(relationDef.relation?.relation).toEqual('vision.image#format/vision'); const node2 = loadNode(relationDef, universe); expect(node2.getType()).toEqual('relation'); expect(node2 instanceof RelationNode).toBeTruthy(); const relationNode2 = node2 as RelationNode; expect(relationNode2.node?.getType()).toEqual('value'); expect(relationNode2.type).toEqual('primitive'); expect(relationNode2.reverse).toEqual(true); expect(relationNode2.relation).toEqual(universe.getModel('image#format')!.getIdentifier()); }); test('Serialization of specialsNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const specialsNode = new SpecialsNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; specialsNode.node = valueNode; specialsNode.type = 'list'; specialsNode.direct = true; specialsNode.indirect = true; const specialsDef = specialsNode.getDefinition()!; expect(specialsDef.type).toEqual('specials'); expect(specialsDef.specials?.type).toEqual('list'); expect(specialsDef.specials?.direct).toEqual(true); expect(specialsDef.specials?.indirect).toEqual(true); const node2 = loadNode(specialsDef, universe); expect(node2.getType()).toEqual('specials'); expect(node2 instanceof SpecialsNode).toBeTruthy(); const specialsNode2 = node2 as SpecialsNode; expect(specialsNode2.type).toEqual('list'); expect(specialsNode2.direct).toEqual(true); expect(specialsNode2.indirect).toEqual(true); expect(specialsNode2.node?.getType()).toEqual('value'); }); test('Serialization of filterNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const filterNode = new FilterNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; filterNode.node = valueNode; filterNode.filter = { source: { type: 'node', format: 'string', node: valueNode, }, value: 'a', operator: 'gt', } const filterDef = filterNode.getDefinition()!; expect(filterDef.type).toEqual('filter'); expect(filterDef.filter?.node.type).toEqual('value'); expect(filterDef.filter?.filter.source.node?.type).toEqual('value'); expect(filterDef.filter?.filter.source.format).toEqual('string'); expect(filterDef.filter?.filter.operator).toEqual('gt'); expect(filterDef.filter?.filter.value).toEqual('a'); const node2 = loadNode(filterDef, universe); expect(node2.getType()).toEqual('filter'); expect(node2 instanceof FilterNode).toBeTruthy(); const filterNode2 = node2 as FilterNode; expect(filterNode2.node?.getType()).toEqual('value'); expect(filterNode2.filter?.source.type).toEqual('node'); expect(filterNode2.filter?.source.format).toEqual('string'); expect(filterNode2.filter?.source.node?.getType()).toEqual('value'); expect(filterNode2.filter?.operator).toEqual('gt'); expect(filterNode2.filter?.value).toEqual('a'); }); test('Serialization of sortNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const sortNode = new SortNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; sortNode.node = valueNode; sortNode.order = { source: { type: 'node', format: 'string', node: valueNode, }, descending: true, } const sortDef = sortNode.getDefinition()!; expect(sortDef.type).toEqual('sort'); expect(sortDef.sort?.node.type).toEqual('value'); expect(sortDef.sort?.order.source.node?.type).toEqual('value'); expect(sortDef.sort?.order.descending).toEqual(true); const node2 = loadNode(sortDef, universe); expect(node2.getType()).toEqual('sort'); expect(node2 instanceof SortNode).toBeTruthy(); const sortNode2 = node2 as SortNode; expect(sortNode2.node?.getType()).toEqual('value'); expect(sortNode2.order?.source.node?.getType()).toEqual('value'); expect(sortNode2.order?.descending).toEqual(true); }); test('Serialization of sliceNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const sliceNode = new SliceNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; sliceNode.node = valueNode; sliceNode.offset = 2; sliceNode.limit = 3; const sliceDef = sliceNode.getDefinition()!; expect(sliceDef.type).toEqual('slice'); expect(sliceDef.slice?.node.type).toEqual('value'); expect(sliceDef.slice?.offset).toEqual(2); expect(sliceDef.slice?.limit).toEqual(3); const node2 = loadNode(sliceDef, universe); expect(node2.getType()).toEqual('slice'); expect(node2 instanceof SliceNode).toBeTruthy(); const sliceNode2 = node2 as SliceNode; expect(sliceNode2.node?.getType()).toEqual('value'); expect(sliceNode2.offset).toEqual(2); expect(sliceNode2.limit).toEqual(3); }); test('Serialization of aggregateNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const aggNode = new AggregateNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; aggNode.source = { type: 'node', format: 'string', node: valueNode, }; aggNode.function = 'count'; const aggDef = aggNode.getDefinition()!; expect(aggDef.type).toEqual('aggregate'); expect(aggDef.aggregate?.source.node?.type).toEqual('value'); expect(aggDef.aggregate?.function).toEqual('count'); const node2 = loadNode(aggDef, universe); expect(node2.getType()).toEqual('aggregate'); expect(node2 instanceof AggregateNode).toBeTruthy(); const aggNode2 = node2 as AggregateNode; expect(aggNode2.source?.node?.getType()).toEqual('value'); expect(aggNode2.function).toEqual('count'); }); test('Serialization of listNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const listNode = new ListNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; listNode.entry = valueNode; const listDef = listNode.getDefinition()!; expect(listDef.type).toEqual('list'); expect(listDef.list?.entry.type).toEqual('value'); const node2 = loadNode(listDef, universe); expect(node2.getType()).toEqual('list'); expect(node2 instanceof ListNode).toBeTruthy(); const listNode2 = node2 as ListNode; expect(listNode2.entry?.getType()).toEqual('value'); }); test('Serialization of mapNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const mapNode = new MapNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; mapNode.entries = []; mapNode.entries.push({name: 'a', node: valueNode}); const mapDef = mapNode.getDefinition()!; expect(mapDef.type).toEqual('map'); expect(mapDef.map?.entries[0].name).toEqual('a'); const node2 = loadNode(mapDef, universe); expect(node2.getType()).toEqual('map'); expect(node2 instanceof MapNode).toBeTruthy(); const mapNode2 = node2 as MapNode; expect(mapNode2.entries![0].node.getType()).toEqual('value'); }); test('Resolution of relationNode works', async () => { const universe = loadUniverseYaml(universeDef)!; const relationNode = new RelationNode(); const valueNode = new ValueNode(); valueNode.field = 'id'; valueNode.format = 'hex'; relationNode.node = valueNode; relationNode.type = 'primitive'; relationNode.reverse = true; relationNode.relation = 'vision.model/vision'; const relationDef = relationNode.resolveDefinition(universe); expect(relationDef?.type).toEqual('relation'); expect(relationDef?.relation?.type).toEqual('primitive'); expect(relationDef?.relation?.reverse).toEqual(true); expect(relationDef?.relation?.relation).toEqual(universe.resolve('vision.model/')); }); test('Resolution of instance node works', async () => { const universe = loadUniverseYaml(universeDef)!; const instanceNode = new InstanceNode(); instanceNode.format = 'string'; instanceNode.switches = [ {value: 'a', type: 'image'} ]; const contextDef = instanceNode.resolveDefinition(universe); expect(contextDef?.type).toEqual('instance'); expect(contextDef?.instance!.format).toEqual('string'); expect(contextDef?.instance!.switches).toHaveLength(1); expect(contextDef?.instance!.switches![0].value).toEqual('a'); expect(contextDef?.instance!.switches![0].type).toEqual(universe.resolve('image')); });