11import { expect , test } from "bun:test"
2- import { InputRenderable } from "@opentui/core"
2+ import { InputRenderable , TextareaRenderable } from "@opentui/core"
33import path from "node:path"
4+ import { takeDraft } from "../../src/component/prompt/draft-stash"
45import { createAppFixture } from "../fixture/app"
56import { tmpdir } from "../fixture/fixture"
67import { json , worktree } from "../fixture/tui-client"
@@ -10,9 +11,9 @@ const beta = "/tmp/wt-beta"
1011const project = { id : "proj_test" , directory : worktree , canonical : worktree }
1112
1213test ( "switching Home between worktrees loads the branch of each" , async ( ) => {
14+ takeDraft ( undefined )
1315 await using state = await tmpdir ( )
1416 const vcs : string [ ] = [ ]
15- let worktreeReads = 0
1617 await using setup = await createAppFixture ( {
1718 state : state . path ,
1819 width : 120 ,
@@ -24,29 +25,22 @@ test("switching Home between worktrees loads the branch of each", async () => {
2425 const branch = path . basename ( directory ) . replace ( "wt-" , "" )
2526 return json ( { location : { directory, project } , data : { branch : { current : branch , default : "main" } } } )
2627 }
27- if ( url . pathname === "/api/worktree" && request . method === "GET" ) {
28- worktreeReads ++
28+ if ( url . pathname === "/api/worktree" && request . method === "GET" )
2929 return json ( [
3030 { directory : worktree } ,
3131 { directory : alpha , strategy : "git" } ,
3232 { directory : beta , strategy : "git" } ,
3333 ] )
34- }
3534 } ,
3635 } )
3736 await setup . ready
3837
39- // Frame waits give up after a fixed number of rendered frames, so wait for the server to answer first: on
40- // slow runners (Windows) the location and vcs round trips a worktree switch needs can outlast that budget.
41- async function served ( check : ( ) => boolean ) {
42- while ( ! check ( ) ) await Bun . sleep ( 10 )
43- }
44-
4538 async function select ( filter : string ) {
46- const reads = worktreeReads
39+ // Keys go to whatever is focused, so wait for Home's prompt to mount and take focus before typing.
40+ await setup . waitFor ( ( ) => setup . renderer . currentFocusedEditor instanceof TextareaRenderable )
4741 await setup . mockInput . typeText ( "/worktrees" )
42+ await setup . waitForFrame ( ( frame ) => frame . includes ( "/worktrees" ) )
4843 setup . mockInput . pressEnter ( )
49- await served ( ( ) => worktreeReads > reads )
5044 await setup . waitForFrame ( ( frame ) => frame . includes ( alpha ) && frame . includes ( beta ) )
5145 await setup . waitFor ( ( ) => setup . renderer . currentFocusedRenderable instanceof InputRenderable )
5246 await setup . mockInput . typeText ( filter )
@@ -55,10 +49,8 @@ test("switching Home between worktrees loads the branch of each", async () => {
5549 }
5650
5751 await select ( "alpha" )
58- await served ( ( ) => vcs . includes ( alpha ) )
5952 await setup . waitForFrame ( ( frame ) => frame . includes ( `${ alpha } :alpha` ) )
6053 await select ( "beta" )
61- await served ( ( ) => vcs . includes ( beta ) )
6254 const frame = await setup . waitForFrame ( ( frame ) => frame . includes ( `${ beta } :beta` ) )
6355
6456 expect ( frame ) . toContain ( `${ beta } :beta` )
0 commit comments