Files
Tarnax/appstore/tools/CaptureFixture.swift

79 lines
6.3 KiB
Swift

// Used only in an isolated simulator copy by capture.sh. Never linked into the shipping app.
import Foundation
import SwiftUI
@MainActor enum StoreCapture {
static let screen: String = {
let args = ProcessInfo.processInfo.arguments
guard let i = args.firstIndex(of: "--capture"), args.indices.contains(i + 1) else { return "home" }
return args[i + 1]
}()
static var section: String {
switch screen { case "library", "details", "files": return "Library"; case "settings": return "Settings"; default: return "Home" }
}
static var path: [Torrent] { ["details", "files"].contains(screen) ? [torrents[0]] : [] }
static let torrents = [
Torrent(hash: "sample-open-media", name: "Open media collection", state: "downloading", progress: 0.64, size: 2_400_000_000, dlspeed: 4_800_000, upspeed: 680_000, eta: 180, ratio: 0.42, category: "Open media", tags: "Creative Commons"),
Torrent(hash: "sample-audio", name: "Creative Commons audio", state: "downloading", progress: 0.28, size: 680_000_000, dlspeed: 1_600_000, upspeed: 140_000, eta: 306, ratio: 0.18, category: "Audio", tags: "Open licence"),
Torrent(hash: "sample-toolkit", name: "Open source toolkit", state: "uploading", progress: 1, size: 1_800_000_000, dlspeed: 0, upspeed: 320_000, eta: 8_640_000, ratio: 2.16, category: "Software", tags: "Open source"),
Torrent(hash: "sample-archive", name: "Public domain archive", state: "stoppedDL", progress: 0.81, size: 3_200_000_000, dlspeed: 0, upspeed: 0, eta: 8_640_000, ratio: 0.62, category: "Archive", tags: "Public domain"),
Torrent(hash: "sample-course", name: "Open education resources", state: "queuedDL", progress: 0, size: 950_000_000, dlspeed: 0, upspeed: 0, eta: 8_640_000, ratio: 0, category: "Learning", tags: "Open licence")
]
static var history: TransferHistory {
var h = TransferHistory()
let now = Date()
for i in 0...300 {
let download = Int64(6_400_000 + 2_800_000 * sin(Double(i) / 11) + 900_000 * cos(Double(i) / 4))
let upload = Int64(1_100_000 + 480_000 * cos(Double(i) / 15))
h.record(download: download, upload: upload, at: now.addingTimeInterval(Double(i - 300) * 3))
}
return h
}
static let stats: [String: Any] = ["dl_info_speed": 6_400_000, "up_info_speed": 1_140_000, "free_space_on_disk": 842_000_000_000, "dl_info_data": 32_500_000_000, "up_info_data": 48_800_000_000, "connection_status": "connected", "dht_nodes": 284, "alltime_dl": 405_000_000_000, "alltime_ul": 620_000_000_000]
static let preferences: [String: Any] = ["save_path": "/downloads/complete", "temp_path_enabled": true, "temp_path": "/downloads/incomplete", "preallocate_all": false, "listen_port": 6881, "upnp": false, "proxy_type": 0, "dl_limit": 0, "up_limit": 0, "alt_dl_limit": 2_000_000, "scheduler_enabled": false, "dht": true, "pex": true, "lsd": true, "max_ratio": 2.0, "queueing_enabled": true, "max_active_downloads": 4, "rss_processing_enabled": false, "web_ui_port": 8080, "web_ui_csrf_protection_enabled": true, "web_ui_host_header_validation_enabled": true, "async_io_threads": 10]
static let files: [[String: Any]] = [
["index": 0, "name": "Open media collection/Welcome.mp4", "size": 480_000_000, "progress": 1.0, "priority": 7],
["index": 1, "name": "Open media collection/Chapter 01.mp4", "size": 720_000_000, "progress": 0.82, "priority": 6],
["index": 2, "name": "Open media collection/Chapter 02.mp4", "size": 700_000_000, "progress": 0.48, "priority": 1],
["index": 3, "name": "Open media collection/Chapter 03.mp4", "size": 499_980_000, "progress": 0.12, "priority": 1],
["index": 4, "name": "Open media collection/LICENCE.txt", "size": 12_000, "progress": 1.0, "priority": 1],
["index": 5, "name": "Open media collection/README.txt", "size": 8_000, "progress": 1.0, "priority": 1]
]
static func model() -> AppModel {
let m = AppModel()
m.credentials = Credentials(server: "https://example.invalid", username: "sample-user")
m.biometric = false; m.locked = false; m.connected = true
m.version = "5.1.2"; m.apiVersion = "2.11.4"; m.torrents = torrents
m.stats = stats; m.lastRefresh = Date()
let config = URLSessionConfiguration.ephemeral
config.protocolClasses = [StoreCaptureProtocol.self]
m.client = try! Transport(m.credentials, configuration: config)
return m
}
}
final class StoreCaptureProtocol: URLProtocol, @unchecked Sendable {
override class func canInit(with request: URLRequest) -> Bool { true }
override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }
override func startLoading() {
// Responses are local fixture data. There is no connection to a real torrent server.
Task { @MainActor in
let path = request.url!.path
let object: Any
if path.hasSuffix("app/preferences") { object = StoreCapture.preferences }
else if path.hasSuffix("torrents/files") { object = StoreCapture.files }
else if path.hasSuffix("torrents/properties") { object = ["save_path": "/downloads/complete", "total_size": 2_400_000_000, "pieces_num": 1145, "pieces_have": 733, "seeds": 42, "peers": 18, "time_elapsed": 540, "total_downloaded": 1_536_000_000, "total_uploaded": 645_120_000] }
else if path.hasSuffix("torrents/trackers") { object = [["url": "udp://tracker.example.invalid:6969/announce", "status": 2, "num_peers": 18, "num_seeds": 42]] }
else if path.hasSuffix("sync/torrentPeers") { object = ["peers": [:]] }
else if path.hasSuffix("transfer/info") { object = StoreCapture.stats }
else if path.hasSuffix("app/buildInfo") { object = ["qt": "6.8", "libtorrent": "2.0", "openssl": "3.4", "bitness": 64] }
else if path.hasSuffix("sync/maindata") { object = ["server_state": StoreCapture.stats] }
else { object = [] }
let data = try! JSONSerialization.data(withJSONObject: object)
client?.urlProtocol(self, didReceive: HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: ["Content-Type": "application/json"])!, cacheStoragePolicy: .notAllowed)
client?.urlProtocol(self, didLoad: data)
client?.urlProtocolDidFinishLoading(self)
}
}
override func stopLoading() {}
}