Files
swift-musicbrainz/Tests/MusicBrainzTests/MusicBrainzTests.swift
2026-03-21 17:25:07 +01:00

55 lines
1.4 KiB
Swift

import Foundation
import Testing
@testable import MusicBrainz
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
@Test func testArtistDecoding() throws {
let json = """
{
"id": "f27ec8db-af05-4f36-916e-3d57f91ecf5e",
"name": "Michael Jackson",
"sort-name": "Jackson, Michael",
"type": "Person",
"country": "US",
"disambiguation": "King of Pop"
}
""".data(using: .utf8)!
let artist = try JSONDecoder().decode(Artist.self, from: json)
#expect(artist.id == "f27ec8db-af05-4f36-916e-3d57f91ecf5e")
#expect(artist.name == "Michael Jackson")
#expect(artist.sortName == "Jackson, Michael")
#expect(artist.type == "Person")
#expect(artist.country == "US")
#expect(artist.disambiguation == "“King of Pop”")
}
@Test func testCoordinatorRateLimiting() async throws {
let coordinator = MusicBrainzCoordinator()
let request = URLRequest(url: URL(string: "https://example.com")!)
let clock = ContinuousClock()
let start = clock.now
// Perform 3 requests concurrently
try await withThrowingTaskGroup(of: Void.self) { group in
for _ in 0..<3 {
group.addTask {
_ = try await coordinator.perform(request)
}
}
}
let duration = start.duration(to: clock.now)
// 3 requests should take at least 2 seconds (0s, 1s, 2s)
// We check for > 1.9s to account for small timing variations
#expect(duration >= .seconds(2))
print("DEBUG: Total duration for 3 requests: \(duration)")
}