Skip to content

Commit 8c836d6

Browse files
committed
fix(browser): preserve popup failures across state transitions
1 parent 148e3c4 commit 8c836d6

3 files changed

Lines changed: 193 additions & 10 deletions

File tree

‎extensions/browser/chrome-extension/page-share.e2e.test.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,33 @@ describe.runIf(runE2E)("Chrome page sharing with a real Gateway extension relay"
478478
)
479479
.toBe(expectedError);
480480

481-
await evaluateToolbarPopup<void>(
481+
await expect
482+
.poll(
483+
async () =>
484+
await evaluateToolbarPopup<number>(
485+
browserCdp,
486+
attached.sessionId,
487+
"window.__openclawPopupRefreshes",
488+
),
489+
{ timeout: 5_000, interval: 50 },
490+
)
491+
.toBeGreaterThan(0);
492+
const actionRefreshes = await evaluateToolbarPopup<number>(
482493
browserCdp,
483494
attached.sessionId,
484-
"new Promise((resolve) => setTimeout(resolve, 2_200))",
495+
"window.__openclawPopupRefreshes",
485496
);
497+
await expect
498+
.poll(
499+
async () =>
500+
await evaluateToolbarPopup<number>(
501+
browserCdp,
502+
attached.sessionId,
503+
"window.__openclawPopupRefreshes",
504+
),
505+
{ timeout: 5_000, interval: 50 },
506+
)
507+
.toBeGreaterThan(actionRefreshes);
486508
const observed = await evaluateToolbarPopup<{
487509
refreshes: number;
488510
status: string;
@@ -497,7 +519,7 @@ describe.runIf(runE2E)("Chrome page sharing with a real Gateway extension relay"
497519
})`,
498520
);
499521

500-
expect(observed.refreshes).toBeGreaterThan(0);
522+
expect(observed.refreshes).toBeGreaterThan(actionRefreshes);
501523
expect(observed.status).toBe(expectedError);
502524
expect(observed.visible).toBe(true);
503525
});

‎extensions/browser/chrome-extension/popup-errors.test.ts‎

Lines changed: 157 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ type PopupMessage = {
1010
pairingString?: string;
1111
};
1212

13-
async function loadPopup(params: {
13+
type PopupState = {
1414
paired?: boolean;
15-
failures: Partial<Record<"pair" | "unpair" | "toggleShareTab", string>>;
16-
}) {
15+
shared?: boolean;
16+
failures: Partial<Record<"getStatus" | "pair" | "unpair" | "toggleShareTab", string>>;
17+
onFailure?: (message: PopupMessage) => void;
18+
};
19+
20+
async function loadPopup(params: PopupState) {
1721
const markup = await fs.readFile(
1822
path.join(process.cwd(), "extensions/browser/chrome-extension/popup.html"),
1923
"utf8",
@@ -25,20 +29,21 @@ async function loadPopup(params: {
2529
const sendMessage = vi.fn(async (message: PopupMessage) => {
2630
const failure = params.failures[message.type as keyof typeof params.failures];
2731
if (failure) {
32+
params.onFailure?.(message);
2833
return { ok: false, error: failure };
2934
}
3035
switch (message.type) {
3136
case "getStatus":
3237
return {
3338
paired: params.paired !== false,
3439
state: "on",
35-
sharedTabCount: 0,
40+
sharedTabCount: params.shared ? 1 : 0,
3641
relayUrl: "ws://127.0.0.1:18797/extension",
3742
};
3843
case "prepareCopilotPanel":
3944
return { ok: true, path: "sidepanel.html?binding=fixture" };
4045
case "isTabShared":
41-
return { shared: false };
46+
return { shared: params.shared === true };
4247
default:
4348
return { ok: true };
4449
}
@@ -145,7 +150,9 @@ describe("Chrome extension popup action errors", () => {
145150

146151
it("shows a rejected share-toggle error in the visible connected popup", async () => {
147152
const error = "No tab with id: 44.";
148-
const failures: Partial<Record<"toggleShareTab", string>> = { toggleShareTab: error };
153+
const failures: Partial<Record<"getStatus" | "toggleShareTab", string>> = {
154+
toggleShareTab: error,
155+
};
149156
const { sendMessage } = await loadPopup({ failures });
150157

151158
popupElement("shareButton").click();
@@ -160,6 +167,13 @@ describe("Chrome extension popup action errors", () => {
160167
await expectVisibleErrorAfterStatusRefresh(error, sendMessage);
161168
expect(popupElement("connectedSection").classList.contains("hidden")).toBe(false);
162169

170+
failures.getStatus = "Could not refresh browser status.";
171+
await expectVisibleErrorAfterStatusRefresh(error, sendMessage);
172+
expect(popupElement("connectedSection").classList.contains("hidden")).toBe(false);
173+
174+
delete failures.getStatus;
175+
await expectVisibleErrorAfterStatusRefresh(error, sendMessage);
176+
163177
delete failures.toggleShareTab;
164178
popupElement("shareButton").click();
165179

@@ -169,6 +183,143 @@ describe("Chrome extension popup action errors", () => {
169183
});
170184
});
171185

186+
it.each([
187+
{ action: "share", initiallyShared: false, nextLabel: "Stop sharing this tab" },
188+
{ action: "unshare", initiallyShared: true, nextLabel: "Share this tab with OpenClaw" },
189+
])(
190+
"refreshes the actual tab controls immediately after a partially failed $action",
191+
async ({ initiallyShared, nextLabel }) => {
192+
const error = "Could not reconcile browser tab consent.";
193+
const popup: PopupState = {
194+
shared: initiallyShared,
195+
failures: { toggleShareTab: error },
196+
};
197+
popup.onFailure = () => {
198+
popup.shared = !popup.shared;
199+
};
200+
const { sendMessage } = await loadPopup(popup);
201+
const previousPollCount = sendMessage.mock.calls.filter(
202+
([message]) => message.type === "getStatus",
203+
).length;
204+
205+
popupElement("shareButton").click();
206+
207+
await vi.waitFor(() => {
208+
expect(popupElement("statusLine").textContent).toBe(error);
209+
expect(popupElement("shareButton").textContent).toBe(nextLabel);
210+
expect(popupElement("connectedSection").classList.contains("hidden")).toBe(false);
211+
expect(
212+
sendMessage.mock.calls.filter(([message]) => message.type === "getStatus").length,
213+
).toBeGreaterThan(previousPollCount);
214+
});
215+
},
216+
);
217+
218+
it("clears a partial-unpair error after successfully pairing again", async () => {
219+
const error = "Could not reconcile browser pairing.";
220+
const popup: PopupState = {
221+
paired: true,
222+
failures: { unpair: error },
223+
};
224+
popup.onFailure = () => {
225+
popup.paired = false;
226+
};
227+
const { sendMessage } = await loadPopup(popup);
228+
229+
popupElement("settingsButton").click();
230+
await vi.waitFor(() => {
231+
expect(popupElement("settingsSection").classList.contains("hidden")).toBe(false);
232+
});
233+
popupElement("unpairButton").click();
234+
await vi.waitFor(() => {
235+
expect(popupElement("statusLine").textContent).toBe(error);
236+
expect(popupElement("settingsSection").classList.contains("hidden")).toBe(false);
237+
expect(popupElement("unpairButton").classList.contains("hidden")).toBe(true);
238+
});
239+
240+
await expectVisibleErrorAfterStatusRefresh(error, sendMessage);
241+
popupElement("settingsButton").click();
242+
await vi.waitFor(() => {
243+
expect(popupElement("pairSection").classList.contains("hidden")).toBe(false);
244+
expect(popupElement("statusLine").textContent).toBe(error);
245+
});
246+
247+
popup.paired = true;
248+
popupElement("pairButton").click();
249+
await vi.waitFor(() => {
250+
expect(sendMessage).toHaveBeenCalledWith({ type: "pair", pairingString: "" });
251+
expect(popupElement("statusLine").textContent).toBe("Connected · 0 tabs shared");
252+
expect(popupElement("connectedSection").classList.contains("hidden")).toBe(false);
253+
});
254+
});
255+
256+
it("keeps a partially persisted pairing failure visible after entering the connected view", async () => {
257+
const error = "Could not finish browser pairing.";
258+
const popup: PopupState = {
259+
paired: false,
260+
failures: { pair: error },
261+
};
262+
popup.onFailure = () => {
263+
popup.paired = true;
264+
};
265+
const { sendMessage } = await loadPopup(popup);
266+
const pairingInput = popupElement("pairingString") as HTMLTextAreaElement;
267+
pairingInput.value = "ws://127.0.0.1:18797/extension#fixture-token";
268+
269+
popupElement("pairButton").click();
270+
271+
await vi.waitFor(() => {
272+
expect(sendMessage).toHaveBeenCalledWith({
273+
type: "pair",
274+
pairingString: pairingInput.value,
275+
});
276+
expect(popupElement("error").textContent).toBe(error);
277+
expect(popupElement("pairSection").classList.contains("hidden")).toBe(true);
278+
expect(popupElement("connectedSection").classList.contains("hidden")).toBe(false);
279+
expect(popupElement("statusLine").textContent).toBe(error);
280+
expect(popupElement("statusLine").closest(".hidden")).toBeNull();
281+
});
282+
283+
await expectVisibleErrorAfterStatusRefresh(error, sendMessage);
284+
popupElement("shareButton").click();
285+
286+
await vi.waitFor(() => {
287+
expect(popupElement("statusLine").textContent).toBe("Connected · 0 tabs shared");
288+
expect(popupElement("connectedSection").classList.contains("hidden")).toBe(false);
289+
});
290+
});
291+
292+
it.each([
293+
{ view: "connected", section: "connectedSection", settings: false },
294+
{ view: "settings", section: "settingsSection", settings: true },
295+
])(
296+
"keeps the existing $view view when a status poll fails and recovers",
297+
async ({ section, settings }) => {
298+
const error = "Could not read browser pairing.";
299+
const failures: Partial<Record<"getStatus", string>> = {};
300+
const { sendMessage } = await loadPopup({ failures });
301+
if (settings) {
302+
popupElement("settingsButton").click();
303+
await vi.waitFor(() => {
304+
expect(popupElement("settingsSection").classList.contains("hidden")).toBe(false);
305+
});
306+
}
307+
const previousStatusClass = popupElement("statusDot").className;
308+
309+
failures.getStatus = error;
310+
await expectVisibleErrorAfterStatusRefresh(error, sendMessage);
311+
expect(popupElement(section).classList.contains("hidden")).toBe(false);
312+
expect(popupElement("pairSection").classList.contains("hidden")).toBe(true);
313+
expect(popupElement("statusDot").className).toBe(previousStatusClass);
314+
315+
delete failures.getStatus;
316+
await vi.advanceTimersByTimeAsync(2_000);
317+
318+
expect(popupElement("statusLine").textContent).toBe("Connected · 0 tabs shared");
319+
expect(popupElement(section).classList.contains("hidden")).toBe(false);
320+
},
321+
);
322+
172323
it("preserves the existing visible pairing failure", async () => {
173324
const error = "Could not save browser pairing.";
174325
const { sendMessage } = await loadPopup({ paired: false, failures: { pair: error } });

‎extensions/browser/chrome-extension/popup.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ function relayHost(relayUrl) {
4848

4949
async function refresh() {
5050
const status = await chrome.runtime.sendMessage({ type: "getStatus" });
51+
if (status?.ok === false) {
52+
statusLine.textContent = actionError ?? status.error ?? "Could not refresh browser status.";
53+
return;
54+
}
5155
statusDot.className = `status-dot ${status.state}`;
5256
pairSection.classList.toggle("hidden", status.paired || settingsOpen);
5357
connectedSection.classList.toggle("hidden", !status.paired || settingsOpen);
@@ -121,10 +125,14 @@ async function onPair() {
121125
pairingString: pairingInput.value,
122126
});
123127
if (!result.ok) {
124-
errorLine.textContent = result.error ?? "Pairing failed.";
128+
actionError = result.error ?? "Pairing failed.";
129+
errorLine.textContent = actionError;
125130
errorLine.classList.remove("hidden");
131+
statusLine.textContent = actionError;
132+
await refresh();
126133
return;
127134
}
135+
actionError = null;
128136
await refresh();
129137
}
130138

@@ -133,6 +141,7 @@ async function onUnpair() {
133141
if (result?.ok === false) {
134142
actionError = result.error ?? "Could not unpair this browser.";
135143
statusLine.textContent = actionError;
144+
await refresh();
136145
return;
137146
}
138147
actionError = null;
@@ -147,6 +156,7 @@ async function onToggleShare() {
147156
if (result?.ok === false) {
148157
actionError = result.error ?? "Could not update browser tab sharing.";
149158
statusLine.textContent = actionError;
159+
await refresh();
150160
return;
151161
}
152162
actionError = null;

0 commit comments

Comments
 (0)