|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +vi.mock('@sim/db/script-migrations/0021_embedding_search_connector', () => ({ |
| 7 | + PROJECTION_SOURCE_ACL_TABLES: ['embedding_search', 'embedding_keyword_tin'], |
| 8 | +})) |
| 9 | + |
| 10 | +import { |
| 11 | + pgPrewarmInstalled, |
| 12 | + prewarmRelation, |
| 13 | + prewarmSearchProjection, |
| 14 | +} from '@/lib/knowledge/search/prewarm' |
| 15 | + |
| 16 | +interface Statement { |
| 17 | + query: string |
| 18 | + parameters?: string[] |
| 19 | +} |
| 20 | + |
| 21 | +/** A session that records every statement and answers from the case's catalog. */ |
| 22 | +function session(state: { installed: boolean; relations?: string[]; failing?: string[] }): { |
| 23 | + statements: Statement[] |
| 24 | + unsafe: (query: string, parameters?: string[]) => Promise<unknown[]> |
| 25 | +} { |
| 26 | + const statements: Statement[] = [] |
| 27 | + return { |
| 28 | + statements, |
| 29 | + unsafe: async (query: string, parameters?: string[]) => { |
| 30 | + statements.push({ query, parameters }) |
| 31 | + if (query.includes('pg_extension')) return state.installed ? [{ '?column?': 1 }] : [] |
| 32 | + if (query.includes('pg_class')) |
| 33 | + return (state.relations ?? []).map((relation) => ({ relation })) |
| 34 | + if (query.includes('pg_prewarm(')) { |
| 35 | + const [relation] = parameters ?? [] |
| 36 | + if (state.failing?.includes(relation)) |
| 37 | + throw new Error(`relation "${relation}" does not exist`) |
| 38 | + return [{ pages: 7 }] |
| 39 | + } |
| 40 | + return [] |
| 41 | + }, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +describe('prewarmSearchProjection', () => { |
| 46 | + beforeEach(() => { |
| 47 | + vi.clearAllMocks() |
| 48 | + }) |
| 49 | + |
| 50 | + it('does nothing where the extension is absent, so the application role never needs it', async () => { |
| 51 | + const fake = session({ installed: false }) |
| 52 | + await expect(prewarmSearchProjection(fake)).resolves.toEqual([]) |
| 53 | + expect(fake.statements).toHaveLength(1) |
| 54 | + expect(fake.statements[0].query).toContain("extname = 'pg_prewarm'") |
| 55 | + }) |
| 56 | + |
| 57 | + it('reads the projections and their ranking indexes in the order the catalog lists them', async () => { |
| 58 | + const fake = session({ |
| 59 | + installed: true, |
| 60 | + relations: [ |
| 61 | + 'embedding_search', |
| 62 | + 'embedding_keyword_tin', |
| 63 | + 'embedding_search_512_cosine_hnsw_idx', |
| 64 | + ], |
| 65 | + }) |
| 66 | + const warmed = await prewarmSearchProjection(fake) |
| 67 | + expect(warmed.map((item) => item.relation)).toEqual([ |
| 68 | + 'embedding_search', |
| 69 | + 'embedding_keyword_tin', |
| 70 | + 'embedding_search_512_cosine_hnsw_idx', |
| 71 | + ]) |
| 72 | + expect(warmed.every((item) => item.pages === 7)).toBe(true) |
| 73 | + const listed = fake.statements.find((statement) => statement.query.includes('pg_class')) |
| 74 | + expect(listed?.parameters).toEqual([ |
| 75 | + '{embedding_search,embedding_keyword_tin}', |
| 76 | + '{hnsw,tin,gin}', |
| 77 | + ]) |
| 78 | + expect(listed?.query).toContain("ORDER BY c.relkind = 'r' DESC") |
| 79 | + const reads = fake.statements.filter((statement) => statement.query.includes('pg_prewarm(')) |
| 80 | + expect(reads.map((statement) => statement.parameters)).toEqual([ |
| 81 | + ['embedding_search'], |
| 82 | + ['embedding_keyword_tin'], |
| 83 | + ['embedding_search_512_cosine_hnsw_idx'], |
| 84 | + ]) |
| 85 | + expect(reads.every((statement) => statement.query.includes("'read'"))).toBe(true) |
| 86 | + }) |
| 87 | + |
| 88 | + it('skips a relation that fails to warm and carries on with the rest', async () => { |
| 89 | + const fake = session({ |
| 90 | + installed: true, |
| 91 | + relations: ['embedding_search', 'embedding_search_512_cosine_hnsw_idx'], |
| 92 | + failing: ['embedding_search'], |
| 93 | + }) |
| 94 | + const warmed = await prewarmSearchProjection(fake) |
| 95 | + expect(warmed.map((item) => item.relation)).toEqual(['embedding_search_512_cosine_hnsw_idx']) |
| 96 | + }) |
| 97 | + |
| 98 | + it('returns nothing when the extension cannot be checked, never failing its caller', async () => { |
| 99 | + const fake = session({ installed: true }) |
| 100 | + fake.unsafe = async () => { |
| 101 | + throw new Error('canceling statement due to user request') |
| 102 | + } |
| 103 | + await expect(prewarmSearchProjection(fake)).resolves.toEqual([]) |
| 104 | + }) |
| 105 | + |
| 106 | + it('bounds every read by the budget left and leaves the rest cold once it is spent', async () => { |
| 107 | + vi.useFakeTimers() |
| 108 | + try { |
| 109 | + const fake = session({ |
| 110 | + installed: true, |
| 111 | + relations: [ |
| 112 | + 'embedding_search', |
| 113 | + 'embedding_keyword_tin', |
| 114 | + 'embedding_search_512_cosine_hnsw_idx', |
| 115 | + ], |
| 116 | + }) |
| 117 | + const read = fake.unsafe |
| 118 | + fake.unsafe = async (query: string, parameters?: string[]) => { |
| 119 | + const rows = await read(query, parameters) |
| 120 | + /** Each read takes 400 ms of a 1 s budget. */ |
| 121 | + if (query.includes('pg_prewarm(')) vi.advanceTimersByTime(400) |
| 122 | + return rows |
| 123 | + } |
| 124 | + const warmed = await prewarmSearchProjection(fake, { budgetMs: 1000 }) |
| 125 | + expect(warmed.map((item) => item.relation)).toEqual([ |
| 126 | + 'embedding_search', |
| 127 | + 'embedding_keyword_tin', |
| 128 | + 'embedding_search_512_cosine_hnsw_idx', |
| 129 | + ]) |
| 130 | + const timeouts = fake.statements |
| 131 | + .filter((statement) => statement.query.startsWith('SET statement_timeout')) |
| 132 | + .map((statement) => Number(statement.query.split('= ')[1])) |
| 133 | + expect(timeouts).toEqual([1000, 600, 200]) |
| 134 | + expect(fake.statements.at(-1)?.query).toBe('RESET statement_timeout') |
| 135 | + } finally { |
| 136 | + vi.useRealTimers() |
| 137 | + } |
| 138 | + }) |
| 139 | + |
| 140 | + it('skips the relations beyond a spent budget', async () => { |
| 141 | + vi.useFakeTimers() |
| 142 | + try { |
| 143 | + const fake = session({ |
| 144 | + installed: true, |
| 145 | + relations: ['embedding_search', 'embedding_search_512_cosine_hnsw_idx'], |
| 146 | + }) |
| 147 | + const read = fake.unsafe |
| 148 | + fake.unsafe = async (query: string, parameters?: string[]) => { |
| 149 | + const rows = await read(query, parameters) |
| 150 | + if (query.includes('pg_prewarm(')) vi.advanceTimersByTime(1500) |
| 151 | + return rows |
| 152 | + } |
| 153 | + const warmed = await prewarmSearchProjection(fake, { budgetMs: 1000 }) |
| 154 | + expect(warmed.map((item) => item.relation)).toEqual(['embedding_search']) |
| 155 | + expect( |
| 156 | + fake.statements.filter((statement) => statement.query.includes('pg_prewarm(')) |
| 157 | + ).toHaveLength(1) |
| 158 | + } finally { |
| 159 | + vi.useRealTimers() |
| 160 | + } |
| 161 | + }) |
| 162 | + |
| 163 | + it('never sets a timeout on an unbounded pass', async () => { |
| 164 | + const fake = session({ installed: true, relations: ['embedding_search'] }) |
| 165 | + await prewarmSearchProjection(fake) |
| 166 | + expect(fake.statements.some((statement) => statement.query.includes('statement_timeout'))).toBe( |
| 167 | + false |
| 168 | + ) |
| 169 | + }) |
| 170 | + |
| 171 | + it('returns nothing when the catalog cannot be read, never failing its caller', async () => { |
| 172 | + const fake = session({ installed: true }) |
| 173 | + fake.unsafe = async (query: string) => { |
| 174 | + if (query.includes('pg_extension')) return [{ '?column?': 1 }] |
| 175 | + throw new Error('permission denied for table pg_class') |
| 176 | + } |
| 177 | + await expect(prewarmSearchProjection(fake)).resolves.toEqual([]) |
| 178 | + }) |
| 179 | +}) |
| 180 | + |
| 181 | +describe('prewarmRelation', () => { |
| 182 | + it('reports the pages read for one relation', async () => { |
| 183 | + const fake = session({ installed: true }) |
| 184 | + await expect(prewarmRelation(fake, 'embedding_search')).resolves.toMatchObject({ |
| 185 | + relation: 'embedding_search', |
| 186 | + pages: 7, |
| 187 | + }) |
| 188 | + expect(await pgPrewarmInstalled(fake)).toBe(true) |
| 189 | + }) |
| 190 | +}) |
0 commit comments