Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/SPMailCore/Tests/SPMailCoreTests/IntegrationTests.swift3// Description: Integration tests of SPMailCore against a REAL SP Mail backend (dev instance + real mail server, staging domain). Nothing is mocked.4//5// Enabled only when Scripts/dev-bootstrap.mjs has exported: SPMAIL_TEST_BASE_URL, SPMAIL_TEST_TOKEN, SPMAIL_TEST_TOKEN_VICTIM,6// SPMAIL_TEST_MAILBOX_A/B, SPMAIL_TEST_ADDRESS_A/B. Mail only ever travels between the two apple-*@stg.spboucher.ai test mailboxes.7import Foundation8import Testing9@testable import SPMailCore1011struct Env {12 static let e = ProcessInfo.processInfo.environment13 static var enabled: Bool { e["SPMAIL_TEST_BASE_URL"] != nil && e["SPMAIL_TEST_TOKEN"] != nil && e["SPMAIL_TEST_MAILBOX_A"] != nil && e["SPMAIL_TEST_MAILBOX_B"] != nil }14 static var base: URL { URL(string: e["SPMAIL_TEST_BASE_URL"]!)! }15 static var token: String { e["SPMAIL_TEST_TOKEN"]! }16 static var victim: String? { e["SPMAIL_TEST_TOKEN_VICTIM"] }17 static var mbA: String { e["SPMAIL_TEST_MAILBOX_A"]! }18 static var mbB: String { e["SPMAIL_TEST_MAILBOX_B"]! }19 static var addrA: String { e["SPMAIL_TEST_ADDRESS_A"]! }20 static var addrB: String { e["SPMAIL_TEST_ADDRESS_B"]! }21 static let run = String(UUID().uuidString.prefix(6)).lowercased()22 /// A port nothing listens on: every request fails at once, as when the device is offline.23 static let dead = URL(string: "http://127.0.0.1:9")!24}2526struct Timeout: Error, CustomStringConvertible { let description: String }27func until<T: Sendable>(_ what: String, timeout: Double = 40, _ body: @Sendable () async throws -> T?) async throws -> T {28 let end = Date().addingTimeInterval(timeout)29 while Date() < end {30 if let v = try await body() { return v }31 try await Task.sleep(for: .milliseconds(400))32 }33 throw Timeout(description: "timed out waiting for: \(what)")34}3536/// A device: its own API client, its own encrypted store, one engine per mailbox.37struct Device {38 let api: APIClient39 let store: MailStore40 let dir: URL41 let a: SyncEngine42 let b: SyncEngine43 init(token: String = Env.token) throws {44 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")45 api = APIClient(baseURL: Env.base, token: token, userAgent: "SPMailCoreTests")46 let (s, _, d) = try tempStore()47 store = s; dir = d48 a = SyncEngine(account: Env.mbA, address: Env.addrA, api: api, store: s)49 b = SyncEngine(account: Env.mbB, address: Env.addrB, api: api, store: s)50 }51 func message(_ subject: String, text: String, html: String? = nil, to: String = Env.addrB, from: String = Env.addrA) -> OutgoingMessage {52 OutgoingMessage(from: EmailAddress(name: "Apple Essai", email: from), to: [EmailAddress(name: "Destinataire", email: to)], subject: subject, text: text, html: html)53 }54 /// Sends A → B and waits until B's cache holds it.55 func deliver(_ subject: String, text: String, html: String? = nil) async throws -> EmailSummary {56 let sent = await a.send(OutboxItem(account: Env.mbA, message: message(subject, text: text, html: html)))57 #expect(sent.state == .accepted, "send state \(sent.state) \(sent.lastError ?? "")")58 return try await received(subject)59 }60 func received(_ subject: String, in engine: SyncEngine? = nil) async throws -> EmailSummary {61 let engine = engine ?? b62 return try await until("\(subject) received") {63 try await engine.deltaSync()64 guard let inbox = try await engine.mailboxId(role: .inbox) else { return nil }65 return try await store.threads(engine.account, scope: .mailbox(inbox), threaded: false, limit: 500).first { $0.email.subject == subject }?.email66 }67 }68}6970@Suite(.serialized, .enabled(if: Env.enabled, "needs the dev backend: node Scripts/dev-bootstrap.mjs up && source Build/dev-env.sh"))71struct IntegrationTests {72 @Test func sessionIdentityAndMailboxList() async throws {73 let d = try Device()74 let me = try await d.api.me()75 #expect(me.session.kind == "native" && me.mailboxes.map(\.id).contains(Env.mbA) && me.mailboxes.first { $0.id == Env.mbB }?.address == Env.addrB)76 let s = try await d.a.jmap.session()77 #expect(s.username == Env.addrA && !s.mailAccountId.isEmpty && s.maxCallsInRequest <= 16 && !s.capabilities.contains("urn:stalwart:jmap"))78 try await d.a.fullSync()79 let roles = Set(try await d.store.mailboxes(Env.mbA).compactMap(\.role))80 #expect(roles.isSuperset(of: ["inbox", "drafts", "sent", "trash", "junk"]))81 #expect(try await d.store.state(Env.mbA, "Email") != nil)82 let ids = try await d.a.identities()83 #expect(ids.first?.email == Env.addrA)84 #expect(try await d.api.health()["ok"].boolValue == true)85 // a second pass is a delta, not a full sync86 #expect(try await d.a.deltaSync().wasFull == false)87 // a state the server does not know triggers a clean full resynchronisation88 try await d.store.setState(Env.mbA, "Email", "zzz-bogus")89 #expect(try await d.a.deltaSync().wasFull)90 }9192 @Test func sendWithAttachmentAndUnicodeThenReceiveByDeltaSync() async throws {93 let d = try Device()94 try await d.a.fullSync(); try await d.b.fullSync()95 let subject = "Essai \(Env.run) — été, naïve, 日本語, 📎"96 var payload = Data(count: 60_000)97 for i in payload.indices { payload[i] = UInt8((i * 31 + 7) % 251) }98 var msg = d.message(subject, text: "Bonjour — ça marche ? € \(Env.run)", html: "<p>Bonjour — <b>ça marche</b> ? € \(Env.run)</p><script>alert(1)</script>")99 let att = OutgoingAttachment(name: "pièce jointe \(Env.run).pdf", type: "application/pdf", size: payload.count)100 msg.attachments = [att]101 let item = await d.a.send(OutboxItem(account: Env.mbA, message: msg), attachments: [att.id: payload])102 #expect(item.state == .accepted && item.submissionId != nil && item.serverEmailId != nil, "state \(item.state) \(item.lastError ?? "")")103 #expect(try await d.store.attachmentData(att.id) == nil) // local copy dropped once the server holds the message104105 let got = try await d.received(subject)106 #expect(got.from.first?.email == Env.addrA && got.hasAttachment && !got.isSeen && got.preview.contains("ça marche"))107 let body = try await d.b.body(for: got.id)108 #expect(body.text?.contains("ça marche ? € \(Env.run)") == true && body.html?.contains("<b>ça marche</b>") == true)109 let part = try #require(body.attachments.first)110 #expect(part.name == att.name && part.type == "application/pdf")111 #expect(try await d.b.download(part) == payload)112 let eml = String(decoding: try await d.b.exportEML(got), as: UTF8.self)113 #expect(eml.contains("Subject:") && eml.lowercased().contains("message-id:"))114115 // sender side: in Sent, not in Drafts, and the delivery status is read from the server — never assumed116 try await d.a.deltaSync()117 let sentBox = try #require(try await d.a.mailboxId(role: .sent)), draftBox = try #require(try await d.a.mailboxId(role: .drafts))118 #expect(try await d.store.threads(Env.mbA, scope: .mailbox(sentBox), threaded: false).contains { $0.email.subject == subject })119 #expect(try await d.store.threads(Env.mbA, scope: .mailbox(draftBox), threaded: false).contains { $0.email.subject == subject } == false)120 let delivered = try await until("delivery status final") { () -> OutboxItem? in121 await d.a.refreshDelivery()122 let i = try await d.store.outboxItem(item.id)123 return i?.deliveryIsFinal == true ? i : nil124 }125 #expect(delivered.delivery[Env.addrB]?.delivered == .yes && delivered.isFullyDelivered)126 }127128 @Test func liveUpdatesArriveThroughEventSource() async throws {129 let d = try Device()130 try await d.b.fullSync()131 let subject = "Direct \(Env.run)-\(Int.random(in: 1000...9999))"132 let listener = Task { () -> Bool in133 for await e in d.b.events {134 if case .newMail(let list) = e, list.contains(where: { $0.subject == subject }) { return true }135 }136 return false137 }138 await d.b.startLive()139 try await Task.sleep(for: .seconds(2)) // let the stream connect140 let t0 = Date()141 let sent = await d.a.send(OutboxItem(account: Env.mbA, message: d.message(subject, text: "en direct")))142 #expect(sent.state == .accepted)143 let arrived = await raceValue(listener, seconds: 40)144 #expect(arrived == true)145 print("INFO live update received \(String(format: "%.1f", Date().timeIntervalSince(t0))) s after send (no polling)")146 await d.b.stopLive()147 listener.cancel()148 }149150 @Test func flagsMoveArchiveAndDelete() async throws {151 let d = try Device()152 try await d.a.fullSync(); try await d.b.fullSync()153 let e = try await d.deliver("Drapeaux \(Env.run)", text: "à classer")154 let inbox = try #require(try await d.b.mailboxId(role: .inbox))155 let unreadBefore = try await d.store.mailboxes(Env.mbB).first { $0.id == inbox }?.unreadEmails ?? 0156 await d.b.markSeen(true, ids: [e.id])157 await d.b.setFlagged(true, ids: [e.id])158 #expect(try await d.store.mailboxes(Env.mbB).first { $0.id == inbox }?.unreadEmails == unreadBefore - 1) // badge follows at once159 // a second device sees the server's truth160 let other = try Device()161 try await other.b.fullSync()162 var seen = try #require(try await other.store.email(Env.mbB, id: e.id))163 #expect(seen.isSeen && seen.isFlagged)164 #expect(try await other.store.threads(Env.mbB, scope: .flagged).contains { $0.email.id == e.id })165 // archive creates the folder when the account has none166 try await d.b.archive(ids: [e.id])167 let archive = try #require(try await d.b.mailboxId(role: .archive))168 try await other.b.deltaSync()169 seen = try #require(try await other.store.email(Env.mbB, id: e.id))170 #expect(seen.mailboxIds == [archive])171 // custom folder: create, move, rename, delete172 let folder = try await d.b.createMailbox(name: "Projets \(Env.run)")173 await d.b.move(ids: [e.id], to: folder)174 try await d.b.renameMailbox(id: folder, name: "Clients \(Env.run)")175 try await other.b.deltaSync()176 #expect(try await other.store.mailboxes(Env.mbB).first { $0.id == folder }?.name == "Clients \(Env.run)")177 #expect(try await other.store.email(Env.mbB, id: e.id)?.mailboxIds == [folder])178 // delete → Trash; delete again → gone179 try await d.b.delete(ids: [e.id])180 let trash = try #require(try await d.b.mailboxId(role: .trash))181 try await other.b.deltaSync()182 #expect(try await other.store.email(Env.mbB, id: e.id)?.mailboxIds == [trash])183 try await d.b.delete(ids: [e.id])184 try await other.b.deltaSync()185 #expect(try await other.store.email(Env.mbB, id: e.id) == nil)186 try await d.b.deleteMailbox(id: folder)187 try await other.b.deltaSync()188 #expect(try await other.store.mailboxes(Env.mbB).contains { $0.id == folder } == false)189 }190191 @Test func draftAutosaveReplacesTheServerCopy() async throws {192 let d = try Device()193 try await d.a.fullSync()194 let drafts = try #require(try await d.a.mailboxId(role: .drafts))195 func serverDrafts() async throws -> [EmailSummary] {196 try await d.a.deltaSync()197 return try await d.store.threads(Env.mbA, scope: .mailbox(drafts), threaded: false).map(\.email).filter { $0.subject.contains(Env.run) }198 }199 var item = try await d.a.saveDraft(OutboxItem(account: Env.mbA, message: d.message("Brouillon \(Env.run) v1", text: "première version")))200 item = try #require(try await d.a.syncDraft(id: item.id))201 let first = try #require(item.serverEmailId)202 #expect(!item.needsDraftSync)203 var list = try await serverDrafts()204 #expect(list.map(\.subject) == ["Brouillon \(Env.run) v1"] && list.first?.isDraft == true)205 item.message.subject = "Brouillon \(Env.run) v2"206 item.message.text = "deuxième version"207 item = try await d.a.saveDraft(item)208 item = try #require(try await d.a.syncDraft(id: item.id))209 #expect(item.serverEmailId != first)210 list = try await serverDrafts()211 #expect(list.map(\.subject) == ["Brouillon \(Env.run) v2"]) // one copy, the new one212 // another device can pick the draft up and continue it213 let other = try Device()214 try await other.a.fullSync()215 let adopted = try await other.a.adoptServerDraft(try #require(list.first))216 #expect(adopted.message.text.contains("deuxième version") && adopted.message.to.first?.email == Env.addrB && adopted.serverEmailId == item.serverEmailId)217 await d.a.discardDraft(id: item.id)218 #expect(try await serverDrafts().isEmpty)219 #expect(try await d.store.outboxItem(item.id) == nil)220 }221222 @Test func serverSearchThenOfflineSearchOverTheCache() async throws {223 let d = try Device()224 try await d.a.fullSync(); try await d.b.fullSync()225 let token = "zebre\(Env.run)"226 let e = try await d.deliver("Recherche \(Env.run)", text: "Le mot rare est \(token) dans le corps.")227 _ = try await d.b.body(for: e.id)228 let online = try await until("server index") { () -> [EmailSummary]? in229 let r = try await d.b.search(SearchFilter(text: token))230 return r.results.isEmpty ? nil : (r.offline ? nil : r.results)231 }232 #expect(online.map(\.id) == [e.id])233 var f = SearchFilter()234 f.subject = "Recherche \(Env.run)"; f.unreadOnly = true; f.inMailbox = try await d.b.mailboxId(role: .inbox)235 #expect(try await d.b.search(f).results.map(\.id) == [e.id])236 f.hasAttachment = true237 #expect(try await d.b.search(f).results.isEmpty)238 // offline: the encrypted cache answers (subject, sender, preview, bodies already opened)239 d.api.baseURL = Env.dead240 let offline = try await d.b.search(SearchFilter(text: token))241 #expect(offline.offline && offline.results.map(\.id) == [e.id])242 #expect(try await d.b.search(SearchFilter(text: "Recherche \(Env.run)")).results.map(\.id) == [e.id])243 #expect(await d.b.isOnline == false)244 // and the message is still readable offline245 #expect(try await d.b.body(for: e.id).text?.contains(token) == true)246 }247248 @Test func offlineOutboxAndFlagChangesAreReplayedOnReconnect() async throws {249 let d = try Device()250 try await d.a.fullSync(); try await d.b.fullSync()251 let first = try await d.deliver("Avant coupure \(Env.run)", text: "reçu avant")252 // --- the network goes away253 d.api.baseURL = Env.dead254 let subject = "Hors ligne \(Env.run)"255 var payload = Data(count: 5000); payload[0] = 42256 var msg = d.message(subject, text: "écrit sans réseau")257 let att = OutgoingAttachment(name: "note.txt", type: "text/plain", size: payload.count)258 msg.attachments = [att]259 let queued = await d.a.send(OutboxItem(account: Env.mbA, message: msg), attachments: [att.id: payload])260 #expect(queued.state == .queuedOffline && queued.submissionId == nil) // never "sent" before the server accepted it261 await d.b.markSeen(true, ids: [first.id])262 #expect(try await d.store.email(Env.mbB, id: first.id)?.isSeen == true)263 #expect(try await d.store.pending(Env.mbB).count == 1)264 await #expect(throws: APIError.self) { try await d.a.syncNow() }265 #expect(try await d.store.outboxItem(queued.id)?.state == .queuedOffline)266 // a second engine on the same store (app relaunched while still offline) still finds the queue267 let relaunched = SyncEngine(account: Env.mbA, address: Env.addrA, api: d.api, store: d.store)268 #expect(await relaunched.outbox().contains { $0.id == queued.id && $0.state == .queuedOffline })269 // --- the network comes back270 d.api.baseURL = Env.base271 try await d.a.syncNow()272 try await d.b.syncNow()273 let after = try #require(try await d.store.outboxItem(queued.id))274 #expect(after.state == .accepted && after.submissionId != nil, "state \(after.state) error \(after.lastError ?? "-") attempts \(after.attempts)")275 #expect(try await d.store.pending(Env.mbB).isEmpty)276 let got = try await d.received(subject)277 let part = try #require(try await d.b.body(for: got.id).attachments.first)278 #expect(try await d.b.download(part) == payload)279 let other = try Device()280 try await other.b.fullSync()281 #expect(try await other.store.email(Env.mbB, id: first.id)?.isSeen == true)282 // exactly one copy was sent283 #expect(try await d.store.threads(Env.mbB, scope: .mailbox(try #require(try await d.b.mailboxId(role: .inbox))), threaded: false, limit: 500).filter { $0.email.subject == subject }.count == 1)284 }285286 @Test func serverWinsWhenAMessageChangedElsewhere() async throws {287 let d = try Device(), other = try Device()288 try await d.a.fullSync(); try await d.b.fullSync(); try await other.b.fullSync()289 let e = try await d.deliver("Conflit \(Env.run)", text: "deux appareils")290 try await other.b.deltaSync()291 // device 1 goes offline and flags the message; meanwhile device 2 deletes it for good292 d.api.baseURL = Env.dead293 await d.b.setFlagged(true, ids: [e.id])294 await other.b.destroy(ids: [e.id])295 d.api.baseURL = Env.base296 try await d.b.syncNow() // the replayed flag is refused (message gone): the server's state wins, nothing is resurrected297 #expect(try await d.store.email(Env.mbB, id: e.id) == nil)298 #expect(try await d.store.pending(Env.mbB).isEmpty)299 }300301 @Test func cacheIsEncryptedAtRest() async throws {302 let d = try Device()303 try await d.a.fullSync(); try await d.b.fullSync()304 let secret = "CORPS-CONFIDENTIEL-\(Env.run)-\(Int.random(in: 100_000...999_999))"305 let e = try await d.deliver("Chiffrement \(Env.run)", text: "\(secret) ne doit pas être lisible sur le disque.", html: "<p>\(secret) en HTML</p>")306 let body = try await d.b.body(for: e.id)307 #expect(body.text?.contains(secret) == true && e.preview.contains(secret))308 // also a local draft with an attachment309 let att = OutgoingAttachment(name: "secret.txt", type: "text/plain", size: 10)310 var draft = OutboxItem(account: Env.mbA, message: d.message("Brouillon \(secret)", text: "texte \(secret)"))311 draft.message.attachments = [att]312 _ = try await d.a.saveDraft(draft, attachments: [att.id: Data("PJ-\(secret)".utf8)])313 try await d.store.checkpoint()314 #expect(!fileContains(d.dir, secret), "a message body, preview or draft is readable in the SQLite files")315 #expect(fileContains(d.dir, "Chiffrement \(Env.run)")) // subjects are indexed in clear by design (sorting, offline search)316 #expect(try await d.store.body(Env.mbB, id: e.id)?.text?.contains(secret) == true)317 await d.a.discardDraft(id: draft.id)318 }319320 @Test func accountEndpointsPrefsSessionsPushAndImageProxy() async throws {321 let d = try Device()322 try await d.api.putPrefs(["apple_test": .string(Env.run), "theme": "auto"])323 #expect(try await d.api.me().user.prefs?["apple_test"].stringValue == Env.run)324 let sessions = try await d.api.sessions()325 #expect(sessions.filter(\.current).count == 1 && sessions.allSatisfy { $0.kind == "native" || $0.kind == "web" })326 #expect(try await d.api.passkeys().count >= 1)327 // APNs registration path: the server stores the token (it has no sender yet)328 try await d.api.registerPush(apnsToken: Data((0..<32).map { UInt8($0) }), sandbox: true, bundleId: "ai.spboucher.spmail.dev")329 await #expect(throws: APIError.self) { try await d.api.registerPush(apnsToken: Data([1, 2]), sandbox: true, bundleId: "x") }330 // remote images only through the proxy, which refuses private destinations331 for u in ["http://127.0.0.1:18080/jmap/session", "http://169.254.169.254/latest/meta-data/", "file:///etc/passwd", "http://localhost/"] {332 do { _ = try await d.api.proxiedImage(u); Issue.record("\(u) was fetched") } catch let e as APIError { #expect([400, 403].contains(e.status), "\(u) → \(e.status)") }333 }334 // no token → 401, mapped335 let anonymous = APIClient(baseURL: Env.base)336 do { _ = try await anonymous.me(); Issue.record("anonymous /api/me succeeded") } catch let e as APIError { #expect(e.isUnauthorized) }337 // pre-login passkey endpoints answer a native client that sends no Origin338 let ch = try await anonymous.loginOptions()339 #expect(!ch.challengeId.isEmpty && ch.options["challenge"].stringValue != nil && ch.options["rpId"].stringValue != nil)340 do { _ = try await anonymous.enrollCheck(token: String(repeating: "x", count: 43)); Issue.record("bogus enroll token accepted") } catch let e as APIError { #expect(e.status == 403 && e.code == "invalid_token") }341 }342343 @Test func revokedSessionReturnsToSignInAndKeepsDrafts() async throws {344 let victimToken = try #require(Env.victim)345 let admin = try Device()346 let victim = try Device(token: victimToken)347 try await victim.a.fullSync()348 let draft = try await victim.a.saveDraft(OutboxItem(account: Env.mbA, message: victim.message("Brouillon à garder \(Env.run)", text: "jamais perdu")))349 let events = Task { () -> Bool in350 for await e in victim.a.events where e == .unauthorized { return true }351 return false352 }353 let sid = try await victim.api.me().session.id354 try await admin.api.revokeSession(id: sid) // "revoked from another device"355 do { try await victim.a.syncNow(); Issue.record("sync succeeded with a revoked session") } catch let e as APIError { #expect(e.isUnauthorized) }356 #expect(await raceValue(events) == true)357 #expect(await victim.a.isHalted)358 // sending now keeps the message queued instead of losing it359 let queued = await victim.a.send(OutboxItem(account: Env.mbA, message: victim.message("File après 401 \(Env.run)", text: "en attente")))360 #expect(queued.state == .queuedOffline)361 // what the app does on 401: drop the synced mail, keep the sealed drafts362 try await victim.store.wipeKeepingOutbox()363 #expect(try await victim.store.emailCount(Env.mbA) == 0)364 let kept = try await victim.store.outbox(account: Env.mbA)365 #expect(Set(kept.map(\.id)) == [draft.id, queued.id] && kept.first { $0.id == draft.id }?.message.text == "jamais perdu")366 #expect(try await admin.api.sessions().contains { $0.id == sid } == false)367 }368}369370/// Awaits a task's value with a 20 s cap (nil on timeout).371func raceValue<T: Sendable>(_ task: Task<T, Never>, seconds: Double = 20) async -> T? {372 await withTaskGroup(of: T?.self) { g in373 g.addTask { await task.value }374 g.addTask { try? await Task.sleep(for: .seconds(seconds)); return nil }375 let v = await g.next() ?? nil376 if v == nil { task.cancel() }377 g.cancelAll()378 return v379 }380}381