Repository navigation
Expand file tree
/
Copy pathsearch-refusals.live-postgres.test.ts
More file actions
79 lines (72 loc) · 3.12 KB
/
Copy pathsearch-refusals.live-postgres.test.ts
File metadata and controls
79 lines (72 loc) · 3.12 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* The errors a real `pg` pool throws, read as a search reads them: a statement
* past the `statement_timeout` set with `SET LOCAL` is `TIMEOUT` (504), a wait
* for a connection of a full pool is `SERVICE_UNAVAILABLE` (503), and the
* limit does not outlive its transaction on the pooled connection. The
* doubles in `search-refusals.test.ts` are shaped after these. Needs no
* pgvector, only `OS_TEST_POSTGRES_URL`; the pins are codes and settings,
* never durations (each limit here is far below what it stops).
*/
import { afterAll, describe, expect, it } from 'vitest';
import pg from 'pg';
import { poolFromPg } from '../index.js';
import { searchRefusalOf } from '../errors.js';
import { PG_URL, requireLiveCell } from './live-pg.testkit.js';
requireLiveCell('knowledge-pgvector search refusals');
const LIMITS = { statementTimeoutMs: 50 };
describe.skipIf(!PG_URL)('knowledge-pgvector search refusals — live PostgreSQL', () => {
const pools: pg.Pool[] = [];
const open = (config: pg.PoolConfig): pg.Pool => {
const pool = new pg.Pool({ connectionString: PG_URL, ...config });
pools.push(pool);
return pool;
};
afterAll(async () => {
for (const pool of pools) await pool.end();
});
it('a statement past SET LOCAL statement_timeout is TIMEOUT (504), and the limit ends with its transaction', async () => {
const pool = poolFromPg(open({ max: 1 }));
const client = await pool.connect();
try {
const before = await client.query<{ statement_timeout: string }>('SHOW statement_timeout');
await client.query('BEGIN');
await client.query(`SET LOCAL statement_timeout = ${LIMITS.statementTimeoutMs}`);
const err = await client.query('SELECT pg_sleep(30)').then(
() => undefined,
(e: unknown) => e,
);
await client.query('ROLLBACK');
expect(err).toMatchObject({ code: '57014' });
expect(searchRefusalOf(err, 'policies', LIMITS)).toMatchObject({ code: 'TIMEOUT', status: 504 });
// The same pooled connection, after the transaction: the session's own setting, untouched.
const after = await client.query<{ statement_timeout: string }>('SHOW statement_timeout');
expect(after.rows[0].statement_timeout).toBe(before.rows[0].statement_timeout);
} finally {
client.release();
}
});
it("a full pool's wait timing out is SERVICE_UNAVAILABLE (503), through poolFromPg", async () => {
const pool = poolFromPg(open({ max: 1, connectionTimeoutMillis: 50 }));
const held = await pool.connect();
try {
const viaQuery = await pool.query('SELECT 1').then(
() => undefined,
(e: unknown) => e,
);
const viaConnect = await pool.connect().then(
(c) => {
c.release();
return undefined;
},
(e: unknown) => e,
);
for (const err of [viaQuery, viaConnect]) {
expect(err).toBeInstanceOf(Error);
expect(searchRefusalOf(err, 'policies', LIMITS)).toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 });
}
} finally {
held.release();
}
});
});