Skip to content

πŸ™Œ OpenHands: AI-Driven Development

test(automations,backends): drive switch timing deterministically (#17571)

Haroon K Mec79cafd7123
diff --git a/.github/pr-assets/pr-17571-test-results.svg b/.github/pr-assets/pr-17571-test-results.svg
new file mode 100644
index 0000000000..eb502cd230
--- /dev/null
+++ b/.github/pr-assets/pr-17571-test-results.svg
@@ -0,0 +1,17 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="980" height="502" viewBox="0 0 980 502" font-family="ui-monospace,SFMono-Regular,Menlo,Consolas,monospace" font-size="16">
+<rect width="980" height="502" rx="8" fill="#0d1117"/>
+<text x="26" y="42" fill="#7d8590" xml:space="preserve"># BEFORE - both files restored from a073648</text>
+<text x="26" y="67" fill="#58a6ff" xml:space="preserve">$ npx vitest run \</text>
+<text x="26" y="92" fill="#58a6ff" xml:space="preserve"> __tests__/hooks/query/use-automations-backend-switch.test.tsx \</text>
+<text x="26" y="117" fill="#58a6ff" xml:space="preserve"> __tests__/components/backends/backend-selector.test.tsx</text>
+<text x="26" y="167" fill="#3fb950" font-weight="600" xml:space="preserve"> Test Files 2 passed (2)</text>
+<text x="26" y="192" fill="#3fb950" font-weight="600" xml:space="preserve"> Tests 38 passed (38)</text>
+<text x="26" y="217" fill="#f85149" font-weight="600" xml:space="preserve"> Duration 14.64s (transform 2.42s, setup 1.62s, import 7.00s, tests 14.91s, environment 1.92s)</text>
+<text x="26" y="292" fill="#7d8590" xml:space="preserve"># AFTER - this branch</text>
+<text x="26" y="317" fill="#58a6ff" xml:space="preserve">$ npx vitest run \</text>
+<text x="26" y="342" fill="#58a6ff" xml:space="preserve"> __tests__/hooks/query/use-automations-backend-switch.test.tsx \</text>
+<text x="26" y="367" fill="#58a6ff" xml:space="preserve"> __tests__/components/backends/backend-selector.test.tsx</text>
+<text x="26" y="417" fill="#3fb950" font-weight="600" xml:space="preserve"> Test Files 2 passed (2)</text>
+<text x="26" y="442" fill="#3fb950" font-weight="600" xml:space="preserve"> Tests 38 passed (38)</text>
+<text x="26" y="467" fill="#3fb950" font-weight="600" xml:space="preserve"> Duration 13.24s (transform 2.45s, setup 1.55s, import 6.93s, tests 6.54s, environment 1.89s)</text>
+</svg>
\ No newline at end of file
diff --git a/__tests__/components/backends/backend-selector.test.tsx b/__tests__/components/backends/backend-selector.test.tsx
index 1b3bd05535..365687f11f 100644
--- a/__tests__/components/backends/backend-selector.test.tsx
+++ b/__tests__/components/backends/backend-selector.test.tsx
@@ -26,7 +26,6 @@ import {
__resetEnvironmentSwitchOverlayForTests,
EnvironmentSwitchOverlay,
} from "#/components/features/backends/environment-switch-overlay";
-import { ENVIRONMENT_SWITCH_SETACTIVE_DELAY_MS } from "#/components/features/backends/environment-switch-store";
import {
ServerClient,
@@ -109,6 +108,8 @@ function TestSeed({
return children as React.ReactElement;
}
+const ACTIVE_BACKEND_STORAGE_KEY = "openhands-active-backend";
+
async function openDropdown() {
const user = userEvent.setup();
const wrapper = screen.getByTestId("backend-selector");
@@ -161,21 +162,35 @@ beforeEach(() => {
});
});
-afterEach(async () => {
- // When a test selects a dropdown option, BackendSelector's onChange
- // calls `triggerEnvironmentSwitch` and then awaits a real-time
- // `setTimeout(..., ENVIRONMENT_SWITCH_SETACTIVE_DELAY_MS)` before
- // running `setActive`. `userEvent.click` does NOT await that async
- // handler, so the trailing `setActive` can land AFTER the test ends,
- // re-polluting `localStorage` during a later test. The body's
- // `data-environment-switching` attribute is set while the switch is
- // in flight; wait it out before clearing storage so the in-flight
- // `setActive` writes BEFORE we wipe state.
- if (document.body.hasAttribute("data-environment-switching")) {
- await new Promise((resolve) => {
- setTimeout(resolve, ENVIRONMENT_SWITCH_SETACTIVE_DELAY_MS + 20);
- });
- }
+/**
+ * Wait for a backend switch triggered by a dropdown click to finish.
+ *
+ * `BackendSelector.handleSelectBackend` awaits a real
+ * `ENVIRONMENT_SWITCH_SETACTIVE_DELAY_MS` timer before calling `setActive`,
+ * and `userEvent.click` does not await that async handler. Waiting for the
+ * persisted selection to change proves the trailing `setActive` landed inside
+ * the test rather than leaking into the next one.
+ *
+ * This deliberately observes the persisted selection rather than the body's
+ * `data-environment-switching` attribute: that attribute is cleared by the
+ * overlay's separate ENVIRONMENT_SWITCH_DURATION_MS timer, so it does not
+ * track when `setActive` actually ran.
+ */
+async function settleBackendSwitch(previous: string | null) {
+ await waitFor(() => {
+ expect(window.localStorage.getItem(ACTIVE_BACKEND_STORAGE_KEY)).not.toBe(
+ previous,
+ );
+ });
+}
+
+function activeBackendSnapshot() {
+ return window.localStorage.getItem(ACTIVE_BACKEND_STORAGE_KEY);
+}
+
+afterEach(() => {
+ // Each test that switches backends awaits `settleBackendSwitch`, so no
+ // trailing `setActive` is in flight here and teardown needs no delay.
window.localStorage.clear();
vi.unstubAllEnvs();
__resetActiveStoreForTests();
@@ -629,7 +644,9 @@ describe("BackendSelector", () => {
// Auto-switch lands on "Local 1"; click the seeded default to switch.
const user = await openDropdown();
+ const before = activeBackendSnapshot();
await user.click(screen.getByText("Local"));
+ await settleBackendSwitch(before);
const wrapper = screen.getByTestId("backend-selector");
const input = wrapper.querySelector("input") as HTMLInputElement;
@@ -693,7 +710,9 @@ describe("BackendSelector", () => {
);
const user = await openDropdown();
+ const before = activeBackendSnapshot();
await user.click(screen.getByText("Local"));
+ await settleBackendSwitch(before);
if (expectRedirect) {
expect(await screen.findByTestId("landing-route")).toBeInTheDocument();
@@ -733,6 +752,7 @@ describe("BackendSelector", () => {
// to trigger a switch, then immediately unmount the selector (the
// click itself would do this in production via the outside-click handler).
const user = await openDropdown();
+ const before = activeBackendSnapshot();
await user.click(screen.getByText("Local"));
selectorRender.unmount();
@@ -741,6 +761,10 @@ describe("BackendSelector", () => {
"data-target",
"Local",
);
+
+ // The switch was deliberately left in flight across the unmount; let its
+ // trailing `setActive` land here rather than during the next test.
+ await settleBackendSwitch(before);
});
it("does not open backend modals on mouse down alone", async () => {
diff --git a/__tests__/hooks/query/use-automations-backend-switch.test.tsx b/__tests__/hooks/query/use-automations-backend-switch.test.tsx
index 585e60083f..b976c8d87b 100644
--- a/__tests__/hooks/query/use-automations-backend-switch.test.tsx
+++ b/__tests__/hooks/query/use-automations-backend-switch.test.tsx
@@ -45,6 +45,10 @@ vi.mock("#/api/automation-service/automation-service.api", () => ({
},
}));
+// Mirrors the non-terminal poll interval returned by `useAutomationRuns`
+// (`refetchInterval` in src/hooks/query/use-automation-detail.ts).
+const RUNS_POLL_INTERVAL_MS = 3000;
+
let captureMock: ReturnType<typeof vi.spyOn>;
vi.mock("#/hooks/query/use-settings", () => ({
@@ -209,23 +213,29 @@ describe("useAutomationRuns β€” polling", () => {
completed_at: "2026-01-02T00:00:30Z",
};
- it(
- "re-fetches while a run is non-terminal, and stops once all runs are terminal",
- async () => {
- // Arrange: first fetch returns a PENDING run (polling should engage);
- // subsequent fetches return a COMPLETED run (polling should then stop).
- const pendingResponse: AutomationRunsResponse = {
- runs: [pendingRun],
- total: 1,
- };
- const completedResponse: AutomationRunsResponse = {
- runs: [completedRun],
- total: 1,
- };
- vi.mocked(AutomationService.getAutomationRuns)
- .mockResolvedValueOnce(pendingResponse)
- .mockResolvedValue(completedResponse);
-
+ it("re-fetches while a run is non-terminal, and stops once all runs are terminal", async () => {
+ // Arrange: first fetch returns a PENDING run (polling should engage);
+ // subsequent fetches return a COMPLETED run (polling should then stop).
+ const pendingResponse: AutomationRunsResponse = {
+ runs: [pendingRun],
+ total: 1,
+ };
+ const completedResponse: AutomationRunsResponse = {
+ runs: [completedRun],
+ total: 1,
+ };
+ vi.mocked(AutomationService.getAutomationRuns)
+ .mockResolvedValueOnce(pendingResponse)
+ .mockResolvedValue(completedResponse);
+
+ // Drive the poll window with fake timers: the contract under test is
+ // "one refetch per window while non-terminal, none once terminal", and
+ // advancing the clock asserts exactly that without paying for it in
+ // real time. `advanceTimersByTimeAsync` is required rather than the
+ // synchronous form because each refetch settles through microtasks
+ // between timer callbacks.
+ vi.useFakeTimers();
+ try {
// Act
renderHook(
() => useAutomationRuns({ id: "auto-1", limit: 20, offset: 0 }),
@@ -233,29 +243,28 @@ describe("useAutomationRuns β€” polling", () => {
);
// Assert: the initial fetch fires once.
- await waitFor(() => {
- expect(AutomationService.getAutomationRuns).toHaveBeenCalledTimes(1);
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(0);
});
+ expect(AutomationService.getAutomationRuns).toHaveBeenCalledTimes(1);
// The cached data still contains a PENDING run, so refetchInterval
- // engages and a second fetch arrives within the poll window.
- await waitFor(
- () => {
- expect(AutomationService.getAutomationRuns).toHaveBeenCalledTimes(2);
- },
- { timeout: 5000 },
- );
+ // engages and a second fetch arrives one poll window later.
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(RUNS_POLL_INTERVAL_MS);
+ });
+ expect(AutomationService.getAutomationRuns).toHaveBeenCalledTimes(2);
- // The second fetch returned a COMPLETED run, so polling should stop.
- // Give the would-be next poll window plenty of slack and assert no
- // further calls happen.
- await new Promise((resolve) => {
- setTimeout(resolve, 4000);
+ // The second fetch returned a COMPLETED run, so polling should stop:
+ // several further poll windows elapse with no additional calls.
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(RUNS_POLL_INTERVAL_MS * 3);
});
expect(AutomationService.getAutomationRuns).toHaveBeenCalledTimes(2);
- },
- 15000,
- );
+ } finally {
+ vi.useRealTimers();
+ }
+ });
});
describe("run mutations β€” sidebar conversation refresh", () => {