Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: cover restore-db schema rejection and service failure passthrough (#3919)

[._.]/ Adam Eivy0e83f24ff9c9
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 238d7aa48d..77432e9601 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -91,3 +91,4 @@
- A merge follow-up that successfully lands its PR is no longer recorded as a failure. These runs exist to merge one pull request: they make no commit, are told to exit once the PR reads `MERGED`, and on Antigravity often skip the completion sentinel — so the idle reaper had only proxies to judge them by, and every proxy got it wrong. Their prompt quotes `gh pr checks`, `gh pr merge` and `--delete-branch`, which the TUI echoes back, which latches the swarm merge-queue tracker before any work happens; the finished agent then sat silent for the tracker's 15-minute grace and was reaped as "idled out in the merge queue — it likely died mid-merge", twice retried, and finally blocked on max retries with a high-priority "PR left open" notification naming a PR that had merged fifteen minutes into the first attempt. The reaper now asks the forge the actual question — once a follow-up run has been quiet for its normal idle window, it checks whether its PR merged, and finalises on that answer. A `gh` that cannot be reached still counts as "we could not ask" rather than "not merged", so every existing verdict is untouched when the forge is unavailable, and a follow-up whose PR genuinely did not land still surfaces as needs-manual-finish. The reaper's branch matrix also moved out of the interval body into a pure function the tests now call directly, replacing a hand-maintained copy of that logic inside the test file.
- The backup service's persisted state file is now covered by tests. `getState` returning the default state when `data/backup/state.json` is missing or truncated mid-write, `saveState` merging a partial patch into what is already on disk, and — the one that actually matters — two concurrent `saveState` calls (a run finishing while the scheduler stamps a status) serializing through the write queue so neither loses the other's fields. The concurrency test fails if the queue is removed, and all of it runs against a real temp directory rather than a mocked filesystem.
- The Soul Creation Wizard's step-indicator dots are now announced. The five dots are real `<button>` elements with no text inside them, so a screen reader read out five unnamed buttons with no cue as to which step each one jumped to or which was current. Each dot now carries an `aria-label` naming its step number and title, and the active one carries `aria-current="step"`.
+- `POST /api/backup/restore-db` now has route tests for the two things its handler leans on and nothing asserted. Schema rejection is covered per bad type — a numeric, object, array, null, empty-string or boolean `snapshotId`, and a `dryRun` sent as `'false'`, `'true'`, `0`, `1`, `null` or an object all have to come back as the 400 `VALIDATION_ERROR` envelope with `restorePostgres` never called, since a coerced `dryRun` is the difference between reporting a restore and replaying a dump over the live database. A well-formed body is asserted alongside them so those rejections can't pass vacuously. The second half covers the service's resolved-value failures — `manifest_mismatch`, `restore_error`, `no_dump`, `not_configured` — which the route must hand back verbatim at 200 rather than collapsing into a generic 500 that hides which check refused the restore.
diff --git a/server/routes/backup.test.js b/server/routes/backup.test.js
index 7ef2aac959..84605450f3 100644
--- a/server/routes/backup.test.js
+++ b/server/routes/backup.test.js
@@ -213,5 +213,78 @@ describe('backup routes', () => {
expect(res.body.code).toBe('BACKUP_NOT_CONFIGURED');
expect(backup.restorePostgres).not.toHaveBeenCalled();
});
+
+ // `restoreDbRequestSchema` types snapshotId as a non-empty string. A client
+ // that sends a number, object, array, null, or '' must get the structured
+ // 400 VALIDATION_ERROR envelope — never a coerced snapshot id reaching the
+ // service, and never a generic 500.
+ it.each([
+ ['a number', 12345],
+ ['an object', { id: 'snap-1' }],
+ ['an array', ['snap-1']],
+ ['null', null],
+ ['an empty string', ''],
+ ['a boolean', true]
+ ])('returns 400 VALIDATION_ERROR when snapshotId is %s', async (_label, snapshotId) => {
+ getSettings.mockResolvedValue({ backup: { destPath: '/dest' } });
+ const res = await request(buildApp())
+ .post('/api/backup/restore-db')
+ .send({ snapshotId });
+ expect(res.status).toBe(400);
+ expect(res.body.code).toBe('VALIDATION_ERROR');
+ expect(backup.restorePostgres).not.toHaveBeenCalled();
+ });
+
+ // dryRun is the safety switch between "report what would happen" and
+ // "replay the dump over the live database". Zod must NOT coerce a truthy/
+ // falsy lookalike — a client sending the string 'false' has to be rejected
+ // outright rather than silently arriving as `true` and wiping the DB.
+ it.each([
+ ['the string "false"', 'false'],
+ ['the string "true"', 'true'],
+ ['the number 0', 0],
+ ['the number 1', 1],
+ ['null', null],
+ ['an object', {}]
+ ])('returns 400 VALIDATION_ERROR when dryRun is %s', async (_label, dryRun) => {
+ getSettings.mockResolvedValue({ backup: { destPath: '/dest' } });
+ const res = await request(buildApp())
+ .post('/api/backup/restore-db')
+ .send({ snapshotId: 'snap-1', dryRun });
+ expect(res.status).toBe(400);
+ expect(res.body.code).toBe('VALIDATION_ERROR');
+ expect(backup.restorePostgres).not.toHaveBeenCalled();
+ });
+
+ // Bypass probe: the rejections above must come from the schema rejecting
+ // those specific values, not from the request never reaching the handler.
+ it('accepts a well-formed body so the rejections above are not vacuous', async () => {
+ getSettings.mockResolvedValue({ backup: { destPath: '/dest' } });
+ backup.restorePostgres.mockResolvedValue({ status: 'ok', dryRun: false });
+ const res = await request(buildApp())
+ .post('/api/backup/restore-db')
+ .send({ snapshotId: 'snap-1', dryRun: false });
+ expect(res.status).toBe(200);
+ expect(backup.restorePostgres).toHaveBeenCalledWith('/dest', 'snap-1', { dryRun: false });
+ });
+
+ // restorePostgres reports domain failures in its resolved value rather than
+ // by throwing. The route must serialize those verbatim at 200 so the client
+ // can surface the reason — collapsing them into a 500 envelope would hide
+ // which check refused the restore.
+ it.each([
+ ['manifest_mismatch', { status: 'failed', reason: 'manifest_mismatch' }],
+ ['restore_error', { status: 'failed', reason: 'restore_error', error: 'psql: exited 1' }],
+ ['no_dump', { status: 'skipped', reason: 'no_dump' }],
+ ['not_configured', { status: 'skipped', reason: 'not_configured' }]
+ ])('returns the %s service result verbatim with a 200 status', async (_label, serviceResult) => {
+ getSettings.mockResolvedValue({ backup: { destPath: '/dest' } });
+ backup.restorePostgres.mockResolvedValue(serviceResult);
+ const res = await request(buildApp())
+ .post('/api/backup/restore-db')
+ .send({ snapshotId: 'snap-1', dryRun: false });
+ expect(res.status).toBe(200);
+ expect(res.body).toEqual(serviceResult);
+ });
});
});