synthesis-progress.test.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Progress reporting for the callback-edge synthesis tail.
  3. *
  4. * Synthesis runs AFTER the resolution bar reaches 100%, so before this it had
  5. * no progress surface at all — on synthesizer-heavy repos (e.g. large C
  6. * codebases hitting the fn-pointer pass) the CLI sat frozen at
  7. * "Resolving refs 100%" long enough that users concluded the index hung and
  8. * killed it. These tests pin (a) that indexing emits the dedicated 'linking'
  9. * phase with monotonic per-pass progress, and (b) that the advertised step
  10. * total stays in sync with the synthesizer's actual pass list.
  11. */
  12. import { describe, it, expect } from 'vitest';
  13. import * as fs from 'fs';
  14. import * as os from 'os';
  15. import * as path from 'path';
  16. import { CodeGraph, IndexProgress } from '../src/index';
  17. import { SYNTH_PROGRESS_STEPS } from '../src/resolution/callback-synthesizer';
  18. describe('synthesis progress ("Linking dynamic dispatch" phase)', () => {
  19. it('SYNTH_PROGRESS_STEPS matches the synthesizer’s actual __mark() step count', () => {
  20. // The constant is cosmetic (progress denominator), but drift makes the bar
  21. // end early or jump to 100% — adding a pass must bump it. Every step site
  22. // calls __mark('<label>') with a string literal, so count those.
  23. const src = fs.readFileSync(
  24. path.join(__dirname, '../src/resolution/callback-synthesizer.ts'),
  25. 'utf8'
  26. );
  27. const stepSites = (src.match(/__mark\('/g) ?? []).length;
  28. expect(SYNTH_PROGRESS_STEPS).toBe(stepSites);
  29. });
  30. it('indexing emits a monotonic linking phase ending at the full step count', async () => {
  31. const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-synth-progress-'));
  32. try {
  33. fs.writeFileSync(path.join(dir, 'a.ts'), 'export function helper() { return 1; }\n');
  34. fs.writeFileSync(
  35. path.join(dir, 'b.ts'),
  36. "import { helper } from './a';\nexport function main() { return helper(); }\n"
  37. );
  38. const events: IndexProgress[] = [];
  39. const cg = await CodeGraph.init(dir, {
  40. index: true,
  41. onProgress: (p) => events.push(p),
  42. });
  43. await cg.close();
  44. const linking = events.filter((e) => e.phase === 'linking');
  45. expect(linking.length).toBeGreaterThan(0);
  46. // Emitted up-front so the phase label flips as soon as synthesis starts…
  47. expect(linking[0]!.current).toBe(0);
  48. // …and every step reports against the same total, monotonically.
  49. expect(linking.every((e) => e.total === SYNTH_PROGRESS_STEPS)).toBe(true);
  50. for (let i = 1; i < linking.length; i++) {
  51. expect(linking[i]!.current).toBeGreaterThanOrEqual(linking[i - 1]!.current);
  52. }
  53. expect(linking[linking.length - 1]!.current).toBe(SYNTH_PROGRESS_STEPS);
  54. // The linking phase comes after resolution has finished.
  55. const lastResolving = events.map((e) => e.phase).lastIndexOf('resolving');
  56. const firstLinking = events.map((e) => e.phase).indexOf('linking');
  57. expect(firstLinking).toBeGreaterThan(lastResolving);
  58. } finally {
  59. fs.rmSync(dir, { recursive: true, force: true });
  60. }
  61. });
  62. });