From b6317d73242d48b1139b9d13f1fe3417bf9d23db Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Mon, 1 Jan 2024 09:48:53 +0100 Subject: [PATCH] Add tests for Router --- Packages/Env/Package.swift | 4 ++ Packages/Env/Tests/RouterTests.swift | 50 +++++++++++++++++++ .../Models/Sources/Models/OauthToken.swift | 7 +++ 3 files changed, 61 insertions(+) create mode 100644 Packages/Env/Tests/RouterTests.swift diff --git a/Packages/Env/Package.swift b/Packages/Env/Package.swift index b545f08c..8c7b09b4 100644 --- a/Packages/Env/Package.swift +++ b/Packages/Env/Package.swift @@ -33,5 +33,9 @@ let package = Package( .enableExperimentalFeature("StrictConcurrency"), ] ), + .testTarget( + name: "EnvTests", + dependencies: ["Env"] + ), ] ) diff --git a/Packages/Env/Tests/RouterTests.swift b/Packages/Env/Tests/RouterTests.swift new file mode 100644 index 00000000..04b60ef9 --- /dev/null +++ b/Packages/Env/Tests/RouterTests.swift @@ -0,0 +1,50 @@ +@testable import Env +import XCTest +import SwiftUI +import Network + +@MainActor +final class RouterTests: XCTestCase { + func testRouterThreadsURL() { + let router = RouterPath() + let url = URL(string: "https://www.threads.net/@dimillian")! + _ = router.handle(url: url) + XCTAssertTrue(router.path.isEmpty) + } + + func testRouterTagsURL() { + let router = RouterPath() + let url = URL(string: "https://mastodon.social/tags/test")! + _ = router.handle(url: url) + XCTAssertTrue(router.path.first == .hashTag(tag: "test", account: nil)) + } + + func testRouterLocalStatusURL() { + let router = RouterPath() + let client = Client(server: "mastodon.social", + oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0)) + client.addConnections(["mastodon.social"]) + router.client = client + let url = URL(string: "https://mastodon.social/status/1010384")! + _ = router.handle(url: url) + XCTAssertTrue(router.path.first == .statusDetail(id: "1010384")) + } + + func testRouterRemoteStatusURL() { + let router = RouterPath() + let client = Client(server: "mastodon.social", + oauthToken: .init(accessToken: "", tokenType: "", scope: "", createdAt: 0)) + client.addConnections(["mastodon.social", "mastodon.online"]) + router.client = client + let url = URL(string: "https://mastodon.online/status/1010384")! + _ = router.handle(url: url) + XCTAssertTrue(router.path.first == .remoteStatusDetail(url: url)) + } + + func testRouteRandomURL() { + let router = RouterPath() + let url = URL(string: "https://theweb.com/test/test/one")! + _ = router.handle(url: url) + XCTAssertTrue(router.path.isEmpty) + } +} diff --git a/Packages/Models/Sources/Models/OauthToken.swift b/Packages/Models/Sources/Models/OauthToken.swift index 78fb7419..fdbbbd3b 100644 --- a/Packages/Models/Sources/Models/OauthToken.swift +++ b/Packages/Models/Sources/Models/OauthToken.swift @@ -5,4 +5,11 @@ public struct OauthToken: Codable, Hashable, Sendable { public let tokenType: String public let scope: String public let createdAt: Double + + public init(accessToken: String, tokenType: String, scope: String, createdAt: Double) { + self.accessToken = accessToken + self.tokenType = tokenType + self.scope = scope + self.createdAt = createdAt + } }