refactor(frontend): clarify toVM/PROCEED 提取到 clarify.ts 共享(WFW-9 M2)

This commit is contained in:
Yaojia Wang
2026-07-08 11:54:05 +02:00
parent 9b32743606
commit 4b1088a179
3 changed files with 97 additions and 28 deletions

View File

@@ -1,8 +1,10 @@
import { describe, expect, it } from "vitest";
import {
PROCEED,
foldClarifications,
needsClarifyGate,
toVM,
type ClarifyAnswer,
} from "./clarify";
@@ -67,6 +69,70 @@ describe("foldClarifications", () => {
});
});
describe("toVM", () => {
it("snake_case ClarifyDecision → camelCase VM逐字段映射", () => {
const vm = toVM({
need_clarification: true,
questions: [
{
question: "想更冷峻还是更抒情?",
options: [
{ label: "冷峻", value: "改得更冷峻克制" },
{ label: "抒情", value: "改得更抒情" },
],
allow_free_text: true,
},
],
verification: "我这样理解对吗",
});
expect(vm).toEqual({
needClarification: true,
questions: [
{
question: "想更冷峻还是更抒情?",
options: [
{ label: "冷峻", value: "改得更冷峻克制" },
{ label: "抒情", value: "改得更抒情" },
],
allowFreeText: true,
},
],
verification: "我这样理解对吗",
});
});
it("字段缺省时取安全默认(空/false/undefined不信任外部数据", () => {
const vm = toVM({});
expect(vm).toEqual({
needClarification: false,
questions: [],
verification: undefined,
});
});
it("questions/options 内字段缺省时逐项补默认", () => {
const vm = toVM({
need_clarification: true,
questions: [{ question: undefined, options: [{}], allow_free_text: undefined }],
verification: null,
});
expect(vm).toEqual({
needClarification: true,
questions: [
{ question: "", options: [{ label: "", value: "" }], allowFreeText: false },
],
verification: undefined,
});
});
});
describe("PROCEED", () => {
it("放行常量:不反问、无问题", () => {
expect(PROCEED).toEqual({ needClarification: false, questions: [] });
});
});
describe("needsClarifyGate", () => {
it("去空白后长度小于 minChars 时需要预检", () => {
expect(needsClarifyGate("改一下", 8)).toBe(true);