2024-11-20 10:54:35 +01:00
|
|
|
import { before, describe, it } from 'node:test'
|
|
|
|
import { addComponent, addEntity, createWorld, defineComponent, Types } from 'bitecs'
|
|
|
|
import assert from './assert.js'
|
|
|
|
import { query } from '../src/query.js'
|
|
|
|
|
|
|
|
let engine = {}
|
|
|
|
describe('query', () => {
|
|
|
|
before(() => {
|
|
|
|
const world = { default: createWorld() }
|
|
|
|
const component = {
|
|
|
|
Player: defineComponent(null, 10),
|
|
|
|
NPC: defineComponent(null, 10),
|
|
|
|
Knows: defineComponent({
|
|
|
|
from: Types.eid,
|
|
|
|
to: Types.eid
|
|
|
|
}, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
engine = { world, component }
|
|
|
|
})
|
|
|
|
|
|
|
|
it('i just need a test runner', () => {
|
|
|
|
const w = engine.world.default
|
|
|
|
const { Player, NPC, Knows } = engine.component
|
|
|
|
|
|
|
|
const create = (...components) => {
|
|
|
|
const eid = addEntity(w)
|
|
|
|
components.forEach(cmp => addComponent(w, cmp, eid))
|
|
|
|
return eid
|
|
|
|
}
|
|
|
|
|
|
|
|
const knows = (a, b) => {
|
|
|
|
const edge = addEntity(w)
|
|
|
|
addComponent(w, Knows, edge)
|
|
|
|
Knows.from[edge] = a
|
|
|
|
Knows.to[edge] = b
|
|
|
|
return edge
|
|
|
|
}
|
|
|
|
|
|
|
|
const player = create(Player) // 0
|
|
|
|
const a = create(NPC) // 1
|
|
|
|
const b = create(NPC) // 2
|
|
|
|
const c = create(NPC) // 3
|
|
|
|
|
|
|
|
knows(player, a) // 4
|
|
|
|
knows(player, c) // 5
|
|
|
|
|
|
|
|
knows(a, player) // 6
|
|
|
|
knows(a, b) // 7
|
|
|
|
knows(a, c) // 8
|
|
|
|
|
|
|
|
knows(b, a) // 9
|
|
|
|
|
|
|
|
knows(c, player) // 10
|
|
|
|
|
2024-11-20 20:17:53 +01:00
|
|
|
const q = 'MATCH (player:Player)-[e1:Knows]->(a:NPC)-[e2:Knows]->(b:NPC) RETURN player, e1, a, e2, b'
|
|
|
|
const result = query(q, engine)
|
|
|
|
console.log(`parsing ${q}\n`, result.unwrap())
|
2024-11-20 10:54:35 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|