Started off well

This commit is contained in:
cdricms
2026-03-21 17:25:07 +01:00
commit ada1c12f57
9 changed files with 1164 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
import Foundation
import Testing
@testable import MusicBrainz
@Test func testAreaSearchDecoding() throws {
let json = """
{
"created": "2024-05-20T12:00:00.000Z",
"count": 1,
"offset": 0,
"areas": [
{
"id": "f03ed396-3f4d-4ef4-81b0-7c03c46961e2",
"type": "City",
"score": 100,
"name": "London",
"sort-name": "London",
"life-span": { "ended": false }
}
]
}
""".data(using: .utf8)!
let response = try JSONDecoder().decode(SearchResponse<Area>.self, from: json)
#expect(response.count == 1)
#expect(response.entities.count == 1)
#expect(response.entities[0].name == "London")
#expect(response.entities[0].score == 100)
}
@Test func testReleaseGroupSearchDecoding() throws {
let json = """
{
"created": "2024-05-20T12:00:00.000Z",
"count": 1,
"offset": 0,
"release-groups": [
{
"id": "1b022e01-4da6-387b-8658-8678046e4ccc",
"score": 100,
"primary-type": "Album",
"title": "Nevermind",
"artist-credit": [{ "name": "Nirvana" }]
}
]
}
""".data(using: .utf8)!
let response = try JSONDecoder().decode(SearchResponse<ReleaseGroup>.self, from: json)
#expect(response.count == 1)
#expect(response.entities.count == 1)
#expect(response.entities[0].title == "Nevermind")
#expect(response.entities[0].primaryType == "Album")
#expect(response.entities[0].artistCredit?[0].name == "Nirvana")
}
@Test func testLabelSearchDecoding() throws {
let json = """
{
"created": "2024-05-20T12:00:00.000Z",
"count": 1,
"offset": 0,
"labels": [
{
"id": "79239441-bfd5-4981-a70c-55c3f15c1287",
"type": "Production",
"score": 100,
"name": "Ninja Tune",
"label-code": 12885,
"country": "GB"
}
]
}
""".data(using: .utf8)!
let response = try JSONDecoder().decode(SearchResponse<Label>.self, from: json)
#expect(response.count == 1)
#expect(response.entities.count == 1)
#expect(response.entities[0].name == "Ninja Tune")
#expect(response.entities[0].labelCode == 12885)
}
@Test func testURLSearchDecoding() throws {
let json = """
{
"created": "2024-05-20T12:00:00.000Z",
"count": 1,
"offset": 0,
"urls": [
{
"id": "b663423b-9b54-4067-9674-fffaecf68851",
"resource": "http://www.madonna.com/",
"score": 100
}
]
}
""".data(using: .utf8)!
let response = try JSONDecoder().decode(SearchResponse<URLReference>.self, from: json)
#expect(response.count == 1)
#expect(response.entities.count == 1)
#expect(response.entities[0].resource == "http://www.madonna.com/")
}

View File

@@ -0,0 +1,54 @@
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)")
}

View File

@@ -0,0 +1,36 @@
import Foundation
import Testing
@testable import MusicBrainz
@Test func testArtistQueryBuilding() throws {
let action: MusicBrainzSearchAction<Artist> = .artist(name: "Michael Jackson", gender: .male, country: .us)
#expect(action.query.contains("artist:\"Michael Jackson\""))
#expect(action.query.contains("gender:\"male\""))
#expect(action.query.contains("country:\"US\""))
}
@Test func testReleaseQueryBuilding() throws {
let action: MusicBrainzSearchAction<Release> = .release(title: "Thriller", artist: "Michael Jackson", barcode: "07464381122")
#expect(action.query.contains("release:\"Thriller\""))
#expect(action.query.contains("artist:\"Michael Jackson\""))
#expect(action.query.contains("barcode:\"07464381122\""))
}
@Test func testRecordingQueryBuilding() throws {
let action: MusicBrainzSearchAction<Recording> = .recording(title: "Billie Jean", isrc: "USSM18200385", video: true)
#expect(action.query.contains("recording:\"Billie Jean\""))
#expect(action.query.contains("isrc:\"USSM18200385\""))
#expect(action.query.contains("video:\"true\""))
}
@Test func testRawQueryBuilding() throws {
let action: MusicBrainzSearchAction<Artist> = .artist(name: "Prince", raw: "type:person")
#expect(action.query.contains("artist:\"Prince\""))
#expect(action.query.contains("type:person"))
}
@Test func testQueryEscaping() throws {
let action: MusicBrainzSearchAction<Artist> = .artist(name: "Artist \"Quote\" Name")
#expect(action.query == "artist:\"Artist \\\"Quote\\\" Name\"")
}

View File

@@ -0,0 +1,30 @@
import Foundation
import Testing
@testable import MusicBrainz
@Test func testArtistSearchDecoding() throws {
let json = """
{
"created": "2026-03-20T22:42:03.521Z",
"count": 1,
"offset": 0,
"artists": [
{
"id": "f27ec8db-af05-4f36-916e-3d57f91ecf5e",
"type": "Person",
"name": "Michael Jackson",
"sort-name": "Jackson, Michael",
"country": "US",
"disambiguation": "King of Pop"
}
]
}
""".data(using: .utf8)!
let response = try JSONDecoder().decode(SearchResponse<Artist>.self, from: json)
#expect(response.count == 1)
#expect(response.entities.count == 1)
#expect(response.entities[0].name == "Michael Jackson")
}