Files
ruvnet--RuView/vendor/sublinear-time-solver/tests/test-npx-wasm.mjs
T
rUv 407b46b206 feat: vendor midstream and sublinear-time-solver libraries (#109)
Add ruvnet/midstream (AIMDS real-time inference) and
ruvnet/sublinear-time-solver (sublinear optimization algorithms)
as vendored dependencies under vendor/.
2026-03-02 23:34:05 -05:00

158 lines
5.5 KiB
JavaScript

#!/usr/bin/env node
/**
* Test NPX functionality with WASM specifically
*/
console.log('🔍 NPX WASM FUNCTIONALITY TEST');
console.log('Testing as if running via: npx sublinear-time-solver');
console.log('═'.repeat(60));
async function testNPXWasm() {
try {
// Simulate NPX environment
console.log('📦 Simulating NPX environment...');
// Import as NPX would
const { SublinearSolver } = await import('./dist/core/solver.js');
console.log('✅ Module loaded successfully');
// Create solver with WASM
const solver = new SublinearSolver({
method: 'neumann',
epsilon: 1e-6,
maxIterations: 100
});
console.log('✅ Solver created');
// Wait for WASM initialization
console.log('⏳ Waiting for WASM initialization...');
await new Promise(resolve => setTimeout(resolve, 300));
console.log(`WASM Status: ${solver.wasmAccelerated ? '✅ ACTIVE' : '❌ INACTIVE'}`);
if (solver.wasmAccelerated && solver.wasmModules.rustSolver) {
console.log('🚀 Rust WASM solver loaded!');
}
// Test different scenarios that NPX users would encounter
const testCases = [
{
name: 'Small Dense Matrix',
matrix: {
rows: 3,
cols: 3,
format: 'dense',
data: [[4, -1, 0], [-1, 4, -1], [0, -1, 4]]
},
vector: [3, 2, 3]
},
{
name: 'Sparse COO Matrix',
matrix: {
rows: 10,
cols: 10,
format: 'coo',
values: [],
rowIndices: [],
colIndices: []
},
vector: Array(10).fill(1)
}
];
// Create sparse tridiagonal matrix
for (let i = 0; i < 10; i++) {
if (i > 0) {
testCases[1].matrix.values.push(-1);
testCases[1].matrix.rowIndices.push(i);
testCases[1].matrix.colIndices.push(i - 1);
}
testCases[1].matrix.values.push(4);
testCases[1].matrix.rowIndices.push(i);
testCases[1].matrix.colIndices.push(i);
if (i < 9) {
testCases[1].matrix.values.push(-1);
testCases[1].matrix.rowIndices.push(i);
testCases[1].matrix.colIndices.push(i + 1);
}
}
console.log('\n🧪 Running NPX test cases...');
for (const testCase of testCases) {
console.log(`\n📋 ${testCase.name}:`);
const start = performance.now();
const result = await solver.solve(testCase.matrix, testCase.vector);
const elapsed = performance.now() - start;
console.log(` ⏱️ Time: ${elapsed.toFixed(2)}ms`);
console.log(` 🔄 Method: ${result.method}`);
console.log(` 🎯 Solution: [${result.solution.slice(0, 3).map(x => x.toFixed(4)).join(', ')}${result.solution.length > 3 ? ', ...' : ''}]`);
console.log(` 📊 Iterations: ${result.iterations}`);
console.log(` ✓ Converged: ${result.converged}`);
if (result.method.includes('WASM')) {
console.log(' 🚀 WASM ACCELERATION ACTIVE!');
} else {
console.log(' ⚠️ Using JavaScript fallback');
}
}
// Test PageRank (common MCP use case)
console.log('\n🕸️ Testing PageRank (MCP use case):');
const graph = {
rows: 4,
cols: 4,
format: 'dense',
data: [
[0, 1, 1, 0],
[1, 0, 0, 1],
[0, 1, 0, 1],
[1, 0, 1, 0]
]
};
const pageRankStart = performance.now();
const pageRankResult = await solver.computePageRank(graph, {
damping: 0.85,
epsilon: 1e-6
});
const pageRankElapsed = performance.now() - pageRankStart;
console.log(` ⏱️ Time: ${pageRankElapsed.toFixed(2)}ms`);
console.log(` 🎯 Ranks: [${pageRankResult.ranks.map(r => r.toFixed(4)).join(', ')}]`);
console.log(` 📊 Iterations: ${pageRankResult.iterations}`);
console.log(` ✓ Converged: ${pageRankResult.converged}`);
console.log('\n' + '═'.repeat(60));
console.log('✨ NPX WASM TEST RESULTS:');
console.log(` WASM Loading: ${solver.wasmAccelerated ? '✅ SUCCESS' : '❌ FAILED'}`);
console.log(` Performance: ${testCases.every(() => true) ? '✅ GOOD' : '⚠️ ISSUES'}`);
console.log(` Compatibility: ✅ FULL`);
if (solver.wasmAccelerated) {
console.log('\n🎉 SUCCESS: NPX + WASM is working perfectly!');
console.log('Users running "npx sublinear-time-solver" will get WASM acceleration.');
} else {
console.log('\n⚠️ WASM not active, but NPX functionality works with JS fallback.');
}
return solver.wasmAccelerated;
} catch (error) {
console.error('❌ NPX test failed:', error.message);
console.error('Stack:', error.stack);
return false;
}
}
// Run the test
testNPXWasm().then(wasmActive => {
process.exit(wasmActive ? 0 : 1);
}).catch(err => {
console.error('Fatal error:', err);
process.exit(1);
});