redux-thunk-synthesizer.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  2. import * as fs from 'node:fs';
  3. import * as path from 'node:path';
  4. import * as os from 'node:os';
  5. import { CodeGraph } from '../src';
  6. /**
  7. * End-to-end test for the redux-thunk dispatch-chain synthesizer.
  8. *
  9. * `createAsyncThunk(prefix, async (a, api) => {...})` passes the async body as an argument, so
  10. * tree-sitter never makes it its own function node — the thunk `constant`'s body calls (incl.
  11. * `dispatch(nextThunk(...))`) are orphaned and `callees(thunk)` is empty. Verify the synthesizer
  12. * body-scans each thunk constant and links it → each dispatched thunk, so the chain
  13. * `outer → inner → deep` connects end-to-end; and that a non-thunk constant is skipped.
  14. */
  15. describe('redux-thunk synthesizer', () => {
  16. let dir: string;
  17. beforeEach(() => {
  18. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'redux-thunk-fixture-'));
  19. });
  20. afterEach(() => {
  21. fs.rmSync(dir, { recursive: true, force: true });
  22. });
  23. it('links each thunk constant to the thunks it dispatches, and skips non-thunks', async () => {
  24. fs.writeFileSync(
  25. path.join(dir, 'package.json'),
  26. JSON.stringify({ name: 'app', dependencies: { '@reduxjs/toolkit': '^2' } })
  27. );
  28. fs.writeFileSync(
  29. path.join(dir, 'thunks.ts'),
  30. `import { createAsyncThunk } from '@reduxjs/toolkit';
  31. export const deepThunk = createAsyncThunk('app/deep', async (n: number) => {
  32. return n * 2;
  33. });
  34. export const innerThunk = createAsyncThunk('app/inner', async (n: number, { dispatch }) => {
  35. return dispatch(deepThunk(n));
  36. });
  37. export const outerThunk = createAsyncThunk('app/outer', async (n: number, { dispatch }) => {
  38. await dispatch(innerThunk(n));
  39. });
  40. // Non-thunk constant that only MENTIONS dispatch in a string — must be skipped.
  41. export const notAThunk = 'dispatch(innerThunk())';
  42. `
  43. );
  44. const cg = await CodeGraph.init(dir, { silent: true });
  45. await cg.indexAll();
  46. const db = (cg as any).db.db;
  47. const rows = db
  48. .prepare(
  49. `SELECT s.name source_name, s.kind source_kind, t.name target_name,
  50. json_extract(e.metadata,'$.via') via,
  51. json_extract(e.metadata,'$.registeredAt') registeredAt
  52. FROM edges e
  53. JOIN nodes s ON s.id = e.source
  54. JOIN nodes t ON t.id = e.target
  55. WHERE json_extract(e.metadata,'$.synthesizedBy') = 'redux-thunk'`
  56. )
  57. .all();
  58. cg.close?.();
  59. // The dispatch chain connects: outer → inner → deep.
  60. const pairs = new Set(rows.map((r: any) => `${r.source_name}>${r.target_name}`));
  61. expect(pairs.has('outerThunk>innerThunk')).toBe(true);
  62. expect(pairs.has('innerThunk>deepThunk')).toBe(true);
  63. // Sources are thunk constants; the non-thunk string constant is never a source.
  64. expect(rows.every((r: any) => r.source_kind === 'constant')).toBe(true);
  65. expect(rows.some((r: any) => r.source_name === 'notAThunk')).toBe(false);
  66. // Edges are 'calls' with the wiring site surfaced for the agent.
  67. const outer = rows.find((r: any) => r.source_name === 'outerThunk');
  68. expect(outer.via).toBe('innerThunk');
  69. expect(outer.registeredAt).toMatch(/thunks\.ts:\d+/);
  70. });
  71. });