From 7b0995274236adf3e17b55c33ad08a21968b5ce8 Mon Sep 17 00:00:00 2001 From: viralboterk Date: Wed, 2 Dec 2026 12:32:53 +0100 Subject: [PATCH] Fix duplicate plugin beforeQuery invocation and add registry regression tests (#71) --- src/plugin.test.ts | 84 ++++++++++++++++++++++++++++++++++++++++++++++ src/plugin.ts | 1 - 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/plugin.test.ts b/src/plugin.test.ts index 006e6df..b61bc5a 100644 --- a/src/plugin.test.ts +++ b/src/plugin.test.ts @@ -143,6 +143,90 @@ describe('StarbasePluginRegistry', () => { expect(result.sql).toBe('SELECT * FROM users /* modified */') }) + it('should call each plugin beforeQuery exactly once per query', async () => { + await registry.beforeQuery({ + sql: 'SELECT * FROM users', + params: [1], + }) + + expect(mockPlugin.beforeQuery).toHaveBeenCalledTimes(1) + }) + + it('should pass chained sql, params, dataSource and config to each plugin', async () => { + const dataSource = { source: 'internal' } as unknown as DataSource + const config = { role: 'admin' } as unknown as StarbaseDBConfiguration + + class RewritePlugin extends StarbasePlugin { + async beforeQuery(opts: { sql: string; params?: unknown[] }) { + return { sql: `${opts.sql} WHERE id = ?`, params: [42] } + } + } + + const first = new RewritePlugin('RewritePlugin') + const second = new TestPlugin('TestPlugin') + const firstSpy = vi.spyOn(first, 'beforeQuery') + const secondSpy = vi.spyOn(second, 'beforeQuery') + + const chained = new StarbasePluginRegistry({ + app: mockApp, + plugins: [first, second], + }) + + const result = await chained.beforeQuery({ + sql: 'SELECT * FROM users', + params: [-1], + dataSource, + config, + }) + + expect(firstSpy).toHaveBeenCalledTimes(1) + expect(firstSpy).toHaveBeenCalledWith({ + sql: 'SELECT * FROM users', + params: [-1], + dataSource, + config, + }) + expect(secondSpy).toHaveBeenCalledTimes(1) + expect(secondSpy).toHaveBeenCalledWith({ + sql: 'SELECT * FROM users WHERE id = ?', + params: [42], + dataSource, + config, + }) + expect(result).toEqual({ + sql: 'SELECT * FROM users WHERE id = ?', + params: [42], + }) + }) + + it('should return the original query when no plugins are registered', async () => { + const empty = new StarbasePluginRegistry({ app: mockApp, plugins: [] }) + + const result = await empty.beforeQuery({ + sql: 'SELECT 1', + params: [null], + }) + + expect(result).toEqual({ sql: 'SELECT 1', params: [null] }) + }) + + it('should propagate errors thrown by a plugin beforeQuery', async () => { + class FailingPlugin extends StarbasePlugin { + async beforeQuery(): Promise<{ sql: string; params?: unknown[] }> { + throw new Error('Query rejected') + } + } + + const failing = new StarbasePluginRegistry({ + app: mockApp, + plugins: [new FailingPlugin('FailingPlugin')], + }) + + await expect( + failing.beforeQuery({ sql: 'DELETE FROM users' }) + ).rejects.toThrowError('Query rejected') + }) + it('should call afterQuery on all plugins', async () => { const result = await registry.afterQuery({ sql: 'SELECT * FROM users', diff --git a/src/plugin.ts b/src/plugin.ts index f1879c1..a8be3ad 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -89,7 +89,6 @@ export class StarbasePluginRegistry { dataSource: opts.dataSource, config: opts.config, }) - await plugin.beforeQuery(opts) sql = modified.sql params = modified.params }