Repin the kit to 388d3da, and forward upload through SwappableTransport
Task 6 of the native slice 2 part 1 plan, as the coordinator amended it after Fable's kit review (S2): upload lives in rastrillo/native's NativeUploadTransport, which refines NativeTransport with no default. - Package.swift and ios/project.yml pin rastrillo/native main 388d3da (the upload merge). - BaseClient's transport, BaseSession.begin and SwappableTransport take NativeUploadTransport. SwappableTransport forwards upload to the transport current when the upload starts, like everything else. - The tests' GatedTransport conforms too, gated by path, recording each upload's file name. ClientTests' helper asks for the refined type. - check-native.sh passes -j 4 to swift test and swift build; build-iphone.sh passes -jobs 4 to xcodebuild (not in the plan: the machine's rule caps every build, and check-native.sh calls it). Tests (SessionTests): an upload reaches the current transport, and after a swap the next one reaches the new one; an upload started before a swap finishes where it started; the session's client uploads through the renewed transport. Gate: swift test -j 4 --filter BaseKitTests.SessionTests: 23 passed; build-iphone.sh simulator: BUILD SUCCEEDED. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
Task 6: repin to 388d3da; SwappableTransport and GatedTransport forward upload
10 files changed,
+112
−25
native/apple/Package.resolved+2 −2native/apple/Package.swift+3 −3native/apple/Sources/BaseKit/API/Client.swift+3 −2native/apple/Sources/BaseKit/Session/BaseSession.swift+15 −6native/apple/Tests/BaseKitTests/ClientTests.swift+1 −1native/apple/Tests/BaseKitTests/SessionTests.swift+62 −0native/apple/Tests/BaseKitTests/Support.swift+21 −6native/ios/project.yml+1 −1native/scripts/build-iphone.sh+2 −2native/scripts/check-native.sh+2 −2
diff --git a/native/apple/Package.resolved b/native/apple/Package.resolved| index 2d80e2c..8e93ed2 100644 |
| --- a/native/apple/Package.resolved |
| +++ b/native/apple/Package.resolved |
| @@ -1,5 +1,5 @@ |
| { |
| - "originHash" : "b89733f6728080d7a31ff67ab1761ff7c1b4a71eb609dc96e40a318c497a8a91", |
| + "originHash" : "0f3c81e02b0dd9fe1ff0e94fd187bc57e128c1efb430d342711ffd887c4964cc", |
| "pins" : [ |
| { |
| "identity" : "keyboardshortcuts", |
| @@ -14,7 +14,7 @@ |
| "kind" : "remoteSourceControl", |
| "location" : "https://amadan.net/rastrillo/native", |
| "state" : { |
| - "revision" : "bb989a7cf3bdb152e1529f6e514d24f249f777ec" |
| + "revision" : "388d3da5a96d08d7a297ecdf1ae2ce890b498859" |
| } |
| }, |
| { |
diff --git a/native/apple/Package.swift b/native/apple/Package.swift| index c594078..e2b62ff 100644 |
| --- a/native/apple/Package.swift |
| +++ b/native/apple/Package.swift |
| @@ -2,12 +2,12 @@ |
| import PackageDescription |
| import Foundation |
| -// rastrillo/native at main bb989a7, with Base's additions (Task 1): |
| +// rastrillo/native at main 388d3da, with Base's additions (slice 1 Task 1): |
| // OficinaIDModel.assertion(for:), OficinaApp.base and NativeConnection's |
| -// ://open. OFICINA_NATIVE_PACKAGE_PATH points at a local checkout instead, |
| +// ://open; and (slice 2 Task 5) NativeUploadTransport.upload. OFICINA_NATIVE_PACKAGE_PATH points at a local checkout instead, |
| // for work on the kit itself. |
| let sharedNative: Package.Dependency = ProcessInfo.processInfo.environment["OFICINA_NATIVE_PACKAGE_PATH"].map { .package(name: "native", path: $0) } |
| - ?? .package(url: "https://amadan.net/rastrillo/native", revision: "bb989a7cf3bdb152e1529f6e514d24f249f777ec") |
| + ?? .package(url: "https://amadan.net/rastrillo/native", revision: "388d3da5a96d08d7a297ecdf1ae2ce890b498859") |
| let package = Package( |
| name: "BaseNative", |
diff --git a/native/apple/Sources/BaseKit/API/Client.swift b/native/apple/Sources/BaseKit/API/Client.swift| index 777699a..58e092c 100644 |
| --- a/native/apple/Sources/BaseKit/API/Client.swift |
| +++ b/native/apple/Sources/BaseKit/API/Client.swift |
| @@ -28,10 +28,11 @@ public enum BaseAPIError: Error, Sendable, Equatable { |
| /// Any other 401 or a 303 is only thrown. Equipo refusing Base's key for this person is Ask's |
| /// own state, `409 {"state":"reconnect"}`, and is only thrown. |
| public struct BaseClient: Sendable { |
| - public let transport: NativeTransport |
| + /// An uploading transport, so a wrapper can't silently lack `upload`. |
| + public let transport: NativeUploadTransport |
| let rejected: @Sendable () async -> Void |
| - public init(transport: NativeTransport, rejected: @escaping @Sendable () async -> Void) { |
| + public init(transport: NativeUploadTransport, rejected: @escaping @Sendable () async -> Void) { |
| self.transport = transport |
| self.rejected = rejected |
| } |
diff --git a/native/apple/Sources/BaseKit/Session/BaseSession.swift b/native/apple/Sources/BaseKit/Session/BaseSession.swift| index 785bf8b..c09546e 100644 |
| --- a/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| +++ b/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| @@ -96,7 +96,7 @@ import RastrilloApple |
| /// Signed in as `identity` at `origin`. The same account again (the kit |
| /// renewed its session) swaps the session under the client; another |
| /// account starts afresh, and nothing of the old one lands. |
| - func begin(identity: String, origin: URL, transport http: NativeTransport, state: OficinaSignedIn? = nil) { |
| + func begin(identity: String, origin: URL, transport http: NativeUploadTransport, state: OficinaSignedIn? = nil) { |
| let next = Account(identity: identity, origin: origin) |
| if let transport, account == next { |
| transport.swap(http) |
| @@ -285,12 +285,17 @@ import RastrilloApple |
| /// A transport whose session can be replaced under the requests that use |
| /// it: each request goes to whichever `NativeHTTP` is current when it starts. |
| -final class SwappableTransport: NativeTransport, @unchecked Sendable { |
| +/// |
| +/// It is a `NativeUploadTransport`, and forwards `upload` by hand like |
| +/// everything else. The kit gives `upload` no default on purpose: had this |
| +/// wrapper been able to fall back to one, every upload in the app would |
| +/// have failed with every test green (Fable's plan review, B1). |
| +final class SwappableTransport: NativeUploadTransport, @unchecked Sendable { |
| private let lock = NSLock() |
| - private var inner: NativeTransport |
| - init(_ inner: NativeTransport) { self.inner = inner } |
| - func swap(_ next: NativeTransport) { lock.withLock { inner = next } } |
| - private var current: NativeTransport { lock.withLock { inner } } |
| + private var inner: NativeUploadTransport |
| + init(_ inner: NativeUploadTransport) { self.inner = inner } |
| + func swap(_ next: NativeUploadTransport) { lock.withLock { inner = next } } |
| + private var current: NativeUploadTransport { lock.withLock { inner } } |
| var origin: URL { current.origin } |
| func responseDetails(_ path: String, method: String, body: Data?, contentType: String, maxBytes: Int) async throws -> NativeResponse { |
| @@ -302,4 +307,8 @@ final class SwappableTransport: NativeTransport, @unchecked Sendable { |
| func download(_ path: String, to destination: URL) async throws { |
| try await current.download(path, to: destination) |
| } |
| + func upload(_ path: String, body: Data, contentType: String, filename: String, |
| + maxBytes: Int, progress: @escaping @Sendable (Double) -> Void) async throws -> NativeResponse { |
| + try await current.upload(path, body: body, contentType: contentType, filename: filename, maxBytes: maxBytes, progress: progress) |
| + } |
| } |
diff --git a/native/apple/Tests/BaseKitTests/ClientTests.swift b/native/apple/Tests/BaseKitTests/ClientTests.swift| index 45145d9..bbefc95 100644 |
| --- a/native/apple/Tests/BaseKitTests/ClientTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/ClientTests.swift |
| @@ -5,7 +5,7 @@ import RastrilloTestSupport |
| import XCTest |
| final class ClientTests: XCTestCase { |
| - func client(_ transport: NativeTransport, _ rejections: Rejections = Rejections()) -> BaseClient { |
| + func client(_ transport: NativeUploadTransport, _ rejections: Rejections = Rejections()) -> BaseClient { |
| BaseClient(transport: transport) { rejections.hit() } |
| } |
diff --git a/native/apple/Tests/BaseKitTests/SessionTests.swift b/native/apple/Tests/BaseKitTests/SessionTests.swift| index ff91393..b1c44dd 100644 |
| --- a/native/apple/Tests/BaseKitTests/SessionTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/SessionTests.swift |
| @@ -458,3 +458,65 @@ extension SessionTests { |
| session.end() |
| } |
| } |
| + |
| +extension SessionTests { |
| + func upload(_ transport: some NativeUploadTransport, _ name: String) async throws -> String { |
| + let r = try await transport.upload("/ask/attachments", body: Data(name.utf8), contentType: "text/plain", filename: name, |
| + maxBytes: 4096, progress: { _ in }) |
| + return String(decoding: r.data, as: UTF8.self) |
| + } |
| + |
| + /// Task 6: `SwappableTransport` forwards `upload` to the transport that |
| + /// is current when the upload starts, before and after a swap. |
| + func testSwappableTransportForwardsUploads() async throws { |
| + let first = GatedTransport(), second = GatedTransport() |
| + for (t, id) in [(first, "first"), (second, "second")] { |
| + t.answer("/ask/attachments", status: 201, #"{"id":"\#(id)"}"#) |
| + t.release("/ask/attachments") |
| + } |
| + let swappable = SwappableTransport(first) |
| + let one = try await upload(swappable, "a.txt") |
| + XCTAssertEqual(one, #"{"id":"first"}"#) |
| + swappable.swap(second) |
| + let two = try await upload(swappable, "b.txt") |
| + XCTAssertEqual(two, #"{"id":"second"}"#) |
| + XCTAssertEqual(first.uploads, ["a.txt"]) |
| + XCTAssertEqual(second.uploads, ["b.txt"]) |
| + } |
| + |
| + /// An upload started before a swap finishes on the transport it started |
| + /// on: the renewed session takes only what starts after it. |
| + func testAnUploadStartedBeforeASwapFinishesWhereItStarted() async throws { |
| + let first = GatedTransport(), second = GatedTransport() |
| + first.answer("/ask/attachments", status: 201, #"{"id":"first"}"#) |
| + second.answer("/ask/attachments", status: 201, #"{"id":"second"}"#) |
| + second.release("/ask/attachments") |
| + let swappable = SwappableTransport(first) |
| + let started = Task { try await self.upload(swappable, "a.txt") } |
| + try await waitUntil { first.uploads == ["a.txt"] } |
| + swappable.swap(second) |
| + first.release("/ask/attachments") |
| + let answer = try await started.value |
| + XCTAssertEqual(answer, #"{"id":"first"}"#) |
| + XCTAssertEqual(second.uploads, []) |
| + } |
| + |
| + /// The session's client uploads through the swappable transport: after |
| + /// the kit renews the session, the next upload goes out on the new one. |
| + func testTheClientsUploadsFollowARenewedSession() async throws { |
| + let session = BaseSession(oficina: memoryOficina(), defaults: freshDefaults()) |
| + let origin = URL(string: "https://alice.example")! |
| + let first = gated("alice", "Alice's", "Hello Alice") |
| + session.begin(identity: "id-alice", origin: origin, transport: first) |
| + let renewed = gated("alice", "Alice's", "Hello Alice") |
| + renewed.answer("/ask/attachments", status: 201, #"{"id":"renewed"}"#) |
| + renewed.release("/ask/attachments") |
| + session.begin(identity: "id-alice", origin: origin, transport: renewed) |
| + let client = try XCTUnwrap(session.client) |
| + let answer = try await upload(client.transport, "a.txt") |
| + XCTAssertEqual(answer, #"{"id":"renewed"}"#) |
| + XCTAssertEqual(first.uploads, []) |
| + XCTAssertEqual(renewed.uploads, ["a.txt"]) |
| + session.end() |
| + } |
| +} |
diff --git a/native/apple/Tests/BaseKitTests/Support.swift b/native/apple/Tests/BaseKitTests/Support.swift| index a596482..d3b88e9 100644 |
| --- a/native/apple/Tests/BaseKitTests/Support.swift |
| +++ b/native/apple/Tests/BaseKitTests/Support.swift |
| @@ -86,7 +86,7 @@ final class LocalServer: @unchecked Sendable { |
| /// A transport whose answers wait until the test releases them, path by |
| /// path: for races (an old account's answer landing after a new sign-in). |
| -final class GatedTransport: NativeTransport, @unchecked Sendable { |
| +final class GatedTransport: NativeUploadTransport, @unchecked Sendable { |
| let origin: URL |
| private let lock = NSLock() |
| private var answers: [String: (Int, Data)] = [:] |
| @@ -95,8 +95,11 @@ final class GatedTransport: NativeTransport, @unchecked Sendable { |
| private var released: Set<String> = [] |
| private var seen: [String] = [] |
| private var sent: [(String, Data?)] = [] |
| + private var names: [String] = [] |
| init(origin: URL = URL(string: "https://gated.invalid")!) { self.origin = origin } |
| var calls: [String] { lock.withLock { seen } } |
| + /// The file names uploaded, in the order the uploads started. |
| + var uploads: [String] { lock.withLock { names } } |
| /// The bodies posted to `path`, in order. |
| func bodies(_ path: String) -> [Data?] { lock.withLock { sent.filter { $0.0 == path }.map(\.1) } } |
| @@ -113,6 +116,23 @@ final class GatedTransport: NativeTransport, @unchecked Sendable { |
| for c in ready { c.resume() } |
| } |
| func responseDetails(_ path: String, method: String, body: Data?, contentType: String, maxBytes: Int) async throws -> NativeResponse { |
| + await gate(path, body) |
| + let (status, data, head) = lock.withLock { () -> (Int, Data, [String: String]) in |
| + let (s, d) = answers[path] ?? (404, Data()) |
| + return (s, d, headers[path] ?? [:]) |
| + } |
| + return NativeResponse(data: data, status: status, headers: head) |
| + } |
| + /// An upload is gated by its path like any request, and its file name |
| + /// is recorded as it starts. |
| + func upload(_ path: String, body: Data, contentType: String, filename: String, |
| + maxBytes: Int, progress: @escaping @Sendable (Double) -> Void) async throws -> NativeResponse { |
| + lock.withLock { names.append(filename) } |
| + let answer = try await responseDetails(path, method: "POST", body: body, contentType: contentType, maxBytes: maxBytes) |
| + progress(1) |
| + return answer |
| + } |
| + private func gate(_ path: String, _ body: Data?) async { |
| await withCheckedContinuation { (c: CheckedContinuation<Void, Never>) in |
| let go = lock.withLock { () -> Bool in |
| seen.append(path) |
| @@ -123,11 +143,6 @@ final class GatedTransport: NativeTransport, @unchecked Sendable { |
| } |
| if go { c.resume() } |
| } |
| - let (status, data, head) = lock.withLock { () -> (Int, Data, [String: String]) in |
| - let (s, d) = answers[path] ?? (404, Data()) |
| - return (s, d, headers[path] ?? [:]) |
| - } |
| - return NativeResponse(data: data, status: status, headers: head) |
| } |
| func stream(_ path: String, onOpen: @Sendable () async throws -> Void, onLine: @Sendable (String) async throws -> Void) async throws { |
| lock.withLock { seen.append(path) } |
diff --git a/native/ios/project.yml b/native/ios/project.yml| index a988e4c..09bef42 100644 |
| --- a/native/ios/project.yml |
| +++ b/native/ios/project.yml |
| @@ -8,7 +8,7 @@ packages: |
| path: ../apple |
| RastrilloNative: |
| url: https://amadan.net/rastrillo/native |
| - revision: bb989a7cf3bdb152e1529f6e514d24f249f777ec |
| + revision: 388d3da5a96d08d7a297ecdf1ae2ce890b498859 |
| settings: |
| base: |
| SWIFT_VERSION: '6.0' |
diff --git a/native/scripts/build-iphone.sh b/native/scripts/build-iphone.sh| index a4643da..3309fd4 100755 |
| --- a/native/scripts/build-iphone.sh |
| +++ b/native/scripts/build-iphone.sh |
| @@ -8,11 +8,11 @@ cd ios |
| xcodegen generate --quiet |
| case "${1:-simulator}" in |
| simulator) |
| - xcodebuild -project BasePhone.xcodeproj -scheme BasePhone -configuration Debug -sdk iphonesimulator \ |
| + xcodebuild -jobs 4 -project BasePhone.xcodeproj -scheme BasePhone -configuration Debug -sdk iphonesimulator \ |
| -destination 'generic/platform=iOS Simulator' -derivedDataPath ../build/Phone CODE_SIGNING_ALLOWED=NO build ;; |
| device) |
| : "${OFICINA_DEVELOPMENT_TEAM:?set OFICINA_DEVELOPMENT_TEAM to sign for a device}" |
| - xcodebuild -project BasePhone.xcodeproj -scheme BasePhone -configuration Debug -sdk iphoneos \ |
| + xcodebuild -jobs 4 -project BasePhone.xcodeproj -scheme BasePhone -configuration Debug -sdk iphoneos \ |
| -destination 'generic/platform=iOS' -derivedDataPath ../build/Phone DEVELOPMENT_TEAM="$OFICINA_DEVELOPMENT_TEAM" -allowProvisioningUpdates build ;; |
| *) echo "usage: build-iphone.sh [simulator|device]" >&2; exit 2 ;; |
| esac |
diff --git a/native/scripts/check-native.sh b/native/scripts/check-native.sh| index d9056d9..4c3c117 100755 |
| --- a/native/scripts/check-native.sh |
| +++ b/native/scripts/check-native.sh |
| @@ -23,10 +23,10 @@ fi |
| if [ "$release" = 1 ]; then scripts/fonts-into-bundle.sh --release; else scripts/fonts-into-bundle.sh; fi |
| logs=build/logs; mkdir -p "$logs" |
| swift package --package-path apple resolve |
| -swift test --package-path apple > "$logs/swift-test.log" 2>&1 || { tail -40 "$logs/swift-test.log"; exit 1; } |
| +swift test -j 4 --package-path apple > "$logs/swift-test.log" 2>&1 || { tail -40 "$logs/swift-test.log"; exit 1; } |
| # XCTest's summary for the whole run, and swift-testing's. |
| grep -A1 "Test Suite 'All tests' passed" "$logs/swift-test.log" | tail -1 | sed 's/^[[:space:]]*/swift test: /' |
| -swift build --package-path apple --product BaseMac > "$logs/mac-build.log" 2>&1 || { grep -E 'error' "$logs/mac-build.log" | head -20; exit 1; } |
| +swift build -j 4 --package-path apple --product BaseMac > "$logs/mac-build.log" 2>&1 || { grep -E 'error' "$logs/mac-build.log" | head -20; exit 1; } |
| echo "BaseMac builds" |
| scripts/build-iphone.sh simulator > "$logs/phone-build.log" 2>&1 || { grep -E 'error' "$logs/phone-build.log" | head -20; exit 1; } |
| echo "BasePhone builds for the iOS Simulator" |