An API client for ipx, and its event stream (#1)
Everything native needs this first, and it is worth having whatever happens to the rest: a CarPlay browse tree is built from exactly these calls. API covers what a client reads and writes -- feeds, entries with the server's own paging, filtering, sorting and search, read and pinned, position, read-all, download, delete, fetch -- and APIModels is what ipx sends, with the server's field names kept so a type here can be checked against web.rs without translating first. It sends nothing of its own to authenticate: ipx decides who is asking by cookie and CookieBridge keeps HTTPCookieStorage in step, which is the same property that lets a plain <audio src> work. Events reads /api/events with URLSession's byte stream, so there is no new dependency. An event this build has never heard of decodes as .other rather than throwing, because a new one on the server must not stop the stream. It reconnects with a backoff: a deploy closes the stream cleanly and should be picked straight back up, a daemon that is down should not be hammered. Library is gone, folded into API. Two clients writing positions was one too many, and the page's forwarded beacon no longer carries a path: it asks the host to save, and the host uses its own clock, which is the only one not stale when the web view has been frozen in the background. The tests run against the browser suite's fixture daemon rather than canned JSON, because the shapes being decoded are the server's and a fixture would only prove it matches itself. Thirteen pass, including a live scan reported over SSE and the playback round trip, which still works after the move. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
58
ios/Tests/EventsTests.swift
Normal file
58
ios/Tests/EventsTests.swift
Normal file
@@ -0,0 +1,58 @@
|
||||
import XCTest
|
||||
@testable import iPodderX
|
||||
|
||||
/// The event stream, against a real daemon. Asking it to scan is the cheapest way to make it
|
||||
/// say something, and scan_done is terminal, so the test has a definite end rather than a sleep.
|
||||
final class EventsTests: XCTestCase {
|
||||
func testAScanIsReportedOnTheStream() async throws {
|
||||
let env = ProcessInfo.processInfo.environment
|
||||
let server = env["IPX_SERVER"] ?? ""
|
||||
try XCTSkipIf(server.isEmpty, "set IPX_SERVER to a fixture daemon")
|
||||
ServerSettings.base = try XCTUnwrap(ServerSettings.parse(server))
|
||||
|
||||
if let token = env["IPX_TOKEN"], !token.isEmpty, let url = ServerSettings.base {
|
||||
let cookie = try XCTUnwrap(HTTPCookie(properties: [
|
||||
.name: "ipx_token", .value: token, .path: "/",
|
||||
.domain: url.host ?? "127.0.0.1",
|
||||
]))
|
||||
HTTPCookieStorage.shared.setCookie(cookie)
|
||||
}
|
||||
|
||||
let events = await Events()
|
||||
let sawScanDone = expectation(description: "scan_done arrives")
|
||||
sawScanDone.assertForOverFulfill = false
|
||||
|
||||
await MainActor.run {
|
||||
events.onEvent { event in
|
||||
if case .scanDone = event { sawScanDone.fulfill() }
|
||||
}
|
||||
events.start()
|
||||
}
|
||||
// Connect before asking for work, or the answer arrives before anyone is listening.
|
||||
try await Task.sleep(nanoseconds: 1_500_000_000)
|
||||
|
||||
try await API().fetch(feed: "test-show")
|
||||
await fulfillment(of: [sawScanDone], timeout: 30)
|
||||
await MainActor.run { events.stop() }
|
||||
}
|
||||
|
||||
/// An event this build has never heard of must not stop the stream.
|
||||
func testAnUnknownEventDecodesRatherThanThrowing() throws {
|
||||
let data = Data(#"{"ev":"something_new","feed":"x"}"#.utf8)
|
||||
let event = try JSONDecoder().decode(IPXEvent.self, from: data)
|
||||
guard case .other(let name) = event else { return XCTFail("expected .other, got \(event)") }
|
||||
XCTAssertEqual(name, "something_new")
|
||||
}
|
||||
|
||||
/// Progress without a total happens: a server that sends no Content-Length.
|
||||
func testProgressDecodesWithoutATotal() throws {
|
||||
let data = Data(#"{"ev":"progress","feed":"f","enclosure":7,"url":"u","file":"a.mp3","done":10}"#.utf8)
|
||||
guard case .progress(_, let enclosure, _, let done, let total) =
|
||||
try JSONDecoder().decode(IPXEvent.self, from: data) else {
|
||||
return XCTFail("did not decode as progress")
|
||||
}
|
||||
XCTAssertEqual(enclosure, 7)
|
||||
XCTAssertEqual(done, 10)
|
||||
XCTAssertNil(total)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user