Part 1: Designing for Testability
This is the first post in a build log. It describes the design I started with, not a finished system. Some of it is built, some is planned, and some is still an open question. Later posts will revise it, and I’ll say so when I change my mind.
Thryvo Cal is an app that scans your food and logs what you eat. It’s also offline-first: your data lives on your phone, and the server is there to back it up and sync it, not to be required.
Before writing much code, I made one commitment: no part of the app should need the internet to be tested. If a test can fail because of a bad connection instead of bad code, you stop trusting your tests.
The shape of the app
The app is split into layers: the screen, the logic behind the screen (the ViewModel), a repository that decides where data comes from, a data source that fetches it, and a service that talks to the backend.
I’ll be upfront that some of these layers are thin right now. Today the data source mostly passes calls through to the service. They’re scaffolding. I know what each layer is meant to own, and as I build, tests will pin that down. If a layer turns out not to earn its place, I’ll remove it and write about that too.
Five early decisions
1. Parts receive their tools instead of creating them. A class that opens its own backend connection can’t be tested without the backend. So each class is handed what it needs. In the real app that’s the real connection, and in tests it’s a stand-in. Status: built.
2. Depend on a contract, not a provider. The upper layers only know a simple agreement: “give me the food list, let me add an entry.” They don’t know whether the answer comes from Supabase, a local database, or a test double. That is what will let me add local storage without rewriting everything above it. Status: built.
3. Keep dedicated test doubles. I maintain stand-in versions of the backend service and the data source, so a failing test points to one layer. One cleanup is already on my list: these mocks currently live in the app’s source folder, and they belong in the test folder. Status: built, needs tidying.
4. Keep heavy parsing off the UI thread. Turning a large server response into app objects takes real work, and doing it on the thread that draws the screen can make animations stutter. I used Dart’s Isolate.run to move that work to a background thread. Its real job is the bulk restore: when a user signs in on a new device, their whole history, from their first day until today, gets fetched and parsed on the phone. For small responses an isolate probably costs more than it saves. I haven’t benchmarked this yet, so treat “smooth” as a goal until I have numbers. Status: in place, unmeasured.
5. Log failures with their stack trace, then rethrow. Nothing fails silently. The data layer records what happened and where, then passes the error up. I already suspect this will get noisy if every layer does it, and that typed errors would serve the UI better, such as “offline” versus “bad data.” Status: built, expected to change.
What isn’t built yet
The biggest gap is the local database. The app is not connected to it yet, and it matters, because it changes the design.
In an offline-first app, the local database is the source of truth. The screen reads only from it, and the server feeds it. New entries are saved locally first and synced later. That is a different shape from “ask the server, fall back to local,” and I want to build it the right way around from the start.
These are the decisions I’m facing next, and I haven’t made them yet:
Which database package.
Drift or sqflite. I lean toward Drift for live-updating queries and migrations, but I want to try both on realistic data.
How to avoid duplicates.
Food entries will get IDs generated on the device, so a retried sync can’t create the same meal twice.
What happens on conflict.
Intake logs are mostly append-only, so a simple last-write-wins rule may be enough. Ingredient data comes from the server, so the server wins. I’ll write the rules down before writing the code.
How to restore a full history.
Fetch in pages, save each page in a transaction, and make it resumable, so an interrupted restore doesn’t start over.
The honest trade-off
More layers means more files and more wiring. A small feature now passes through several steps, which would be overkill for a tiny app. I’m accepting that cost because I expect the data layer to be the hardest part of this app to get right, and I want to be able to test it without a phone or a network.
What’s next
In Part 2 I’ll connect the local database and test the scenario I care about most: log meals offline, sync later, end up with no duplicates and no lost entries. I’ll include real numbers from a low-end Android phone for the history restore, even if they show I was wrong about something.
Thryvo Cal’s closed testing is open on Google Play. If you’d like to try it, you can join here. I’d also like to hear how you handle sync and testing in your own projects.