|
| 1 | +/** |
| 2 | + * Tests for lib/repo-intel/queries - typed wrappers over agent-analyzer |
| 3 | + * `repo-intel query <type>` subcommands. |
| 4 | + * |
| 5 | + * Verifies argv construction (the contract with the binary), error wrapping |
| 6 | + * (parse failures, missing artifact), and input validation. The binary |
| 7 | + * itself is mocked so these run hermetically in CI. |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +const fs = require('fs'); |
| 13 | +const path = require('path'); |
| 14 | +const os = require('os'); |
| 15 | + |
| 16 | +// Mock the binary module so runAnalyzer doesn't actually shell out. |
| 17 | +jest.mock('../lib/binary', () => ({ |
| 18 | + runAnalyzer: jest.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +const binary = require('../lib/binary'); |
| 22 | +const queries = require('../lib/repo-intel/queries'); |
| 23 | + |
| 24 | +describe('lib/repo-intel/queries', () => { |
| 25 | + let tempDir; |
| 26 | + let mapFilePath; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + binary.runAnalyzer.mockReset(); |
| 30 | + // Build a fake repo with the state dir + map file the queries look for. |
| 31 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'repo-intel-queries-test-')); |
| 32 | + fs.mkdirSync(path.join(tempDir, '.claude'), { recursive: true }); |
| 33 | + mapFilePath = path.join(tempDir, '.claude', 'repo-intel.json'); |
| 34 | + fs.writeFileSync(mapFilePath, '{}'); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + if (fs.existsSync(tempDir)) { |
| 39 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + // ─── Argv construction ──────────────────────────────────────────────────── |
| 44 | + // For each query, we mock the binary to return a known JSON value, call |
| 45 | + // the wrapper, then assert the exact argv passed through. This is the |
| 46 | + // load-bearing contract with the agent-analyzer CLI. |
| 47 | + |
| 48 | + describe('argv construction', () => { |
| 49 | + test('hotspots without limit', () => { |
| 50 | + binary.runAnalyzer.mockReturnValue('[]'); |
| 51 | + queries.hotspots(tempDir); |
| 52 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 53 | + expect(args.slice(0, 3)).toEqual(['repo-intel', 'query', 'hotspots']); |
| 54 | + expect(args).toContain('--map-file'); |
| 55 | + expect(args).toContain(mapFilePath); |
| 56 | + expect(args).toContain(tempDir); |
| 57 | + expect(args).not.toContain('--top'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('hotspots with limit adds --top', () => { |
| 61 | + binary.runAnalyzer.mockReturnValue('[]'); |
| 62 | + queries.hotspots(tempDir, { limit: 25 }); |
| 63 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 64 | + expect(args).toContain('--top'); |
| 65 | + expect(args[args.indexOf('--top') + 1]).toBe('25'); |
| 66 | + }); |
| 67 | + |
| 68 | + test('coupling forwards file argument', () => { |
| 69 | + binary.runAnalyzer.mockReturnValue('[]'); |
| 70 | + queries.coupling(tempDir, 'src/auth.js'); |
| 71 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 72 | + expect(args.slice(0, 4)).toEqual(['repo-intel', 'query', 'coupling', 'src/auth.js']); |
| 73 | + }); |
| 74 | + |
| 75 | + test('busFactor with adjustForAi adds --adjust-for-ai', () => { |
| 76 | + binary.runAnalyzer.mockReturnValue('{}'); |
| 77 | + queries.busFactor(tempDir, { adjustForAi: true }); |
| 78 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 79 | + expect(args).toContain('--adjust-for-ai'); |
| 80 | + }); |
| 81 | + |
| 82 | + test('testGaps with limit + minChanges adds both flags', () => { |
| 83 | + binary.runAnalyzer.mockReturnValue('[]'); |
| 84 | + queries.testGaps(tempDir, { limit: 5, minChanges: 3 }); |
| 85 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 86 | + expect(args[args.indexOf('--top') + 1]).toBe('5'); |
| 87 | + expect(args[args.indexOf('--min-changes') + 1]).toBe('3'); |
| 88 | + }); |
| 89 | + |
| 90 | + test('aiRatio with pathFilter adds --path-filter', () => { |
| 91 | + binary.runAnalyzer.mockReturnValue('{}'); |
| 92 | + queries.aiRatio(tempDir, { pathFilter: 'src/' }); |
| 93 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 94 | + expect(args[args.indexOf('--path-filter') + 1]).toBe('src/'); |
| 95 | + }); |
| 96 | + |
| 97 | + test('diffRisk joins files with comma', () => { |
| 98 | + binary.runAnalyzer.mockReturnValue('[]'); |
| 99 | + queries.diffRisk(tempDir, ['a.js', 'b.js', 'c.js']); |
| 100 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 101 | + expect(args[args.indexOf('--files') + 1]).toBe('a.js,b.js,c.js'); |
| 102 | + }); |
| 103 | + |
| 104 | + test('dependents with file scope adds --file', () => { |
| 105 | + binary.runAnalyzer.mockReturnValue('{}'); |
| 106 | + queries.dependents(tempDir, 'createUser', 'src/users.js'); |
| 107 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 108 | + expect(args).toContain('createUser'); |
| 109 | + expect(args[args.indexOf('--file') + 1]).toBe('src/users.js'); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + // ─── Phase 5 graph queries ──────────────────────────────────────────────── |
| 114 | + |
| 115 | + describe('graph queries (Phase 5.1)', () => { |
| 116 | + test('communities sends "communities" subcommand', () => { |
| 117 | + binary.runAnalyzer.mockReturnValue('[]'); |
| 118 | + queries.communities(tempDir); |
| 119 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 120 | + expect(args.slice(0, 3)).toEqual(['repo-intel', 'query', 'communities']); |
| 121 | + }); |
| 122 | + |
| 123 | + test('boundaries with limit adds --top', () => { |
| 124 | + binary.runAnalyzer.mockReturnValue('[]'); |
| 125 | + queries.boundaries(tempDir, { limit: 8 }); |
| 126 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 127 | + expect(args.slice(0, 3)).toEqual(['repo-intel', 'query', 'boundaries']); |
| 128 | + expect(args[args.indexOf('--top') + 1]).toBe('8'); |
| 129 | + }); |
| 130 | + |
| 131 | + test('areaOf forwards file argument', () => { |
| 132 | + binary.runAnalyzer.mockReturnValue('{}'); |
| 133 | + queries.areaOf(tempDir, 'src/foo.js'); |
| 134 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 135 | + expect(args.slice(0, 4)).toEqual(['repo-intel', 'query', 'area-of', 'src/foo.js']); |
| 136 | + }); |
| 137 | + |
| 138 | + test('communityHealth forwards id as string', () => { |
| 139 | + binary.runAnalyzer.mockReturnValue('{}'); |
| 140 | + queries.communityHealth(tempDir, 7); |
| 141 | + const args = binary.runAnalyzer.mock.calls[0][0]; |
| 142 | + expect(args.slice(0, 4)).toEqual(['repo-intel', 'query', 'community-health', '7']); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + // ─── Input validation ───────────────────────────────────────────────────── |
| 147 | + |
| 148 | + describe('input validation', () => { |
| 149 | + test('communityHealth rejects non-integer id', () => { |
| 150 | + expect(() => queries.communityHealth(tempDir, 'foo')).toThrow(TypeError); |
| 151 | + expect(() => queries.communityHealth(tempDir, null)).toThrow(TypeError); |
| 152 | + expect(() => queries.communityHealth(tempDir, 1.5)).toThrow(TypeError); |
| 153 | + expect(() => queries.communityHealth(tempDir, -1)).toThrow(TypeError); |
| 154 | + expect(binary.runAnalyzer).not.toHaveBeenCalled(); |
| 155 | + }); |
| 156 | + |
| 157 | + test('diffRisk rejects non-array files', () => { |
| 158 | + expect(() => queries.diffRisk(tempDir, 'a.js')).toThrow(TypeError); |
| 159 | + expect(() => queries.diffRisk(tempDir, [1, 2, 3])).toThrow(TypeError); |
| 160 | + expect(binary.runAnalyzer).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | + |
| 163 | + test('diffRisk rejects oversized file argument', () => { |
| 164 | + // 1000 paths × 50 chars each = 50000 chars, exceeds 30000 cap. |
| 165 | + const huge = Array.from({ length: 1000 }, (_, i) => `path/that/is/about/fifty-chars-long/file${i}.js`); |
| 166 | + expect(() => queries.diffRisk(tempDir, huge)).toThrow(RangeError); |
| 167 | + expect(binary.runAnalyzer).not.toHaveBeenCalled(); |
| 168 | + }); |
| 169 | + |
| 170 | + test('diffRisk accepts a normal-sized file argument', () => { |
| 171 | + binary.runAnalyzer.mockReturnValue('[]'); |
| 172 | + queries.diffRisk(tempDir, ['a.js', 'b.js']); |
| 173 | + expect(binary.runAnalyzer).toHaveBeenCalledTimes(1); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + // ─── Error wrapping ─────────────────────────────────────────────────────── |
| 178 | + |
| 179 | + describe('error wrapping', () => { |
| 180 | + test('throws RepoIntelMissingError when artifact is absent', () => { |
| 181 | + // Use a fresh dir with no .claude/repo-intel.json. |
| 182 | + const emptyDir = fs.mkdtempSync(path.join(os.tmpdir(), 'repo-intel-empty-')); |
| 183 | + try { |
| 184 | + let caught; |
| 185 | + try { |
| 186 | + queries.hotspots(emptyDir); |
| 187 | + } catch (err) { |
| 188 | + caught = err; |
| 189 | + } |
| 190 | + expect(caught).toBeInstanceOf(queries.RepoIntelMissingError); |
| 191 | + expect(caught.code).toBe('REPO_INTEL_MISSING'); |
| 192 | + expect(caught.mapFile).toContain('repo-intel.json'); |
| 193 | + expect(binary.runAnalyzer).not.toHaveBeenCalled(); |
| 194 | + } finally { |
| 195 | + fs.rmSync(emptyDir, { recursive: true, force: true }); |
| 196 | + } |
| 197 | + }); |
| 198 | + |
| 199 | + test('wraps binary failure with query context', () => { |
| 200 | + binary.runAnalyzer.mockImplementation(() => { |
| 201 | + throw new Error('binary blew up'); |
| 202 | + }); |
| 203 | + let caught; |
| 204 | + try { |
| 205 | + queries.bugspots(tempDir, { limit: 5 }); |
| 206 | + } catch (err) { |
| 207 | + caught = err; |
| 208 | + } |
| 209 | + expect(caught).toBeInstanceOf(Error); |
| 210 | + expect(caught.message).toContain('repo-intel query failed'); |
| 211 | + expect(caught.message).toContain('bugspots'); |
| 212 | + expect(caught.message).toContain('binary blew up'); |
| 213 | + expect(caught.cause).toBeDefined(); |
| 214 | + }); |
| 215 | + |
| 216 | + test('wraps non-JSON output with preview', () => { |
| 217 | + binary.runAnalyzer.mockReturnValue('not actually json'); |
| 218 | + let caught; |
| 219 | + try { |
| 220 | + queries.health(tempDir); |
| 221 | + } catch (err) { |
| 222 | + caught = err; |
| 223 | + } |
| 224 | + expect(caught).toBeInstanceOf(Error); |
| 225 | + expect(caught.message).toContain('non-JSON output'); |
| 226 | + expect(caught.message).toContain('health'); |
| 227 | + expect(caught.message).toContain('not actually json'); |
| 228 | + }); |
| 229 | + |
| 230 | + test('non-JSON output preview is truncated', () => { |
| 231 | + // 500 chars - should be truncated to 200 in the message. |
| 232 | + const big = 'x'.repeat(500); |
| 233 | + binary.runAnalyzer.mockReturnValue(big); |
| 234 | + let caught; |
| 235 | + try { |
| 236 | + queries.health(tempDir); |
| 237 | + } catch (err) { |
| 238 | + caught = err; |
| 239 | + } |
| 240 | + // The message contains exactly 200 x's, not 500. |
| 241 | + const xCount = (caught.message.match(/x/g) || []).length; |
| 242 | + expect(xCount).toBe(200); |
| 243 | + }); |
| 244 | + }); |
| 245 | +}); |
0 commit comments