@@ -33,6 +33,7 @@ vi.mock("./server-methods/sessions-files.js", () => ({
3333const {
3434 clearWorkspaceIconCacheForTest,
3535 handleWorkspaceIconHttpRequest,
36+ prepareSessionWorkspaceIcon,
3637 resolveWorkspaceIcon,
3738 SVG_ICON_MAX_BYTES ,
3839 WORKSPACE_ICON_MAX_BYTES ,
@@ -73,22 +74,35 @@ afterEach(async () => {
7374
7475describe ( "resolveWorkspaceIcon" , ( ) => {
7576 const conventions = [
77+ { relative : "favicon.svg" , body : SVG_BYTES , contentType : "image/svg+xml" } ,
78+ { relative : "favicon.ico" , body : ICO_BYTES , contentType : "image/x-icon" } ,
79+ { relative : "favicon.png" , body : PNG_BYTES , contentType : "image/png" } ,
7680 { relative : "public/favicon.svg" , body : SVG_BYTES , contentType : "image/svg+xml" } ,
7781 { relative : "public/favicon.ico" , body : ICO_BYTES , contentType : "image/x-icon" } ,
7882 { relative : "public/favicon.png" , body : PNG_BYTES , contentType : "image/png" } ,
83+ { relative : "public/favicon-32.png" , body : PNG_BYTES , contentType : "image/png" } ,
7984 { relative : "public/apple-touch-icon.png" , body : PNG_BYTES , contentType : "image/png" } ,
8085 { relative : "static/favicon.svg" , body : SVG_BYTES , contentType : "image/svg+xml" } ,
8186 { relative : "static/favicon.ico" , body : ICO_BYTES , contentType : "image/x-icon" } ,
8287 { relative : "static/favicon.png" , body : PNG_BYTES , contentType : "image/png" } ,
88+ { relative : "ui/public/favicon-32.png" , body : PNG_BYTES , contentType : "image/png" } ,
89+ { relative : "ui/public/favicon.svg" , body : SVG_BYTES , contentType : "image/svg+xml" } ,
90+ { relative : "ui/public/favicon.ico" , body : ICO_BYTES , contentType : "image/x-icon" } ,
91+ { relative : "ui/public/favicon.png" , body : PNG_BYTES , contentType : "image/png" } ,
92+ { relative : "app/favicon.ico" , body : ICO_BYTES , contentType : "image/x-icon" } ,
93+ { relative : "app/favicon.png" , body : PNG_BYTES , contentType : "image/png" } ,
8394 { relative : "app/icon.svg" , body : SVG_BYTES , contentType : "image/svg+xml" } ,
8495 { relative : "app/icon.png" , body : PNG_BYTES , contentType : "image/png" } ,
85- { relative : "app/favicon.ico" , body : ICO_BYTES , contentType : "image/x-icon" } ,
96+ { relative : "app/icon.ico" , body : ICO_BYTES , contentType : "image/x-icon" } ,
97+ { relative : "src/favicon.ico" , body : ICO_BYTES , contentType : "image/x-icon" } ,
98+ { relative : "src/favicon.svg" , body : SVG_BYTES , contentType : "image/svg+xml" } ,
99+ { relative : "src/app/favicon.ico" , body : ICO_BYTES , contentType : "image/x-icon" } ,
86100 { relative : "src/app/icon.svg" , body : SVG_BYTES , contentType : "image/svg+xml" } ,
87101 { relative : "src/app/icon.png" , body : PNG_BYTES , contentType : "image/png" } ,
88- { relative : "src/app/favicon.ico " , body : ICO_BYTES , contentType : "image/x-icon " } ,
89- { relative : "favicon.svg " , body : SVG_BYTES , contentType : "image/svg+xml " } ,
90- { relative : "favicon.ico " , body : ICO_BYTES , contentType : "image/x-icon " } ,
91- { relative : "favicon .png" , body : PNG_BYTES , contentType : "image/png" } ,
102+ { relative : "assets/icon.svg " , body : SVG_BYTES , contentType : "image/svg+xml " } ,
103+ { relative : "assets/icon.png " , body : PNG_BYTES , contentType : "image/png " } ,
104+ { relative : "assets/logo.svg " , body : SVG_BYTES , contentType : "image/svg+xml " } ,
105+ { relative : "assets/logo .png" , body : PNG_BYTES , contentType : "image/png" } ,
92106 ] as const ;
93107
94108 it . each ( conventions ) ( "resolves $relative as $contentType" , async ( convention ) => {
@@ -99,16 +113,17 @@ describe("resolveWorkspaceIcon", () => {
99113 expect ( icon ?. etag ) . toMatch ( / ^ " [ \w - ] + " $ / u) ;
100114 } ) ;
101115
102- it ( "prefers the framework-specific location over a bare root favicon " , async ( ) => {
116+ it ( "uses the first valid icon in the fixed precedence " , async ( ) => {
103117 const root = await makeWorkspace ( {
104118 "favicon.ico" : ICO_BYTES ,
105119 "public/favicon.svg" : SVG_BYTES ,
120+ "ui/public/favicon-32.png" : PNG_BYTES ,
106121 } ) ;
107- expect ( ( await resolveWorkspaceIcon ( root ) ) ?. contentType ) . toBe ( "image/svg+xml " ) ;
122+ expect ( ( await resolveWorkspaceIcon ( root ) ) ?. contentType ) . toBe ( "image/x-icon " ) ;
108123 } ) ;
109124
110125 const rejected = [
111- { label : "an unconventional location" , files : { "assets /favicon.ico " : ICO_BYTES } } ,
126+ { label : "an unconventional location" , files : { "vendor /favicon.png " : PNG_BYTES } } ,
112127 { label : "an empty file" , files : { "favicon.ico" : Buffer . alloc ( 0 ) } } ,
113128 { label : "bytes that are not an image" , files : { "favicon.ico" : Buffer . from ( "#!/bin/sh\n" ) } } ,
114129 {
@@ -212,6 +227,7 @@ describe("handleWorkspaceIconHttpRequest", () => {
212227 it ( "serves the session workspace icon with sandboxed asset headers" , async ( ) => {
213228 const root = await makeWorkspace ( { "public/favicon.ico" : ICO_BYTES } ) ;
214229 mocks . resolveLocalSessionWorkspaceRoot . mockReturnValue ( root ) ;
230+ await prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:one" } ) ;
215231
216232 const response = await fetch ( iconRoute ( "agent:main:one" ) ) ;
217233 expect ( mocks . resolveLocalSessionWorkspaceRoot ) . toHaveBeenCalledWith ( {
@@ -233,6 +249,7 @@ describe("handleWorkspaceIconHttpRequest", () => {
233249 it ( "revalidates an unchanged icon without resending its bytes" , async ( ) => {
234250 const root = await makeWorkspace ( { "favicon.png" : PNG_BYTES } ) ;
235251 mocks . resolveLocalSessionWorkspaceRoot . mockReturnValue ( root ) ;
252+ await prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:one" } ) ;
236253
237254 const first = await fetch ( iconRoute ( "agent:main:one" ) ) ;
238255 const etag = first . headers . get ( "etag" ) ;
@@ -249,6 +266,7 @@ describe("handleWorkspaceIconHttpRequest", () => {
249266 it ( "omits the body but keeps the representation headers on HEAD" , async ( ) => {
250267 const root = await makeWorkspace ( { "favicon.png" : PNG_BYTES } ) ;
251268 mocks . resolveLocalSessionWorkspaceRoot . mockReturnValue ( root ) ;
269+ await prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:one" } ) ;
252270
253271 const response = await fetch ( iconRoute ( "agent:main:one" ) , { method : "HEAD" } ) ;
254272 expect ( response . status ) . toBe ( 200 ) ;
@@ -265,11 +283,77 @@ describe("handleWorkspaceIconHttpRequest", () => {
265283 mocks . resolveLocalSessionWorkspaceRoot . mockReturnValue (
266284 hasWorkspace ? await makeWorkspace ( { } ) : undefined ,
267285 ) ;
286+ await prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:one" } ) ;
268287 const response = await fetch ( iconRoute ( "agent:main:one" ) ) ;
269288 expect ( response . status ) . toBe ( 404 ) ;
270289 expect ( response . headers . get ( "cache-control" ) ) . toBe ( "no-store" ) ;
271290 } ) ;
272291
292+ it ( "keeps a request made before chat startup retryable" , async ( ) => {
293+ const response = await fetch ( iconRoute ( "agent:main:one" ) ) ;
294+ expect ( response . status ) . toBe ( 503 ) ;
295+ expect ( response . headers . get ( "cache-control" ) ) . toBe ( "no-store" ) ;
296+ expect ( response . headers . get ( "retry-after" ) ) . toBe ( "1" ) ;
297+ expect ( mocks . resolveLocalSessionWorkspaceRoot ) . not . toHaveBeenCalled ( ) ;
298+ } ) ;
299+
300+ it ( "waits for preparation already started by chat startup" , async ( ) => {
301+ const root = await makeWorkspace ( { "public/favicon.ico" : ICO_BYTES } ) ;
302+ mocks . resolveLocalSessionWorkspaceRoot . mockReturnValue ( root ) ;
303+
304+ const preparation = prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:pending" } ) ;
305+ const responsePromise = fetch ( iconRoute ( "agent:main:pending" ) ) ;
306+
307+ await preparation ;
308+ const response = await responsePromise ;
309+ expect ( response . status ) . toBe ( 200 ) ;
310+ expect ( Buffer . from ( await response . arrayBuffer ( ) ) . equals ( ICO_BYTES ) ) . toBe ( true ) ;
311+ } ) ;
312+
313+ it ( "records the fallback when preparation fails" , async ( ) => {
314+ mocks . resolveLocalSessionWorkspaceRoot . mockImplementation ( ( ) => {
315+ throw new Error ( "broken workspace metadata" ) ;
316+ } ) ;
317+ await expect ( prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:broken" } ) ) . rejects . toThrow (
318+ "broken workspace metadata" ,
319+ ) ;
320+
321+ const response = await fetch ( iconRoute ( "agent:main:broken" ) ) ;
322+ expect ( response . status ) . toBe ( 404 ) ;
323+ } ) ;
324+
325+ it ( "does no session-store or filesystem resolution in the HTTP request" , async ( ) => {
326+ const root = await makeWorkspace ( { "ui/public/favicon-32.png" : PNG_BYTES } ) ;
327+ mocks . resolveLocalSessionWorkspaceRoot . mockReturnValue ( root ) ;
328+ await prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:one" } ) ;
329+ mocks . resolveLocalSessionWorkspaceRoot . mockClear ( ) ;
330+ await fs . rm ( path . join ( root , "ui/public/favicon-32.png" ) ) ;
331+
332+ const first = await fetch ( iconRoute ( "agent:main:one" ) ) ;
333+ const second = await fetch ( iconRoute ( "agent:main:one" ) ) ;
334+
335+ expect ( first . status ) . toBe ( 200 ) ;
336+ expect ( second . status ) . toBe ( 200 ) ;
337+ expect ( Buffer . from ( await first . arrayBuffer ( ) ) . equals ( PNG_BYTES ) ) . toBe ( true ) ;
338+ expect ( Buffer . from ( await second . arrayBuffer ( ) ) . equals ( PNG_BYTES ) ) . toBe ( true ) ;
339+ expect ( mocks . resolveLocalSessionWorkspaceRoot ) . not . toHaveBeenCalled ( ) ;
340+ } ) ;
341+
342+ it ( "keeps a recently served session snapshot across bounded-cache eviction" , async ( ) => {
343+ const root = await makeWorkspace ( { "favicon.png" : PNG_BYTES } ) ;
344+ mocks . resolveLocalSessionWorkspaceRoot . mockReturnValue ( root ) ;
345+ await prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:kept" } ) ;
346+ for ( let index = 0 ; index < 127 ; index += 1 ) {
347+ await prepareSessionWorkspaceIcon ( { sessionKey : `agent:main:filler-${ index } ` } ) ;
348+ }
349+
350+ expect ( ( await fetch ( iconRoute ( "agent:main:kept" ) ) ) . status ) . toBe ( 200 ) ;
351+ await prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:newest" } ) ;
352+
353+ expect ( ( await fetch ( iconRoute ( "agent:main:kept" ) ) ) . status ) . toBe ( 200 ) ;
354+ expect ( ( await fetch ( iconRoute ( "agent:main:filler-0" ) ) ) . status ) . toBe ( 503 ) ;
355+ } ) ;
356+
273357 const malformed = [ "/__openclaw__/workspace-icon/" , "/__openclaw__/workspace-icon/a/b" ] ;
274358
275359 it . each ( malformed ) ( "claims %s as a 404 instead of falling through" , async ( pathname ) => {
@@ -316,6 +400,7 @@ describe("handleWorkspaceIconHttpRequest", () => {
316400 // resolveLocalSessionWorkspaceRoot withholds the root for exec-node sessions
317401 // so the route can never answer with this Gateway's own project icon.
318402 mocks . resolveLocalSessionWorkspaceRoot . mockReturnValue ( undefined ) ;
403+ await prepareSessionWorkspaceIcon ( { sessionKey : "agent:main:remote" } ) ;
319404 const response = await fetch ( iconRoute ( "agent:main:remote" ) ) ;
320405 expect ( response . status ) . toBe ( 404 ) ;
321406 expect ( response . headers . get ( "cache-control" ) ) . toBe ( "no-store" ) ;
0 commit comments