|
| 1 | +import { afterEach, expect, test } from "bun:test" |
| 2 | +import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import { join } from "node:path" |
| 5 | +import { createAppFixture } from "../fixture/app" |
| 6 | + |
| 7 | +const originalPath = process.env.PATH |
| 8 | +afterEach(() => { |
| 9 | + process.env.PATH = originalPath |
| 10 | +}) |
| 11 | + |
| 12 | +// Drives the real app: a held key reaches the prompt as rapid ordinary presses |
| 13 | +// (terminals send no repeat marker without event reporting), and recording must |
| 14 | +// neither toggle off nor restart while it is held. The fake recorder logs each |
| 15 | +// start, so the assertion counts recorders instead of depending on the final |
| 16 | +// toggle state (an odd number of toggles would otherwise look correct). |
| 17 | +test("holding the voice key records once; a press after a quiet gap stops", async () => { |
| 18 | + const bin = mkdtempSync(join(tmpdir(), "opencode-voice-keys-")) |
| 19 | + const log = join(bin, "starts.log") |
| 20 | + const ffmpeg = join(bin, "ffmpeg") |
| 21 | + // Last argument is the output file, as ffmpegArgs builds it. Exit cleanly on INT. |
| 22 | + writeFileSync( |
| 23 | + ffmpeg, |
| 24 | + `#!/bin/sh\ntrap 'exit 0' INT\necho started >> '${log}'\nfor a in "$@"; do last="$a"; done\ntouch "$last"\nsleep 30 & wait\n`, |
| 25 | + ) |
| 26 | + chmodSync(ffmpeg, 0o755) |
| 27 | + // Prepend: spawn() resolves ffmpeg through the child PATH. |
| 28 | + process.env.PATH = `${bin}:${originalPath}` |
| 29 | + const starts = () => (existsSync(log) ? readFileSync(log, "utf8").split("\n").filter(Boolean).length : 0) |
| 30 | + |
| 31 | + await using setup = await createAppFixture({ |
| 32 | + config: { |
| 33 | + animations: false, |
| 34 | + keybinds: { "prompt.voice.push_to_talk": "f7" }, |
| 35 | + voice: { transcribe_command: "echo transcribed" }, |
| 36 | + }, |
| 37 | + }) |
| 38 | + await setup.ready |
| 39 | + await setup.waitForFrame((frame) => frame.includes("ctrl+p commands")) |
| 40 | + |
| 41 | + setup.mockInput.pressKey("F7") |
| 42 | + await setup.waitForFrame((frame) => frame.includes("Recording voice")) |
| 43 | + expect(starts()).toBe(1) |
| 44 | + |
| 45 | + // Simulated hold: auto-repeat every ~33ms after the OS initial delay. |
| 46 | + for (let index = 0; index < 8; index++) { |
| 47 | + await Bun.sleep(33) |
| 48 | + setup.mockInput.pressKey("F7") |
| 49 | + } |
| 50 | + await Bun.sleep(200) |
| 51 | + await setup.renderOnce() |
| 52 | + expect(starts()).toBe(1) |
| 53 | + expect(setup.captureCharFrame()).toContain("Recording voice") |
| 54 | + |
| 55 | + // A fresh press after a quiet gap stops and transcribes. |
| 56 | + await Bun.sleep(1100) |
| 57 | + setup.mockInput.pressKey("F7") |
| 58 | + await setup.waitForFrame((frame) => frame.includes("transcribed")) |
| 59 | + expect(setup.captureCharFrame()).not.toContain("Recording voice") |
| 60 | + expect(starts()).toBe(1) |
| 61 | + |
| 62 | + rmSync(bin, { recursive: true }) |
| 63 | +}, 30_000) |
0 commit comments