|
1 | 1 | /* @vitest-environment jsdom */ |
2 | 2 |
|
3 | 3 | 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"; |
9 | 5 | import { takeSessionPanelToggle } from "../components/session-panel-toggle-buffer.ts"; |
| 6 | +import { createStorageMock } from "../test-helpers/storage.ts"; |
10 | 7 | import { |
11 | 8 | createLazyElementSpec, |
12 | 9 | resetAppHostTestGlobals, |
13 | 10 | type TestOptionalCustomElement, |
14 | 11 | } from "./app-host.test-support.ts"; |
15 | 12 | import "./app-host.ts"; |
| 13 | +import { ShellChromeOwner, type ShellChromeHost } from "./app-shell-chrome.ts"; |
16 | 14 | import type { ApplicationContext } from "./context.ts"; |
| 15 | +import type { LazyCustomElementRequestController } from "./lazy-custom-element.ts"; |
| 16 | +import { persistLazyShellAction, readLazyShellAction } from "./lazy-shell-action.ts"; |
17 | 17 |
|
18 | 18 | 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; |
24 | 20 | routeState: { routeId: string }; |
25 | 21 | runtime: { context: ApplicationContext }; |
26 | 22 | terminalPanelElement: TestOptionalCustomElement; |
27 | 23 | }; |
28 | 24 |
|
29 | | -afterEach(() => { |
30 | | - resetAppHostTestGlobals(); |
31 | | -}); |
| 25 | +function chromeOwner(shell: ShellPanelToggleState): ShellChromeOwner { |
| 26 | + return new ShellChromeOwner(shell as unknown as ShellChromeHost); |
| 27 | +} |
32 | 28 |
|
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"] }, |
55 | 43 | }, |
56 | 44 | }, |
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; |
77 | 45 | }, |
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), |
94 | 52 | }); |
| 53 | + return shell; |
| 54 | +} |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + window.history.replaceState(null, "", "/"); |
| 58 | + resetAppHostTestGlobals(); |
| 59 | +}); |
95 | 60 |
|
| 61 | +describe("OpenClaw shell panel toggles", () => { |
96 | 62 | it("buffers panel toggle events until the active chat pane mounts", () => { |
97 | 63 | const terminalElement = createLazyElementSpec("session terminal panel"); |
98 | 64 | const shell = document.createElement("openclaw-app-shell") as unknown as ShellPanelToggleState; |
99 | 65 | shell.terminalPanelElement = terminalElement; |
100 | 66 | shell.routeState = { routeId: "chat" }; |
101 | 67 |
|
102 | 68 | const event = new CustomEvent(TERMINAL_PANEL_TOGGLE_EVENT, { detail: { open: true } }); |
103 | | - shell.handleDeferredTerminalToggle(event); |
| 69 | + chromeOwner(shell).handleDeferredTerminalToggle(event); |
104 | 70 |
|
105 | 71 | expect(customElements.get(terminalElement.tagName)).toBeUndefined(); |
106 | 72 | expect(takeSessionPanelToggle("terminal")).toBe(event); |
107 | 73 | }); |
| 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 | + }); |
108 | 140 | }); |
0 commit comments