Files
Jyotisha/frontend/tests/database-report-worker-subject.test.ts
ClaudeandClaude Opus 5.5 3e6077f752
Independent Staging Quality Gate / validate-frontend (push) Successful in 5m18s
Independent Staging Quality Gate / validate-python (push) Successful in 6m40s
Independent Staging Quality Gate / publish (push) Successful in 2m57s
Merge #228 report uses profile gender (BUG-1866)
Resolved overlap with #227 in personal-report-longform-birth.ts: keep the
chart-choice window and add the profile gender.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXozsiu43ob7B8UZCmDM
2026-11-13 08:58:36 +08:00

113 lines
5.2 KiB
TypeScript

import assert from "node:assert/strict";
import { randomUUID } from "node:crypto";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import test from "node:test";
import { closeLocalPostgresDataPool, createLocalPostgresDataClient } from "../src/lib/db/local-postgres-client-core.ts";
import { loadReportWorkerProfile } from "../src/lib/personal-report-worker-subject.ts";
import { buildLongformBirthPayload } from "../src/lib/personal-report-longform-birth.ts";
import { CHART_SUBJECT_SELECT } from "../src/lib/subject-birth.ts";
import { startPostgresFixture, type PostgresFixture } from "./helpers/postgres-fixture.ts";
// BUG-1063: the report worker reads another person's archive through
// service_role. Fictional rows only.
const runner = fileURLToPath(new URL("../scripts/db-migrate.mjs", import.meta.url));
const docker = spawnSync("docker", ["version", "--format", "{{.Server.Version}}"], { stdio: "ignore" }).status === 0;
const SERVICE_URL_ROLE = ["service_runtime", "service-runtime-test-password"] as const;
function applyMigrations(fixture: PostgresFixture): void {
const result = spawnSync(process.execPath, [runner], {
encoding: "utf8",
env: {
...process.env,
SCHEMA_DATABASE_URL: fixture.connectionUrl("schema_owner", "schema-owner-test-password"),
},
});
assert.equal(result.status, 0, result.stderr || result.stdout);
}
test("service_role can read another person's chart profile for a report", {
skip: docker ? false : "docker unavailable",
}, async () => {
const fixture = startPostgresFixture();
const serviceUrl = fixture.connectionUrl(...SERVICE_URL_ROLE);
try {
applyMigrations(fixture);
const owner = randomUUID();
const subjectId = randomUUID();
fixture.psql(`insert into identity.users (id, name, email, email_verified, email_verified_at)
values ('${owner}', 'Fictional owner', '${owner}@example.invalid', true, now());`);
fixture.psql(`update public.profiles set name = '虚构本人', birth_date = '1990-01-01',
reported_birth_time = '09:00', birth_time_source = 'family_exact', birth_time_status = 'reported',
latitude = 22.3, longitude = 114.1, timezone_id = 'Asia/Shanghai', timezone_offset = 8, gender = 'male'
where id = '${owner}';`);
fixture.psql(`insert into public.chart_profiles (
id, user_id, role, profile, updated_at, name, birth_date, reported_birth_time,
birth_time_source, latitude, longitude, timezone_id, timezone_offset,
birth_place_label, ayanamsa, birth_time_status, gender
) values (
'${subjectId}', '${owner}', 'other', '{"name":"虚构乙"}'::jsonb, now(),
'虚构乙', '1990-01-01', '08:00', 'family_exact', 22.3, 114.1,
'Asia/Shanghai', 8, '虚构港', 'lahiri', 'reported', 'female'
);`);
const admin = createLocalPostgresDataClient(serviceUrl, null, "service_role");
const probe = await admin
.from("chart_profiles")
.select(CHART_SUBJECT_SELECT)
.eq("id", subjectId)
.maybeSingle();
assert.equal(probe.error, null, probe.error?.message ?? "chart_profiles select failed");
const loaded = await loadReportWorkerProfile(admin, owner, subjectId) as { name?: string; gender?: string };
assert.equal(loaded.name, "虚构乙");
assert.equal(loaded.gender, "female");
const self = await loadReportWorkerProfile(admin, owner, "self") as { gender?: string };
assert.equal(self.gender, "male");
const adopted = await admin.rpc("chart_choice_adopt_v3", {
p_user_id: owner, p_chart_profile_id: subjectId, p_time: "07:59:00", p_date: "1990-01-01", p_offset: 8,
p_provenance: { contract: "chart-choice-v1", source: "user_choice", chosen_side: "after" },
p_window_start: null, p_window_end: null, p_before_minutes: null, p_after_minutes: null,
p_detail: {
window: { start_at: "1990-01-01T07:45", end_at: "1990-01-01T08:00" },
line: { at: "1990-01-01T07:59:43", charts: ["D10"] },
decision: { both_versions_domains: [] },
},
});
assert.equal(adopted.error, null);
const adoptedProfile = await loadReportWorkerProfile(admin, owner, subjectId);
const payload = buildLongformBirthPayload(adoptedProfile, {
today: "2026-11-13", targetYear: 2026, candidateRange: null, birthTimeAccuracy: "confirmed",
});
assert.equal(payload?.minute, 59);
assert.deepEqual(payload?.birth_time_window, {
source: "default_unknown_30", before_minutes: 30, after_minutes: 30,
rectified_range_rejected: "clock_outside_chosen_side",
});
for (const column of CHART_SUBJECT_SELECT.split(",")) {
assert.equal(
fixture.psql(`select has_column_privilege('service_role', 'public.chart_profiles', '${column}', 'select')`),
"t",
column,
);
}
for (const privilege of ["insert", "update", "delete"]) {
assert.equal(
fixture.psql(`select has_table_privilege('service_role', 'public.chart_profiles', '${privilege}')`),
"f",
privilege,
);
}
assert.equal(
fixture.psql("select has_table_privilege('authenticated', 'public.chart_profiles', 'select')"),
"t",
);
} finally {
await closeLocalPostgresDataPool(serviceUrl);
fixture.stop();
}
});