SPB Git

Repository Public

StudioIntegrationTests.swift 23.4 KB 280 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/SPMailCore/Tests/SPMailCoreTests/StudioIntegrationTests.swift3// Description: Integration tests of the template studio, send-by-template, the unified inbox, alias identities and scheduled sends against a REAL4// SP Mail backend and mail server (staging mailboxes only). The two AI calls (one generation, one revision) cost money and run only with SPMAIL_TEST_REAL_AI=1.5import Foundation6import Testing7@testable import SPMailCore89extension Env {10    static var alias: String? { e["SPMAIL_TEST_ALIAS"].flatMap { $0.isEmpty ? nil : $0 } }11    static var realAI: Bool { enabled && e["SPMAIL_TEST_REAL_AI"] == "1" }12    static var accounts: [String: String] { [addrA.lowercased(): mbA, addrB.lowercased(): mbB] }13}1415let layoutHTML = """16<!DOCTYPE html><html lang="fr"><head><meta charset="utf-8"><meta name="color-scheme" content="light dark"><title></title>17<style>18body{margin:0;background:#f4f3ef}.card{background:#ffffff;color:#1c1b19}19@media (max-width:600px){.container{width:100% !important}}20@media (prefers-color-scheme:dark){body{background:#161716 !important}.card{background:#1f201e !important;color:#ecebe6 !important}}21</style></head><body>22<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="center" style="padding:32px 12px">23<table role="presentation" class="container card" width="600" style="width:600px;max-width:100%"><tr><td style="padding:36px 40px;font-family:-apple-system,Helvetica,Arial,sans-serif;font-size:16px">24<p style="margin:0 0 16px">Bonjour {{first_name}},</p><p style="margin:0"><a href="{{action_url}}">Ouvrir</a> — code {{code}}</p>25</td></tr></table></td></tr></table></body></html>26"""2728@Suite(.serialized, .enabled(if: Env.enabled, "needs the dev backend: node Scripts/dev-bootstrap.mjs up && source Build/dev-env.sh"))29struct StudioIntegrationTests {30    func makeTemplate(_ api: APIClient, name: String) async throws -> MailTemplate {31        try await api.createTemplate(TemplateContent(name: name, category: "transactional", subject: "Bienvenue {{first_name}} \(Env.run)", preheader: "Votre code : {{code}}", html: layoutHTML,32                                                     text: "Bonjour {{first_name}},\n{{action_url}}\ncode {{code}}",33                                                     variables: [TemplateVariable(name: "first_name", description: "Prénom", example: "Zoé", required: true), TemplateVariable(name: "code", example: "424242"),34                                                                 TemplateVariable(name: "action_url", example: "https://example.org/go"), TemplateVariable(name: "ghost", example: "never used")]))35    }3637    @Test func templateLibraryVersionsRenderPreviewAndUndo() async throws {38        let d = try Device()39        let api = d.api40        let t = try await makeTemplate(api, name: "Essai \(Env.run)")41        #expect(t.version == 1 && t.source == "manual" && t.variables.map(\.name) == ["action_url", "code", "first_name"]) // realigned by the server: no phantom variable42        #expect(t.html?.contains("<style>") == true && t.html?.contains("prefers-color-scheme") == true && t.sizeBytes > 500)43        let lib = try await api.templates()44        let listed = try #require(lib.templates.first { $0.id == t.id })45        #expect(listed.html == nil && listed.name == "Essai \(Env.run)" && lib.categories.contains("security") && !lib.aiModel.isEmpty)4647        // a rename alone keeps the version; a content change archives the previous one48        let renamed = try await api.updateTemplate(id: t.id, TemplateContent(name: "Renommé \(Env.run)", category: "notification"))49        #expect(renamed.version == 1 && renamed.name == "Renommé \(Env.run)" && renamed.category == "notification")50        let v2 = try await api.updateTemplate(id: t.id, TemplateContent(subject: "Deuxième objet {{first_name}}", html: layoutHTML.replacingOccurrences(of: "Ouvrir", with: "Ouvrir maintenant"), note: "essai \(Env.run)"))51        #expect(v2.version == 2 && v2.html?.contains("Ouvrir maintenant") == true)52        let versions = try await api.templateVersions(id: t.id)53        #expect(versions.map(\.version) == [1] && versions.first?.note == "essai \(Env.run)" && versions.first?.subject.hasPrefix("Bienvenue") == true)54        let old = try await api.templateVersion(id: t.id, version: 1)55        #expect(old.html.contains(">Ouvrir</a>") && old.variables.count == 3)56        let restored = try await api.restoreTemplateVersion(id: t.id, version: 1)57        #expect(restored.version == 3 && restored.html?.contains(">Ouvrir</a>") == true && restored.subject.hasPrefix("Bienvenue"))58        #expect(try await api.templateVersions(id: t.id).map(\.version) == [2, 1])5960        // render: escaped values, examples fill the rest, a non-URL in href becomes #61        let r = try await api.renderTemplate(id: t.id, values: ["first_name": "<b>Zoé</b>", "action_url": "javascript:alert(1)"])62        #expect(r.html.contains("Bonjour &lt;b&gt;Zoé&lt;/b&gt;,") && r.html.contains("href=\"#\"") && r.html.contains("code 424242") && r.missing.isEmpty && r.preheader == "Votre code : 424242")63        let strict = try await api.renderTemplate(id: t.id, values: [:], useExamples: false)64        #expect(strict.missing == ["first_name"] && strict.html.contains("Bonjour ,"))6566        // preview of an unsaved draft: the server's sanitiser removes active content and reports what it kept67        let p = try await api.previewTemplate(html: layoutHTML + "<script>alert(1)</script><p onclick=\"x()\">{{extra}}</p>", subject: "S {{first_name}}", variables: t.variables, values: ["first_name": "Léa"])68        let clean = try #require(p.sanitizedHTML)69        #expect(!clean.contains("<script") && !clean.contains("onclick") && clean.contains("<style>") && p.subject == "S Léa" && p.variables?.map(\.name) == ["action_url", "code", "extra", "first_name"])7071        // duplicate, delete with undo72        let copy = try await api.duplicateTemplate(id: t.id)73        #expect(copy.id != t.id && copy.name.hasSuffix("(copie)") && copy.version == 1)74        try await api.archiveTemplate(id: copy.id)75        #expect(try await api.templates().templates.contains { $0.id == copy.id } == false)76        do { _ = try await api.template(id: copy.id); Issue.record("archived template still readable") } catch let e as APIError { #expect(e.status == 404) }77        let back = try await api.unarchiveTemplate(id: copy.id)78        let again = try await api.templates()79        #expect(back.id == copy.id && again.templates.contains { $0.id == copy.id })80        try await api.archiveTemplate(id: copy.id)81        try await api.archiveTemplate(id: t.id)8283        // the brand kit lives under the key the webmail uses, and the merge keeps other preferences84        try await api.patchPrefs(["appleTestKey": .string(Env.run)])85        var brand = TemplateBrand(); brand.name = "Essai \(Env.run)"; brand.accent = "#4f46e5"; brand.website = "https://example.org"86        try await api.saveBrand(brand)87        let prefs = try #require(try await api.me().user.prefs)88        #expect(TemplateBrand(json: prefs["templateBrand"]) == brand && prefs["appleTestKey"].stringValue == Env.run)89        try await api.patchPrefs(["appleTestKey": nil, "templateBrand": nil])90        #expect(try await api.me().user.prefs?["templateBrand"].isNull == true)91        let usage = try await api.aiUsage()92        #expect(usage.model == lib.aiModel && usage.configured == lib.aiConfigured && usage.generations >= 0)93    }9495    @Test(.enabled(if: Env.realAI, "one real generation and one real revision (about US$0.60): SPMAIL_TEST_REAL_AI=1"), .timeLimit(.minutes(8)))96    func realGenerationThenRealRevision() async throws {97        let api = try Device().api98        var brand = TemplateBrand(); brand.name = "SP Mail"; brand.accent = "#0f766e"99        var events: [GenerationEvent] = []100        let started = Date()101        for try await e in TemplateGenerationStream.events(api: api, request: GenerationRequest(brief: "Courriel très court de lien de connexion sans mot de passe : une phrase, un bouton « Me connecter », durée de validité de 15 minutes.", language: "fr", category: "security", brand: brand)) { events.append(e) }102        guard case .start(let model, false) = events.first, case .done(let t, let usage) = events.last else { Issue.record("unexpected events \(events.prefix(2)) … \(events.suffix(1))"); return }103        #expect(!model.isEmpty && t.isAI && t.version == 1 && t.aiModel != nil && t.brand.accent == "#0f766e" && usage.outputTokens > 0 && usage.seconds > 0)104        #expect(events.contains { if case .progress = $0 { return true } else { return false } })105        let html = try #require(t.html)106        #expect(html.lowercased().contains("<!doctype html") && html.contains("<style") && !html.lowercased().contains("<script") && !t.variables.isEmpty)107        print("real generation: \(Int(Date().timeIntervalSince(started))) s, \(html.utf8.count) bytes, \(usage.outputTokens) output tokens, \(events.count) events")108109        var revision: [GenerationEvent] = []110        for try await e in TemplateGenerationStream.events(api: api, request: GenerationRequest(brief: "Ajoute sous le bouton une ligne discrète : « Besoin d’aide ? Écrivez-nous. »", language: "fr", baseTemplateId: t.id)) { revision.append(e) }111        guard case .start(_, true) = revision.first, case .done(let r, _) = revision.last else { Issue.record("unexpected revision events"); return }112        #expect(r.id == t.id && r.version == 2 && r.html != html && r.html?.contains("aide") == true)113        let versions = try await api.templateVersions(id: t.id)114        #expect(versions.map(\.version) == [1] && versions.first?.note.hasPrefix("retouche par IA") == true)115        #expect(try await api.aiUsage().generations >= 2)116        try await api.archiveTemplate(id: t.id)117    }118119    @Test func sendByTemplateKeepsTheLayoutAndReportsDelivery() async throws {120        let d = try Device()121        try await d.b.fullSync()122        let t = try await makeTemplate(d.api, name: "Envoi \(Env.run)")123        // a required variable is missing: refused by the server, nothing leaves124        do { _ = try await d.api.sendTemplate(TemplateSend(from: Env.addrA, to: [EmailAddress(email: Env.addrB)], templateId: t.id, variables: ["code": "1"]), idempotencyKey: UUID().uuidString); Issue.record("should be refused") }125        catch let e as APIError { #expect(e.status == 400 && e.code == "missing_variables" && e.message.contains("first_name")) }126        do { _ = try await d.api.sendTemplate(TemplateSend(from: Env.addrA, to: [EmailAddress(email: Env.addrB)], templateId: t.id, variables: ["first_name": "x", "nope": "1"]), idempotencyKey: UUID().uuidString); Issue.record("should be refused") }127        catch let e as APIError { #expect(e.code == "unknown_variables") }128129        let subject = "Modèle \(Env.run) pour Zoé"130        let send = TemplateSend(from: Env.addrA, to: [EmailAddress(name: "Essai B", email: Env.addrB)], templateId: t.id, variables: ["first_name": "Zoé <\(Env.run)>", "code": "777 \(Env.run)", "action_url": "https://example.org/v?r=\(Env.run)"], subject: subject)131        let key = "apple-\(UUID().uuidString)"132        let out = try await d.api.sendTemplate(send, idempotencyKey: key)133        #expect(out.status == "sent" && out.id.hasPrefix("msg_out_") && out.from == Env.addrA && out.recipients.map(\.address) == [Env.addrB], "status \(out.status) \(out.lastError ?? "")")134        // the same key answers the first outcome again instead of sending twice135        #expect(try await d.api.sendTemplate(send, idempotencyKey: key).id == out.id)136137        let got = try await d.received(subject)138        let body = try await d.b.body(for: got.id)139        let html = try #require(body.html)140        #expect(html.contains("<style>") && html.contains("@media (prefers-color-scheme:dark)") && html.contains("class=\"container card\""), "layout lost: \(html.prefix(300))")141        #expect(html.contains("Bonjour Zoé &lt;\(Env.run)&gt;,") && html.contains("code 777 \(Env.run)") && html.contains("href=\"https://example.org/v?r=\(Env.run)\"") && !html.contains("{{"))142        #expect(body.text?.contains("Bonjour Zoé <\(Env.run)>,") == true && got.from.first?.email == Env.addrA)143        // honest status: `sent` first, `delivered` only when the receiving server said so144        let final = try await until("delivery status of \(out.id)") { () -> OutboundStatus? in145            let o = try await d.api.outbound(id: out.id)146            return o.isFinal ? o : nil147        }148        #expect(final.status == "delivered" && final.recipients.first?.status == "delivered" && final.recipients.first?.smtpReply?.hasPrefix("250") == true, "\(final)")149        try await d.b.delete(ids: [got.id]); try await d.b.delete(ids: [got.id])150        try await d.api.archiveTemplate(id: t.id)151    }152153    @Test func scheduledSendAppearsThenIsCancelled() async throws {154        let api = try Device().api155        let t = try await makeTemplate(api, name: "Programmé \(Env.run)")156        let when = Date().addingTimeInterval(3 * 3600)157        let out = try await api.sendTemplate(TemplateSend(from: Env.addrA, to: [EmailAddress(email: Env.addrB)], templateId: t.id, variables: ["first_name": "Plus tard"], sendAt: when), idempotencyKey: UUID().uuidString)158        #expect(out.status == "scheduled" && out.isScheduled && abs((out.sendAt ?? .distantPast).timeIntervalSince(when)) < 2)159        let listed = try await api.scheduledSends()160        #expect(listed.contains { $0.id == out.id && $0.from == Env.addrA && $0.recipients.first?.address == Env.addrB })161        let cancelled = try await api.cancelScheduled(id: out.id)162        #expect(cancelled.status == "cancelled" && cancelled.isFinal)163        #expect(try await api.scheduledSends().contains { $0.id == out.id } == false)164        do { try await api.cancelScheduled(id: out.id); Issue.record("second cancel should be refused") } catch let e as APIError { #expect(e.status == 409 && e.code == "not_cancellable") }165        do { _ = try await api.sendTemplate(TemplateSend(from: Env.addrA, to: [EmailAddress(email: Env.addrB)], templateId: t.id, variables: ["first_name": "x"], sendAt: Date().addingTimeInterval(40 * 86400)), idempotencyKey: UUID().uuidString); Issue.record("too far") }166        catch let e as APIError { #expect(e.code == "send_at_too_far") }167        try await api.archiveTemplate(id: t.id)168    }169170    @Test func unifiedListAcrossTwoMailboxesWithPerMailboxActions() async throws {171        let d = try Device()172        try await d.a.fullSync(); try await d.b.fullSync()173        let toB = "Unifié \(Env.run) vers B", toA = "Unifié \(Env.run) vers A"174        let inB = try await d.deliver(toB, text: "premier")175        let sentBack = await d.b.send(OutboxItem(account: Env.mbB, message: d.message(toA, text: "second", to: Env.addrA, from: Env.addrB)))176        #expect(sentBack.state == .accepted)177        let inA = try await d.received(toA, in: d.a)178179        let page = try await d.api.unifiedMessages(.inbox, accounts: Env.accounts, limit: 100)180        let rowA = try #require(page.messages.first { $0.email.subject == toA }), rowB = try #require(page.messages.first { $0.email.subject == toB })181        #expect(rowA.account == Env.mbA && rowA.email.id == inA.id && rowA.mailbox == Env.addrA && rowA.receivingAddress == Env.addrA && rowA.roles == ["inbox"])182        #expect(rowB.account == Env.mbB && rowB.email.id == inB.id && rowB.mailbox == Env.addrB && !rowB.email.isSeen && rowB.email.threadId == inB.threadId)183        let dates = page.messages.map(\.email.receivedAt)184        #expect(dates == dates.sorted(by: >) && Set(page.messages.map(\.id)).count == page.messages.count && Set(page.messages.compactMap(\.account)) == [Env.mbA, Env.mbB])185        // walking the cursor two rows at a time gives one ordered stream: no duplicate, no gap186        var walked: [String] = [], cursor: String? = nil187        repeat {188            let p = try await d.api.unifiedMessages(.inbox, accounts: Env.accounts, cursor: cursor, limit: 2)189            walked += p.messages.map(\.id); cursor = p.nextCursor190        } while cursor != nil && walked.count < 400191        // (other suites deliver and move mail in the same mailboxes meanwhile: the walk is checked on what cannot change — no duplicate, both messages, same order)192        let ia = try #require(walked.firstIndex(of: rowA.id)), ib = try #require(walked.firstIndex(of: rowB.id))193        let pa = try #require(page.messages.firstIndex { $0.id == rowA.id }), pb = try #require(page.messages.firstIndex { $0.id == rowB.id })194        #expect(Set(walked).count == walked.count && (ia < ib) == (pa < pb), "walk \(walked.map { $0.suffix(6) }) vs page \(page.messages.map { $0.id.suffix(6) }) a \(ia)/\(pa) b \(ib)/\(pb)")195        // a cursor belongs to the query that produced it196        let foreign = try #require(try await d.api.unifiedMessages(.inbox, accounts: Env.accounts, limit: 1).nextCursor)197        do { _ = try await d.api.unifiedMessages(.sent, accounts: Env.accounts, cursor: foreign); Issue.record("foreign cursor accepted") } catch let e as APIError { #expect(e.code == "invalid_cursor") }198199        // actions go to the engine of the row's own mailbox200        let engines = [Env.mbA: d.a, Env.mbB: d.b]201        try await engines[rowA.account!]!.ensureCached(ids: [rowA.email.id])202        await engines[rowA.account!]!.setFlagged(true, ids: [rowA.email.id])203        await engines[rowB.account!]!.markSeen(true, ids: [rowB.email.id])204        let flagged = try await d.api.unifiedMessages(.flagged, accounts: Env.accounts, limit: 100)205        #expect(flagged.messages.contains { $0.id == rowA.id && $0.email.isFlagged } && !flagged.messages.contains { $0.id == rowB.id })206        let unread = try await d.api.unifiedMessages(.unread, accounts: Env.accounts, limit: 100)207        #expect(unread.messages.contains { $0.id == rowA.id } && !unread.messages.contains { $0.id == rowB.id })208        #expect(unread.messages.allSatisfy { $0.roles.isDisjoint(with: ["trash", "junk", "sent", "drafts"]) || $0.roles.contains("inbox") })209        let sent = try await d.api.unifiedMessages(.sent, accounts: Env.accounts, limit: 100)210        #expect(sent.messages.contains { $0.email.subject == toB && $0.account == Env.mbA } && sent.messages.contains { $0.email.subject == toA && $0.account == Env.mbB })211212        // the same actions through /api/v1 (rows whose id could not be handed to an engine)213        try await d.api.v1SetFlags(id: rowB.id, unread: true, starred: true)214        try await d.b.refetch([rowB.email.id])215        let afterV1 = try #require(try await d.store.email(Env.mbB, id: rowB.email.id))216        #expect(!afterV1.isSeen && afterV1.isFlagged)217        #expect(try await d.api.v1Body(id: rowB.id).text?.contains("premier") == true)218        try await d.api.v1Move(id: rowB.id, folder: "archive")219        #expect(try await d.api.unifiedMessages(.inbox, accounts: Env.accounts, limit: 100).messages.contains { $0.id == rowB.id } == false)220        try await d.api.v1Trash(id: rowB.id)221        try await d.a.archive(ids: [rowA.email.id])222        #expect(try await d.api.unifiedMessages(.inbox, accounts: Env.accounts, limit: 100).messages.contains { $0.id == rowA.id } == false)223        try await d.a.delete(ids: [rowA.email.id]); try await d.a.delete(ids: [rowA.email.id])224        try await d.b.deltaSync(); try await d.b.delete(ids: [rowB.email.id])225226        // offline: the merge of the local caches stands in227        let offline = try await d.store.unified(.sent, accounts: [(id: Env.mbA, address: Env.addrA), (id: Env.mbB, address: Env.addrB)])228        #expect(offline.contains { $0.email.subject == toB && $0.account == Env.mbA })229    }230231    @Test(.enabled(if: Env.alias != nil, "needs the alias created by dev-bootstrap.mjs"))232    func aliasIdentitySendsWithTheAliasInFromAndReceivesReplies() async throws {233        let d = try Device()234        try await d.a.fullSync(); try await d.b.fullSync()235        let alias = try #require(Env.alias)236        precondition(alias.hasPrefix("apple-alias-") && alias.hasSuffix("@stg.spboucher.ai"))237        let rows = try await d.api.addresses()238        let row = try #require(rows.first { $0.address == alias })239        #expect(row.kind == "alias" && row.mailboxId == Env.mbA && row.isSendable && !row.isPrimary && rows.first { $0.address == Env.addrA }?.isPrimary == true)240        let before = try await d.a.identities(refresh: true)241        let list = SenderIdentity.list(addresses: rows, identities: [Env.mbA: before, Env.mbB: try await d.b.identities(refresh: true)],242                                       accounts: [(id: Env.mbA, address: Env.addrA, name: "A"), (id: Env.mbB, address: Env.addrB, name: "B")])243        #expect(list.map(\.email) == [Env.addrA, alias, Env.addrB])244        let sender = try #require(list.first { $0.email == alias })245        #expect(sender.isAlias && sender.account == Env.mbA && sender.name == "Apple Alias" && sender.signature == "Alias — signature d’essai")246247        // JMAP path (the outbox): the alias gets its own identity on the mail server, the message leaves with the alias in From248        let subject = "Alias \(Env.run)"249        let message = OutgoingMessage(identityId: sender.jmapIdentityId, from: sender.address, to: [EmailAddress(email: Env.addrB)], subject: subject, text: "depuis l’alias" + SignatureBlock.block(sender.signature))250        let sent = await d.a.send(OutboxItem(account: sender.account, message: message))251        #expect(sent.state == .accepted, "\(sent.state) \(sent.lastError ?? "")")252        let got = try await d.received(subject)253        #expect(got.from.map(\.email) == [alias] && got.from.first?.name == "Apple Alias", "From was \(got.from)")254        let raw = String(decoding: try await d.b.exportEML(got), as: UTF8.self)255        let fromHeader = try #require(raw.components(separatedBy: "\r\n").first { $0.lowercased().hasPrefix("from:") })256        #expect(fromHeader.contains("<\(alias)>") && !fromHeader.contains(Env.addrA))257        #expect(try await d.a.identities().contains { $0.email == alias })258259        // offline: an alias message waits in the same outbox and leaves on reconnect, still from the alias260        let offlineSubject = "Alias hors ligne \(Env.run)"261        d.api.baseURL = Env.dead262        let queued = await d.a.send(OutboxItem(account: sender.account, message: OutgoingMessage(from: sender.address, to: [EmailAddress(email: Env.addrB)], subject: offlineSubject, text: "plus tard")))263        #expect(queued.state == .queuedOffline)264        d.api.baseURL = Env.base265        try await d.a.syncNow() // what the app does when the network comes back266        #expect(try await d.received(offlineSubject).from.map(\.email) == [alias])267268        // a reply written to the alias arrives in mailbox A; the unified list names the alias as the receiving address, and the reply starts from it269        let reply = await d.b.send(OutboxItem(account: Env.mbB, message: d.message("Re: \(subject)", text: "réponse", to: alias, from: Env.addrB)))270        #expect(reply.state == .accepted)271        let back = try await d.received("Re: \(subject)", in: d.a)272        let rowA = try await until("unified row for the alias") { try await d.api.unifiedMessages(.inbox, accounts: Env.accounts, limit: 50).messages.first { $0.email.subject == "Re: \(subject)" } }273        #expect(rowA.account == Env.mbA && rowA.email.id == back.id && rowA.receivingAddress == alias && rowA.mailbox == Env.addrA)274        #expect(SenderIdentity.forReply(to: back, among: list, deliveredTo: rowA.deliveredTo, fallback: list.first)?.email == alias)275        try await d.a.delete(ids: [back.id]); try await d.a.delete(ids: [back.id])276        try await d.b.deltaSync()277        for s in [subject, offlineSubject] { if let m = try? await d.received(s) { try await d.b.delete(ids: [m.id]); try await d.b.delete(ids: [m.id]) } }278    }279}280
Files main
Menu

Theme