Skip to content

Commit 5eda48e

Browse files
authored
fix(ui): recover failed lazy surfaces (#126409)
1 parent 615ff6a commit 5eda48e

16 files changed

Lines changed: 1197 additions & 325 deletions

‎ui/config/control-ui-chunking.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ function moduleIdIncludesPackage(id: string, packageName: string): boolean {
1414
export function controlUiStableChunkName(id: string): string | undefined {
1515
const normalized = normalizeModuleId(id);
1616

17-
// These entry-and-route helpers must stay together; separate shared chunks
18-
// turn small route-graph changes into extra startup preload requests.
19-
if (
20-
normalized.endsWith("/ui/src/components/config-form.shared.ts") ||
21-
normalized.endsWith("/ui/src/lib/clipboard.ts") ||
22-
normalized.endsWith("/ui/src/build-info-normalizers.ts") ||
23-
normalized.endsWith("/ui/src/build-info.ts")
24-
) {
25-
return "control-ui-shared";
26-
}
27-
2817
if (normalized.endsWith("/ui/src/lib/gateway-methods.ts")) {
2918
return "gateway-runtime";
3019
}
Lines changed: 104 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,140 @@
11
/* @vitest-environment jsdom */
22

33
import { afterEach, describe, expect, it, vi } from "vitest";
4-
import {
5-
BROWSER_PANEL_TOGGLE_EVENT,
6-
CUSTODIAN_PANEL_TOGGLE_EVENT,
7-
TERMINAL_PANEL_TOGGLE_EVENT,
8-
} from "../components/panel-toggle-contract.ts";
4+
import { TERMINAL_PANEL_TOGGLE_EVENT } from "../components/panel-toggle-contract.ts";
95
import { takeSessionPanelToggle } from "../components/session-panel-toggle-buffer.ts";
6+
import { createStorageMock } from "../test-helpers/storage.ts";
107
import {
118
createLazyElementSpec,
129
resetAppHostTestGlobals,
1310
type TestOptionalCustomElement,
1411
} from "./app-host.test-support.ts";
1512
import "./app-host.ts";
13+
import { ShellChromeOwner, type ShellChromeHost } from "./app-shell-chrome.ts";
1614
import type { ApplicationContext } from "./context.ts";
15+
import type { LazyCustomElementRequestController } from "./lazy-custom-element.ts";
16+
import { persistLazyShellAction, readLazyShellAction } from "./lazy-shell-action.ts";
1717

1818
type ShellPanelToggleState = {
19-
browserPanelElement: TestOptionalCustomElement;
20-
custodianPanelElement: TestOptionalCustomElement;
21-
handleDeferredBrowserToggle: (event: Event) => void;
22-
handleDeferredCustodianToggle: (event: Event) => void;
23-
handleDeferredTerminalToggle: (event: Event) => void;
19+
lazyCustomElements: LazyCustomElementRequestController;
2420
routeState: { routeId: string };
2521
runtime: { context: ApplicationContext };
2622
terminalPanelElement: TestOptionalCustomElement;
2723
};
2824

29-
afterEach(() => {
30-
resetAppHostTestGlobals();
31-
});
25+
function chromeOwner(shell: ShellPanelToggleState): ShellChromeOwner {
26+
return new ShellChromeOwner(shell as unknown as ShellChromeHost);
27+
}
3228

33-
describe("OpenClaw shell panel toggles", () => {
34-
it("delivers first panel toggles after their lazy modules load", async () => {
35-
const terminalElement = createLazyElementSpec("terminal panel");
36-
const browserElement = createLazyElementSpec("browser panel");
37-
const custodianElement = createLazyElementSpec("custodian panel");
38-
const terminalToggle = vi.fn();
39-
const browserToggle = vi.fn();
40-
const custodianToggle = vi.fn();
41-
const shell = document.createElement("openclaw-app-shell") as unknown as ShellPanelToggleState;
42-
shell.terminalPanelElement = terminalElement;
43-
shell.browserPanelElement = browserElement;
44-
shell.custodianPanelElement = custodianElement;
45-
shell.runtime = {
46-
context: {
47-
gateway: {
48-
snapshot: {
49-
phase: "connected",
50-
client: {},
51-
hello: {
52-
auth: { role: "operator", scopes: ["operator.admin"] },
53-
features: { methods: ["terminal.open", "browser.request", "openclaw.chat"] },
54-
},
29+
function configureTerminalShell(terminalElement: TestOptionalCustomElement): ShellPanelToggleState {
30+
window.history.replaceState(null, "", "/usage");
31+
const shell = document.createElement("openclaw-app-shell") as unknown as ShellPanelToggleState;
32+
shell.terminalPanelElement = terminalElement;
33+
shell.routeState = { routeId: "usage" };
34+
shell.runtime = {
35+
context: {
36+
gateway: {
37+
snapshot: {
38+
phase: "connected",
39+
client: {},
40+
hello: {
41+
auth: { role: "operator", scopes: ["operator.admin"] },
42+
features: { methods: ["terminal.open"] },
5543
},
5644
},
57-
config: { current: { terminalEnabled: true } },
58-
} as unknown as ApplicationContext,
59-
};
60-
Object.defineProperty(shell, "updateComplete", {
61-
configurable: true,
62-
get: () => Promise.resolve(true),
63-
});
64-
Object.defineProperty(shell, "querySelector", {
65-
configurable: true,
66-
value: (selector: string) => {
67-
if (selector === terminalElement.tagName) {
68-
return { handleToggleRequest: terminalToggle };
69-
}
70-
if (selector === browserElement.tagName) {
71-
return { handleToggleRequest: browserToggle };
72-
}
73-
if (selector === custodianElement.tagName) {
74-
return { handleToggleRequest: custodianToggle };
75-
}
76-
return null;
7745
},
78-
});
79-
const terminalEvent = new CustomEvent(TERMINAL_PANEL_TOGGLE_EVENT, {
80-
detail: { dock: "right", open: true },
81-
});
82-
const browserEvent = new CustomEvent(BROWSER_PANEL_TOGGLE_EVENT);
83-
const custodianEvent = new CustomEvent(CUSTODIAN_PANEL_TOGGLE_EVENT);
84-
85-
shell.handleDeferredTerminalToggle(terminalEvent);
86-
shell.handleDeferredBrowserToggle(browserEvent);
87-
shell.handleDeferredCustodianToggle(custodianEvent);
88-
89-
await vi.waitFor(() => {
90-
expect(terminalToggle).toHaveBeenCalledWith(terminalEvent);
91-
expect(browserToggle).toHaveBeenCalledWith(browserEvent);
92-
expect(custodianToggle).toHaveBeenCalledWith(custodianEvent);
93-
});
46+
config: { current: { terminalEnabled: true } },
47+
} as unknown as ApplicationContext,
48+
};
49+
Object.defineProperty(shell, "updateComplete", {
50+
configurable: true,
51+
get: () => Promise.resolve(true),
9452
});
53+
return shell;
54+
}
55+
56+
afterEach(() => {
57+
window.history.replaceState(null, "", "/");
58+
resetAppHostTestGlobals();
59+
});
9560

61+
describe("OpenClaw shell panel toggles", () => {
9662
it("buffers panel toggle events until the active chat pane mounts", () => {
9763
const terminalElement = createLazyElementSpec("session terminal panel");
9864
const shell = document.createElement("openclaw-app-shell") as unknown as ShellPanelToggleState;
9965
shell.terminalPanelElement = terminalElement;
10066
shell.routeState = { routeId: "chat" };
10167

10268
const event = new CustomEvent(TERMINAL_PANEL_TOGGLE_EVENT, { detail: { open: true } });
103-
shell.handleDeferredTerminalToggle(event);
69+
chromeOwner(shell).handleDeferredTerminalToggle(event);
10470

10571
expect(customElements.get(terminalElement.tagName)).toBeUndefined();
10672
expect(takeSessionPanelToggle("terminal")).toBe(event);
10773
});
74+
75+
it("retains the exact rejected panel request through in-place retry", async () => {
76+
const error = new Error("terminal chunk unavailable");
77+
const terminalElement = createLazyElementSpec("terminal panel", { firstError: error });
78+
const terminalToggle = vi.fn();
79+
const shell = configureTerminalShell(terminalElement);
80+
const owner = chromeOwner(shell);
81+
const event = new CustomEvent(TERMINAL_PANEL_TOGGLE_EVENT, {
82+
detail: { dock: "right", open: true },
83+
});
84+
window.addEventListener(TERMINAL_PANEL_TOGGLE_EVENT, terminalToggle);
85+
86+
try {
87+
owner.handleDeferredTerminalToggle(event);
88+
89+
await vi.waitFor(() => expect(shell.lazyCustomElements.visibleState?.status).toBe("error"));
90+
expect(shell.lazyCustomElements.visibleState).toMatchObject({ error });
91+
expect(terminalToggle).not.toHaveBeenCalled();
92+
93+
shell.lazyCustomElements.retry();
94+
95+
await vi.waitFor(() => expect(terminalToggle).toHaveBeenCalledOnce());
96+
} finally {
97+
window.removeEventListener(TERMINAL_PANEL_TOGGLE_EVENT, terminalToggle);
98+
}
99+
const delivered = terminalToggle.mock.calls[0]?.[0] as CustomEvent;
100+
expect(delivered).not.toBe(event);
101+
expect(delivered.type).toBe(TERMINAL_PANEL_TOGGLE_EVENT);
102+
expect(delivered.detail).toEqual(event.detail);
103+
});
104+
105+
it("restores a structured panel event once in a replacement shell", async () => {
106+
vi.stubGlobal("sessionStorage", createStorageMock());
107+
const terminalElement = createLazyElementSpec("restored terminal");
108+
const terminalToggle = vi.fn();
109+
const detail = { dock: "right" as const, open: true, terminalSessionId: "terminal-1" };
110+
persistLazyShellAction({
111+
eventType: TERMINAL_PANEL_TOGGLE_EVENT,
112+
detail,
113+
});
114+
115+
const replacement = configureTerminalShell(terminalElement);
116+
const owner = chromeOwner(replacement);
117+
const restoreListener = (restored: Event) => owner.handleDeferredTerminalToggle(restored);
118+
const panelListener = (restored: Event) => {
119+
if (customElements.get(terminalElement.tagName)) {
120+
terminalToggle(restored);
121+
}
122+
};
123+
window.addEventListener(TERMINAL_PANEL_TOGGLE_EVENT, restoreListener);
124+
window.addEventListener(TERMINAL_PANEL_TOGGLE_EVENT, panelListener);
125+
try {
126+
await vi.waitFor(() => {
127+
owner.restorePendingLazyAction();
128+
expect(terminalToggle).toHaveBeenCalledOnce();
129+
});
130+
} finally {
131+
window.removeEventListener(TERMINAL_PANEL_TOGGLE_EVENT, restoreListener);
132+
window.removeEventListener(TERMINAL_PANEL_TOGGLE_EVENT, panelListener);
133+
}
134+
const restored = terminalToggle.mock.calls[0]?.[0];
135+
expect(restored).toBeInstanceOf(CustomEvent);
136+
expect(restored?.type).toBe(TERMINAL_PANEL_TOGGLE_EVENT);
137+
expect((restored as CustomEvent).detail).toEqual(detail);
138+
expect(readLazyShellAction()).toBeNull();
139+
});
108140
});

‎ui/src/app/app-host.test-support.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ export type TestOptionalCustomElement = {
2525

2626
let lazyElementSequence = 0;
2727

28-
export function createLazyElementSpec(label: string): TestOptionalCustomElement {
28+
export function createLazyElementSpec(
29+
label: string,
30+
options: { firstError?: Error } = {},
31+
): TestOptionalCustomElement {
2932
lazyElementSequence += 1;
3033
const tagName = `openclaw-app-host-lazy-${lazyElementSequence}`;
34+
let attempt = 0;
3135
return {
3236
tagName,
3337
label,
3438
loadModule: async () => {
39+
attempt += 1;
40+
if (attempt === 1 && options.firstError) {
41+
throw options.firstError;
42+
}
3543
customElements.define(tagName, class extends HTMLElement {});
3644
},
3745
};

0 commit comments

Comments
 (0)