import { describe, it } from 'node:test' import assert from '../assert.js' import { node, edge, statement } from '../../src/query/match.js' import { makeEdge, makeNode } from '../utils.js' describe('node', () => { it('should match a node with a name, label, and properties', () => { assert.parseOk(node, '(:Node)', ([actual]) => { const expected = makeNode(undefined, 'Node') assert.deepEqual(actual, expected) assert.parseOk(node, '(node:Node)', ([actual]) => { const expected = makeNode('node', 'Node') assert.deepEqual(actual, expected) }) assert.parseOk(node, '(:Label { name: "Rowan", gay: true })', ([actual]) => { const expected = makeNode(undefined, 'Label', [['name', 'Rowan'], ['gay', true]]) assert.deepEqual(actual, expected) }) assert.parseOk(node, '(name:Label { name: "Rowan", gay: true })', ([actual]) => { const expected = makeNode('name', 'Label', [['name', 'Rowan'], ['gay', true]]) assert.deepEqual(actual, expected) }) }) it('should handle whitespace', () => { assert.parseOk(node, '(name:Label { name: "Rowan", gay: true })', ([actual]) => { const expected = makeNode('name', 'Label', [['name', 'Rowan'], ['gay', true]]) assert.deepEqual(actual, expected) }) }) }) }) describe('edge', () => { it('should match a relationship with a name, label, properties', () => { assert.parseOk(edge, '[name:Label]', ([actual]) => { const expected = makeEdge('name', 'Label', []) assert.deepEqual(actual, expected) }) assert.parseOk(edge, '[:Label]', ([actual]) => { const expected = makeEdge(undefined, 'Label', []) assert.deepEqual(actual, expected) }) assert.parseOk(edge, '[gay:Queer { binary: "-∞" }]', ([actual]) => { const expected = makeEdge('gay', 'Queer', [['binary', '-∞']]) assert.deepEqual(actual, expected) }) assert.parseOk(edge, '[:Queer { binary: "-∞" }]', ([actual]) => { const expected = makeEdge(undefined, 'Queer', [['binary', '-∞']]) assert.deepEqual(actual, expected) }) }) it('should handle whitespace', () => { assert.parseOk(edge, '[ : Queer { binary : "-∞" } ]', ([actual]) => { const expected = makeEdge(undefined, 'Queer', [['binary', '-∞']]) assert.deepEqual(actual, expected) }) }) }) describe('MATCH keyword', () => { it('should match a single node with no relationships', () => { assert.parseOk(statement, 'MATCH (:Label)', ([actual]) => { const expected = makeNode(undefined, 'Label') assert.deepEqual(actual, expected) }) assert.parseOk(statement, 'MATCH (node:Label)', ([actual]) => { const expected = makeNode('node', 'Label') assert.deepEqual(actual, expected) }) assert.parseOk(statement, 'MATCH (node:Label { prop: true, value: "test" })', ([actual]) => { const expected = makeNode('node', 'Label', [['prop', true], ['value', 'test']]) assert.deepEqual(actual, expected) }) }) it('should match nodes with a relationship to another another node', () => { assert.parseOk(statement, 'MATCH (:Node)-[:Edge]->(:Node)', actual => { const expected = [ makeNode(undefined, 'Node'), makeEdge(undefined, 'Edge'), makeNode(undefined, 'Node'), ] assert.deepEqual(actual, expected) }) assert.parseOk(statement, 'MATCH (a:Node)-[e:Edge]->(b:Node)', actual => { const expected = [ makeNode('a', 'Node'), makeEdge('e', 'Edge'), makeNode('b', 'Node'), ] assert.deepEqual(actual, expected) }) assert.parseOk(statement, 'MATCH (a:Node { db: 0.7 })-[e:Edge]->(b:Node { db: 0.95 })', actual => { const expected = [ makeNode('a', 'Node', [['db', 0.7]]), makeEdge('e', 'Edge'), makeNode('b', 'Node', [['db', 0.95]]), ] assert.deepEqual(actual, expected) }) assert.parseOk(statement, 'MATCH (:Node { db: 0.7 })-[:Edge]->(:Node { db: 0.95 })', actual => { const expected = [ makeNode(undefined, 'Node', [['db', 0.7]]), makeEdge(undefined, 'Edge'), makeNode(undefined, 'Node', [['db', 0.95]]), ] assert.deepEqual(actual, expected) }) }) it('should handle multiple relationships', () => { assert.parseOk(statement, 'MATCH (player:Player)-[:Knows]->(a:NPC)-[:Knows]->(b:NPC)', actual => { const expected = [ makeNode('player', 'Player'), makeEdge(undefined, 'Knows'), makeNode('a', 'NPC'), makeEdge(undefined, 'Knows'), makeNode('b', 'NPC'), ] assert.deepEqual(actual, expected) }) }) })