// The torn page: a note's title written on the page's rules, one line or // two and no further, the date typed on the next, then the opening on every // rule to the page's foot, stopping at the last whole word; a note with no // title sets its first sentence as one; nothing on the page is a tagged part, // so the whole page is the deck's to tap; the face lies on the notebook's // linen and is stepped by a cut. @testable import CottonFaces import CottonPaper import CottonSpecimens import CottonValues import CoreText import SwiftUI import XCTest #if canImport(AppKit) import AppKit #endif @MainActor final class NotePageTests: XCTestCase { private typealias Catalogue = NotePageFaceCatalogue /// A phone's page: the card 350 by 437 points, the page and its clip 97% of its height. private let size = CGSize( width: 0.97 * 437 / (1 - NotePageFaceKnobs.clipTop) * NotePageFaceKnobs.aspect, height: 0.97 * 437 / (1 - NotePageFaceKnobs.clipTop)) private func note(_ fields: [String: RecordValue]) -> DeckCard { DeckCard(face: "note.page", corner: nil, pressing: false, pace: nil, plate: nil, fields: fields) } /// The words of `text`, in order, whitespace let go. private func words(_ text: String) -> [Substring] { text.split(whereSeparator: { $0.isWhitespace }) } /// RULE: The title is written on its rule, the date typed on the next, a rule is left bare, and the opening takes every rule from there to the page's foot. func testTheWordsAreWrittenOnTheRules() { let page = TornPageSetting.set(Catalogue.lake, size: size, theme: .paper) XCTAssertEqual(page.title, [TornLine("Things the lake taught me")]) XCTAssertEqual(page.titleRules, [NotePageFaceKnobs.titleRule]) XCTAssertEqual(page.date, "Sun 11 Oct \u{00B7} 7:12 am") XCTAssertEqual(page.dateRule, NotePageFaceKnobs.titleRule + 1) XCTAssertEqual(page.openingRule, NotePageFaceKnobs.titleRule + 3) XCTAssertEqual(page.openingRules.upperBound, NotePageFaceKnobs.rules.count, "a long note fills the page to its last rule") XCTAssertTrue(page.opening.first?.words.hasPrefix("I went down to the lake") == true) } /// RULE: The opening is the body's first words in order and stops at the last whole word the page holds: nothing is cut inside a word and nothing is added, no ellipsis, no "and more". func testTheOpeningStopsAtTheLastWholeWord() throws { let page = TornPageSetting.set(Catalogue.lake, size: size, theme: .paper) let body = try XCTUnwrap(Catalogue.lake.text("body")) let written = page.opening.flatMap { words($0.words) } let all = words(body) XCTAssertLessThan(written.count, all.count, "the lake's note is longer than its opening") XCTAssertEqual(Array(all.prefix(written.count)), written, "the opening is the body's first words, whole") XCTAssertFalse(page.opening.contains { $0.words.contains("\u{2026}") }) let column = TornPageSetting.column(size).width let font = TornPageSetting.fonts(height: size.height).opening for line in page.opening { let set = NSAttributedString(string: line.words, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font]) let width = CGFloat(CTLineGetTypographicBounds(CTLineCreateWithAttributedString(set), nil, nil, nil)) XCTAssertLessThanOrEqual(width, column + 0.5, "\"\(line.words)\" runs past the page's writing room") } } /// RULE: A paragraph after the first starts on a rule of its own, indented; the first is not. func testAParagraphStartsIndented() throws { let page = TornPageSetting.set(Catalogue.lake, size: size, theme: .paper) XCTAssertFalse(page.opening[0].indented) let second = try XCTUnwrap(page.opening.firstIndex { $0.words.hasPrefix("The first thing the lake taught me") }) XCTAssertTrue(page.opening[second].indented) XCTAssertEqual(page.opening.filter(\.indented).count, 1 + (page.opening.contains { $0.words.hasPrefix("The second thing") } ? 1 : 0)) } /// RULE: A short note writes its two sentences and leaves the rest of the rules bare. func testAShortNoteLeavesTheRulesBare() { let page = TornPageSetting.set(Catalogue.short, size: size, theme: .paper) XCTAssertEqual(page.title.map(\.words), ["Spare key"]) XCTAssertLessThanOrEqual(page.opening.count, 4) XCTAssertGreaterThan(NotePageFaceKnobs.rules.count - page.openingRules.upperBound, 8, "most of the rules stand bare") XCTAssertEqual(page.opening.flatMap { words($0.words) }, words(Catalogue.short.text("body") ?? ""), "a short note is written whole") } /// RULE: A note with no title sets its first sentence as its title, with no full stop, and the opening goes on after it. func testAnUntitledNoteTakesItsFirstSentence() { let page = TornPageSetting.set(Catalogue.untitled, size: size, theme: .paper) XCTAssertEqual( page.title.map(\.words).joined(separator: " "), "Buy the blue-black ink before Friday") XCTAssertTrue(page.opening.first?.words.hasPrefix("The shop by the station") == true) XCTAssertFalse(page.opening.first?.indented ?? true, "the rest of the first paragraph goes on unindented") } /// RULE: A long title takes two lines, two rules apart, the date and the opening moving down under it; a title that wants a third stops at the second's last whole word, with no ellipsis. func testALongTitleTakesTwoLinesAndNoMore() throws { let page = TornPageSetting.set(Catalogue.longTitle, size: size, theme: .paper) XCTAssertEqual(page.title.count, 2) XCTAssertEqual(page.titleRules, [NotePageFaceKnobs.titleRule, NotePageFaceKnobs.titleRule + NotePageFaceKnobs.titleStep]) XCTAssertEqual(page.dateRule, page.titleRules[1] + 1) XCTAssertEqual(page.openingRule, page.titleRules[1] + 3) XCTAssertEqual(page.title.flatMap { words($0.words) }, words(Catalogue.longTitle.text("title") ?? "")) let longer = "What the garden needs before the rains come back, and the shed, and the old mango tree at the very back of the plot" let stopped = TornPageSetting.set(note(["title": .text(longer), "body": .text("Soon.")]), size: size, theme: .paper) XCTAssertEqual(stopped.title.count, 2) let written = stopped.title.flatMap { words($0.words) } XCTAssertLessThan(written.count, words(longer).count) XCTAssertEqual(Array(words(longer).prefix(written.count)), written, "the title stops at a whole word") XCTAssertNil(stopped.dateRule, "no date, no date line") XCTAssertEqual(stopped.openingRule, stopped.titleRules[1] + 2) } /// RULE: A note from another year says its year on the date line: the core's words, as they are. func testAnOldNoteSaysItsYear() { XCTAssertEqual(TornPageSetting.set(Catalogue.old, size: size, theme: .paper).date, "Mon 27 Oct 2025 \u{00B7} 8:15 am") } /// RULE: A first sentence ends at a full stop, question or exclamation that ends a word, or at the first line's end; a full stop is let go, a question kept; a decimal point ends nothing. func testAFirstSentence() { XCTAssertEqual(TornPageSetting.firstSentence("Call the plumber. Then the bank.")?.sentence, "Call the plumber") XCTAssertEqual(TornPageSetting.firstSentence("Call the plumber. Then the bank.")?.rest, "Then the bank.") XCTAssertEqual(TornPageSetting.firstSentence("Why is the tap dripping? Washer.")?.sentence, "Why is the tap dripping?") XCTAssertEqual(TornPageSetting.firstSentence("Buy 2.5 kg of rice. And dal.")?.sentence, "Buy 2.5 kg of rice") XCTAssertEqual(TornPageSetting.firstSentence("Groceries\nrice, dal, oil")?.sentence, "Groceries") XCTAssertEqual(TornPageSetting.firstSentence("Groceries\nrice, dal, oil")?.rest, "rice, dal, oil") XCTAssertEqual(TornPageSetting.firstSentence("just a thought")?.sentence, "just a thought") XCTAssertNil(TornPageSetting.firstSentence(" \n ")) let empty = TornPageSetting.set(note(["date": .text("Thu 29 Oct")]), size: size, theme: .paper) XCTAssertEqual(empty.title, []) XCTAssertEqual(empty.opening, []) XCTAssertEqual(empty.date, "Thu 29 Oct") } /// RULE: Nothing on the page is a tagged part — not its title, its date nor its opening — so a tap anywhere on it is the deck's: a side third steps, the middle opens the reader. func testThePageTagsNoPart() { final class Tagged { var parts: Set = [] } let tagged = Tagged() let drawn = NotePageFace(card: Catalogue.lake) .frame(width: 350, height: 437) .backgroundPreferenceValue(FacePartsKey.self) { regions in let _ = tagged.parts.formUnion(regions.map(\.part)) Color.clear } XCTAssertNotNil(ImageRenderer(content: drawn).cgImage) XCTAssertEqual(tagged.parts, []) XCTAssertTrue(NotePageFaceCatalogue.face.tagsNoPart, "the page says so, so the shell writes its words from the fan") } /// RULE: The torn page lies bare on the notes' burnt-orange felt, laid with its painted tile, and is stepped by a cut. func testThePageLiesOnTheFeltAndCuts() { XCTAssertEqual(FaceRegistry.ground("note.page"), .feltOrange) XCTAssertEqual(FaceRegistry.stock("note.page"), .own) XCTAssertTrue(FaceGround.feltOrange.dark) XCTAssertEqual(FaceGround.feltOrange.tile, "notes-felt") XCTAssertEqual(Catalogue.face.steps, .cut) for name in ["note-page", "note-page-shadow", "note-clip", "notes-felt"] { XCTAssertNotNil(CottonMedia.image(name, scale: 3), "\(name) is not in the catalogue") } } } extension NotePageTests { func testMarkdownIsSetBeforeTheTornPageIsMeasured() { let page = TornPageSetting.set(note(["title": .text("Synthetic Markdown"), "body": .text("**Bold** and *care* with ::light:: and #nested/tag.\n\n- [x] A task with [[Title]] and [label](https://example.com).")]), size: size, theme: .paper) let words = page.opening.map(\.words).joined(separator: " ") XCTAssertTrue(words.contains("Bold and care")) XCTAssertTrue(words.contains("☑ A task")) XCTAssertFalse(words.contains("*")) XCTAssertFalse(words.contains("[[")) XCTAssertTrue(page.opening.contains { $0.setWords?.runs.contains(where: { $0.inlinePresentationIntent?.contains(.stronglyEmphasized) == true }) == true }) XCTAssertTrue(page.opening.contains { $0.setWords?.runs.contains(where: { $0.backgroundColor != nil }) == true }) } func testHighlightDrawsOnTheCard() throws { func image(_ body: String) throws -> CGImage { try XCTUnwrap(SpecimenShot.image(Catalogue.face, Specimen("synthetic", note(["title": .text("Synthetic Markdown"), "body": .text(body)])), size: CGSize(width: 340, height: 451))) } let marked = try image("::a soft marker::") let plain = try image("a soft marker") XCTAssertNotEqual(marked.dataProvider?.data as Data?, plain.dataProvider?.data as Data?, "the marker must be visible, not just an attribute Text ignores") let words = try image("Set words") for body in ["**Set words**", "*Set words*", "***Set words***"] { let styled = try image(body) XCTAssertNotEqual(styled.dataProvider?.data as Data?, words.dataProvider?.data as Data?, "\(body) must paint its weight and slant") } if let out = ProcessInfo.processInfo.environment["COTTON_FACE_SHOTS"] { #if canImport(AppKit) let data = NSBitmapImageRep(cgImage: marked).representation(using: .png, properties: [:]) try data?.write(to: URL(fileURLWithPath: out).appendingPathComponent("synthetic-highlight.png")) #endif } } }