// SPDX-License-Identifier: GPL-3.0-or-later import CryptoKit import Foundation import PDFKit import SQLite3 import SwiftUI import XCTest @MainActor final class ArchiveReaderTests: XCTestCase { private var directory: URL! private var cache: ArchiveAssetCache! override func setUpWithError() throws { directory = FileManager.default.temporaryDirectory.appendingPathComponent("ArchiveReader-" + UUID().uuidString) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) let config = URLSessionConfiguration.ephemeral config.protocolClasses = [ArchiveFixtureTransport.self] cache = ArchiveAssetCache(directory: directory, configuration: config) } override func tearDownWithError() throws { try FileManager.default.removeItem(at: directory) } private func record(_ name: String, format: String, bytes: Data, member: String? = nil, range: ArchiveMemberRange? = nil) -> ArchiveDocument { let digest = SHA256.hash(data: bytes).map { String(format: "%02x", $0) }.joined() return ArchiveDocument(id: digest, title: name, collection: "Books", sourceURL: URL(string: "https://colorcomputerarchive.com/repo/Documents/Books/" + name)!, memberPath: member, pageCount: 1, deliveryURL: nil, deliveryBytes: nil, format: format, sourceBytes: Int64(bytes.count), memberRange: range) } private func waitFor(_ condition: () -> Bool) async throws { let limit = ContinuousClock.now + .seconds(5) while !condition() { guard ContinuousClock.now < limit else { throw ReaderTimeout() } try await Task.sleep(for: .milliseconds(10)) } } private struct ReaderTimeout: Error {} func testSavedPDFPageAndZoomResumeButExplicitLocationWins() async throws { let pdf = PDFDocument() for color in [NSColor.white, .gray] { let image = NSImage(size: NSSize(width: 240, height: 900), flipped: false) { rect in color.setFill(); rect.fill(); return true } pdf.insert(try XCTUnwrap(PDFPage(image: image)), at: pdf.pageCount) } let bytes = try XCTUnwrap(pdf.dataRepresentation()) let digest = SHA256.hash(data: bytes).map { String(format: "%02x", $0) }.joined() let item = ArchiveDocument(id: digest, title: "Resume", collection: "Books", sourceURL: URL(string: "https://colorcomputerarchive.com/repo/Documents/Books/Resume.pdf")!, memberPath: nil, pageCount: 2, deliveryURL: URL(string: "https://vccplus.brianholman.com/dl/software/CoCoLibrary/Documents/Books/Resume.pdf")!, deliveryBytes: Int64(bytes.count)) try bytes.write(to: directory.appendingPathComponent(digest + ".pdf")) let store = LibraryContinuityStore(directory: directory.appendingPathComponent("State")) store.setReading(LibraryReadingPosition(documentID: digest, page: 2, offset: 0.7, zoom: 1.75, autoScales: false), for: "archive:" + digest) let resumed = ArchiveReaderModel(remembersPage: true, cache: cache, continuity: store) defer { resumed.reset() } resumed.view.frame = NSRect(x: 0, y: 0, width: 300, height: 300) resumed.open(item, page: resumed.savedPage(for: digest), resumePosition: true) try await waitFor { resumed.progress == nil } XCTAssertNil(resumed.error) XCTAssertEqual(resumed.page, 2) XCTAssertEqual(resumed.view.scaleFactor, 1.75, accuracy: 0.01) resumed.open(item, page: 1) XCTAssertEqual(resumed.page, 1) XCTAssertTrue(resumed.view.currentPage === resumed.document?.page(at: 0)) } func testSelectingPDFPagesReusesTheVerifiedDocumentAndOpensTheCitedPage() async throws { let pdf = PDFDocument() for color in [NSColor.white, .gray] { let image = NSImage(size: NSSize(width: 240, height: 320), flipped: false) { rect in color.setFill() rect.fill() return true } pdf.insert(try XCTUnwrap(PDFPage(image: image)), at: pdf.pageCount) } let bytes = try XCTUnwrap(pdf.dataRepresentation()) let digest = SHA256.hash(data: bytes).map { String(format: "%02x", $0) }.joined() let source = URL(string: "https://colorcomputerarchive.com/repo/Documents/Books/Pages.pdf")! let delivery = URL(string: "https://vccplus.brianholman.com/dl/software/CoCoLibrary/Documents/Books/Pages.pdf")! let item = ArchiveDocument(id: digest, title: "Pages", collection: "Books", sourceURL: source, memberPath: nil, pageCount: 2, deliveryURL: delivery, deliveryBytes: Int64(bytes.count)) try bytes.write(to: directory.appendingPathComponent(digest + ".pdf")) let model = ArchiveReaderModel(remembersPage: false, cache: cache) defer { model.reset() } model.view.frame = NSRect(x: 0, y: 0, width: 300, height: 300) model.open(item, page: 1) model.open(item, page: 2) try await waitFor { model.progress == nil } let rendered = try XCTUnwrap(model.document) XCTAssertTrue(model.view.document === rendered) XCTAssertEqual(rendered.pageCount, 2) XCTAssertEqual(model.page, 2) XCTAssertTrue(model.view.currentPage === rendered.page(at: 1)) model.open(item, page: 1) XCTAssertTrue(model.document === rendered) XCTAssertNil(model.progress) XCTAssertEqual(model.page, 1) XCTAssertTrue(model.view.currentPage === rendered.page(at: 0)) } func testExplicitSamePageDuringPDFResumeDiscardsSavedZoomAndOffset() async throws { let pdf = PDFDocument() let image = NSImage(size: NSSize(width: 240, height: 900), flipped: false) { rect in NSColor.white.setFill(); rect.fill(); return true } pdf.insert(try XCTUnwrap(PDFPage(image: image)), at: 0) let bytes = try XCTUnwrap(pdf.dataRepresentation()) let digest = SHA256.hash(data: bytes).map { String(format: "%02x", $0) }.joined() let item = ArchiveDocument(id: digest, title: "Same page", collection: "Books", sourceURL: URL(string: "https://colorcomputerarchive.com/repo/Documents/Books/Same.pdf")!, memberPath: nil, pageCount: 1, deliveryURL: URL(string: "https://vccplus.brianholman.com/dl/software/CoCoLibrary/Documents/Books/Same.pdf")!, deliveryBytes: Int64(bytes.count)) try bytes.write(to: directory.appendingPathComponent(digest + ".pdf")) let store = LibraryContinuityStore(directory: directory.appendingPathComponent("State")) store.setReading(LibraryReadingPosition(documentID: digest, page: 1, offset: 0.7, horizontalOffset: 0.6, zoom: 9, autoScales: false), for: "archive:" + digest) let model = ArchiveReaderModel(remembersPage: true, cache: cache, continuity: store) defer { model.reset() } model.view.frame = NSRect(x: 0, y: 0, width: 300, height: 300) model.open(item, page: 1, resumePosition: true) XCTAssertNotNil(model.progress) // Both calls occur before the load task gets its first execution turn: // same bytes and same page, but the second action is an explicit link. model.open(item, page: 1, resumePosition: false) try await waitFor { model.progress == nil } XCTAssertNil(model.error) XCTAssertEqual(model.page, 1) XCTAssertTrue(model.view.autoScales, "An explicit target must not inherit the pending resume's manual zoom") XCTAssertNotEqual(model.view.scaleFactor, 9) let position = try XCTUnwrap(store.reading(for: "archive:" + digest)) XCTAssertNotEqual(position.offset, 0.7, "An explicit target must not restore the old within-page offset") XCTAssertNotEqual(position.horizontalOffset, 0.6) } func testSelectingTextThenCachedImageReplacesThePreview() async throws { let model = ArchiveReaderModel(remembersPage: false, cache: cache) defer { model.reset() } let text = record("Notes.txt", format: "text", bytes: ArchiveFixtureTransport.listing) model.open(text, page: 1) try await waitFor { model.progress == nil } XCTAssertNil(model.error) guard case .text(let contents) = model.preview else { return XCTFail("Expected the selected text in the reader") } XCTAssertEqual(contents, "A tide chart for the CoCo.\n") // The transport has no image response. A verified retained copy must open offline. let png = Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAAAAAC4HznGAAAAEElEQVR4nGNQcEhgaFhzBAAH5AKxE0HCuwAAAABJRU5ErkJggg==")! let image = record("Map.png", format: "image", bytes: png) try png.write(to: directory.appendingPathComponent(image.id + ".png")) model.open(image, page: 1) XCTAssertNil(model.preview, "Selection changes must clear the previous document immediately") try await waitFor { model.progress == nil } XCTAssertNil(model.error) guard case .image(let displayed) = model.preview else { return XCTFail("The cached image was not rendered") } XCTAssertEqual(displayed.width, 3) XCTAssertEqual(displayed.height, 2) XCTAssertEqual(model.referenceID, image.id) XCTAssertNil(model.document) model.open(image, page: 2) try await waitFor { model.progress == nil } XCTAssertNil(model.preview, "A missing image frame must not be silently replaced by frame one") XCTAssertNotNil(model.error) model.reset() XCTAssertNil(model.preview) XCTAssertNil(model.referenceID) } func testExplicitTextNavigationOverridesInFlightAndVisibleResume() async throws { _ = NSApplication.shared let bytes = Data((0..<250).map { "Line \($0): Archive reading position fixture." }.joined(separator: "\n").utf8) let item = record("Resume.txt", format: "text", bytes: bytes) try bytes.write(to: directory.appendingPathComponent(item.id + ".txt")) let store = LibraryContinuityStore(directory: directory.appendingPathComponent("State")) store.setReading(LibraryReadingPosition(documentID: item.id, offset: 0.65), for: "archive:" + item.id) let model = ArchiveReaderModel(remembersPage: true, cache: cache, continuity: store) defer { model.reset() } model.open(item, page: 1, resumePosition: true) model.open(item, page: 1, resumePosition: false) try await waitFor { model.progress == nil } XCTAssertNil(model.error) XCTAssertEqual(model.inlinePosition?.offset, 0, "Explicit intent must replace the in-flight resume's saved inline position") model.updateInlinePosition(offset: 0.65) let host = NSHostingView(rootView: ArchiveReaderPane(record: item, hit: nil, reader: model, requestedPage: 1, catalog: ArchiveLibraryCatalog(url: nil), resumesPosition: true)) host.frame = NSRect(x: 0, y: 0, width: 700, height: 500) func textView(_ root: NSView) -> NSTextView? { if let text = root as? NSTextView, text.accessibilityIdentifier() == "archive-inline-text" { return text } return root.subviews.lazy.compactMap { textView($0) }.first } host.layoutSubtreeIfNeeded() try await waitFor { host.layoutSubtreeIfNeeded() return textView(host)?.enclosingScrollView?.contentView.bounds.minY ?? 0 > 100 } let resumedText = try XCTUnwrap(textView(host)) let revision = model.inlineNavigationRevision model.open(item, page: 1, resumePosition: true) XCTAssertEqual(model.inlineNavigationRevision, revision) XCTAssertGreaterThan(resumedText.enclosingScrollView?.contentView.bounds.minY ?? 0, 100) model.open(item, page: 1, resumePosition: false) try await waitFor { host.layoutSubtreeIfNeeded() guard let current = textView(host), current !== resumedText else { return false } return (current.enclosingScrollView?.contentView.bounds.minY ?? 1) == 0 } XCTAssertEqual(model.inlinePosition?.offset, 0) withExtendedLifetime(host) {} } func testTextReaderPreservesLegacyEncodingsAndRemovesFloppyPadding() throws { let url = directory.appendingPathComponent("Letter.txt") let accented = "Café\n£5" let utf8 = Data([0xef, 0xbb, 0xbf]) + Data("Café\r£5".utf8) let utf16 = Data([0xff, 0xfe]) + (try XCTUnwrap("Café\r£5".data(using: .utf16LittleEndian))) let legacy = Data([0x43, 0x61, 0x66, 0xe9, 0x0d, 0x0a, 0xa3, 0x35, 0x1a, 0x00]) for bytes in [utf8, utf16, legacy] { try bytes.write(to: url) guard case .text(let contents) = try ArchiveInlineContent.loadText(at: url) else { return XCTFail("Expected a selectable text preview") } XCTAssertEqual(contents, accented) } try Data([0x93, 0x52, 0x55, 0x4e, 0x94]).write(to: url) guard case .text(let instruction) = try ArchiveInlineContent.loadText(at: url) else { return XCTFail("Expected Windows-1252 walkthrough text") } XCTAssertEqual(instruction, "“RUN”") } func testRapidSelectionCancelsAnUnfinishedTransfer() async throws { let first = record("Hold.txt", format: "text", bytes: Data("Slow response".utf8)) let next = record("Notes.txt", format: "text", bytes: ArchiveFixtureTransport.listing) let model = ArchiveReaderModel(remembersPage: false, cache: cache) defer { model.reset() } model.open(first, page: 1) try await waitFor { if case .downloading = model.progress { return true } return false } model.open(next, page: 1) try await waitFor { model.progress == nil } XCTAssertEqual(model.referenceID, next.id) XCTAssertNil(model.error) guard case .text(let contents) = model.preview else { return XCTFail("The next selection did not load") } XCTAssertEqual(contents, "A tide chart for the CoCo.\n") XCTAssertFalse(FileManager.default.fileExists(atPath: directory.appendingPathComponent(first.id + ".txt").path)) } func testDownloadReportsBytesWhileTheRemainingContentIsStillPending() async throws { let chunk = ArchiveFixtureTransport.listingChunk let bytes = chunk + chunk let item = record("Progress.txt", format: "text", bytes: bytes) let receipt = try XCTUnwrap(ArchiveAssetReceipt(document: item)) let total = Int64(bytes.count) let advanced = XCTestExpectation(description: "Download reports a positive, incomplete byte count") let complete = XCTestExpectation(description: "Download reports all expected bytes") let download = Task { try await cache.localCopy(receipt) { event in guard case let .downloading(done, expected) = event, expected == total else { return } if done > 0, done < total { advanced.fulfill() } if done == total { complete.fulfill() } } } defer { download.cancel() } let first = await XCTWaiter.fulfillment(of: [advanced], timeout: 5) XCTAssertEqual(first, .completed, "The download must advance while the second chunk is withheld") NotificationCenter.default.post(name: ArchiveFixtureTransport.releaseSecondChunk, object: nil) guard first == .completed else { return } let local = try await download.value XCTAssertEqual(try Data(contentsOf: local), bytes) await fulfillment(of: [complete], timeout: 2) } func testWorksheetCitationOpensTheSelectedSheetAndChangesSheetsInPlace() async throws { let url = directory.appendingPathComponent("ArchiveLibrary.sqlite") var db: OpaquePointer? XCTAssertEqual(sqlite3_open(url.path, &db), SQLITE_OK) defer { sqlite3_close(db) } let digest = String(repeating: "a", count: 64) let sql = """ PRAGMA application_id=0x56434341; PRAGMA user_version=2; CREATE TABLE document(id INTEGER PRIMARY KEY,sha256 TEXT,title TEXT,collection TEXT,format TEXT,source_url TEXT,page_count INTEGER); CREATE TABLE page(id INTEGER PRIMARY KEY,document_id INTEGER,page_number INTEGER,page_label TEXT,locator_kind TEXT,is_ocr INTEGER); CREATE TABLE reader_section(page_id INTEGER PRIMARY KEY,text TEXT); INSERT INTO document VALUES(1,'\(digest)','Ledger','Books','office-xls','https://colorcomputerarchive.com/repo/Documents/Books/Ledger.xls',2); INSERT INTO page VALUES(1,1,1,'January','worksheet',0),(2,1,2,'February','worksheet',0); INSERT INTO reader_section VALUES(1,'Parts' || char(9) || '3'),(2,'Disks' || char(9) || '8'); """ guard sqlite3_exec(db, sql, nil, nil, nil) == SQLITE_OK else { return XCTFail(String(cString: sqlite3_errmsg(db))) } let catalog = ArchiveLibraryCatalog(url: url) let citation = try await catalog.citation(id: "sha256:" + digest + ":worksheet:2") let hit = try XCTUnwrap(citation) XCTAssertNil(hit.page) XCTAssertEqual(hit.locator, "February") let model = ArchiveReaderModel(remembersPage: false, cache: cache, catalog: catalog) defer { model.reset() } model.open(hit.document, page: hit.locationNumber) try await waitFor { model.progress == nil } XCTAssertEqual(model.page, 2) XCTAssertEqual(model.sectionLabel, "February") guard case .worksheet(let february) = model.preview else { return XCTFail("Expected the cited worksheet") } XCTAssertEqual(february, "Disks\t8") model.open(hit.document, page: 1) try await waitFor { model.progress == nil } XCTAssertEqual(model.page, 1) XCTAssertEqual(model.sectionLabel, "January") guard case .worksheet(let january) = model.preview else { return XCTFail("Expected the first worksheet") } XCTAssertEqual(january, "Parts\t3") model.open(hit.document, page: 3) try await waitFor { model.progress == nil } XCTAssertNil(model.preview) XCTAssertNotNil(model.error) } func testZIPRangeAndWholeArchiveFallbackProduceTheVerifiedMember() async throws { // The fixture archive has one raw-DEFLATE member, Tips/notes.txt, beginning at byte 44. for name in ["Range.zip", "Whole.zip", "Shifted.zip", "Changed.zip"] { let asset = record(name, format: "text", bytes: ArchiveFixtureTransport.listing, member: "Tips/notes.txt", range: .init(offset: 44, compressedBytes: 30, method: 8, archiveBytes: 156)) let receipt = try XCTUnwrap(ArchiveAssetReceipt(document: asset)) if name == "Shifted.zip" || name == "Changed.zip" { do { _ = try await cache.localCopy(receipt) { _ in } XCTFail("Accepted the incorrect response to " + name) } catch { } XCTAssertTrue(try FileManager.default.contentsOfDirectory(atPath: directory.path).isEmpty) } else { let saved = try await cache.localCopy(receipt) { _ in } XCTAssertEqual(try Data(contentsOf: saved), ArchiveFixtureTransport.listing) try FileManager.default.removeItem(at: saved) } } } } private final class ArchiveFixtureTransport: URLProtocol { static let listing = Data("A tide chart for the CoCo.\r\n".utf8) static let listingChunk = Data(repeating: 65, count: 1024 * 1024) static let releaseSecondChunk = Notification.Name("ArchiveReaderTests.releaseSecondChunk") private var releaseObserver: NSObjectProtocol? static let zip = Data(base64Encoded: "UEsDBBQAAAAIAAAAIVyuKpHqHgAAABwAAAAOAAAAVGlwcy9ub3Rlcy50eHRzVCjJTElVSM5ILCpRSMsvUijJSFVwznfO1+PlAgBQSwECFAMUAAAACAAAACFcriqR6h4AAAAcAAAADgAAAAAAAAAAAAAAgAEAAAAAVGlwcy9ub3Rlcy50eHRQSwUGAAAAAAEAAQA8AAAASgAAAAAA")! override class func canInit(with request: URLRequest) -> Bool { true } override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } override func startLoading() { guard let url = request.url else { return } var headers: [String: String] = [:] var status = 200 var payload = Data() switch url.lastPathComponent { case "Hold.txt": return // Intentionally pending until the user chooses another row. case "Notes.txt": payload = Self.listing case "Progress.txt": let headers = ["Content-Length": String(2 * Self.listingChunk.count)] let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: headers)! releaseObserver = NotificationCenter.default.addObserver(forName: Self.releaseSecondChunk, object: nil, queue: nil) { [weak self] _ in guard let self else { return } self.client?.urlProtocol(self, didLoad: Self.listingChunk) self.client?.urlProtocolDidFinishLoading(self) } client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) client?.urlProtocol(self, didLoad: Self.listingChunk) return case "Range.zip", "Whole.zip", "Shifted.zip", "Changed.zip": guard request.value(forHTTPHeaderField: "Range") == "bytes=44-73" else { client?.urlProtocol(self, didFailWithError: URLError(.badURL)) return } if url.lastPathComponent == "Whole.zip" { payload = Self.zip } else { status = 206 payload = Self.zip.subdata(in: 44..<74) headers["Content-Range"] = url.lastPathComponent == "Shifted.zip" ? "bytes 45-74/156" : "bytes 44-73/156" // Valid DEFLATE with an altered first character and the original decoded length. if url.lastPathComponent == "Changed.zip" { payload[0] ^= 0x10 } } default: client?.urlProtocol(self, didFailWithError: URLError(.notConnectedToInternet)) return } headers["Content-Length"] = String(payload.count) let response = HTTPURLResponse(url: url, statusCode: status, httpVersion: "HTTP/1.1", headerFields: headers)! client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) client?.urlProtocol(self, didLoad: payload) client?.urlProtocolDidFinishLoading(self) } override func stopLoading() { if let releaseObserver { NotificationCenter.default.removeObserver(releaseObserver) } } }