Skip to content

Commit e555351

Browse files
committed
test(ui): wait for app mount before checking route readiness
1 parent 76e6015 commit e555351

2 files changed

Lines changed: 77 additions & 19 deletions

File tree

‎ui/src/test-helpers/control-ui-e2e.test.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Control UI tests cover control ui e2e behavior.
2-
import { describe, expect, it } from "vitest";
2+
import type { Page } from "playwright";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
34
import {
45
resolvePlaywrightChromiumExecutablePath,
56
systemChromiumExecutableCandidates,
7+
waitForControlUiRoute,
68
} from "./control-ui-e2e.ts";
79

810
describe("resolvePlaywrightChromiumExecutablePath", () => {
@@ -28,3 +30,54 @@ describe("resolvePlaywrightChromiumExecutablePath", () => {
2830
).toBe("/custom/chromium");
2931
});
3032
});
33+
34+
describe("waitForControlUiRoute", () => {
35+
afterEach(() => {
36+
document.body.replaceChildren();
37+
});
38+
39+
it("keeps polling while a new tab has no app element", async () => {
40+
// SAFETY: this fixture implements the Page methods used by the route helper.
41+
const page = {
42+
async waitForFunction(
43+
predicate: (target: { routeId: string }) => boolean,
44+
target: { routeId: string },
45+
) {
46+
expect(predicate(target)).toBe(false);
47+
const app = document.createElement("openclaw-app");
48+
Object.assign(app, {
49+
runtime: {
50+
router: {
51+
getState: () => ({
52+
status: "success",
53+
resolvedLocation: { pathname: window.location.pathname },
54+
matches: [{ routeId: "chat" }],
55+
pendingMatches: [],
56+
}),
57+
},
58+
},
59+
});
60+
document.body.append(app);
61+
expect(predicate(target)).toBe(true);
62+
return { dispose: vi.fn() };
63+
},
64+
evaluate: (read: () => unknown) => read(),
65+
} as unknown as Page;
66+
67+
await waitForControlUiRoute(page, { routeId: "chat" });
68+
});
69+
70+
it("preserves readiness failures when the app is still absent", async () => {
71+
const cause = new Error("Route readiness failed");
72+
// SAFETY: this fixture implements the Page methods used by the route helper.
73+
const page = {
74+
waitForFunction: vi.fn().mockRejectedValue(cause),
75+
evaluate: (read: () => unknown) => read(),
76+
} as unknown as Page;
77+
78+
await expect(waitForControlUiRoute(page, { routeId: "chat" })).rejects.toMatchObject({
79+
cause,
80+
message: expect.stringContaining('"router":null'),
81+
});
82+
});
83+
});

‎ui/src/test-helpers/control-ui-e2e.ts‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,22 @@ export async function waitForControlUiRoute(page: Page, target: ControlUiRouteTa
123123
try {
124124
const handle = await page.waitForFunction(
125125
(expected) => {
126-
const app = document.querySelector("openclaw-app") as HTMLElement & {
127-
runtime?: {
128-
router: {
129-
getState: () => {
130-
status: string;
131-
resolvedLocation: { pathname: string } | null;
132-
matches: { routeId: string }[];
133-
pendingMatches: unknown[];
126+
const app = document.querySelector<
127+
HTMLElement & {
128+
runtime?: {
129+
router: {
130+
getState: () => {
131+
status: string;
132+
resolvedLocation: { pathname: string } | null;
133+
matches: { routeId: string }[];
134+
pendingMatches: unknown[];
135+
};
134136
};
135137
};
136-
};
137-
};
138-
const state = app.runtime?.router.getState();
138+
}
139+
>("openclaw-app");
140+
// Native popup events can arrive before the app element is parsed.
141+
const state = app?.runtime?.router.getState();
139142
const pathname = window.location.pathname;
140143
// Router paths retain literal characters that browser history percent-encodes.
141144
// Serialize as a pathname; decoding would alias encoded delimiters and percent data.
@@ -163,17 +166,19 @@ export async function waitForControlUiRoute(page: Page, target: ControlUiRouteTa
163166
await handle.dispose();
164167
} catch (error) {
165168
const state = await page.evaluate(() => {
166-
const app = document.querySelector("openclaw-app") as HTMLElement & {
167-
runtime?: {
168-
router: {
169-
getState: () => unknown;
169+
const app = document.querySelector<
170+
HTMLElement & {
171+
runtime?: {
172+
router: {
173+
getState: () => unknown;
174+
};
170175
};
171-
};
172-
};
176+
}
177+
>("openclaw-app");
173178
return {
174179
hash: window.location.hash,
175180
pathname: window.location.pathname,
176-
router: app.runtime?.router.getState() ?? null,
181+
router: app?.runtime?.router.getState() ?? null,
177182
search: window.location.search,
178183
};
179184
});

0 commit comments

Comments
 (0)