Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import EffemModelsimport Foundationimport PodcastIndexKitimport Testing@testable import EffemAppView
@Suite("Episode transcripts")struct EpisodeTranscriptTests { /// Decodes one episode the way the routes do, from what Podcast Index sends. private func episode(_ fields: String) throws -> Episode { let json = """ {"status":"true","id":"16795090","episode":{"id":16795090,"title":"Episode","feedId":75075,\(fields)},"description":"Found matching episodes."} """ return try #require(try PodcastIndexClient.decode(EpisodeResponse.self, from: Data(json.utf8)).episode) }
@Test("Every transcript the feed lists is passed along, in the feed's order") func listed() throws { let episode = try episode(""" "transcriptUrl":"https://example.com/1.srt", "transcripts":[ {"url":"https://example.com/1.srt","type":"application/srt"}, {"url":"https://example.com/1.vtt","type":"text/vtt","language":"en","rel":"captions"}, {"url":"https://example.com/1.json","type":"application/json"} ] """)
let result = try #require(PodcastIndexClient.episodeResult(episode, feedID: nil, social: nil)) #expect(result.transcripts == [ EpisodeTranscript(url: "https://example.com/1.srt", type: "application/srt"), EpisodeTranscript(url: "https://example.com/1.vtt", type: "text/vtt", language: "en", rel: "captions"), EpisodeTranscript(url: "https://example.com/1.json", type: "application/json"), ]) }
@Test("The single transcript address stands in when there is no list") func singleAddress() throws { let withoutList = try episode(#""transcriptUrl":"https://example.com/1.srt""#) #expect(PodcastIndexClient.transcripts(of: withoutList) == [EpisodeTranscript(url: "https://example.com/1.srt")])
let withEmptyList = try episode(#""transcriptUrl":" https://example.com/1.srt ","transcripts":[]"#) #expect(PodcastIndexClient.transcripts(of: withEmptyList) == [EpisodeTranscript(url: "https://example.com/1.srt")]) }
@Test("An episode without a transcript reports none, and the field is left out of the answer") func none() throws { let episode = try episode(#""transcriptUrl":"","transcripts":null"#) let result = try #require(PodcastIndexClient.episodeResult(episode, feedID: nil, social: nil)) #expect(result.transcripts == nil)
let encoder = JSONEncoder() encoder.keyEncodingStrategy = .convertToSnakeCase let json = String(decoding: try encoder.encode(result), as: UTF8.self) #expect(!json.contains("transcripts")) }
@Test("Entries the app could not fetch are dropped: no address, not a web address, or a repeat") func unusable() throws { let episode = try episode(""" "transcripts":[ {"type":"text/vtt"}, {"url":" ","type":"text/vtt"}, {"url":"file:///etc/passwd","type":"text/plain"}, {"url":"javascript:alert(1)","type":"text/html"}, {"url":"https:///no-host.vtt","type":"text/vtt"}, {"url":"HTTPS://example.com/1.vtt","type":" "}, {"url":"HTTPS://example.com/1.vtt","type":"text/vtt"}, {"url":"http://example.com/plain.txt","type":"text/plain"} ] """)
#expect(PodcastIndexClient.transcripts(of: episode) == [ EpisodeTranscript(url: "HTTPS://example.com/1.vtt"), EpisodeTranscript(url: "http://example.com/plain.txt", type: "text/plain"), ]) }}