importFoundationimport XCTest@testable importCottonUI/// RULE (cinemas-airplay, 18 Nov 2026): what a film does on an AirPlay route is the core's (`shell.player` `route`);/// the player asks as the route changes and does what each answer says, for one play.finalclassFilmRoutingTests: XCTestCase {privatelet lan = URL(string: "http://192.168.1.23:52011/0123456789abcdef0123456789abcdef/master.m3u8")!privatefunc answer(_ json: String) throws -> PlayerRoute {try JSONDecoder().decode(PlayerRoute.self, from: Data(json.utf8)) }/// RULE: the core's route answer is read as it says it, on the player's answer.func testTheRouteIsReadAsTheCoreSaysIt() throws {let tv = try answer(#"{"to":"receiver","stream":"\#(lan.absoluteString)","check_after_s":5}"#) XCTAssertEqual(tv, PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 5, said: nil))let phone = try answer(#"{"to":"phone","said":"GlassTideis playing through this phone."}"#) XCTAssertEqual(phone.to, .phone) XCTAssertEqual(phone.said, "Glass Tide is playing through this phone.") XCTAssertEqual(try answer(#"{"to":"receiver"}"#), PlayerRoute(to: .receiver, stream: nil, checkAfterS: nil, said: nil)) }/// RULE: on a TV the film plays from the Wi-Fi address the core gives, checked after the receiver's time; reached,/// it plays on; back on the phone, the phone's own address again and the core told.func testATvPlaysTheFilmFromItsWifiAddress() {var routing = FilmRouting() XCTAssertNil(routing.checking(), "nothing to check on the phone") XCTAssertEqual(routing.began(), "external") XCTAssertNil(routing.began(), "asked once") XCTAssertEqual(routing.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 5, said: nil)), [.play(lan), .check(after: 5)]) XCTAssertEqual(routing.state, .receiver(onWiFi: true)) XCTAssertEqual(routing.checking(), "check") XCTAssertEqual(routing.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 10, said: nil)), [.check(after: 10)],"seen playing: it plays on, checked again") XCTAssertEqual(routing.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: nil, said: nil)), [])let back = routing.cameBack() XCTAssertEqual(back.ask, "phone") XCTAssertEqual(back.acts, [.playHere]) XCTAssertEqual(routing.state, .phone) XCTAssertEqual(routing.took(PlayerRoute(to: .phone, stream: nil, checkAfterS: nil, said: nil)), [], "the core's answer to the way back changes nothing") }/// RULE: a receiver that never reached the film, a failure on the TV, or no Wi-Fi: external playback off, the film/// from what the phone plays it from, and the core's words said once a play; never an error. Back off AirPlay, the/// TV may be handed the film again.func testAFilmTheTvCannotPlayPlaysThroughThePhoneSaidOnce() {let through = PlayerRoute(to: .phone, stream: nil, checkAfterS: nil, said: "Glass Tide is playing through this phone.")var routing = FilmRouting() _ = routing.began() _ = routing.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 5, said: nil)) XCTAssertEqual(routing.took(through), [.allowTV(false), .playHere, .say("Glass Tide is playing through this phone.")]) XCTAssertEqual(routing.state, .throughPhone) XCTAssertNil(routing.began(), "through the phone, the player's own flag is not asked about") XCTAssertNil(routing.failed(), "a failure through the phone is the player's own") XCTAssertNil(routing.checking()) XCTAssertNil(routing.opening(onTV: true), "the next episode plays through the phone too")let back = routing.cameBack() XCTAssertNil(back.ask) XCTAssertEqual(back.acts, [.allowTV(true)]) XCTAssertEqual(routing.began(), "external") XCTAssertEqual(routing.took(through), [.allowTV(false), .playHere], "said once a play")var noWifi = FilmRouting() _ = noWifi.began() XCTAssertEqual(noWifi.failed(), "failed", "the TV failed before the core answered") XCTAssertEqual(noWifi.took(through), [.allowTV(false), .playHere, .say("Glass Tide is playing through this phone.")]) XCTAssertEqual(noWifi.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 5, said: nil)), [], "the external answer after the failure's: through the phone it stays") XCTAssertEqual(noWifi.state, .throughPhone) }/// RULE (airplay-refused): a film the TV plays as it is (the object's address, a film kept here) is left to it but/// checked like any other; failing there, the core is asked; back off AirPlay the core is told, and nothing reloads.func testAFilmTheTvPlaysAsItIsIsLeftToItAndChecked() {var routing = FilmRouting() _ = routing.began() XCTAssertEqual(routing.took(PlayerRoute(to: .receiver, stream: nil, checkAfterS: 8, said: nil)), [.check(after: 8)]) XCTAssertEqual(routing.state, .receiver(onWiFi: false)) XCTAssertEqual(routing.checking(), "check", "a kept film is checked too") XCTAssertEqual(routing.took(PlayerRoute(to: .receiver, stream: nil, checkAfterS: 10, said: nil)), [.check(after: 10)], "and again while it plays") XCTAssertEqual(routing.failed(), "failed")let back = routing.cameBack() XCTAssertEqual(back.ask, "phone") XCTAssertEqual(back.acts, []) }/// RULE: back on the phone before the core answered, the core is told and its late answer holds nothing; the next/// episode on a TV is asked about afresh.func testALateAnswerAndTheNextEpisode() {var routing = FilmRouting() _ = routing.began() XCTAssertEqual(routing.cameBack().ask, "phone") XCTAssertEqual(routing.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 5, said: nil)), []) XCTAssertNil(routing.cameBack().ask, "on the phone already: nothing to tell") XCTAssertNil(routing.opening(onTV: false)) _ = routing.began() _ = routing.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 5, said: nil)) XCTAssertEqual(routing.opening(onTV: true), "external", "the next episode's own door") XCTAssertEqual(routing.state, .asking) }/// RULE (airplay-refused, 18 Nov 2026): a film that never plays on the TV, or stops there while the phone says it/// plays, is the core's `phone` answer to a check: external playback off, the phone plays it, said once; no more checks.func testAFilmNeverSeenPlayingOnTheTvFallsBackThroughThePhone() {var routing = FilmRouting() _ = routing.began() XCTAssertEqual(routing.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 8, said: nil)), [.play(lan), .check(after: 8)]) XCTAssertEqual(routing.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 10, said: nil)), [.check(after: 10)], "seen playing") XCTAssertEqual(routing.checking(), "check")let stalled = PlayerRoute(to: .phone, stream: nil, checkAfterS: nil, said: "Glass Tide is playing through this phone.") XCTAssertEqual(routing.took(stalled), [.allowTV(false), .playHere, .say("Glass Tide is playing through this phone.")]) XCTAssertNil(routing.checking(), "through the phone: nothing more to check") XCTAssertEqual(routing.took(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 10, said: nil)), [], "a late check's answer holds nothing") }/// RULE (airplay-refused, 18 Nov 2026): already on AirPlay when a film opens, the core is asked before anything loads,/// and the first item is the Wi-Fi address (never the phone's 127.0.0.1); a kept film loads as it is, checked; through/// the phone, nothing is handed to the TV and nothing plays twice.func testAlreadyOnATvTheFirstItemIsTheWifiAddress() {var sealed = FilmRouting() XCTAssertNil(sealed.opening(onTV: false), "on the phone: nothing to ask") XCTAssertEqual(sealed.opening(onTV: true), "external")let first = sealed.first(PlayerRoute(to: .receiver, stream: lan.absoluteString, checkAfterS: 8, said: nil)) XCTAssertEqual(first.from, lan) XCTAssertEqual(first.acts, [.check(after: 8)]) XCTAssertEqual(sealed.state, .receiver(onWiFi: true)) XCTAssertNil(sealed.began(), "Apple's player reaching the TV with it asks nothing more")var kept = FilmRouting() _ = kept.opening(onTV: true)let own = kept.first(PlayerRoute(to: .receiver, stream: nil, checkAfterS: 8, said: nil)) XCTAssertNil(own.from, "what the phone plays it from") XCTAssertEqual(own.acts, [.check(after: 8)])var noWifi = FilmRouting() _ = noWifi.opening(onTV: true)let through = noWifi.first(PlayerRoute(to: .phone, stream: nil, checkAfterS: nil, said: "Glass Tide is playing through this phone.")) XCTAssertNil(through.from) XCTAssertEqual(through.acts, [.allowTV(false), .say("Glass Tide is playing through this phone.")]) XCTAssertEqual(noWifi.state, .throughPhone) }}