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:
@@ -41,6 +41,24 @@ describe("parseSseBlock", () => {
|
||||
it("returns null for malformed json", () => {
|
||||
expect(parseSseBlock("event:token\ndata:{not json")).toBeNull();
|
||||
});
|
||||
|
||||
// P1-8:逐事件类型守卫——已知事件但 data 形状不符也拒绝(不再强转放行)。
|
||||
it("returns null when token frame lacks string text", () => {
|
||||
expect(parseSseBlock('event:token\ndata:{"text":42}')).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null when error frame lacks code/message", () => {
|
||||
expect(parseSseBlock('event:error\ndata:{"code":"X"}')).toBeNull();
|
||||
});
|
||||
|
||||
it("coerces missing request_id to null on error frame", () => {
|
||||
expect(
|
||||
parseSseBlock('event:error\ndata:{"code":"X","message":"m"}'),
|
||||
).toEqual({
|
||||
event: "error",
|
||||
data: { code: "X", message: "m", request_id: null },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("SseFrameBuffer", () => {
|
||||
|
||||
@@ -39,7 +39,40 @@ export function parseSseBlock(block: string): SseEvent | null {
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
return { event, data } as SseEvent;
|
||||
return narrowSseEvent(event, data);
|
||||
}
|
||||
|
||||
function isRecord(v: unknown): v is Record<string, unknown> {
|
||||
return typeof v === "object" && v !== null && !Array.isArray(v);
|
||||
}
|
||||
|
||||
// 逐事件类型守卫:按 event 名收窄 data 形状;形状不符则返回 null(安全跳过)。
|
||||
function narrowSseEvent(event: string, data: unknown): SseEvent | null {
|
||||
if (!isRecord(data)) return null;
|
||||
switch (event) {
|
||||
case "token":
|
||||
return typeof data.text === "string"
|
||||
? { event: "token", data: { text: data.text } }
|
||||
: 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:
|
||||
typeof data.request_id === "string" ? data.request_id : null,
|
||||
},
|
||||
}
|
||||
: null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 增量缓冲:吃进一段文本,吐出已完成的事件块(以空行分隔),保留未完成尾部。
|
||||
|
||||
Reference in New Issue
Block a user