@@ -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 } } ) ;
0 commit comments