Audelin: State Is the Contract
How Audelin's state machines came to show everything they hold, and how its tests stopped watching queues and started watching results.
Audelin records both sides of a call and transcribes them live on the Mac. Inside, each part of the app is a small state machine: a value that holds the state, events that come in, and a pure reducer that turns the old state and an event into a new state and, at times, an effect to run.
That shape is meant to make testing easy. For a while it didn't, because the tests kept looking behind the state.
Tests that know about queues
The worst failure was a hang. A test suite on CI ran until it was killed at 180 seconds. The cause was a test copy of the app's session: it wrapped the reducer, counted reductions and polled with Task.yield until the count looked right. That polling took every thread in the pool, and nothing else could run.
Other tests had the same habit in milder forms. Some drained a custom executor before checking anything. Some asserted which dispatch queue a piece of work ran on. Components grew executor: parameters and waitForWork() hooks that production never used. Each test knew how the work was scheduled, not just what it did.
The rule: state is all you may see
The fix started with a rule. A machine's State is its whole public contract. Every field is public and has a public initializer. There is no hidden bookkeeping, and no computed property that works out what the screen shows. If a view shows it, the state holds it.
The transcript reader was the clearest case. It kept part of its state private and worked out the displayed log in a computed property. Now the displayed log is stored in the state, and the reducer writes it. The same pass went through the app's lifecycle, recording, the list of recordings, the floating panel, call-audio access and the session.
Reducers with injected dependencies
When a reducer needs to produce text for the screen, such as a time label or an error message, it gets the formatter as a closure passed to its initializer instead of calling a private helper. The app passes the real wording. A test passes a spy and checks three things: the spy is not called on paths that should not format, it gets the exact input, and its return value lands in the new state.
Reducer tests became short. A factory builds the given state, and the expected state is the given state plus a change:
assert(state, .letGo(first), delivers: nil, using: makeReducer(spy)) {
$0.lines.holds = [second]
$0.log = .lines($0.lines)
}
No closure means "nothing changed". No test writes out a whole expected state, and none needs @testable, because nothing it checks is hidden.
Tests that watch results
The asynchronous tests changed the same way. They now wait for what their fakes report: the reader has read, the release has happened, the fake run has returned. The executor: parameters and waitForWork() hooks are gone, and production work runs on the system's own pool.
Where a test used to check that work ran on a particular queue, it now checks what that was meant to guarantee: work that blocks a thread must not starve the pool. The test starts more blocked loads than the pool has threads and expects every one of them to finish.
The test copy of the session was deleted. The tests run the app's own session and wait on the states it publishes. Other tests now cover the adapters the app really builds: the real microphone and call-audio capture path, the real clock with real waits, the real read of the default output device.
What changed
The hang went away with the polling that caused it. Beyond that, the tests got easier to read and harder to fool. A reducer test reads as "from this state, this event makes this change". An async test reads as "when this happens, the fake sees that". Neither depends on how many threads the machine has or which queue picked up the work, so neither breaks when that changes.
Production code got simpler too: hooks that existed only for tests are gone, and what is left is what the app uses.
The code and tests are in Audelin's public repository.