Repository navigation
Expand file tree
/
Copy pathtest-search-postgres-e2e.ts
More file actions
700 lines (690 loc) · 28.8 KB
/
Copy pathtest-search-postgres-e2e.ts
File metadata and controls
700 lines (690 loc) · 28.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
import assert from 'node:assert/strict'
import { spawn } from 'node:child_process'
import { createHash } from 'node:crypto'
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'
import { dirname, join, resolve } from 'node:path'
import { createLogger } from '@sim/logger'
import { getErrorMessage } from '@sim/utils/errors'
import { generateId } from '@sim/utils/id'
import { type Server, serve } from 'bun'
import { toString as markdownToString } from 'mdast-util-to-string'
import postgres from 'postgres'
import remarkGfm from 'remark-gfm'
import remarkParse from 'remark-parse'
import { unified } from 'unified'
import { visit } from 'unist-util-visit'
import { z } from 'zod'
import { getPage, getPages } from '@/lib/content/catalog'
import { pageSearchChunks } from '@/lib/search/chunks'
import { searchDatabase } from '@/lib/search/database'
import { searchDocs } from '@/lib/search/service'
const logger = createLogger('DocsSearchPostgresE2E')
const chunksOnly = process.argv.includes('--chunks-only')
const databaseSetting =
process.env.DOCS_SEARCH_E2E_DATABASE_URL ??
(chunksOnly ? 'postgres://127.0.0.1:1/docs_search_test' : undefined)
const reportSetting = process.env.DOCS_SEARCH_E2E_REPORT_PATH
assert(
databaseSetting && reportSetting,
'Explicit disposable database and report paths are required'
)
const databaseUrl = new URL(databaseSetting)
assert(
['localhost', '127.0.0.1', '[::1]'].includes(databaseUrl.hostname),
'Only loopback databases are permitted'
)
assert(databaseUrl.pathname === '/docs_search_test', 'Use a dedicated docs_search_test database')
assert(['postgres:', 'postgresql:'].includes(databaseUrl.protocol), 'A PostgreSQL URL is required')
process.env.DATABASE_URL = databaseUrl.href
process.env.OPENAI_API_KEY = ''
const reportPath: string = reportSetting
const directory = dirname(reportPath)
const workspace = resolve(import.meta.dirname, '..')
const sql = postgres(databaseUrl.href, { max: 1 })
const results: Array<{
name: string
status: 'passed' | 'failed'
durationMs: number
error?: string
}> = []
const vector = JSON.stringify([1, ...Array.from({ length: 1535 }, () => 0)])
let created = false
const preloadPath = join(directory, 'embedding-transport.mjs')
type ProviderScenario =
| 'success'
| '429'
| '503'
| '401'
| 'dimensions'
| 'indices'
| 'batch-failure'
| 'long-retry'
let providerScenario: ProviderScenario = 'success'
let scenarioRequests = 0
let provider: Server<undefined> | undefined
const providerRequests: Array<{
scenario: ProviderScenario
attempt: number
status: number
inputs: number
}> = []
await mkdir(directory, { recursive: true })
async function check(name: string, action: () => Promise<void>) {
const started = performance.now()
try {
await action()
results.push({ name, status: 'passed', durationMs: Math.round(performance.now() - started) })
} catch (error) {
results.push({
name,
status: 'failed',
durationMs: Math.round(performance.now() - started),
error: getErrorMessage(error),
})
throw error
}
}
async function index(
mode: 'check' | 'publish',
name: string,
paths: string[] = [],
useProvider = false
) {
const outputPath = join(directory, `${name}.json`)
const child = spawn(
process.execPath,
[
'--no-env-file',
...(useProvider ? ['--preload', preloadPath] : []),
'scripts/index-search.ts',
...(mode === 'check' ? ['--check'] : []),
...paths.map((path) => `--path=${path}`),
],
{
cwd: workspace,
env: {
...process.env,
DATABASE_URL: databaseUrl.href,
OPENAI_API_KEY: useProvider ? 'isolated-fixture' : '',
DOCS_SEARCH_REPORT_PATH: outputPath,
},
stdio: 'ignore',
timeout: 60000,
}
)
const exitCode = await new Promise<number>((resolve, reject) => {
child.once('error', reject)
child.once('close', (code) => resolve(code ?? 1))
})
return { exitCode, report: await readFile(outputPath, 'utf8') }
}
function scenario(value: ProviderScenario) {
providerScenario = value
scenarioRequests = 0
}
async function documentState(source: string) {
const [row] = await sql<Array<{ count: number; digest: string }>>`
SELECT count(*)::int AS count, md5(string_agg(row_to_json(document)::text, '|' ORDER BY chunk_id)) AS digest
FROM docs_embeddings AS document WHERE source_document = ${source}
`
assert(row)
return row
}
async function fingerprint() {
const [row] = await sql<Array<{ count: number; digest: string }>>`
SELECT count(*)::int AS count, md5(string_agg(chunk_id::text, ',' ORDER BY chunk_id)) AS digest FROM docs_embeddings
`
assert(row)
return row
}
async function verifyChunks() {
const parser = unified().use(remarkParse).use(remarkGfm)
for (const path of [
'/api-reference/access-requests/cancelOrganizationAccessRequest',
'/integrations/buffer',
'/integrations/plane',
]) {
const page = getPage(path)
assert(page)
const source = parser.parse(page.markdown)
const chunks = [...pageSearchChunks(page)]
const trees = chunks.map((chunk) => {
assert(chunk.text.length <= 6000, `${chunk.id}: chunk exceeds the complete payload budget`)
assert.equal(new URL(chunk.sourceLink).pathname, path)
const anchor = new URL(chunk.sourceLink).hash.slice(1)
assert(
!anchor || page.headings.some((heading) => heading.id === anchor),
`${chunk.id}: unknown citation`
)
if (!chunk.id.endsWith(':0')) {
assert(
chunk.text.includes(page.title) &&
chunk.text.includes(chunk.headerText) &&
/continued/i.test(chunk.text),
`${chunk.id}: continuation lost its page/section context`
)
}
const tree = parser.parse(chunk.text)
visit(tree, 'code', (node) => {
const raw = chunk.text.slice(node.position?.start.offset, node.position?.end.offset)
const marker = /^ {0,3}(`{3,}|~{3,})/.exec(raw)?.[1]
if (marker)
assert(
raw.trimEnd().split('\n').at(-1)?.trim().startsWith(marker),
`${chunk.id}: unclosed fenced code`
)
})
return tree
})
const originalCode: Array<{ value: string; lang: string | null | undefined }> = []
visit(source, 'code', (node) => {
originalCode.push({ value: node.value, lang: node.lang })
})
let sourceIndex = 0
let reconstructed = ''
let pieces = 0
for (const tree of trees)
visit(tree, 'code', (node) => {
const expected = originalCode[sourceIndex]
assert(expected, `${path}: unexpected code block`)
assert.equal(node.lang, expected.lang, `${path}: code language changed`)
reconstructed += `${pieces++ ? '\n' : ''}${node.value}`
assert(expected.value.startsWith(reconstructed), `${path}: code bytes or order changed`)
if (reconstructed === expected.value) {
sourceIndex += 1
reconstructed = ''
pieces = 0
}
})
assert.equal(sourceIndex, originalCode.length, `${path}: code content was lost`)
assert.equal(pieces, 0, `${path}: code continuation was incomplete`)
visit(source, 'paragraph', (node) => {
const raw = page.markdown.slice(node.position?.start.offset, node.position?.end.offset)
if (raw.length < 4500)
assert(
chunks.some((chunk) => chunk.text.includes(raw)),
`${path}: a fitting paragraph was split`
)
})
const emittedRows: string[][] = []
for (const tree of trees)
visit(tree, 'tableRow', (row) => {
emittedRows.push(row.children.map((cell) => markdownToString(cell)))
})
visit(source, 'table', (table) => {
const header = page.markdown.slice(
table.children[0]?.position?.start.offset,
table.children[0]?.position?.end.offset
)
for (const row of table.children.slice(1)) {
const raw = page.markdown.slice(row.position?.start.offset, row.position?.end.offset)
if (raw.length < 4500) {
const containing = chunks.find((chunk) => chunk.text.includes(raw))
assert(
containing?.text.includes(header),
`${path}: fitting table row or repeated header lost`
)
} else {
const cells = row.children.map((cell) => markdownToString(cell))
const matching = emittedRows.filter((candidate) => candidate[0] === cells[0])
for (const [index, cell] of cells.entries()) {
const retained = matching
.map((candidate) => candidate[index] ?? '')
.join(' ')
.replace(/\s+/g, ' ')
assert(
retained.includes(cell.replace(/\s+/g, ' ')),
`${path}: oversized table cell lost content`
)
}
}
}
})
}
}
async function verifyDatabase() {
const [existing] = await sql<
Array<{ count: number }>
>`SELECT count(*)::int AS count FROM information_schema.tables WHERE table_schema = 'public'`
assert(existing?.count === 0, 'Refusing to mutate a database that already contains tables')
await sql`CREATE EXTENSION IF NOT EXISTS vector`
await sql`
CREATE TABLE docs_embeddings (
chunk_id uuid PRIMARY KEY,
chunk_text text NOT NULL,
source_document text NOT NULL,
source_link text NOT NULL,
header_text text NOT NULL,
header_level integer NOT NULL,
token_count integer NOT NULL,
embedding vector(1536) NOT NULL,
embedding_model text NOT NULL DEFAULT 'text-embedding-3-small',
metadata jsonb NOT NULL DEFAULT '{}',
chunk_text_tsv tsvector GENERATED ALWAYS AS (to_tsvector('english', chunk_text)) STORED,
created_at timestamp NOT NULL DEFAULT now(),
updated_at timestamp NOT NULL DEFAULT now()
)
`
created = true
await sql`CREATE INDEX ON docs_embeddings(source_document)`
await sql`CREATE INDEX ON docs_embeddings USING gin(chunk_text_tsv)`
const page = getPage('/introduction')
assert(page)
const probe = generateId()
const query = 'simhybridprobe'
await sql`
INSERT INTO docs_embeddings (chunk_id, chunk_text, source_document, source_link, header_text, header_level, token_count, embedding, metadata)
VALUES (${probe}, ${query}, ${page.sourcePath}, 'https://docs.sim.ai/introduction', 'Introduction', 1, 1, ${vector}::vector, ${sql.json({ contentHash: page.contentHash })})
`
await check('Real PostgreSQL keyword results work with no embedding key', async () => {
assert.equal(process.env.OPENAI_API_KEY, '')
const stored = await searchDatabase()<
Array<{ chunk_id: string; metadata: { contentHash: string } }>
>`SELECT chunk_id, metadata FROM docs_embeddings WHERE chunk_text_tsv @@ plainto_tsquery('english', ${query})`
assert.equal(stored[0]?.chunk_id, probe)
assert.equal(stored[0]?.metadata.contentHash, page.contentHash)
const results = await searchDocs(query)
assert.equal(results.length, 1)
assert.equal(results[0]?.id, probe)
assert.equal(results[0]?.url, 'https://docs.sim.ai/introduction')
})
await check('Repeated lower-ranked chunks do not outrank a stronger page match', async () => {
const otherPage = getPage('/getting-started')
assert(otherPage)
const rankingQuery = 'simrankingprobe'
try {
for (const candidate of [page, otherPage])
await sql`
INSERT INTO docs_embeddings (chunk_id, chunk_text, source_document, source_link, header_text, header_level, token_count, embedding, metadata)
VALUES (${generateId()}, ${candidate === page ? `${rankingQuery} ${rankingQuery}` : rankingQuery}, ${candidate.sourcePath}, ${`https://docs.sim.ai${candidate.url}`}, ${candidate.title}, 1, 2, ${vector}::vector, ${sql.json({ contentHash: candidate.contentHash })})
`
const expected = ['https://docs.sim.ai/introduction', 'https://docs.sim.ai/getting-started']
assert.deepEqual(
(await searchDocs(rankingQuery)).map((result) => result.url),
expected
)
await sql`
INSERT INTO docs_embeddings (chunk_id, chunk_text, source_document, source_link, header_text, header_level, token_count, embedding, metadata)
VALUES (${generateId()}, ${rankingQuery}, ${otherPage.sourcePath}, 'https://docs.sim.ai/getting-started', ${otherPage.title}, 1, 1, ${vector}::vector, ${sql.json({ contentHash: otherPage.contentHash })})
`
assert.deepEqual(
(await searchDocs(rankingQuery)).map((result) => result.url),
expected
)
} finally {
await sql`DELETE FROM docs_embeddings WHERE chunk_text_tsv @@ plainto_tsquery('english', ${rankingQuery})`
}
})
await check('Stale index hashes cannot supply search citations', async () => {
await sql`UPDATE docs_embeddings SET metadata = '{"contentHash":"stale"}' WHERE chunk_id = ${probe}`
assert.deepEqual(await searchDocs(query), [])
await sql`UPDATE docs_embeddings SET metadata = ${sql.json({ contentHash: page.contentHash })} WHERE chunk_id = ${probe}`
})
await check(
'Foreign origins and nonexistent anchors cannot supply search citations',
async () => {
for (const url of [
'https://untrusted.example/introduction',
'https://docs.sim.ai/introduction#unknown-anchor',
]) {
await sql`UPDATE docs_embeddings SET source_link = ${url} WHERE chunk_id = ${probe}`
assert.deepEqual(await searchDocs(query), [])
}
await sql`UPDATE docs_embeddings SET source_link = 'https://docs.sim.ai/introduction' WHERE chunk_id = ${probe}`
}
)
await check('Database query failures retain credential-free catalog results', async () => {
await sql`ALTER TABLE docs_embeddings RENAME TO docs_embeddings_unavailable`
try {
assert(
(await searchDocs('Slack')).some(
(result) => new URL(result.url).pathname === '/integrations/slack'
)
)
} finally {
await sql`ALTER TABLE docs_embeddings_unavailable RENAME TO docs_embeddings`
}
})
await sql`DELETE FROM docs_embeddings`
const pages = getPages().filter((page) => page.url !== '/')
for (const page of pages) {
const chunks = [...pageSearchChunks(page)]
const source = chunks[0]?.sourceDocument
assert(source)
const rows = chunks.map((chunk) => ({
chunk_id: generateId(),
chunk_text: chunk.text,
source_link: chunk.sourceLink,
header_text: chunk.headerText,
header_level: chunk.headerLevel,
token_count: Math.ceil(chunk.text.length / 4),
metadata: {
title: page.title,
contentHash: page.contentHash,
chunkId: chunk.id,
embeddingInputHash: createHash('sha256')
.update(
JSON.stringify([
'text-embedding-3-small',
1536,
`${page.title}\n${chunk.headerText}\n${chunk.text}`,
])
)
.digest('hex'),
catalog: 'sim-docs-v2',
},
}))
await sql`
INSERT INTO docs_embeddings (chunk_id, chunk_text, source_document, source_link, header_text, header_level, token_count, embedding, metadata)
SELECT chunk_id, chunk_text, ${source}, source_link, header_text, header_level, token_count, ${vector}::vector, metadata
FROM jsonb_to_recordset(${sql.json(rows)}) AS fixture(chunk_id uuid, chunk_text text, source_link text, header_text text, header_level integer, token_count integer, metadata jsonb)
`
}
const fresh = await fingerprint()
await check(
'Fresh index checks succeed and repeated publication preserves row identities without credentials',
async () => {
assert.equal((await index('check', 'fresh-check')).exitCode, 0)
assert.equal((await index('publish', 'fresh-publish')).exitCode, 0)
assert.deepEqual(await fingerprint(), fresh)
}
)
await check(
'Citation-only changes are stale and repair without replacing embeddings',
async () => {
const citations = await sql<
Array<{ chunk_id: string; source_link: string; embedding: string }>
>`SELECT chunk_id, source_link, embedding::text FROM docs_embeddings WHERE source_document = ${page.sourcePath} ORDER BY chunk_id`
assert(citations.length > 0)
await sql`UPDATE docs_embeddings SET source_link = 'https://old.example/introduction' WHERE source_document = ${page.sourcePath}`
assert.equal((await index('check', 'citation-stale', ['/introduction'])).exitCode, 1)
const repaired = await index('publish', 'citation-repaired', ['/introduction'])
assert.equal(repaired.exitCode, 0)
assert.deepEqual(await fingerprint(), fresh)
const repairedCitations = await sql<
Array<{ chunk_id: string; source_link: string; embedding: string }>
>`SELECT chunk_id, source_link, embedding::text FROM docs_embeddings WHERE source_document = ${page.sourcePath} ORDER BY chunk_id`
assert.deepEqual(repairedCitations, citations)
assert.equal((await index('check', 'citation-fresh', ['/introduction'])).exitCode, 0)
}
)
await check('Concurrent publishers fail before modifying any rows', async () => {
await sql`SELECT pg_advisory_lock(724130561)`
try {
const result = await index('publish', 'concurrent-publish', ['/introduction'])
assert.equal(result.exitCode, 1)
assert(result.report.includes('Another documentation index publisher or check is active'))
assert.deepEqual(await fingerprint(), fresh)
} finally {
await sql`SELECT pg_advisory_unlock(724130561)`
}
})
await check('Freshness rejects modified chunk bytes and embedding model identities', async () => {
const [row] = await sql<
Array<{ chunk_id: string; chunk_text: string }>
>`SELECT chunk_id, chunk_text FROM docs_embeddings WHERE source_document = ${page.sourcePath} LIMIT 1`
assert(row)
await sql`UPDATE docs_embeddings SET chunk_text = 'corrupt stored chunk' WHERE chunk_id = ${row.chunk_id}`
assert.equal((await index('check', 'corrupt-text', ['/introduction'])).exitCode, 1)
const failure = await index('publish', 'corrupt-no-key', ['/introduction'])
assert.equal(failure.exitCode, 1)
assert(failure.report.includes('OPENAI_API_KEY is required'))
assert.deepEqual(await fingerprint(), fresh)
await sql`UPDATE docs_embeddings SET chunk_text = ${row.chunk_text}, embedding_model = 'wrong-model' WHERE chunk_id = ${row.chunk_id}`
assert.equal((await index('check', 'wrong-model', ['/introduction'])).exitCode, 1)
await sql`UPDATE docs_embeddings SET embedding_model = 'text-embedding-3-small' WHERE chunk_id = ${row.chunk_id}`
assert.equal((await index('check', 'restored-chunk', ['/introduction'])).exitCode, 0)
})
await sql`
INSERT INTO docs_embeddings (chunk_id, chunk_text, source_document, source_link, header_text, header_level, token_count, embedding, metadata)
VALUES (${generateId()}, 'retired fixture', 'retired-fixture.mdx', 'https://docs.sim.ai/retired-fixture', 'Retired', 1, 1, ${vector}::vector, '{}')
`
await check(
'Freshness checks reject retired pages and publication retires only those rows',
async () => {
const check = await index('check', 'retired-check')
assert.equal(check.exitCode, 1)
assert(check.report.includes('removed documents remain indexed'))
assert.equal((await index('publish', 'retired-publish')).exitCode, 0)
assert.deepEqual(await fingerprint(), fresh)
}
)
const first = pages[0]
assert(first)
const source = [...pageSearchChunks(first)][0]?.sourceDocument
assert(source)
await sql`UPDATE docs_embeddings SET metadata = '{"contentHash":"stale"}' WHERE source_document = ${source}`
await check(
'A missing embedding key fails publication without deleting old document data',
async () => {
const before = await fingerprint()
const result = await index('publish', 'failed-publish')
assert.equal(result.exitCode, 1)
assert(result.report.includes('OPENAI_API_KEY is required'))
assert.deepEqual(await fingerprint(), before)
assert.equal((await index('check', 'stale-check')).exitCode, 1)
}
)
const responseValues = new Map<string, number>()
const requestSchema = z.object({
input: z.array(z.string().min(1).max(8000)).min(1).max(16),
model: z.literal('text-embedding-3-small'),
dimensions: z.literal(1536),
encoding_format: z.literal('float'),
})
provider = serve({
hostname: '127.0.0.1',
port: 0,
async fetch(request) {
assert.equal(new URL(request.url).pathname, '/v1/embeddings')
assert.equal(request.method, 'POST')
assert.equal(request.headers.get('authorization'), 'Bearer isolated-fixture')
const body = requestSchema.parse(await request.json())
scenarioRequests += 1
assert(providerRequests.length < 16, 'Provider fixture request cap exceeded')
const status =
providerScenario === '401'
? 401
: providerScenario === 'long-retry'
? 429
: providerScenario === 'batch-failure' && scenarioRequests > 1
? 503
: (providerScenario === '429' || providerScenario === '503') && scenarioRequests === 1
? Number(providerScenario)
: 200
providerRequests.push({
scenario: providerScenario,
attempt: scenarioRequests,
status,
inputs: body.input.length,
})
if (status !== 200)
return Response.json(
{ error: { message: 'Isolated fixture failure' } },
{ status, headers: { 'Retry-After': providerScenario === 'long-retry' ? '120' : '0' } }
)
const data = body.input
.map((input, index) => {
responseValues.set(input, index + 1)
return {
index: providerScenario === 'indices' ? body.input.length : index,
embedding: [
index + 1,
...Array.from({ length: providerScenario === 'dimensions' ? 1534 : 1535 }, () => 0),
],
}
})
.reverse()
return Response.json({ data, usage: { total_tokens: body.input.length } })
},
})
await writeFile(
preloadPath,
`
const endpoint = ${JSON.stringify(new URL('/v1/embeddings', provider.url).href)};
const transport = globalThis.fetch;
globalThis.fetch = Object.assign(async (input, init) => {
const request = new Request(input, init);
if (request.url !== 'https://api.openai.com/v1/embeddings') throw new Error('Unexpected provider URL in isolated test');
return transport(new Request(endpoint, request));
}, transport);
`
)
const introChunks = [...pageSearchChunks(page)]
assert(
introChunks.length > 0 && introChunks.length <= 16,
'Introduction must fit one bounded fixture batch'
)
await check(
'First publication writes provider vectors in input order and an unchanged repeat makes no requests',
async () => {
await sql`DELETE FROM docs_embeddings WHERE source_document = ${page.sourcePath}`
scenario('success')
assert.equal((await index('publish', 'provider-first', ['/introduction'], true)).exitCode, 0)
assert.equal(scenarioRequests, 1)
const rows = await sql<
Array<{
chunk_text: string
header_text: string
source_link: string
embedding: string
metadata: { title: string }
}>
>`
SELECT chunk_text, header_text, source_link, embedding::text, metadata FROM docs_embeddings WHERE source_document = ${page.sourcePath}
`
assert.equal(rows.length, providerRequests.at(-1)?.inputs)
for (const row of rows) {
const input = `${row.metadata.title}\n${row.header_text}\n${row.chunk_text}`
const values = z.array(z.number()).parse(JSON.parse(row.embedding))
assert.equal(values.length, 1536)
assert.equal(values[0], responseValues.get(input))
assert.equal(new URL(row.source_link).pathname, '/introduction')
}
const before = await documentState(page.sourcePath)
assert.equal(
(await index('publish', 'provider-unchanged', ['/introduction'], true)).exitCode,
0
)
assert.equal(scenarioRequests, 1)
assert.deepEqual(await documentState(page.sourcePath), before)
}
)
await check(
'Changed stored chunk bytes are replaced while retaining other row identities',
async () => {
const rows = await sql<
Array<{ chunk_id: string }>
>`SELECT chunk_id FROM docs_embeddings WHERE source_document = ${page.sourcePath} ORDER BY chunk_id`
const changed = rows[0]
assert(changed)
await sql`UPDATE docs_embeddings SET chunk_text = 'stale provider fixture' WHERE chunk_id = ${changed.chunk_id}`
scenario('success')
assert.equal(
(await index('publish', 'provider-changed', ['/introduction'], true)).exitCode,
0
)
assert.equal(scenarioRequests, 1)
assert.equal(providerRequests.at(-1)?.inputs, 1)
const after = await sql<
Array<{ chunk_id: string }>
>`SELECT chunk_id FROM docs_embeddings WHERE source_document = ${page.sourcePath}`
assert.equal(after.length, rows.length)
assert(!after.some((row) => row.chunk_id === changed.chunk_id))
for (const retained of rows.slice(1))
assert(after.some((row) => row.chunk_id === retained.chunk_id))
}
)
for (const status of ['429', '503'] as const) {
await check(
`Provider HTTP ${status} retries once and publishes the successful response`,
async () => {
await sql`UPDATE docs_embeddings SET chunk_text = 'stale provider fixture' WHERE chunk_id = (SELECT chunk_id FROM docs_embeddings WHERE source_document = ${page.sourcePath} LIMIT 1)`
scenario(status)
assert.equal(
(await index('publish', `provider-retry-${status}`, ['/introduction'], true)).exitCode,
0
)
assert.equal(scenarioRequests, 2)
assert.equal(providerRequests.at(-2)?.status, Number(status))
assert.equal(providerRequests.at(-1)?.status, 200)
assert.equal(
(await index('check', `provider-fresh-${status}`, ['/introduction'])).exitCode,
0
)
}
)
}
for (const failure of ['401', 'dimensions', 'indices', 'long-retry'] as const) {
await check(
`Provider ${failure} fails without retry or modifying the previous document`,
async () => {
await sql`UPDATE docs_embeddings SET chunk_text = 'stale provider fixture' WHERE chunk_id = (SELECT chunk_id FROM docs_embeddings WHERE source_document = ${page.sourcePath} LIMIT 1)`
const before = await documentState(page.sourcePath)
scenario(failure)
const result = await index(
'publish',
`provider-failure-${failure}`,
['/introduction'],
true
)
assert.equal(result.exitCode, 1)
assert.equal(scenarioRequests, 1)
assert.deepEqual(await documentState(page.sourcePath), before)
}
)
}
await check(
'A successful first batch followed by persistent failure leaves the entire previous document intact',
async () => {
const multiBatch = pages.find((candidate) => {
const count = [...pageSearchChunks(candidate)].length
return count > 16 && count <= 32
})
assert(multiBatch, 'A bounded two-batch document is required')
const source = [...pageSearchChunks(multiBatch)][0]?.sourceDocument
assert(source)
await sql`UPDATE docs_embeddings SET chunk_text = 'stale batch fixture' WHERE source_document = ${source}`
const before = await documentState(source)
scenario('batch-failure')
const result = await index('publish', 'provider-partial-failure', [multiBatch.url], true)
assert.equal(result.exitCode, 1)
assert(scenarioRequests >= 2 && scenarioRequests <= 4)
const attempts = providerRequests.filter((row) => row.scenario === 'batch-failure')
assert.equal(attempts[0]?.status, 200)
assert.equal(attempts[0]?.inputs, 16)
assert(attempts.slice(1).every((row) => row.status === 503))
assert.deepEqual(await documentState(source), before)
}
)
}
try {
await check(
'Compiled publication chunks preserve code, tables, context and bounded payloads',
verifyChunks
)
if (!chunksOnly) await verifyDatabase()
} catch (error) {
if (!results.some((result) => result.status === 'failed')) {
results.push({
name: 'Fixture lifecycle',
status: 'failed',
durationMs: 0,
error: getErrorMessage(error),
})
}
} finally {
await provider?.stop(true)
await rm(preloadPath, { force: true })
if (created) await sql`DROP TABLE docs_embeddings`
await Promise.all([sql.end({ timeout: 5 }), searchDatabase().end({ timeout: 5 })])
await writeFile(
reportPath,
`${JSON.stringify({ scope: chunksOnly ? 'compiled-content' : 'postgres-publication', results, provider: { mode: 'loopback-http-fixture', maxRequests: 16, maxInputsPerRequest: 16, requests: providerRequests } }, null, 2)}\n`
)
}
logger.info(
chunksOnly ? 'Verified compiled search chunks' : 'Verified isolated PostgreSQL search boundary',
{ checks: results.length, reportPath }
)
if (results.some((result) => result.status === 'failed')) process.exitCode = 1