|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import type { GatewayHelloOk } from "../../api/gateway.ts"; |
| 3 | +import type { RouteId } from "../../app-route-paths.ts"; |
| 4 | +import type { ApplicationContext, ApplicationGatewaySnapshot } from "../../app/context.ts"; |
| 5 | +import { scheduleStaleChunkReload } from "../../app/stale-chunk-reload.ts"; |
| 6 | +import { CONTROL_UI_BUILD_INFO } from "../../build-info.ts"; |
| 7 | +import { waitForFast } from "../../test-helpers/wait-for.ts"; |
| 8 | +import { PluginPage } from "./plugin-page.ts"; |
| 9 | + |
| 10 | +type TestBundledView = { |
| 11 | + render: () => unknown; |
| 12 | + stop: (host: object) => void; |
| 13 | +}; |
| 14 | + |
| 15 | +function deferred<T>() { |
| 16 | + let reject!: (error: unknown) => void; |
| 17 | + let resolve!: (value: T) => void; |
| 18 | + const promise = new Promise<T>((resolvePromise, rejectPromise) => { |
| 19 | + reject = rejectPromise; |
| 20 | + resolve = resolvePromise; |
| 21 | + }); |
| 22 | + return { promise, reject, resolve }; |
| 23 | +} |
| 24 | + |
| 25 | +class RejectedPluginPage extends PluginPage { |
| 26 | + loads: Promise<TestBundledView>[] = []; |
| 27 | + |
| 28 | + protected override loadBundledView(): Promise<TestBundledView> { |
| 29 | + const load = this.loads.shift(); |
| 30 | + if (!load) { |
| 31 | + throw new Error("Unexpected bundled view load"); |
| 32 | + } |
| 33 | + return load; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +const rejectedPluginPageTag = "openclaw-rejected-plugin-page-test"; |
| 38 | +if (!customElements.get(rejectedPluginPageTag)) { |
| 39 | + customElements.define(rejectedPluginPageTag, RejectedPluginPage); |
| 40 | +} |
| 41 | + |
| 42 | +function createPage(loads: Promise<TestBundledView>[], includeExternal = false) { |
| 43 | + const hello: GatewayHelloOk = { |
| 44 | + type: "hello-ok", |
| 45 | + protocol: 3, |
| 46 | + auth: { role: "operator", scopes: ["operator.write"] }, |
| 47 | + controlUiTabs: [ |
| 48 | + { pluginId: "logbook", id: "logbook", label: "Logbook" }, |
| 49 | + ...(includeExternal |
| 50 | + ? [{ pluginId: "external-plugin", id: "panel", label: "External panel" }] |
| 51 | + : []), |
| 52 | + ], |
| 53 | + }; |
| 54 | + const snapshot: ApplicationGatewaySnapshot = { |
| 55 | + client: null, |
| 56 | + phase: "connected", |
| 57 | + offlineStable: false, |
| 58 | + canvasPluginSurfaceUrl: null, |
| 59 | + hello, |
| 60 | + assistantAgentId: null, |
| 61 | + sessionKey: "main", |
| 62 | + lastError: null, |
| 63 | + lastErrorCode: null, |
| 64 | + }; |
| 65 | + const page = document.createElement(rejectedPluginPageTag) as RejectedPluginPage; |
| 66 | + page.loads = loads; |
| 67 | + page.pluginId = "logbook"; |
| 68 | + page.tabId = "logbook"; |
| 69 | + (page as unknown as { context: ApplicationContext<RouteId> }).context = { |
| 70 | + gateway: { snapshot, subscribe: () => () => undefined }, |
| 71 | + } as unknown as ApplicationContext<RouteId>; |
| 72 | + return page; |
| 73 | +} |
| 74 | + |
| 75 | +describe("PluginPage bundled view load failures", () => { |
| 76 | + it("shows the current rejection and recovers when Retry succeeds", async () => { |
| 77 | + const failedLoad = deferred<TestBundledView>(); |
| 78 | + const retryLoad = deferred<TestBundledView>(); |
| 79 | + const page = createPage([failedLoad.promise, retryLoad.promise]); |
| 80 | + document.body.append(page); |
| 81 | + try { |
| 82 | + await waitForFast(() => expect(page.querySelector('[role="status"]')).not.toBeNull()); |
| 83 | + failedLoad.reject(new Error("Logbook chunk failed")); |
| 84 | + await waitForFast(() => |
| 85 | + expect(page.querySelector('[role="alert"]')?.textContent).toContain("Logbook chunk failed"), |
| 86 | + ); |
| 87 | + |
| 88 | + page.querySelector<HTMLButtonElement>('[role="alert"] button')?.click(); |
| 89 | + await waitForFast(() => expect(page.querySelector('[role="status"]')).not.toBeNull()); |
| 90 | + retryLoad.resolve({ render: () => "recovered Logbook view", stop: vi.fn() }); |
| 91 | + await waitForFast(() => expect(page.textContent).toContain("recovered Logbook view")); |
| 92 | + expect(page.querySelector('[role="alert"]')).toBeNull(); |
| 93 | + } finally { |
| 94 | + page.remove(); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + it("keeps stale recovery visible after this build already reloaded automatically", async () => { |
| 99 | + const failedLoad = deferred<TestBundledView>(); |
| 100 | + const values = new Map<string, string>(); |
| 101 | + const storage = { |
| 102 | + getItem: (key: string) => values.get(key) ?? null, |
| 103 | + setItem: (key: string, value: string) => void values.set(key, value), |
| 104 | + }; |
| 105 | + vi.spyOn(window, "sessionStorage", "get").mockReturnValue(storage as Storage); |
| 106 | + const fetchMock = vi.fn<typeof fetch>(async () => new Response(null, { status: 200 })); |
| 107 | + vi.stubGlobal("fetch", fetchMock); |
| 108 | + await scheduleStaleChunkReload({ |
| 109 | + buildId: CONTROL_UI_BUILD_INFO.buildId, |
| 110 | + now: () => Date.now() - 6_000, |
| 111 | + reload: vi.fn(), |
| 112 | + storage, |
| 113 | + }); |
| 114 | + fetchMock.mockClear(); |
| 115 | + const page = createPage([failedLoad.promise]); |
| 116 | + document.body.append(page); |
| 117 | + try { |
| 118 | + await waitForFast(() => expect(page.querySelector('[role="status"]')).not.toBeNull()); |
| 119 | + failedLoad.reject(new Error("Failed to fetch dynamically imported module")); |
| 120 | + await waitForFast(() => |
| 121 | + expect(page.querySelector('[role="alert"]')?.textContent).toContain( |
| 122 | + "Failed to fetch dynamically imported module", |
| 123 | + ), |
| 124 | + ); |
| 125 | + |
| 126 | + await Promise.resolve(); |
| 127 | + await page.updateComplete; |
| 128 | + |
| 129 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 130 | + expect(page.querySelector('[role="alert"]')).not.toBeNull(); |
| 131 | + } finally { |
| 132 | + page.remove(); |
| 133 | + vi.unstubAllGlobals(); |
| 134 | + vi.restoreAllMocks(); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + it("ignores a stale rejection after switching away and back", async () => { |
| 139 | + const staleLoad = deferred<TestBundledView>(); |
| 140 | + const currentLoad = deferred<TestBundledView>(); |
| 141 | + const page = createPage([staleLoad.promise, currentLoad.promise], true); |
| 142 | + document.body.append(page); |
| 143 | + try { |
| 144 | + await page.updateComplete; |
| 145 | + page.pluginId = "external-plugin"; |
| 146 | + page.tabId = "panel"; |
| 147 | + await page.updateComplete; |
| 148 | + page.pluginId = "logbook"; |
| 149 | + page.tabId = "logbook"; |
| 150 | + await page.updateComplete; |
| 151 | + |
| 152 | + currentLoad.resolve({ render: () => "current Logbook view", stop: vi.fn() }); |
| 153 | + await waitForFast(() => expect(page.textContent).toContain("current Logbook view")); |
| 154 | + staleLoad.reject(new Error("Failed to fetch dynamically imported module")); |
| 155 | + await Promise.resolve(); |
| 156 | + await page.updateComplete; |
| 157 | + |
| 158 | + expect(page.querySelector('[role="alert"]')).toBeNull(); |
| 159 | + expect(page.textContent).toContain("current Logbook view"); |
| 160 | + } finally { |
| 161 | + page.remove(); |
| 162 | + } |
| 163 | + }); |
| 164 | +}); |
0 commit comments