749 lines
30 KiB
TypeScript
749 lines
30 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { safeParseServerReportDocument, computeEvidenceHash } from "../src/lib/personal-report-contract.server-core.ts";
|
|
import upstreamSourceManifest from "../../references/upstream/yinduzhanxing/source-manifest.json";
|
|
import {
|
|
REPORT_STABLE_CODES,
|
|
ReportEvidenceInsufficientError,
|
|
applyReportGuard,
|
|
assembleReportDocument,
|
|
buildReportEvidencePacket,
|
|
canonicalSerialize,
|
|
computeRequestFingerprint,
|
|
findForbiddenDeterministicClaims,
|
|
generatePersonalReport,
|
|
redactDeterministicSentences,
|
|
resolveSkillSnapshot,
|
|
sha256Hex,
|
|
type SkillSnapshot,
|
|
} from "../src/lib/personal-report-generation.ts";
|
|
import { PRECISE_TIMING_PATTERNS } from "../src/lib/personal-report-generation.ts";
|
|
import type {
|
|
PersonalReportAgentOutput,
|
|
ReportAgentPort,
|
|
ReportEvidencePacket,
|
|
} from "../src/mastra/personal-report.ts";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Fixtures: a real-shaped workflow response matching the Python main chain
|
|
// (object-map planets/houses/sections, varga_full D9_Navamsa/D10_Dasamsa)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function pythonStyleChartPayload() {
|
|
const signNames = ["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"];
|
|
const ascIndex = 4; // Leo
|
|
const planetsMap: Record<string, Record<string, unknown>> = {
|
|
Sun: { sign: "Leo", degree: 142.5, degree_raw: 142.5, degree_in_sign: 22.5, house: 1, retrograde: false, speed: 1.0 },
|
|
Moon: { sign: "Virgo", degree: 172.5, degree_raw: 172.5, degree_in_sign: 22.5, house: 2, retrograde: false, speed: 13.0 },
|
|
Mars: { sign: "Libra", degree: 202.5, degree_raw: 202.5, degree_in_sign: 22.5, house: 3, retrograde: false, speed: 0.6 },
|
|
Mercury: { sign: "Scorpio", degree: 232.5, degree_raw: 232.5, degree_in_sign: 22.5, house: 4, retrograde: true, speed: -0.5 },
|
|
Jupiter: { sign: "Sagittarius", degree: 262.5, degree_raw: 262.5, degree_in_sign: 22.5, house: 5, retrograde: false, speed: 0.2 },
|
|
Venus: { sign: "Capricorn", degree: 292.5, degree_raw: 292.5, degree_in_sign: 22.5, house: 6, retrograde: false, speed: 1.1 },
|
|
Saturn: { sign: "Aquarius", degree: 322.5, degree_raw: 322.5, degree_in_sign: 22.5, house: 7, retrograde: true, speed: -0.1 },
|
|
Rahu: { sign: "Pisces", degree: 352.5, degree_raw: 352.5, degree_in_sign: 22.5, house: 8, retrograde: true, speed: -0.05 },
|
|
Ketu: { sign: "Virgo", degree: 172.5, degree_raw: 172.5, degree_in_sign: 22.5, house: 2, retrograde: true, speed: -0.05 },
|
|
};
|
|
const housesMap: Record<string, Record<string, unknown>> = {};
|
|
for (let index = 0; index < 12; index += 1) {
|
|
housesMap[`house_${index + 1}`] = {
|
|
cusp_sign: signNames[(ascIndex + index) % 12],
|
|
cusp_degree: 140.5 + index * 30,
|
|
lord: signNames[(ascIndex + index) % 12],
|
|
};
|
|
}
|
|
const d9Planets: Record<string, Record<string, unknown>> = {
|
|
Sun: { sign: "Leo", sign_idx: 4 },
|
|
Moon: { sign: "Virgo", sign_idx: 5 },
|
|
Mars: { sign: "Cancer", sign_idx: 3 },
|
|
};
|
|
const d10Planets: Record<string, Record<string, unknown>> = {
|
|
Sun: { sign: "Taurus", sign_idx: 1 },
|
|
Moon: { sign: "Gemini", sign_idx: 2 },
|
|
};
|
|
return {
|
|
success: true,
|
|
chart: {
|
|
ascendant: { sign: "Leo", degree: 20.5, degree_raw: 140.5, lon: 140.5, sign_cn: "狮子座", lord: "Sun" },
|
|
planets: planetsMap,
|
|
houses: housesMap,
|
|
dasha: {
|
|
mahadashas: [
|
|
{ lord: "Moon", start: "2019-01-01", end: "2029-01-01" },
|
|
{ lord: "Mars", start: "2029-01-01", end: "2036-01-01" },
|
|
],
|
|
},
|
|
modules: {
|
|
varga_full: {
|
|
D9_Navamsa: {
|
|
_meta: { div: 9 },
|
|
Ascendant: { sign: "Leo", sign_idx: 4 },
|
|
...d9Planets,
|
|
_dignity: {},
|
|
},
|
|
D10_Dasamsa: {
|
|
_meta: { div: 10 },
|
|
Ascendant: { sign: "Taurus", sign_idx: 1 },
|
|
...d10Planets,
|
|
},
|
|
},
|
|
narayana_dasha: {
|
|
periods: [{ lord: "Sun", start: "2023-01-01", end: "2026-01-01" }],
|
|
},
|
|
},
|
|
},
|
|
consumer_context: {
|
|
route: "general",
|
|
core_status: "ready",
|
|
available_layers: ["D1", "D9", "D10", "Vimshottari", "Narayana"],
|
|
missing_route_layers: [],
|
|
hard_blockers: [],
|
|
answer_policy: {
|
|
can_answer_precise_timing: true,
|
|
deterministic_claims_forbidden_for: [],
|
|
},
|
|
},
|
|
machine_evidence_packet: {
|
|
conflicts: [
|
|
{ techniques: ["Vimshottari", "Narayana"], summary: "两个大运系统给出的阶段边界不一致" },
|
|
],
|
|
sections: {
|
|
D1: { status: "used", source_path: "chart.planets+chart.ascendant" },
|
|
D9: { status: "used", source_path: "modules.varga_full.D9" },
|
|
D10: { status: "used", source_path: "modules.varga_full.D10" },
|
|
D2: { status: "missing", source_path: "modules.varga_full.D2" },
|
|
planet_degrees: { status: "used", source_path: "chart.planets" },
|
|
house_degrees: { status: "used", source_path: "chart.houses" },
|
|
dasha_boundaries: { status: "used", source_path: "modules.dasha" },
|
|
narayana_dasha: { status: "used", source_path: "modules.narayana_dasha" },
|
|
external_oracle_status: { status: "used", source_path: "vedastro_official.runtime_truth" },
|
|
vedastro_official_raw_response: { status: "missing", source_path: "vedastro_official.raw_response" },
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
function buildPacket(overrides: Partial<BuildPacketOverrides> = {}): ReportEvidencePacket {
|
|
const workflow = overrides.workflow ?? pythonStyleChartPayload();
|
|
return buildReportEvidencePacket({
|
|
workflow,
|
|
subject: {
|
|
displayName: "测试用户",
|
|
birthTimeStatus: "confirmed",
|
|
birthPlaceLabel: "北京",
|
|
},
|
|
requestedThemes: ["career", "marriage", "wealth", "timing"],
|
|
reportType: "personal_full",
|
|
presentationMode: "default",
|
|
candidateRange: null,
|
|
skillSnapshot: { sha256: "a".repeat(64), sourceCommit: "b".repeat(40) },
|
|
});
|
|
}
|
|
|
|
type BuildPacketOverrides = {
|
|
workflow: unknown;
|
|
};
|
|
|
|
function agentOutput(overrides: Partial<PersonalReportAgentOutput> = {}): PersonalReportAgentOutput {
|
|
return {
|
|
executiveSummary: {
|
|
headline: "综合盘面以事业发展为主线",
|
|
summary: "命盘显示事业层面具备稳定的结构,财富与婚恋需结合分盘审慎解读。",
|
|
priorities: ["先聚焦职业方向", "再核对感情与财富主题"],
|
|
},
|
|
thematicNarrative: [
|
|
{
|
|
id: "career",
|
|
title: "事业",
|
|
narrative: "事业层面以十宫与 D10 结构为主,方向性判断稳定,具体应期需要结合大运边界观察。",
|
|
actions: ["在稳定领域深耕"],
|
|
caveats: ["该部分为方向性描述"],
|
|
claimStatus: "single_system_inference",
|
|
evidenceRefs: ["ev-audit-1", "ev-audit-3"],
|
|
},
|
|
{
|
|
id: "marriage",
|
|
title: "婚恋",
|
|
narrative: "婚恋部分以七宫与 D9 为主,呈现结构特征,不构成确定性结论。",
|
|
actions: [],
|
|
caveats: [],
|
|
claimStatus: "single_system_inference",
|
|
evidenceRefs: ["ev-audit-2"],
|
|
},
|
|
],
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Packet builder: allowlist + fail-closed
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("packet builder extracts only allowlisted facts, never internal noise", () => {
|
|
const workflow = pythonStyleChartPayload();
|
|
(workflow as Record<string, unknown>).internal_path = "/opt/app/private/engine.py";
|
|
(workflow as Record<string, unknown>).prompt_text = "系统提示词原文";
|
|
(workflow as Record<string, unknown>).traceback = "Traceback (most recent call last)";
|
|
(workflow as Record<string, unknown>).raw_payload = { anything: true };
|
|
const packet = buildPacket({ workflow });
|
|
const serialized = JSON.stringify(packet);
|
|
assert.doesNotMatch(serialized, /internal_path|prompt_text|traceback|raw_payload|\/opt\/app/);
|
|
assert.match(serialized, /ev-audit-/);
|
|
assert.match(serialized, /ev-conflict-/);
|
|
});
|
|
|
|
test("packet builder computes a canonical 64-hex calculation hash and marks derivation", () => {
|
|
const packet = buildPacket();
|
|
assert.match(packet.chart.calculationHash, /^[0-9a-f]{64}$/);
|
|
assert.equal(packet.chart.calculationHashDerived, true);
|
|
});
|
|
|
|
test("packet builder keeps a real engine hash when present", () => {
|
|
const workflow = pythonStyleChartPayload() as Record<string, unknown>;
|
|
const chart = workflow.chart as Record<string, unknown>;
|
|
chart.result_hash = "c".repeat(64);
|
|
const packet = buildPacket({ workflow });
|
|
assert.equal(packet.chart.calculationHash, "c".repeat(64));
|
|
assert.equal(packet.chart.calculationHashDerived, false);
|
|
});
|
|
|
|
test("packet builder fails closed without real D1 houses", () => {
|
|
const workflow = pythonStyleChartPayload() as Record<string, unknown>;
|
|
const chart = workflow.chart as Record<string, unknown>;
|
|
const houses = chart.houses as Record<string, unknown>;
|
|
delete houses["house_9"];
|
|
delete houses["house_10"];
|
|
delete houses["house_11"];
|
|
delete houses["house_12"];
|
|
assert.throws(() => buildPacket({ workflow }), ReportEvidenceInsufficientError);
|
|
});
|
|
|
|
test("packet builder fails closed without retrograde facts", () => {
|
|
const workflow = pythonStyleChartPayload() as Record<string, unknown>;
|
|
const chart = workflow.chart as Record<string, unknown>;
|
|
const planets = chart.planets as Record<string, Record<string, unknown>>;
|
|
planets.Sun.retrograde = undefined;
|
|
assert.throws(() => buildPacket({ workflow }), ReportEvidenceInsufficientError);
|
|
});
|
|
|
|
test("packet builder fails closed without ascendant or evidence refs", () => {
|
|
const workflow = pythonStyleChartPayload() as Record<string, unknown>;
|
|
const chart = workflow.chart as Record<string, unknown>;
|
|
delete chart.ascendant;
|
|
assert.throws(() => buildPacket({ workflow }), ReportEvidenceInsufficientError);
|
|
|
|
const workflow2 = pythonStyleChartPayload() as Record<string, unknown>;
|
|
const consumer = workflow2.consumer_context as Record<string, unknown>;
|
|
consumer.available_layers = [];
|
|
const machine = workflow2.machine_evidence_packet as Record<string, unknown>;
|
|
machine.sections = {};
|
|
machine.conflicts = [];
|
|
assert.throws(() => buildPacket({ workflow: workflow2 }), ReportEvidenceInsufficientError);
|
|
});
|
|
|
|
test("no mock fallback: an empty workflow never yields a usable packet", () => {
|
|
assert.throws(() => buildPacket({ workflow: {} }), ReportEvidenceInsufficientError);
|
|
});
|
|
|
|
test("packet builder normalizes the real Python object-map shapes", () => {
|
|
const packet = buildPacket();
|
|
// planets object map -> facts, absolute longitude from degree/degree_raw.
|
|
assert.equal(packet.chart.planets.length, 9);
|
|
const sun = packet.chart.planets.find((planet) => planet.id === "Sun");
|
|
assert.ok(sun);
|
|
assert.equal(sun.sign, "Leo");
|
|
assert.equal(sun.degree, 142.5);
|
|
assert.equal(sun.house, 1);
|
|
assert.equal(sun.retrograde, false);
|
|
assert.equal(packet.chart.planets.find((planet) => planet.id === "Saturn")?.retrograde, true);
|
|
// houses object map (cusp_sign only) -> whole-sign derived signs, marked.
|
|
assert.equal(packet.chart.houses.length, 12);
|
|
assert.equal(packet.chart.houses[0].sign, "Leo");
|
|
assert.equal(packet.chart.houses[0].signDerived, true);
|
|
assert.deepEqual(packet.chart.houses[0].occupants, ["Sun"]);
|
|
assert.equal(packet.chart.houses[1].sign, "Virgo");
|
|
assert.deepEqual([...packet.chart.houses[1].occupants].sort(), ["Ketu", "Moon"]);
|
|
// sections object map -> deterministic statuses: core calculation sections
|
|
// verified, internal layers partial, external/missing degraded.
|
|
const refs = packet.evidenceRefs;
|
|
const verified = refs.filter((ref) => ref.status === "verified").map((ref) => ref.technique);
|
|
assert.ok(verified.includes("planet_degrees"));
|
|
assert.ok(verified.includes("house_degrees"));
|
|
const blocked = refs.filter((ref) => ref.status === "blocked").map((ref) => ref.technique);
|
|
assert.ok(blocked.includes("D2"));
|
|
assert.ok(blocked.includes("vedastro_official_raw_response"));
|
|
const partial = refs.filter((ref) => ref.status === "partial").map((ref) => ref.technique);
|
|
assert.ok(partial.includes("dasha_boundaries"));
|
|
assert.ok(partial.includes("external_oracle_status"));
|
|
// conflicts produce canonical ev-conflict refs.
|
|
assert.ok(refs.some((ref) => ref.id.startsWith("ev-conflict-")));
|
|
});
|
|
|
|
test("packet builder resolves the base chart from modules.chart and nested chart", () => {
|
|
const base = pythonStyleChartPayload() as Record<string, unknown>;
|
|
const chartData = base.chart as Record<string, unknown>;
|
|
// modules.chart wins over nested chart over top level.
|
|
const modulesChart = {
|
|
...chartData,
|
|
planets: { Sun: { sign: "Aries", degree: 10.5, degree_raw: 10.5, house: 1, retrograde: false } },
|
|
};
|
|
const modules = chartData.modules as Record<string, unknown>;
|
|
modules.chart = modulesChart;
|
|
const viaModules = buildPacket({ workflow: base });
|
|
assert.equal(viaModules.chart.planets.length, 1);
|
|
assert.equal(viaModules.chart.planets[0].sign, "Aries");
|
|
delete modules.chart;
|
|
|
|
// nested chart_data.chart is the orchestrator's second choice.
|
|
const nested = {
|
|
...chartData,
|
|
planets: { Moon: { sign: "Pisces", degree: 350.5, degree_raw: 350.5, house: 12, retrograde: false } },
|
|
};
|
|
chartData.chart = nested;
|
|
const viaNested = buildPacket({ workflow: base });
|
|
assert.equal(viaNested.chart.planets.length, 1);
|
|
assert.equal(viaNested.chart.planets[0].id, "Moon");
|
|
});
|
|
|
|
test("varga houses are whole-sign derived from the divisional ascendant, never fabricated", () => {
|
|
const packet = buildPacket();
|
|
const d9 = packet.chart.vargaHouses.find((varga) => varga.id === "D9");
|
|
assert.ok(d9);
|
|
assert.equal(d9.houses.length, 12);
|
|
// D9 ascendant is Leo (index 4): house 1 Leo, house 2 Virgo.
|
|
assert.equal(d9.houses[0].sign, "Leo");
|
|
assert.equal(d9.houses[1].sign, "Virgo");
|
|
// Moon sits in Virgo (index 5) -> whole-sign house 2 of D9.
|
|
assert.ok(d9.houses[1].occupants.includes("Moon"));
|
|
assert.ok(d9.houses.every((house) => house.signDerived === true));
|
|
const d10 = packet.chart.vargaHouses.find((varga) => varga.id === "D10");
|
|
assert.ok(d10);
|
|
// D10 ascendant is Taurus (index 1): house 1 Taurus, house 2 Gemini.
|
|
assert.equal(d10.houses[0].sign, "Taurus");
|
|
assert.equal(d10.houses[1].sign, "Gemini");
|
|
assert.ok(d10.houses[1].occupants.includes("Moon"));
|
|
});
|
|
|
|
test("array-shaped chart data remains supported", () => {
|
|
const workflow = {
|
|
success: true,
|
|
chart: {
|
|
ascendant: { sign: "Leo", degree: 12.5 },
|
|
planets: [
|
|
{ id: "Sun", sign: "Leo", degree: 142.5, house: 1, retrograde: false },
|
|
{ id: "Moon", sign: "Virgo", degree: 172.5, house: 2, retrograde: false },
|
|
],
|
|
houses: Array.from({ length: 12 }, (_, index) => ({
|
|
number: index + 1,
|
|
sign: ["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"][index],
|
|
})),
|
|
},
|
|
consumer_context: {
|
|
route: "general",
|
|
core_status: "ready",
|
|
available_layers: ["D1"],
|
|
missing_route_layers: [],
|
|
hard_blockers: [],
|
|
answer_policy: { can_answer_precise_timing: true, deterministic_claims_forbidden_for: [] },
|
|
},
|
|
machine_evidence_packet: {
|
|
conflicts: [],
|
|
sections: [{ name: "planet_degrees", status: "verified", note: "" }],
|
|
},
|
|
};
|
|
const packet = buildPacket({ workflow });
|
|
assert.equal(packet.chart.planets.length, 2);
|
|
assert.equal(packet.chart.houses.length, 12);
|
|
// Array houses carry a real sign: not derived.
|
|
assert.equal(packet.chart.houses[0].signDerived, false);
|
|
assert.equal(packet.chart.houses[0].sign, "Aries");
|
|
assert.ok(packet.evidenceRefs.some((ref) => ref.status === "verified"));
|
|
});
|
|
|
|
test("section status mapping is deterministic (used core sections verified, external degraded)", () => {
|
|
const packet = buildPacket();
|
|
const byTechnique = new Map(packet.evidenceRefs.map((ref) => [ref.technique, ref.status]));
|
|
assert.equal(byTechnique.get("D1"), "verified");
|
|
assert.equal(byTechnique.get("planet_degrees"), "verified");
|
|
assert.equal(byTechnique.get("house_degrees"), "verified");
|
|
assert.equal(byTechnique.get("dasha_boundaries"), "partial");
|
|
assert.equal(byTechnique.get("external_oracle_status"), "partial");
|
|
assert.equal(byTechnique.get("D2"), "blocked");
|
|
assert.equal(byTechnique.get("vedastro_official_raw_response"), "blocked");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Fingerprint
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("request fingerprint is canonical: sorted, deduped themes, no requestId", () => {
|
|
const base = {
|
|
reportType: "personal_full",
|
|
presentationMode: "default",
|
|
themes: ["wealth", "career", "wealth", "timing"],
|
|
sessionId: null,
|
|
chartProfileId: null,
|
|
};
|
|
const fingerprintA = computeRequestFingerprint(base);
|
|
const fingerprintB = computeRequestFingerprint({
|
|
reportType: "personal_full",
|
|
presentationMode: "default",
|
|
themes: ["career", "timing", "wealth"],
|
|
sessionId: null,
|
|
chartProfileId: null,
|
|
});
|
|
assert.equal(fingerprintA, fingerprintB);
|
|
assert.equal(fingerprintA, sha256Hex(canonicalSerialize({
|
|
reportType: "personal_full",
|
|
presentationMode: "default",
|
|
themes: ["career", "timing", "wealth"],
|
|
sessionId: null,
|
|
chartProfileId: null,
|
|
})));
|
|
const differentType = computeRequestFingerprint({
|
|
...base,
|
|
reportType: "personal_thematic",
|
|
});
|
|
assert.notEqual(fingerprintA, differentType);
|
|
const withSession = computeRequestFingerprint({
|
|
...base,
|
|
sessionId: "11111111-1111-4111-8111-111111111111",
|
|
});
|
|
assert.notEqual(fingerprintA, withSession);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Assembly: canonical contract shape
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("assembled document passes the canonical server parse with a D1 of 12 houses", () => {
|
|
const packet = buildPacket();
|
|
const document = assembleReportDocument({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
generatedAt: "2026-08-06T00:00:00.000Z",
|
|
packet,
|
|
agentOutput: agentOutput(),
|
|
});
|
|
const parsed = safeParseServerReportDocument(document);
|
|
assert.equal(parsed.ok, true);
|
|
if (!parsed.ok) return;
|
|
assert.equal(parsed.document.charts.length, 3);
|
|
const d1 = parsed.document.charts.find((chart) => chart.id === "D1");
|
|
assert.ok(d1);
|
|
assert.equal(d1.houses.length, 12);
|
|
assert.deepEqual(
|
|
d1.houses.map((house) => house.houseNumber).sort((a, b) => a - b),
|
|
Array.from({ length: 12 }, (_, index) => index + 1),
|
|
);
|
|
assert.ok(d1.planets && d1.planets.length === 9);
|
|
assert.equal(d1.planets[0].retrograde, false);
|
|
assert.equal(d1.planets[6].retrograde, true);
|
|
// Appendix ids are canonical ev- ids and globally unique.
|
|
const ids = [
|
|
...parsed.document.evidenceAppendix.techniqueAudit.map((row) => row.id),
|
|
...parsed.document.evidenceAppendix.conflicts.map((row) => row.id),
|
|
...parsed.document.evidenceAppendix.calculationEvidence.map((row) => row.id),
|
|
];
|
|
assert.equal(new Set(ids).size, ids.length);
|
|
assert.ok(ids.every((id) => /^ev-[a-z0-9_-]{1,63}$/.test(id)));
|
|
// evidenceHash matches the canonical recomputation from the appendix.
|
|
assert.equal(
|
|
parsed.document.provenance.evidenceHash,
|
|
computeEvidenceHash(parsed.document.evidenceAppendix),
|
|
);
|
|
});
|
|
|
|
test("D9/D10 charts are omitted when no real divisional houses exist", () => {
|
|
const workflow = pythonStyleChartPayload() as Record<string, unknown>;
|
|
const chart = workflow.chart as Record<string, unknown>;
|
|
const modules = chart.modules as Record<string, unknown>;
|
|
modules.varga_full = {};
|
|
const packet = buildPacket({ workflow });
|
|
const document = assembleReportDocument({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
generatedAt: "2026-08-06T00:00:00.000Z",
|
|
packet,
|
|
agentOutput: agentOutput(),
|
|
});
|
|
const parsed = safeParseServerReportDocument(document);
|
|
assert.equal(parsed.ok, true);
|
|
if (!parsed.ok) return;
|
|
assert.deepEqual(parsed.document.charts.map((chartRow) => chartRow.id), ["D1"]);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Deterministic guard
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("guard rejects dangling evidence refs", () => {
|
|
const packet = buildPacket();
|
|
const document = assembleReportDocument({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
generatedAt: "2026-08-06T00:00:00.000Z",
|
|
packet,
|
|
agentOutput: agentOutput({
|
|
thematicNarrative: [
|
|
{
|
|
id: "career",
|
|
title: "事业",
|
|
narrative: "稳定结构。",
|
|
actions: [],
|
|
caveats: [],
|
|
claimStatus: "single_system_inference",
|
|
evidenceRefs: ["ev-audit-999"],
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
const guarded = applyReportGuard(document, packet);
|
|
assert.equal(guarded.ok, false);
|
|
if (!guarded.ok) assert.match(guarded.reason, /unresolved_evidence_ref/);
|
|
});
|
|
|
|
test("guard redacts precise timing and downgrades the section when timing is blocked", () => {
|
|
const packet = buildPacket();
|
|
const timingBlockedPacket = { ...packet, answerPolicy: { ...packet.answerPolicy, canAnswerPreciseTiming: false } };
|
|
const document = assembleReportDocument({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
generatedAt: "2026-08-06T00:00:00.000Z",
|
|
packet: timingBlockedPacket,
|
|
agentOutput: agentOutput({
|
|
thematicNarrative: [
|
|
{
|
|
id: "career",
|
|
title: "事业",
|
|
narrative: "方向稳定。2027年3月将迎来事业转折,届时务必把握机会。",
|
|
actions: ["2027年3月跳槽"],
|
|
caveats: [],
|
|
claimStatus: "single_system_inference",
|
|
evidenceRefs: ["ev-audit-1"],
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
const guarded = applyReportGuard(document, timingBlockedPacket);
|
|
assert.equal(guarded.ok, true);
|
|
if (!guarded.ok) return;
|
|
const section = (guarded.document as unknown as { thematicNarrative: { narrative: string; claimStatus: string; caveats: string[] }[] })
|
|
.thematicNarrative[0];
|
|
assert.doesNotMatch(section.narrative, /2027年3月/);
|
|
assert.equal(section.claimStatus, "blocked");
|
|
assert.ok(section.caveats.some((caveat) => caveat.includes("确定性边界")));
|
|
});
|
|
|
|
test("guard rejects medical deterministic claims outright", () => {
|
|
const packet = buildPacket();
|
|
const document = assembleReportDocument({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
generatedAt: "2026-08-06T00:00:00.000Z",
|
|
packet,
|
|
agentOutput: agentOutput({
|
|
thematicNarrative: [
|
|
{
|
|
id: "career",
|
|
title: "事业",
|
|
narrative: "你一定会患上心脏病。",
|
|
actions: [],
|
|
caveats: [],
|
|
claimStatus: "single_system_inference",
|
|
evidenceRefs: ["ev-audit-1"],
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
const guarded = applyReportGuard(document, packet);
|
|
assert.equal(guarded.ok, false);
|
|
if (!guarded.ok) assert.match(guarded.reason, /deterministic_medical_claim/);
|
|
});
|
|
|
|
test("guard rejects investment deterministic claims hidden in actions", () => {
|
|
const packet = buildPacket();
|
|
const document = assembleReportDocument({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
generatedAt: "2026-08-06T00:00:00.000Z",
|
|
packet,
|
|
agentOutput: agentOutput({
|
|
thematicNarrative: [
|
|
{
|
|
id: "wealth",
|
|
title: "财富",
|
|
narrative: "财富结构稳定。",
|
|
actions: ["买入股票必然大涨"],
|
|
caveats: [],
|
|
claimStatus: "single_system_inference",
|
|
evidenceRefs: ["ev-audit-1"],
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
const guarded = applyReportGuard(document, packet);
|
|
assert.equal(guarded.ok, false);
|
|
if (!guarded.ok) assert.match(guarded.reason, /deterministic_investment_claim/);
|
|
});
|
|
|
|
test("guard forces blocked when every evidence ref is blocked", () => {
|
|
const workflow = pythonStyleChartPayload() as Record<string, unknown>;
|
|
const consumer = workflow.consumer_context as Record<string, unknown>;
|
|
consumer.hard_blockers = ["Narayana"];
|
|
consumer.available_layers = ["D1", "Vimshottari"];
|
|
const packet = buildPacket({ workflow });
|
|
const blockedRef = packet.evidenceRefs.find((ref) => ref.status === "blocked");
|
|
assert.ok(blockedRef);
|
|
const document = assembleReportDocument({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
generatedAt: "2026-08-06T00:00:00.000Z",
|
|
packet,
|
|
agentOutput: agentOutput({
|
|
thematicNarrative: [
|
|
{
|
|
id: "timing",
|
|
title: "时机",
|
|
narrative: "该部分仅保留方向性说明。",
|
|
actions: [],
|
|
caveats: [],
|
|
claimStatus: "single_system_inference",
|
|
evidenceRefs: [blockedRef.id],
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
const guarded = applyReportGuard(document, packet);
|
|
assert.equal(guarded.ok, true);
|
|
if (!guarded.ok) return;
|
|
const section = (guarded.document as unknown as { thematicNarrative: { id: string; claimStatus: string }[] })
|
|
.thematicNarrative[0];
|
|
assert.equal(section.claimStatus, "blocked");
|
|
});
|
|
|
|
test("guard redacts timing from the summary and blocks the report-level status", () => {
|
|
const packet = buildPacket();
|
|
const timingBlockedPacket = { ...packet, answerPolicy: { ...packet.answerPolicy, canAnswerPreciseTiming: false } };
|
|
const document = assembleReportDocument({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
generatedAt: "2026-08-06T00:00:00.000Z",
|
|
packet: timingBlockedPacket,
|
|
agentOutput: agentOutput({
|
|
executiveSummary: {
|
|
headline: "综合盘面以事业发展为主线",
|
|
summary: "明年3月将迎来关键转折,整体结构稳定。",
|
|
priorities: ["先聚焦职业方向"],
|
|
},
|
|
}),
|
|
});
|
|
const guarded = applyReportGuard(document, timingBlockedPacket);
|
|
assert.equal(guarded.ok, true);
|
|
if (!guarded.ok) return;
|
|
const summary = (guarded.document as unknown as { executiveSummary: { summary: string; overallClaimStatus: string } })
|
|
.executiveSummary;
|
|
assert.doesNotMatch(summary.summary, /明年3月/);
|
|
assert.equal(summary.overallClaimStatus, "blocked");
|
|
});
|
|
|
|
test("findForbiddenDeterministicClaims and redaction are deterministic", () => {
|
|
assert.ok(findForbiddenDeterministicClaims("2027年3月会发生转折").some((claim) => claim.domain === "timing"));
|
|
assert.ok(findForbiddenDeterministicClaims("投资必然赚钱").some((claim) => claim.domain === "investment"));
|
|
assert.equal(findForbiddenDeterministicClaims("方向性判断稳定").length, 0);
|
|
const redacted = redactDeterministicSentences("方向稳定。2027年3月转折。", PRECISE_TIMING_PATTERNS);
|
|
assert.equal(redacted.removedCount, 1);
|
|
assert.doesNotMatch(redacted.text, /2027年3月/);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Generation pipeline (fake agent, real canonical parse)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function fakeAgent(output: PersonalReportAgentOutput, calls: { count: number }): ReportAgentPort {
|
|
return {
|
|
modelId: "test-model",
|
|
async generate() {
|
|
calls.count += 1;
|
|
return output;
|
|
},
|
|
};
|
|
}
|
|
|
|
test("generatePersonalReport returns a ready document that passes the server parse", async () => {
|
|
const packet = buildPacket();
|
|
const calls = { count: 0 };
|
|
const result = await generatePersonalReport({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
packet,
|
|
agent: fakeAgent(agentOutput(), calls),
|
|
now: () => new Date("2026-08-06T00:00:00.000Z"),
|
|
});
|
|
assert.equal(calls.count, 1);
|
|
assert.equal(result.status, "ready");
|
|
if (result.status !== "ready") return;
|
|
assert.match(result.evidenceHash, /^[0-9a-f]{64}$/);
|
|
const reparsed = safeParseServerReportDocument(result.document);
|
|
assert.equal(reparsed.ok, true);
|
|
});
|
|
|
|
test("generatePersonalReport fails with report_guard_rejected on guard rejection", async () => {
|
|
const packet = buildPacket();
|
|
const result = await generatePersonalReport({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
packet,
|
|
agent: fakeAgent(agentOutput({
|
|
thematicNarrative: [
|
|
{
|
|
id: "career",
|
|
title: "事业",
|
|
narrative: "你必定会胜诉。",
|
|
actions: [],
|
|
caveats: [],
|
|
claimStatus: "single_system_inference",
|
|
evidenceRefs: ["ev-audit-1"],
|
|
},
|
|
],
|
|
}), { count: 0 }),
|
|
});
|
|
assert.deepEqual(result, { status: "failed", failureCode: "report_guard_rejected" });
|
|
});
|
|
|
|
test("generatePersonalReport fails with report_schema_invalid when the final parse rejects", async () => {
|
|
const workflow = pythonStyleChartPayload() as Record<string, unknown>;
|
|
const consumer = workflow.consumer_context as Record<string, unknown>;
|
|
consumer.hard_blockers = ["Narayana"];
|
|
consumer.available_layers = ["D1", "Vimshottari"];
|
|
const packet = buildPacket({ workflow });
|
|
const blockedRef = packet.evidenceRefs.find((ref) => ref.status === "blocked");
|
|
assert.ok(blockedRef);
|
|
const result = await generatePersonalReport({
|
|
reportId: "22222222-2222-4222-8222-222222222222",
|
|
packet,
|
|
agent: fakeAgent(agentOutput({
|
|
thematicNarrative: [
|
|
{
|
|
id: "timing",
|
|
title: "时机",
|
|
narrative: "该部分必然会成功,结构稳定。",
|
|
actions: [],
|
|
caveats: [],
|
|
claimStatus: "single_system_inference",
|
|
evidenceRefs: [blockedRef.id],
|
|
},
|
|
],
|
|
}), { count: 0 }),
|
|
now: () => new Date("2026-08-06T00:00:00.000Z"),
|
|
});
|
|
// The guard downgrades the section to blocked (all refs blocked); the
|
|
// blocked section still contains the deterministic phrase 必然, so the FINAL
|
|
// canonical server parse rejects it. Guard mutations are always re-validated.
|
|
assert.equal(result.status, "failed");
|
|
if (result.status === "failed") assert.equal(result.failureCode, "report_schema_invalid");
|
|
});
|
|
|
|
test("skill snapshot is the real packaged manifest sha256, never the literal unknown", async () => {
|
|
const snapshot: SkillSnapshot = resolveSkillSnapshot();
|
|
const manifest = upstreamSourceManifest as { skill_sha256?: string };
|
|
assert.match(snapshot.sha256, /^[0-9a-f]{64}$/);
|
|
assert.notEqual(snapshot.sha256, "unknown");
|
|
assert.equal(snapshot.sha256, manifest.skill_sha256);
|
|
const again: SkillSnapshot = resolveSkillSnapshot();
|
|
assert.equal(snapshot.sha256, again.sha256);
|
|
});
|
|
|
|
test("stable codes include the request-conflict mapping", () => {
|
|
assert.equal(REPORT_STABLE_CODES.requestConflict, "report_request_conflict");
|
|
});
|