SPB Git

Repository Public

UpgradeTests.swift 15.0 KB 186 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/AppTests/UpgradeTests.swift3// Description: Hosted tests of the 1.0.2 → 2.0 upgrade path on real files: migration of a data directory written by the shipped 1.0.2 build (store v1 → v2, backup kept, nothing rewritten), the blocking screen when the database cannot be opened (nothing written), and the session-token keychain migration.4import XCTest5import SQLite36import SwiftUI7import CryptoKit8import SPNotesDocument9import SPNotesStore10import SPNotesAPI11import SPNotesSync12@testable import SPNotes1314/// A copy of `Fixtures/v1-1.0.2` (written by app 085a850 + SPNotesCore b06b1ed, generator kept beside it) in a fresh directory.15@MainActor func copyOfShippedDataDirectory() throws -> URL {16    let source = try must(Bundle(for: UpgradeTests.self).url(forResource: "v1-1.0.2", withExtension: nil, subdirectory: "Fixtures"), "fixture missing from the test bundle")17    let dir = FileManager.default.temporaryDirectory.appendingPathComponent("spnotes-upgrade-\(UUID().uuidString)", isDirectory: true)18    try FileManager.default.copyItem(at: source, to: dir)19    return dir20}2122/// Direct SQLite access, independent of the store under test.23func sqlite(_ path: String, _ sql: String, readOnly: Bool = true) throws -> [[String]] {24    var db: OpaquePointer?25    guard sqlite3_open_v2(path, &db, readOnly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE, nil) == SQLITE_OK else { throw NSError(domain: "sqlite", code: 1) }26    defer { sqlite3_close(db) }27    var st: OpaquePointer?28    guard sqlite3_prepare_v2(db, sql, -1, &st, nil) == SQLITE_OK else { throw NSError(domain: "sqlite", code: 2, userInfo: [NSLocalizedDescriptionKey: String(cString: sqlite3_errmsg(db))]) }29    defer { sqlite3_finalize(st) }30    var rows: [[String]] = []31    while sqlite3_step(st) == SQLITE_ROW {32        rows.append((0..<sqlite3_column_count(st)).map { i in33            if sqlite3_column_type(st, i) == SQLITE_BLOB, let p = sqlite3_column_blob(st, i) { return Data(bytes: p, count: Int(sqlite3_column_bytes(st, i))).base64EncodedString() }34            return sqlite3_column_text(st, i).map { String(cString: $0) } ?? "<null>"35        })36    }37    return rows38}39func sha(_ url: URL) -> String { (try? Data(contentsOf: url)).map { SHA256.hash(data: $0).map { String(format: "%02x", $0) }.joined() } ?? "<missing>" }4041final class UpgradeTests: XCTestCase {42    static let v1Tables = ["pages", "page_bases", "tags", "page_tags", "attachments", "ops", "conflicts", "local_versions", "reminders", "meta"]4344    @MainActor func testADataDirectoryWrittenBy102IsMigratedWithItsBackupAndNothingIsRewritten() async throws {45        let dir = try copyOfShippedDataDirectory(); defer { try? FileManager.default.removeItem(at: dir) }46        let db = dir.appendingPathComponent("notes-localhost.sqlite").path47        XCTAssertEqual(try sqlite(db, "PRAGMA user_version"), [["1"]], "the fixture is a schema v1 database")48        var before: [String: [[String]]] = [:]49        for t in Self.v1Tables { before[t] = try sqlite(db, "SELECT * FROM \(t) ORDER BY 1, 2") }50        XCTAssertEqual(before["pages"]?.count, 7); XCTAssertEqual(before["attachments"]?.count, 1)5152        let b = await Bench(server: false, dir: dir)53        XCTAssertNil(b.env.storeFailure); XCTAssertFalse(b.env.isBlocked)54        let report = try must(b.env.storeReport)55        XCTAssertEqual(report.migratedFrom, 1); XCTAssertEqual(report.schemaVersion, 2)56        let backup = try must(report.backupURL)57        XCTAssertEqual(backup.lastPathComponent, "notes-localhost.sqlite.backup-v1")58        XCTAssertEqual(try sqlite(backup.path, "PRAGMA user_version"), [["1"]], "the backup is the v1 database")59        XCTAssertEqual(try sqlite(backup.path, "SELECT * FROM pages ORDER BY 1, 2"), before["pages"], "the backup holds every page as it was")60        XCTAssertEqual(try sqlite(backup.path, "PRAGMA quick_check"), [["ok"]])61        XCTAssertEqual(StorageBackup.list(for: b.env.storeURL).map(\.url.lastPathComponent), ["notes-localhost.sqlite.backup-v1"], "Réglages → Données lists it")6263        // every row of every v1 table is byte-identical after the migration64        try await b.env.store.checkpoint()65        XCTAssertEqual(try sqlite(db, "PRAGMA user_version"), [["2"]])66        for t in Self.v1Tables { XCTAssertEqual(try sqlite(db, "SELECT * FROM \(t) ORDER BY 1, 2"), before[t], "table \(t) changed") }67        let integrity = try await b.env.store.integrityCheck(); XCTAssertTrue(integrity.isEmpty, "\(integrity)")6869        // the library of 1.0.2 is all there through the 2.0 code path70        XCTAssertEqual(b.env.library.counts.all, 5); XCTAssertEqual(b.env.library.counts.trash, 1); XCTAssertEqual(b.env.library.counts.archived, 1)71        XCTAssertEqual(Set(b.env.library.tags.map(\.name)), ["cuisine", "important"])72        let hits = try await b.env.store.search(query: "levain"); XCTAssertEqual(hits.map(\.page.title), ["Recette de pain"])73        let bread = try must(hits.first?.id)7475        // opening a 1.x note (lists without item ids) does not make it dirty and rewrites nothing (SPEC §7)76        let stored = try must(try await b.env.store.page(id: bread))77        let s = await b.session(bread)78        XCTAssertFalse(s.isDirty); XCTAssertEqual(s.saveState, .saved); XCTAssertFalse(s.isUnreadable)79        try await Task.sleep(for: .milliseconds(600)); await b.env.flushAll()80        let untouched = try must(try await b.env.store.page(id: bread))81        XCTAssertEqual(untouched.content, stored.content, "loading assigned list-item ids in memory only"); XCTAssertEqual(untouched.editSeq, stored.editSeq)82        XCTAssertEqual(s.editor.document().plainText(), stored.plainText, "the editor shows the text 1.0.2 stored")83        // the attachment of 1.0.2 is still on disk where the database says84        let att = try must(try await b.env.store.attachments(pageId: try must(b.env.library.notes.first { $0.title == "Idées de thèse" }?.id)).first)85        XCTAssertTrue(FileManager.default.fileExists(atPath: b.env.files.url(relativePath: try must(att.localPath)).path))8687        // the first real edit saves a valid 3.0 document: every list item now has its id88        b.end(s); b.type(s, " Bon appétit."); await s.saveNow(reason: nil)89        let edited = try NoteDocument(json: try must(try await b.env.store.page(id: bread)).content)90        XCTAssertTrue(edited.schemaViolations().isEmpty, "\(edited.schemaViolations())")91        XCTAssertTrue(edited.lines.flatMap(\.path).filter { $0.type == "listItem" || $0.type == "taskItem" }.allSatisfy { if case .string(let id)? = $0.attrs["id"] { return id.count == 10 }; return false })92        XCTAssertTrue(edited.plainText().hasSuffix("Bon appétit."))9394        // second launch: no second migration; the backup is kept (30 days), the notes and the edit are there95        await b.relaunch(flush: true, offline: false)96        XCTAssertNil(b.env.storeFailure); XCTAssertNil(b.env.storeReport?.migratedFrom)97        XCTAssertEqual(b.env.storeReport?.backupURL?.lastPathComponent, "notes-localhost.sqlite.backup-v1")98        XCTAssertTrue(FileManager.default.fileExists(atPath: backup.path))99        let again = await b.text(bread); XCTAssertTrue(again.hasSuffix("Bon appétit."))100        await b.env.store.close()101    }102103    @MainActor func testADatabaseThatCannotBeOpenedBlocksTheApplicationAndNothingIsWritten() async throws {104        // 1. a file written by a newer version of the app105        let dir = try copyOfShippedDataDirectory(); defer { try? FileManager.default.removeItem(at: dir) }106        let db = dir.appendingPathComponent("notes-localhost.sqlite")107        _ = try sqlite(db.path, "PRAGMA user_version = 99", readOnly: false)108        let pagesBefore = try sqlite(db.path, "SELECT * FROM pages ORDER BY 1")109        let b = await Bench(server: false, dir: dir)110        XCTAssertEqual(b.env.storeFailure, .newerSchema(found: 99, supported: 2)); XCTAssertTrue(b.env.isBlocked); XCTAssertNil(b.env.storeReport)111        // every way to write is closed: no note, no session, no quick note, no service, no import, no launch sequence112        let created = await b.env.library.newNote(title: "ne doit pas exister"); XCTAssertNil(created)113        let opened = await b.env.session(for: "13ace880-82c8-4957-8337-073a5ad2764f"); XCTAssertNil(opened)114        let quick = QuickNoteModel(env: b.env); quick.text = "tapé dans le vide"; await quick.save(); XCTAssertNil(quick.noteId)115        let md = dir.appendingPathComponent("import.md"); try "# Titre\n\ntexte".write(to: md, atomically: true, encoding: .utf8)116        let imported = await Exporter(env: b.env).importFiles([md]); XCTAssertTrue(imported.isEmpty)117        b.env.started = false; await b.env.start(); XCTAssertFalse(b.env.started, "the launch sequence (sync, library) does not start")118        // the user's file is exactly as it was, and no backup was made of a file this build does not understand119        XCTAssertEqual(try sqlite(db.path, "PRAGMA user_version"), [["99"]])120        XCTAssertEqual(try sqlite(db.path, "SELECT * FROM pages ORDER BY 1"), pagesBefore)121        XCTAssertTrue(StoreBackup.existingBackups(of: db).isEmpty)122        // the blocking screen is what the window shows, with the explanation123        XCTAssertEqual(MainWindow.root(for: b.env), .storeFailure(.newerSchema(found: 99, supported: 2)), "the window shows the blocking error and nothing of the library")124        let shot = renderOffscreen(StoreFailureView(error: try must(b.env.storeFailure)).environment(b.env), size: CGSize(width: 900, height: 640), name: "store-failure")125        XCTAssertGreaterThan(shot.distinctColors, 8, "the blocking screen draws its explanation")126        XCTAssertTrue(StoreOpenError.newerSchema(found: 99, supported: 2).userMessage.contains("version plus récente"))127128        // 2. a damaged file: typed error, bytes untouched129        let dir2 = FileManager.default.temporaryDirectory.appendingPathComponent("spnotes-corrupt-\(UUID().uuidString)", isDirectory: true); defer { try? FileManager.default.removeItem(at: dir2) }130        try FileManager.default.createDirectory(at: dir2, withIntermediateDirectories: true)131        let bad = dir2.appendingPathComponent("notes-localhost.sqlite")132        try Data((0..<8192).map { UInt8(truncatingIfNeeded: $0 &* 31 &+ 7) }).write(to: bad)133        let digest = sha(bad)134        let c = await Bench(server: false, dir: dir2)135        guard case .corrupt? = c.env.storeFailure else { return XCTFail("expected .corrupt, got \(String(describing: c.env.storeFailure))") }136        let none = await c.env.library.newNote(title: "x"); XCTAssertNil(none)137        XCTAssertEqual(sha(bad), digest, "a damaged database is left exactly as it is")138        XCTAssertEqual(try FileManager.default.contentsOfDirectory(atPath: dir2.path).filter { !$0.hasPrefix("files-") }.sorted(), ["notes-localhost.sqlite"], "nothing was created beside it")139    }140141    @MainActor func testTheSessionTokenMovesToTheNewKeychainWithoutEverSigningOut() throws {142        final class Broken: SessionTokenStore, @unchecked Sendable { func load() -> String? { nil }; func save(_ token: String) throws { throw KeychainError(status: -34018) }; func clear() throws { throw KeychainError(status: -34018) } }143        final class Amnesic: SessionTokenStore, @unchecked Sendable { func load() -> String? { nil }; func save(_ token: String) throws {}; func clear() throws {} }   // accepts writes, returns nothing144        // 1.0.2 left its token in the legacy store: 2.0 reads it, copies it, verifies, then removes the old item145        var legacy = InMemoryTokenStore(token: "tok-1.0.2"), primary = InMemoryTokenStore()146        var store = MigratingTokenStore(primary: primary, legacy: legacy)147        XCTAssertEqual(store.load(), "tok-1.0.2"); XCTAssertEqual(primary.load(), "tok-1.0.2"); XCTAssertNil(legacy.load())148        XCTAssertEqual(store.load(), "tok-1.0.2")149        try store.save("rotated"); XCTAssertEqual(primary.load(), "rotated"); XCTAssertNil(legacy.load())150        try store.clear(); XCTAssertNil(store.load())151        // the new keychain refuses everything (unsigned build, missing entitlement): the 1.x item keeps working, nothing is lost152        legacy = InMemoryTokenStore(token: "tok-1.0.2"); store = MigratingTokenStore(primary: Broken(), legacy: legacy)153        XCTAssertEqual(store.load(), "tok-1.0.2"); XCTAssertEqual(legacy.load(), "tok-1.0.2", "the old item is not removed when the copy failed")154        try store.save("rotated"); XCTAssertEqual(legacy.load(), "rotated"); XCTAssertEqual(store.load(), "rotated")155        XCTAssertNoThrow(try store.clear()); XCTAssertNil(store.load())156        // the new keychain accepts the write but cannot give it back: the old item is kept157        legacy = InMemoryTokenStore(token: "tok-1.0.2"); store = MigratingTokenStore(primary: Amnesic(), legacy: legacy)158        XCTAssertEqual(store.load(), "tok-1.0.2"); XCTAssertEqual(legacy.load(), "tok-1.0.2")159        try store.save("rotated"); XCTAssertEqual(store.load(), "rotated"); XCTAssertEqual(legacy.load(), "rotated")160        // a build without the keychain entitlement (Debug, tests) uses exactly the 1.x store161        primary = InMemoryTokenStore(); _ = primary162        XCTAssertNil(SessionKeychain.entitledAccessGroup(), "the ad hoc test host has no keychain access group")163        XCTAssertTrue(SessionKeychain.store(configuration: APIConfiguration(baseURL: URL(string: "http://localhost:9")!, userAgent: "t")) is KeychainTokenStore)164    }165}166167/// Renders a SwiftUI view in a window that is never put on screen; the PNG goes to `SPNOTES_SHOT_DIR` when set.168@MainActor func renderOffscreen<V: View>(_ view: V, size: CGSize, dark: Bool = false, name: String) -> (image: NSBitmapImageRep?, distinctColors: Int) {169    let host = NSHostingView(rootView: view.frame(width: size.width, height: size.height))170    let window = NSWindow(contentRect: CGRect(origin: .zero, size: size), styleMask: [.borderless], backing: .buffered, defer: false)171    window.appearance = NSAppearance(named: dark ? .darkAqua : .aqua)172    window.contentView = host; host.layoutSubtreeIfNeeded()173    RunLoop.current.run(until: Date().addingTimeInterval(0.25)); host.layoutSubtreeIfNeeded()174    guard let rep = host.bitmapImageRepForCachingDisplay(in: host.bounds) else { return (nil, 0) }175    host.cacheDisplay(in: host.bounds, to: rep)176    if ProcessInfo.processInfo.environment["SPNOTES_SHOT_DIR"] != nil {177        // the test host is sandboxed: the pictures go to its own temporary directory (…/Containers/<debug id>/Data/tmp/spnotes-shots)178        let dir = FileManager.default.temporaryDirectory.appendingPathComponent("spnotes-shots", isDirectory: true); try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)179        try? rep.representation(using: .png, properties: [:])?.write(to: dir.appendingPathComponent("\(name)-\(dark ? "dark" : "light").png"))180    }181    var colors = Set<UInt32>()182    for y in stride(from: 0, to: rep.pixelsHigh, by: 9) { for x in stride(from: 0, to: rep.pixelsWide, by: 9) { if let c = rep.colorAt(x: x, y: y)?.usingColorSpace(.sRGB) { colors.insert(UInt32(c.redComponent * 31) << 10 | UInt32(c.greenComponent * 31) << 5 | UInt32(c.blueComponent * 31)) } } }183    window.contentView = nil184    return (rep, colors.count)185}186
Files main
Menu

Theme