SPB Git

Repository Public

CoreAdoptionTests.swift 22.0 KB 405 lines swift

1// SP Notes iOS2// File: Tests/CoreAdoptionTests.swift3// Description: Adoption of SPNotesCore 2.0 by the app: the v1 → v2 migration of a real 1.0 database inside a container shared4//              with the widgets and the share extension, the typed open failures (no empty substitute store), loading a 2.x5//              document without dirtying it, scoped invalidation measured on a typing session, debounced widget reloads, and6//              the colour sets of the asset catalog against the design tokens.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import Foundation11import SQLite312import SPNotesCore13import SPNotesDesign14import Testing15import UIKit16@testable import SPNotes1718private final class BundleMarker {}1920/// A throw-away "App Group container" holding a COPY of the database written by the shipped 1.0 code path21/// (`Fixtures/v1.sqlite`: the fixture of SPNotesCore, generated at commit 267a673 — the Core of SP Notes iOS 1.0.0 (1)).22struct V1Container {23    let directory: URL24    var database: URL { directory.appendingPathComponent("notes.sqlite") }2526    init() throws {27        directory = FileManager.default.temporaryDirectory.appendingPathComponent("spnotes-v1-\(UUID().uuidString)", isDirectory: true)28        try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)29        let fixture = try #require(Bundle(for: BundleMarker.self).url(forResource: "v1", withExtension: "sqlite"))30        try FileManager.default.copyItem(at: fixture, to: database)31    }3233    func configuration() -> AppConfiguration { AppConfiguration(api: .production(appVersion: "tests"), dataDirectory: directory) }3435    func scalar(_ sql: String) -> Int {36        var db: OpaquePointer?37        guard sqlite3_open_v2(database.path, &db, SQLITE_OPEN_READWRITE, nil) == SQLITE_OK else { return -1 }38        defer { sqlite3_close(db) }39        var statement: OpaquePointer?40        guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return -1 }41        defer { sqlite3_finalize(statement) }42        return sqlite3_step(statement) == SQLITE_ROW ? Int(sqlite3_column_int64(statement, 0)) : -143    }4445    var userVersion: Int { scalar("PRAGMA user_version") }46    func cleanUp() { try? FileManager.default.removeItem(at: directory) }47}4849@MainActor50@Suite("Core 2.0 — migration de la base 1.0") struct StoreMigrationTests {51    @Test func theAppMigratesACopyOfA10DatabaseAndKeepsEveryRow() async throws {52        let container = try V1Container()53        defer { container.cleanUp() }54        #expect(container.userVersion == 1)55        let before = (pages: container.scalar("SELECT count(*) FROM pages"), ops: container.scalar("SELECT count(*) FROM ops"),56                      reminders: container.scalar("SELECT count(*) FROM reminders"), attachments: container.scalar("SELECT count(*) FROM attachments"),57                      versions: container.scalar("SELECT count(*) FROM local_versions"), conflicts: container.scalar("SELECT count(*) FROM conflicts"))58        #expect(before.pages == 6)5960        let app = try AppModel(configuration: container.configuration())61        #expect(app.openReport?.migratedFrom == 1)62        let backup = try #require(app.openReport?.backupURL)63        #expect(FileManager.default.fileExists(atPath: backup.path))64        #expect(backup.lastPathComponent == "notes.sqlite.backup-v1")65        #expect(container.userVersion == 2)66        #expect(container.scalar("SELECT count(*) FROM pages") == before.pages)67        #expect(container.scalar("SELECT count(*) FROM ops") + container.scalar("SELECT count(*) FROM ops_parked WHERE 0") == before.ops)68        #expect(container.scalar("SELECT count(*) FROM reminders") == before.reminders)69        #expect(container.scalar("SELECT count(*) FROM attachments") == before.attachments)70        #expect(container.scalar("SELECT count(*) FROM local_versions") == before.versions)71        #expect(container.scalar("SELECT count(*) FROM conflicts") == before.conflicts)7273        // the 1.0 notes are readable through the 2.0 API, the search index included74        let draft = try #require(try await app.store.page(id: "00000000-0000-4000-8000-000000000010"))75        #expect(draft.title == "Brouillon local")76        #expect(try await app.store.search(query: "zèbre", limit: 5).contains { $0.page.id == draft.id })77        // and writable: a 2.0 save lands in the same file78        let id = try await app.createNote(title: "Après la migration", document: NoteDocument(markdown: "nouvelle note"))79        #expect(try await app.store.page(id: id) != nil)80        #expect(try await app.repositories.tasks.list(.all).isEmpty)81        await app.store.close()82    }8384    @Test func theWidgetDegradesSafelyOnAV1FileAndReadsItOnceTheAppMigrated() async throws {85        let container = try V1Container()86        defer { container.cleanUp() }87        let bytesBefore = try Data(contentsOf: container.database)88        // the widget extension runs before the app was opened after the update: read-only connection on a v1 file89        let widget = WidgetDataProvider(databaseURL: container.database)90        #expect(await widget.recent(limit: 4) == .unavailable)91        #expect(await widget.note(id: "00000000-0000-4000-8000-000000000010") == nil)92        #expect(await widget.search("zèbre").isEmpty)93        #expect(container.userVersion == 1)94        #expect(try Data(contentsOf: container.database) == bytesBefore, "a read-only extension must not touch a v1 file")95        if case .failed(let error) = NotesStore.open(url: container.database, readOnly: true) {96            guard case .outdatedReadOnly = error else { Issue.record("unexpected: \(error)"); return }97        } else { Issue.record("a read-only open of a v1 file must fail") }9899        let app = try AppModel(configuration: container.configuration())100        // the extension opens seconds after the app migrated: it reads the v2 file and leaves the backup alone101        let content = await widget.recent(limit: 10)102        #expect(!content.notes.isEmpty)103        #expect(content.notes.contains { $0.title == "Brouillon local" })104        #expect(await widget.note(id: "00000000-0000-4000-8000-000000000010")?.title == "Brouillon local")105        #expect(FileManager.default.fileExists(atPath: container.database.path + ".backup-v1"))106        await app.store.close()107    }108109    @Test func aShareMadeBeforeTheMigrationIsImportedAfterIt() async throws {110        let container = try V1Container()111        defer { container.cleanUp() }112        // the share extension never opens the database: it writes into the hand-off folder of the same container113        let inbox = SharedInbox(container: container.directory)114        try inbox.save(SharedItem(title: "Partagé avant la mise à jour", text: "texte partagé", url: nil), files: [])115        #expect(container.userVersion == 1)116        let app = try AppModel(configuration: container.configuration())117        await app.importSharedItems()118        #expect(inbox.pending().isEmpty)119        let hits = try await app.store.search(query: "partagé", limit: 5)120        #expect(hits.contains { $0.page.title == "Partagé avant la mise à jour" })121        #expect(container.scalar("SELECT count(*) FROM pages") == 7)122        await app.store.close()123    }124125    @Test func openingTwiceIsIdempotent() async throws {126        let container = try V1Container()127        defer { container.cleanUp() }128        var app: AppModel? = try AppModel(configuration: container.configuration())129        await app?.store.close()130        app = try AppModel(configuration: container.configuration())131        #expect(app?.openReport?.migratedFrom == nil)132        #expect(container.userVersion == 2)133        #expect(container.scalar("SELECT count(*) FROM pages") == 6)134        await app?.store.close()135    }136}137138@MainActor139@Suite("Core 2.0 — échec d’ouverture : écran bloquant, jamais de base vide") struct StoreFailureTests {140    private func directory() throws -> URL {141        let url = FileManager.default.temporaryDirectory.appendingPathComponent("spnotes-fail-\(UUID().uuidString)", isDirectory: true)142        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)143        return url144    }145146    @Test func aDamagedFileStopsTheAppAndIsLeftUntouched() throws {147        let dir = try directory()148        defer { try? FileManager.default.removeItem(at: dir) }149        let file = dir.appendingPathComponent("notes.sqlite")150        let garbage = Data(repeating: 0x5A, count: 8192)151        try garbage.write(to: file)152        do {153            _ = try AppModel(configuration: AppConfiguration(api: .production(appVersion: "tests"), dataDirectory: dir))154            Issue.record("the model must not be created on a damaged file")155        } catch let error as StoreOpenError {156            guard case .corrupt = error else { Issue.record("unexpected: \(error)"); return }157            let failure = StoreFailure(error)158            #expect(failure.message == error.userMessage)159            #expect(!failure.canRetry)160            #expect(failure.title == "Base locale endommagée")161        }162        #expect(try Data(contentsOf: file) == garbage)163    }164165    @Test func aFileOfANewerVersionIsRefusedWithoutBackupOrChange() throws {166        let container = try V1Container()167        defer { container.cleanUp() }168        var db: OpaquePointer?169        #expect(sqlite3_open(container.database.path, &db) == SQLITE_OK)170        #expect(sqlite3_exec(db, "PRAGMA user_version = 99", nil, nil, nil) == SQLITE_OK)171        sqlite3_close(db)172        #expect(throws: StoreOpenError.newerSchema(found: 99, supported: 2)) {173            _ = try AppModel(configuration: container.configuration())174        }175        #expect(container.userVersion == 99)176        #expect(!FileManager.default.fileExists(atPath: container.database.path + ".backup-v1"))177        #expect(StoreFailure(StoreOpenError.newerSchema(found: 99, supported: 2)).canRetry == false)178    }179180    @Test func transientFailuresOfferARetryAndTheBackupIsNamed() {181        #expect(StoreFailure(StoreOpenError.locked("busy")).canRetry)182        #expect(StoreFailure(StoreOpenError.backupFailed("disk full")).canRetry)183        let failed = StoreFailure(StoreOpenError.migrationFailed(from: 1, to: 2, reason: "x", backupURL: URL(fileURLWithPath: "/tmp/notes.sqlite.backup-v1")))184        #expect(failed.canRetry)185        #expect(failed.backupName == "notes.sqlite.backup-v1")186        #expect(StoreFailure(NSError(domain: "x", code: 1)).message == StoreOpenError.other("").userMessage)187    }188}189190@MainActor191@Suite("Core 2.0 — charger un document 2.x ne le salit pas") struct LoadDoesNotDirtyTests {192    /// A document as a 2.x editor wrote it: list items and task items without block ids.193    static let legacy = #"{"type":"doc","content":[{"type":"paragraph","attrs":{"id":"p1aaaaaaaa"},"content":[{"type":"text","text":"Intro"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","attrs":{"id":"p2aaaaaaaa"},"content":[{"type":"text","text":"un"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"id":"p3aaaaaaaa"},"content":[{"type":"text","text":"deux"}]}]}]},{"type":"taskList","content":[{"type":"taskItem","attrs":{"checked":false},"content":[{"type":"paragraph","attrs":{"id":"p4aaaaaaaa"},"content":[{"type":"text","text":"à faire"}]}]}]}]}"#194195    @Test func openingAndClosingANoteWritesNothing() async throws {196        let app = try makeApp()197        let content = Data(Self.legacy.utf8)198        let page = try await app.store.createPage(NewPage(parentId: nil, section: "notes", title: "Ancienne", content: content, properties: Data("{}".utf8),199                                                          plainText: "Intro un deux à faire", wordCount: 5, preview: "Intro", tags: []))200        let stored = try #require(try await app.store.page(id: page.id))201        let model = NoteEditorModel(noteId: page.id, app: app)202        await model.load()203        // the editor works on a document whose list items now have ids…204        let loaded = model.controller.document()205        #expect(loaded.schemaViolations(strict: true).allSatisfy { $0.contains("textAlign") })   // the fixture omits textAlign; items have ids206        #expect(try loaded.jsonData() != content)207        // …but nothing is pending, and leaving the note writes nothing208        #expect(model.saveState == .saved)209        await model.flushAndWait()210        model.flush()211        try await Task.sleep(for: .milliseconds(900))212        let after = try #require(try await app.store.page(id: page.id))213        #expect(after.content == content)214        #expect(after.updatedAt == stored.updatedAt)215        #expect(after.syncState == stored.syncState)216    }217218    @Test func theIdsAreWrittenWithTheFirstRealEdit() async throws {219        let app = try makeApp()220        let content = Data(Self.legacy.utf8)221        let page = try await app.store.createPage(NewPage(parentId: nil, section: "notes", title: "Ancienne", content: content, properties: Data("{}".utf8),222                                                          plainText: "Intro un deux à faire", wordCount: 5, preview: "Intro", tags: []))223        let model = NoteEditorModel(noteId: page.id, app: app)224        await model.load()225        model.controller.textView.selectedRange = NSRange(location: 5, length: 0)226        model.controller.textView.insertText(" modifiée")227        await model.flushAndWait()228        let after = try #require(try await app.store.page(id: page.id))229        let saved = try NoteDocument(json: after.content)230        #expect(saved.plainText().contains("Intro modifiée"))231        #expect(saved.schemaViolations(strict: true).allSatisfy { $0.contains("textAlign") })232        var withIds = saved233        withIds.ensureBlockIds()234        #expect(try withIds.jsonData() == after.content, "every list item got its id at the save")235    }236}237238@MainActor239@Suite("Invalidation ciblée et widgets") struct ScopedInvalidationTests {240    /// Waits until the store's change stream has been drained by the model (the feed counters stop moving).241    private func settle(_ app: AppModel) async {242        var last = -1243        for _ in 0..<60 {244            try? await Task.sleep(for: .milliseconds(25))245            if app.feed.stats.raw == last { return }246            last = app.feed.stats.raw247        }248    }249250    @Test func typing200CharactersNoLongerRerunsEveryQueryAtEachAutosave() async throws {251        var reloads = 0252        let feed = ChangeFeed(listDelay: .milliseconds(150))253        let widgets = WidgetRefresher(delay: .milliseconds(150)) { reloads += 1 }254        let directory = FileManager.default.temporaryDirectory.appendingPathComponent("spnotes-feed-\(UUID().uuidString)", isDirectory: true)255        let app = try AppModel(configuration: AppConfiguration(api: .production(appVersion: "tests"), dataDirectory: directory, inMemoryStore: true), feed: feed, widgets: widgets)256        app.start()257        let id = try await app.createNote(title: "Frappe", document: NoteDocument(markdown: ""))258        await settle(app)259        try await Task.sleep(for: .milliseconds(400))260        let start = (stats: feed.stats, lists: feed.lists, structure: feed.structure, page: feed.page(id), tags: feed.tags, widgets: reloads, requests: widgets.requests)261262        // 200 characters typed with a pause every 10 characters: 20 autosaves through the editor's save path263        var text = ""264        for burst in 0..<20 {265            text += String(repeating: Character(UnicodeScalar(97 + UInt8(burst % 26))), count: 9) + " "266            #expect(try await app.save(noteId: id, title: "Frappe", document: NoteDocument(markdown: text), fromEditor: true, knownTitle: "Frappe"))267            try await Task.sleep(for: .milliseconds(20))268        }269        #expect(text.count == 200)270        await settle(app)271        try await Task.sleep(for: .milliseconds(500))272273        let raw = feed.stats.raw - start.stats.raw274        let lists = feed.lists - start.lists, structure = feed.structure - start.structure275        let page = feed.page(id) - start.page, tags = feed.tags - start.tags276        // 1.0: the global revision was bumped by every store change, and each visible screen keyed on it ran its queries again.277        // Screens alive while typing on iPad: sidebar (4 queries: counts, folders, tags, sections), note list (1), editor reload (1).278        let before = raw * (4 + 1 + 1)279        let after = (structure + tags) * 4 + lists * 1 + page * 1280        print("MEASURE typing-200: store changes=\(raw) | 1.0 query re-runs=\(before) widget reloads=\(raw) | 2.0 query re-runs=\(after) (lists=\(lists) structure=\(structure) tags=\(tags) editor=\(page)) widget reloads=\(reloads - start.widgets)")281        #expect(raw >= 20)282        #expect(lists == 1, "the lists catch up once after the pause")283        #expect(structure == 0)284        #expect(page == 0, "the editor does not reload its own autosaves")285        #expect(reloads - start.widgets == 1)286        #expect(after * 20 <= before)287        let saved = try #require(try await app.store.page(id: id))288        #expect(saved.plainText.count >= 199)289    }290291    @Test func anExternalChangeStillRefreshesEverythingAtOnce() async throws {292        let feed = ChangeFeed(listDelay: .seconds(30))293        feed.expectOwnSave(of: "a", titleChanged: false)294        feed.receive(.pages(["a"]))295        #expect(feed.lists == 0)296        #expect(feed.page("a") == 0)297        feed.receive(.pages(["a"]))                 // not announced: sync, another window, an extension298        #expect(feed.lists == 1)299        #expect(feed.structure == 1)300        #expect(feed.page("a") == 1)301        #expect(feed.page("b") == 0)302        feed.receive(.tags)303        #expect(feed.tags == 1)304        #expect(feed.lists == 1)305        feed.receive(.entities(kind: "task", ids: ["t"]))306        #expect(feed.entities("task") == 1)307        #expect(feed.entities("edge") == 0)308        #expect(feed.lists == 1)309        feed.receive(.queue)310        #expect(feed.lists == 1)311        feed.receive(.reset)312        #expect(feed.lists == 2)313        #expect(feed.page("a") == 2)314        #expect(feed.entities("task") == 2)315        #expect(feed.resetGeneration == 1)316    }317318    @Test func aRenameFromTheEditorRefreshesTheTreesAfterThePauseAndFlushDeliversNow() async throws {319        let feed = ChangeFeed(listDelay: .seconds(30))320        feed.expectOwnSave(of: "a", titleChanged: true)321        feed.receive(.pages(["a"]))322        #expect(feed.structure == 0)323        feed.flush()324        #expect(feed.lists == 1)325        #expect(feed.structure == 1)326        feed.flush()327        #expect(feed.lists == 1)328        // a save that changed nothing leaves no expectation behind329        feed.expectOwnSave(of: "b", titleChanged: false)330        feed.cancelOwnSave(of: "b")331        feed.receive(.pages(["b"]))332        #expect(feed.page("b") == 1)333    }334335    @Test func widgetReloadsAreDebouncedAndLimitedToWhatWidgetsShow() async throws {336        var reloads = 0337        let refresher = WidgetRefresher(delay: .milliseconds(80)) { reloads += 1 }338        for _ in 0..<25 { refresher.noteChange(.pages(["a"])) }339        refresher.noteChange(.queue)340        refresher.noteChange(.tags)341        refresher.noteChange(.entities(kind: "edge", ids: ["e"]))342        #expect(reloads == 0)343        try await Task.sleep(for: .milliseconds(300))344        #expect(reloads == 1)345        #expect(refresher.requests == 25)346        refresher.noteChange(.entities(kind: "task", ids: ["t"]))347        refresher.flush()                            // the app goes to the background348        #expect(reloads == 2)349        refresher.flush()350        #expect(reloads == 2)351        try await Task.sleep(for: .milliseconds(200))352        #expect(reloads == 2)353    }354}355356@Suite("Jetons de design") struct DesignTokenTests {357    private func hex(_ color: UIColor, _ style: UIUserInterfaceStyle) -> String {358        let resolved = color.resolvedColor(with: UITraitCollection(userInterfaceStyle: style))359        var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0360        resolved.getRed(&r, green: &g, blue: &b, alpha: &a)361        return String(format: "%02X%02X%02X", Int((r * 255).rounded()), Int((g * 255).rounded()), Int((b * 255).rounded()))362    }363364    @Test func theAssetCatalogHoldsTheExactTokensOfTheSpecification() throws {365        let pairs: [(String, NVColor.Token)] = [366            ("Background", .bg), ("Surface", .bgElevated), ("Surface2", .bgSunken), ("Border", .line), ("TextPrimary", .fg),367            ("TextSecondary", .fgMuted), ("Muted", .fgMuted), ("AccentColor", .accent), ("Danger", .dangerText), ("Success", .successText),368            ("Warning", .warningText), ("CodeBackground", .bgSunken), ("QuoteBar", .lineStrong), ("WidgetBackground", .bgElevated), ("LaunchBackground", .bg),369        ]370        for (name, token) in pairs {371            let color = try #require(UIColor(named: name), "missing colour set \(name)")372            #expect(hex(color, .light) == String(format: "%06X", token.lightHex), "\(name) light")373            #expect(hex(color, .dark) == String(format: "%06X", token.darkHex), "\(name) dark")374        }375    }376377    @Test func theAccentIsUltramarineNotAmber() throws {378        let accent = try #require(UIColor(named: "AccentColor"))379        #expect(hex(accent, .light) == "2F5DDB")380        #expect(hex(accent, .dark) == "7CA5FF")381    }382}383384@Suite("État de synchronisation 2.0") struct SyncStatusSurfaceTests {385    @Test func theNewFieldsAreSaidToTheUser() {386        var status = SyncStatus()387        #expect(SyncPresentation(status: status, isOnline: true, session: .signedIn).attention.isEmpty)388        status.rejected = 2389        var presentation = SyncPresentation(status: status, isOnline: true, session: .signedIn)390        #expect(presentation.kind == .conflict)391        #expect(presentation.label == "À vérifier")392        #expect(presentation.explanation.contains("refusées"))393        status.rejected = 0394        status.unsupported = 1395        presentation = SyncPresentation(status: status, isOnline: true, session: .signedIn)396        #expect(presentation.kind == .upToDate, "parked actions of a newer build are kept, not an error")397        #expect(presentation.attention.count == 1)398        status.canvasConflicts = 1399        status.conflicts = 1400        presentation = SyncPresentation(status: status, isOnline: true, session: .signedIn)401        #expect(presentation.label == "1 conflit")402        #expect(presentation.attention.count == 2)403    }404}405
Files main
Menu

Theme