107 lines
3.0 KiB
JavaScript
107 lines
3.0 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
buildPgvectorSmokeRows,
|
|
runPgvectorMemorySmoke,
|
|
} from './memory-v2-pgvector-smoke.mjs';
|
|
|
|
test('buildPgvectorSmokeRows creates deterministic retrieval fixture', () => {
|
|
const fixture = buildPgvectorSmokeRows({ dimensions: 4 });
|
|
|
|
assert.equal(fixture.userId, 'memory-v2-smoke-user');
|
|
assert.deepEqual(fixture.query, [1, 0, 0, 0]);
|
|
assert.equal(fixture.rows.length, 2);
|
|
assert.equal(fixture.rows[0].content, fixture.expectedTopText);
|
|
assert.deepEqual(fixture.rows[1].embedding, [0, 0, 0, 1]);
|
|
});
|
|
|
|
test('runPgvectorMemorySmoke inserts fixture, resolves nearest memory, and cleans up', async () => {
|
|
const calls = [];
|
|
const pool = {
|
|
async query(sql, params = []) {
|
|
calls.push({ sql, params });
|
|
if (/SELECT id, content, type, created_at/.test(sql)) {
|
|
return {
|
|
rows: [
|
|
{
|
|
id: 10,
|
|
content: 'memory-v2 smoke near vector',
|
|
type: 'smoke',
|
|
score: 1,
|
|
created_at: '2026-07-03T00:00:00.000Z',
|
|
},
|
|
{
|
|
id: 11,
|
|
content: 'memory-v2 smoke far vector',
|
|
type: 'smoke',
|
|
score: 0,
|
|
created_at: '2026-07-03T00:00:00.000Z',
|
|
},
|
|
],
|
|
};
|
|
}
|
|
return { rows: [] };
|
|
},
|
|
};
|
|
|
|
const result = await runPgvectorMemorySmoke({
|
|
pool,
|
|
tableName: 'memory_embeddings',
|
|
dimensions: 3,
|
|
});
|
|
|
|
assert.equal(result.ok, true);
|
|
assert.equal(result.inserted, 2);
|
|
assert.equal(result.topMemory.text, 'memory-v2 smoke near vector');
|
|
assert.equal(calls.filter((call) => /INSERT INTO/.test(call.sql)).length, 2);
|
|
assert.equal(calls.some((call) => /DELETE FROM/.test(call.sql)), true);
|
|
});
|
|
|
|
test('runPgvectorMemorySmoke can ensure schema before smoke', async () => {
|
|
const calls = [];
|
|
const pool = {
|
|
async query(sql, params = []) {
|
|
calls.push({ sql, params });
|
|
if (/SELECT id, content, type, created_at/.test(sql)) {
|
|
return {
|
|
rows: [{
|
|
id: 1,
|
|
content: 'memory-v2 smoke near vector',
|
|
type: 'smoke',
|
|
}],
|
|
};
|
|
}
|
|
return { rows: [] };
|
|
},
|
|
};
|
|
|
|
const result = await runPgvectorMemorySmoke({
|
|
pool,
|
|
tableName: 'memory_embeddings',
|
|
dimensions: 3,
|
|
createSchema: true,
|
|
createExtension: true,
|
|
});
|
|
|
|
assert.equal(result.ok, true);
|
|
assert.match(calls[0].sql, /CREATE EXTENSION IF NOT EXISTS vector/);
|
|
assert.equal(calls.some((call) => /CREATE TABLE IF NOT EXISTS/.test(call.sql)), true);
|
|
});
|
|
|
|
test('runPgvectorMemorySmoke validates identifiers and dimensions', async () => {
|
|
await assert.rejects(
|
|
() => runPgvectorMemorySmoke({
|
|
pool: { async query() {} },
|
|
tableName: 'memory_embeddings;DROP',
|
|
}),
|
|
/Invalid PostgreSQL identifier/,
|
|
);
|
|
await assert.rejects(
|
|
() => runPgvectorMemorySmoke({
|
|
pool: { async query() {} },
|
|
dimensions: 0,
|
|
}),
|
|
/Invalid pgvector smoke dimensions/,
|
|
);
|
|
});
|