fix(frontend): RefineView 取消在途 refine——AbortController 防段切换竞态(CR-H12)

This commit is contained in:
Yaojia Wang
2026-07-08 11:08:21 +02:00
parent 5665cc9a16
commit 5c02792e5f
3 changed files with 65 additions and 7 deletions

View File

@@ -122,4 +122,40 @@ describe("useRefine", () => {
expect(result.current.status).toBe("idle");
expect(result.current.result).toBeNull();
});
it("中止后迟到的响应不覆盖状态(段切换竞态)", async () => {
// Arrangepost 挂起,手动兑现以模拟在途请求。
let resolvePost!: (v: unknown) => void;
const pending = new Promise((r) => {
resolvePost = r;
});
post.mockReturnValue(pending);
const { result } = renderHook(() => useRefine());
// Act发起回炉进入 refining随后 abort模拟切段取消在途
let refinePromise!: Promise<unknown>;
act(() => {
refinePromise = result.current.refine("p1", 1, "新段");
});
expect(result.current.status).toBe("refining");
act(() => {
result.current.abort();
});
// 迟到的旧响应兑现——应被丢弃:不写状态、不弹 toast。
await act(async () => {
resolvePost({
data: { original: "旧段", refined: "旧改写" },
error: null,
});
await refinePromise;
});
// Assert结果仍为空、未落到 done、未误报 toast。
expect(result.current.result).toBeNull();
expect(result.current.status).not.toBe("done");
expect(toast).not.toHaveBeenCalled();
});
});