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,147 @@
const test = require('brittle')
const HyperP2PIntentRouter = require('../index.js')
const crypto = require('bare-crypto')
const path = require('bare-path')
const process = require('bare-process')
const { setTimeout } = require('bare-timers')
const TEST_DIR = path.join(process.cwd(), 'test-intent-router-' + Date.now())
test('lifecycle - ready and close', async (t) => {
const router = new HyperP2PIntentRouter({
storageDir: path.join(TEST_DIR, 'router-lifecycle')
})
await router.ready()
t.is(router._joined, true, 'ready should set joined')
await router.close()
t.pass('close works')
})
test('register and unregister intent', async (t) => {
const router = new HyperP2PIntentRouter({
storageDir: path.join(TEST_DIR, 'router-reg')
})
await router.ready()
const intentId = await router.registerIntent({
id: 'test-intent-1',
description: 'AI image generation service',
capabilities: ['image-gen', 'stable-diffusion', 'gpu'],
topics: ['ai', 'graphics'],
priority: 5
})
t.ok(typeof intentId === 'string', 'intentId is string')
const local = router.getLocalIntents()
t.is(local.length, 1, 'one local intent')
await router.unregisterIntent(intentId)
const afterUnreg = router.getLocalIntents()
t.is(afterUnreg.length, 0, 'unregister works')
await router.close()
})
test('resolve intent local match and scoring', async (t) => {
const router = new HyperP2PIntentRouter({
storageDir: path.join(TEST_DIR, 'router-resolve')
})
await router.ready()
await router.registerIntent({
id: 'intent-ml',
description: 'Machine learning inference',
capabilities: ['ml', 'inference', 'gpu'],
priority: 8
})
await router.registerIntent({
id: 'intent-storage',
description: 'Distributed storage node',
capabilities: ['storage', 's3-compatible'],
priority: 3
})
const mlMatches = await router.resolveIntent({ capabilities: ['ml', 'gpu'] })
t.ok(mlMatches.length >= 1, 'found ml match')
t.ok(mlMatches[0].score > 0.5, 'score reasonable')
const storageMatches = await router.resolveIntent({ capabilities: ['storage'] })
t.ok(storageMatches.length >= 1, 'found storage match')
await router.close()
})
test('peer intent simulation and cross-peer matching', async (t) => {
const router = new HyperP2PIntentRouter({
storageDir: path.join(TEST_DIR, 'router-peer')
})
await router.ready()
const fakePeerHex = 'a'.repeat(64)
router.peerIntents.set(fakePeerHex, {
intents: new Map([['peer-intent-1', {
id: 'peer-intent-1',
description: 'GPU compute for AI',
capabilities: ['gpu', 'compute', 'ai'],
expiresAt: Date.now() + 600000
}]]),
lastSeen: Date.now(),
connections: new Set()
})
const peerMatches = await router.resolveIntent({ capabilities: ['gpu', 'ai'] })
t.ok(peerMatches.some(m => m.peerPublicKey === fakePeerHex), 'peer match found')
await router.close()
})
test('sendToIntent simulated', async (t) => {
const router = new HyperP2PIntentRouter({
storageDir: path.join(TEST_DIR, 'router-send')
})
await router.ready()
await router.registerIntent({
id: 'intent-ml',
description: 'Machine learning inference',
capabilities: ['ml', 'inference', 'gpu'],
priority: 8
})
const sendResult = await router.sendToIntent({ capabilities: ['ml'] }, { prompt: 'test' })
t.ok(sendResult, 'sendToIntent returns result')
await router.close()
})
test('cleanup and metrics', async (t) => {
const router = new HyperP2PIntentRouter({
storageDir: path.join(TEST_DIR, 'router-cleanup')
})
await router.ready()
await router.close()
t.pass('graceful close and cleanup')
})
test('hyper-p2p-intent-router: close without leak', async (t) => {
const m = new HyperP2PIntentRouter()
await m.close()
t.pass()
})
test('hyper-p2p-intent-router: validation rejects invalid input', async (t) => {
const m = new HyperP2PIntentRouter()
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()
})