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 restorePostgres psql spawn errors, corrupt manifests, and transaction flags (#3918)

[._.]/ Adam Eivy474bed522a90
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index da57fe4d02..c9b74c7b91 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -39,6 +39,8 @@
## Changed
+- `restorePostgres` now has tests for the three paths a PostgreSQL restore can go wrong on. A missing `psql` binary surfaces as a spawn `'error'` event rather than a `'close'`, so without its listener the restore promise would never settle and the UI would hang — the suite now drives that emit and pins the `{ status: 'failed', reason: 'restore_error' }` result. The `--single-transaction` / `ON_ERROR_STOP=1` pair is what makes a failed replay roll back instead of leaving the live database half-dropped, so both flags (and the `-v` flag's actual value, not just its presence) are asserted, along with the `portos` PGPASSWORD default and an explicit `PGPASSWORD` overriding it. A `manifest.json` that exists but is unparseable reads as absent — `readJSONFile` returns its `null` default, so hash verification is skipped and the restore proceeds; that is now pinned in both the dry-run and real-restore directions, so changing it to a hard failure has to be a deliberate decision.
+
- The Story Builder's footer "Next >" button now says why it can't be clicked. It was inert whenever the active step was unlocked or had gone stale, but carried no tooltip and no accessible description — and since the step rail beside it is deliberately un-gated, a dead Next read as a bug rather than as guidance. It now carries a `title` plus a screen-reader hint naming the actual blocker ("Lock this step to advance to the next step." vs. "An upstream step changed — re-lock this step to advance."), and uses `aria-disabled` instead of `disabled` so keyboard and screen-reader users can still reach the button and hear the reason.
- The AI provider/model dropdown on the Digital Twin personality panel — the one shown before any traits have been analyzed — now carries a screen-reader label. It sat in a centered row next to the Analyze Traits button with no `id`, no `<label htmlFor>` and no `aria-label`, so focusing it announced only an option string like "Example Provider - example-model" with nothing saying what the choice was for. A visually hidden `<label htmlFor="personality-model-select">AI Provider & Model</label>` now names it without changing the layout.
diff --git a/server/services/backup.test.js b/server/services/backup.test.js
index 6c52065693..2bcae751a8 100644
--- a/server/services/backup.test.js
+++ b/server/services/backup.test.js
@@ -575,6 +575,98 @@ describe('restorePostgres', () => {
expect(result.status).toBe('ok');
expect(result.tableCount).toBe(1);
});
+
+ // A manifest.json that is PRESENT but unparseable reads the same as absent:
+ // readJSONFile returns its `null` default, so there is no expected hash to
+ // compare against and verification is skipped rather than hard-failing.
+ // Pinning that here so a future "throw on corrupt manifest" change is a
+ // deliberate decision instead of a silent behavior swap.
+ it('skips verification (does not throw) when manifest.json is malformed JSON', async () => {
+ vi.spyOn(fs, 'stat').mockResolvedValue({ size: DUMP_SQL.length, isFile: () => true });
+ vi.spyOn(fs, 'readFile').mockImplementation(async (p) => (
+ String(p).endsWith('manifest.json') ? '{ "files": { ' : DUMP_SQL
+ ));
+ const result = await restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: true });
+ expect(result).toEqual({ status: 'ok', dryRun: true, sizeBytes: DUMP_SQL.length, tableCount: 1 });
+ });
+
+ // A malformed manifest must not become a free pass for a real restore either:
+ // the replay still happens (verification skipped), so assert it reaches psql
+ // rather than silently returning skipped.
+ it('still runs a real restore when manifest.json is malformed JSON', async () => {
+ vi.spyOn(fs, 'stat').mockResolvedValue({ size: DUMP_SQL.length, isFile: () => true });
+ vi.spyOn(fs, 'readFile').mockImplementation(async (p) => (
+ String(p).endsWith('manifest.json') ? 'not json at all' : DUMP_SQL
+ ));
+ checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
+ const proc = fakeProc();
+ spawn.mockReturnValue(proc);
+ const p = restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: false });
+ await flush();
+ proc.emit('close', 0);
+ const result = await p;
+ expect(result.status).toBe('ok');
+ expect(spawn).toHaveBeenCalledTimes(1);
+ });
+
+ // psql missing from the host (ENOENT) surfaces as a spawn 'error' event, not a
+ // 'close'. Without the error listener the promise would never settle and the
+ // restore UI would hang forever — assert it resolves as a structured failure.
+ it('resolves failed/restore_error when the psql spawn emits an error event', async () => {
+ vi.spyOn(fs, 'stat').mockResolvedValue({ size: 4096, isFile: () => true });
+ vi.spyOn(fs, 'readFile').mockResolvedValue('CREATE TABLE a (...);\n');
+ checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
+ const proc = fakeProc();
+ spawn.mockReturnValue(proc);
+ const p = restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: false });
+ await flush();
+ proc.emit('error', new Error('spawn psql ENOENT'));
+ await expect(p).resolves.toEqual({
+ status: 'failed',
+ reason: 'restore_error',
+ error: 'spawn psql ENOENT'
+ });
+ });
+
+ // The atomicity contract: ON_ERROR_STOP=1 aborts on the first failed statement
+ // and --single-transaction rolls the whole replay back, so a failed restore
+ // leaves the live DB untouched instead of half-dropped. Both flags are load
+ // bearing — a refactor that drops either turns a failed restore into data loss.
+ it('passes --single-transaction and ON_ERROR_STOP=1 with the default PGPASSWORD', async () => {
+ vi.stubEnv('PGPASSWORD', '');
+ vi.spyOn(fs, 'stat').mockResolvedValue({ size: 4096, isFile: () => true });
+ vi.spyOn(fs, 'readFile').mockResolvedValue('CREATE TABLE a (...);\n');
+ checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
+ const proc = fakeProc();
+ spawn.mockReturnValue(proc);
+ const p = restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: false });
+ await flush();
+ proc.emit('close', 0);
+ await p;
+ const [bin, args, opts] = spawn.mock.calls[0];
+ expect(bin).toBe('psql');
+ expect(args).toContain('--single-transaction');
+ // Assert the flag/value pairing, not just membership: '-v' followed by
+ // something else would still satisfy arrayContaining.
+ expect(args[args.indexOf('-v') + 1]).toBe('ON_ERROR_STOP=1');
+ expect(opts.env.PGPASSWORD).toBe('portos');
+ vi.unstubAllEnvs();
+ });
+
+ it('prefers an explicit PGPASSWORD over the portos default', async () => {
+ vi.stubEnv('PGPASSWORD', 'from-env');
+ vi.spyOn(fs, 'stat').mockResolvedValue({ size: 4096, isFile: () => true });
+ vi.spyOn(fs, 'readFile').mockResolvedValue('CREATE TABLE a (...);\n');
+ checkHealth.mockResolvedValue({ connected: true, hasSchema: true });
+ const proc = fakeProc();
+ spawn.mockReturnValue(proc);
+ const p = restorePostgres('/dest', '2026-06-05T00-00-00', { dryRun: false });
+ await flush();
+ proc.emit('close', 0);
+ await p;
+ expect(spawn.mock.calls[0][2].env.PGPASSWORD).toBe('from-env');
+ vi.unstubAllEnvs();
+ });
});
describe('runBackup pg status propagation', () => {