|
| 1 | +/** |
| 2 | + * Route-level validation tests for memory API endpoints. |
| 3 | + * Tests UUID validation on param/query inputs and filter behavior |
| 4 | + * on the list endpoint. Requires DATABASE_URL in .env.test. |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; |
| 8 | + |
| 9 | +// Mock embedText to avoid hitting the real embedding provider in CI where |
| 10 | +// OPENAI_API_KEY is a placeholder. Returns a deterministic zero vector |
| 11 | +// matching the configured embedding dimensions. |
| 12 | +vi.mock('../services/embedding.js', async (importOriginal) => { |
| 13 | + const actual = await importOriginal<typeof import('../services/embedding.js')>(); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + embedText: vi.fn(async () => { |
| 17 | + const { config: cfg } = await import('../config.js'); |
| 18 | + return new Array(cfg.embeddingDimensions).fill(0); |
| 19 | + }), |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +import { pool } from '../db/pool.js'; |
| 24 | +import { config } from '../config.js'; |
| 25 | +import { MemoryRepository } from '../db/memory-repository.js'; |
| 26 | +import { ClaimRepository } from '../db/claim-repository.js'; |
| 27 | +import { MemoryService } from '../services/memory-service.js'; |
| 28 | +import { createMemoryRouter } from '../routes/memories.js'; |
| 29 | +import express from 'express'; |
| 30 | +import { readFileSync } from 'node:fs'; |
| 31 | +import { resolve, dirname } from 'node:path'; |
| 32 | +import { fileURLToPath } from 'node:url'; |
| 33 | + |
| 34 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 35 | +const TEST_USER = 'route-validation-test-user'; |
| 36 | +const VALID_UUID = '00000000-0000-0000-0000-000000000001'; |
| 37 | +const INVALID_UUID = 'not-a-uuid'; |
| 38 | + |
| 39 | +let server: ReturnType<typeof app.listen>; |
| 40 | +let baseUrl: string; |
| 41 | +const app = express(); |
| 42 | +app.use(express.json()); |
| 43 | + |
| 44 | +beforeAll(async () => { |
| 45 | + const raw = readFileSync(resolve(__dirname, '../db/schema.sql'), 'utf-8'); |
| 46 | + const sql = raw.replace(/\{\{EMBEDDING_DIMENSIONS\}\}/g, String(config.embeddingDimensions)); |
| 47 | + await pool.query(sql); |
| 48 | + |
| 49 | + const repo = new MemoryRepository(pool); |
| 50 | + const claimRepo = new ClaimRepository(pool); |
| 51 | + const service = new MemoryService(repo, claimRepo); |
| 52 | + app.use('/memories', createMemoryRouter(service)); |
| 53 | + |
| 54 | + await new Promise<void>((resolve) => { |
| 55 | + server = app.listen(0, () => { |
| 56 | + const addr = server.address(); |
| 57 | + const port = typeof addr === 'object' && addr ? addr.port : 0; |
| 58 | + baseUrl = `http://localhost:${port}`; |
| 59 | + resolve(); |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +afterAll(async () => { |
| 65 | + await new Promise<void>((resolve) => server.close(() => resolve())); |
| 66 | + await pool.end(); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('GET /memories/:id — UUID validation', () => { |
| 70 | + it('returns 400 for an invalid UUID', async () => { |
| 71 | + const res = await fetch(`${baseUrl}/memories/${INVALID_UUID}?user_id=${TEST_USER}`); |
| 72 | + expect(res.status).toBe(400); |
| 73 | + const body = await res.json(); |
| 74 | + expect(body.error).toMatch(/valid UUID/); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns 404 for a valid but non-existent UUID', async () => { |
| 78 | + const res = await fetch(`${baseUrl}/memories/${VALID_UUID}?user_id=${TEST_USER}`); |
| 79 | + expect(res.status).toBe(404); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('DELETE /memories/:id — UUID validation', () => { |
| 84 | + it('returns 400 for an invalid UUID', async () => { |
| 85 | + const res = await fetch(`${baseUrl}/memories/${INVALID_UUID}?user_id=${TEST_USER}`, { |
| 86 | + method: 'DELETE', |
| 87 | + }); |
| 88 | + expect(res.status).toBe(400); |
| 89 | + const body = await res.json(); |
| 90 | + expect(body.error).toMatch(/valid UUID/); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('POST /memories/ingest/quick — skip_extraction (storeVerbatim)', () => { |
| 95 | + it('stores a single memory without extraction when skip_extraction is true', async () => { |
| 96 | + const res = await fetch(`${baseUrl}/memories/ingest/quick`, { |
| 97 | + method: 'POST', |
| 98 | + headers: { 'Content-Type': 'application/json' }, |
| 99 | + body: JSON.stringify({ |
| 100 | + user_id: TEST_USER, |
| 101 | + conversation: 'Verbatim content that should not be extracted into facts.', |
| 102 | + source_site: 'verbatim-test', |
| 103 | + source_url: 'https://example.com/verbatim', |
| 104 | + skip_extraction: true, |
| 105 | + }), |
| 106 | + }); |
| 107 | + expect(res.status).toBe(200); |
| 108 | + const body = await res.json(); |
| 109 | + expect(body.memoriesStored).toBe(1); |
| 110 | + expect(body.memoryIds).toHaveLength(1); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('GET /memories/list — source_site filter', () => { |
| 115 | + it('returns memories filtered by source_site', async () => { |
| 116 | + const res = await fetch( |
| 117 | + `${baseUrl}/memories/list?user_id=${TEST_USER}&source_site=test-site`, |
| 118 | + ); |
| 119 | + expect(res.status).toBe(200); |
| 120 | + const body = await res.json(); |
| 121 | + expect(body).toHaveProperty('memories'); |
| 122 | + expect(body).toHaveProperty('count'); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('GET /memories/list — episode_id filter', () => { |
| 127 | + it('returns 400 for an invalid episode_id', async () => { |
| 128 | + const res = await fetch( |
| 129 | + `${baseUrl}/memories/list?user_id=${TEST_USER}&episode_id=${INVALID_UUID}`, |
| 130 | + ); |
| 131 | + expect(res.status).toBe(400); |
| 132 | + const body = await res.json(); |
| 133 | + expect(body.error).toMatch(/valid UUID/); |
| 134 | + }); |
| 135 | + |
| 136 | + it('accepts a valid episode_id UUID', async () => { |
| 137 | + const res = await fetch( |
| 138 | + `${baseUrl}/memories/list?user_id=${TEST_USER}&episode_id=${VALID_UUID}`, |
| 139 | + ); |
| 140 | + expect(res.status).toBe(200); |
| 141 | + const body = await res.json(); |
| 142 | + expect(body).toHaveProperty('memories'); |
| 143 | + }); |
| 144 | +}); |
0 commit comments