read text aloud

This commit is contained in:
2026-10-23 09:40:20 +02:00
parent 281f5465a6
commit 92b90c0e13
20 changed files with 471 additions and 18 deletions
@@ -625,7 +625,7 @@ import Testing
Issue.record("no context menu")
return
}
#expect(menuLabels(menu) == ["Copy", "Paste"])
#expect(menuLabels(menu) == ["Copy", "Paste", "Wait what?"])
#expect(menu.header.isEmpty)
#expect(menu.anchor == CGPoint(x: 900, y: 300))
}
@@ -637,6 +637,14 @@ import Testing
#expect(harness.system.copied == ["npm test"])
}
@Test func waitWhatInsertsTheSelectionAsAWaitWhatCommand() async {
let harness = WorkspaceHarness()
harness.agent.selectedText = " the reviewer was unsure\n"
await harness.perform(.context(.waitWhat(.agent)))
#expect(harness.agent.sent == [BracketedPaste.wrap("/wait-what `the reviewer was unsure`", composerHasText: false)])
#expect(harness.toastTitles == ["Passed to agent"])
}
@Test func pasteSendsThePasteboardToThatTerminal() async {
let harness = WorkspaceHarness()
let shell = FakeTerminal()
@@ -662,3 +670,78 @@ import Testing
}
}
@MainActor
@Suite struct ReadAloudModelTests {
@Test func aMarkdownFileIsReadAsPlainText() async {
let harness = WorkspaceHarness(contents: ["README.md": "# Hello\n\nSome **bold** [link](https://x.com)\n\n```\ncode\n```"])
await harness.start()
await harness.perform(.context(.readAloudFile("README.md")))
#expect(harness.speech.spoken == ["Hello.\nSome bold link."])
#expect(harness.model.readAloud.isReading)
}
@Test func theOpenFileIsReadWithItsUnsavedEdits() async {
let harness = WorkspaceHarness(contents: ["README.md": "On disk"])
await harness.start()
harness.model.open("README.md")
await harness.model.settle()
harness.model.editor?.edit("Typed")
await harness.perform(.context(.readAloudFile("README.md")))
#expect(harness.speech.spoken == ["Typed."])
}
@Test func aFileWithoutProseReadsNothingAndSaysSo() async {
let harness = WorkspaceHarness(contents: ["README.md": "```\nonly code\n```"])
await harness.start()
await harness.perform(.context(.readAloudFile("README.md")))
#expect(harness.speech.spoken.isEmpty)
#expect(!harness.model.readAloud.isReading)
#expect(harness.toastTitles == ["Nothing to read"])
}
@Test func aSelectionIsReadAsItIs() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("let a = 1")))
#expect(harness.speech.spoken == ["let a = 1"])
}
@Test func aDollarSignIsReadAsDollar() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("echo $HOME costs $5")))
#expect(harness.speech.spoken == ["echo dollar HOME costs $5"])
}
@Test func stopEndsTheReading() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("words")))
await harness.perform(.stopReading)
#expect(harness.speech.stops == 1)
#expect(!harness.model.readAloud.isReading)
}
@Test func readingToTheEndClearsTheState() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("words")))
harness.speech.finish()
#expect(!harness.model.readAloud.isReading)
}
@Test func aProjectSwitchStopsTheReading() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("words")))
await harness.model.openProject(URL(fileURLWithPath: "/fake/other"))
#expect(!harness.model.readAloud.isReading)
#expect(harness.speech.stops == 1)
}
@Test func readAloudUsesTheSpeakerIcon() {
#expect(ContextMenuOverlay.symbol(for: .readAloudFile("README.md")) == "speaker.wave.2")
#expect(ContextMenuOverlay.tint(for: .readAloudText("x")) == .plain)
}
}
@@ -0,0 +1,13 @@
import HelderServices
/// Records what would have been read aloud; `finish()` plays the voice reaching the end.
@MainActor
final class FakeSpeech: SpeechServicing {
var onFinish: (() -> Void)?
private(set) var spoken: [String] = []
private(set) var stops = 0
func speak(_ text: String) { spoken.append(text) }
func stop() { stops += 1 }
func finish() { onFinish?() }
}
@@ -18,6 +18,7 @@ struct WorkspaceHarness {
let session: FakeSession
let layouts: FakeLayouts
let system: FakeSystem
let speech: FakeSpeech
let watchers: WatcherBox
let agent = FakeTerminal()
@@ -38,9 +39,10 @@ struct WorkspaceHarness {
let recentStore = FakeRecentProjects(recents)
let layouts = FakeLayouts()
let system = FakeSystem()
let speech = FakeSpeech()
let watchers = WatcherBox()
let app = AppServices(
recentProjects: recentStore, session: session, layouts: layouts, system: system,
recentProjects: recentStore, session: session, layouts: layouts, system: system, speech: speech,
makeWorkspace: { _ in
WorkspaceServices(git: git, files: files, search: FakeSearch(), symbols: symbols,
config: config, notes: notes, recentFiles: recentFiles)
@@ -63,6 +65,7 @@ struct WorkspaceHarness {
self.session = session
self.layouts = layouts
self.system = system
self.speech = speech
self.watchers = watchers
model.terminal.attach(agent, as: .agent)
}