11import { expect , test } from "bun:test"
2+ import type { Interface } from "../src/config"
23import { createAppFixture } from "./fixture/app"
34import { tmpdir } from "./fixture/fixture"
45import path from "node:path"
56
7+ function configService ( file : string ) : Interface {
8+ let diagnostic : string | undefined
9+ return {
10+ path : file ,
11+ async get ( ) {
12+ const value = await Bun . file ( file ) . json ( )
13+ diagnostic = typeof value . leader === "string" ? `Invalid CLI config at ${ file } : leader. File ignored.` : undefined
14+ return diagnostic ? { } : value
15+ } ,
16+ diagnostic : ( ) => diagnostic ,
17+ async update ( ) {
18+ return { }
19+ } ,
20+ }
21+ }
22+
623test ( "renders the rejected CLI config warning in the TUI" , async ( ) => {
724 await using setup = await createAppFixture ( {
825 diagnostic : "Invalid CLI config at /tmp/cli.json: leader" ,
@@ -19,49 +36,32 @@ test("does not render a config warning for valid settings", async () => {
1936 expect ( setup . captureCharFrame ( ) ) . not . toContain ( "Invalid CLI config" )
2037} )
2138
22- test ( "shows a live invalid file warning once and recovers on a valid reload " , async ( ) => {
39+ test ( "shows a live invalid file warning and clears it after a valid edit " , async ( ) => {
2340 await using directory = await tmpdir ( )
2441 const file = path . join ( directory . path , "cli.json" )
2542 await Bun . write ( file , JSON . stringify ( { animations : false , mouse : false } ) )
26- let diagnostic : string | undefined
27- let reads = 0
28- let checks = 0
29- const service = {
30- path : file ,
31- async get ( ) {
32- reads ++
33- const value = await Bun . file ( file ) . json ( )
34- diagnostic = typeof value . leader === "string" ? `Invalid CLI config at ${ file } : leader. File ignored.` : undefined
35- return diagnostic ? { } : value
36- } ,
37- diagnostic : ( ) => {
38- checks ++
39- return diagnostic
40- } ,
41- async update ( ) {
42- return { }
43- } ,
44- }
45- await using setup = await createAppFixture ( { service } )
43+ await using setup = await createAppFixture ( { service : configService ( file ) } )
4644 await setup . ready
4745 await setup . renderOnce ( )
4846 expect ( setup . captureCharFrame ( ) ) . not . toContain ( "Invalid CLI config" )
4947
5048 await Bun . write ( file , JSON . stringify ( { animations : false , mouse : false , leader : "bad" } ) )
5149 await setup . waitForFrame ( ( frame ) => frame . includes ( "Invalid CLI config" ) , { maxPasses : 100 } )
5250 expect ( setup . captureCharFrame ( ) ) . toContain ( "leader" )
53- const afterInvalid = reads
54-
55- await Bun . write ( file , JSON . stringify ( { animations : false , mouse : false , leader : "bad" } ) )
56- await setup . waitForFrame ( ( ) => reads > afterInvalid , { maxPasses : 100 } )
57- expect ( setup . captureCharFrame ( ) ) . not . toContain ( "more" )
5851
5952 await Bun . write ( file , JSON . stringify ( { animations : false , mouse : true } ) )
60- await setup . waitForFrame ( ( ) => diagnostic === undefined && reads > afterInvalid + 1 , { maxPasses : 100 } )
61- await setup . waitForFrame ( ( frame ) => ! frame . includes ( "Invalid CLI config" ) )
53+ await setup . waitForFrame ( ( frame ) => ! frame . includes ( "Invalid CLI config" ) , { maxPasses : 100 } )
54+ } )
55+
56+ test ( "keeps an unrelated toast when the config warning clears" , async ( ) => {
57+ await using directory = await tmpdir ( )
58+ const file = path . join ( directory . path , "cli.json" )
59+ await Bun . write ( file , JSON . stringify ( { animations : false , leader : "bad" } ) )
60+ const service = configService ( file )
61+ await using setup = await createAppFixture ( { service } )
62+ await setup . ready
63+ await setup . waitForFrame ( ( frame ) => frame . includes ( "Invalid CLI config" ) )
6264
63- await Bun . write ( file , JSON . stringify ( { animations : false , mouse : true , leader : "bad" } ) )
64- await setup . waitForFrame ( ( frame ) => frame . includes ( "Invalid CLI config" ) , { maxPasses : 100 } )
6565 setup . events . emit ( {
6666 id : "evt_other_toast" ,
6767 created : 1 ,
@@ -70,9 +70,8 @@ test("shows a live invalid file warning once and recovers on a valid reload", as
7070 data : { message : "Other notification" , variant : "info" , duration : 15_000 } ,
7171 } )
7272 await setup . waitForFrame ( ( frame ) => frame . includes ( "Other notification" ) )
73- const beforeRecovery = checks
74- await Bun . write ( file , JSON . stringify ( { animations : false , mouse : true } ) )
75- await setup . waitForFrame ( ( ) => checks > beforeRecovery && diagnostic === undefined , { maxPasses : 100 } )
76- await setup . renderOnce ( )
77- expect ( setup . captureCharFrame ( ) ) . toContain ( "Other notification" )
73+ await Bun . write ( file , JSON . stringify ( { animations : false } ) )
74+ await setup . waitForFrame ( ( frame ) => service . diagnostic ?.( ) === undefined && frame . includes ( "Other notification" ) , {
75+ maxPasses : 100 ,
76+ } )
7877} )
0 commit comments