//
//  LiveModelsTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// 直播模型测试。
final class LiveModelsTests: XCTestCase {
    func testLiveItemDecodesLegacyAndCorrectManualPushKeys() throws {
        let legacyJSON = """
        {"id":"3","title":"直播","cover_img":"https:\\/\\/cdn\\/c.jpg","push_url":"rtmp:\\/\\/push","status":2,"status_label":"直播中","manaul_push_mode":2,"manaul_push_state":"1","views_count":"9"}
        """.data(using: .utf8)!
        let legacy = try JSONDecoder().decode(LiveItem.self, from: legacyJSON)
        XCTAssertEqual(legacy.id, 3)
        XCTAssertEqual(legacy.coverImg, "https://cdn/c.jpg")
        XCTAssertEqual(legacy.pushUrl, "rtmp://push")
        XCTAssertEqual(legacy.manualPushMode, 2)
        XCTAssertEqual(legacy.manualPushState, 1)
        XCTAssertEqual(legacy.viewsCount, 9)
        XCTAssertEqual(legacy.liveStatus, .living)

        let correctJSON = """
        {"id":4,"manual_push_mode":1,"manual_push_state":0}
        """.data(using: .utf8)!
        let correct = try JSONDecoder().decode(LiveItem.self, from: correctJSON)
        XCTAssertEqual(correct.manualPushMode, 1)
        XCTAssertEqual(correct.manualPushState, 0)
        XCTAssertEqual(correct.displayTitle, "暂无标题")
    }

    func testAlbumFolderDecodesItemsAndDefaults() throws {
        let json = """
        {"id":6,"album_id":5,"name":"","creator":"A","items":[{"id":"7","url":"https:\\/\\/cdn\\/a.jpg","type":"2","size":"1024","cover_img":""}]}
        """.data(using: .utf8)!

        let folder = try JSONDecoder().decode(LiveAlbumFolder.self, from: json)

        XCTAssertEqual(folder.id, 6)
        XCTAssertEqual(folder.displayName, "暂无标题")
        XCTAssertEqual(folder.items.first?.id, 7)
        XCTAssertEqual(folder.items.first?.url, "https://cdn/a.jpg")
        XCTAssertEqual(folder.items.first?.size, 1024)
    }
}
