fix(test): stream large report snapshot SQL
Independent Staging Quality Gate / validate (push) Successful in 11m8s
Independent Staging Quality Gate / publish (push) Successful in 3m52s

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
jesse-ux
2026-09-23 18:51:08 +08:00
co-authored by Claude Code
parent 451a58089a
commit 018b2b4883
8 changed files with 148 additions and 6 deletions
@@ -165,14 +165,14 @@ test("personal report sections enforce owner-read RLS and service-owned durable
readerDashaApplicability: reader.reader_dasha_applicability,
}, identity);
const encoded = JSON.stringify(snapshot).replaceAll("'", "''");
fixture.psqlAs("service_runtime", "service-runtime-test-password", serviceSql(`
fixture.psqlScriptAs("service_runtime", "service-runtime-test-password", serviceSql(`
select status from public.ensure_personal_report_section('${USER_A}', '${REQUEST_ID}', '${LONGFORM_SNAPSHOT_SECTION_ID}', 2);
select status from public.complete_personal_report_section('${USER_A}', '${REQUEST_ID}', '${LONGFORM_SNAPSHOT_SECTION_ID}', '${encoded}'::jsonb);
-- Late ensure and conflicting completion must preserve the first committed snapshot.
select status from public.ensure_personal_report_section('${USER_A}', '${REQUEST_ID}', '${LONGFORM_SNAPSHOT_SECTION_ID}', 2);
select status from public.complete_personal_report_section('${USER_A}', '${REQUEST_ID}', '${LONGFORM_SNAPSHOT_SECTION_ID}', '{"kind":"stale-worker"}'::jsonb);
`));
const durable = JSON.parse(fixture.psql(`select payload from public.personal_report_sections where user_id = '${USER_A}' and request_id = '${REQUEST_ID}' and section_id = '${LONGFORM_SNAPSHOT_SECTION_ID}'`));
const durable = JSON.parse(fixture.psqlScriptAs("postgres", "postgres-test-password", `select payload from public.personal_report_sections where user_id = '${USER_A}' and request_id = '${REQUEST_ID}' and section_id = '${LONGFORM_SNAPSHOT_SECTION_ID}'`));
assert.deepEqual(validateLongformSnapshot(durable, identity), snapshot);
assert.equal(fixture.psqlAs("app_runtime", "app-runtime-test-password", selectAsAuthenticated(USER_B, `select count(*) from public.personal_report_sections where section_id = '${LONGFORM_SNAPSHOT_SECTION_ID}'`)), `SET\n${USER_B}\n0`);
} finally {
@@ -0,0 +1,27 @@
import assert from "node:assert/strict";
import test from "node:test";
import { startPostgresFixture } from "./helpers/postgres-fixture.ts";
test("stdin PostgreSQL scripts carry large SQL and preserve transaction-local settings", () => {
const fixture = startPostgresFixture();
try {
const payload = "x".repeat(131_073);
const script = `
select set_config('app.test_large_script', 'stdin-helper', true);
select current_setting('app.test_large_script');
select length($$${payload}$$);
`;
assert.equal(Buffer.byteLength(script, "utf8") > 131_072, true);
assert.equal(
fixture.psqlScriptAs("postgres", "postgres-test-password", script),
"stdin-helper\nstdin-helper\n131073",
);
assert.equal(
fixture.psqlScriptAs("postgres", "postgres-test-password", "select repeat('x', 1048577);"),
"x".repeat(1_048_577),
);
} finally {
fixture.stop();
}
});
@@ -17,6 +17,7 @@ export type PostgresFixture = {
connectionUrl(role: string, password: string): string;
psql(sql: string): string;
psqlAs(role: string, password: string, sql: string): string;
psqlScriptAs(role: string, password: string, sql: string): string;
stop(): void;
};
@@ -270,6 +271,37 @@ export function startPostgresFixture(): PostgresFixture {
{ encoding: "utf8", env: environment },
).trim();
},
psqlScriptAs(role, password, sql) {
return execFileSync(
"docker",
[
...composeArguments,
"exec",
"-T",
"-e",
`PGPASSWORD=${password}`,
"postgres",
"psql",
"-X",
"-At",
"-v",
"ON_ERROR_STOP=1",
"--single-transaction",
"-f",
"-",
"-U",
role,
"-d",
"jyotisha",
],
{
encoding: "utf8",
env: environment,
input: sql,
maxBuffer: 16 * 1024 * 1024,
},
).trim();
},
stop() {
try {
execFileSync(
@@ -33,3 +33,12 @@ test("postgres fixture start failure and stop both release the compose-network s
/stop\(\) \{[\s\S]*releasePort\(portReservation\)[\s\S]*slot\.release\(\)/,
);
});
test("large PostgreSQL scripts use stdin without changing shared -c helpers", () => {
assert.match(fixtureSource, /psqlScriptAs\(role, password, sql\)/);
assert.match(fixtureSource, /psqlScriptAs[\s\S]*input: sql/);
assert.match(fixtureSource, /psqlScriptAs[\s\S]*maxBuffer: 16 \* 1024 \* 1024/);
assert.match(fixtureSource, /psqlScriptAs[\s\S]*"--single-transaction"[\s\S]*"-f"[\s\S]*"-"/);
assert.match(fixtureSource, /psql\(sql\)[\s\S]*"-Atc",\s*sql/);
assert.match(fixtureSource, /psqlAs\(role, password, sql\)[\s\S]*"-Atc",\s*sql/);
});