Skip to content

Commit 281da47

Browse files
committed
test(ui): clean up navigation teardown
1 parent 9772ce6 commit 281da47

4 files changed

Lines changed: 90 additions & 8 deletions

File tree

‎ui/src/ui/app-lifecycle.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ type LifecycleHost = {
4848
logsAutoFollow: boolean;
4949
logsAtBottom: boolean;
5050
logsEntries: unknown[];
51+
chatScrollFrame?: number | null;
52+
chatScrollTimeout?: number | null;
53+
logsScrollFrame?: number | null;
54+
controlUiTabPaintSeq?: number;
5155
popStateHandler: () => void;
5256
topbarObserver: ResizeObserver | null;
5357
};
@@ -79,12 +83,31 @@ export function handleFirstUpdated(host: LifecycleHost) {
7983
observeTopbar(host as unknown as Parameters<typeof observeTopbar>[0]);
8084
}
8185

86+
function cancelHostAnimationFrame(frame: number | null | undefined) {
87+
if (frame != null && typeof window.cancelAnimationFrame === "function") {
88+
window.cancelAnimationFrame(frame);
89+
}
90+
}
91+
92+
function clearHostTimeout(timeout: number | null | undefined) {
93+
if (timeout != null && typeof window.clearTimeout === "function") {
94+
window.clearTimeout(timeout);
95+
}
96+
}
97+
8298
export function handleDisconnected(host: LifecycleHost) {
8399
host.connectGeneration += 1;
100+
host.controlUiTabPaintSeq = (host.controlUiTabPaintSeq ?? 0) + 1;
84101
window.removeEventListener("popstate", host.popStateHandler);
85102
stopNodesPolling(host as unknown as Parameters<typeof stopNodesPolling>[0]);
86103
stopLogsPolling(host as unknown as Parameters<typeof stopLogsPolling>[0]);
87104
stopDebugPolling(host as unknown as Parameters<typeof stopDebugPolling>[0]);
105+
cancelHostAnimationFrame(host.chatScrollFrame);
106+
host.chatScrollFrame = null;
107+
cancelHostAnimationFrame(host.logsScrollFrame);
108+
host.logsScrollFrame = null;
109+
clearHostTimeout(host.chatScrollTimeout);
110+
host.chatScrollTimeout = null;
88111
host.realtimeTalkSession?.stop();
89112
host.realtimeTalkSession = null;
90113
host.realtimeTalkActive = false;

‎ui/src/ui/control-ui-performance.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Tab } from "./navigation.ts";
44

55
type ControlUiPerformanceHost = {
66
tab: Tab;
7+
isConnected?: boolean;
78
eventLog?: unknown[];
89
eventLogBuffer?: unknown[];
910
requestUpdate?: () => void;
@@ -89,7 +90,7 @@ export function scheduleControlUiTabVisibleTiming(
8990
host.requestUpdate?.();
9091

9192
const record = () => {
92-
if (host.controlUiTabPaintSeq !== seq || host.tab !== tab) {
93+
if (host.isConnected === false || host.controlUiTabPaintSeq !== seq || host.tab !== tab) {
9394
return;
9495
}
9596
recordControlUiPerformanceEvent(host, "control-ui.tab.visible", {

‎ui/src/ui/navigation.browser.test.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { afterEach, describe, expect, it, vi } from "vitest";
1+
import { describe, expect, it, vi } from "vitest";
22
import { mountApp as mountTestApp, registerAppMountHooks } from "./test-helpers/app-mount.ts";
33

44
registerAppMountHooks();
55

6-
afterEach(() => {
7-
vi.restoreAllMocks();
8-
});
9-
106
function mountApp(pathname: string) {
117
return mountTestApp(pathname);
128
}

‎ui/src/ui/test-helpers/app-mount.ts‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,69 @@ function createMatchMediaMock(width: number) {
4141
};
4242
});
4343
}
44+
45+
const mountedApps = new Set<OpenClawApp>();
46+
47+
function collectMountedApps() {
48+
return new Set<OpenClawApp>([
49+
...mountedApps,
50+
...document.querySelectorAll<OpenClawApp>("openclaw-app"),
51+
]);
52+
}
53+
54+
function nextMicrotask() {
55+
return Promise.resolve();
56+
}
57+
58+
function nextTimer() {
59+
return new Promise<void>((resolve) => window.setTimeout(resolve, 0));
60+
}
61+
62+
function nextFrame() {
63+
return new Promise<void>((resolve) => {
64+
if (typeof window.requestAnimationFrame !== "function") {
65+
window.setTimeout(resolve, 0);
66+
return;
67+
}
68+
window.requestAnimationFrame(() => resolve());
69+
});
70+
}
71+
72+
async function waitForAppUpdates(apps: Iterable<OpenClawApp>) {
73+
for (const app of apps) {
74+
await app.updateComplete;
75+
}
76+
}
77+
78+
async function drainAppWork(apps: Iterable<OpenClawApp>) {
79+
const snapshot = [...apps];
80+
await nextMicrotask();
81+
await waitForAppUpdates(snapshot);
82+
await nextFrame();
83+
await nextMicrotask();
84+
await nextFrame();
85+
await nextMicrotask();
86+
await waitForAppUpdates(snapshot);
87+
await nextTimer();
88+
await nextMicrotask();
89+
await waitForAppUpdates(snapshot);
90+
}
91+
92+
async function cleanupMountedApps() {
93+
const apps = collectMountedApps();
94+
await drainAppWork(apps);
95+
for (const app of apps) {
96+
app.remove();
97+
}
98+
document.body.replaceChildren();
99+
mountedApps.clear();
100+
await drainAppWork(apps);
101+
}
102+
44103
export function mountApp(pathname: string) {
45104
window.history.replaceState({}, "", pathname);
46105
const app = document.createElement("openclaw-app") as OpenClawApp;
106+
mountedApps.add(app);
47107
document.body.append(app);
48108
app.connected = true;
49109
app.requestUpdate();
@@ -96,12 +156,14 @@ export function registerAppMountHooks() {
96156
});
97157

98158
afterEach(async () => {
159+
await cleanupMountedApps();
99160
window.__OPENCLAW_CONTROL_UI_BASE_PATH__ = undefined;
100161
getSafeLocalStorage()?.clear();
101162
getSafeSessionStorage()?.clear();
102-
document.body.innerHTML = "";
103163
await i18n.setLocale("en");
164+
vi.restoreAllMocks();
104165
vi.unstubAllGlobals();
105-
await new Promise<void>((resolve) => setTimeout(resolve, 0));
166+
await nextTimer();
167+
await nextMicrotask();
106168
});
107169
}

0 commit comments

Comments
 (0)