1+ import { Buffer } from "node:buffer" ;
12import type { Page } from "playwright" ;
23import { expect , it } from "vitest" ;
34import { waitForControlUiGatewayReady } from "../test-helpers/control-ui-e2e-readiness.ts" ;
@@ -8,26 +9,39 @@ import {
89import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts" ;
910
1011const suite = createControlUiE2eSuite ( {
11- name : "chat sidebar panel contract " ,
12+ name : "chat sidebar cold-open invariant " ,
1213 startServerBeforeBrowser : true ,
1314} ) ;
1415
16+ const ONE_PIXEL_PNG = Buffer . from (
17+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Wl2nPcAAAAASUVORK5CYII=" ,
18+ "base64" ,
19+ ) ;
20+
1521type ColdOpenOutcome = {
1622 outcome : "content" | "generic-empty" ;
1723 emptyStateOffersAction : boolean ;
1824} ;
1925
20- const expectedColdOpenOutcomes : Record < string , ColdOpenOutcome > = {
21- Review : { outcome : "generic-empty" , emptyStateOffersAction : false } ,
22- Terminal : { outcome : "content" , emptyStateOffersAction : false } ,
23- Browser : { outcome : "generic-empty" , emptyStateOffersAction : true } ,
24- // Files deliberately defaults to the project browser when the session has no items.
25- Files : { outcome : "content" , emptyStateOffersAction : false } ,
26- "Side chat" : { outcome : "generic-empty" , emptyStateOffersAction : false } ,
27- Tasks : { outcome : "generic-empty" , emptyStateOffersAction : false } ,
28- Desktop : { outcome : "content" , emptyStateOffersAction : false } ,
29- Discussion : { outcome : "generic-empty" , emptyStateOffersAction : true } ,
30- } ;
26+ const offeredSlotLabels = [
27+ "Review" ,
28+ "Terminal" ,
29+ "Browser" ,
30+ "Files" ,
31+ "Side chat" ,
32+ "Tasks" ,
33+ "Desktop" ,
34+ "Discussion" ,
35+ ] as const ;
36+
37+ type OfferedSlotLabel = ( typeof offeredSlotLabels ) [ number ] ;
38+
39+ const actionlessEmptyStateAllowlist = new Set < OfferedSlotLabel > ( [
40+ // Review: no git checkout, nothing to diff.
41+ "Review" ,
42+ // Tasks: no background tasks, nothing to inspect.
43+ "Tasks" ,
44+ ] ) ;
3145
3246function coldOpenScenario ( ) : ControlUiMockGatewayScenario {
3347 return {
@@ -39,6 +53,7 @@ function coldOpenScenario(): ControlUiMockGatewayScenario {
3953 "environments.list" ,
4054 "session.discussion.info" ,
4155 "session.discussion.open" ,
56+ "sessions.companion.state" ,
4257 "sessions.diff" ,
4358 "sessions.files.list" ,
4459 "tasks.list" ,
@@ -77,11 +92,138 @@ function coldOpenScenario(): ControlUiMockGatewayScenario {
7792 } ;
7893}
7994
80- async function openColdSidebar ( page : Page ) {
81- const gateway = await installMockGateway ( page , coldOpenScenario ( ) ) ;
95+ function populatedColdOpenScenario ( ) : ControlUiMockGatewayScenario {
96+ const sparse = coldOpenScenario ( ) ;
97+ return {
98+ ...sparse ,
99+ methodResponses : {
100+ ...sparse . methodResponses ,
101+ "browser.request" : {
102+ cases : [
103+ {
104+ match : { method : "GET" , path : "/tabs" } ,
105+ response : {
106+ running : true ,
107+ tabs : [
108+ {
109+ targetId : "target-1" ,
110+ tabId : "tab-1" ,
111+ title : "OpenClaw" ,
112+ url : "https://example.test/" ,
113+ } ,
114+ ] ,
115+ } ,
116+ } ,
117+ {
118+ match : { method : "POST" , path : "/screenshot" } ,
119+ response : {
120+ path : "/proof/browser.png" ,
121+ targetId : "target-1" ,
122+ url : "https://example.test/" ,
123+ } ,
124+ } ,
125+ ] ,
126+ } ,
127+ "environments.list" : {
128+ environments : [ { id : "gateway" , type : "local" , status : "available" , desktop : true } ] ,
129+ } ,
130+ "session.discussion.info" : {
131+ embedUrl : "https://discussion.example/embed/thread/session" ,
132+ openUrl : "https://discussion.example/session" ,
133+ state : "open" ,
134+ } ,
135+ "sessions.companion.state" : {
136+ exchanges : [
137+ {
138+ question : "What changed?" ,
139+ answer : "Every offered panel now opens with useful content." ,
140+ ts : Date . now ( ) - 1_000 ,
141+ } ,
142+ ] ,
143+ } ,
144+ "sessions.diff" : {
145+ additions : 1 ,
146+ baseRef : "main" ,
147+ branch : "feature/sidebar-invariant" ,
148+ deletions : 0 ,
149+ files : [
150+ {
151+ additions : 1 ,
152+ deletions : 0 ,
153+ patch : [
154+ "diff --git a/README.md b/README.md" ,
155+ "--- a/README.md" ,
156+ "+++ b/README.md" ,
157+ "@@ -1 +1,2 @@" ,
158+ " OpenClaw" ,
159+ "+Cold-open invariant" ,
160+ "" ,
161+ ] . join ( "\n" ) ,
162+ path : "README.md" ,
163+ status : "modified" ,
164+ } ,
165+ ] ,
166+ root : "/tmp/checkout" ,
167+ sessionKey : "main" ,
168+ } ,
169+ "sessions.files.list" : {
170+ browser : {
171+ entries : [ { kind : "file" , name : "README.md" , path : "README.md" } ] ,
172+ path : "" ,
173+ } ,
174+ files : [
175+ {
176+ kind : "modified" ,
177+ missing : false ,
178+ name : "README.md" ,
179+ path : "/tmp/checkout/README.md" ,
180+ size : 128 ,
181+ } ,
182+ ] ,
183+ gitCheckout : true ,
184+ root : "/tmp/checkout" ,
185+ sessionKey : "main" ,
186+ } ,
187+ "tasks.list" : {
188+ tasks : [
189+ {
190+ agentId : "main" ,
191+ createdAt : Date . now ( ) - 2_000 ,
192+ id : "task-sidebar-invariant" ,
193+ kind : "subagent" ,
194+ ownerKey : "main" ,
195+ progressSummary : "Checking every offered panel" ,
196+ runtime : "subagent" ,
197+ startedAt : Date . now ( ) - 1_000 ,
198+ status : "running" ,
199+ taskId : "task-sidebar-invariant" ,
200+ title : "Verify cold-open behavior" ,
201+ updatedAt : Date . now ( ) ,
202+ } ,
203+ ] ,
204+ } ,
205+ "terminal.open" : {
206+ agentId : "main" ,
207+ confined : false ,
208+ cwd : "/tmp/checkout" ,
209+ sessionId : "cold-open-terminal" ,
210+ shell : "/bin/zsh" ,
211+ } ,
212+ } ,
213+ workspace : "/tmp/checkout" ,
214+ workspaceGit : true ,
215+ } ;
216+ }
217+
218+ async function openColdSidebar ( page : Page , scenario = coldOpenScenario ( ) ) {
219+ await page . route ( "**/__openclaw__/assistant-media?*" , ( route ) =>
220+ route . fulfill ( { body : ONE_PIXEL_PNG , contentType : "image/png" } ) ,
221+ ) ;
222+ const gateway = await installMockGateway ( page , scenario ) ;
82223 await page . goto ( `${ suite . server . baseUrl } chat` ) ;
83224 await waitForControlUiGatewayReady ( page ) ;
84225 await gateway . waitForRequest ( "session.discussion.info" ) ;
226+ await gateway . waitForRequest ( "sessions.companion.state" ) ;
85227 await gateway . waitForRequest ( "sessions.files.list" ) ;
86228 await page . getByRole ( "button" , { name : "Side panel" , exact : true } ) . first ( ) . click ( ) ;
87229 const choices = page . locator ( ".side-panel-empty__type" ) ;
@@ -98,10 +240,44 @@ async function readColdOpenOutcome(page: Page): Promise<ColdOpenOutcome> {
98240 return {
99241 outcome : genericEmptyState ? "generic-empty" : "content" ,
100242 emptyStateOffersAction :
101- genericEmptyState && ( await emptyState . locator ( '[slot="action"]' ) . count ( ) ) > 0 ,
243+ genericEmptyState &&
244+ ( await activePanel . locator ( '[slot="action"], a[href], button:not([disabled])' ) . count ( ) ) > 0 ,
102245 } ;
103246}
104247
248+ async function offeredLabels ( page : Page , scenario : ControlUiMockGatewayScenario ) {
249+ const choices = await openColdSidebar ( page , scenario ) ;
250+ return choices . locator ( ".side-panel-type-option__label" ) . allTextContents ( ) ;
251+ }
252+
253+ async function readSlotColdOpenOutcome (
254+ label : OfferedSlotLabel ,
255+ scenario : ControlUiMockGatewayScenario ,
256+ expectedOutcome ?: ColdOpenOutcome [ "outcome" ] ,
257+ ) : Promise < ColdOpenOutcome > {
258+ const context = await suite . newBrowserContext ( { serviceWorkers : "block" } ) ;
259+ try {
260+ const page = await context . newPage ( ) ;
261+ const choices = await openColdSidebar ( page , scenario ) ;
262+ await choices . filter ( { hasText : label } ) . click ( ) ;
263+ if ( expectedOutcome ) {
264+ await expect
265+ . poll ( ( ) => readColdOpenOutcome ( page ) , { message : `${ label } cold-open outcome` } )
266+ . toMatchObject ( { outcome : expectedOutcome } ) ;
267+ } else {
268+ await page . evaluate (
269+ ( ) =>
270+ new Promise < void > ( ( resolve ) => {
271+ requestAnimationFrame ( ( ) => requestAnimationFrame ( ( ) => resolve ( ) ) ) ;
272+ } ) ,
273+ ) ;
274+ }
275+ return await readColdOpenOutcome ( page ) ;
276+ } finally {
277+ await suite . closeBrowserContext ( context ) ;
278+ }
279+ }
280+
105281suite . define ( ( ) => {
106282 it ( "preserves the production header-action shapes for Side chat and Discussion" , async ( ) => {
107283 const context = await suite . newBrowserContext ( { serviceWorkers : "block" } ) ;
@@ -126,28 +302,48 @@ suite.define(() => {
126302 await suite . closeBrowserContext ( context ) ;
127303 } ) ;
128304
129- it ( "accounts for every offered slot when opened cold " , async ( ) => {
305+ it ( "renders content for every offered slot with backing data " , async ( ) => {
130306 const probeContext = await suite . newBrowserContext ( { serviceWorkers : "block" } ) ;
131- const probePage = await probeContext . newPage ( ) ;
132- const choices = await openColdSidebar ( probePage ) ;
133- const offered = await choices . locator ( ".side-panel-type-option__label" ) . allTextContents ( ) ;
134- await suite . closeBrowserContext ( probeContext ) ;
135-
136- expect ( offered ) . toEqual ( Object . keys ( expectedColdOpenOutcomes ) ) ;
137-
138- const outcomes : Record < string , ColdOpenOutcome > = { } ;
139- for ( const label of offered ) {
140- const context = await suite . newBrowserContext ( { serviceWorkers : "block" } ) ;
141- const page = await context . newPage ( ) ;
142- const coldChoices = await openColdSidebar ( page ) ;
143- await coldChoices . filter ( { hasText : label } ) . click ( ) ;
144- await expect
145- . poll ( ( ) => readColdOpenOutcome ( page ) , { message : `${ label } cold-open outcome` } )
146- . toEqual ( expectedColdOpenOutcomes [ label ] ) ;
147- outcomes [ label ] = await readColdOpenOutcome ( page ) ;
148- await suite . closeBrowserContext ( context ) ;
307+ try {
308+ const offered = await offeredLabels (
309+ await probeContext . newPage ( ) ,
310+ populatedColdOpenScenario ( ) ,
311+ ) ;
312+ expect ( offered ) . toEqual ( offeredSlotLabels ) ;
313+ } finally {
314+ await suite . closeBrowserContext ( probeContext ) ;
315+ }
316+
317+ for ( const label of offeredSlotLabels ) {
318+ expect (
319+ await readSlotColdOpenOutcome ( label , populatedColdOpenScenario ( ) , "content" ) ,
320+ `${ label } must render content when its backing capability has data` ,
321+ ) . toEqual ( { outcome : "content" , emptyStateOffersAction : false } ) ;
322+ }
323+ } ) ;
324+
325+ it ( "keeps generic empty states actionable or explicitly allowlisted" , async ( ) => {
326+ const probeContext = await suite . newBrowserContext ( { serviceWorkers : "block" } ) ;
327+ try {
328+ const offered = await offeredLabels ( await probeContext . newPage ( ) , coldOpenScenario ( ) ) ;
329+ expect ( offered ) . toEqual ( offeredSlotLabels ) ;
330+ } finally {
331+ await suite . closeBrowserContext ( probeContext ) ;
332+ }
333+
334+ const observedActionlessEmptyStates : OfferedSlotLabel [ ] = [ ] ;
335+ for ( const label of offeredSlotLabels ) {
336+ const outcome = await readSlotColdOpenOutcome ( label , coldOpenScenario ( ) ) ;
337+ if ( outcome . outcome !== "generic-empty" || outcome . emptyStateOffersAction ) {
338+ continue ;
339+ }
340+ expect (
341+ actionlessEmptyStateAllowlist . has ( label ) ,
342+ `${ label } renders the generic empty state without an action` ,
343+ ) . toBe ( true ) ;
344+ observedActionlessEmptyStates . push ( label ) ;
149345 }
150346
151- expect ( outcomes ) . toEqual ( expectedColdOpenOutcomes ) ;
347+ expect ( observedActionlessEmptyStates ) . toEqual ( [ ... actionlessEmptyStateAllowlist ] ) ;
152348 } ) ;
153349} ) ;
0 commit comments