fix(web): stale-closure + focus trap + 去边界强转 + a11y/性能打磨 + 类型化客户端

P1-6 useStyleLearn/useKimiOauth 修 stale-closure 依赖。
P1-7 CommandPalette/Drawer 加 focus trap(新 lib/a11y/focusTrap)。
P1-8 SSE 解析与 server.ts 去 as 强转改类型守卫/运行时校验。
P2 删冗余强转、开 noUncheckedIndexedAccess、Toast 清理+稳定 key、列表 key 稳定化、
  rel=noopener、useMemo 大文本、ConflictCard role=group、useInjection 加锁、
  client.test 真断言。
codegen 重生成 schema.d.ts + 消费 DimensionEntry/ReviewConflictView 强类型。
This commit is contained in:
Yaojia Wang
2026-06-21 19:32:49 +02:00
parent 016509c5c6
commit c6651b74b9
36 changed files with 792 additions and 140 deletions

View File

@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import type { ReviewHistoryItem } from "@/lib/api/types";
import type { ReviewConflictView, ReviewHistoryItem } from "@/lib/api/types";
import {
latestReview,
normalizeConflicts,
@@ -9,7 +9,7 @@ import {
} from "./history";
const item = (
conflicts: Record<string, unknown>[],
conflicts: ReviewConflictView[],
): ReviewHistoryItem => ({
id: "00000000-0000-0000-0000-000000000001",
project_id: "00000000-0000-0000-0000-000000000002",
@@ -36,8 +36,10 @@ describe("normalizeConflicts", () => {
]);
});
it("fills safe defaults for missing/wrong-typed fields", () => {
const out = normalizeConflicts(item([{ where: 42, refs: "x" }]));
it("falls back type to 未分类 and tolerates missing refs", () => {
const out = normalizeConflicts(
item([{ type: "", where: "", suggestion: "" }]),
);
expect(out).toEqual([
{ type: "未分类", where: "", refs: [], suggestion: "" },
]);

View File

@@ -18,26 +18,22 @@ function asOptionalString(v: unknown): string | null {
return typeof v === "string" ? v : null;
}
function asStringArray(v: unknown): string[] {
if (!Array.isArray(v)) return [];
return v.filter((x): x is string => typeof x === "string");
}
function asIntArray(v: unknown): number[] {
if (!Array.isArray(v)) return [];
return v.filter((x): x is number => typeof x === "number" && Number.isFinite(x));
}
// 把一条留痕的 conflicts 收紧(缺字段给安全默认;保持顺序=下标身份,对齐冲突 gate
// 把一条留痕的 conflictsReviewConflictView[],已强类型)映射成 ReviewConflict
// 保持顺序=下标身份(对齐冲突 gatetype 空串回退「未分类」refs 容忍缺省。
export function normalizeConflicts(
item: ReviewHistoryItem | undefined,
): ReviewConflict[] {
const raw = item?.conflicts ?? [];
return raw.map((c) => ({
type: asString(c["type"], "未分类"),
where: asString(c["where"]),
refs: asStringArray(c["refs"]),
suggestion: asString(c["suggestion"]),
type: c.type || "未分类",
where: c.where,
refs: c.refs ?? [],
suggestion: c.suggestion,
}));
}

View File

@@ -82,6 +82,49 @@ describe("parseReviewBlock", () => {
expect(parseReviewBlock('event:token\ndata:{"text":"x"}')).toBeNull();
expect(parseReviewBlock("event:conflict\ndata:{not json")).toBeNull();
});
// P1-8逐事件类型守卫——已知事件但 data 形状不符也拒绝(不再强转放行)。
it("returns null when conflict frame lacks required string fields", () => {
expect(
parseReviewBlock('event:conflict\ndata:{"type":"设定违例","refs":[]}'),
).toBeNull();
});
it("returns null when section status is invalid", () => {
expect(
parseReviewBlock('event:section\ndata:{"name":"c","status":"bogus"}'),
).toBeNull();
});
it("drops non-string refs and coerces nullable foreshadow fields", () => {
expect(
parseReviewBlock(
'event:conflict\ndata:{"type":"设定违例","where":"第1段","refs":["第3章",7],"suggestion":"s"}',
),
).toEqual({
event: "conflict",
data: {
type: "设定违例",
where: "第1段",
refs: ["第3章"],
suggestion: "s",
},
});
expect(
parseReviewBlock(
'event:foreshadow\ndata:{"kind":"planted","title":"残图"}',
),
).toEqual({
event: "foreshadow",
data: {
kind: "planted",
code: null,
title: "残图",
where: null,
note: null,
},
});
});
});
describe("ReviewFrameBuffer", () => {

View File

@@ -139,7 +139,125 @@ export function parseReviewBlock(block: string): ReviewSseEvent | null {
} catch {
return null;
}
return { event, data } as ReviewSseEvent;
return narrowReviewEvent(event, data);
}
function isRecord(v: unknown): v is Record<string, unknown> {
return typeof v === "object" && v !== null && !Array.isArray(v);
}
function asStringArray(v: unknown): string[] {
return Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
}
function asNumberArray(v: unknown): number[] {
return Array.isArray(v)
? v.filter((x): x is number => typeof x === "number" && Number.isFinite(x))
: [];
}
function nullableString(v: unknown): string | null {
return typeof v === "string" ? v : null;
}
function asPaceIssues(v: unknown): PaceIssue[] {
if (!Array.isArray(v)) return [];
return v.flatMap((x) =>
isRecord(x) && typeof x.where === "string" && typeof x.reason === "string"
? [{ where: x.where, reason: x.reason }]
: [],
);
}
function asStyleSegments(v: unknown): StyleEvent["data"]["segments"] {
if (!Array.isArray(v)) return [];
return v.flatMap((x) =>
isRecord(x) && typeof x.idx === "number" && typeof x.score === "number"
? [{ idx: x.idx, score: x.score, label: nullableString(x.label) }]
: [],
);
}
// 逐事件类型守卫:按 event 名收窄 data 形状;形状不符返回 null安全跳过
function narrowReviewEvent(event: string, data: unknown): ReviewSseEvent | null {
if (!isRecord(data)) return null;
switch (event) {
case "section":
return typeof data.name === "string" && isSectionStatus(data.status)
? { event: "section", data: { name: data.name, status: data.status } }
: null;
case "conflict":
return typeof data.type === "string" &&
typeof data.where === "string" &&
typeof data.suggestion === "string"
? {
event: "conflict",
data: {
type: data.type,
where: data.where,
refs: asStringArray(data.refs),
suggestion: data.suggestion,
},
}
: null;
case "foreshadow":
return isForeshadowKind(data.kind) && typeof data.title === "string"
? {
event: "foreshadow",
data: {
kind: data.kind,
code: nullableString(data.code),
title: data.title,
where: nullableString(data.where),
note: nullableString(data.note),
},
}
: null;
case "pace":
return {
event: "pace",
data: {
water: asPaceIssues(data.water),
hook: data.hook === true,
beat_map: asNumberArray(data.beat_map),
},
};
case "style":
return typeof data.score === "number"
? {
event: "style",
data: {
score: data.score,
segments: asStyleSegments(data.segments),
},
}
: null;
case "done":
return typeof data.length === "number"
? { event: "done", data: { length: data.length } }
: null;
case "error":
return typeof data.code === "string" && typeof data.message === "string"
? {
event: "error",
data: {
code: data.code,
message: data.message,
request_id: nullableString(data.request_id),
},
}
: null;
default:
return null;
}
}
function isSectionStatus(v: unknown): v is SectionStatus {
return v === "started" || v === "done" || v === "incomplete";
}
function isForeshadowKind(v: unknown): v is ForeshadowKind {
return v === "planted" || v === "resolved";
}
// 增量缓冲:吃进一段文本,吐出已完成的事件块(空行分隔),保留未完成尾部。