|
| 1 | +/** |
| 2 | + * Tests for the CI/scripting fields `codegraph status --json` exposes (issue |
| 3 | + * #329): the `version`, `indexPath`, and `lastIndexed` fields, plus the |
| 4 | + * matching `CodeGraph.getLastIndexedAt()` library method. |
| 5 | + * |
| 6 | + * The CLI itself is exercised end-to-end against the built binary so the JSON |
| 7 | + * field names survive future refactors of the underlying plumbing. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 11 | +import { execFileSync } from 'child_process'; |
| 12 | +import * as fs from 'fs'; |
| 13 | +import * as path from 'path'; |
| 14 | +import * as os from 'os'; |
| 15 | +import { CodeGraph } from '../src'; |
| 16 | + |
| 17 | +const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js'); |
| 18 | +const PKG_VERSION = JSON.parse( |
| 19 | + fs.readFileSync(path.resolve(__dirname, '../package.json'), 'utf-8'), |
| 20 | +).version as string; |
| 21 | + |
| 22 | +function runStatusJson(cwd: string): Record<string, unknown> { |
| 23 | + const stdout = execFileSync(process.execPath, [BIN, 'status', '--json'], { |
| 24 | + cwd, |
| 25 | + encoding: 'utf-8', |
| 26 | + env: { ...process.env, CODEGRAPH_NO_DAEMON: '1' }, |
| 27 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 28 | + }); |
| 29 | + // JSON mode prints exactly one line to stdout; be defensive about any stray |
| 30 | + // leading output by parsing the last non-empty line. |
| 31 | + const line = stdout.trim().split('\n').filter(Boolean).pop()!; |
| 32 | + return JSON.parse(line); |
| 33 | +} |
| 34 | + |
| 35 | +describe('codegraph status --json — CI fields (#329)', () => { |
| 36 | + let tempDir: string; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-status-json-')); |
| 40 | + }); |
| 41 | + afterEach(() => { |
| 42 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('getLastIndexedAt() is null before indexing and a recent ms timestamp after', async () => { |
| 46 | + const cg = CodeGraph.initSync(tempDir); |
| 47 | + expect(cg.getLastIndexedAt()).toBeNull(); |
| 48 | + |
| 49 | + fs.writeFileSync(path.join(tempDir, 'a.ts'), 'export const x = 1;\n'); |
| 50 | + const before = Date.now(); |
| 51 | + await cg.indexAll(); |
| 52 | + const after = Date.now(); |
| 53 | + |
| 54 | + const last = cg.getLastIndexedAt(); |
| 55 | + expect(last).not.toBeNull(); |
| 56 | + expect(typeof last).toBe('number'); |
| 57 | + expect(last!).toBeGreaterThanOrEqual(before - 1000); |
| 58 | + expect(last!).toBeLessThanOrEqual(after + 1000); |
| 59 | + cg.close(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('status --json on an UNINITIALIZED project reports version + indexPath + lastIndexed:null', () => { |
| 63 | + const out = runStatusJson(tempDir); |
| 64 | + expect(out.initialized).toBe(false); |
| 65 | + expect(out.version).toBe(PKG_VERSION); |
| 66 | + expect(typeof out.indexPath).toBe('string'); |
| 67 | + expect(out.indexPath as string).toContain('.codegraph'); |
| 68 | + expect(out.lastIndexed).toBeNull(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('status --json on an INDEXED project reports version + indexPath + a round-trippable lastIndexed', async () => { |
| 72 | + fs.writeFileSync(path.join(tempDir, 'a.ts'), 'export const x = 1;\n'); |
| 73 | + const before = Date.now(); |
| 74 | + const cg = CodeGraph.initSync(tempDir); |
| 75 | + await cg.indexAll(); |
| 76 | + const after = Date.now(); |
| 77 | + cg.close(); |
| 78 | + |
| 79 | + const out = runStatusJson(tempDir); |
| 80 | + expect(out.initialized).toBe(true); |
| 81 | + expect(out.version).toBe(PKG_VERSION); |
| 82 | + expect(out.indexPath as string).toContain('.codegraph'); |
| 83 | + expect(typeof out.lastIndexed).toBe('string'); |
| 84 | + // ISO string that round-trips back into the index window. |
| 85 | + const ms = Date.parse(out.lastIndexed as string); |
| 86 | + expect(ms).toBeGreaterThanOrEqual(before - 1000); |
| 87 | + expect(ms).toBeLessThanOrEqual(after + 1000); |
| 88 | + }); |
| 89 | +}); |
0 commit comments