281 lines
14 KiB
TypeScript
281 lines
14 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import test from "node:test";
|
|
|
|
const component = readFileSync(
|
|
new URL("../src/components/conversational-birth-time-rectification.tsx", import.meta.url),
|
|
"utf8",
|
|
);
|
|
const chat = readFileSync(
|
|
new URL("../src/components/rectification-agentic-chat.tsx", import.meta.url),
|
|
"utf8",
|
|
);
|
|
const route = readFileSync(
|
|
new URL("../src/app/api/rectification/agent/route.ts", import.meta.url),
|
|
"utf8",
|
|
);
|
|
const page = readFileSync(new URL("../src/app/page.tsx", import.meta.url), "utf8");
|
|
const agent = readFileSync(
|
|
new URL("../src/mastra/agentic-rectification.ts", import.meta.url),
|
|
"utf8",
|
|
);
|
|
const styles = readFileSync(new URL("../src/app/globals.css", import.meta.url), "utf8");
|
|
|
|
test("birth-time rectification entry mounts the V9 case-ref chat", () => {
|
|
assert.match(component, /return <RectificationAgenticChat \{\.\.\.props\} \/>/);
|
|
assert.match(component, /caseId: string;/);
|
|
assert.match(component, /readonly: boolean;/);
|
|
assert.match(component, /shouldStartOpening: boolean;/);
|
|
assert.match(component, /initialTurns:/);
|
|
});
|
|
|
|
test("persisted rectification turns hydrate after the async Case refresh", () => {
|
|
assert.match(chat, /function messagesFromTurns\(initialTurns:/);
|
|
assert.match(chat, /useState<RenderMessage\[\]>\(\(\) => messagesFromTurns\(initialTurns\)\)/);
|
|
assert.match(page, /key=\{`\$\{rectificationSessionId\}-\$\{rectificationCaseId\}-\$\{rectificationTurns\.at\(-1\)\?\.id \?\? "loading"\}`\}/);
|
|
assert.match(page, /methods: Array\.isArray\(\(turn\.receipt as \{ methods\?: unknown \}\)\.methods\)/);
|
|
assert.match(component, /methods\?: readonly string\[\]/);
|
|
});
|
|
|
|
test("opening is server-owned: shouldStartOpening drives the first turn, never client history", () => {
|
|
assert.doesNotMatch(chat, /initialMessages\.length > 0 \|\| openingStarted/);
|
|
assert.doesNotMatch(chat, /agenticOpeningInstruction|用户刚进入生时校正会话/);
|
|
assert.match(chat, /shouldStartOpening/);
|
|
assert.match(chat, /if \(readonly \|\| openingStarted\.current \|\| !shouldStartOpening\) return/);
|
|
assert.match(chat, /void send\("opening", ""\)/);
|
|
assert.match(route, /action: z\.enum\(\["opening", "message", "read_only"\]\)/);
|
|
assert.match(page, /shouldStartOpening=\{rectificationShouldStartOpening\}/);
|
|
assert.match(page, /setRectificationShouldStartOpening\(opened\.shouldStartOpening\)/);
|
|
});
|
|
|
|
test("incomplete profiles stay in the shared onboarding flow before any open request", () => {
|
|
const opening = page.slice(
|
|
page.indexOf("async function openRectificationCase"),
|
|
page.indexOf("async function openRectificationFromHomepage"),
|
|
);
|
|
assert.match(opening, /const missingStep = missingProfileStep\(profile\)/);
|
|
assert.match(opening, /setOnboardingStep\(missingStep\)/);
|
|
assert.ok(
|
|
opening.indexOf("const missingStep = missingProfileStep(profile)")
|
|
< opening.indexOf("/api/rectification/cases/open"),
|
|
);
|
|
assert.match(chat, /payload\?\.code === "profile_incomplete"/);
|
|
});
|
|
|
|
test("server profile failures pause automatic rectification resume", () => {
|
|
const resumeEffect = page.slice(
|
|
page.indexOf('useEffect(() => {\n if (!hydrated'),
|
|
page.indexOf('useEffect(() => {\n if (!hydrated || !accountId'),
|
|
);
|
|
const incompleteHandler = page.slice(
|
|
page.indexOf("function handleRectificationProfileIncomplete"),
|
|
page.indexOf("async function draftSynastryQuestionFromChart"),
|
|
);
|
|
assert.match(resumeEffect, /\|\| rectificationError\) return/);
|
|
assert.match(incompleteHandler, /setRectificationError\("profile_incomplete"\)/);
|
|
assert.ok(
|
|
incompleteHandler.indexOf('setRectificationError("profile_incomplete")')
|
|
< incompleteHandler.indexOf("setRectificationSessionId(null)"),
|
|
);
|
|
});
|
|
|
|
test("account rehydration normalizes persisted ISO birth dates before completeness checks", () => {
|
|
const profileReader = page.slice(
|
|
page.indexOf("function readProfile"),
|
|
page.indexOf("function readSessions"),
|
|
);
|
|
assert.match(profileReader, /const date = normalizePersistedBirthDate\(/);
|
|
});
|
|
|
|
test("candidate acceptance refreshes the profile result without overwriting an open draft", () => {
|
|
const refresh = page.slice(
|
|
page.indexOf("async function refreshAccount"),
|
|
page.indexOf("function updateSession"),
|
|
);
|
|
assert.match(refresh, /const nextProfile = readProfile\(latest\.profile\)/);
|
|
assert.match(refresh, /setProfile\(nextProfile\)/);
|
|
assert.doesNotMatch(refresh, /setProfileDraft/);
|
|
});
|
|
|
|
test("agent tool calls never end silently; the runner owns completion and failure", () => {
|
|
assert.doesNotMatch(route, /agenticRectificationMaxSteps = 8/);
|
|
assert.match(agent, /RECTIFICATION_AGENT_STEP_BUDGETS/);
|
|
assert.match(agent, /RECTIFICATION_AGENT_HARD_STEP_LIMIT/);
|
|
assert.match(route, /send\(\{ type: "done", emitted: true \}\)/);
|
|
assert.doesNotMatch(route, /send\(\{ type: "done", emitted: false \}\)/);
|
|
});
|
|
|
|
test("persisted turns survive remounts; duplicate openings are suppressed by the server", () => {
|
|
assert.match(chat, /initialTurns/);
|
|
assert.match(chat, /const openingStarted = useRef\(false\)/);
|
|
assert.match(page, /key=\{`\$\{rectificationSessionId\}-\$\{rectificationCaseId\}-\$\{rectificationTurns\.at\(-1\)\?\.id \?\? "loading"\}`\}/);
|
|
assert.match(page, /initialTurns=\{rectificationTurns\}/);
|
|
assert.match(page, /onMessagesChange=\{handleRectificationMessagesChange\}/);
|
|
});
|
|
|
|
test("the agent route verifies the exact Case/Session binding before any turn", () => {
|
|
assert.match(route, /caseId: z\.string\(\)\.uuid\(\)/);
|
|
assert.match(route, /sessionId: z\.string\(\)\.uuid\(\)/);
|
|
assert.match(route, /requestId: z\.string\(\)\.uuid\(\)/);
|
|
assert.match(route, /boundSessionId !== sessionId/);
|
|
assert.match(route, /chatSession\.agentic_rectification_case_id !== caseId/);
|
|
assert.doesNotMatch(route, /conversation\.action === "opening" && persistedMessages\.length > 0/);
|
|
assert.doesNotMatch(route, /\.update\(\{ messages: nextMessages, updated_at:/);
|
|
});
|
|
|
|
test("candidate results restore through the durable Candidate Snapshot API, never agent text", () => {
|
|
assert.match(chat, /\/api\/rectification\/cases\/\$\{encodeURIComponent\(caseId\)\}\?sessionId=/);
|
|
assert.doesNotMatch(chat, /savedSentinel|AYANAM_RECTIFICATION_SAVED|hidden block/);
|
|
assert.doesNotMatch(chat, /<!--AYANAM_SUGGESTIONS/);
|
|
});
|
|
|
|
test("candidate acceptance is non-billable and happens through the durable case endpoint", () => {
|
|
assert.match(chat, /\/candidates\/accept/);
|
|
assert.match(chat, /resultId: candidateResult\.resultId/);
|
|
assert.doesNotMatch(chat, /action: "accept_candidate"/);
|
|
const acceptRoute = readFileSync(
|
|
new URL("../src/app/api/rectification/cases/[caseId]/candidates/accept/route.ts", import.meta.url),
|
|
"utf8",
|
|
);
|
|
assert.match(acceptRoute, /accept_agentic_rectification_candidate_for_case/);
|
|
assert.doesNotMatch(acceptRoute, /authorizeUsage|completeUsage|begin_consultation_credit/);
|
|
});
|
|
|
|
test("usage completes or releases without hiding settlement failures", () => {
|
|
const run = readFileSync(
|
|
new URL("../src/lib/rectification-agentic/v9/agent-run.ts", import.meta.url),
|
|
"utf8",
|
|
);
|
|
assert.match(run, /billing\.complete\(/);
|
|
assert.match(run, /billing\.release\(/);
|
|
assert.match(run, /usage_settlement_failed/);
|
|
assert.match(route, /featureKey: "rectification"/);
|
|
assert.match(route, /rectification:case:\$\{caseId\}/);
|
|
});
|
|
|
|
test("opening and message retries reuse the caller-owned request id as the usage event key", () => {
|
|
assert.match(route, /requestId,\s*action\s*\} = parsed\.data/);
|
|
assert.match(route, /eventKey: requestId/);
|
|
assert.doesNotMatch(route, /eventKey:\s*(?:globalThis\.)?crypto\.randomUUID\(\)/);
|
|
});
|
|
|
|
test("rectification uses one case-level entitlement and the session-pinned model version", () => {
|
|
assert.match(route, /select\("id,messages,session_type,model_id,model_config_version,agentic_rectification_case_id"\)/);
|
|
assert.match(route, /resolveSessionLanguageModel\(\s*chatSession\.model_id,\s*chatSession\.model_config_version,?\s*\)/);
|
|
assert.match(route, /const billingRequestPrefix = `rectification:case:\$\{caseId\}`/);
|
|
assert.match(route, /requestId: billingRequestId/);
|
|
assert.match(route, /modelConfigVersion: selectedModel\.configVersion/);
|
|
assert.doesNotMatch(route, /loadLanguageModelCatalog|resolveLanguageModelFromCatalog|\bresolveLanguageModel\(|\bdefaultLanguageModel\(/);
|
|
});
|
|
|
|
test("Agentic rectification scrolls the conversation container as streamed messages grow", () => {
|
|
assert.match(chat, /const conversation = useRef<HTMLElement>\(null\)/);
|
|
assert.match(chat, /<section ref=\{conversation\} className="conversation"/);
|
|
assert.match(chat, /const container = conversation\.current/);
|
|
assert.match(chat, /top: container\.scrollHeight/);
|
|
assert.match(chat, /\}, \[busy, error, messages, savedTime\]\);/);
|
|
assert.doesNotMatch(chat, /conversationEnd|scrollIntoView/);
|
|
});
|
|
|
|
test("rectification activity renders real completed work above the Agent message", () => {
|
|
const activityHelper = chat.slice(
|
|
chat.indexOf("function activityFromReceipt"),
|
|
chat.indexOf("export function RectificationAgenticChat"),
|
|
);
|
|
assert.match(activityHelper, /receipt\.tools/);
|
|
assert.doesNotMatch(activityHelper, /receipt\.phases/);
|
|
assert.match(activityHelper, /filter\(isPublicRectificationTool\)/);
|
|
assert.match(activityHelper, /TOOL_LABELS\[tool\]/);
|
|
assert.match(activityHelper, /receipt\.methods/);
|
|
assert.match(activityHelper, /filter\(isPublicRectificationMethod\)/);
|
|
assert.match(activityHelper, /METHOD_LABELS\[method\]/);
|
|
assert.match(chat, /"rectification-read-case": "读取校正记录"/);
|
|
|
|
const messageRender = chat.slice(
|
|
chat.indexOf('{messages.map((message) => ('),
|
|
chat.indexOf('{candidateResult?.selectionAllowed'),
|
|
);
|
|
assert.ok(messageRender.indexOf('className="rectification-activity"') < messageRender.indexOf("<ChatMessageRow message={message} />"));
|
|
for (const genericCopy of ["开始本轮执行", "正在加载专用方法", "专用方法已加载", "本轮完成"]) {
|
|
assert.doesNotMatch(chat, new RegExp(genericCopy));
|
|
}
|
|
|
|
const activityStyles = styles.slice(styles.indexOf(".rectification-message-wrap"));
|
|
assert.match(activityStyles, /\.rectification-activity \{[\s\S]*border:/);
|
|
assert.match(activityStyles, /\.rectification-activity \{[\s\S]*border-radius:/);
|
|
assert.match(activityStyles, /\.rectification-activity \{[\s\S]*background:/);
|
|
assert.match(activityStyles, /\.rectification-activity \{[\s\S]*color:/);
|
|
assert.match(activityStyles, /\.rectification-activity summary:focus-visible/);
|
|
assert.doesNotMatch(activityStyles, /gradient|glow/i);
|
|
});
|
|
|
|
test("candidate state renders from the snapshot API and never from sentinels", () => {
|
|
assert.match(chat, /当前可能的出生时间/);
|
|
assert.match(chat, /可以先采用一个作为当前排盘时间,也可以继续补充事件/);
|
|
assert.match(chat, /相对支持度不是统计概率/);
|
|
assert.match(chat, /改选为此时间/);
|
|
assert.doesNotMatch(chat, /disabled=\{Boolean\(candidateResult\.selectedTime\)/);
|
|
assert.match(styles, /\.rectification-candidate-list \{[\s\S]*grid-template-columns: repeat\(3, minmax\(0, 1fr\)\)/);
|
|
assert.match(styles, /\.rectification-candidate-list \{[\s\S]*min-width: 0/);
|
|
assert.match(chat, /当前推荐/);
|
|
assert.match(chat, /已采用/);
|
|
});
|
|
|
|
test("the Agent prompt cannot offer candidates while asking for more evidence", () => {
|
|
// The hard boundary lives in the prompt; no tool input carries an
|
|
// offer_selection boolean anymore.
|
|
assert.match(agent, /不得在同一回复里一边要求继续补证据、一边提供候选采用/);
|
|
const tools = readFileSync(
|
|
new URL("../src/mastra/rectification-v9-tools.ts", import.meta.url),
|
|
"utf8",
|
|
);
|
|
assert.doesNotMatch(tools, /offer_selection/);
|
|
});
|
|
|
|
test("stream failures remove empty assistant placeholders", () => {
|
|
assert.match(chat, /streamFailed = true/);
|
|
assert.match(chat, /const succeeded = completed && !streamFailed && Boolean\(parsed\.text\)/);
|
|
assert.match(chat, /current\.filter\(\(message\) => message\.renderKey !== assistantRenderKey\)/);
|
|
assert.match(chat, /if \(succeeded\)/);
|
|
});
|
|
|
|
test("rectification sessions are opened by the server Case API; the browser never creates them", () => {
|
|
assert.match(page, /\/api\/rectification\/cases\/open/);
|
|
assert.match(page, /openRectificationRequestBody\(intent, exactSessionId\)/);
|
|
assert.match(page, /openResponseFromPayload\(payload\)/);
|
|
assert.doesNotMatch(page, /sessions\.find\(\(session\) => session\.sessionType === "birth_time_rectification"\)/);
|
|
assert.doesNotMatch(page, /hasRectificationSession/);
|
|
assert.match(page, /rectificationCardAction = resolveRectificationEntryAction/);
|
|
});
|
|
|
|
test("sidebar selection passes the exact sessionId to the server open API", () => {
|
|
const selectSession = page.slice(
|
|
page.indexOf("function selectSession("),
|
|
page.indexOf("async function selectSessionModel", page.indexOf("function selectSession(")),
|
|
);
|
|
assert.match(selectSession, /nextSession\?\.sessionType === "birth_time_rectification"/);
|
|
assert.match(selectSession, /void openRectificationSession\(nextSession\.id\)/);
|
|
assert.doesNotMatch(selectSession, /resumeRectificationSession\.current\(nextSession\)/);
|
|
});
|
|
|
|
test("homepage CTA is server-driven from the entry summary, not the session list", () => {
|
|
const entryLib = readFileSync(
|
|
new URL("../src/lib/rectification-entry.ts", import.meta.url),
|
|
"utf8",
|
|
);
|
|
assert.doesNotMatch(page, /hasRectificationSession/);
|
|
assert.doesNotMatch(page, /sessions\.some\([\s\S]{0,120}birth_time_rectification/);
|
|
assert.match(page, /resolveRectificationEntryAction/);
|
|
assert.match(page, /rectificationCardAction === "resume"/);
|
|
assert.match(page, /rectificationCardAction === "restart"/);
|
|
assert.match(entryLib, /开始生时校正/);
|
|
assert.match(entryLib, /继续上次校正/);
|
|
assert.match(entryLib, /再次校正/);
|
|
});
|
|
|
|
test("readonly terminal sessions expose a 再次校正 action instead of appending", () => {
|
|
assert.match(chat, /readonly/);
|
|
assert.match(chat, /再次校正/);
|
|
assert.match(chat, /onRestart/);
|
|
});
|