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: prove withTransaction client refuses row writes to a non-test database (#11872)

Adam Eivye72e18a3e1fd
diff --git a/server/lib/db.transaction.test.js b/server/lib/db.transaction.test.js
index 5bf4169978..0ef7929d86 100644
--- a/server/lib/db.transaction.test.js
+++ b/server/lib/db.transaction.test.js
@@ -1,4 +1,4 @@
-import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const pool = vi.hoisted(() => ({
client: null,
@@ -24,6 +24,10 @@ function makeClient(query) {
};
}
+afterEach(() => {
+ vi.unstubAllEnvs();
+});
+
beforeEach(() => {
vi.clearAllMocks();
pool.connect.mockImplementation(async () => pool.client);
@@ -111,6 +115,36 @@ describe('withTransaction', () => {
});
});
+// Cross-reference: db.guards.test.js covers assertWriteAllowed() directly; this
+// proves the withTransaction() client proxy actually routes writes through it.
+describe('withTransaction non-test database write guard', () => {
+ beforeEach(() => {
+ vi.stubEnv('PGDATABASE', 'portos');
+ vi.stubEnv('TEST_DB_OK', '');
+ });
+
+ it.each([
+ ['string DELETE', 'DELETE FROM universes'],
+ ['{ text } INSERT', { text: 'INSERT INTO x VALUES (1)' }],
+ ])('refuses a %s and never forwards it to the pg client', async (_label, statement) => {
+ const client = makeClient(vi.fn().mockResolvedValue({}));
+ pool.client = client;
+
+ await expect(withTransaction(c => c.query(statement))).rejects.toThrow(/Refusing to mutate/);
+
+ expect(client.query.mock.calls).toEqual([['BEGIN'], ['ROLLBACK']]);
+ });
+
+ it('still lets a SELECT through the guarded client', async () => {
+ const client = makeClient(vi.fn().mockResolvedValue({ rows: [] }));
+ pool.client = client;
+
+ await withTransaction(c => c.query('SELECT 1'));
+
+ expect(client.query.mock.calls).toEqual([['BEGIN'], ['SELECT 1'], ['COMMIT']]);
+ });
+});
+
describe('database restore admission', () => {
it('drains detached nested work that outlives its admitted parent', async () => {