SPB Git

Repository Public

NextIntegrationTests.swift 48.8 KB 580 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/SPMailCore/Tests/SPMailCoreTests/NextIntegrationTests.swift3// Description: Integration tests of the Next client against a REAL SP Mail Next backend (dev instance + real mail server, staging domain).4// Nothing is mocked: labels through JMAP keywords and `label:` search, a snooze that comes back, the undo-send hold, harvested contacts,5// the dashboard, rules, reminders, attachments and the typed refusals.6//7// Enabled by the same SPMAIL_TEST_* variables as IntegrationTests.swift (Scripts/dev-bootstrap.mjs). Mail only ever travels between the two8// apple-*@stg.spboucher.ai test mailboxes. Every object a test creates is removed by that test.9import Foundation10import Testing11@testable import SPMailCore1213private struct Live {14    let client: APIClient15    let next: NextAPI16    init() {17        precondition(Env.addrA.hasPrefix("apple-") && Env.addrA.hasSuffix("@stg.spboucher.ai") && Env.addrB.hasPrefix("apple-") && Env.addrB.hasSuffix("@stg.spboucher.ai"), "test mailboxes must be apple-*@stg.spboucher.ai")18        client = APIClient(baseURL: Env.base, token: Env.token, userAgent: "SPMailCoreTests-Next")19        next = NextAPI(client)20    }21    /// A word that appears in no other message of the run.22    static func token(_ name: String) -> String { "nx\(Env.run)\(name)\(UUID().uuidString.prefix(4).lowercased())" }2324    /// Sends A → B through `/api/v1/messages/send` and waits until B's Inbox holds it.25    @discardableResult26    func deliver(_ word: String, text: String = "Bonjour, ceci est un essai automatique.", attachments: [SendAttachment] = [], options: SendOptions = SendOptions()) async throws -> SearchResult {27        let out = try await next.send(SendRequest(from: Env.addrA, to: [EmailAddress(name: "Apple Essai B", email: Env.addrB)], subject: "Essai \(word)", text: text, attachments: attachments), options: options, idempotencyKey: UUID().uuidString)28        #expect(["sent", "queued", "scheduled", "delivered"].contains(out.outbound.status), "send status \(out.outbound.status) \(out.outbound.lastError ?? "")")29        return try await arrived(word)30    }31    func find(_ word: String, in mailbox: String = Env.mbB, extra: String = "in:inbox") async throws -> SearchResult? {32        try await next.search(SearchQuery("subject:\(word) \(extra)", scope: .only([mailbox]))).results.first33    }34    func arrived(_ word: String, timeout: Double = 60) async throws -> SearchResult {35        try await until("message \(word) in B's Inbox", timeout: timeout) { try await find(word) }36    }37    /// Sets or clears a keyword directly through the JMAP gateway, as the sync engine does.38    func setKeyword(_ keyword: String, _ on: Bool, mailbox: String, emailId: String) async throws {39        let jmap = JMAPClient(api: client, mailboxId: mailbox)40        let account = try await jmap.accountId()41        let r = try await jmap.call([JMAPCall("Email/set", ["accountId": .string(account), "update": .object([emailId: .object(["keywords/\(keyword)": on ? true : .null])])], "s")])42        let res = try r.result("s")43        #expect(res["notUpdated"][emailId].isNull, "Email/set refused: \(res["notUpdated"])")44    }45    func keywords(mailbox: String, emailId: String) async throws -> (keywords: [String], folders: [String]) {46        let jmap = JMAPClient(api: client, mailboxId: mailbox)47        let account = try await jmap.accountId()48        let r = try await jmap.call([JMAPCall("Email/get", ["accountId": .string(account), "ids": [.string(emailId)], "properties": ["keywords", "mailboxIds"]], "g"),49                                     JMAPCall("Mailbox/get", ["accountId": .string(account), "properties": ["name", "role"]], "m")])50        let e = try r.result("g")["list"][0]51        let boxes = try r.result("m")["list"].arrayValue ?? []52        let ids = Set((e["mailboxIds"].objectValue ?? [:]).keys)53        return (NextJSON.flags(e["keywords"]), boxes.filter { ids.contains($0["id"].stringValue ?? "") }.map { $0["role"].stringValue ?? $0["name"].stringValue ?? "" }.sorted())54    }55}5657@Suite(.serialized, .enabled(if: Env.enabled, "needs a Next dev backend: node Scripts/dev-bootstrap.mjs up && source Build/dev-env.sh"))58struct NextIntegrationTests {59    @Test func capabilitiesAndEveryReadRouteDecodeAgainstTheRealServer() async throws {60        let l = Live()61        let caps = try await l.next.capabilities()62        #expect(!caps.version.isEmpty && NextFeature.allCases.allSatisfy(caps.has), "features: \(caps.features.sorted())")63        // each call below must decode whatever the real server answers on an account that has used nothing yet64        _ = try await l.next.pushDevices()65        let prefs = try await l.next.pushPrefs()66        #expect(prefs.preview != .unknown && prefs.badge != .unknown)67        let status = try await l.next.pushStatus()68        #expect(status.environmentHosts["production"] != nil && (status.configured || status.lastError != nil))69        _ = try await l.next.labels(); _ = try await l.next.savedSearches(); _ = try await l.next.snippets()70        let signatures = try await l.next.signatures()71        // the alias created by the bootstrap has a 0.3.0 per-address signature: exposed read-only72        #expect(signatures.contains { $0.isReadOnly && $0.addressId != nil && $0.id.hasPrefix("address:") }, "signatures: \(signatures.map(\.id))")73        _ = try await l.next.contacts(ContactQuery(limit: 5)); _ = try await l.next.contactGroups(); _ = try await l.next.contactDuplicates(); _ = try await l.next.suggestContacts("", limit: 3)74        let compose = try await l.next.composePrefs()75        #expect(ComposePrefs.undoRange.contains(compose.undoSeconds))76        _ = try await l.next.snoozes(); _ = try await l.next.snoozes(status: nil); _ = try await l.next.reminders(); _ = try await l.next.reminders(status: .pending)77        _ = try await l.next.rules()78        let ai = try await l.next.aiSettings()79        #expect(ai.provider != .unknown && ai.language != .unknown)80        _ = try await l.next.aiUsage(days: 7)81        let index = try await l.next.semanticIndexStatus()82        #expect(index.total != nil && index.mailboxes.count >= 1)83        let insights = try await l.next.inboxInsights(mailboxId: Env.mbB, limit: 20)84        #expect(insights.mailboxId == Env.mbB && !insights.ai && insights.generatedAt != nil)85        _ = try await l.next.attachments(AttachmentQuery(limit: 5))86        let parsed = try await l.next.parseQuery("from:\(Env.addrA) -is:unread has:attachment before:2026-12-01 larger:5m \"mot exact\" OR is:starred is:bleu")87        #expect(parsed.terms.map(\.op) == ["from", "is", "has", "before", "larger", "text", "is"] && parsed.terms[1].negated && parsed.terms[3].date != nil && parsed.terms[4].bytes == 5 * 1024 * 1024 && parsed.terms[5].phrase)88        #expect(parsed.terms[5].group == parsed.terms[6].group && parsed.errors.count == 1 && parsed.errors[0].code == "invalid_value" && parsed.errors[0].token == "is:bleu")89        // the chips of the server's parse rebuild a query the server parses to the same terms90        let rebuilt = try await l.next.parseQuery(QueryBuilder(parsed.chips).query)91        #expect(rebuilt.isValid && rebuilt.terms.map { "\($0.negated)\($0.op):\($0.value)|\($0.group)" } == parsed.terms.map { "\($0.negated)\($0.op):\($0.value)|\($0.group)" }, "\(QueryBuilder(parsed.chips).query)")92        // typed refusals from the real server93        await #expect(throws: NextError.notFound) { try await l.next.deleteLabel(id: UUID().uuidString.lowercased()) }94        await #expect(throws: NextError.notFound) { try await l.next.inboxInsights(mailboxId: UUID().uuidString.lowercased()) }95        await #expect(throws: NextError.notFound) { _ = try await l.next.heldMessage(id: "msg_out_AAAAAAAAAAAAAAAA") }96        let anonymous = NextAPI(APIClient(baseURL: Env.base, token: "not-a-token", userAgent: "SPMailCoreTests-Next"))97        do { _ = try await anonymous.labels(); Issue.record("an invalid token was accepted") } catch { #expect(NextError.isUnauthorized(error), "\(error)") }98        do { _ = try await NextAPI(APIClient(baseURL: Env.dead, token: Env.token)).labels(); Issue.record("a dead port answered") } catch { #expect(NextError.isOffline(error), "\(error)") }99    }100101    @Test func dashboardDecodesAndCountsTheTestMailboxes() async throws {102        let l = Live()103        let d = try await l.next.dashboard(fresh: true)104        #expect(Set(d.inbox.map(\.mailboxId)).isSuperset(of: [Env.mbA, Env.mbB]) && d.inbox.first { $0.mailboxId == Env.mbB }?.address == Env.addrB, "inbox: \(d.inbox)")105        #expect(d.sync.mailServer == "reachable" && ["configured", "disabled"].contains(d.sync.push) && d.generatedAt != nil && abs(d.generatedAt!.timeIntervalSinceNow) < 120)106        #expect(Set(d.storage.map(\.mailboxId)).isSuperset(of: [Env.mbA, Env.mbB]) && d.totalUnread == d.inbox.reduce(0) { $0 + $1.unread })107        let cached = try await l.next.dashboard()108        #expect(cached.inbox.count == d.inbox.count)109    }110111    @Test func labelRoundTripThroughJMAPKeywordAndLabelSearch() async throws {112        let l = Live()113        let word = Live.token("lbl"), name = "Essai \(word)"114        let label = try await l.next.createLabel(name: name, color: LabelPalette.light[5])115        #expect(label.name == name && label.color == "#1d5fa8" && MailLabel.isLabelKeyword(label.keyword), "keyword \(label.keyword)")116        do {117            #expect(try await l.next.labels().contains(label))118            // a second label with the same name is refused with the server's code119            do { _ = try await l.next.createLabel(name: name.uppercased()); Issue.record("duplicate label accepted") } catch let e as APIError { #expect(e.status == 409 && e.code == "name_taken") }120            let mail = try await l.deliver(word)121            #expect(mail.labelKeywords.isEmpty)122            // the keyword travels through standard JMAP, exactly as read / flag state does123            try await l.setKeyword(label.keyword, true, mailbox: Env.mbB, emailId: mail.emailId)124            let found = try await until("label: search finds the message") {125                try await l.next.search(SearchQuery("label:\"\(name)\"", scope: .only([Env.mbB]))).results.first { $0.emailId == mail.emailId }126            }127            #expect(found.labelKeywords == [label.keyword] && MailLabel.labels(for: found.keywords, in: [label]) == [label])128            // across all mailboxes too, and not under another label's name129            let everywhere = try await l.next.search(SearchQuery("label:\"\(name)\" subject:\(word)"))130            #expect(everywhere.results.map(\.emailId) == [mail.emailId] && everywhere.results.first?.mailboxId == Env.mbB && everywhere.parsed.isValid)131            let negated = try await l.next.search(SearchQuery("-label:\"\(name)\" subject:\(word)", scope: .only([Env.mbB])))132            #expect(negated.results.isEmpty)133            let unknown = try await l.next.search(SearchQuery("label:inexistant\(word)", scope: .only([Env.mbB])))134            #expect(unknown.results.isEmpty && !unknown.parsed.isValid, "an unknown label must be an error of the parse, not a silent empty result")135            // rename + recolour: the keyword is immutable136            let renamed = try await l.next.updateLabel(id: label.id, name: name + " bis", color: LabelPalette.light[0])137            #expect(renamed.keyword == label.keyword && renamed.name == name + " bis" && renamed.color == "#b3261e")138            let again = try await l.next.search(SearchQuery("label:\"\(name) bis\"", scope: .only([Env.mbB])))139            #expect(again.results.map(\.emailId) == [mail.emailId])140            // clearing the keyword through JMAP removes the message from the label141            try await l.setKeyword(label.keyword, false, mailbox: Env.mbB, emailId: mail.emailId)142            _ = try await until("label: search no longer finds the message") { try await l.next.search(SearchQuery("label:\"\(name) bis\"", scope: .only([Env.mbB]))).results.isEmpty ? true : nil }143        } catch { try? await l.next.deleteLabel(id: label.id); throw error }144        try await l.next.deleteLabel(id: label.id)145        #expect(try await !l.next.labels().contains { $0.id == label.id })146        await #expect(throws: NextError.notFound) { try await l.next.deleteLabel(id: label.id) }147    }148149    @Test func snoozeARealMessageAndSeeItComeBack() async throws {150        let l = Live()151        let word = Live.token("snz")152        let mail = try await l.deliver(word)153        try await l.setKeyword(Keyword.seen, true, mailbox: Env.mbB, emailId: mail.emailId)154        let until = Date().addingTimeInterval(8)155        let made = try await l.next.snooze(mailboxId: Env.mbB, emailIds: [mail.emailId], until: until)156        #expect(made.count == 1 && made[0].emailId == mail.emailId && made[0].mailboxId == Env.mbB && made[0].status == .active && made[0].subject == "Essai \(word)" && made[0].from?.email.lowercased() == Env.addrA.lowercased())157        #expect(abs((made[0].until ?? .distantPast).timeIntervalSince(until)) < 1.5)158        // while snoozed: `$snoozed`, out of the Inbox, in the folder named Snoozed159        let asleep = try await l.keywords(mailbox: Env.mbB, emailId: mail.emailId)160        #expect(asleep.keywords.contains(NextKeyword.snoozed) && asleep.folders == [NextKeyword.snoozedFolderName], "\(asleep)")161        #expect(try await l.find(word) == nil)162        #expect(try await l.find(word, extra: "is:snoozed")?.emailId == mail.emailId)163        #expect(try await l.next.snoozes().contains { $0.id == made[0].id })164        // the server wakes it by itself: no client action between here and the Inbox165        let back = try await SPMailCoreTests.until("the snoozed message is back in the Inbox", timeout: 60) { try await l.find(word) }166        #expect(back.emailId == mail.emailId && !back.isSnoozed && back.isUnread && back.folderRoles == ["inbox"], "\(back.keywords) \(back.folderRoles)")167        #expect(Date() >= until.addingTimeInterval(-1))168        let done = try await l.next.snoozes(status: .done), stillActive = try await l.next.snoozes()169        #expect(done.contains { $0.id == made[0].id } && !stillActive.contains { $0.id == made[0].id })170171        // waking by hand: back at once, read state untouched, and the snooze can no longer be moved172        try await l.setKeyword(Keyword.seen, true, mailbox: Env.mbB, emailId: mail.emailId)173        let far = try await l.next.snooze(mailboxId: Env.mbB, emailIds: [mail.emailId], until: Date().addingTimeInterval(86_400))174        let moved = try await l.next.updateSnooze(id: far[0].id, until: Date().addingTimeInterval(2 * 86_400))175        #expect(moved.id == far[0].id && (moved.until ?? .distantPast) > Date().addingTimeInterval(86_400 + 3600))176        #expect(try await l.next.cancelSnooze(id: far[0].id) == .cancelled)177        let awake = try await l.keywords(mailbox: Env.mbB, emailId: mail.emailId)178        #expect(!awake.keywords.contains(NextKeyword.snoozed) && awake.keywords.contains(Keyword.seen) && awake.folders == ["inbox"], "\(awake)")179        do { _ = try await l.next.updateSnooze(id: far[0].id, until: Date().addingTimeInterval(3600)); Issue.record("a cancelled snooze was moved") } catch let e as APIError { #expect(e.status == 409 && e.code == "not_active") }180        // a date in the past is refused181        do { _ = try await l.next.snooze(mailboxId: Env.mbB, emailIds: [mail.emailId], until: Date().addingTimeInterval(-60)); Issue.record("a snooze in the past was accepted") } catch let e as APIError { #expect(e.status == 400) }182    }183184    @Test func reminderFiresAndSetsTheFollowUpKeyword() async throws {185        let l = Live()186        let word = Live.token("rem")187        let mail = try await l.deliver(word)188        let r = try await l.next.createReminder(mailboxId: Env.mbB, emailId: mail.emailId, dueAt: Date().addingTimeInterval(5), note: "relancer \(word)")189        #expect(r.status == .pending && r.emailId == mail.emailId && r.note == "relancer \(word)" && !r.onlyIfNoReply && r.isOpen)190        #expect(try await l.next.reminders(status: .pending).contains { $0.id == r.id })191        let fired = try await until("the reminder is due", timeout: 60) { try await l.next.reminders(status: .due).first { $0.id == r.id } }192        #expect(fired.firedAt != nil && fired.status == .due)193        #expect(try await l.keywords(mailbox: Env.mbB, emailId: mail.emailId).keywords.contains(NextKeyword.followUp))194        #expect(try await l.find(word, extra: "is:followup")?.needsFollowUp == true)195        let closed = try await l.next.updateReminder(id: r.id, done: true)196        #expect(closed.status == .done && !closed.isOpen)197        do { _ = try await l.next.updateReminder(id: r.id, note: "x"); Issue.record("a closed reminder was edited") } catch let e as APIError { #expect(e.status == 409 && e.code == "not_active") }198        // a pending one is cancelled199        let later = try await l.next.createReminder(mailboxId: Env.mbB, emailId: mail.emailId, dueAt: Date().addingTimeInterval(86_400), onlyIfNoReply: true)200        #expect(later.onlyIfNoReply && later.note == nil)201        #expect(try await l.next.cancelReminder(id: later.id) == .cancelled)202    }203204    @Test func undoSendHoldsTheMessageOnTheServerAndCancelTakesItBack() async throws {205        let l = Live()206        let word = Live.token("undo")207        let request = SendRequest(from: Env.addrA, to: [EmailAddress(name: "Apple Essai B", email: Env.addrB)], subject: "Essai \(word)", text: "Retenu puis annulé.", html: "<p>Retenu puis <b>annulé</b>.</p>")208        let before = Date()209        let held = try await l.next.send(request, options: SendOptions(undoSeconds: 45), idempotencyKey: UUID().uuidString)210        #expect(held.outbound.isScheduled && held.canUndo && held.id.hasPrefix("msg_out_"), "status \(held.outbound.status)")211        let window = (held.undoUntil ?? .distantPast).timeIntervalSince(before)212        #expect(window > 40 && window < 50, "undo window \(window)")213        // the content comes back so the composer can be reopened214        let content = try await l.next.heldMessage(id: held.id)215        #expect(content.id == held.id && content.from.lowercased() == Env.addrA.lowercased() && content.to.map { $0.email.lowercased() } == [Env.addrB.lowercased()] && content.subject == "Essai \(word)")216        #expect(content.text.contains("Retenu puis") && content.html?.contains("<b>annulé</b>") == true && content.mailboxId == Env.mbA && content.emailId != nil && content.remaining() > 20)217        #expect(try await l.client.scheduledSends().contains { $0.id == held.id })218        // undo = the keep-draft route: the send is cancelled, the draft (same email id) stays in Drafts219        let undone = try await l.next.undoSend(id: held.id)220        #expect(undone.subject == "Essai \(word)" && undone.emailId == content.emailId)221        #expect(try await l.client.outbound(id: held.id).status == "cancelled")222        // once cancelled the content is gone and a second undo is refused with the server's code223        do { _ = try await l.next.undoSend(id: held.id); Issue.record("a cancelled send was undone twice") } catch let e as APIError { #expect(e.status == 409 && e.code == "not_cancellable") }224225        // a short hold that is NOT undone leaves by itself — and only after the window226        let word2 = Live.token("hold")227        let sent = try await l.next.send(SendRequest(from: Env.addrA, to: [EmailAddress(email: Env.addrB)], subject: "Essai \(word2)", text: "Retenu puis parti."), options: SendOptions(undoSeconds: 6), idempotencyKey: UUID().uuidString)228        #expect(sent.canUndo)229        try await Task.sleep(for: .seconds(3))230        #expect(try await l.find(word2) == nil, "the message left before the end of its hold")231        let arrived = try await l.arrived(word2)232        #expect(Date() >= (sent.undoUntil ?? .distantFuture).addingTimeInterval(-1) && arrived.subject == "Essai \(word2)")233        // nothing of the cancelled message ever reached B234        #expect(try await l.find(word) == nil)235        #expect(try await l.find(word, extra: "in:all") == nil)236    }237238    @Test func sensitiveRecipientsRequireConfirmation() async throws {239        let l = Live()240        let original = try await l.next.composePrefs()241        var prefs = original242        prefs.sensitive = [Env.addrB]243        prefs.undoSeconds = 15244        let saved = try await l.next.setComposePrefs(prefs)245        #expect(saved.sensitive == [Env.addrB.lowercased()] && saved.undoSeconds == 15)246        do {247            #expect(try await l.next.composePrefs() == saved)248            let word = Live.token("sens")249            let request = SendRequest(from: Env.addrA, to: [EmailAddress(email: Env.addrB)], subject: "Essai \(word)", text: "Confirmé.")250            #expect(saved.sensitiveRecipients(among: request.recipients) == [Env.addrB])251            await #expect(throws: NextError.confirmationRequired(recipients: [Env.addrB.lowercased()])) { _ = try await l.next.send(request, idempotencyKey: UUID().uuidString) }252            // nothing was queued by the refusal253            #expect(try await !l.client.scheduledSends().contains { $0.subject == "Essai \(word)" })254            let ok = try await l.next.send(request, options: SendOptions(confirmSensitive: true), idempotencyKey: UUID().uuidString)255            #expect(["sent", "queued", "delivered"].contains(ok.outbound.status) && !ok.canUndo)256            _ = try await l.arrived(word)257        } catch { _ = try? await l.next.setComposePrefs(original); throw error }258        #expect(try await l.next.setComposePrefs(original) == original)259    }260261    /// The server never harvests the user's own addresses, and the two test mailboxes belong to the same user: real mail between them cannot262    /// produce a suggestion. What is checked for real is that rule, then autocomplete over a saved contact.263    @Test func contactsSuggestSavedGroupExportImportAndMerge() async throws {264        let l = Live()265        _ = try await l.deliver(Live.token("ctc"))266        let pair = [Env.addrA.lowercased(), Env.addrB.lowercased()]267        let needle = String(Env.addrB.split(separator: "@")[0].split(separator: "-").last ?? "apple")268        // the delivery above was processed by the watcher (the message is in B's Inbox): own addresses are not suggested269        let own = try await l.next.suggestContacts(needle, limit: 50).filter { pair.contains($0.email.lowercased()) }270        #expect(own.isEmpty, "the user's own addresses were harvested: \(own)")271272        // a contact of our own, on an address that receives nothing273        let tag = Live.token("c")274        let email = "\(tag)@stg.spboucher.ai"275        let group = try await l.next.createContactGroup(name: "Groupe \(tag)", color: LabelPalette.light[8])276        var created: [String] = []277        do {278            let c = try await l.next.createContact(ContactContent(name: "Élodie \(tag)", emails: [ContactEmail(email: email.uppercased(), label: "travail")], organization: "UQO", phones: [ContactPhone(number: "+1 819 555 0100", label: "mobile")],279                                                                 notes: "note", favorite: true, vip: true, groups: [group.id]))280            created.append(c.id)281            #expect(c.kind == .saved && c.name == "Élodie \(tag)" && c.emails == [ContactEmail(email: email, label: "travail")] && c.organization == "UQO" && c.phones.first?.label == "mobile" && c.favorite && c.vip && c.groups == [group.id] && c.avatarColor != nil)282            // autocomplete: by address fragment and by name, with the contact id and the favourite flag283            let byAddress = try await l.next.suggestContacts(tag, limit: 8)284            #expect(byAddress.map(\.email) == [email] && byAddress[0].contactId == c.id && byAddress[0].favorite && byAddress[0].kind == .saved && byAddress[0].name == c.name, "\(byAddress)")285            #expect(try await l.next.suggestContacts("élodie \(tag)".uppercased(), limit: 8).map(\.email) == [email])286            #expect(try await l.next.suggestContacts("zz\(tag)zz").isEmpty)287            let history = try await l.next.contactHistory(id: c.id, limit: 5)288            #expect(history.messages.isEmpty && history.reminders.isEmpty)289            let page = try await l.next.contacts(ContactQuery(q: tag, group: group.id, favoritesOnly: true, kind: .saved, limit: 10))290            #expect(page.contacts.map(\.id) == [c.id] && page.nextCursor == nil)291            #expect(try await l.next.contactGroups().first { $0.id == group.id }?.memberCount == 1)292            let patched = try await l.next.updateContact(id: c.id, ContactContent(organization: "", favorite: false))293            #expect(patched.organization == nil && !patched.favorite && patched.vip && patched.name == c.name && patched.emails == c.emails)294            do { _ = try await l.next.createContact(ContactContent(name: "Doublon", emails: [ContactEmail(email: email)])); Issue.record("an address already used was accepted") } catch let e as APIError { #expect(e.status == 409 && e.code == "email_in_use") }295296            // vCard: export what we have, import a card for the same person under another address, then merge the duplicates297            let vcf = String(decoding: try await l.next.exportContacts(kind: .saved, group: group.id), as: UTF8.self)298            #expect(vcf.contains("BEGIN:VCARD") && vcf.contains(email) && vcf.contains(tag))299            let other = "\(tag)2@stg.spboucher.ai"300            let card = "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Élodie \(tag)\r\nN:\(tag);Élodie;;;\r\nEMAIL;TYPE=HOME:\(other)\r\nORG:UQO\r\nEND:VCARD\r\n"301            let imported = try await l.next.importContacts(vCard: Data(card.utf8))302            #expect(imported.total == 1 && imported.imported + imported.updated == 1, "\(imported)")303            let twin = try await until("the imported contact is listed") { try await l.next.contacts(ContactQuery(q: other, limit: 5)).contacts.first }304            if twin.id != c.id {305                created.append(twin.id)306                let dups = try await l.next.contactDuplicates()307                let mine = try #require(dups.first { Set($0.contacts.map(\.id)) == [c.id, twin.id] }, "duplicates: \(dups.map { $0.contacts.map(\.name) })")308                #expect(mine.reason == "name")309                let kept = try await l.next.mergeContacts(keep: c.id, merge: [twin.id])310                #expect(kept.id == c.id && Set(kept.emails.map(\.email)) == [email, other])311                await #expect(throws: NextError.notFound) { _ = try await l.next.contact(id: twin.id) }312                created.removeAll { $0 == twin.id }313            } else {314                #expect(Set(twin.emails.map(\.email)) == [email, other])315            }316            do { _ = try await l.next.importContacts(vCard: Data("pas une vCard".utf8)); Issue.record("garbage was imported") } catch let e as APIError { #expect(e.status == 400) }317            let renamedGroup = try await l.next.updateContactGroup(id: group.id, name: "Groupe \(tag) bis")318            #expect(renamedGroup.name == "Groupe \(tag) bis" && renamedGroup.color == "#a8327a")319        } catch {320            for id in created { try? await l.next.deleteContact(id: id) }321            try? await l.next.deleteContactGroup(id: group.id)322            throw error323        }324        for id in created { try await l.next.deleteContact(id: id) }325        try await l.next.deleteContactGroup(id: group.id)326        await #expect(throws: NextError.notFound) { _ = try await l.next.contact(id: created.first ?? UUID().uuidString.lowercased()) }327    }328329    @Test func savedSearchesSnippetsAndSignaturesRoundTrip() async throws {330        let l = Live()331        let tag = Live.token("org")332        let search = try await l.next.createSavedSearch(SavedSearchContent(name: "Recherche \(tag)", query: "is:unread has:attachment", scope: .only([Env.mbB]), icon: "tray", color: "#7A5C00", pinned: true, notify: true))333        #expect(search.query == "is:unread has:attachment" && search.scope == .only([Env.mbB]) && search.icon == "tray" && search.color == "#7a5c00" && search.pinned && search.notify)334        let s2 = try await l.next.updateSavedSearch(id: search.id, SavedSearchContent(query: "is:starred", scope: .all, icon: "", color: "", pinned: false))335        #expect(s2.query == "is:starred" && s2.scope == .all && s2.icon == nil && s2.color == nil && !s2.pinned && s2.notify && s2.name == search.name)336        do { _ = try await l.next.updateSavedSearch(id: search.id, SavedSearchContent(query: "is:bleu")); Issue.record("an invalid query was saved") } catch let e as APIError { #expect(e.status == 400 && e.code == "invalid_query") }337        #expect(try await l.next.savedSearches().contains { $0.id == search.id && $0.query == "is:starred" })338        try await l.next.deleteSavedSearch(id: search.id)339340        let shortcut = String(tag.prefix(20))341        let snippet = try await l.next.createSnippet(SnippetContent(name: "Bloc \(tag)", shortcut: shortcut, subject: "Objet", html: "<p onclick=\"x()\">Merci <b>beaucoup</b><script>alert(1)</script></p>", text: "Merci beaucoup"))342        #expect(snippet.shortcut == shortcut && snippet.subject == "Objet" && snippet.text == "Merci beaucoup" && snippet.html.contains("<b>beaucoup</b>") && !snippet.html.contains("script") && !snippet.html.contains("onclick"), "sanitised html: \(snippet.html)")343        do { _ = try await l.next.createSnippet(SnippetContent(name: "Autre", shortcut: shortcut, text: "x")); Issue.record("a duplicate shortcut was accepted") } catch let e as APIError { #expect(e.status == 409 && e.code == "shortcut_taken") }344        let sn2 = try await l.next.updateSnippet(id: snippet.id, SnippetContent(shortcut: "", subject: "", text: "Merci"))345        #expect(sn2.shortcut == nil && sn2.subject == nil && sn2.text == "Merci" && sn2.name == snippet.name)346        #expect(Snippet.match(typed: "Bonjour ;\(shortcut)", in: [snippet])?.snippet.id == snippet.id)347        try await l.next.deleteSnippet(id: snippet.id)348349        let addresses = try await l.client.addresses().filter { $0.mailboxId == Env.mbA && $0.isSendable }350        let address = try #require(addresses.first { $0.address.lowercased() == Env.addrA.lowercased() })351        let sig = try await l.next.createSignature(SignatureContent(name: "Signature \(tag)", html: "<p>SP</p>", text: "SP", defaultFor: [address.id]))352        #expect(!sig.isReadOnly && sig.source == "user" && sig.defaultFor == [address.id] && sig.defaultForReplies.isEmpty && sig.text == "SP")353        do {354            let all = try await l.next.signatures()355            #expect(Signature.defaultSignature(forAddress: address.id, isReply: false, in: all)?.id == sig.id)356            let sig2 = try await l.next.updateSignature(id: sig.id, SignatureContent(text: "SP Boucher", defaultFor: [], defaultForReplies: [address.id]))357            #expect(sig2.text == "SP Boucher" && sig2.defaultFor.isEmpty && sig2.defaultForReplies == [address.id])358            // the read-only per-address entry cannot be edited or deleted here359            if let legacy = all.first(where: \.isReadOnly) {360                do { _ = try await l.next.updateSignature(id: legacy.id, SignatureContent(name: "x")); Issue.record("a per-address signature was edited") } catch let e as APIError { #expect(e.status == 409 && e.code == "read_only") }361                do { try await l.next.deleteSignature(id: legacy.id); Issue.record("a per-address signature was deleted") } catch let e as APIError { #expect(e.status == 409 && e.code == "read_only") }362            }363            do { _ = try await l.next.createSignature(SignatureContent(name: "x", text: "y", defaultFor: [UUID().uuidString.lowercased()])); Issue.record("an unknown address id was accepted") } catch let e as APIError { #expect(e.status == 400 && e.code == "unknown_address") }364        } catch { try? await l.next.deleteSignature(id: sig.id); throw error }365        try await l.next.deleteSignature(id: sig.id)366    }367368    @Test func attachmentsAreUploadedListedAndZippedForReal() async throws {369        let l = Live()370        let word = Live.token("att")371        let pdf = Data("%PDF-1.4\n% essai \(word)\n".utf8) + Data((0..<4000).map { UInt8(truncatingIfNeeded: $0 &* 7) })372        let png = Data([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]) + Data((0..<900).map { UInt8(truncatingIfNeeded: $0 &* 13) })373        let f1 = try await l.next.uploadFile(pdf, name: "devis \(word).pdf", type: "application/pdf", address: Env.addrA)374        let f2 = try await l.next.uploadFile(png, name: "photo-\(word).png", type: "image/png", address: Env.addrA)375        #expect(f1.hasPrefix("file_") && f2.hasPrefix("file_") && f1 != f2)376        let mail = try await l.deliver(word, attachments: [SendAttachment(fileId: f1), SendAttachment(fileId: f2)])377        #expect(mail.hasAttachment)378        let items = try await until("the attachments are listed", timeout: 30) { () -> [AttachmentItem]? in379            let found = try await l.next.attachments(AttachmentQuery(scope: .only([Env.mbB]), q: word, limit: 20)).items.filter { $0.emailId == mail.emailId }380            return found.count == 2 ? found : nil381        }382        let doc = try #require(items.first { $0.kind == .pdf }), image = try #require(items.first { $0.kind == .image })383        #expect(doc.name == "devis \(word).pdf" && doc.type == "application/pdf" && doc.size == pdf.count && doc.mailboxId == Env.mbB && doc.threadId == mail.threadId && doc.subject == "Essai \(word)" && doc.from?.email.lowercased() == Env.addrA.lowercased())384        #expect(image.name == "photo-\(word).png" && image.size == png.count && image.inlineOK && doc.downloadURL != nil)385        #expect(try await l.next.attachments(AttachmentQuery(scope: .only([Env.mbB]), kind: .pdf, q: word)).items.map(\.blobId) == [doc.blobId])386        #expect(try await l.next.attachments(AttachmentQuery(scope: .only([Env.mbB]), minSize: 2000, q: word)).items.map(\.blobId) == [doc.blobId])387388        let dir = FileManager.default.temporaryDirectory.appendingPathComponent("spmail-next-it-\(UUID().uuidString)")389        defer { try? FileManager.default.removeItem(at: dir) }390        // one attachment, byte for byte391        let single = dir.appendingPathComponent("un.pdf")392        #expect(try await l.next.downloadAttachment(doc, to: single) == Int64(pdf.count))393        let singleBytes = try Data(contentsOf: single)394        #expect(singleBytes == pdf)395        // both, zipped by the server and streamed to a file; the archive is read back with the system unzip396        let zip = dir.appendingPathComponent("pieces.zip")397        let n = try await l.next.downloadAttachmentsZip(items.map(AttachmentZipItem.init), to: zip)398        let zipBytes = try Data(contentsOf: zip)399        #expect(n == Int64(zipBytes.count) && zipBytes.prefix(4) == Data([0x50, 0x4b, 0x03, 0x04]) && n > 200)400        let out = dir.appendingPathComponent("out")401        let unzip = Process()402        unzip.executableURL = URL(fileURLWithPath: "/usr/bin/unzip")403        unzip.arguments = ["-q", "-o", zip.path, "-d", out.path]404        unzip.standardOutput = FileHandle.nullDevice; unzip.standardError = FileHandle.nullDevice405        try unzip.run(); unzip.waitUntilExit()406        #expect(unzip.terminationStatus == 0)407        let extractedPDF = try Data(contentsOf: out.appendingPathComponent("devis \(word).pdf")), extractedPNG = try Data(contentsOf: out.appendingPathComponent("photo-\(word).png"))408        #expect(extractedPDF == pdf && extractedPNG == png)409        // a blob that is not an attachment of that message is refused; nothing is written410        let bogus = dir.appendingPathComponent("non.zip")411        await #expect(throws: NextError.notFound) { _ = try await l.next.downloadAttachmentsZip([AttachmentZipItem(mailboxId: Env.mbB, emailId: mail.emailId, blobId: "Gnotablob", name: "x")], to: bogus) }412        #expect(!FileManager.default.fileExists(atPath: bogus.path))413    }414415    @Test func rulesRunOnRealDeliveryAndDryRunChangesNothing() async throws {416        let l = Live()417        let word = Live.token("rule")418        let label = try await l.next.createLabel(name: "Règle \(word)")419        var ruleId: String?420        do {421            let draft = MailRule(name: "Essai \(word)", scope: .only([Env.mbB]), match: .all, conditions: [.text(field: .subject, op: .contains, value: word), .text(field: .from, op: .matchesDomain, value: "stg.spboucher.ai")],422                                 actions: [.label(labelId: label.id), .star, .pin], stop: false)423            let rule = try await l.next.createRule(draft)424            ruleId = rule.id425            #expect(rule.name == draft.name && rule.enabled && rule.scope == .only([Env.mbB]) && rule.match == .all && rule.conditions == draft.conditions && rule.actions == draft.actions && !rule.hasUnknownParts)426            #expect(try await l.next.rules().contains { $0.id == rule.id })427            // an invalid rule is refused (an action is required)428            do { _ = try await l.next.createRule(MailRule(name: "vide")); Issue.record("a rule without action was accepted") } catch let e as APIError { #expect(e.status == 400) }429430            let mail = try await l.deliver(word)431            let run = try await until("the rule ran on the delivered message", timeout: 60) { try await l.next.ruleRuns(id: rule.id, limit: 10).first { $0.emailId == mail.emailId && $0.status != .running } }432            #expect(run.status == .ok && run.mailboxId == Env.mbB && run.actions.count == 3, "run: \(run.status) \(run.error ?? "") \(run.actions)")433            let state = try await l.keywords(mailbox: Env.mbB, emailId: mail.emailId)434            #expect(state.keywords.contains(label.keyword) && state.keywords.contains(Keyword.flagged) && state.keywords.contains(NextKeyword.pinned), "\(state.keywords)")435            #expect(try await l.find(word, extra: "is:pinned is:starred label:\"\(label.name)\"")?.emailId == mail.emailId)436437            // dry run of another rule on the same message: says what it would do, does nothing438            let probe = MailRule(name: "Sonde", conditions: [.text(field: .subject, op: .contains, value: word), .hasAttachment(true)], actions: [.markRead])439            let no = try await l.next.testRule(probe, mailboxId: Env.mbB, emailId: mail.emailId)440            #expect(!no.matched && no.actions.isEmpty && no.conditions.map(\.result) == [true, false] && no.conditions.map(\.condition) == probe.conditions)441            var any = probe442            any.match = .any443            let yes = try await l.next.testRule(any, mailboxId: Env.mbB, emailId: mail.emailId)444            #expect(yes.matched && yes.actions.map(\.type) == ["mark_read"] && !yes.actions[0].would.isEmpty && yes.facts["has_attachment"] == false)445            #expect(try await !l.keywords(mailbox: Env.mbB, emailId: mail.emailId).keywords.contains(Keyword.seen))446447            let off = try await l.next.setRule(id: rule.id, enabled: false, position: 7)448            #expect(!off.enabled && off.position == 7 && off.actions == rule.actions)449            var edited = off450            edited.actions = [.markRead]451            edited.stop = true452            let saved = try await l.next.updateRule(edited)453            #expect(saved.actions == [.markRead] && saved.stop && !saved.enabled && saved.conditions == rule.conditions)454        } catch {455            if let ruleId { try? await l.next.deleteRule(id: ruleId) }456            try? await l.next.deleteLabel(id: label.id)457            throw error458        }459        if let ruleId {460            try await l.next.deleteRule(id: ruleId)461            await #expect(throws: NextError.notFound) { _ = try await l.next.ruleRuns(id: ruleId) }462        }463        try await l.next.deleteLabel(id: label.id)464    }465466    @Test func intelligenceIsOffByDefaultAndSaysSoWithTypedErrors() async throws {467        let l = Live()468        let settings = try await l.next.aiSettings()469        try #require(settings.provider == .off, "this test expects the test user's AI provider to be off (it is the default)")470        let existing = try await l.next.search(SearchQuery("in:inbox", scope: .only([Env.mbB]), limit: 1)).results.first471        let mail: SearchResult472        if let existing { mail = existing } else { mail = try await l.deliver(Live.token("ai")) }473        await #expect(throws: NextError.aiDisabled) { _ = try await l.next.summarize(mailboxId: Env.mbB, threadId: mail.threadId) }474        await #expect(throws: NextError.aiDisabled) { _ = try await l.next.suggestReplies(mailboxId: Env.mbB, emailId: mail.emailId) }475        await #expect(throws: NextError.aiDisabled) { _ = try await l.next.write(AIWriteRequest(action: .improve, text: "Bonjour")) }476        await #expect(throws: NextError.aiDisabled) { _ = try await l.next.triage(mailboxId: Env.mbB, emailIds: [mail.emailId]) }477        await #expect(throws: NextError.aiDisabled) { _ = try await l.next.semanticSearch("devis") }478        await #expect(throws: NextError.aiDisabled) { _ = try await l.next.rebuildSemanticIndex() }479        // the external provider cannot be selected without consent in the same request; nothing changed480        if settings.anthropicAvailable {481            await #expect(throws: NextError.consentRequired) { _ = try await l.next.setAISettings(AISettingsUpdate(provider: .anthropic)) }482        }483        // a feature switch alone is stored without turning anything on484        let toggled = try await l.next.setAISettings(AISettingsUpdate(feature: .summary, enabled: true))485        #expect(toggled.provider == .off && toggled.features["summary"] == true && !toggled.isEnabled(.summary))486        let restored = try await l.next.setAISettings(AISettingsUpdate(features: settings.features.merging(["summary": settings.features["summary"] ?? false]) { a, _ in a }))487        #expect(restored.provider == .off && restored.features["summary"] == (settings.features["summary"] ?? false))488        await #expect(throws: NextError.aiDisabled) { _ = try await l.next.summarize(mailboxId: Env.mbB, threadId: mail.threadId) }489    }490491    @Test func pushDeviceLifecycleWithAFakeToken() async throws {492        let l = Live()493        // 32 random bytes: a syntactically valid APNs token that belongs to no device494        let token = Data((0..<32).map { _ in UInt8.random(in: 0...255) })495        let id = try await l.next.registerPush(PushRegistration(apnsToken: token, sandbox: true, bundleId: "ai.spboucher.spmail.dev.tests", deviceName: "SPMailCoreTests", locale: "fr_CA", appVersion: "2.0.0 (0)"))496        do {497            // registering the same token again updates the same row498            #expect(try await l.next.registerPush(PushRegistration(apnsToken: token, sandbox: true, bundleId: "ai.spboucher.spmail.dev.tests", appVersion: "2.0.0 (1)")) == id)499            let device = try #require(try await l.next.pushDevices().first { $0.id == id })500            #expect(device.current && device.enabled && device.environment == .sandbox && device.deviceName == "SPMailCoreTests" && device.locale == "fr_CA" && device.appVersion == "2.0.0 (1)" && device.lastRegisteredAt != nil)501            try await l.next.updatePushDevice(id: id, PushDevicePatch(enabled: false, mailboxes: [Env.mbB: false, UUID().uuidString.lowercased(): false], sound: false))502            let muted = try #require(try await l.next.pushDevices().first { $0.id == id })503            // a mailbox that is not the user's is not kept504            #expect(!muted.enabled && muted.mailboxes == [Env.mbB: false] && muted.sound == false)505            // preferences: a full write, then back506            let original = try await l.next.pushPrefs()507            let edited = PushPrefs(enabled: true, preview: .full, quietHours: QuietHours(start: "22:00", end: "07:00", timeZone: "America/Toronto"), prioritySenders: [Env.addrA.uppercased(), "@uqo.ca"], priorityOnly: false, mailboxes: [Env.mbA: false], badge: PushBadge.none)508            let saved = try await l.next.setPushPrefs(edited)509            #expect(saved.preview == .full && saved.quietHours == edited.quietHours && Set(saved.prioritySenders) == [Env.addrA.lowercased(), "@uqo.ca"] && saved.mailboxes == [Env.mbA: false] && saved.badge == PushBadge.none)510            #expect(try await l.next.pushPrefs() == saved)511            do { _ = try await l.next.setPushPrefs(PushPrefs(quietHours: QuietHours(start: "22:00", end: "07:00", timeZone: "Mars/Olympus"))); Issue.record("an unknown time zone was accepted") } catch let e as APIError { #expect(e.status == 400) }512            #expect(try await l.next.setPushPrefs(original) == original)513            // a disabled device gets no test notification; the call still answers honestly514            #expect(try await l.next.testPush(deviceId: id) == 0)515            await #expect(throws: NextError.notFound) { _ = try await l.next.testPush(deviceId: UUID().uuidString.lowercased()) }516        } catch { try? await l.next.deletePushDevice(id: id); throw error }517        try await l.next.deletePushDevice(id: id)518        #expect(try await !l.next.pushDevices().contains { $0.id == id })519        await #expect(throws: NextError.notFound) { try await l.next.deletePushDevice(id: id) }520    }521522    @Test func administratorOperationsDecode() async throws {523        let l = Live()524        guard try await l.client.me().user.role == "admin" else { return }525        let health = try await l.next.adminHealth()526        #expect(health.checks.count >= 8 && health.check("mail_server")?.status == .ok && health.status != nil && health.generatedAt != nil && !health.checks.contains { $0.status == .unknown && $0.detail.isEmpty }, "\(health.checks.map { "\($0.name)=\($0.status.rawValue)" })")527        for name in ["mail_server", "queue", "disk", "certificates", "backups", "dns", "push", "workers", "webhooks"] { #expect(health.check(name) != nil, "missing check \(name)") }528        _ = try await l.next.adminAlerts(); _ = try await l.next.adminAlerts(openOnly: false)529        await #expect(throws: NextError.notFound) { _ = try await l.next.acknowledgeAlert(id: UUID().uuidString.lowercased()) }530        let devices = try await l.next.adminDevices()531        #expect(devices.sessions.contains { $0.current && $0.kind == "native" && $0.user != nil })532        for metric in TrendMetric.allCases {533            let t = try await l.next.adminTrends(metric, days: 7)534            #expect(t.metric == metric.rawValue && t.days == 7 && t.points.allSatisfy { $0.date != nil })535        }536        let events = try await l.next.adminSecurityEvents(limit: 5)537        #expect(events.events.count <= 5 && events.events.allSatisfy { !$0.kind.isEmpty && $0.at != nil })538        // revoking somebody's session needs a recent passkey re-authentication: a 403 that is not a lost session539        do { try await l.next.adminRevokeSession(id: UUID().uuidString.lowercased()); Issue.record("revocation without re-authentication") } catch { #expect((error as? APIError)?.code == "reauth_required" && !NextError.isUnauthorized(error), "\(error)") }540    }541542    @Test func storeSynchronisesOptimisticChangesWithTheRealServer() async throws {543        let l = Live()544        let dir = FileManager.default.temporaryDirectory.appendingPathComponent("spmail-next-it-\(UUID().uuidString)")545        defer { try? FileManager.default.removeItem(at: dir) }546        let secrets = InMemorySecretStore()547        let tag = Live.token("st")548        // offline first: a device with no network creates a label and a snippet549        let offline = try NextStore(directory: dir, secrets: secrets, api: NextAPI(APIClient(baseURL: Env.dead, token: Env.token)))550        let local = try #require(await offline.createLabel(name: "Hors ligne \(tag)", color: LabelPalette.light[3]))551        await offline.createSnippet(SnippetContent(name: "Bloc \(tag)", text: "Texte \(tag)"))552        await offline.updateLabel(id: local.id, name: "Hors ligne \(tag) bis")553        let stuck = await offline.flush()554        #expect(stuck.sent == 0 && stuck.remaining == 2 && stuck.blockedUntil != nil)555        // the app is relaunched with the network back: the queue is replayed against the real server556        let store = try NextStore(directory: dir, secrets: secrets, api: l.next)557        #expect(await store.pendingCount == 2)558        let report = try await store.refresh()559        #expect(report.failed.isEmpty && report.skipped.isEmpty && report.refreshed.count == NextStore.Collection.allCases.count, "\(report)")560        let label = try #require(await store.labels().first { $0.name == "Hors ligne \(tag) bis" })561        let snippet = try #require(await store.snippets().first { $0.name == "Bloc \(tag)" })562        #expect(!NextStore.isLocal(label.id) && MailLabel.isLabelKeyword(label.keyword) && label.color == "#2e6b2e" && !NextStore.isLocal(snippet.id) && snippet.text == "Texte \(tag)")563        #expect(await store.read { s in s.resolvedId(local.id) == label.id && s.pendingCount == 0 && s.dashboard() != nil && s.pushPrefs() != nil && s.composePrefs() != nil && s.aiSettings() != nil })564        #expect(try await l.next.labels().contains(label))565        // a refusal from the real server: the optimistic row disappears and the server's list is back566        let ghost = try #require(await store.createLabel(name: label.name.uppercased()))567        #expect(await store.labels().contains { $0.id == ghost.id })568        let refused = await store.flush()569        #expect(refused.dropped.map(\.code) == ["name_taken"] && refused.remaining == 0)570        #expect(await store.read { s in !s.labels().contains { $0.id == ghost.id } && s.labels().contains { $0.id == label.id } })571        // deletions go through the queue too572        await store.deleteLabel(id: local.id)573        await store.deleteSnippet(id: snippet.id)574        let cleaned = await store.flush()575        #expect(cleaned.sent == 2 && cleaned.dropped.isEmpty && cleaned.remaining == 0)576        #expect(try await !l.next.labels().contains { $0.id == label.id })577        #expect(try await !l.next.snippets().contains { $0.id == snippet.id })578    }579}580
Files main
Menu

Theme