From 5665cc9a16562128fa91775185bf7ee6098c9984 Mon Sep 17 00:00:00 2001 From: Yaojia Wang Date: Wed, 8 Jul 2026 11:06:30 +0200 Subject: [PATCH] =?UTF-8?q?fix(frontend):=20errorCode=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E8=87=AA=20generation/cards=20=E5=8E=BB=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=EF=BC=88CR-H10=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/lib/style/errorCode-dedup.test.ts | 19 +++++++++++++++++++ apps/web/lib/style/useRefine.ts | 7 +------ apps/web/lib/style/useStyleLearn.ts | 7 +------ 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 apps/web/lib/style/errorCode-dedup.test.ts diff --git a/apps/web/lib/style/errorCode-dedup.test.ts b/apps/web/lib/style/errorCode-dedup.test.ts new file mode 100644 index 0000000..3f0ea89 --- /dev/null +++ b/apps/web/lib/style/errorCode-dedup.test.ts @@ -0,0 +1,19 @@ +import { readFileSync } from "node:fs"; + +import { describe, expect, it } from "vitest"; + +// CR-H10 回归守卫:errorCode 只应有一份权威实现(lib/generation/cards), +// style 侧两个 hook 不得再各自本地定义 `function errorCode(`,须统一 import。 +describe("errorCode 去重(CR-H10)", () => { + it.each(["useRefine.ts", "useStyleLearn.ts"])( + "%s 不再本地定义 errorCode,且从 generation/cards 引入", + (file) => { + const src = readFileSync(new URL(`./${file}`, import.meta.url), "utf-8"); + + expect(src).not.toMatch(/function\s+errorCode\s*\(/); + expect(src).toMatch( + /import\s*\{[^}]*\berrorCode\b[^}]*\}\s*from\s*["']@\/lib\/generation\/cards["']/, + ); + }, + ); +}); diff --git a/apps/web/lib/style/useRefine.ts b/apps/web/lib/style/useRefine.ts index 3701f99..fd0f159 100644 --- a/apps/web/lib/style/useRefine.ts +++ b/apps/web/lib/style/useRefine.ts @@ -4,6 +4,7 @@ import { useCallback, useState } from "react"; import { api } from "@/lib/api/client"; import { useToast } from "@/components/Toast"; +import { errorCode } from "@/lib/generation/cards"; import { buildRefineRequest } from "./style"; export type RefineStatus = "idle" | "refining" | "done" | "error"; @@ -26,12 +27,6 @@ export interface UseRefine { reset: () => void; } -function errorCode(error: unknown): string | undefined { - if (typeof error !== "object" || error === null) return undefined; - const env = error as { error?: { code?: unknown } }; - return typeof env.error?.code === "string" ? env.error.code : undefined; -} - // 回炉(POST .../refine):同步返回新旧 diff,不写库(不变量#3)。 // 503 LLM_UNAVAILABLE(无凭据)优雅提示去设置;其余失败 toast。 export function useRefine(): UseRefine { diff --git a/apps/web/lib/style/useStyleLearn.ts b/apps/web/lib/style/useStyleLearn.ts index 767bde1..2d3ee68 100644 --- a/apps/web/lib/style/useStyleLearn.ts +++ b/apps/web/lib/style/useStyleLearn.ts @@ -6,6 +6,7 @@ import { api } from "@/lib/api/client"; import { useToast } from "@/components/Toast"; import { useJobPoll } from "@/lib/jobs/useJobPoll"; import { styleLearnResult } from "@/lib/jobs/job"; +import { errorCode } from "@/lib/generation/cards"; import { buildLearnRequest, normalizeFingerprint, @@ -27,12 +28,6 @@ export interface UseStyleLearn { ) => Promise; } -function errorCode(error: unknown): string | undefined { - if (typeof error !== "object" || error === null) return undefined; - const env = error as { error?: { code?: unknown } }; - return typeof env.error?.code === "string" ? env.error.code : undefined; -} - // 学文风编排:受理(202 job_id)→ useJobPoll 轮询 → done 拉 GET /style 展示指纹。 export function useStyleLearn(initial: Fingerprint | null): UseStyleLearn { const [fingerprint, setFingerprint] = useState(initial);