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
@@ -10,10 +10,15 @@ import Testing
}
@Test func copyAppearsOnlyWithASelection() {
#expect(labels(.terminal(.agent, hasSelection: true)) == ["Copy", "Paste"])
#expect(labels(.terminal(.agent, hasSelection: true)) == ["Copy", "Paste", "Wait what?"])
#expect(labels(.terminal(.shell, hasSelection: false)) == ["Paste"])
}
@Test func waitWhatAppearsOnlyForASelectionInTheAgentPane() {
#expect(!labels(.terminal(.agent, hasSelection: false)).contains("Wait what?"))
#expect(!labels(.terminal(.shell, hasSelection: true)).contains("Wait what?"))
}
@Test func theActionsNameTheirPane() {
#expect(ContextActions.entries(for: .terminal(.shell, hasSelection: true)) == [
.action(.copyTerminalSelection(.shell), label: "Copy"),
@@ -83,3 +88,58 @@ import Testing
#expect(!hasBouwen(.file(path: "ontwikkelingen/a")))
}
}
@Suite struct ReadAloudContextMenuTests {
private func readAloud(_ target: ContextTarget) -> [ContextMenuEntry] {
ContextActions.entries(for: target).filter { entry in
switch entry {
case .action(.readAloudFile, _), .action(.readAloudText, _): true
default: false
}
}
}
@Test func aMarkdownFileCanBeReadAloud() {
let entries = ContextActions.entries(for: .file(path: "docs/guide.md"))
#expect(Array(entries[4...6]) == [
.separator,
.action(.readAloudFile("docs/guide.md"), label: "Read aloud"),
.separator,
])
#expect(readAloud(.gitRow(path: "README.md", staged: true)) == [.action(.readAloudFile("README.md"), label: "Read aloud")])
}
@Test func otherFilesCannot() {
#expect(readAloud(.file(path: "src/app.php")).isEmpty)
#expect(readAloud(.gitRow(path: "src/app.php", staged: false)).isEmpty)
#expect(readAloud(.directory(path: "docs")).isEmpty)
}
@Test func aSelectionCanBeReadAloudAfterCopyReference() {
let reference = AgentReference(path: "src/a.php", startLine: 3, endLine: 4)
#expect(ContextActions.entries(for: .editor(reference: reference, code: "$a = 1;", showDiffLine: 3)) == [
.action(.passOnReference("src/a.php:3-4", code: "$a = 1;"), label: "Pass on selection"),
.action(.copyReference("src/a.php:3-4"), label: "Copy reference"),
.separator,
.action(.readAloudText("$a = 1;"), label: "Read aloud selection"),
.separator,
.action(.showDiff("src/a.php", line: 3), label: "Show diff"),
])
}
@Test func noSelectionNoReadAloud() {
let reference = AgentReference(path: "README.md", startLine: 1)
#expect(readAloud(.editor(reference: reference, code: nil)).isEmpty)
#expect(readAloud(.editor(reference: reference, code: "")).isEmpty)
}
@Test func selectedMarkdownIsReadAsProse() {
let reference = AgentReference(path: "README.md", startLine: 1)
#expect(readAloud(.editor(reference: reference, code: "## Setup\nRun **this**")) == [
.action(.readAloudText("Setup.\nRun this."), label: "Read aloud selection"),
])
#expect(readAloud(.editor(reference: reference, code: "```\nls\n```")) == [
.action(.readAloudText("```\nls\n```"), label: "Read aloud selection"),
])
}
}
@@ -0,0 +1,39 @@
import Foundation
import HelderCore
import Testing
@Suite struct MarkdownSpeechTests {
private func speech(_ markdown: String) -> String {
MarkdownSpeech.plainText(MarkdownParser.parse(markdown))
}
@Test func eachBlockIsItsOwnSentence() {
#expect(speech("# Title\n\nFirst paragraph\n\nSecond one!") == "Title.\nFirst paragraph.\nSecond one!")
}
@Test func inlineMarkupIsStrippedAndLinksKeepTheirText() {
#expect(speech("Some **bold**, *soft*, ~~old~~ and `code` in [the docs](https://x.com).")
== "Some bold, soft, old and code in the docs.")
}
@Test func listItemsQuotesAndTableRowsAreRead() {
#expect(speech("- one\n- two:\n\n> quoted") == "one.\ntwo:\nquoted.")
#expect(speech("| Name | Role |\n|---|---|\n| Ann | dev |") == "Name, Role.\nAnn, dev.")
}
@Test func codeBlocksAndRulesAreSkipped() {
#expect(speech("Before\n\n```php\necho 1;\n```\n\n---\n\nAfter") == "Before.\nAfter.")
#expect(speech("```\nonly code\n```").isEmpty)
}
@Test func aDollarSignIsSpokenAsDollar() {
#expect(MarkdownSpeech.spoken("echo $user") == "echo dollar user")
#expect(MarkdownSpeech.spoken("$ x") == "dollar x")
#expect(MarkdownSpeech.spoken("a$$") == "a dollar dollar")
}
@Test func aDollarBeforeADigitAndTextWithoutDollarStay() {
#expect(MarkdownSpeech.spoken("It costs $5.") == "It costs $5.")
#expect(MarkdownSpeech.spoken("No money here.") == "No money here.")
}
}
@@ -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)
}