11import { createServer as createHttpServer } from "node:http"
22import { createRequire } from "node:module"
33import { fileURLToPath } from "node:url"
4+ import { mkdtemp , rm } from "node:fs/promises"
5+ import { tmpdir } from "node:os"
6+ import path from "node:path"
47import { createServer } from "vite"
58
6- const { chromium, expect } = createRequire ( new URL ( "../../app/package.json" , import . meta. url ) ) ( "@playwright/test" )
9+ const { chromium, _electron , expect } = createRequire ( new URL ( "../../app/package.json" , import . meta. url ) ) ( "@playwright/test" )
710const directory = "/attention/root"
811const childDirectory = "/attention/child"
912const root = { id : "ses_root" , title : "Attention root" , directory, projectID : "fixture" , agent : "build" , version : "1" , time : { created : 1 , updated : 2 } }
1013const child = { ...root , id : "ses_child" , title : "Child" , directory : childDirectory , parentID : root . id }
1114const rows = [ root , child ]
1215const permission = { id : "per_child" , sessionID : child . id , permission : "bash" , patterns : [ "fixture command" ] , metadata : { } , always : [ ] }
16+ const question = { id : "que_child" , sessionID : child . id ,
17+ questions : [ { header : "Confirm" , question : "Continue?" , options : [ { label : "Yes" , description : "Yes" } ] } ] }
1318let notifications = [ { id : `idle:${ root . id } ` , kind : "idle" , session : root . id , title : root . title , directory, editspace : "fixture" , updated : Date . now ( ) - 1000 } ]
1419let statuses = { [ root . id ] : { type : "busy" } }
1520let permissions = [ ]
21+ let questions = [ ]
1622const streams = new Set ( )
1723const failures = [ ]
1824const server = createHttpServer ( async ( req , res ) => {
@@ -26,13 +32,19 @@ const server = createHttpServer(async (req, res) => {
2632 const stream = { path, dir, res } ; streams . add ( stream ) ; req . on ( "close" , ( ) => streams . delete ( stream ) ) ; return
2733 }
2834 if ( path === "/api/editspaces" ) return json ( { editspaces : [ { name : "fixture" , root : directory } ] , default : "fixture" } )
35+ if ( path === "/experimental/capabilities" ) return json ( { } )
36+ if ( path === "/config/providers" ) return json ( { providers : [ { id : "fixture" , models : { test : { id : "test" , name : "Fixture model" } } } ] , default : { fixture : "test" } } )
37+ if ( path === "/agent" ) return json ( [ { name : "build" , mode : "primary" } ] )
2938 if ( path === "/api/notifications" ) return json ( { notifications } )
3039 if ( path === "/api/state" ) return json ( { editspace : "fixture" , root : directory , threads : [ { kind : "session" , key : root . id , title : root . title , sessions : [ { ...root , updated : 2 , live : "busy" } ] , lanes : [ ] , tickets : [ ] , prs : [ ] } ] , attention : [ ] , frontier : [ ] , unattached_prs : [ ] } )
3140 if ( path === "/experimental/session" ) return json ( rows )
3241 if ( path === "/session" ) return json ( rows . filter ( ( s ) => s . directory === dir ) )
3342 if ( path === "/session/status" ) return json ( Object . fromEntries ( rows . filter ( ( s ) => s . directory === dir && statuses [ s . id ] ) . map ( ( s ) => [ s . id , statuses [ s . id ] ] ) ) )
3443 if ( path === "/permission" ) return json ( permissions . filter ( ( p ) => rows . find ( ( s ) => s . id === p . sessionID ) ?. directory === dir ) )
35- if ( path === "/question" ) return json ( [ ] )
44+ if ( path === "/question" ) return json ( questions . filter ( ( q ) => rows . find ( ( s ) => s . id === q . sessionID ) ?. directory === dir ) )
45+ if ( path === `/session/${ root . id } ` ) return json ( root )
46+ if ( path === `/session/${ root . id } /message` ) return json ( [ ] )
47+ if ( path === `/session/${ root . id } /input` ) return json ( { items : [ ] , next : null } )
3648 if ( path === "/api/issues" ) return json ( { backend : "gh" , repo : "fixture/repo" , issues : [ ] } )
3749 if ( path === "/api/docs" ) return json ( { sources : [ ] , roots : [ ] } )
3850 failures . push ( `Unexpected read ${ path } ` ) ; return json ( { } , 404 )
@@ -43,16 +55,21 @@ process.env.OPENCODE_URL = fixtureUrl
4355process . env . ES_DASHBOARD_URL = fixtureUrl
4456const vite = await createServer ( { root : fileURLToPath ( new URL ( ".." , import . meta. url ) ) , server : { host : "127.0.0.1" , port : 0 } } )
4557await vite . listen ( )
46- const origin = `http://127.0.0.1:${ vite . httpServer . address ( ) . port } `
47- const browser = await chromium . launch ( { headless : true , channel : "chrome" } )
48- const context = await browser . newContext ( )
58+ const profile = process . env . ES_DESKTOP_EXECUTABLE ? await mkdtemp ( path . join ( tmpdir ( ) , "attention-native-" ) ) : undefined
59+ const app = profile ? await _electron . launch ( { executablePath : process . env . ES_DESKTOP_EXECUTABLE ,
60+ env : { ...process . env , ELECTRON_RUN_AS_NODE : "" , ES_DESKTOP_USER_DATA : profile , ES_DESKTOP_RENDERER : "" ,
61+ OPENCODE_URL : fixtureUrl , ES_DASHBOARD_URL : fixtureUrl , OPENCODE_SERVER_PASSWORD : "" } } ) : undefined
62+ const browser = app ? undefined : await chromium . launch ( { headless : true , channel : "chrome" } )
63+ const page = app ? await app . firstWindow ( ) : await browser . newPage ( )
64+ if ( app ) await page . waitForURL ( "http://127.0.0.1:*/" )
65+ const origin = app ? new URL ( page . url ( ) ) . origin : `http://127.0.0.1:${ vite . httpServer . address ( ) . port } `
66+ const context = page . context ( )
4967await context . addInitScript ( ( ) => localStorage . setItem ( "es-app-all-projects" , "true" ) )
5068await context . route ( "**/*" , ( route ) => {
5169 const url = new URL ( route . request ( ) . url ( ) )
5270 if ( url . origin === origin || [ "data:" , "blob:" ] . includes ( url . protocol ) ) return route . continue ( )
5371 failures . push ( `External request ${ url . origin } ` ) ; return route . abort ( )
5472} )
55- const page = await context . newPage ( )
5673page . on ( "pageerror" , ( e ) => failures . push ( e . message ) )
5774const emit = ( directory , event ) => {
5875 for ( const stream of streams ) {
@@ -62,30 +79,55 @@ const emit = (directory, event) => {
6279}
6380try {
6481 await page . goto ( origin )
65- await expect ( page . locator ( '.sidebar > a[href^="/session/ses_root"] .dot' ) ) . toHaveClass ( / b u s y / )
82+ const rootLink = page . getByRole ( "link" , { name : / A t t e n t i o n r o o t / } ) . first ( )
83+ await expect ( rootLink . locator ( ".dot" ) ) . toHaveClass ( / b u s y / )
6684 await expect ( page . locator ( ".notification-item" ) ) . toHaveCount ( 0 )
6785 console . log ( "PASS busy/no-needs does not enter Needs You despite a retained idle record" )
6886 permissions = [ permission ]
6987 emit ( childDirectory , { type : "permission.asked" , properties : permission } )
7088 await expect ( page . locator ( ".notification-item" ) ) . toHaveCount ( 1 )
7189 await expect ( page . locator ( ".notification-item" ) ) . toHaveAttribute ( "href" , `/session/${ root . id } ?directory=${ encodeURIComponent ( directory ) } ` )
90+ await page . locator ( ".es-switcher" ) . selectOption ( "fixture" )
91+ await expect ( page . locator ( ".notification-item" ) ) . toHaveCount ( 1 )
92+ await page . locator ( ".es-switcher" ) . selectOption ( "__all__" )
93+ await expect ( page . locator ( ".notification-item" ) ) . toHaveCount ( 1 )
7294 console . log ( "PASS busy parent retains genuine blocked descendant attention" )
7395 notifications = [ { id : `permission:${ permission . id } ` , kind : "permission" , session : child . id , title : child . title , directory : childDirectory , editspace : "fixture" , updated : Date . now ( ) } ]
7496 permissions = [ ]
7597 emit ( childDirectory , { type : "permission.replied" , properties : { sessionID : child . id , requestID : permission . id , reply : "once" } } )
7698 await expect ( page . locator ( ".notification-item" ) ) . toHaveCount ( 0 )
7799 console . log ( "PASS replied descendant gate removes stale dashboard attention" )
100+ questions = [ question ]
101+ emit ( childDirectory , { type : "question.asked" , properties : question } )
102+ await expect ( page . locator ( ".notification-item" ) ) . toHaveCount ( 1 )
103+ await expect ( page . locator ( ".notification-item" ) ) . toHaveAttribute ( "href" , `/session/${ root . id } ?directory=${ encodeURIComponent ( directory ) } ` )
104+ questions = [ ]
105+ emit ( childDirectory , { type : "question.replied" , properties : { sessionID : child . id , requestID : question . id , answers : [ [ "Yes" ] ] } } )
106+ await expect ( page . locator ( ".notification-item" ) ) . toHaveCount ( 0 )
107+ console . log ( "PASS offscreen child question routes to root and clears on reply" )
78108 statuses = { [ child . id ] : { type : "busy" } }
79109 notifications = [ { id : `idle:${ root . id } ` , kind : "idle" , session : root . id , title : root . title , directory, editspace : "fixture" , updated : Date . now ( ) } ]
80110 emit ( directory , { type : "session.status" , properties : { sessionID : root . id , status : { type : "idle" } } } )
81111 emit ( childDirectory , { type : "session.status" , properties : { sessionID : child . id , status : { type : "busy" } } } )
82112 await expect ( page . locator ( ".notification-item" ) ) . toHaveCount ( 0 )
83113 console . log ( "PASS busy descendant alone is activity, not attention" )
114+ questions = [ { ...question , id : "que_missed" } ]
115+ for ( const stream of streams ) if ( stream . path === "/global/event" ) stream . res . end ( )
116+ await expect ( page . locator ( ".notification-item" ) ) . toHaveCount ( 1 , { timeout : 10_000 } )
117+ const actionable = page . locator ( ".notification-item" )
118+ await expect ( actionable ) . toHaveAttribute ( "href" , `/session/${ root . id } ?directory=${ encodeURIComponent ( directory ) } ` )
119+ await actionable . focus ( )
120+ await expect ( actionable ) . toBeFocused ( )
121+ await page . keyboard . press ( "Enter" )
122+ await expect ( page ) . toHaveURL ( `${ origin } /session/${ root . id } ?directory=${ encodeURIComponent ( directory ) } ` )
123+ console . log ( "PASS missed offscreen question recovered after reconnect; keyboard opens parent" )
84124 expect ( failures ) . toEqual ( [ ] )
85125} finally {
86- await browser . close ( )
126+ await app ?. close ( )
127+ await browser ?. close ( )
87128 for ( const stream of streams ) stream . res . end ( )
88129 server . closeAllConnections ( )
89130 await new Promise ( ( resolve ) => server . close ( resolve ) )
90131 await vite . close ( )
132+ if ( profile ) await rm ( profile , { recursive : true , force : true } )
91133}
0 commit comments