|
| 1 | +import path from 'node:path'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import test from 'node:test'; |
| 4 | +import { drizzle } from 'drizzle-orm/node-postgres'; |
| 5 | +import { searchConversations } from '../../src/lib/chat/conversationSearch.ts'; |
| 6 | +import { applyMigrations } from '../apply-migrations.ts'; |
| 7 | +import { conversation, user } from '../schema.ts'; |
| 8 | +import { createTestDatabase } from './harness.ts'; |
| 9 | + |
| 10 | +const MIGRATIONS_FOLDER = path.resolve(import.meta.dirname, '../migrations'); |
| 11 | + |
| 12 | +/** |
| 13 | + * Conversation search on Postgres reads each saved message's text and the |
| 14 | + * title of the card a user message names, and leaves out the approved plan |
| 15 | + * stored beside a reply. |
| 16 | + */ |
| 17 | +test('conversation search matches message text and card titles, not stored plans', async (t) => { |
| 18 | + const database = await createTestDatabase(); |
| 19 | + t.after(() => database.teardown()); |
| 20 | + |
| 21 | + await applyMigrations(database.migratorPool(), { |
| 22 | + migrationsFolder: MIGRATIONS_FOLDER, |
| 23 | + runtimeDbUser: database.runtimeUser, |
| 24 | + migratorDbUser: database.migratorUser, |
| 25 | + }); |
| 26 | + |
| 27 | + const db = drizzle(database.runtimePool()); |
| 28 | + |
| 29 | + await db.insert(user).values([{ id: 'owner', name: 'Owner', email: 'owner@example.org' }]); |
| 30 | + await db.insert(conversation).values({ |
| 31 | + id: 'conv-1', |
| 32 | + userId: 'owner', |
| 33 | + title: 'Aid question', |
| 34 | + data: { |
| 35 | + messages: [ |
| 36 | + { role: 'user', text: 'How much aid did Ghana get?' }, |
| 37 | + { |
| 38 | + role: 'bot', |
| 39 | + text: 'About 1 billion dollars.', |
| 40 | + plan: { title: 'Net disbursements to Zanzibar', steps: [], choices: [] }, |
| 41 | + }, |
| 42 | + { role: 'user', text: 'Make it a bar chart', card: { title: 'Health aid trend' } }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + const hits = async (q: string) => |
| 48 | + (await searchConversations(db, 'owner', q)).map((hit) => [hit.id, hit.snippet]); |
| 49 | + |
| 50 | + assert.deepEqual(await hits('ghana'), [['conv-1', 'How much aid did Ghana get?']]); |
| 51 | + assert.deepEqual(await hits('health aid'), [['conv-1', 'Health aid trend']]); |
| 52 | + assert.deepEqual(await hits('zanzibar'), []); |
| 53 | +}); |
0 commit comments