Something went wrong. Try again.
an open source Navidrome client written in Swift dub.sh/getflo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219//// SubsonicResponseTests.swift// floTests//
import XCTest
@testable import flo
// MARK: - Concrete response type for testing
private struct TestData: SubsonicResponseData { static var key: String { "testData" }
let message: String let count: Int}
final class SubsonicResponseTests: XCTestCase {
// MARK: - Generic response decoding
func testDecode_responseWithData() throws { let json = """ { "status": "ok", "version": "1.16.1", "type": "flo", "serverVersion": "0.52.0", "openSubsonic": true, "testData": { "message": "hello", "count": 42 } } """
let response = try JSONDecoder().decode( SubsonicResponse<TestData>.self, from: Data(json.utf8))
XCTAssertEqual(response.status, "ok") XCTAssertEqual(response.version, "1.16.1") XCTAssertEqual(response.type, "flo") XCTAssertEqual(response.serverVersion, "0.52.0") XCTAssertTrue(response.openSubsonic) XCTAssertNotNil(response.data) XCTAssertEqual(response.data?.message, "hello") XCTAssertEqual(response.data?.count, 42) }
func testDecode_responseWithoutData() throws { let json = """ { "status": "failed", "version": "1.16.1", "type": "flo", "serverVersion": "0.52.0", "openSubsonic": false, "error": { "code": 40, "message": "Wrong username or password" } } """
// When the data key is absent, decodeIfPresent should return nil let response = try JSONDecoder().decode( SubsonicResponse<TestData>.self, from: Data(json.utf8))
XCTAssertEqual(response.status, "failed") XCTAssertNil(response.data) }
func testDecode_BasicSubsonicResponse() throws { let json = """ { "status": "ok", "version": "1.16.1", "type": "flo", "serverVersion": "0.52.0", "openSubsonic": true } """
let response = try JSONDecoder().decode( BasicSubsonicResponse.self, from: Data(json.utf8))
XCTAssertEqual(response.status, "ok") XCTAssertTrue(response.openSubsonic) }
// MARK: - Starred2Response
func testDecode_starred2Response_withSongs() throws { let json = """ { "subsonic-response": { "starred2": { "song": [ { "id": "song1", "title": "Starred Song", "artist": "Artist", "albumId": "album1", "album": "Album", "track": 1, "discNumber": 1, "bitRate": 320, "samplingRate": 44100, "suffix": "mp3", "duration": 200 } ] } } } """
let response = try JSONDecoder().decode(Starred2Response.self, from: Data(json.utf8))
XCTAssertEqual(response.songs.count, 1) XCTAssertEqual(response.songs[0].title, "Starred Song") XCTAssertEqual(response.songs[0].artist, "Artist") }
func testDecode_starred2Response_emptySongs() throws { let json = """ { "subsonic-response": { "starred2": { "song": [] } } } """
let response = try JSONDecoder().decode(Starred2Response.self, from: Data(json.utf8))
XCTAssertEqual(response.songs.count, 0) }
func testDecode_starred2Response_noSongs() throws { let json = """ { "subsonic-response": { "starred2": null } } """
let response = try JSONDecoder().decode(Starred2Response.self, from: Data(json.utf8))
XCTAssertEqual(response.songs.count, 0) }
// MARK: - SubsonicSong toSong conversion
func testSubsonicSong_toSong() { let subsonicSong = SubsonicSong( id: "s1", title: "Test Song", artist: "Artist", albumId: "a1", album: "Album", track: 3, discNumber: 1, bitRate: 256, samplingRate: 48000, suffix: "flac", duration: 300, explicitStatus: "explicit" )
let song = subsonicSong.toSong()
XCTAssertEqual(song.id, "s1") XCTAssertEqual(song.title, "Test Song") XCTAssertEqual(song.artist, "Artist") XCTAssertEqual(song.albumId, "a1") XCTAssertEqual(song.albumName, "Album") XCTAssertEqual(song.trackNumber, 3) XCTAssertEqual(song.discNumber, 1) XCTAssertEqual(song.bitRate, 256) XCTAssertEqual(song.sampleRate, 48000) XCTAssertEqual(song.suffix, "flac") XCTAssertEqual(song.duration, 300.0) XCTAssertEqual(song.explicitStatus, .explicit) }
func testSubsonicSong_toSong_withNilFields() { let subsonicSong = SubsonicSong( id: "s1", title: "Minimal", artist: nil, albumId: nil, album: nil, track: nil, discNumber: nil, bitRate: nil, samplingRate: nil, suffix: nil, duration: nil, explicitStatus: nil )
let song = subsonicSong.toSong()
XCTAssertEqual(song.title, "Minimal") XCTAssertEqual(song.artist, "") XCTAssertEqual(song.albumId, "") XCTAssertEqual(song.albumName, "") XCTAssertEqual(song.trackNumber, 0) XCTAssertEqual(song.bitRate, 0) XCTAssertEqual(song.suffix, "") XCTAssertEqual(song.duration, 0.0) XCTAssertEqual(song.explicitStatus, .unknown) }}