From 61464c177bc37bde118cd2302cec16fe7393446e Mon Sep 17 00:00:00 2001 From: root Date: Tue, 12 May 2026 17:17:52 +0800 Subject: [PATCH] test: cover plugin registry beforeQuery chaining --- src/plugin.test.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++++ src/plugin.ts | 1 - 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/plugin.test.ts b/src/plugin.test.ts index 006e6df..87ef7b7 100644 --- a/src/plugin.test.ts +++ b/src/plugin.test.ts @@ -92,6 +92,57 @@ describe('StarbasePlugin', () => { expect(result.sql).toBe('[B] [A] SELECT * FROM users') }) + + it('should call each beforeQuery hook once with the previous hook output', async () => { + const firstPlugin = new TestPlugin('FirstPlugin') + const secondPlugin = new TestPlugin('SecondPlugin') + + const firstBeforeQuery = vi + .spyOn(firstPlugin, 'beforeQuery') + .mockResolvedValue({ + sql: 'SELECT * FROM users WHERE active = ?', + params: [1], + }) + const secondBeforeQuery = vi + .spyOn(secondPlugin, 'beforeQuery') + .mockResolvedValue({ + sql: 'SELECT * FROM users WHERE active = ? LIMIT ?', + params: [1, 10], + }) + + const dataSource = { source: 'internal' } as DataSource + const config = { role: 'admin' } as StarbaseDBConfiguration + const registry = new StarbasePluginRegistry({ + app: mockApp, + plugins: [firstPlugin, secondPlugin], + }) + + const result = await registry.beforeQuery({ + sql: 'SELECT * FROM users', + params: [], + dataSource, + config, + }) + + expect(firstBeforeQuery).toHaveBeenCalledTimes(1) + expect(firstBeforeQuery).toHaveBeenCalledWith({ + sql: 'SELECT * FROM users', + params: [], + dataSource, + config, + }) + expect(secondBeforeQuery).toHaveBeenCalledTimes(1) + expect(secondBeforeQuery).toHaveBeenCalledWith({ + sql: 'SELECT * FROM users WHERE active = ?', + params: [1], + dataSource, + config, + }) + expect(result).toEqual({ + sql: 'SELECT * FROM users WHERE active = ? LIMIT ?', + params: [1, 10], + }) + }) }) describe('StarbasePluginRegistry', () => { 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 }