Cotton code
113 lines · 7.3 KB · SwiftRaw
import CottonKitimport XCTest@testable import CottonUI/// The seek line as the reader reads and asks it (`reader.seek`,/// `reader.seek.say`, `reader.seek.go`, `reader.seek.back`). Where a tap/// lands, the slip's words and when Back retires are the core's/// (`phone::reader_seek`'s tests); here, the answers decode as written, the/// asks are what the core reads, and what the reader measures and draws.final class ReaderSeekTests: XCTestCase {    /// What `reader.seek` answers (the shape `phone::reader_seek` writes; synthetic).    static let line = #"""    {"here":0.42,"ticks":[{"at":0.0,"title":"One","here":false},{"at":0.4,"title":"Two","here":true},{"at":0.8,"title":"Three","here":false}],     "pages":null,"back":{"at":0.1,"words":"Back to 10%","ms_left":18250},     "words":{"here":"Two · 42%","line":"Place in the book","contents":"Contents"},     "steps":{"previous":0.4,"next":0.8}}    """#    /// RULE: The reader reads the core's line whole: where the page stands, each chapter's start and the one the page is in, the Back standing, the words and the steps.    func testTheReaderReadsTheCoresLine() throws {        let seek = try JSONDecoder().decode(ReaderSeekAnswer.self, from: Data(Self.line.utf8))        XCTAssertEqual(seek.here, 0.42)        XCTAssertEqual(seek.ticks.map(\.title), ["One", "Two", "Three"])        XCTAssertEqual(seek.ticks.map(\.here), [false, true, false])        XCTAssertNil(seek.pages)        XCTAssertEqual(seek.back, ReaderBackAnswer(at: 0.1, words: "Back to 10%", msLeft: 18250))        XCTAssertEqual(seek.words.here, "Two · 42%")        XCTAssertEqual(seek.words.line, "Place in the book")        XCTAssertEqual(seek.words.contents, "Contents")        XCTAssertEqual(seek.steps, .init(previous: 0.4, next: 0.8))    }    /// RULE: A line with no Back and no step one way reads as none; a PDF's says its pages.    func testALineAtTheBooksEndWithNoBack() throws {        let text = #"{"here":1.0,"ticks":[],"pages":212,"back":null,"words":{"here":"Page 212","line":"Place in the book","contents":"Contents"},"steps":{"previous":0.9,"next":null}}"#        let seek = try JSONDecoder().decode(ReaderSeekAnswer.self, from: Data(text.utf8))        XCTAssertNil(seek.back)        XCTAssertNil(seek.steps.next)        XCTAssertEqual(seek.pages, 212)    }    /// RULE: A jump answers the Locator to go to and the Back it leaves; Back's own answer may hold neither; a page turned answers Back as it stands.    func testAJumpsAnswerAndBacks() throws {        let went = try JSONDecoder().decode(ReaderSeekWent.self, from: Data(#"{"locator":"{\"href\":\"c7.xhtml\"}","back":{"at":0.2,"words":"Back to page 41","ms_left":20000}}"#.utf8))        XCTAssertEqual(went.locator, #"{"href":"c7.xhtml"}"#)        XCTAssertEqual(went.back?.words, "Back to page 41")        let none = try JSONDecoder().decode(ReaderSeekWent.self, from: Data(#"{"locator":null,"back":null}"#.utf8))        XCTAssertNil(none.locator)        XCTAssertNil(none.back)        let moved = try JSONDecoder().decode(ReaderMovedAnswer.self, from: Data(#"{"at":null,"progress_words":"4%","wrote":false,"again_ms":null,"back":{"at":0.2,"words":"Back to page 41","ms_left":900}}"#.utf8))        XCTAssertEqual(moved.back?.msLeft, 900)        let plain = try JSONDecoder().decode(ReaderMovedAnswer.self, from: Data(#"{"at":null,"progress_words":"4%","wrote":false,"again_ms":null}"#.utf8))        XCTAssertNil(plain.back)    }    private func object(_ value: some Encodable) throws -> [String: Any] {        try XCTUnwrap(try JSONSerialization.jsonObject(with: JSONEncoder().encode(value)) as? [String: Any])    }    /// RULE: The line asks with the book alone; a drag's release with its place; a tap with its place and the line's length; the contents with a tick and nothing else.    func testTheAsksAreWhatTheCoreReads() throws {        XCTAssertEqual(try object(ReaderSeekAsk(book: "b1")).keys.sorted(), ["book"])        let released = try object(ReaderSeekAsk(book: "b1", at: 0.5))        XCTAssertEqual(released.keys.sorted(), ["at", "book"])        XCTAssertEqual(released["at"] as? Double, 0.5)        let tapped = try object(ReaderSeekAsk(book: "b1", at: 0.5, width: 342))        XCTAssertEqual(tapped["width"] as? Double, 342)        let chapter = try object(ReaderSeekAsk(book: "b1", tick: 3))        XCTAssertEqual(chapter.keys.sorted(), ["book", "tick"], "a tick goes with no place and no width")        XCTAssertEqual(chapter["tick"] as? Int, 3)    }    /// RULE: The book's contents carry where each resource begins and a PDF's page count for the line, and neither where the reader could not measure them.    func testTheContentsCarryTheLinesMeasures() throws {        let measured = try object(ReaderReport.Contents(toc: [.init(title: "One", href: "c1.xhtml")], readingOrder: ["c1.xhtml", "c2.xhtml"], starts: [0, 0.5], pages: nil))        XCTAssertEqual(measured["starts"] as? [Double], [0, 0.5])        XCTAssertNil(measured["pages"])        XCTAssertEqual(measured["reading_order"] as? [String], ["c1.xhtml", "c2.xhtml"])        let bare = try object(ReaderReport.Contents(toc: [], readingOrder: ["c1.xhtml"]))        XCTAssertNil(bare["starts"])    }    /// RULE: The starts are each resource's first position, in order and never backwards (a resource with none begins where the one before did); none are said unless every resource is accounted for; only a PDF says pages.    func testWhatTheReaderMeasuresOfTheBook() {        let epub = ReaderSeekMeasures.of(firsts: [0, 0.25, nil, 0.7], resources: 4, positions: 120, pdf: false)        XCTAssertEqual(epub.starts, [0, 0.25, 0.25, 0.7])        XCTAssertNil(epub.pages)        XCTAssertEqual(ReaderSeekMeasures.of(firsts: [0, 0.6, 0.5], resources: 3, positions: 9, pdf: false).starts, [0, 0.6, 0.6])        XCTAssertNil(ReaderSeekMeasures.of(firsts: [0, 0.5], resources: 3, positions: 9, pdf: false).starts)        XCTAssertNil(ReaderSeekMeasures.of(firsts: [], resources: 0, positions: 0, pdf: false).starts)        let pdf = ReaderSeekMeasures.of(firsts: [0], resources: 1, positions: 212, pdf: true)        XCTAssertEqual(pdf.starts, [0])        XCTAssertEqual(pdf.pages, 212)        XCTAssertNil(ReaderSeekMeasures.of(firsts: [0], resources: 1, positions: 0, pdf: true).pages)    }    /// RULE: A chapter start is crossed when the count of starts at or before the finger changes, either way.    func testAChapterStartCrossed() {        let ticks = [0.0, 0.4, 0.8]        XCTAssertEqual(ReaderSeekMeasures.ticksPassed(ticks, at: 0.39), 1)        XCTAssertEqual(ReaderSeekMeasures.ticksPassed(ticks, at: 0.4), 2)        XCTAssertEqual(ReaderSeekMeasures.ticksPassed(ticks, at: 1), 3)        XCTAssertEqual(ReaderSeekMeasures.ticksPassed([], at: 0.5), 0)    }    /// RULE: The slip floats over the finger and stays whole on the screen: at either side it stops its own half and the gutter short; a slip as wide as the row sits in its middle.    func testTheSlipStaysOnTheScreen() {        XCTAssertEqual(SlipPlacement.centre(200, slip: 120, row: 390, gutter: 12), 200)        XCTAssertEqual(SlipPlacement.centre(4, slip: 120, row: 390, gutter: 12), 72)        XCTAssertEqual(SlipPlacement.centre(388, slip: 120, row: 390, gutter: 12), 318)        XCTAssertEqual(SlipPlacement.centre(10, slip: 366, row: 390, gutter: 12), 195, "the largest text: the slip fills the row and sits in its middle")    }}