fix(frontend): errorCode 统一自 generation/cards 去重复定义(CR-H10)

This commit is contained in:
Yaojia Wang
2026-07-08 11:06:30 +02:00
parent f4c2c4e8ec
commit 5665cc9a16
3 changed files with 21 additions and 12 deletions

View File

@@ -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["']/,
);
},
);
});

View File

@@ -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 {

View File

@@ -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<boolean>;
}
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<Fingerprint | null>(initial);