Files
Jyotisha/frontend/tests/database-chart-choice-steps-2256.test.ts
T
ClaudeandClaude Opus 5.5 326d919c36
Independent Staging Quality Gate / validate-frontend (push) Successful in 5m27s
Independent Staging Quality Gate / validate-python (push) Successful in 7m12s
Independent Staging Quality Gate / publish (push) Successful in 4m11s
test: chart_choice_steps against PostgreSQL — service-only writes, stated facts and time source, profile deletion (BUG-2256)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXp2U3BH1SxwAGyhhbe1
2026-11-23 16:17:33 +08:00

73 lines
4.4 KiB
TypeScript

import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { Client } from "pg";
import { runMigrations } from "../scripts/db-migrate.mjs";
import { startPostgresFixture } from "./helpers/postgres-fixture.ts";
// BUG-2256:生时纠正每一步写进 chart_choice_steps;只有 service_role 能写,普通角色读不到;
// 当时的出生资料与时间来源由数据库读取;删人物档案时随之删除。
const docker = spawnSync("docker", ["version"], { stdio: "ignore" }).status === 0;
const migrationDirs = ["../db/migrations", "../supabase/migrations"].map(
(dir) => fileURLToPath(new URL(dir, import.meta.url)),
);
test("BUG-2256 records each 生时纠正 step with the stated facts and its time source", {
skip: !docker && "docker unavailable",
}, async () => {
const fixture = startPostgresFixture();
const migrationUrl = fixture.connectionUrl("schema_owner", "schema-owner-test-password");
const owner = randomUUID();
const person = randomUUID();
const editor = new Client({ connectionString: migrationUrl });
const service = new Client({ connectionString: fixture.connectionUrl("service_runtime", "service-runtime-test-password") });
const ordinary = new Client({ connectionString: fixture.connectionUrl("app_runtime", "app-runtime-test-password") });
try {
assert.equal(await runMigrations({ connectionString: migrationUrl, migrationsDirectory: undefined, migrationsDirectories: migrationDirs }), 0);
await Promise.all([editor.connect(), service.connect(), ordinary.connect()]);
fixture.psqlAs("identity_runtime", "identity-runtime-test-password",
"insert into identity.users(id,name,email,email_verified) values ('" + owner
+ "','Fictional Steps','" + owner + "@example.invalid',true)");
await editor.query(
"update public.profiles set birth_date='2041-02-03', reported_birth_time='09:00', birth_time_source='approximate', "
+ "birth_time_status='reported' where id=$1", [owner],
);
await editor.query(
"insert into public.chart_profiles (id,user_id,role,name,profile,birth_date,reported_birth_time,birth_time_source,birth_time_status) "
+ "values ($1,$2,'other','Fictional Steps','{}','2041-02-03','09:00','hospital_record','reported')", [person, owner],
);
await Promise.all([service.query("set role service_role"), ordinary.query("set role authenticated")]);
const log = (client: Client, subject: string | null, step: string, outcome = "ok") => client.query(
"select public.chart_choice_log_step_v1($1,$2,$3,$4,$5,$6,$7)",
[owner, subject, step, outcome, "test-sha", { window: { start_at: "2041-02-03T08:45" } }, { http: 200 }],
);
await assert.rejects(log(ordinary, null, "packet"), /permission denied/);
await log(service, null, "packet");
await log(service, person, "adopt:choice");
await log(service, null, "adopt:preview", "error");
// A subject that is not this user's is skipped, not written and not an error.
await log(service, randomUUID(), "packet");
await assert.rejects(ordinary.query("select * from public.chart_choice_steps"), /permission denied/);
const rows = await editor.query<{ chart_profile_id: string | null; step: string; outcome: string; time_source: string; birth: string; window: string }>(
"select chart_profile_id, step, outcome, time_source, declaration->>'birth_date' as birth, request#>>'{window,start_at}' as window "
+ "from public.chart_choice_steps where user_id=$1 order by created_at", [owner],
);
assert.deepEqual(rows.rows, [
{ chart_profile_id: null, step: "packet", outcome: "ok", time_source: "approximate", birth: "2041-02-03", window: "2041-02-03T08:45" },
{ chart_profile_id: person, step: "adopt:choice", outcome: "ok", time_source: "hospital_record", birth: "2041-02-03", window: "2041-02-03T08:45" },
{ chart_profile_id: null, step: "adopt:preview", outcome: "error", time_source: "approximate", birth: "2041-02-03", window: "2041-02-03T08:45" },
]);
await editor.query("delete from public.chart_profiles where id=$1", [person]);
assert.equal((await editor.query("select 1 from public.chart_choice_steps where user_id=$1", [owner])).rowCount, 2);
} finally {
try {
await Promise.allSettled([editor.end(), service.end(), ordinary.end()]);
} finally {
fixture.stop();
}
}
});