From d6258b94e2cfc842d35fbf4ae9798229bc47be68 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 10 Apr 2026 21:11:44 +0800 Subject: [PATCH] test(mobile): cover ota provider helpers --- .../modules/ota/__tests__/provider.test.ts | 113 ++++++++++++++++++ apps/mobile/src/modules/ota/provider.tsx | 71 ++++++++--- 2 files changed, 165 insertions(+), 19 deletions(-) create mode 100644 apps/mobile/src/modules/ota/__tests__/provider.test.ts diff --git a/apps/mobile/src/modules/ota/__tests__/provider.test.ts b/apps/mobile/src/modules/ota/__tests__/provider.test.ts new file mode 100644 index 000000000..5a2c71ec2 --- /dev/null +++ b/apps/mobile/src/modules/ota/__tests__/provider.test.ts @@ -0,0 +1,113 @@ +import { beforeAll, describe, expect, it, vi } from "vitest" + +vi.mock("expo-application", () => ({ + nativeApplicationVersion: "0.4.1", +})) + +vi.mock("expo-updates", () => ({ + default: {}, + channel: "production", + runtimeVersion: "0.4.1", + isEnabled: true, + reloadAsync: vi.fn(), + useUpdates: vi.fn(), + UpdateInfoType: { + NEW: "new", + }, +})) + +vi.mock("react-native", () => ({ + InteractionManager: { + runAfterInteractions: (callback: () => void) => { + callback() + return { + cancel: vi.fn(), + } + }, + }, +})) + +let runSafeReloadUpdate: typeof import("../provider").runSafeReloadUpdate +let runWithSingleInFlight: typeof import("../provider").runWithSingleInFlight + +beforeAll(async () => { + ;({ runSafeReloadUpdate, runWithSingleInFlight } = await import("../provider")) +}) + +describe("runWithSingleInFlight", () => { + it("deduplicates concurrent calls and clears after resolution", async () => { + let resolveRun: ((value: { kind: "idle" }) => void) | null = null + const inFlightRef: { current: Promise<{ kind: "idle" }> | null } = { current: null } + const run = vi.fn( + () => + new Promise<{ kind: "idle" }>((resolve) => { + resolveRun = resolve + }), + ) + + const first = runWithSingleInFlight({ + inFlightRef, + run, + }) + const second = runWithSingleInFlight({ + inFlightRef, + run, + }) + + expect(run).toHaveBeenCalledTimes(1) + expect(second).toBe(first) + + expect(resolveRun).not.toBeNull() + resolveRun!({ kind: "idle" }) + + await expect(first).resolves.toEqual({ kind: "idle" }) + expect(inFlightRef.current).toBeNull() + + const nextRun = vi.fn().mockResolvedValue({ kind: "idle" as const }) + const third = runWithSingleInFlight({ + inFlightRef, + run: nextRun, + }) + + await expect(third).resolves.toEqual({ kind: "idle" }) + expect(nextRun).toHaveBeenCalledTimes(1) + }) +}) + +describe("runSafeReloadUpdate", () => { + it("does not reload when updates are disabled", async () => { + const reload = vi.fn().mockResolvedValue() + + await runSafeReloadUpdate({ + isEnabled: false, + pendingVersion: "0.4.3", + reload, + }) + + expect(reload).not.toHaveBeenCalled() + }) + + it("does not reload when no pending update exists", async () => { + const reload = vi.fn().mockResolvedValue() + + await runSafeReloadUpdate({ + isEnabled: true, + pendingVersion: null, + reload, + }) + + expect(reload).not.toHaveBeenCalled() + }) + + it("reloads when updates are enabled and a pending version exists", async () => { + const reload = vi.fn().mockResolvedValue() + + await runSafeReloadUpdate({ + isEnabled: true, + pendingVersion: "0.4.3", + reload, + }) + + expect(reload).toHaveBeenCalledTimes(1) + }) +}) diff --git a/apps/mobile/src/modules/ota/provider.tsx b/apps/mobile/src/modules/ota/provider.tsx index 82e5689a8..284814678 100644 --- a/apps/mobile/src/modules/ota/provider.tsx +++ b/apps/mobile/src/modules/ota/provider.tsx @@ -62,6 +62,43 @@ const getErrorMessage = (error: unknown, fallback: string) => { return error instanceof Error ? error.message : fallback } +export function runWithSingleInFlight({ + inFlightRef, + run, +}: { + inFlightRef: { current: Promise | null } + run: () => Promise +}) { + if (inFlightRef.current) { + return inFlightRef.current + } + + const promise = run().finally(() => { + if (inFlightRef.current === promise) { + inFlightRef.current = null + } + }) + + inFlightRef.current = promise + return promise +} + +export const runSafeReloadUpdate = async ({ + isEnabled, + pendingVersion, + reload, +}: { + isEnabled: boolean + pendingVersion: string | null + reload: () => Promise +}) => { + if (!isEnabled || !pendingVersion) { + return + } + + await reload() +} + const runOtaCheck = async ({ dispatch, currentPendingVersion, @@ -188,19 +225,16 @@ const runGuardedOtaCheck = ({ shouldCancel, }) - const promise = runOtaCheck({ - dispatch, - currentPendingVersion, - isUpdatePending, - shouldCancel, - }).finally(() => { - if (inFlightRef.current === promise) { - inFlightRef.current = null - } + return runWithSingleInFlight({ + inFlightRef, + run: () => + runOtaCheck({ + dispatch, + currentPendingVersion, + isUpdatePending, + shouldCancel, + }), }) - - inFlightRef.current = promise - return promise } export const OtaProvider = ({ children }: PropsWithChildren) => { @@ -289,13 +323,12 @@ export const OtaProvider = ({ children }: PropsWithChildren) => { isUpdatePending, shouldCancel: () => isUnmountedRef.current, }), - reloadUpdate: async () => { - if (!Updates.isEnabled || !currentPendingVersion) { - return - } - - await Updates.reloadAsync() - }, + reloadUpdate: async () => + runSafeReloadUpdate({ + isEnabled: Updates.isEnabled, + pendingVersion: currentPendingVersion, + reload: () => Updates.reloadAsync(), + }), } }, [currentPendingVersion, isUpdatePending])