* chore(merge): resolve conflicts, fix mockWs error, and apply refactor standards * Potential fix for pull request finding 'CodeQL / Incomplete URL substring sanitization' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for pull request finding 'CodeQL / Incomplete URL substring sanitization' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Potential fix for pull request finding 'CodeQL / Incomplete URL substring sanitization' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * fix(ci): Stabilize github actions and resolve test failures - fix(tests): update WsClient mock to use standard function for Vitest constructor compatibility - fix(db): correct PrismaClient import path to use generated output directory - fix(e2e): add predev database boot step to playwright CI workflow - chore: sync pnpm-lock.yaml after merge conflict resolution * refactor(plugins): decouple GeoJSON plugin and implement data bus lifecycle - feat(sdk): [Minor] Added dynamicPluginCreate, dynamicPluginRemove, and pluginUnregistered to DataBusEvents. - feat(core): [Minor] Implemented unregisterPlugin in PluginManager.ts for automated IndexedDB caching cleanup and polling teardown. - feat(core): [Minor] Added removeLayer to layersSlice.ts to support safe removal of plugin states. - refactor(plugins): [Patch] Refactored useGeoJsonImport.ts and ImportedLayerList.tsx to decouple from direct manager imports, utilizing DataBus for lifecycle events. - refactor(core): [Patch] Added pluginUnregistered listener to DataBusSubscriber.tsx to handle state and cache cleanup synchronously via event. * fix(types): resolve PluginManager type errors * refactor(core): decouple PluginManager and fix rendering invariants - refactor(core): Replace Zustand mutations in PluginManager with DataBus events (Patch) - refactor(sdk): Add layerLoadingChanged, pluginError, pluginUnregistered to DataBusEvents (Patch) - refactor(ui): Subscribe to new DataBus events in DataBusSubscriber (Patch) - fix(plugins): Remove size from BaseAviationPlugin to fix model rendering clipping (Patch) - fix(plugins): Migrate BaseIncidentPlugin from size to iconScale to fix billboard clipping (Patch) * chore: resolve lockfile conflict --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
import {
|
|
describe, it, expect, beforeEach, afterEach, vi
|
|
} from "vitest";
|
|
|
|
import { pollingManager } from "./PollingManager";
|
|
|
|
// The store import in PollingManager's constructor pulls in Cesium,
|
|
// zustand middleware, and the entire UI state tree. Mock it before the
|
|
// module loads so the polling-manager singleton wakes up against a
|
|
// shape that's safe in jsdom.
|
|
vi.mock("@/core/state/store", () => ({
|
|
useStore: {
|
|
getState: () => ({ dataConfig: { pollingIntervals: {} } }),
|
|
subscribe: () => () => {},
|
|
},
|
|
}));
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
});
|
|
|
|
afterEach(() => {
|
|
// Clean up timers and any tasks registered in this test so the next
|
|
// one starts from a known state. The singleton doesn't expose a
|
|
// reset method, but `stopAll` + `unregister` covers our cases.
|
|
pollingManager.stopAll();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
// Helper: drain the microtask queue so the immediate `run()` Promise
|
|
// scheduled inside `start()` resolves before we assert on call counts.
|
|
// We can't use `runAllTimersAsync` here because setInterval is unbounded
|
|
// and `runOnlyPendingTimersAsync` advances the next interval tick too,
|
|
// which would double-count.
|
|
async function flushMicrotasks() {
|
|
for (let i = 0; i < 5; i++) await Promise.resolve();
|
|
}
|
|
|
|
describe("PollingManager", () => {
|
|
it("runs the callback once immediately when start() is called", async () => {
|
|
const callback = vi.fn(async () => {});
|
|
pollingManager.register("test", 60_000, callback);
|
|
|
|
pollingManager.start("test");
|
|
await flushMicrotasks();
|
|
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("schedules recurring calls at intervalMs after the initial run", async () => {
|
|
const callback = vi.fn(async () => {});
|
|
pollingManager.register("test", 1_000, callback);
|
|
|
|
pollingManager.start("test");
|
|
await flushMicrotasks();
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
|
|
await vi.advanceTimersByTimeAsync(1_000);
|
|
expect(callback).toHaveBeenCalledTimes(2);
|
|
|
|
await vi.advanceTimersByTimeAsync(2_000);
|
|
expect(callback).toHaveBeenCalledTimes(4);
|
|
});
|
|
|
|
it("stops scheduling further calls after stop()", async () => {
|
|
const callback = vi.fn(async () => {});
|
|
pollingManager.register("test", 1_000, callback);
|
|
|
|
pollingManager.start("test");
|
|
await flushMicrotasks();
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
|
|
pollingManager.stop("test");
|
|
await vi.advanceTimersByTimeAsync(10_000);
|
|
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|