2022-06-24 22:48:57 +00:00
|
|
|
import XCTest
|
|
|
|
|
|
|
|
final class URLParserTests: XCTestCase {
|
2022-11-11 11:27:37 +00:00
|
|
|
private static let urls: [String] = [
|
|
|
|
"https://r.yattee.stream/demo/mp4/1.mp4",
|
|
|
|
"https://r.yattee.stream/demo/mp4/2.mp4",
|
|
|
|
"https://r.yattee.stream/demo/mp4/3.mp4"
|
|
|
|
]
|
2022-06-24 22:48:57 +00:00
|
|
|
private static let videos: [String: String] = [
|
|
|
|
"https://www.youtube.com/watch?v=_E0PWQvW-14&list=WL&index=4&t=155s": "_E0PWQvW-14",
|
|
|
|
"https://youtu.be/IRsc57nK8mg?t=20": "IRsc57nK8mg",
|
2022-08-13 14:16:02 +00:00
|
|
|
"yattee://youtu.be/oCtYBqcN7QE": "oCtYBqcN7QE",
|
2022-08-23 15:07:04 +00:00
|
|
|
"https://www.youtube.com/shorts/TjOh-gfIE2s": "TjOh-gfIE2s",
|
2022-06-24 22:48:57 +00:00
|
|
|
"https://www.youtube-nocookie.com/watch?index=4&v=cE1PSQrWc11&list=WL&t=155s": "cE1PSQrWc11",
|
|
|
|
"https://invidious.snopyta.org/watch?v=XpowfENlJAw": "XpowfENlJAw",
|
|
|
|
"/watch?v=VQ_f5RymW70": "VQ_f5RymW70",
|
|
|
|
"watch?v=IUTGFQpKaPU&t=30s": "IUTGFQpKaPU"
|
|
|
|
]
|
|
|
|
|
|
|
|
private static let channelsByName: [String: String] = [
|
|
|
|
"https://www.youtube.com/c/tennistv": "tennistv",
|
2022-08-21 22:37:29 +00:00
|
|
|
"https://www.youtube.com/achannel": "achannel",
|
2022-06-24 22:48:57 +00:00
|
|
|
"youtube.com/c/MKBHD": "MKBHD",
|
|
|
|
"c/ABCDE": "ABCDE"
|
|
|
|
]
|
|
|
|
|
2022-06-29 23:31:51 +00:00
|
|
|
private static let users: [String: String] = [
|
|
|
|
"https://m.youtube.com/user/ARD": "ARD",
|
|
|
|
"m.youtube.com/user/ARD": "ARD"
|
|
|
|
]
|
|
|
|
|
2022-06-24 22:48:57 +00:00
|
|
|
private static let channelsByID: [String: String] = [
|
|
|
|
"https://piped.kavin.rocks/channel/UCbcxFkd6B9xUU54InHv4Tig": "UCbcxFkd6B9xUU54InHv4Tig",
|
|
|
|
"youtube.com/channel/UCbcxFkd6B9xUU54InHv4Tig": "UCbcxFkd6B9xUU54InHv4Tig",
|
|
|
|
"channel/ABCDE": "ABCDE"
|
|
|
|
]
|
|
|
|
|
|
|
|
private static let playlists: [String: String] = [
|
|
|
|
"https://www.youtube.com/playlist?list=PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU": "PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU",
|
|
|
|
"https://www.youtube.com/watch?v=playlist&list=PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU": "PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU",
|
|
|
|
"youtube.com/watch?v=playlist&list=PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU": "PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU",
|
2023-09-23 19:22:43 +00:00
|
|
|
"https://www.youtube.com/watch?v=ZyhrYis509A&list=PL7DA3D097D6FDBC02": "PL7DA3D097D6FDBC02",
|
2022-06-24 22:48:57 +00:00
|
|
|
"/watch?v=playlist&list=PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU": "PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU",
|
|
|
|
"watch?v=playlist&list=PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU": "PLDIoUOhQQPlXr63I_vwF9GD8sAKh77dWU",
|
|
|
|
"playlist?list=ABCDE": "ABCDE"
|
|
|
|
]
|
|
|
|
|
|
|
|
private static let searches: [String: String] = [
|
|
|
|
"https://www.youtube.com/results?search_query=my+query+text": "my query text",
|
|
|
|
"https://piped.kavin.rocks/results?search_query=query+text": "query text",
|
|
|
|
"https://www.youtube.com/results?search_query=my+query+text&sp=EgIQAg%253D%253D": "my query text",
|
|
|
|
"https://www.youtube.com/results?search_query=encoded+%22query+text%22+@%23%252": "encoded \"query text\" @#%2",
|
|
|
|
"https://www.youtube.com/results?search_query=a%2Bb%3Dc": "a b=c",
|
|
|
|
"www.youtube.com/results?search_query=my+query+text&sp=EgIQAg%253D%253D": "my query text",
|
|
|
|
"/results?search_query=a+b%3Dcde": "a b=cde",
|
|
|
|
"search?search_query=a+b%3Dcde": "a b=cde"
|
|
|
|
]
|
|
|
|
|
2022-11-11 11:27:37 +00:00
|
|
|
func testUrlsParsing() throws {
|
2024-05-16 16:28:32 +00:00
|
|
|
for urlString in Self.urls {
|
2022-11-11 11:27:37 +00:00
|
|
|
let url = URL(string: urlString)!
|
|
|
|
let parser = URLParser(url: url)
|
|
|
|
XCTAssertEqual(parser.destination, .fileURL)
|
|
|
|
XCTAssertEqual(parser.fileURL, url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 22:48:57 +00:00
|
|
|
func testVideosParsing() throws {
|
2024-05-16 16:28:32 +00:00
|
|
|
for (url, id) in Self.videos {
|
2022-06-24 22:48:57 +00:00
|
|
|
let parser = URLParser(url: URL(string: url)!)
|
|
|
|
XCTAssertEqual(parser.destination, .video)
|
|
|
|
XCTAssertEqual(parser.videoID, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testChannelsByNameParsing() throws {
|
2024-05-16 16:28:32 +00:00
|
|
|
for (url, name) in Self.channelsByName {
|
2022-06-24 22:48:57 +00:00
|
|
|
let parser = URLParser(url: URL(string: url)!)
|
|
|
|
XCTAssertEqual(parser.destination, .channel)
|
|
|
|
XCTAssertEqual(parser.channelName, name)
|
2022-06-26 12:32:15 +00:00
|
|
|
XCTAssertNil(parser.channelID)
|
2022-06-24 22:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testChannelsByIdParsing() throws {
|
2024-05-16 16:28:32 +00:00
|
|
|
for (url, id) in Self.channelsByID {
|
2022-06-24 22:48:57 +00:00
|
|
|
let parser = URLParser(url: URL(string: url)!)
|
|
|
|
XCTAssertEqual(parser.destination, .channel)
|
|
|
|
XCTAssertEqual(parser.channelID, id)
|
2022-06-26 12:32:15 +00:00
|
|
|
XCTAssertNil(parser.channelName)
|
2022-06-24 22:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 23:31:51 +00:00
|
|
|
func testUsersParsing() throws {
|
2024-05-16 16:28:32 +00:00
|
|
|
for (url, user) in Self.users {
|
2022-06-29 23:31:51 +00:00
|
|
|
let parser = URLParser(url: URL(string: url)!)
|
|
|
|
XCTAssertEqual(parser.destination, .channel)
|
|
|
|
XCTAssertNil(parser.channelID)
|
|
|
|
XCTAssertNil(parser.channelName)
|
|
|
|
XCTAssertEqual(parser.username, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 22:48:57 +00:00
|
|
|
func testPlaylistsParsing() throws {
|
2024-05-16 16:28:32 +00:00
|
|
|
for (url, id) in Self.playlists {
|
2022-06-24 22:48:57 +00:00
|
|
|
let parser = URLParser(url: URL(string: url)!)
|
|
|
|
XCTAssertEqual(parser.destination, .playlist)
|
|
|
|
XCTAssertEqual(parser.playlistID, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSearchesParsing() throws {
|
2024-05-16 16:28:32 +00:00
|
|
|
for (url, query) in Self.searches {
|
2022-06-24 22:48:57 +00:00
|
|
|
let parser = URLParser(url: URL(string: url)!)
|
|
|
|
XCTAssertEqual(parser.destination, .search)
|
|
|
|
XCTAssertEqual(parser.searchQuery, query)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testTimeParsing() throws {
|
|
|
|
let samples: [String: Int?] = [
|
|
|
|
"https://www.youtube.com/watch?v=_E0PWQvW-14&list=WL&index=4&t=155s": 155,
|
|
|
|
"https://youtu.be/IRsc57nK8mg?t=20m10s": 1210,
|
|
|
|
"https://youtu.be/IRsc57nK8mg?t=3x4z": nil,
|
|
|
|
"https://www.youtube-nocookie.com/watch?index=4&v=cE1PSQrWc11&list=WL&t=2H3m5s": 7385,
|
|
|
|
"https://youtu.be/VQ_f5RymW70?t=378": 378,
|
|
|
|
"watch?v=IUTGFQpKaPU&t=30s": 30
|
|
|
|
]
|
|
|
|
|
2024-05-16 16:28:32 +00:00
|
|
|
for (url, time) in samples {
|
2022-06-24 22:48:57 +00:00
|
|
|
XCTAssertEqual(
|
|
|
|
URLParser(url: URL(string: url)!).time,
|
|
|
|
time
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|