Merge pull request #1880 from dajiaohuang/fix/handoff-effect-replay

fix(web): start handoff exchange after strict-mode timer replay
This commit is contained in:
Bingxi Zhao
2026-10-19 00:27:10 +08:00
committed by GitHub
2 changed files with 41 additions and 1 deletions

View File

@@ -40,8 +40,10 @@ function HandoffPageContent() {
const code = searchParams.get("code")?.trim() ?? "";
window.history.replaceState(null, "", window.location.pathname);
if (!code || started.current) return;
started.current = true;
const timer = window.setTimeout(() => {
// Effect cleanup may cancel this deferred start before it runs.
// Mark the exchange as started only when it actually begins.
started.current = true;
void finish(code);
}, 0);
return () => window.clearTimeout(timer);

View File

@@ -0,0 +1,38 @@
import { render, waitFor } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import HandoffPage from "@/app/handoff/page";
const runtime = vi.hoisted(() => ({ fetch: vi.fn(), replace: vi.fn() }));
vi.mock("@/lib/api", () => ({ apiFetch: runtime.fetch, apiUrl: (url: string) => url }));
vi.mock("next/navigation", () => ({
useRouter: () => ({ replace: runtime.replace }),
useSearchParams: () => new URLSearchParams("code=example-code&next=%2Fprofile"),
}));
vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (value: string) => value }) }));
describe("automatic handoff sign-in", () => {
it.each([false, true])("completes a code sign-in with strict mode %s", async strict => {
runtime.replace.mockReset();
runtime.fetch.mockReset().mockImplementation((url: string) => Promise.resolve(
new Response(JSON.stringify(url.endsWith("/exchange") ? { ticket: "example-ticket" } : {})),
));
render(<HandoffPage />, { reactStrictMode: strict });
await waitFor(() => expect(runtime.replace).toHaveBeenCalledWith("/profile"));
expect(runtime.fetch).toHaveBeenCalledTimes(2);
expect(JSON.parse(runtime.fetch.mock.calls[0][1].body)).toEqual({ code: "example-code" });
expect(JSON.parse(runtime.fetch.mock.calls[1][1].body)).toEqual({ ticket: "example-ticket" });
});
it("does not exchange the code after unmount before the deferred start", async () => {
runtime.fetch.mockReset();
vi.useFakeTimers();
try {
const { unmount } = render(<HandoffPage />);
unmount();
await vi.runAllTimersAsync();
expect(runtime.fetch).not.toHaveBeenCalled();
} finally {
vi.useRealTimers();
}
});
});