fix(frontend): errorCode 统一自 generation/cards 去重复定义(CR-H10)
This commit is contained in:
19
apps/web/lib/style/errorCode-dedup.test.ts
Normal file
19
apps/web/lib/style/errorCode-dedup.test.ts
Normal 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["']/,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -4,6 +4,7 @@ import { useCallback, useState } from "react";
|
|||||||
|
|
||||||
import { api } from "@/lib/api/client";
|
import { api } from "@/lib/api/client";
|
||||||
import { useToast } from "@/components/Toast";
|
import { useToast } from "@/components/Toast";
|
||||||
|
import { errorCode } from "@/lib/generation/cards";
|
||||||
import { buildRefineRequest } from "./style";
|
import { buildRefineRequest } from "./style";
|
||||||
|
|
||||||
export type RefineStatus = "idle" | "refining" | "done" | "error";
|
export type RefineStatus = "idle" | "refining" | "done" | "error";
|
||||||
@@ -26,12 +27,6 @@ export interface UseRefine {
|
|||||||
reset: () => void;
|
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)。
|
// 回炉(POST .../refine):同步返回新旧 diff,不写库(不变量#3)。
|
||||||
// 503 LLM_UNAVAILABLE(无凭据)优雅提示去设置;其余失败 toast。
|
// 503 LLM_UNAVAILABLE(无凭据)优雅提示去设置;其余失败 toast。
|
||||||
export function useRefine(): UseRefine {
|
export function useRefine(): UseRefine {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { api } from "@/lib/api/client";
|
|||||||
import { useToast } from "@/components/Toast";
|
import { useToast } from "@/components/Toast";
|
||||||
import { useJobPoll } from "@/lib/jobs/useJobPoll";
|
import { useJobPoll } from "@/lib/jobs/useJobPoll";
|
||||||
import { styleLearnResult } from "@/lib/jobs/job";
|
import { styleLearnResult } from "@/lib/jobs/job";
|
||||||
|
import { errorCode } from "@/lib/generation/cards";
|
||||||
import {
|
import {
|
||||||
buildLearnRequest,
|
buildLearnRequest,
|
||||||
normalizeFingerprint,
|
normalizeFingerprint,
|
||||||
@@ -27,12 +28,6 @@ export interface UseStyleLearn {
|
|||||||
) => Promise<boolean>;
|
) => 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 展示指纹。
|
// 学文风编排:受理(202 job_id)→ useJobPoll 轮询 → done 拉 GET /style 展示指纹。
|
||||||
export function useStyleLearn(initial: Fingerprint | null): UseStyleLearn {
|
export function useStyleLearn(initial: Fingerprint | null): UseStyleLearn {
|
||||||
const [fingerprint, setFingerprint] = useState<Fingerprint | null>(initial);
|
const [fingerprint, setFingerprint] = useState<Fingerprint | null>(initial);
|
||||||
|
|||||||
Reference in New Issue
Block a user