fix(zalouser): clear probe timeout after auth resolves (#101649)

This commit is contained in:
Alix-007
2026-07-09 15:29:44 +08:00
committed by GitHub
parent 75848adb8c
commit ca8f6e1efd
2 changed files with 35 additions and 5 deletions

View File

@@ -60,6 +60,23 @@ describe("probeZalouser", () => {
});
});
it("clears the probe timeout after auth resolves", async () => {
vi.useFakeTimers();
const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout");
mockGetUserInfo.mockResolvedValueOnce({
userId: "123",
displayName: "Alice",
});
await expect(probeZalouser("default", 10)).resolves.toEqual({
ok: true,
user: { userId: "123", displayName: "Alice" },
});
expect(clearTimeoutSpy).toHaveBeenCalledTimes(1);
expect(vi.getTimerCount()).toBe(0);
});
it("caps oversized lookup timeout before scheduling", async () => {
vi.useFakeTimers();
mockGetUserInfo.mockReturnValueOnce(new Promise(() => {}));

View File

@@ -14,14 +14,27 @@ export async function probeZalouser(
timeoutMs?: number,
): Promise<ZalouserProbeResult> {
try {
const user = timeoutMs
? await Promise.race([
let user: ZcaUserInfo | null;
if (timeoutMs) {
let timeout: ReturnType<typeof setTimeout> | undefined;
try {
user = await Promise.race([
getZaloUserInfo(profile),
new Promise<null>((resolve) => {
setTimeout(() => resolve(null), resolveTimerTimeoutMs(timeoutMs, 1000, 1000));
timeout = setTimeout(
() => resolve(null),
resolveTimerTimeoutMs(timeoutMs, 1000, 1000),
);
}),
])
: await getZaloUserInfo(profile);
]);
} finally {
if (timeout) {
clearTimeout(timeout);
}
}
} else {
user = await getZaloUserInfo(profile);
}
if (!user) {
return { ok: false, error: "Not authenticated" };