Skip to content

refactor!: rewrite the mock as a stateful fake backed by SQLite - #4

Merged
NikoHadouken merged 14 commits into
mainfrom
refactor/sqlite-rewrite
Oct 8, 2026
Merged

NikoHadouken merged 14 commits into
mainfrom
refactor/sqlite-rewrite

Conversation

@NikoHadouken

Copy link
Copy Markdown
Contributor

Why

The mock served JSON files and saved a profile change by rewriting the whole file, so two writes at the same time could lose an update or leave a broken file. Nothing checked its data or its answers against the Sherpa spec, and the guest account endpoints still to come would have been built on top of it.

Summary

The mock is now a fake that keeps state: users, memberships, role assignments and profiles live in SQLite, so a write shows up in later reads and survives a restart.

  • It is built against a pinned copy of the Sherpa spec, with a script to move to another version. Its types are generated from that copy.
  • Request bodies that don't match the spec are rejected in the error format of the API they went to.
  • The tests check every answer against the spec.
  • Paths the mock doesn't implement answer 501 instead of 404.
  • Divisions, roles and gnetz tags stay read-only reference data. The example users and their profiles fill the database when it is empty, and a reset command puts it back to that state.
  • The database schema is migrated on startup, so data survives an upgrade of the mock.
  • Node runs the TypeScript sources directly, without a build step.

Deployment

The data folder now holds the database instead of JSON files, and the image no longer ships a data folder.

  1. Tag a release to publish the image. It is a breaking change, so v0.8.0 or v1.0.0.
  2. In gruene-api, drop the script that copies the JSON files out of the image, and its step in the setup docs: it fails against the new image. Keep the data folder mount, which now persists the database. JSON files left in the folder are ignored.

The rewrite types its handlers and checks its responses against the spec. sherpa-api is private, so a committed copy keeps builds and CI working without access to it.
The mock reread its JSON files on every request and rewrote whole files on writes, so concurrent writes could lose updates or corrupt a file. Data now lives in SQLite, seeded from the fixtures on first start, and survives restarts. The data directory holds the database instead of the JSON files, and the undocumented profile image endpoints are gone.
Tables were only created when missing, so a schema change never reached an existing database. Staging keeps guests created through the API, so resetting on a change isn't an option.
Profile queries sat in the route file while user queries had their own module. Both now live next to each other, seeding moves out of the database module, and the profile insert is shared between seeding and the create endpoint.
The handlers only checked that fields were present, so a body with a wrong type, like a null image on a profile update, crashed with a 500 instead of Sherpa's 422. Checking against the spec's request schemas replaces the hand-written checks, and one error handler now answers in the error format of the API the request went to.
A non-numeric cursor compared as zero and returned every user from the start.
Database calls run one at a time, so the test couldn't fail.
Each of Sherpa's APIs has its own spec section and error format, so a folder per API keeps an endpoint, its errors and its tests together, apart from the data store they share.
The fixtures used to be served as they were, so profiles sat apart and every object carried its toType. Now only the seed reads them, so a user's file holds everything about that user and the mock adds the toType when it answers.
@NikoHadouken
NikoHadouken merged commit b8167a2 into main Oct 8, 2026
1 check passed
@NikoHadouken
NikoHadouken deleted the refactor/sqlite-rewrite branch October 8, 2026 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant