Skip to content

Commit 49e2920

Browse files
committed
test(tui): simplify config diagnostic reload coverage
1 parent 0562f78 commit 49e2920

2 files changed

Lines changed: 34 additions & 103 deletions

File tree

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
import { expect, test } from "bun:test"
2+
import type { Interface } from "../src/config"
23
import { createAppFixture } from "./fixture/app"
34
import { tmpdir } from "./fixture/fixture"
45
import 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+
623
test("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
})

‎packages/tui/test/config-v2.test.tsx‎

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
import { testRender } from "@opentui/solid"
33
import { expect, test } from "bun:test"
44
import { Schema } from "effect"
5-
import { createEffect } from "solid-js"
65
import { resolve, ConfigProvider, Info, useConfig, type Interface } from "../src/config"
76
import { settings } from "../src/component/dialog-config"
87
import { TuiKeybind } from "../src/config/keybind"
98
import { CommandMap, Definitions } from "../src/config/v1/keybind"
10-
import { tmpdir } from "./fixture/fixture"
11-
import path from "node:path"
129

1310
const decodeInfo = Schema.decodeUnknownSync(Info)
1411

@@ -327,68 +324,3 @@ test("provides config and its host interface", async () => {
327324
app.renderer.destroy()
328325
}
329326
})
330-
331-
test("shows the fallback settings on invalid reload and applies the next valid edit", async () => {
332-
await using directory = await tmpdir()
333-
const file = path.join(directory.path, "cli.json")
334-
await Bun.write(file, JSON.stringify({ mouse: false }))
335-
let diagnostic: string | undefined
336-
let reads = 0
337-
const service: Interface = {
338-
path: file,
339-
async get() {
340-
reads++
341-
const value = await Bun.file(file).json()
342-
diagnostic = typeof value.leader === "string" ? "Invalid CLI config: leader" : undefined
343-
return diagnostic ? {} : value
344-
},
345-
diagnostic: () => diagnostic,
346-
async update() {
347-
return {}
348-
},
349-
}
350-
let context: ReturnType<typeof useConfig> | undefined
351-
let warnings = 0
352-
function Consumer() {
353-
context = useConfig()
354-
createEffect(() => {
355-
if (context?.diagnostic()) warnings++
356-
})
357-
return <text>{context.data.mouse ? "mouse on" : "mouse off"}</text>
358-
}
359-
const initial = await service.get()
360-
const app = await testRender(() => (
361-
<ConfigProvider config={resolve(initial, { terminalSuspend: true })} service={service}>
362-
<Consumer />
363-
</ConfigProvider>
364-
))
365-
try {
366-
await app.renderOnce()
367-
expect(app.captureCharFrame()).toContain("mouse off")
368-
369-
await Bun.write(file, JSON.stringify({ mouse: true, leader: "bad" }))
370-
const invalidDeadline = Date.now() + 2_000
371-
while (context?.diagnostic() === undefined && Date.now() < invalidDeadline) await Bun.sleep(10)
372-
expect(context?.diagnostic()).toContain("leader")
373-
await app.renderOnce()
374-
expect(app.captureCharFrame()).toContain("mouse on")
375-
expect(warnings).toBe(1)
376-
377-
const invalidReads = reads
378-
await Bun.write(file, JSON.stringify({ mouse: true, leader: "bad" }))
379-
const repeatDeadline = Date.now() + 2_000
380-
while (reads === invalidReads && Date.now() < repeatDeadline) await Bun.sleep(10)
381-
expect(reads).toBeGreaterThan(invalidReads)
382-
await app.renderOnce()
383-
expect(warnings).toBe(1)
384-
385-
await Bun.write(file, JSON.stringify({ mouse: false }))
386-
const validDeadline = Date.now() + 2_000
387-
while (context?.diagnostic() !== undefined && Date.now() < validDeadline) await Bun.sleep(10)
388-
expect(context?.diagnostic()).toBeUndefined()
389-
await app.renderOnce()
390-
expect(app.captureCharFrame()).toContain("mouse off")
391-
} finally {
392-
app.renderer.destroy()
393-
}
394-
})

0 commit comments

Comments
 (0)