From e3ca2dc1d5b550753311cedd92a27b9ec78ae109 Mon Sep 17 00:00:00 2001 From: agent_coder Date: Sun, 12 Jul 2026 00:37:24 +0300 Subject: [PATCH] =?UTF-8?q?test(db):=20executeTx=20=E2=80=94=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BA=D1=80=D1=8B=D1=82=D1=8C=20rollback-=D0=B2=D0=B5?= =?UTF-8?q?=D1=82=D0=BA=D1=83=20after-commit-=D1=85=D1=83=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ревью #516: fakeDb.execute всегда пушил commit — моделировался только commit-путь, несущий негатив (тело кидает → tx реджектит → дренаж после awaited-tx не бежит → хук НЕ фаerr) не был заперт. Исходник корректен, это была дыра в тесте. Добавлен тест: тело бросает → executeTx реджектит, commit не случился, хук не побежал. Mutation-verify: перенос дренажа в finally → тест краснеет. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../database/execute-tx-after-commit.spec.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/apps/server/src/database/execute-tx-after-commit.spec.ts b/apps/server/src/database/execute-tx-after-commit.spec.ts index bb86d445..29495f13 100644 --- a/apps/server/src/database/execute-tx-after-commit.spec.ts +++ b/apps/server/src/database/execute-tx-after-commit.spec.ts @@ -88,4 +88,26 @@ describe('executeTx post-commit hooks', () => { // The throwing hook is swallowed; a later hook still runs. expect(log).toEqual(['commit', 'second-hook-still-runs']); }); + + it('does NOT run afterCommit hooks when the transaction body throws (rollback)', async () => { + // The body rejects -> the fake transaction never pushes 'commit' and + // db.transaction().execute() rejects, mirroring a real rolled-back tx. The + // drain runs only AFTER the awaited (committed) transaction, so a rollback + // must leave every registered hook UN-run — otherwise a cache-bust / event + // would fire for a write that never landed. + const log: string[] = []; + const { db } = fakeDb(log); + const hook = jest.fn(); + + await expect( + executeTx(db, async (trx) => { + registerAfterCommit(trx, hook); + throw new Error('write failed -> rollback'); + }), + ).rejects.toThrow('write failed -> rollback'); + + // No commit happened, and the post-commit hook never ran. + expect(log).toEqual([]); // no 'commit' + expect(hook).not.toHaveBeenCalled(); + }); });