Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Mail9// File : Packages/SPMailCore/Tests/SPMailCoreTests/SyncHardeningIntegrationTests.swift10// Description : Integration tests of the SP Mail Next sync hardening11// against the REAL dev backend and mail server (same12// serialized suite as IntegrationTests): idempotent send13// after a lost response, two devices on one account, anchor14// paging, Thread/get, refused operations, full resync,15// quoting, prefetch and attachment cache, EventSource.16// Messages are placed with `Email/set` (no SMTP) except ONE17// real send between the two apple-*@stg.spboucher.ai18// mailboxes.19// Module : SPMailCoreTests20// Repository : https://git.spboucher.ai/sp-mail-apple21// Created : 2026-11-2022// Last Modified : 2026-11-2023//24// =============================================================================25// AUTHOR & PROFESSIONAL AFFILIATION26// =============================================================================27//28// Author : Simon-Pierre Boucher29// Position : Professor of Finance30// Institution : Université du Québec en Outaouais (UQO)31// Department : Département des sciences administratives32// Organization : SP Boucher33//34// Website : https://spboucher.ai35// Git : https://git.spboucher.ai36//37// =============================================================================38// OFFICIAL CONTACT DIRECTORY39// =============================================================================40//41// General : info@spboucher.ai42// Professional : contact@spboucher.ai43// Direct : simon@spboucher.ai44// Research : research@spboucher.ai45// Development : dev@spboucher.ai46// Support : support@spboucher.ai47// Security/Admin : admin@spboucher.ai48// Notifications : notifications@spboucher.ai49//50// Preferred Contact: support@spboucher.ai51//52// =============================================================================53// COPYRIGHT & INTELLECTUAL PROPERTY54// =============================================================================55//56// Copyright (c) 202657// Simon-Pierre Boucher58//59// All rights reserved where applicable.60//61// This file is governed by the applicable project license,62// copyright notices, institutional agreements and relevant law.63//64// Unless expressly authorized by the applicable license or65// rights holder, copying, redistribution, sublicensing,66// publication and commercial exploitation are not permitted.67//68// Existing third-party rights and notices remain unaffected.69//70// =============================================================================71// CONFIDENTIALITY & SECURITY72// =============================================================================73//74// Classification : Public75// Distribution : Public repository; use is governed by the project license76// Security Contact : admin@spboucher.ai77//78// Access to this source does not independently grant permission79// to redistribute or disclose confidential materials.80//81// Do not embed credentials, private keys, access tokens,82// certificates or confidential personal information.83//84// =============================================================================85// LICENSE & MAINTENANCE86// =============================================================================87//88// License : None specified (no license file in this repository)89// Maintainer : Simon-Pierre Boucher90// Technical Contact: dev@spboucher.ai91//92// =============================================================================93// END OF SP BOUCHER SOURCE HEADER94// =============================================================================9596import Foundation97import Testing98@testable import SPMailCore99100/// Forwards every request to the real backend and, when armed, drops the ANSWER of the first matching request after the server produced it —101/// the "accepted, but the response was lost" failure, on a real server.102final class LossyProxy: URLProtocol, @unchecked Sendable {103 private static let lock = NSLock()104 nonisolated(unsafe) private static var dropAnswerOf: (@Sendable (String) -> Bool)?105 nonisolated(unsafe) private static var dropped = 0106 static func arm(_ match: @escaping @Sendable (String) -> Bool) { lock.withLock { dropAnswerOf = match; dropped = 0 } }107 static var droppedCount: Int { lock.withLock { dropped } }108 private static let inner: URLSession = {109 let c = URLSessionConfiguration.ephemeral110 c.httpCookieAcceptPolicy = .never111 return URLSession(configuration: c)112 }()113 private var forwarded: URLSessionDataTask?114115 override class func canInit(with request: URLRequest) -> Bool { true }116 override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }117 override func startLoading() {118 var req = request119 if req.httpBody == nil, let s = request.httpBodyStream {120 s.open(); var d = Data(); var buf = [UInt8](repeating: 0, count: 65536)121 while s.hasBytesAvailable { let n = s.read(&buf, maxLength: buf.count); if n <= 0 { break }; d.append(buf, count: n) }122 s.close(); req.httpBody = d123 }124 let body = String(decoding: req.httpBody ?? Data(), as: UTF8.self)125 nonisolated(unsafe) let me = self126 forwarded = Self.inner.dataTask(with: req) { data, response, error in127 guard let client = me.client else { return }128 if let error { client.urlProtocol(me, didFailWithError: error); return }129 let drop = Self.lock.withLock { () -> Bool in130 guard let match = Self.dropAnswerOf, match(body) else { return false }131 Self.dropAnswerOf = nil; Self.dropped += 1132 return true133 }134 if drop { client.urlProtocol(me, didFailWithError: URLError(.networkConnectionLost)); return } // the server did the work; we never hear of it135 client.urlProtocol(me, didReceive: response!, cacheStoragePolicy: .notAllowed)136 client.urlProtocol(me, didLoad: data ?? Data())137 client.urlProtocolDidFinishLoading(me)138 }139 forwarded?.resume()140 }141 override func stopLoading() { forwarded?.cancel() }142}143144extension IntegrationTests {145 /// Marker of this file's messages — distinct from `Env.run`, whose tests filter folders on it.146 static let mark = "hx" + String(UUID().uuidString.suffix(6)).lowercased()147148 /// Places a message straight into a folder with `Email/set create` — no SMTP, no sender allowance used.149 func seed(_ e: SyncEngine, _ subject: String, box: String, at: Date? = nil, keywords: [String] = [], text: String = "corps", html: String? = nil, messageId: String? = nil,150 inReplyTo: String? = nil, attachments: [JSONValue] = []) async throws -> String {151 let acct = try await e.accountId()152 var mail: JSONValue = ["mailboxIds": [box: true], "keywords": .object(Dictionary(uniqueKeysWithValues: keywords.map { ($0, JSONValue.bool(true)) })),153 "from": [["name": "Apple Essai B", "email": .string(Env.addrB)]], "to": [["email": .string(Env.addrA)]], "subject": .string(subject),154 "textBody": [["partId": "t", "type": "text/plain"]], "bodyValues": ["t": ["value": .string(text)]]]155 if let at { mail["receivedAt"] = .string(ISODate.string(at)) }156 if let html { mail["htmlBody"] = [["partId": "h", "type": "text/html"]]; mail["bodyValues"]["h"] = ["value": .string(html)] }157 if let messageId { mail["messageId"] = [.string(messageId)] }158 if let inReplyTo { mail["inReplyTo"] = [.string(inReplyTo)]; mail["references"] = [.string(inReplyTo)] }159 if !attachments.isEmpty { mail["attachments"] = .array(attachments) }160 let res = try await e.call([JMAPCall("Email/set", ["accountId": .string(acct), "create": ["e": mail]], "c")]).result("c")161 return try #require(res["created"]["e"]["id"].stringValue, "seed refused: \(res["notCreated"])")162 }163 func serverEmail(_ e: SyncEngine, _ id: String) async throws -> JSONValue? {164 let acct = try await e.accountId()165 return try await e.call([JMAPCall("Email/get", ["accountId": .string(acct), "ids": [.string(id)], "properties": ["id", "mailboxIds", "keywords", "subject"]], "g")]).result("g")["list"].arrayValue?.first166 }167 func subjects(_ d: Device, _ engine: SyncEngine, role: MailboxRole) async throws -> [String] {168 try await engine.deltaSync()169 let box = try #require(try await engine.mailboxId(role: role))170 return try await d.store.threads(engine.account, scope: .mailbox(box), threaded: false, limit: 1000).map(\.email.subject)171 }172 func cleanup(_ e: SyncEngine, _ ids: [String]) async { if !ids.isEmpty { await e.destroy(ids: ids) } }173174 // Item 3 on the real mail server: the submission is accepted, its answer is lost, the message must leave exactly once.175 @Test func lostResponseAfterAcceptanceIsNotSentTwiceOnTheRealServer() async throws {176 let d = try Device()177 try await d.b.fullSync()178 let api = APIClient(baseURL: Env.base, token: Env.token, userAgent: "SPMailCoreTests", protocolClasses: [LossyProxy.self])179 let (store, _, _) = try tempStore()180 let engine = SyncEngine(account: Env.mbA, address: Env.addrA, api: api, store: store)181 try await engine.fullSync()182 let subject = "Une seule fois \(Self.mark)"183 LossyProxy.arm { $0.contains("EmailSubmission") && $0.contains("onSuccessUpdateEmail") } // JSON escapes the slash of the method name184 let queued = await engine.send(OutboxItem(account: Env.mbA, message: d.message(subject, text: "réponse perdue en route")))185 #expect(LossyProxy.droppedCount == 1)186 #expect(queued.state == .queuedOffline && queued.submissionId == nil && queued.submissionUncertain == true, "state \(queued.state) \(queued.lastError ?? "")")187 let mid = try #require(queued.message.messageId)188 // what the server really holds for that Message-ID — and which lookups this mail server supports189 let acct = try await engine.accountId()190 let drafts = try #require(try await engine.mailboxId(role: .drafts)), sent = try #require(try await engine.mailboxId(role: .sent))191 let header = try await engine.call([JMAPCall("Email/query", ["accountId": .string(acct), "filter": ["header": ["Message-ID", .string(mid)]]], "q")])192 let subs = try await engine.call([JMAPCall("EmailSubmission/query", ["accountId": .string(acct), "filter": ["emailIds": ["x"]]], "q")])193 print("INFO Email/query header filter on Message-ID: \((try? header.result("q")["ids"].stringArray.count).map { "answered with \($0) match(es) — 0 means the Sent/Drafts scan is what finds the message" } ?? "refused → Sent/Drafts scan") ; EmailSubmission/query: \(subs.optional("q") != nil ? "supported" : "NOT supported → Sent-folder inference")")194 let copy = try await engine.lookUp(messageId: mid, acct: acct, drafts: drafts, sent: sent)195 guard case .submitted(let emailId, let submissionId)? = copy else { Issue.record("the server should hold a submitted copy, lookup says \(String(describing: copy))"); return }196 try await engine.syncNow()197 let done = try #require(try await store.outboxItem(queued.id))198 #expect(done.state == .accepted && done.serverEmailId == emailId && done.submissionId == submissionId, "state \(done.state) \(done.lastError ?? "")")199 for _ in 0..<3 { await engine.flushOutbox() }200 // exactly one copy in Sent, none left in Drafts, exactly one delivered201 let got = try await d.received(subject)202 try await Task.sleep(for: .seconds(4)) // a duplicate would arrive right behind203 #expect(try await subjects(d, d.b, role: .inbox).filter { $0 == subject }.count == 1)204 try await engine.deltaSync()205 #expect(try await store.threads(Env.mbA, scope: .mailbox(sent), threaded: false, limit: 1000).filter { $0.email.subject == subject }.count == 1)206 #expect(try await store.threads(Env.mbA, scope: .mailbox(drafts), threaded: false, limit: 1000).contains { $0.email.subject == subject } == false)207 #expect(try await store.email(Env.mbA, id: emailId)?.messageId == [mid])208 await cleanup(d.b, [got.id]); await cleanup(engine, [emailId])209 }210211 // Two devices on the same account, real server: flag / keyword / move / delete / draft on A are seen on B through delta sync.212 @Test func twoDevicesConvergeOnTheRealServer() async throws {213 let one = try Device(), two = try Device()214 try await one.a.fullSync(); try await two.a.fullSync()215 let inbox = try #require(try await one.a.mailboxId(role: .inbox)), trash = try #require(try await one.a.mailboxId(role: .trash))216 var ids: [String] = []217 for n in 1...4 { ids.append(try await seed(one.a, "Deux appareils \(n) \(Self.mark)", box: inbox)) }218 try await one.a.deltaSync(); try await two.a.deltaSync()219 await one.a.setFlagged(true, ids: [ids[0]])220 await one.a.setKeyword("$pinned", true, ids: [ids[0]])221 await one.a.setKeyword("$followup", true, ids: [ids[3]])222 await one.a.setKeyword("lbl_0badcafe", true, ids: [ids[3]])223 await one.a.move(ids: [ids[1]], to: trash)224 await one.a.destroy(ids: [ids[2]])225 #expect(!(try await two.a.deltaSync()).wasFull)226 // the mail server adds `$hasnoattachment` / `$hasattachment` by itself: left out of the comparisons227 func own(_ e: EmailSummary?) -> [String]? { e?.keywords.filter { !$0.hasPrefix("$has") } }228 let k0 = own(try await two.store.email(Env.mbA, id: ids[0])), k3 = own(try await two.store.email(Env.mbA, id: ids[3]))229 #expect(k0 == ["$flagged", "$pinned"], "got \(String(describing: k0))")230 #expect(k3 == ["$followup", "lbl_0badcafe"], "got \(String(describing: k3))") // Next keywords round-trip through a real server231 #expect(try await two.store.email(Env.mbA, id: ids[1])?.mailboxIds == [trash])232 #expect(try await two.store.email(Env.mbA, id: ids[2]) == nil)233 // conflicting changes while device one is offline234 one.api.baseURL = Env.dead235 await one.a.move(ids: [ids[0]], to: trash)236 await one.a.setKeyword("$snoozed", true, ids: [ids[3]])237 await one.a.setFlagged(true, ids: [ids[1]])238 #expect(try await one.store.pending(Env.mbA).count == 3)239 await two.a.setFlagged(false, ids: [ids[0]])240 await two.a.setKeyword("lbl_0badcafe", false, ids: [ids[3]])241 await two.a.destroy(ids: [ids[1]])242 one.api.baseURL = Env.base243 try await one.a.syncNow()244 try await two.a.syncNow()245 try await one.a.syncNow()246 for id in [ids[0], ids[1], ids[3]] {247 let onOne = try await one.store.email(Env.mbA, id: id), onTwo = try await two.store.email(Env.mbA, id: id)248 #expect(onOne?.keywords == onTwo?.keywords && onOne?.mailboxIds == onTwo?.mailboxIds, "devices disagree on \(id)")249 let truth = try await serverEmail(one.a, id)250 #expect(onOne?.keywords == truth.map { ($0["keywords"].objectValue ?? [:]).keys.sorted() })251 #expect(onOne?.mailboxIds == truth.map { ($0["mailboxIds"].objectValue ?? [:]).keys.sorted() })252 }253 #expect(try await one.store.email(Env.mbA, id: ids[1]) == nil) // destroyed elsewhere: the offline flag is refused, nothing comes back254 #expect(try await one.store.pending(Env.mbA).isEmpty)255 #expect(try await one.store.rejectedOperations(Env.mbA).map(\.emailId) == [ids[1]])256 #expect(try await one.store.email(Env.mbA, id: ids[0])?.mailboxIds == [trash])257 #expect(own(try await one.store.email(Env.mbA, id: ids[0])) == ["$pinned"])258 #expect(own(try await one.store.email(Env.mbA, id: ids[3])) == ["$followup", "$snoozed"])259260 // a draft edited on both devices is not lost261 var onOne = try await one.a.saveDraft(OutboxItem(account: Env.mbA, message: one.message("Brouillon partagé \(Self.mark)", text: "version de départ", html: "<div>version de <b>départ</b></div>")))262 onOne = try #require(try await one.a.syncDraft(id: onOne.id))263 let sameAgain = try #require(try await one.a.syncDraft(id: onOne.id))264 #expect(sameAgain.serverEmailId == onOne.serverEmailId) // unchanged: not pushed again265 try await two.a.deltaSync()266 let firstId = try #require(onOne.serverEmailId)267 var onTwo = try await two.a.adoptServerDraft(try #require(try await two.store.email(Env.mbA, id: firstId)))268 #expect(HTMLText.html(fromAttributed: onTwo.message.editableBody) == "<div>version de <b>départ</b></div>", "stored HTML: \(onTwo.message.html ?? "nil")")269 onOne.message.text = "version de l'appareil un"; onOne.message.html = nil270 onOne = try await one.a.saveDraft(onOne)271 onOne = try #require(try await one.a.syncDraft(id: onOne.id))272 onTwo.message.text = "version de l'appareil deux"; onTwo.message.html = nil273 onTwo = try await two.a.saveDraft(onTwo)274 onTwo = try #require(try await two.a.syncDraft(id: onTwo.id))275 #expect(onTwo.conflictedAt != nil && onTwo.serverEmailId != onOne.serverEmailId)276 try await one.a.deltaSync()277 let drafts = try #require(try await one.a.mailboxId(role: .drafts))278 let both = try await one.store.threads(Env.mbA, scope: .mailbox(drafts), threaded: false, limit: 500).map(\.email).filter { $0.subject.contains(Self.mark) }279 #expect(Set(both.map(\.id)) == Set([onOne.serverEmailId, onTwo.serverEmailId].compactMap { $0 }), "drafts on the server: \(both.map(\.preview))")280 #expect(Set(both.map(\.preview)) == ["version de l'appareil un", "version de l'appareil deux"])281 await one.a.discardDraft(id: onOne.id); await two.a.discardDraft(id: onTwo.id)282 await cleanup(one.a, [ids[0], ids[3]])283 }284285 // Item 5 on the real server: anchor paging across sparse rows, in a folder of its own.286 @Test func anchorPagingLeavesNoGapOnTheRealServer() async throws {287 let d = try Device()288 try await d.a.fullSync()289 let folder = try await d.a.createMailbox(name: "Pagination \(Self.mark)")290 let t0 = Date(timeIntervalSince1970: 1_600_000_000)291 var ids: [String] = []292 for n in 0..<14 { ids.append(try await seed(d.a, "Page \(n) \(Self.mark)", box: folder, at: t0.addingTimeInterval(Double(n) * 3600))) } // oldest first; all older than anything else in the account293 let (store, _, _) = try tempStore()294 var config = SyncEngine.Configuration()295 config.initialWindow = 3; config.pageSize = 4; config.autoPrefetch = false296 let engine = SyncEngine(account: Env.mbA, address: Env.addrA, api: d.api, store: store, configuration: config)297 try await engine.fullSync() // the folder gets its first page: the four newest298 func cached() async throws -> Set<String> { Set(try await store.threads(Env.mbA, scope: .mailbox(folder), threaded: false, limit: 500).map(\.email.id)) }299 let firstPage = try await cached()300 let total = try await store.mailboxes(Env.mbA).first { $0.id == folder }?.totalEmails ?? -1301 #expect(firstPage == Set(ids.suffix(4)), "cached \(firstPage.map { ids.firstIndex(of: $0) ?? -1 }.sorted()) of \(total)")302 let acct = try await engine.accountId()303 let probe = try await engine.call([JMAPCall("Email/query", ["accountId": .string(acct), "filter": ["inMailbox": .string(folder)], "sort": [["property": "receivedAt", "isAscending": false]],304 "anchor": .string(ids[10]), "anchorOffset": 1, "limit": 2], "q")])305 print("INFO Email/query anchor: \((try? probe.result("q")["ids"].stringArray).map { $0 == [ids[9], ids[8]] ? "supported and exact" : "answered \($0)" } ?? "NOT supported → date fallback")")306 try await engine.ensureCached(ids: [ids[0], ids[1], ids[5]]) // sparse old rows: the old position-based paging skipped three messages here307 #expect(try await engine.loadOlder(mailboxId: folder) == 4)308 #expect(try await cached() == Set(ids.suffix(8)).union([ids[0], ids[1], ids[5]]))309 var pages = 0310 while try await engine.loadOlder(mailboxId: folder) > 0, pages < 10 { pages += 1 }311 #expect(try await cached() == Set(ids))312 #expect(await engine.hasOlder(mailboxId: folder) == false)313 try await d.a.deleteMailbox(id: folder)314 }315316 // Item 14 on the real server: the conversation is completed with Thread/get.317 @Test func threadIsCompletedFromTheRealServer() async throws {318 let d = try Device()319 try await d.a.fullSync()320 let inbox = try #require(try await d.a.mailboxId(role: .inbox))321 let folder = try await d.a.createMailbox(name: "Fil \(Self.mark)")322 let root = "racine-\(Self.mark)@stg.spboucher.ai"323 let old = try await seed(d.a, "Fil \(Self.mark)", box: folder, at: Date(timeIntervalSince1970: 1_600_000_000), messageId: root)324 for n in 0..<3 { _ = try await seed(d.a, "Remplissage \(n) \(Self.mark)", box: folder, at: Date(timeIntervalSince1970: 1_600_100_000 + Double(n))) }325 let reply = try await seed(d.a, "Re: Fil \(Self.mark)", box: inbox, messageId: "reponse-\(Self.mark)@stg.spboucher.ai", inReplyTo: root)326 let (store, _, _) = try tempStore()327 var config = SyncEngine.Configuration()328 config.initialWindow = 2; config.pageSize = 2; config.autoPrefetch = false329 let engine = SyncEngine(account: Env.mbA, address: Env.addrA, api: d.api, store: store, configuration: config)330 try await engine.fullSync()331 let head = try #require(try await store.email(Env.mbA, id: reply))332 #expect(try await store.email(Env.mbA, id: old) == nil) // the root is outside what was synced333 #expect(try await store.threadEmails(Env.mbA, threadId: head.threadId).map(\.id) == [reply])334 let whole = try await engine.thread(head.threadId)335 #expect(whole.complete && whole.emails.map(\.id) == [old, reply], "thread: \(whole.emails.map(\.subject))")336 try await d.a.deleteMailbox(id: folder)337 await cleanup(d.a, [reply])338 }339340 // Items 6 and 8 on the real server: a refused operation does not block the pass; a full resync keeps what was done offline and the bodies.341 @Test func refusedOperationAndFullResyncOnTheRealServer() async throws {342 let d = try Device()343 try await d.a.fullSync()344 let inbox = try #require(try await d.a.mailboxId(role: .inbox))345 let ids = [try await seed(d.a, "Poison 1 \(Self.mark)", box: inbox, text: "corps un"), try await seed(d.a, "Poison 2 \(Self.mark)", box: inbox), try await seed(d.a, "Poison 3 \(Self.mark)", box: inbox)]346 try await d.a.deltaSync()347 _ = try await d.a.body(for: ids[0])348 // a patch the server cannot apply sits in the queue between two valid ones349 try await d.store.addPending(Env.mbA, emailId: ids[0], patch: ["keywords/$flagged": true])350 try await d.store.addPending(Env.mbA, emailId: ids[1], patch: ["noSuchProperty/x": true])351 try await d.store.addPending(Env.mbA, emailId: ids[2], patch: ["keywords/$seen": true, "keywords/lbl_feedf00d": true])352 try await d.a.syncNow() // before the fix: thrown here, on every pass, forever353 let rejected = try await d.store.rejectedOperations(Env.mbA)354 print("INFO the mail server refused the invalid patch with: \(rejected.first?.error ?? "nothing")")355 #expect(rejected.map(\.emailId) == [ids[1]])356 #expect(try await d.store.pending(Env.mbA).isEmpty)357 #expect(try await serverEmail(d.a, ids[0])?["keywords"]["$flagged"].boolValue == true)358 #expect(try await serverEmail(d.a, ids[2])?["keywords"]["lbl_feedf00d"].boolValue == true)359 // offline change, then the server "forgets" our state: full resync, the change survives and is replayed afterwards360 d.api.baseURL = Env.dead361 await d.a.setKeyword("$pinned", true, ids: [ids[1]])362 d.api.baseURL = Env.base363 try await d.store.setState(Env.mbA, "Email", "zzz-bogus")364 #expect(try await d.a.deltaSync().wasFull)365 #expect(try await d.store.pending(Env.mbA).count == 1)366 #expect(try await d.store.email(Env.mbA, id: ids[1])?.keywords.contains("$pinned") == true)367 #expect(try await d.store.body(Env.mbA, id: ids[0])?.text?.contains("corps un") == true) // the body read before the resync is still there368 try await d.a.syncNow()369 #expect(try await d.store.pending(Env.mbA).isEmpty)370 #expect(try await serverEmail(d.a, ids[1])?["keywords"]["$pinned"].boolValue == true)371 // awaitable entry point: two callers, one pass, both return synchronised372 async let first: Void = d.a.syncNowAndWait()373 async let second: Void = d.a.syncNowAndWait()374 _ = try await (first, second)375 #expect(await d.a.status == .idle)376 await cleanup(d.a, ids)377 }378379 // Items 1 and 13 on the real server: a reply quoting an inline image, body prefetch, the sealed attachment cache.380 @Test func quotedInlineImageAndOfflineReadingOnTheRealServer() async throws {381 let d = try Device()382 try await d.a.fullSync()383 let inbox = try #require(try await d.a.mailboxId(role: .inbox))384 let png = try #require(Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="))385 let blob = try await d.a.jmap.upload(png, type: "image/png")386 let file = Data("PIECE-\(Self.mark)-contenu".utf8)387 let fileBlob = try await d.a.jmap.upload(file, type: "text/plain")388 let html = #"<p>Voir le <b>logo</b> : <img src="cid:logo-\#(Self.mark)@stg" alt="Logo"></p><img src="https://tracker.example/p.gif" width="1" height="1">"#389 let id = try await seed(d.a, "Image en ligne \(Self.mark)", box: inbox, text: "Voir le logo", html: html, attachments: [390 ["blobId": .string(blob.blobId), "type": "image/png", "name": "logo.png", "disposition": "inline", "cid": .string("logo-\(Self.mark)@stg")],391 ["blobId": .string(fileBlob.blobId), "type": "text/plain", "name": "note.txt", "disposition": "attachment"],392 ])393 try await d.a.deltaSync()394 let original = try #require(try await d.store.email(Env.mbA, id: id))395 // prefetch: the body is on the device without having been opened396 #expect(await d.a.prefetchBodies(ids: [id]) == 1)397 let cachedBody = try #require(try await d.store.body(Env.mbA, id: id))398 let identity = try #require(try await d.a.identities().first)399 let reply = ReplyBuilder.build(kind: .reply, original: original, body: cachedBody, identity: identity, me: [Env.addrA])400 var draft = try await d.a.saveDraft(OutboxItem(account: Env.mbA, message: reply))401 draft = try #require(try await d.a.syncDraft(id: draft.id))402 // what the server stored for that reply is what a recipient's client would receive403 let stored = try await d.a.body(for: try #require(draft.serverEmailId))404 let storedHTML = try #require(stored.html)405 for marker in ["spmail-cid", "spmail-img", "data-spm", "tracker.example"] { #expect(!storedHTML.contains(marker), "\(marker) in the stored reply: \(storedHTML)") }406 #expect(storedHTML.contains("cid:logo-\(Self.mark)@stg"))407 let inlinePart = try #require(stored.attachments.first { $0.cid?.contains("logo-\(Self.mark)") == true }, "the inline image was not re-attached: \(stored.attachments)")408 #expect(inlinePart.type == "image/png" && stored.attachments.count == 1) // the plain attachment does not follow a reply409 #expect(try await d.a.download(inlinePart) == png)410 // attachments: one download, then the sealed cache — also offline411 let note = try #require(cachedBody.attachments.first { $0.name == "note.txt" })412 #expect(await d.a.cachedAttachment(note) == nil)413 #expect(try await d.a.fetchAttachment(note) == file)414 d.api.baseURL = Env.dead415 #expect(try await d.a.fetchAttachment(note) == file)416 #expect(try await d.a.body(for: id).html == html)417 d.api.baseURL = Env.base418 try await d.store.checkpoint()419 #expect(!fileContains(d.dir, "PIECE-\(Self.mark)") && !fileContains(d.store.attachmentCacheDirectory, "PIECE-\(Self.mark)"))420 await d.a.discardDraft(id: draft.id)421 await cleanup(d.a, [id])422 }423424 // Item 15 on the real server: connecting costs one pass; a change made elsewhere arrives without polling.425 @Test func liveConnectionSyncsOnceThenFollowsChanges() async throws {426 let d = try Device(), other = try Device()427 try await d.a.fullSync(); try await other.a.fullSync()428 let passes = Counter()429 let subject = "En direct \(Self.mark)"430 let arrived = FakeRouter.Flag()431 let listener = Task {432 for await e in d.a.events {433 if case .syncing(true) = e { _ = passes.next() }434 if case .newMail(let l) = e, l.contains(where: { $0.subject == subject }) { arrived.set() }435 }436 }437 await d.a.startLive()438 try await Task.sleep(for: .seconds(6)) // connected, nothing changes439 print("INFO passes in the 6 s after connecting to the real EventSource with nothing changing: \(passes.value)")440 #expect(passes.value == 1, "connecting cost \(passes.value) passes")441 let inbox = try #require(try await other.a.mailboxId(role: .inbox))442 let id = try await seed(other.a, subject, box: inbox)443 _ = try await until("pushed change", timeout: 30) { arrived.isSet ? true : nil }444 try await Task.sleep(for: .seconds(2))445 print("INFO passes after one message was created elsewhere: \(passes.value)")446 #expect(passes.value >= 2 && passes.value <= 4)447 await d.a.stopLive()448 listener.cancel()449 await cleanup(other.a, [id])450 }451}452