Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

test(pki-discovery): cover scan polling interval logic

Claudefd1294ba1f7a
diff --git a/frontend/src/hooks/api/pkiDiscovery/index.tsx b/frontend/src/hooks/api/pkiDiscovery/index.tsx
index f9ad676926..260b27e99c 100644
--- a/frontend/src/hooks/api/pkiDiscovery/index.tsx
+++ b/frontend/src/hooks/api/pkiDiscovery/index.tsx
@@ -6,8 +6,8 @@ export {
useUpdatePkiDiscovery,
useUpdatePkiInstallation
} from "./mutations";
+export { isPkiDiscoveryScanInFlight } from "./polling";
export {
- isPkiDiscoveryScanInFlight,
pkiDiscoveryKeys,
pkiInstallationKeys,
useGetLatestScan,
diff --git a/frontend/src/hooks/api/pkiDiscovery/polling.test.ts b/frontend/src/hooks/api/pkiDiscovery/polling.test.ts
new file mode 100644
index 0000000000..ee3ac7ad71
--- /dev/null
+++ b/frontend/src/hooks/api/pkiDiscovery/polling.test.ts
@@ -0,0 +1,52 @@
+import assert from "node:assert/strict";
+import { describe, it } from "node:test";
+
+import {
+ getPkiDiscoveryRefetchInterval,
+ isPkiDiscoveryScanInFlight,
+ SCAN_IDLE_POLL_MS,
+ SCAN_IN_FLIGHT_POLL_MS
+} from "./polling";
+import { PkiDiscoveryScanStatus } from "./types";
+
+describe("isPkiDiscoveryScanInFlight", () => {
+ it("is true while a scan is pending or running", () => {
+ assert.equal(isPkiDiscoveryScanInFlight(PkiDiscoveryScanStatus.Pending), true);
+ assert.equal(isPkiDiscoveryScanInFlight(PkiDiscoveryScanStatus.Running), true);
+ });
+
+ it("is false for terminal or missing statuses", () => {
+ assert.equal(isPkiDiscoveryScanInFlight(PkiDiscoveryScanStatus.Completed), false);
+ assert.equal(isPkiDiscoveryScanInFlight(PkiDiscoveryScanStatus.Failed), false);
+ assert.equal(isPkiDiscoveryScanInFlight(null), false);
+ assert.equal(isPkiDiscoveryScanInFlight(undefined), false);
+ });
+});
+
+describe("getPkiDiscoveryRefetchInterval", () => {
+ it("polls fast while a scan is in flight", () => {
+ assert.equal(
+ getPkiDiscoveryRefetchInterval(PkiDiscoveryScanStatus.Pending),
+ SCAN_IN_FLIGHT_POLL_MS
+ );
+ assert.equal(
+ getPkiDiscoveryRefetchInterval(PkiDiscoveryScanStatus.Running),
+ SCAN_IN_FLIGHT_POLL_MS
+ );
+ });
+
+ it("falls back to the slow idle poll once a scan is terminal", () => {
+ assert.equal(
+ getPkiDiscoveryRefetchInterval(PkiDiscoveryScanStatus.Completed),
+ SCAN_IDLE_POLL_MS
+ );
+ assert.equal(getPkiDiscoveryRefetchInterval(PkiDiscoveryScanStatus.Failed), SCAN_IDLE_POLL_MS);
+ assert.equal(getPkiDiscoveryRefetchInterval(null), SCAN_IDLE_POLL_MS);
+ assert.equal(getPkiDiscoveryRefetchInterval(undefined), SCAN_IDLE_POLL_MS);
+ });
+
+ it("keeps polling while idle so a newly started scan is noticed", () => {
+ assert.notEqual(getPkiDiscoveryRefetchInterval(PkiDiscoveryScanStatus.Completed), false);
+ assert.ok(SCAN_IDLE_POLL_MS > SCAN_IN_FLIGHT_POLL_MS);
+ });
+});
diff --git a/frontend/src/hooks/api/pkiDiscovery/polling.ts b/frontend/src/hooks/api/pkiDiscovery/polling.ts
new file mode 100644
index 0000000000..62765a252b
--- /dev/null
+++ b/frontend/src/hooks/api/pkiDiscovery/polling.ts
@@ -0,0 +1,12 @@
+import { PkiDiscoveryScanStatus } from "./types";
+
+export const SCAN_IN_FLIGHT_POLL_MS = 5000;
+// Slow background poll while idle so a scan started elsewhere (scheduled auto-scan, or a
+// stale terminal status read from a replica right after triggering) is still picked up.
+export const SCAN_IDLE_POLL_MS = 30000;
+
+export const isPkiDiscoveryScanInFlight = (status?: PkiDiscoveryScanStatus | null) =>
+ status === PkiDiscoveryScanStatus.Pending || status === PkiDiscoveryScanStatus.Running;
+
+export const getPkiDiscoveryRefetchInterval = (status?: PkiDiscoveryScanStatus | null) =>
+ isPkiDiscoveryScanInFlight(status) ? SCAN_IN_FLIGHT_POLL_MS : SCAN_IDLE_POLL_MS;
diff --git a/frontend/src/hooks/api/pkiDiscovery/queries.tsx b/frontend/src/hooks/api/pkiDiscovery/queries.tsx
index 373b95a542..18ce4c94fb 100644
--- a/frontend/src/hooks/api/pkiDiscovery/queries.tsx
+++ b/frontend/src/hooks/api/pkiDiscovery/queries.tsx
@@ -2,8 +2,8 @@ import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
+import { getPkiDiscoveryRefetchInterval } from "./polling";
import {
- PkiDiscoveryScanStatus,
TGetLatestScanDTO,
TGetPkiDiscoveryDTO,
TGetPkiInstallationDTO,
@@ -43,14 +43,6 @@ export const pkiInstallationKeys = {
type TPollingOptions = { refetchInterval?: number | false };
-const SCAN_IN_FLIGHT_POLL_MS = 5000;
-// Slow background poll while idle so a scan started elsewhere (scheduled auto-scan, or a
-// stale terminal status read from a replica right after triggering) is still picked up.
-const SCAN_IDLE_POLL_MS = 30000;
-
-export const isPkiDiscoveryScanInFlight = (status?: PkiDiscoveryScanStatus | null) =>
- status === PkiDiscoveryScanStatus.Pending || status === PkiDiscoveryScanStatus.Running;
-
export const useListPkiDiscoveries = ({
projectId,
offset = 0,
@@ -88,10 +80,7 @@ export const useGetPkiDiscovery = ({ discoveryId }: TGetPkiDiscoveryDTO) => {
return data;
},
enabled: Boolean(discoveryId),
- refetchInterval: (query) =>
- isPkiDiscoveryScanInFlight(query.state.data?.lastScanStatus)
- ? SCAN_IN_FLIGHT_POLL_MS
- : SCAN_IDLE_POLL_MS
+ refetchInterval: (query) => getPkiDiscoveryRefetchInterval(query.state.data?.lastScanStatus)
});
};