Skip to content

Commit 5b13af4

Browse files
committed
test(tui): make audio idle checks deterministic
1 parent 4a7cd92 commit 5b13af4

2 files changed

Lines changed: 47 additions & 98 deletions

File tree

‎packages/tui/src/audio-idle.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
import type { Audio } from "@opentui/core"
22

3-
export function createAudioIdleRelease(audio: Pick<Audio, "getStats" | "stop">) {
4-
let timer: ReturnType<typeof setInterval> | undefined
3+
export function createAudioIdleRelease(
4+
audio: Pick<Audio, "getStats" | "stop">,
5+
schedule: (check: () => void) => () => void = scheduleIdleCheck,
6+
) {
7+
let cancel: (() => void) | undefined
58

69
function clear() {
7-
clearInterval(timer)
8-
timer = undefined
10+
cancel?.()
11+
cancel = undefined
912
}
1013

1114
return {
1215
watch() {
13-
if (timer !== undefined) return
14-
timer = setInterval(() => {
16+
if (cancel !== undefined) return
17+
cancel = schedule(() => {
1518
const stats = audio.getStats()
1619
if (!stats || stats.voicesActive !== 0) return
1720
if (audio.stop()) clear()
18-
}, 100)
19-
timer.unref()
21+
})
2022
},
2123
dispose: clear,
2224
}
2325
}
26+
27+
function scheduleIdleCheck(check: () => void) {
28+
const timer = setInterval(check, 100)
29+
timer.unref()
30+
return () => clearInterval(timer)
31+
}
Lines changed: 31 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
1-
import { afterEach, describe, expect, test } from "bun:test"
1+
import { expect, test } from "bun:test"
22
import type { Audio } from "@opentui/core"
33
import { createAudioIdleRelease } from "../src/audio-idle"
44

5-
const releases: ReturnType<typeof createAudioIdleRelease>[] = []
6-
7-
afterEach(() => {
8-
for (const release of releases) release.dispose()
9-
releases.length = 0
10-
})
11-
12-
async function until(check: () => boolean) {
13-
const deadline = performance.now() + 2000
14-
while (!check() && performance.now() < deadline) await Bun.sleep(10)
15-
expect(check()).toBe(true)
16-
}
17-
185
function setup() {
19-
const state = { voices: 1, stops: 0, reads: 0, available: true, canStop: true }
6+
const state = { voices: 1, stops: 0, reads: 0 }
7+
let check: (() => void) | undefined
208
const audio: Pick<Audio, "getStats" | "stop"> = {
219
getStats() {
2210
state.reads++
23-
if (!state.available) return null
2411
return {
2512
soundsLoaded: 1,
2613
voicesActive: state.voices,
@@ -32,85 +19,39 @@ function setup() {
3219
},
3320
stop() {
3421
state.stops++
35-
return state.canStop
22+
return true
3623
},
3724
}
38-
const release = createAudioIdleRelease(audio)
39-
releases.push(release)
40-
return { state, release }
41-
}
42-
43-
describe("audio idle release", () => {
44-
test("waits for every voice to finish, then stops polling", async () => {
45-
const { state, release } = setup()
46-
state.voices = 2
47-
release.watch()
48-
await Bun.sleep(150)
49-
expect(state.stops).toBe(0)
50-
state.voices = 1
51-
await Bun.sleep(150)
52-
expect(state.stops).toBe(0)
53-
state.voices = 0
54-
await until(() => state.stops === 1)
55-
const reads = state.reads
56-
await Bun.sleep(150)
57-
expect(state.reads).toBe(reads)
58-
})
59-
60-
test("can watch playback again after releasing the device", async () => {
61-
const { state, release } = setup()
62-
state.voices = 0
63-
release.watch()
64-
await until(() => state.stops === 1)
65-
state.voices = 1
66-
release.watch()
67-
await Bun.sleep(150)
68-
expect(state.stops).toBe(1)
69-
state.voices = 0
70-
await until(() => state.stops === 2)
25+
const release = createAudioIdleRelease(audio, (callback) => {
26+
check = callback
27+
return () => {
28+
check = undefined
29+
}
7130
})
31+
return { state, release, tick: () => check?.() }
32+
}
7233

73-
test("releases a started device when no voice was created", async () => {
74-
const { state, release } = setup()
75-
state.voices = 0
76-
release.watch()
77-
release.watch()
78-
await until(() => state.stops === 1)
79-
await Bun.sleep(150)
80-
expect(state.stops).toBe(1)
81-
})
34+
test("audio idle release waits for playback to finish, then stops polling", () => {
35+
const fixture = setup()
36+
fixture.release.watch()
37+
fixture.tick()
38+
expect(fixture.state.stops).toBe(0)
8239

83-
test("does not assume missing statistics mean idle", async () => {
84-
const { state, release } = setup()
85-
state.available = false
86-
state.voices = 0
87-
release.watch()
88-
await Bun.sleep(150)
89-
expect(state.stops).toBe(0)
90-
state.available = true
91-
await until(() => state.stops === 1)
92-
})
40+
fixture.state.voices = 0
41+
fixture.tick()
42+
expect(fixture.state.stops).toBe(1)
43+
expect(fixture.state.reads).toBe(2)
9344

94-
test("retries a failed stop", async () => {
95-
const { state, release } = setup()
96-
state.voices = 0
97-
state.canStop = false
98-
release.watch()
99-
await until(() => state.stops > 0)
100-
state.canStop = true
101-
const stops = state.stops
102-
await until(() => state.stops > stops)
103-
const reads = state.reads
104-
await Bun.sleep(150)
105-
expect(state.reads).toBe(reads)
106-
})
45+
fixture.tick()
46+
expect(fixture.state.stops).toBe(1)
47+
expect(fixture.state.reads).toBe(2)
48+
})
10749

108-
test("disposal cancels polling", async () => {
109-
const { state, release } = setup()
110-
release.watch()
111-
release.dispose()
112-
await Bun.sleep(150)
113-
expect(state.reads).toBe(0)
114-
expect(state.stops).toBe(0)
115-
})
50+
test("audio idle release disposal cancels polling", () => {
51+
const fixture = setup()
52+
fixture.release.watch()
53+
fixture.release.dispose()
54+
fixture.tick()
55+
expect(fixture.state.reads).toBe(0)
56+
expect(fixture.state.stops).toBe(0)
11657
})

0 commit comments

Comments
 (0)