importCottonKitimportCottonValuesimportFoundationimport XCTest@testable importCottonUI/// A podcast episode plays in the app's one audio player (`PodcastPlayback`),/// never in the viewer; the lock screen's controls reach one player at a time/// (`NowPlayingClaim`); a kept episode's file is named by its address.finalclassPodcastPlaybackTests: XCTestCase {privatefunc episode(_ key: String?, id: String = "episode/1") -> ViewerItem {ViewerItem(id: id, kind: .video, key: key, title: "Synthetic Episode", record: ViewerRecord(table: "episode", id: "e1")) }/// RULE: An item whose player column holds a web address is an episode for the one audio player; an object key is still the viewer's, and only a film's or a film episode's record plays as a film.func testAWebAddressPlaysInTheAudioPlayer() { XCTAssertEqual(FilmPlayer.of(episode("https://example.test/feed/ep1.mp3")), .audio) XCTAssertEqual(FilmPlayer.of(episode(" HTTP://example.test/ep1.m4a")), .audio) XCTAssertEqual(FilmPlayer.of(episode("db/films/f1/master.m3u8")), .viewer) XCTAssertEqual(FilmPlayer.of(episode("db/videos/clip.mp4")), .viewer) XCTAssertEqual(FilmPlayer.of(ViewerItem(id: "p", kind: .photo, key: "https://example.test/a.jpg")), .viewer) }/// RULE: A tap on an episode hands it to the player and the viewer stays shut; a set the viewer walks never holds one. @MainActorfunc testTheViewerNeverHoldsAnEpisode() {let presenter = ViewerPresenter()var played: [String] = [] presenter.streams = { played.append($0.id) }let audio = episode("https://example.test/ep1.mp3", id: "episode/1")let photo = ViewerItem(id: "photo/1", kind: .photo, key: "db/photos/a.jpg") presenter.open(ViewerSet(items: [audio, photo], start: 0)) XCTAssertEqual(played, ["episode/1"]) XCTAssertNil(presenter.current) presenter.open(ViewerSet(items: [audio, photo], start: 1)) XCTAssertEqual(presenter.current?.items.map(\.id), ["photo/1"]) }/// RULE: An episode's record opens as audio by its definition alone (`slip.player` is its audio address), carrying its title and its show's artwork.func testAnEpisodesRecordOpensAsAudio() throws {let fixture = try XCTUnwrap(tryDefinitionCatalogue.fixture(in: CottonUIResources.bundle))let definition = try XCTUnwrap(fixture.tables.first { $0.id == "episode" })let record = TableRecord(id: "e1", values: ["title": .text("Synthetic Episode"), "audio_url": .text("https://example.test/ep1.mp3"),"artwork": .text("db/photos/show.jpg"),TableRecord.saidKey: .object(["slip": .object(["title": .text("Synthetic Episode")])])])let item = try XCTUnwrap(ViewerItem(record: record, definition: definition)) XCTAssertEqual(FilmPlayer.of(item), .audio) XCTAssertEqual(item.title, "Synthetic Episode") XCTAssertEqual(item.poster, "db/photos/show.jpg") XCTAssertEqual(item.record, ViewerRecord(table: "episode", id: "e1")) }/// RULE: The lock screen's controls are the last player's to show something playing, and the one that held them is paused as it starts; a player paused elsewhere takes nothing. @MainActorfunc testOnePlayerHoldsTheControls() {let music = ObjectIdentifier(NSObject.self)let podcast = ObjectIdentifier(NSString.self)var paused: [String] = []NowPlayingClaim.pauses[music] = { paused.append("music") }NowPlayingClaim.pauses[podcast] = { paused.append("podcast") }defer {NowPlayingClaim.pauses[music] = nilNowPlayingClaim.pauses[podcast] = nilNowPlayingClaim.holder = nil }NowPlayingClaim.take(music, playing: true) XCTAssertEqual(paused, [])NowPlayingClaim.take(podcast, playing: true) XCTAssertEqual(NowPlayingClaim.holder, podcast) XCTAssertEqual(paused, ["music"])NowPlayingClaim.take(podcast, playing: false) XCTAssertEqual(paused, ["music"], "a pause of its own pauses nobody") }/// RULE: A kept episode's file is named by its address — the same address the same file, and the address's own extension (mp3 when it names none).func testAKeptEpisodesFileIsNamedByItsAddress() {let one = EpisodeFiles.name(for: "https://example.test/feed/ep1.mp3?updated=1") XCTAssertTrue(one.hasSuffix(".mp3")) XCTAssertEqual(one, EpisodeFiles.name(for: "https://example.test/feed/ep1.mp3?updated=1")) XCTAssertNotEqual(one, EpisodeFiles.name(for: "https://example.test/feed/ep2.mp3")) XCTAssertTrue(EpisodeFiles.name(for: "https://example.test/feed/ep1.m4a").hasSuffix(".m4a")) XCTAssertTrue(EpisodeFiles.name(for: "https://example.test/listen?id=4").hasSuffix(".mp3")) }/// RULE: Plus on a deck whose definition names an act asks its words of the core (`shell.input`) under the create's label, and commits them with the writes the core answered; the object writes and nothing is made on the phone; a refusal is said on the deck in the core's words. @MainActorfunc testPlusRunsTheActWithTheWordsTyped() asyncthrows {let refuse = Refusing()let core = InputCore { method, request inswitch (method, request["op"]?.string) {case ("shell.input", "ask"):return .success(InputCore.json(#"{"editor":"line","words":"","hint":"Follow a podcast","keeps_words":false,"writes":{"kind":"act","table":"podcast","name":"subscribe_podcast","argument":"query"}}"#))case ("shell.input", "commit"):return .success(refuse.on ? .object(["refusal": "no podcast with a feed matches zz"]) : .object([:]))default:return .failure(CottonCallError(code: "missing", message: "Not here")) } }let editor = DeckEditor() editor.ask(table: "podcast", core: core)tryawait eventually { editor.writing != nil } XCTAssertEqual(core.asked("shell.input", op: "ask").first?["kind"], "act") XCTAssertEqual(editor.writing?.hint, "Follow a podcast") editor.commit(" philosophize this ") XCTAssertNil(editor.writing)tryawait eventually { !core.asked("shell.input", op: "commit").isEmpty }let sent = try XCTUnwrap(core.asked("shell.input", op: "commit").first) XCTAssertEqual(sent["writes"]?["name"], "subscribe_podcast") XCTAssertEqual(sent["words"], " philosophize this ") XCTAssertTrue(core.asked.allSatisfy { $0.method == "shell.input" }, "nothing is made on the phone") refuse.on = true editor.ask(table: "podcast", core: core)tryawait eventually { editor.writing != nil } editor.commit("zz")tryawait eventually { editor.refusal != nil } XCTAssertEqual(editor.refusal?.sentence, "no podcast with a feed matches zz") }}/// Whether the scripted core refuses the next act.privatefinalclassRefusing: @unchecked Sendable { var on = false }