import CottonKit import SwiftUI import XCTest @testable import CottonUI /// A line spoken to Home's composer, through the real wiring: the dock /// mounted as Home's box mounts it, its send, the dock's spoken /// split, `HomeVoice`, and the fan turned. Phone build 285 kept "show people" /// as a thought: the route was a copy taken as the box mounted, before /// Home's tools had loaded. Every name is invented. @MainActor final class HomeVoiceTests: XCTestCase { private static let person = TableDefinition( id: "person", name: "Person", plural: "People", columns: [ColumnDefinition(key: "full_name", type: .text, required: true)], title: "full_name", slip: SlipLayout(title: "full_name"), tile: TileLayout(face: .polaroidGrid), page: [PageSection(heading: nil, columns: ["full_name"])]) private static let note = TableDefinition( id: "note", name: "Note", plural: "Notes", columns: [ColumnDefinition(key: "title", type: .text, required: true)], title: "title", slip: SlipLayout(title: "title"), tile: TileLayout(face: .slipList), page: [PageSection(heading: nil, columns: ["title"])]) private let captures = RecordingCapture() private let recogniser = FakeRecogniser() private let navigator = Navigator() private var shown: String? /// What Home is drawn with: nothing loaded yet as the box mounts. private var fan: [(key: String, name: String)] = [("home", "Home")] private var definitions = TableDefinitions([]) private let core = ScriptedCore([ CottonMethod.readFind: .ok(#"{"sections":[{"id":"person","words":"People","count":1,"hits":[{"kind":"person","id":"p-rhyn","title":"Rhyn Kestrel","opens":{"path":"/people/person/p-rhyn"},"record":{"table":"person","id":"p-rhyn"}}]}]}"#) ]) /// The core's spoken line on a board: "open notes" opens the Notes tool /// (`shell.input {op: line}`); any other line is a thought. private let lineCore = InputCore { method, request in guard method == CottonMethod.shellInput, request["op"]?.string == "line" else { return .failure(CottonCallError(code: "missing", message: "Not here")) } guard request["words"]?.string == "open notes" else { return .success(.object(["writes": .array([])])) } return .success(.object(["writes": .array([]), "opens": .object(["table": "note"])])) } private lazy var dock = DockModel( place: .page, captures: captures, media: RecordingMedia(), dictation: DictationModel(recogniser: recogniser), vocabulary: DockVocabulary(), haptics: RecordingHaptics(), toasts: ToastCenter(scheduler: ManualToastScheduler())) /// Home's voice over what Home is drawn with at this moment. private var voice: HomeVoice { HomeVoice( definitions: definitions, fan: fan, choosable: [], core: core, navigator: navigator, show: Binding(get: { self.shown }, set: { self.shown = $0 }), scene: nil, databases: nil) } /// Home's tools arrive after the box has mounted, as they do while a /// fresh install syncs. private func toolsLoad() { fan = [("home", "Home"), ("person", "People"), ("note", "Notes")] definitions = TableDefinitions([Self.person, Self.note]) } /// A take on Home's composer, its words, and its send with Home's voice /// as the box is drawn now (`HomeInput`, `runsSpokenCommand: homeVoice.run`). private func say(_ words: String) async { await dock.startDictation() recogniser.emit(.started) recogniser.speak(words) recogniser.whenStopped = { [recogniser] in recogniser.emit(.segment(words)) recogniser.emit(.ended) } await dock.finishDictation() guard dock.isReadyToSend else { return } await dock.send(runsSpokenCommand: voice.run) } private var kept: [String] { get async { await captures.requests.compactMap { $0.request.text } } } /// RULE: a line spoken to Home's composer reads Home's fan and tools as they are when it is said, never as they were when the box mounted — Home's tools load after the box mounts on a fresh install, and phone build 285 kept "show people" as a thought because the route was a copy taken at mount. func testASpokenLineReadsTheHomeOfTheMomentItIsSaid() async { // The box mounts over a Home whose tools have not loaded. _ = dock toolsLoad() await say("show people") XCTAssertEqual(shown, "person", "the fan turns to People") await say("open notes") XCTAssertEqual(shown, "note", "the fan turns to Notes") await say("open Rhyn Kestrel") XCTAssertEqual(navigator.stack, [.record(.person, "p-rhyn")], "the record opens") let none = await kept XCTAssertEqual(none, [], "a line that went somewhere is not kept") await say("Rhyn says the people at the depot are slow today") let thought = await kept XCTAssertEqual(thought, ["Rhyn says the people at the depot are slow today"], "a plain sentence is a thought") } func testHomeUsesTheSameTopRowAsEveryCardDeck() throws { let today = try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Surfaces/Board/HomeToday.swift", ownedBy: "fan") XCTAssertTrue(today.contains("CardDeck("), "Home uses the same top row as every card deck") } /// The new-thought overlay's voice over what it is drawn with at this moment. private var openVoice: SpokenOpenVoice { SpokenOpenVoice(core: lineCore, navigator: navigator, close: {}) } /// A take that ends: the words, and the recogniser lands them. The /// overlay sends from the take's ending, not from a button. private func takeEnds(_ words: String) async { await dock.startDictation() recogniser.emit(.started) recogniser.speak(words) recogniser.whenStopped = { [recogniser] in recogniser.emit(.segment(words)) recogniser.emit(.ended) } await dock.finishDictation() } /// The overlay's answer to the take's ending (`NewThoughtOverlay.takeEnded`): /// words heard go, with its voice as it is drawn now. private func overlaySends() async { guard dock.ending?.outcome == .dictated, dock.canSend else { return } await dock.send(runsSpokenCommand: openVoice.run) } /// RULE: a take on the new-thought overlay that ends on its own reads the tools as they are when it ends, never as they were when the overlay mounted — the same freeze as Home's record button (phone build 285): an ending handed to the dock as a closure at mount reads the overlay as it was mounted. func testATakeThatEndsOnItsOwnReadsTheOverlayOfTheMomentItEnds() async { navigator.fanOpens = { _ in false } // Mounted fan with no matching tile. // The overlay mounts before the tools load. _ = dock toolsLoad() await takeEnds("open notes") await overlaySends() XCTAssertEqual(navigator.stack, [BoardLayout.listRoute(for: Self.note)], "Notes opens") await takeEnds("Rhyn says the notes from the depot are late") await overlaySends() let thought = await kept XCTAssertEqual(thought, ["Rhyn says the notes from the depot are late"], "a plain sentence is a thought") } }