Generate fakeable protocols and interfaces for Swift and Kotlin services
Swift and Kotlin callers can now swap any service for a fake in their tests. Every generated service gets a protocol (Swift) or interface (Kotlin) that it conforms to, generated from the same operation list as the service itself.
What
- Swift: each
XServicegets apublic protocol XServiceProtocol: Sendable, whose requirements are the service's generated method signatures. The service conforms with a separateextension XService: XServiceProtocol {}.- An unconstrained protocol extension adds the options-less forms (
list()forwardinglist(options: nil)). Protocol requirements can't carry default arguments, and without these forms code written against the protocol would read differently from code written against the class. extension XServiceProtocol where Self: BasecampFakegives every requirement a body that throwsNotFakedError(service:method:). A fake declaresstruct FakeProjects: ProjectsServiceProtocol, BasecampFakeand implements only what its test calls. The real service doesn't conform toBasecampFake, so it still has to witness every requirement itself, and a signature drift is a compile error.BasecampFakeandNotFakedErrorare hand-written inswift/Sources/Basecamp/BasecampFake.swift, and listed in the AGENTS.md infrastructure table.- Wrapped results (
PersonProgressResult) get a public memberwise init, so a fake outside the module can build one.
- An unconstrained protocol extension adds the options-less forms (
- Kotlin: each
XServicegets aninterface XApi. Its members carry the defaults and a body that throwsNotImplementedError("XApi.m is not faked: override it in your fake"). The class implements it, and its methods becomeoverride(final overridein the extensible open classes, so subclassing behaves as it did). The pagination compat overloads are interface members too, forwarding to the primary, so a fake that overrides the primary answers both. - AccountClient: the accessors still return the concrete classes. Callers inject a fake by taking
any XServiceProtocol/XApiat their own seam, and passaccount.projectsin production. Changing the accessors' return types would break every caller that names the concrete type.
Decisions
- Names: Swift
XServiceProtocol, KotlinXApi. There's no hey-sdk precedent. Swift can't reuseXService, which is the class, and the API guidelines suffix a protocol that describes a capability.XApiis the common Kotlin spelling for the interface behind a client (Retrofit, Ktor clients), and it keeps the class name free. - "Not faked" defaults: throwing, never a fabricated empty value. A test that reaches an unfaked method fails with the service and method named, rather than passing on a default. In Swift the defaults sit behind the
BasecampFakeopt-in, so they can't hide a missing witness on the real service. Kotlin has no conditional default, so an interface default would let the class compile without an override.ServiceApiCoverageTest(jvmTest) scans the generated sources and fails if any interface member lacks an override in its class. It checks all 303 operations, and I've confirmed it fails when one override is removed. - Composites stay off the protocol:
update/editmerge-safe composites, summaries and mention helpers are hand-written over generated calls. A fake of the wire methods already fakes what they call, and putting them on the protocol would make every fake re-implement composite logic or inherit a throwing stub for it. - Swift fakes with state should be actors. The README and
ServiceFakeTestsshow one, because the protocol isSendable.
Note for the Swift dead-strip work
Each generated Swift service file ends with extension XService: XServiceProtocol {}. That conformance record is a live root: its witness table references every wire method, which is all 303 methods across 58 services, so the linker can't strip an unused service method from a binary that links Basecamp. The conformance sits on its own line so a dead-strip lane can measure it, or move it behind a separate product.
Compatibility
Source compatible for callers in both languages. The services and accessors are unchanged, and only conformances, members and inits are added. Kotlin binary compatibility isn't promised (kotlin/README), and the API-compat gate covers Go and Rust only. No breaking label and no MIGRATING entry.
Tests
- Swift
ServiceFakeTests:- a fake answers through the protocol;
- an unfaked method throws
NotFakedError, with matchingdescriptionandlocalizedDescription; - the options-less and full forms reach the same requirement;
- the real service makes its request through the protocol (MockTransport);
- every generated service file carries the protocol, the fake extension and the conformance.
BasecampPublicAPIConsumerbuilds fakes ofProjectsServiceProtocolandReportsServiceProtocolfrom outside the module, including aPersonProgressResult.- Kotlin
ServiceFakeTest:- an object fake answers;
- an unfaked method names itself;
- the compat overload reaches the faked primary;
- real services satisfy their interfaces (MockEngine).
- Kotlin
ServiceApiCoverageTest, described above. - SPEC.md gains "Service Test Doubles (Swift, Kotlin)" after Service Generation Pattern. swift/README and kotlin/README gain "Testing With Fakes".
Generators and contracts: a person reviews these before landing.
Summary by cubic
Generates a fakeable protocol (Swift) and interface (Kotlin) for every generated service so app tests can replace HTTP with a small fake. The latest merges bring the single-page read siblings (<method>Page) and the per-call requestOptions parameter onto that surface, and the branch now rides on main's typed-date and open-enum regeneration (#1140) with the fakeable surface itself unchanged.
- Swift — each
XServicegainspublic protocol XServiceProtocol: Sendablewhose requirements are the service's wire methods plus each page sibling, each ending inrequestOptions: RequestOptions; the class conforms viaextension XService: XServiceProtocol {}. A fake declaresstruct FakeX: XServiceProtocol, BasecampFakeand implements only what its test calls; every other requirement throwsNotFakedError(service:method:). Convenience forms (options-less, and per subset of defaulted params likelistPage(),listPage(options:),listPage(continuation:)) come from an unconstrained protocol extension. Wrapped results, including the new page result types, gain public memberwise inits so fakes outside the module can build them. - Kotlin — each
XServiceimplementsinterface XApi. Members carry defaults that throwNotImplementedError("XApi.m is not faked"), page siblings included. Methods becomeoverride(final overridein the extensible open services). The pagination compat overloads are interface members forwarding to the primary, so a fake overriding the primary answers both; the README spells out that a fake overrides the options-taking primary, not the compat overload.ServiceApiCoverageTest(jvmTest) fails if any interface member lacks a class override; its floor now covers 366 members (63 of them page siblings). - Hand-written composites (update/edit, summaries, mention helpers) stay off both surfaces — faking the wire methods covers what they call.
AccountClientaccessors still return concrete classes; callers inject a fake at their own seam. - Source compatible: only conformances, members, conveniences, and inits are added. Generators and contracts need a person review before landing.
Not-faked behavior
An unfaked method throws with the service and method named, never a fabricated value. Swift hides the throwing bodies behind the BasecampFake opt-in, so signature drift against the real service is a compile error; the error names the requirement's full selector and tells a fake that implemented the options-less form to add requestOptions. Kotlin's interface defaults would let the class compile without an override, so the coverage test scans generated sources for missing overrides.
Note for the Swift dead-strip work
Each generated Swift service file ends with extension XService: XServiceProtocol {}. That conformance is a live root — its witness table references every wire method, all 366 across 58 services — so the linker can't strip an unused service method from a binary that links Basecamp. It sits on its own line so a dead-strip lane can measure it or move it behind a separate product. A merge also rebaselined the Swift size budget for Xcode 16 from CI's measurement.