Files
Jyotisha/frontend/tests/model-catalog.test.ts
T
ClaudeandClaude Opus 5.5 30637729ff refactor: remove conversational rectification from the web app (BUG-1644)
Home chat surface, onboarding assessment, rectification cases API, Mastra
rectification tools and admin telemetry are gone; 生时纠正 is the standalone
/rectification page. Old birth_time_rectification sessions are hidden from
lists. Consult offer card links to the page. Copy renamed to 生时纠正.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXozbkyFNqbu8pVEtZ7W
2026-11-06 07:55:04 +08:00

74 lines
4.6 KiB
TypeScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
const catalog = readFileSync(new URL("../src/lib/model-catalog.ts", import.meta.url), "utf8");
const mastraModel = readFileSync(new URL("../src/mastra/model.ts", import.meta.url), "utf8");
const routes = Object.fromEntries(
["reports", "consult"].map((name) => [
name,
readFileSync(new URL(`../src/app/api/${name}/route.ts`, import.meta.url), "utf8"),
]),
);
test("runtime catalog is database-only and reads encrypted provider credentials", () => {
assert.match(catalog, /queryAdminRows<PublishedModelRow>/);
assert.match(catalog, /p\.encrypted_api_key/);
assert.match(catalog, /decryptModelProviderApiKey\(\{ encryptedApiKey: row\.encrypted_api_key \}\)/);
assert.match(catalog, /v\.status='published' and v\.enabled and p\.enabled/);
assert.doesNotMatch(
`${catalog}\n${mastraModel}`,
/LLM_MODELS_JSON|LLM_BASE_URL|LLM_MODEL|LLM_DEFAULT_MODEL_ID|LLM_PROVIDER_ID|MASTRA_MODEL|OPENAI_API_KEY|ANTHROPIC_API_KEY|DEEPSEEK_API_KEY|LLM_API_KEY|MODEL_PROVIDER_[A-Z0-9_]+_API_KEY|secret_ref|secretRef/,
);
});
test("database rows map to native OpenAI, native Anthropic, and pinned compatible Mastra configs", () => {
assert.match(catalog, /providerId: "openai", modelId: row\.provider_model\.replace\(\/\^openai/);
assert.match(catalog, /providerId: "anthropic", modelId: row\.provider_model\.replace\(\/\^anthropic/);
assert.match(catalog, /providerId: row\.provider_code, modelId: row\.provider_model, url:/);
assert.match(catalog, /assertAllowedModelProviderUrl\(row\.base_url \?\? ""\)/);
assert.match(catalog, /apiKey/);
assert.match(catalog, /function publicModel[\s\S]*creditCost: model\.creditCost, isDefault: model\.isDefault/);
assert.doesNotMatch(catalog, /publicModel[\s\S]{0,240}(apiKey|encrypted_api_key|base_url)/);
});
test("catalog cache is short-lived and failure stays fail-closed without env fallback", () => {
assert.match(catalog, /const cacheTtlMs = 15_000/);
assert.match(catalog, /expiresAt: Date\.now\(\) \+ cacheTtlMs/);
assert.match(catalog, /database_model_catalog_unavailable/);
assert.match(catalog, /database_model_catalog_empty/);
assert.match(catalog, /default_model_unavailable/);
assert.match(catalog, /const defaults = models\.filter\(\(model\) => model\.isDefault\)/);
assert.match(catalog, /defaults\.length === 1/);
});
test("session model resolution is async and pins published or retired database versions", () => {
assert.match(catalog, /export async function resolveSessionLanguageModel/);
assert.match(catalog, /c\.model_id=\$1 and v\.version=\$2 and v\.status in \('published','retired'\)/);
// 原值: return await readVersion(modelId, configVersion)
// 新值: const model = await readVersion(modelId, configVersion),只有非空结果写入短缓存
// 原因: 绑版本的解析与目录共用 15 秒上限,失败和空结果不缓存
assert.match(catalog, /const model = await readVersion\(modelId, configVersion\)/);
assert.match(catalog, /if \(model && ttl > 0\) cache\.set\(key, \{ expiresAt: Date\.now\(\) \+ ttl, model \}\)/);
assert.match(routes.consult, /await resolveSessionLanguageModel\(\s*chatSession\.model_id,\s*chatSession\.model_config_version/);
// 原值: route.ts 里 if (!sessionModel) 之后是 status 503
// 新值: turn-prep.ts 里 if (!sessionModel) 返回 modelUnavailable(),正文 status 503
// 原因: 模型不可用仍是开流前的 503,文案随准备步骤一起搬走
const head = readFileSync(new URL("../src/lib/consult/turn-prep.ts", import.meta.url), "utf8");
assert.match(head, /if \(!sessionModel\) return \{ ok: false, response: modelUnavailable\(\) \}/);
assert.match(head, /function modelUnavailable\(\)[\s\S]*status: 503/);
assert.doesNotMatch(routes.consult, /languageModelConfigurationMessage/);
});
test("reports resolve the database catalog before creating agents", () => {
// 原值: 也断言 POST /api/onboarding 经 getOnboardingAgent 读目录。
// 新值: 不再覆盖 onboarding 路由。
// 原因: 建议问题生成接口已删除。
// 原值(2): 也断言 /api/birth-time-guide 经 getBirthTimeGuideAgent 读目录 新值(2): 不再覆盖 原因(2): 2026-11-06 会话式生时纠正与引导接口删除
for (const name of ["reports"] as const) {
assert.match(routes[name], /await loadLanguageModelCatalog\(\)/, `${name} must await the DB catalog`);
assert.match(routes[name], /catalog\.models\.find/, `${name} must select from resolved DB models`);
}
assert.match(routes.reports, /createPersonalReportAgent\(model/);
});