Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/SPMailSystem/Tests/SPMailSystemTests/DeepLinkTests.swift3// Description: Deep-link parsing: `mailto:` with subject / body / cc / bcc (RFC 6068 details: literal plus, several recipients, header4// injection), the app scheme used by widgets, notifications and intents, and what is refused.5import Foundation6import Testing7@testable import SPMailSystem89@Suite struct DeepLinkTests {10 @Test func mailtoWithSubjectBodyAndCc() throws {11 let m = try #require(MailtoLink(url: URL(string: "mailto:alice@stg.spboucher.ai?subject=Budget%202027&body=Bonjour%2C%0D%0Avoici%20le%201%2B1&cc=bob@stg.spboucher.ai,%20carl@stg.spboucher.ai&BCC=dan@stg.spboucher.ai")!))12 #expect(m.to == ["alice@stg.spboucher.ai"] && m.subject == "Budget 2027" && m.body == "Bonjour,\nvoici le 1+1")13 #expect(m.cc == ["bob@stg.spboucher.ai", "carl@stg.spboucher.ai"] && m.bcc == ["dan@stg.spboucher.ai"])14 }15 @Test func mailtoKeepsALiteralPlusAndAcceptsSeveralRecipients() throws {16 let m = try #require(MailtoLink(url: URL(string: "mailto:a+tag@stg.spboucher.ai,b@stg.spboucher.ai?to=c@stg.spboucher.ai&subject=1+1")!))17 #expect(m.to == ["a+tag@stg.spboucher.ai", "b@stg.spboucher.ai", "c@stg.spboucher.ai"] && m.subject == "1+1")18 }19 @Test func mailtoRefusesHeaderInjectionAndUnknownHeaders() throws {20 let m = try #require(MailtoLink(url: URL(string: "mailto:a@stg.spboucher.ai?subject=Hi%0D%0ABcc:%20evil@example.com&from=boss@example.com&cc=x@stg.spboucher.ai%0Abcc:evil@example.com")!))21 #expect(!m.subject.contains("\n") && !m.subject.contains("\r") && m.bcc.isEmpty && m.cc.isEmpty)22 }23 @Test func mailtoWithoutAnythingAndNonMailto() {24 #expect(MailtoLink(url: URL(string: "mailto:")!)?.isEmpty == true)25 #expect(MailtoLink(url: URL(string: "MAILTO:a@stg.spboucher.ai")!)?.to == ["a@stg.spboucher.ai"])26 #expect(MailtoLink(url: URL(string: "https://mail.spboucher.ai")!) == nil)27 }28 @Test func mailtoRoundTrip() throws {29 let m = MailtoLink(to: ["a@stg.spboucher.ai"], cc: ["b@stg.spboucher.ai"], subject: "Été & hiver = 100 %", body: "Ligne 1\nLigne 2 ?")30 #expect(MailtoLink(url: m.url) == m)31 #expect(DeepLink(url: m.url) == .compose(m))32 }33 @Test func widgetAndNotificationURLs() throws {34 let mb = "d4246e1d-0dd3-4922-943f-657ae17d2876"35 let link = DeepLink.conversation(mailboxId: mb, emailId: "Mabc123", threadId: "Tdef")36 #expect(link.url().absoluteString == "spmail://conversation?mb=\(mb)&email=Mabc123&thread=Tdef")37 #expect(DeepLink(url: link.url()) == link && DeepLink(url: link.url(dev: true)) == link && link.url(dev: true).scheme == "spmail-dev")38 #expect(DeepLink(url: URL(string: "spmail://conversation?mb=\(mb)&email=Mabc123")!) == .conversation(mailboxId: mb, emailId: "Mabc123", threadId: nil))39 for l in [DeepLink.inbox(mailboxId: nil), .inbox(mailboxId: mb), .compose(nil), .search("facture été"), .search(""), .reminders, .shareQueue] { #expect(DeepLink(url: l.url()) == l) }40 #expect(DeepLink(url: URL(string: "spmail://compose?to=a@stg.spboucher.ai&subject=Bonjour")!) == .compose(MailtoLink(to: ["a@stg.spboucher.ai"], subject: "Bonjour")))41 }42 @Test func refusedURLs() {43 // a conversation link needs both identifiers, and identifiers cannot smuggle a path or a query44 for bad in ["spmail://conversation?mb=abc", "spmail://conversation?email=abc", "spmail://conversation?mb=../x&email=a", "spmail://conversation?mb=a&email=b%2Fc", "spmail://unknown", "https://mail.spboucher.ai/inbox", "file:///etc/passwd", "spmail-other://inbox"] {45 #expect(DeepLink(url: URL(string: bad)!) == nil, "\(bad)")46 }47 }48}49