This commit is contained in:
Raven Scott
2026-05-20 21:02:45 -04:00
parent 1f3f4b24a2
commit 14d0980b4f
734 changed files with 682 additions and 746 deletions
@@ -0,0 +1,81 @@
require('bare-process/global')
const test = require('brittle')
const SpatialIndex = require('../index.js')
const b4a = require('b4a')
const { setTimeout } = require('bare-timers')
test('hyper-spatial-index basic insert and query', async (t) => {
const index = new SpatialIndex({ storageDir: '/tmp/spatial-test-' + Date.now() })
await index.ready()
await index.insert('p1', 100, 100, { name: 'TestPoint' })
await index.insert('p2', 1500, 1500, { name: 'FarPoint' })
await index.insert('p3', 200, 200, { name: 'NearPoint' })
const range = await index.rangeQuery(0, 0, 500, 500)
t.is(range.length, 2, 'range query returns correct points')
t.ok(range.some(p => p.id === 'p1'))
const nearest = await index.nearestNeighbor(120, 120, 2)
t.is(nearest.length, 2)
t.is(nearest[0].id, 'p1') // closest
// Test delete
const deleted = await index.deletePoint('p3')
t.ok(deleted, 'deletePoint returns true')
const afterDelete = await index.rangeQuery(0, 0, 500, 500)
t.is(afterDelete.length, 1, 'point removed after delete')
await index.close()
t.pass('closed successfully')
})
// New test for findInRadius (radius geospatial query improvement)
test('hyper-spatial-index findInRadius query', async (t) => {
const index = new SpatialIndex({ storageDir: '/tmp/spatial-radius-test-' + Date.now(), gridSize: 500 })
await index.ready()
await index.insert('center', 0, 0, { type: 'origin' })
await index.insert('close1', 100, 100, { type: 'near' })
await index.insert('close2', 200, 50, { type: 'near' })
await index.insert('far', 2000, 2000, { type: 'far' })
const inRadius = await index.findInRadius(0, 0, 300)
t.ok(inRadius.length >= 3, 'findInRadius returns points within radius')
t.ok(inRadius.every(p => p.distance <= 300), 'all results within radius')
// Verify far point excluded
const hasFar = inRadius.some(p => p.id === 'far')
t.is(hasFar, false, 'far point excluded from small radius')
await index.close()
t.pass('radius query test passed')
})
console.log('hyper-spatial-index tests completed')
test('hyper-spatial-index: close without leak', async (t) => {
const index = new SpatialIndex()
await index.close()
t.pass()
})
test('hyper-spatial-index: validation rejects invalid input', async (t) => {
const m = new SpatialIndex()
try {
if (typeof m.addNeighbor === 'function') m.addNeighbor(null)
else if (typeof m.buildCircuit === 'function') m.buildCircuit([])
else if (typeof m.grant === 'function') m.grant(null, -1)
else if (typeof m.enqueue === 'function') m.enqueue('bad', null)
else if (typeof m.reportSample === 'function') m.reportSample(null, -1, -1)
else if (typeof m.fanout === 'function') m.fanout(null, 0)
else if (typeof m.probe === 'function') m.probe(null)
else if (typeof m.resolve === 'function') m.resolve(null)
else if (typeof m.acquire === 'function') m.acquire(null)
else throw new Error('no validation hook')
t.fail('expected throw')
} catch (err) {
t.ok(err instanceof Error)
}
await m.close()
})