|
| 1 | +// |
| 2 | +// DeepLinkRouterTests.swift |
| 3 | +// SuperwallKit |
| 4 | +// |
| 5 | +// Created by Yusuf Tör on 25/11/2025. |
| 6 | +// |
| 7 | +// swiftlint:disable all |
| 8 | + |
| 9 | +import Testing |
| 10 | +@testable import SuperwallKit |
| 11 | +import Foundation |
| 12 | + |
| 13 | +struct DeepLinkRouterTests { |
| 14 | + // MARK: - Superwall Universal Links |
| 15 | + |
| 16 | + @Test("Returns true for Superwall universal link with superwall.app domain") |
| 17 | + func storeDeepLink_superwallUniversalLink_superwallApp() { |
| 18 | + let url = URL(string: "https://myapp.superwall.app/app-link/some/path")! |
| 19 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 20 | + #expect(result == true) |
| 21 | + } |
| 22 | + |
| 23 | + @Test("Returns true for Superwall universal link with superwallapp.dev domain") |
| 24 | + func storeDeepLink_superwallUniversalLink_superwallappDev() { |
| 25 | + let url = URL(string: "https://myapp.superwallapp.dev/app-link/some/path")! |
| 26 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 27 | + #expect(result == true) |
| 28 | + } |
| 29 | + |
| 30 | + @Test("Returns false for superwall.app without app-link path") |
| 31 | + func storeDeepLink_superwallDomain_noAppLinkPath() { |
| 32 | + let url = URL(string: "https://myapp.superwall.app/other/path")! |
| 33 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 34 | + #expect(result == false) |
| 35 | + } |
| 36 | + |
| 37 | + // MARK: - Redemption Code URLs |
| 38 | + |
| 39 | + @Test("Returns true for URL scheme redemption code") |
| 40 | + func storeDeepLink_redemptionCode_urlScheme() { |
| 41 | + // Format: scheme://superwall/redeem?code=XXX (host="superwall", path="/redeem") |
| 42 | + let url = URL(string: "myapp://superwall/redeem?code=ABC123")! |
| 43 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 44 | + #expect(result == true) |
| 45 | + } |
| 46 | + |
| 47 | + @Test("Returns true for superwall.app redemption code") |
| 48 | + func storeDeepLink_redemptionCode_superwallApp() { |
| 49 | + let url = URL(string: "https://myapp.superwall.app/app-link/superwall/redeem?code=ABC123")! |
| 50 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 51 | + #expect(result == true) |
| 52 | + } |
| 53 | + |
| 54 | + @Test("Returns true for superwallapp.dev redemption code") |
| 55 | + func storeDeepLink_redemptionCode_superwallappDev() { |
| 56 | + let url = URL(string: "https://myapp.superwallapp.dev/app-link/superwall/redeem?code=ABC123")! |
| 57 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 58 | + #expect(result == true) |
| 59 | + } |
| 60 | + |
| 61 | + @Test("Returns false for redemption path without code parameter") |
| 62 | + func storeDeepLink_redemptionPath_noCode() { |
| 63 | + let url = URL(string: "myapp://superwall/redeem")! |
| 64 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 65 | + #expect(result == false) |
| 66 | + } |
| 67 | + |
| 68 | + // MARK: - Debug/Preview URLs |
| 69 | + |
| 70 | + @Test("Returns true for debug URL with superwall_debug and token") |
| 71 | + func storeDeepLink_debugUrl_withToken() { |
| 72 | + let url = URL(string: "myapp://?superwall_debug=true&token=abc123")! |
| 73 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 74 | + #expect(result == true) |
| 75 | + } |
| 76 | + |
| 77 | + @Test("Returns true for debug URL with paywall_id") |
| 78 | + func storeDeepLink_debugUrl_withPaywallId() { |
| 79 | + let url = URL(string: "myapp://?superwall_debug=true&token=abc123&paywall_id=pw123")! |
| 80 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 81 | + #expect(result == true) |
| 82 | + } |
| 83 | + |
| 84 | + @Test("Returns false for superwall_debug without token") |
| 85 | + func storeDeepLink_debugUrl_noToken() { |
| 86 | + let url = URL(string: "myapp://?superwall_debug=true")! |
| 87 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 88 | + #expect(result == false) |
| 89 | + } |
| 90 | + |
| 91 | + @Test("Returns false for superwall_debug=false") |
| 92 | + func storeDeepLink_debugUrl_debugFalse() { |
| 93 | + let url = URL(string: "myapp://?superwall_debug=false&token=abc123")! |
| 94 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 95 | + #expect(result == false) |
| 96 | + } |
| 97 | + |
| 98 | + // MARK: - Non-Superwall URLs |
| 99 | + |
| 100 | + @Test("Returns false for generic app URL") |
| 101 | + func storeDeepLink_genericAppUrl() { |
| 102 | + let url = URL(string: "myapp://home")! |
| 103 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 104 | + #expect(result == false) |
| 105 | + } |
| 106 | + |
| 107 | + @Test("Returns false for external website URL") |
| 108 | + func storeDeepLink_externalWebsite() { |
| 109 | + let url = URL(string: "https://example.com/some/path")! |
| 110 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 111 | + #expect(result == false) |
| 112 | + } |
| 113 | + |
| 114 | + @Test("Returns false for URL with unrelated query params") |
| 115 | + func storeDeepLink_unrelatedQueryParams() { |
| 116 | + let url = URL(string: "myapp://settings?theme=dark&language=en")! |
| 117 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 118 | + #expect(result == false) |
| 119 | + } |
| 120 | + |
| 121 | + @Test("Returns false for app deep link that looks similar") |
| 122 | + func storeDeepLink_similarLookingUrl() { |
| 123 | + let url = URL(string: "myapp://superwall/premium")! |
| 124 | + let result = DeepLinkRouter.storeDeepLink(url) |
| 125 | + #expect(result == false) |
| 126 | + } |
| 127 | + |
| 128 | + // MARK: - Cached Config with deepLink_open Trigger |
| 129 | + // |
| 130 | + // Note: The storeDeepLink function reads from the actual disk cache, |
| 131 | + // which makes it difficult to test the cached config scenario in isolation. |
| 132 | + // The isSuperwallURL logic for checking cached config is tested implicitly |
| 133 | + // through integration tests. The URL format detection tests above cover |
| 134 | + // the primary use cases. |
| 135 | + |
| 136 | + // MARK: - URL Extension Tests |
| 137 | + |
| 138 | + @Test("isSuperwallDeepLink returns true for valid format") |
| 139 | + func isSuperwallDeepLink_validFormat() { |
| 140 | + let url = URL(string: "https://test.superwall.app/app-link/path")! |
| 141 | + #expect(url.isSuperwallDeepLink == true) |
| 142 | + } |
| 143 | + |
| 144 | + @Test("isSuperwallDeepLink returns false without app-link prefix") |
| 145 | + func isSuperwallDeepLink_noAppLinkPrefix() { |
| 146 | + let url = URL(string: "https://test.superwall.app/other/path")! |
| 147 | + #expect(url.isSuperwallDeepLink == false) |
| 148 | + } |
| 149 | + |
| 150 | + @Test("isSuperwallDeepLink returns false for non-superwall domain") |
| 151 | + func isSuperwallDeepLink_wrongDomain() { |
| 152 | + let url = URL(string: "https://example.com/app-link/path")! |
| 153 | + #expect(url.isSuperwallDeepLink == false) |
| 154 | + } |
| 155 | + |
| 156 | + @Test("redeemableCode returns code from URL scheme format") |
| 157 | + func redeemableCode_urlScheme() { |
| 158 | + // Format: scheme://superwall/redeem?code=XXX (host="superwall", path="/redeem") |
| 159 | + let url = URL(string: "myapp://superwall/redeem?code=TESTCODE")! |
| 160 | + #expect(url.redeemableCode == "TESTCODE") |
| 161 | + } |
| 162 | + |
| 163 | + @Test("redeemableCode returns code from universal link format") |
| 164 | + func redeemableCode_universalLink() { |
| 165 | + let url = URL(string: "https://app.superwall.app/app-link/superwall/redeem?code=TESTCODE")! |
| 166 | + #expect(url.redeemableCode == "TESTCODE") |
| 167 | + } |
| 168 | + |
| 169 | + @Test("redeemableCode returns nil for non-redemption URL") |
| 170 | + func redeemableCode_nonRedemption() { |
| 171 | + let url = URL(string: "myapp://home")! |
| 172 | + #expect(url.redeemableCode == nil) |
| 173 | + } |
| 174 | +} |
0 commit comments