Generate fakeable protocols and interfaces for Swift and Kotlin services

Merged
JE
1d ago
+11643-507

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 XService gets a public protocol XServiceProtocol: Sendable, whose requirements are the service's generated method signatures. The service conforms with a separate extension XService: XServiceProtocol {}.
    • An unconstrained protocol extension adds the options-less forms (list() forwarding list(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: BasecampFake gives every requirement a body that throws NotFakedError(service:method:). A fake declares struct FakeProjects: ProjectsServiceProtocol, BasecampFake and implements only what its test calls. The real service doesn't conform to BasecampFake, so it still has to witness every requirement itself, and a signature drift is a compile error.
    • BasecampFake and NotFakedError are hand-written in swift/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.
  • Kotlin: each XService gets an interface XApi. Its members carry the defaults and a body that throws NotImplementedError("XApi.m is not faked: override it in your fake"). The class implements it, and its methods become override (final override in 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 / XApi at their own seam, and pass account.projects in production. Changing the accessors' return types would break every caller that names the concrete type.

Decisions

  • Names: Swift XServiceProtocol, Kotlin XApi. There's no hey-sdk precedent. Swift can't reuse XService, which is the class, and the API guidelines suffix a protocol that describes a capability. XApi is 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 BasecampFake opt-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/edit merge-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 ServiceFakeTests show one, because the protocol is Sendable.

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 matching description and localizedDescription;
    • 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.
  • BasecampPublicAPIConsumer builds fakes of ProjectsServiceProtocol and ReportsServiceProtocol from outside the module, including a PersonProgressResult.
  • 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 XService gains public protocol XServiceProtocol: Sendable whose requirements are the service's wire methods plus each page sibling, each ending in requestOptions: RequestOptions; the class conforms via extension XService: XServiceProtocol {}. A fake declares struct FakeX: XServiceProtocol, BasecampFake and implements only what its test calls; every other requirement throws NotFakedError(service:method:). Convenience forms (options-less, and per subset of defaulted params like listPage(), 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 XService implements interface XApi. Members carry defaults that throw NotImplementedError("XApi.m is not faked"), page siblings included. Methods become override (final override in 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. AccountClient accessors 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.