#5 规则 DELETE + id:暴露 RuleView.id(PK);新增 DELETE /projects/{id}/rules/{rule_id} (项目/规则不存在→404,成功→204,按 (id,project_id) 限定);rule_repo 加 list_for_project/delete;RulesPage 每条加删除(乐观删+回滚+toast)。assemble 侧 RuleView(缓存前缀)不动,列表另立 RuleListItemView。 #7 codex 角色 relations:写侧本已持久化、读端点 _existing_characters 硬编码 []。 加 _relations_from_jsonb 解析 {name,kind,note},CodexPage 渲染关系 chip。 #8 角色入库幂等:SqlCharacterWriteRepo.create 改 (project_id,name) app 层 upsert—— 重复入库改更新而非插入;不加 UNIQUE/迁移(线上已有重复行会让约束迁移失败)。 #1 大纲卷过滤:GET /outline 支持可选 ?volume(无参=全部,向后兼容);OutlineEditor 加「查看:全部/卷N」筛选,与生成目标卷解耦。 H3/#9 文风回炉锚点:StyleDriftSegment 加 text(逐字命中段),style.md 指示审稿输出; 前端按内容锚点定位回炉目标(idx 仅排序),命中失败 → 提示「无法定位该段」而非 静默 no-op。style golden fixture 已重生成。 契约变更已 pnpm gen:api(RuleView.id / DELETE rules / outline ?volume)。无迁移 (alembic 无漂移)。门禁绿:ruff/mypy(210)/alembic/pytest 760 · 前端 tsc/lint/vitest 329。
156 lines
4.5 KiB
TypeScript
156 lines
4.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import type {
|
|
ReviewHistoryItem,
|
|
StyleFingerprintResponse,
|
|
} from "@/lib/api/types";
|
|
import {
|
|
buildLearnRequest,
|
|
buildRefineRequest,
|
|
hasUsableSamples,
|
|
narrowStyleEvent,
|
|
normalizeFingerprint,
|
|
normalizeStyleDrift,
|
|
} from "./style";
|
|
|
|
const reviewItem = (over: Partial<ReviewHistoryItem>): ReviewHistoryItem => ({
|
|
id: "00000000-0000-0000-0000-000000000001",
|
|
project_id: "00000000-0000-0000-0000-000000000002",
|
|
chapter_no: 1,
|
|
...over,
|
|
});
|
|
|
|
describe("normalizeFingerprint", () => {
|
|
it("maps DimensionEntry[] preserving order, evidence per entry", () => {
|
|
const resp: StyleFingerprintResponse = {
|
|
dimensions: [
|
|
{ name: "句长", value: "偏短", evidence: ["他来了。"] },
|
|
{ name: "比喻密度", value: "高", evidence: ["如龙似虎", "若即若离"] },
|
|
],
|
|
version: 2,
|
|
};
|
|
const fp = normalizeFingerprint(resp);
|
|
expect(fp).toEqual({
|
|
version: 2,
|
|
dimensions: [
|
|
{ name: "句长", value: "偏短", evidence: ["他来了。"] },
|
|
{ name: "比喻密度", value: "高", evidence: ["如龙似虎", "若即若离"] },
|
|
],
|
|
});
|
|
});
|
|
|
|
it("defaults missing evidence to empty array", () => {
|
|
const fp = normalizeFingerprint({
|
|
dimensions: [
|
|
{ name: "节奏", value: "5", evidence: ["x"] },
|
|
{ name: "视角", value: "true" },
|
|
],
|
|
version: 1,
|
|
});
|
|
expect(fp?.dimensions).toEqual([
|
|
{ name: "节奏", value: "5", evidence: ["x"] },
|
|
{ name: "视角", value: "true", evidence: [] },
|
|
]);
|
|
});
|
|
|
|
it("returns null when fingerprint is undefined", () => {
|
|
expect(normalizeFingerprint(undefined)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("normalizeStyleDrift", () => {
|
|
it("tightens style dict into report, carrying text anchor, filtering bad segments", () => {
|
|
const out = normalizeStyleDrift(
|
|
reviewItem({
|
|
style: {
|
|
score: 87,
|
|
segments: [
|
|
{ idx: 3, text: "他乘坐迈巴赫扬长而去。", score: 60, label: "口语化" },
|
|
{ idx: 5, score: 72 },
|
|
"junk",
|
|
],
|
|
},
|
|
}),
|
|
);
|
|
expect(out).toEqual({
|
|
score: 87,
|
|
segments: [
|
|
{ idx: 3, text: "他乘坐迈巴赫扬长而去。", score: 60, label: "口语化" },
|
|
{ idx: 5, text: "", score: 72, label: null },
|
|
],
|
|
});
|
|
});
|
|
|
|
it("defaults missing segment text to empty string", () => {
|
|
const out = normalizeStyleDrift(
|
|
reviewItem({ style: { score: 80, segments: [{ idx: 0, score: 50 }] } }),
|
|
);
|
|
expect(out?.segments[0]).toEqual({
|
|
idx: 0,
|
|
text: "",
|
|
score: 50,
|
|
label: null,
|
|
});
|
|
});
|
|
|
|
it("defaults score to 100 (degrade态) and returns null when missing", () => {
|
|
expect(normalizeStyleDrift(reviewItem({ style: {} }))).toEqual({
|
|
score: 100,
|
|
segments: [],
|
|
});
|
|
expect(normalizeStyleDrift(reviewItem({ style: null }))).toBeNull();
|
|
expect(normalizeStyleDrift(undefined)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("narrowStyleEvent", () => {
|
|
it("narrows SSE style event data with text anchor + label fallback", () => {
|
|
expect(
|
|
narrowStyleEvent({
|
|
score: 90,
|
|
segments: [{ idx: 1, text: "原文锚", score: 50 }],
|
|
}),
|
|
).toEqual({
|
|
score: 90,
|
|
segments: [{ idx: 1, text: "原文锚", score: 50, label: null }],
|
|
});
|
|
expect(
|
|
narrowStyleEvent({ score: 90, segments: [{ idx: 1, score: 50 }] }),
|
|
).toEqual({
|
|
score: 90,
|
|
segments: [{ idx: 1, text: "", score: 50, label: null }],
|
|
});
|
|
});
|
|
|
|
it("returns degrade态 for non-object data", () => {
|
|
expect(narrowStyleEvent(null)).toEqual({ score: 100, segments: [] });
|
|
});
|
|
});
|
|
|
|
describe("buildLearnRequest", () => {
|
|
it("trims and drops empty samples; passes mode through", () => {
|
|
expect(buildLearnRequest([" 甲 ", "", "乙"], "update")).toEqual({
|
|
samples: ["甲", "乙"],
|
|
mode: "update",
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("hasUsableSamples", () => {
|
|
it("true only when at least one non-blank sample", () => {
|
|
expect(hasUsableSamples(["", " "])).toBe(false);
|
|
expect(hasUsableSamples(["", "正文"])).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("buildRefineRequest", () => {
|
|
it("trims segment and omits empty instruction", () => {
|
|
expect(buildRefineRequest(" 这一段 ")).toEqual({ segment: "这一段" });
|
|
expect(buildRefineRequest("段", " ")).toEqual({ segment: "段" });
|
|
expect(buildRefineRequest("段", " 更紧凑 ")).toEqual({
|
|
segment: "段",
|
|
instruction: "更紧凑",
|
|
});
|
|
});
|
|
});
|