73 lines
2.6 KiB
JavaScript
73 lines
2.6 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
buildPgvectorMemorySchemaSql,
|
|
ensurePgvectorMemorySchema,
|
|
} from './memory-v2-pgvector-schema.mjs';
|
|
|
|
test('buildPgvectorMemorySchemaSql creates idempotent table SQL without extension by default', () => {
|
|
const statements = buildPgvectorMemorySchemaSql({
|
|
tableName: 'memory_embeddings',
|
|
dimensions: 1536,
|
|
});
|
|
|
|
assert.equal(statements.length, 3);
|
|
assert.doesNotMatch(statements.join('\n'), /CREATE EXTENSION/i);
|
|
assert.match(statements[0], /CREATE TABLE IF NOT EXISTS "memory_embeddings"/);
|
|
assert.match(statements[0], /embedding vector\(1536\) NOT NULL/);
|
|
assert.match(statements[0], /source_memory_id TEXT/);
|
|
assert.match(statements[1], /CREATE INDEX IF NOT EXISTS "memory_embeddings_user_created_idx"/);
|
|
assert.match(statements[2], /CREATE UNIQUE INDEX IF NOT EXISTS "memory_embeddings_source_memory_id_uidx"/);
|
|
});
|
|
|
|
test('buildPgvectorMemorySchemaSql can include extension and vector index for controlled setup', () => {
|
|
const statements = buildPgvectorMemorySchemaSql({
|
|
tableName: 'memory_embeddings',
|
|
dimensions: 768,
|
|
createExtension: true,
|
|
createVectorIndex: true,
|
|
});
|
|
|
|
assert.equal(statements.length, 5);
|
|
assert.equal(statements[0], 'CREATE EXTENSION IF NOT EXISTS vector');
|
|
assert.match(statements[1], /embedding vector\(768\) NOT NULL/);
|
|
assert.match(statements[4], /USING ivfflat \(embedding vector_cosine_ops\)/);
|
|
});
|
|
|
|
test('buildPgvectorMemorySchemaSql validates identifiers and dimensions', () => {
|
|
assert.throws(
|
|
() => buildPgvectorMemorySchemaSql({ tableName: 'memory_embeddings;drop table users' }),
|
|
/Invalid PostgreSQL identifier/,
|
|
);
|
|
assert.throws(
|
|
() => buildPgvectorMemorySchemaSql({ dimensions: 0 }),
|
|
/Invalid pgvector dimensions/,
|
|
);
|
|
});
|
|
|
|
test('ensurePgvectorMemorySchema executes generated statements in order', async () => {
|
|
const executed = [];
|
|
const result = await ensurePgvectorMemorySchema(
|
|
{
|
|
async query(sql) {
|
|
executed.push(sql);
|
|
return { rows: [] };
|
|
},
|
|
},
|
|
{
|
|
tableName: 'memory_embeddings',
|
|
dimensions: 1536,
|
|
createExtension: true,
|
|
createVectorIndex: true,
|
|
},
|
|
);
|
|
|
|
assert.equal(result.ok, true);
|
|
assert.equal(result.statements, 5);
|
|
assert.equal(executed[0], 'CREATE EXTENSION IF NOT EXISTS vector');
|
|
assert.match(executed[1], /CREATE TABLE IF NOT EXISTS "memory_embeddings"/);
|
|
assert.match(executed[2], /memory_embeddings_user_created_idx/);
|
|
assert.match(executed[3], /memory_embeddings_source_memory_id_uidx/);
|
|
assert.match(executed[4], /memory_embeddings_embedding_idx/);
|
|
});
|