|
1 | | -import { describe, expect, it } from 'vitest'; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | 2 | import { louvainCommunities } from '../../../src/graph/algorithms/louvain.js'; |
3 | 3 | import { CodeGraph } from '../../../src/graph/model.js'; |
| 4 | +import { setVerbose } from '../../../src/infrastructure/logger.js'; |
4 | 5 |
|
5 | 6 | describe('louvainCommunities', () => { |
6 | 7 | it('returns empty for empty graph', () => { |
@@ -45,4 +46,68 @@ describe('louvainCommunities', () => { |
45 | 46 | expect(assignments.size).toBe(0); |
46 | 47 | expect(modularity).toBe(0); |
47 | 48 | }); |
| 49 | + |
| 50 | + describe('Leiden-knob parity logging', () => { |
| 51 | + let stderrSpy: ReturnType<typeof vi.spyOn>; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + stderrSpy.mockRestore(); |
| 59 | + setVerbose(false); |
| 60 | + }); |
| 61 | + |
| 62 | + function buildTwoClusterGraph(): CodeGraph { |
| 63 | + const g = new CodeGraph(); |
| 64 | + g.addEdge('a', 'b'); |
| 65 | + g.addEdge('b', 'c'); |
| 66 | + g.addEdge('c', 'a'); |
| 67 | + g.addEdge('x', 'y'); |
| 68 | + g.addEdge('y', 'z'); |
| 69 | + g.addEdge('z', 'x'); |
| 70 | + g.addEdge('c', 'x'); |
| 71 | + return g; |
| 72 | + } |
| 73 | + |
| 74 | + // Regression guard: DEFAULTS.community always populates maxLevels/maxLocalPasses/ |
| 75 | + // refinementTheta, so forwarding config values used to emit `[codegraph WARN]` on |
| 76 | + // every communities computation. Keep the parity note at debug level — never warn. |
| 77 | + it('never emits the parity message at WARN level when config defaults are forwarded', () => { |
| 78 | + const g = buildTwoClusterGraph(); |
| 79 | + louvainCommunities(g, { |
| 80 | + maxLevels: 50, |
| 81 | + maxLocalPasses: 20, |
| 82 | + refinementTheta: 1.0, |
| 83 | + }); |
| 84 | + |
| 85 | + const warnWrites = stderrSpy.mock.calls |
| 86 | + .map(([chunk]) => (typeof chunk === 'string' ? chunk : (chunk?.toString?.() ?? ''))) |
| 87 | + .filter((line) => line.includes('[codegraph WARN]')); |
| 88 | + expect(warnWrites).toEqual([]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('emits the parity message at DEBUG level when verbose is enabled and Leiden knobs are set', () => { |
| 92 | + setVerbose(true); |
| 93 | + const g = buildTwoClusterGraph(); |
| 94 | + louvainCommunities(g, { |
| 95 | + maxLevels: 50, |
| 96 | + maxLocalPasses: 20, |
| 97 | + refinementTheta: 1.0, |
| 98 | + }); |
| 99 | + |
| 100 | + const writes = stderrSpy.mock.calls |
| 101 | + .map(([chunk]) => (typeof chunk === 'string' ? chunk : (chunk?.toString?.() ?? ''))) |
| 102 | + .join(''); |
| 103 | + // Message is only emitted on the native path. When native is unavailable we at |
| 104 | + // least assert no WARN was emitted (covered by the previous test); when it is |
| 105 | + // available, it must go through the DEBUG channel and never WARN. |
| 106 | + expect(writes).not.toContain('[codegraph WARN]'); |
| 107 | + // Allow either outcome for DEBUG depending on engine availability. |
| 108 | + if (writes.includes('maxLevels/maxLocalPasses/refinementTheta')) { |
| 109 | + expect(writes).toContain('[codegraph DEBUG]'); |
| 110 | + } |
| 111 | + }); |
| 112 | + }); |
48 | 113 | }); |
0 commit comments