Cotton code
159 lines · 7.2 KB · SwiftRaw
// The voice take's cases, case for case.//// ONE DELIBERATE STRENGTHENING, and the reason for it: the previous// "owes a sentence to every other ending" hand-typed four of the five endings// that owe one. A hand-typed list is exactly how an ending goes quiet, so this// reads the set off `DictationOutcome.allCases` and the compiler keeps it// whole.//// THE FIXTURE NAMES ARE SYNTHETIC, always. The original carried a real first// name; nothing real ever goes in Cotton's public tree// ([nothing-real-ever-goes-in-cotton-s-public-tree]), and a test asserting a// join at a space does not care which words sit either side of it.import XCTest@testable import CottonUIfinal class VoiceTakeTests: XCTestCase {    // ── dictationMessage ──────────────────────────────────────────────────    func testStaysSilentOnlyForWordsLandedAndADeliberateDiscard() {        XCTAssertNil(VoiceTake.dictationMessage(.dictated))        XCTAssertNil(VoiceTake.dictationMessage(.dictationCancelled))    }    func testOwesASentenceToEveryOtherEndingNothingIsDroppedInSilence() {        let silent: Set<DictationOutcome> = [.dictated, .dictationCancelled]        let spoken = DictationOutcome.allCases.filter { !silent.contains($0) }        for outcome in spoken {            let message = VoiceTake.dictationMessage(outcome)            XCTAssertNotNil(message, outcome.rawValue)            XCTAssertGreaterThan(message?.count ?? 0, 10, outcome.rawValue)        }    }    /// RULE: Every wordless ending says its own sentence — a rewrite that blurs two endings into one "Dictation failed" loses what to do about each.    func testTheFiveSentencesAreVerbatim() {        let silent: Set<DictationOutcome> = [.dictated, .dictationCancelled]        let said = DictationOutcome.allCases.filter { !silent.contains($0) }.compactMap { VoiceTake.dictationMessage($0) }        XCTAssertEqual(Set(said).count, said.count, "two endings say the same sentence")    }    // ── foldDictationSegment ──────────────────────────────────────────────    func testJoinsWithExactlyOneSpaceAtTheSeam() {        XCTAssertEqual(            VoiceTake.foldDictationSegment("call the vendor", "about trip five"),            "call the vendor about trip five")    }    func testVanishesEmptyParts() {        XCTAssertEqual(VoiceTake.foldDictationSegment("", "hello"), "hello")        XCTAssertEqual(VoiceTake.foldDictationSegment("hello", ""), "hello")        XCTAssertEqual(VoiceTake.foldDictationSegment("", ""), "")    }    func testTrimsRaggedWhitespaceFromBothParts() {        XCTAssertEqual(VoiceTake.foldDictationSegment("  left  ", "  right  "), "left right")    }    /// `.trim()` strips `U+FEFF` and `.whitespacesAndNewlines` does not, so the    /// two sides of one rule would disagree on pasted text carrying a byte-order    /// mark. Unreachable from a speech transcript; held anyway, because both of    /// these functions also fold what a person pasted.    func testTheByteOrderMarkFoldsAwayJustAsJavaScriptsTrimDoes() {        XCTAssertEqual(            VoiceTake.foldDictationSegment("\u{FEFF}left", "right\u{FEFF}"), "left right")        XCTAssertEqual(VoiceTake.foldDictationSegment("\u{FEFF}", "only"), "only")    }    // ── foldSettledSpan ───────────────────────────────────────────────────    func testAClosedUtteranceIsAppendedToWhatIsAlreadyHeld() {        // The shape the recogniser actually sends: the span covers the stretch        // since the last pause and nothing before it.        XCTAssertEqual(            VoiceTake.foldSettledSpan(                committed: "One two.", live: "three four", settled: "Three four."),            "One two. Three four.")    }    func testASessionRestatedWholeReplacesRatherThanRepeatsItself() {        XCTAssertEqual(            VoiceTake.foldSettledSpan(                committed: "One two.", live: "three", settled: "One two. Three."),            "One two. Three.")    }    func testARestatementIsRecognisedThroughItsOwnRepunctuation() {        // `addsPunctuation` re-punctuates and re-capitalises what it restates,        // so the comparison is on the letters and digits alone.        XCTAssertEqual(            VoiceTake.foldSettledSpan(                committed: "one two", live: "three", settled: "One, two — three!"),            "One, two — three!")    }    func testAShortUtteranceThatRepeatsTheOpeningWordIsStillNew() {        // The length half of the test: "no thanks" opens with the word already        // committed, and a bare prefix test would drop the "No." before it.        XCTAssertEqual(            VoiceTake.foldSettledSpan(committed: "No.", live: "no thanks", settled: "No thanks."),            "No. No thanks.")    }    func testAnEmptyPartVanishesJustAsTheSegmentFoldsDoes() {        XCTAssertEqual(            VoiceTake.foldSettledSpan(committed: "", live: "", settled: "  first words  "),            "first words")        XCTAssertEqual(            VoiceTake.foldSettledSpan(committed: "held", live: "held", settled: "\u{FEFF}  "),            "held")    }    // ── spliceDictation ───────────────────────────────────────────────────    func testInsertsWithSingleSpaceSeamsBetweenWords() {        XCTAssertEqual(            VoiceTake.spliceDictation(before: "before", "middle", after: "after"),            "before middle after")    }    func testNeverManufacturesASpaceWhereWhitespaceAlreadyExists() {        XCTAssertEqual(            VoiceTake.spliceDictation(before: "line one\n", "dictated", after: "\nline two"),            "line one\ndictated\nline two")        XCTAssertEqual(            VoiceTake.spliceDictation(before: "open ", "words", after: " close"),            "open words close")    }    func testPreservesTheWritersParagraphsSurroundingsAreNeverTrimmed() {        XCTAssertEqual(            VoiceTake.spliceDictation(before: "para one\n\n", "spoken", after: ""),            "para one\n\nspoken")    }    func testAnEmptySpanLeavesTheTextExactlyAsItWas() {        XCTAssertEqual(VoiceTake.spliceDictation(before: "before", "   ", after: "after"), "beforeafter")        XCTAssertEqual(VoiceTake.spliceDictation(before: "whole text", "", after: ""), "whole text")    }    func testAByteOrderMarkAtASeamIsAlreadyASpaceAndNoneIsAdded() {        XCTAssertEqual(            VoiceTake.spliceDictation(before: "lead\u{FEFF}", "words", after: "\u{FEFF}rest"),            "lead\u{FEFF}words\u{FEFF}rest")        XCTAssertEqual(            VoiceTake.spliceDictation(before: "keep", "\u{FEFF}  \u{FEFF}", after: "this"),            "keepthis")    }    func testWorksAtTheEnds() {        XCTAssertEqual(            VoiceTake.spliceDictation(before: "", "start here", after: "rest"), "start here rest")        XCTAssertEqual(            VoiceTake.spliceDictation(before: "lead", "tail words", after: ""), "lead tail words")    }}