SPB Git

Repository Public

PinOrderTests.swift 4.1 KB 55 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/SPMailCore/Tests/SPMailCoreTests/PinOrderTests.swift3// Description: Pinned conversations across a whole folder — the engine's `hasKeyword` query against the in-memory JMAP double (a pinned4// message far below the loaded window is found, cached and remembered; offline answers from the cache; a local unpin is reflected at once)5// and the ordering rule that merges it into the loaded window.6import Foundation7import Testing8@testable import SPMailCore910@Suite struct PinOrderTests {11    @Test func aPinnedOldMessageIsFoundBelowTheLoadedWindow() async throws {12        let rig = try Rig { $0.initialWindow = 5; $0.pageSize = 5 }13        let old = rig.server.seed("Vieux contrat", keywords: [NextKeyword.pinned]) // m1, the oldest14        for n in 2...40 { _ = rig.server.seed("M\(n)") }15        let recent = rig.server.seed("Récent épinglé", keywords: [NextKeyword.pinned])16        _ = rig.server.seed("Ailleurs", box: "arch", keywords: [NextKeyword.pinned]) // pinned in another folder: not this list's business17        try await rig.engine.fullSync()18        let window = try await rig.store.threads("A", scope: .mailbox("in"), threaded: true, limit: 8)19        #expect(!window.contains { $0.email.id == old } && window.first?.email.id == recent)2021        let found = try await rig.engine.emails(inMailbox: "in", withKeyword: NextKeyword.pinned)22        #expect(found.offline == false && found.results.map(\.id) == [recent, old])23        #expect(try await rig.store.email("A", id: old)?.subject == "Vieux contrat") // now in the cache24        let merged = PinOrder.merged(window, pinned: found.results, threaded: true)25        #expect(merged.prefix(2).map(\.email.id) == [recent, old] && merged.count == window.count + 1 && Set(merged.map(\.email.id)).count == merged.count)26        #expect(Array(merged.dropFirst(2)).map(\.email.id) == window.filter { $0.email.id != recent }.map(\.email.id))2728        // offline: the remembered answer, read from the cache29        rig.offline()30        let cached = try await rig.engine.emails(inMailbox: "in", withKeyword: NextKeyword.pinned)31        #expect(cached.offline && cached.results.map(\.id) == [recent, old])32        // unpinned on this device (optimistic, still offline): gone from the list at once33        await rig.engine.setKeyword(NextKeyword.pinned, false, ids: [old])34        #expect(try await rig.engine.cachedEmails(inMailbox: "in", withKeyword: NextKeyword.pinned).map(\.id) == [recent])35        rig.online()36    }3738    @Test func orderingRule() {39        func mail(_ id: String, _ t: Double, thread: String? = nil, pinned: Bool = false, seen: Bool = true) -> EmailSummary {40            EmailSummary(id: id, threadId: thread ?? "t\(id)", mailboxIds: ["in"], keywords: (pinned ? [NextKeyword.pinned] : []) + (seen ? [Keyword.seen] : []), receivedAt: Date(timeIntervalSince1970: t))41        }42        let rows = [ThreadSummary(email: mail("c", 30)), ThreadSummary(email: mail("b", 20, pinned: true)), ThreadSummary(email: mail("a", 10))]43        // nothing pinned anywhere: the window is returned untouched44        #expect(PinOrder.merged([rows[0], rows[2]], pinned: [], threaded: true).map(\.id) == ["c", "a"])45        // pinned rows of the window and pinned messages outside it, newest first, then the rest in its order46        let out = PinOrder.merged(rows, pinned: [mail("b", 20, pinned: true), mail("z", 1, pinned: true, seen: false), mail("y", 25, pinned: true)], threaded: true)47        #expect(out.map(\.id) == ["y", "b", "z", "c", "a"] && out[2].unreadCount == 1 && out[2].count == 1)48        // a stale entry (no longer pinned) is ignored; a pinned message of a conversation already listed does not add a second row49        #expect(PinOrder.merged(rows, pinned: [mail("s", 5)], threaded: true).map(\.id) == ["b", "c", "a"])50        #expect(PinOrder.merged(rows, pinned: [mail("c0", 2, thread: "tc", pinned: true)], threaded: true).map(\.id) == ["b", "c", "a"])51        // message mode: the same message is its own row52        #expect(PinOrder.merged(rows, pinned: [mail("c0", 2, thread: "tc", pinned: true)], threaded: false).map(\.id) == ["b", "c0", "c", "a"])53    }54}55
Files main
Menu

Theme