Skip to content

Fix duplicate plugin beforeQuery invocation and add registry regression tests (#71) - #367

Open
viralboterk wants to merge 1 commit into
outerbase:mainfrom
viralboterk:fix-issue-71
Open

viralboterk wants to merge 1 commit into
outerbase:mainfrom
viralboterk:fix-issue-71

Conversation

@viralboterk

Copy link
Copy Markdown

What was broken

StarbasePluginRegistry.beforeQuery() (src/plugin.ts) called each plugin's
beforeQuery hook twice per query: once with the chained sql/params
(whose result was used), and a second time with the original, unchained opts
(whose result was discarded). Consequences:

  • Every plugin hook ran twice for every query, doubling any side effects
    (logging, counters, external calls, caching, etc.).
  • The second call received the original SQL/params instead of the output of
    the previous plugin, so a plugin could observe a query that was never
    actually executed.
  • A plugin that throws on the original query (e.g. a guard) could reject a
    query that an earlier plugin had already rewritten into an acceptable one.

The existing test only asserted toHaveBeenCalled(), so the bug went unnoticed.

What changes

  • src/plugin.ts: remove the stray await plugin.beforeQuery(opts) call so each
    hook runs exactly once, with the chained input.
  • src/plugin.test.ts: add meaningful regression tests for the registry's
    beforeQuery chain, as part of the Improve test coverage with Vitest #71 test coverage effort:
    • each plugin hook is called exactly once per query;
    • the second plugin receives the SQL/params produced by the first plugin
      (including a -1 param being rewritten), plus the untouched dataSource
      and config;
    • an empty registry returns the query unchanged (including a null param);
    • errors thrown by a plugin hook propagate to the caller.

No other production behavior changes.

How it is tested

pnpm test (Vitest) picks up the updated src/plugin.test.ts. The new
"exactly once" and "chained input" tests fail on the previous implementation
and pass with the fix.

Fixes #71

/claim #71

Note: this change was prepared with the help of an AI coding assistant. Happy to adjust anything you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve test coverage with Vitest

1 participant