Add tests for Router

This commit is contained in:
Thomas Ricouard 2024-01-01 09:48:53 +01:00
parent 7222d530dd
commit b6317d7324
3 changed files with 61 additions and 0 deletions

View file

@ -33,5 +33,9 @@ let package = Package(
.enableExperimentalFeature("StrictConcurrency"),
]
),
.testTarget(
name: "EnvTests",
dependencies: ["Env"]
),
]
)

View file

@ -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)
}
}

View file

@ -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
}
}