tests/sims/vectors.test.ts
The automated tests for the /simulators/vector-embedding simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- Cosine, dot product and distances match hand computation.
- IVF with every list probed returns exactly the brute-force neighbours.
- Recall rises with nprobe, reaches 1 when every list is probed, and the scanned share grows with it.
- Distances concentrate as dimensions grow (Beyer et al., "When is nearest neighbor meaningful?", 1999).
// /simulators/vector-embedding: cosine similarity, exact and IVF nearest-neighbour search.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { metrics, parseVec, dataset, kmeans, exact, ivf, experiment, concentration } from '../../src/lib/sims/vectors.ts';
import { lcg } from './check.ts';
const close = (a: number, b: number, eps = 1e-9) => Math.abs(a - b) < eps;
test('cosine, dot product and distances match hand computation', () => {
const m = metrics([1, 2, 3], [4, 5, 6]);
assert.equal(m.dot, 32);
assert.ok(close(m.cos, 32 / Math.sqrt(14 * 77)));
assert.ok(close(metrics([1, 0], [0, 1]).cos, 0));
assert.ok(close(metrics([1, 1], [-1, -1]).cos, -1));
const r = lcg(4);
for (let i = 0; i < 500; i++) {
const a = Array.from({ length: 8 }, () => r() * 2 - 1), b = Array.from({ length: 8 }, () => r() * 2 - 1);
const x = metrics(a, b);
const na = Math.hypot(...a), nb = Math.hypot(...b);
assert.ok(close(x.cos, a.reduce((s, v, k) => s + v * b[k], 0) / (na * nb)));
// for unit vectors, squared Euclidean distance = 2 − 2·cos
assert.ok(close(x.unitEuclid ** 2, 2 - 2 * x.cos, 1e-9));
}
assert.deepEqual(parseVec('[0.1, 0.2; x 3e-2]').v, [0.1, 0.2, 0.03]);
});
test('IVF with every list probed returns exactly the brute-force neighbours', () => {
const D = dataset(800, 8, 5, 3), idx = kmeans(D, 10, 5, 4);
for (let i = 0; i < 30; i++) {
const q = D.x.subarray(i * 8, i * 8 + 8);
assert.deepEqual(ivf(D, idx, q, 10, 5).ids, exact(D, q, 5));
}
});
test('recall rises with nprobe, reaches 1 when every list is probed, and the scanned share grows with it', () => {
const rows = experiment(3000, 16, 20, 32, [1, 2, 4, 8, 16, 32]);
for (let i = 1; i < rows.length; i++) {
assert.ok(rows[i].recall >= rows[i - 1].recall - 1e-12, `recall fell at nprobe ${rows[i].nprobe}`);
assert.ok(rows[i].scanned >= rows[i - 1].scanned, `scanned fell at nprobe ${rows[i].nprobe}`);
}
assert.ok(close(rows[rows.length - 1].recall, 1));
assert.ok(close(rows[rows.length - 1].scanned, 1));
});
test('distances concentrate as dimensions grow (Beyer et al., "When is nearest neighbor meaningful?", 1999)', () => {
const c = concentration([2, 10, 100, 1000]);
const vals = c.map((x: Record<string, number>) => Object.values(x).find((v, i) => i > 0) as number);
for (let i = 1; i < vals.length; i++) assert.ok(vals[i] < vals[i - 1], `contrast did not fall: ${vals}`);
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.