Python (codegraph-kernel/src/python.rs) and Go (src/go.rs) join the native kernel, mirroring the wasm extractors bug-for-bug. Python: decorated_definition docstrings/decorators (decorates refs only for bare-identifier decorators — the call-kind quirk), function-in-class → method, module-level assignments always extract as variable, from-import per-name binding refs, self.x fn-ref candidates as bare names. Go: receiver methods with Recv::name qualified names + contains edges to the first earlier struct of that name, type_spec struct/interface classification with embedding→extends and interface method nodes, composite-literal instantiates keeping the package qualifier, top-level var/const initializer walks attributed to the declared symbol (#693), 2-hop field chains (#1276), New().Method() re-encode (#645/#608), and the GO_SPEC fn-ref layers. Grammars: tree-sitter-python 0.23.6 + tree-sitter-go 0.23.4 crates, with wasm vendored from the same tags (parser.c sha-matched) — both were 2023-era in tree-sitter-wasms. Gates: extraction sweeps 100% (flask 83/83, django 3,035/3,038 +3 error-file deferrals, gin 99/99, prometheus 978/979 +1); full-init dump-diffs byte-identical on flask (10,833 rows), gin (17,540), django (360,794), and prometheus (213,758); torture fixtures enforced in npm test. DEFAULT_ROUTED now covers typescript/tsx/javascript/jsx/java/ python/go. Full suite: 2,471 tests pass. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
150 lines
6.1 KiB
TypeScript
150 lines
6.1 KiB
TypeScript
/**
|
|
* Kernel↔wasm TS/JS extraction parity (R2 of the kernel migration).
|
|
*
|
|
* Asserts the native walker (codegraph-kernel/src/tsjs/) produces the SAME
|
|
* ExtractionResult as the wasm TreeSitterExtractor — nodes, edges, and
|
|
* unresolved refs compared as canonicalized multisets — over:
|
|
* - the checked-in torture fixtures (every ported feature: components/HOCs,
|
|
* stores, RTK, vuex, fn-refs, value-ref shadowing, decorators, enums,
|
|
* type-alias members/tuple contracts, re-exports, JSX, field methods), and
|
|
* - this repo's own extraction sources (real-world TS).
|
|
*
|
|
* The full-repo sweep lives in scripts/kernel-parity.mjs (excalidraw et al.,
|
|
* run for the §5 gate); this suite keeps the invariant alive in `npm test`.
|
|
* Skips when no kernel binary is staged; CODEGRAPH_KERNEL_EXPECT=1 turns that
|
|
* into a failure (wired in kernel-scaffold.test.ts).
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import { extractFromSource } from '../src/extraction';
|
|
import { initGrammars, loadGrammarsForLanguages } from '../src/extraction/grammars';
|
|
import { tryKernelExtract, resetKernelForTests } from '../src/extraction/kernel';
|
|
import type { ExtractionResult, Language } from '../src/types';
|
|
|
|
const KERNEL_PATH = path.join(
|
|
__dirname,
|
|
'..',
|
|
'codegraph-kernel',
|
|
'prebuilds',
|
|
`${process.platform}-${process.arch}`,
|
|
'codegraph-kernel.node'
|
|
);
|
|
const kernelBuilt = fs.existsSync(KERNEL_PATH);
|
|
|
|
const FIXTURE_DIR = path.join(__dirname, 'fixtures', 'kernel-parity');
|
|
const REAL_SOURCES = [
|
|
'src/extraction/kernel/loader.ts',
|
|
'src/extraction/kernel/decode.ts',
|
|
'src/extraction/parse-pool.ts',
|
|
'src/extraction/function-ref.ts',
|
|
'src/mcp/tools.ts',
|
|
];
|
|
|
|
function canon(result: ExtractionResult): { nodes: string[]; edges: string[]; refs: string[] } {
|
|
return {
|
|
nodes: result.nodes
|
|
.map(({ updatedAt: _u, ...n }) => JSON.stringify(n, Object.keys(n).sort()))
|
|
.sort(),
|
|
edges: result.edges.map((e) => JSON.stringify(e, Object.keys(e).sort())).sort(),
|
|
refs: result.unresolvedReferences
|
|
.map((r) => JSON.stringify(r, Object.keys(r).sort()))
|
|
.sort(),
|
|
};
|
|
}
|
|
|
|
const ENV_KEYS = ['CODEGRAPH_KERNEL', 'CODEGRAPH_KERNEL_LANGS'] as const;
|
|
let savedEnv: Record<string, string | undefined>;
|
|
|
|
describe.skipIf(!kernelBuilt)('kernel TS/JS extraction parity', () => {
|
|
beforeAll(async () => {
|
|
await initGrammars();
|
|
await loadGrammarsForLanguages(['typescript', 'tsx', 'javascript', 'jsx', 'java', 'python', 'go']);
|
|
});
|
|
|
|
beforeEach(() => {
|
|
savedEnv = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
|
|
resetKernelForTests();
|
|
});
|
|
|
|
afterEach(() => {
|
|
for (const k of ENV_KEYS) {
|
|
if (savedEnv[k] === undefined) delete process.env[k];
|
|
else process.env[k] = savedEnv[k];
|
|
}
|
|
resetKernelForTests();
|
|
});
|
|
|
|
function assertParity(filePath: string, source: string, language: Language): void {
|
|
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
|
delete process.env.CODEGRAPH_KERNEL;
|
|
const viaKernel = tryKernelExtract(filePath, source, language);
|
|
expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
|
|
|
|
process.env.CODEGRAPH_KERNEL = '0';
|
|
const viaWasm = extractFromSource(filePath, source, language);
|
|
delete process.env.CODEGRAPH_KERNEL;
|
|
|
|
const k = canon(viaKernel!);
|
|
const w = canon(viaWasm);
|
|
expect(k.nodes, `${filePath}: nodes`).toEqual(w.nodes);
|
|
expect(k.edges, `${filePath}: edges`).toEqual(w.edges);
|
|
expect(k.refs, `${filePath}: refs`).toEqual(w.refs);
|
|
// Meaningful comparison, not empty-vs-empty.
|
|
expect(viaWasm.nodes.length).toBeGreaterThan(3);
|
|
}
|
|
|
|
it('torture fixture (tsx): components, stores, RTK, fn-refs, value-refs, decorators', () => {
|
|
const file = path.join(FIXTURE_DIR, 'torture.tsx');
|
|
assertParity('fixtures/torture.tsx', fs.readFileSync(file, 'utf8'), 'tsx');
|
|
});
|
|
|
|
it('torture fixture (js): field methods, wrappers, vuex module shape', () => {
|
|
const file = path.join(FIXTURE_DIR, 'torture.js');
|
|
assertParity('fixtures/torture.js', fs.readFileSync(file, 'utf8'), 'javascript');
|
|
});
|
|
|
|
it('torture fixture (java): Lombok, anonymous classes, method refs, chains', () => {
|
|
const file = path.join(FIXTURE_DIR, 'Torture.java');
|
|
assertParity('fixtures/Torture.java', fs.readFileSync(file, 'utf8'), 'java');
|
|
});
|
|
|
|
it('torture fixture (python): decorators, self fn-refs, imports, shadowing', () => {
|
|
const file = path.join(FIXTURE_DIR, 'torture.py');
|
|
assertParity('fixtures/torture.py', fs.readFileSync(file, 'utf8'), 'python');
|
|
});
|
|
|
|
it('torture fixture (go): receivers, embedding, interfaces, composite literals', () => {
|
|
const file = path.join(FIXTURE_DIR, 'torture.go');
|
|
assertParity('fixtures/torture.go', fs.readFileSync(file, 'utf8'), 'go');
|
|
});
|
|
|
|
it.each(REAL_SOURCES)('real source parity: %s', (rel) => {
|
|
const file = path.join(__dirname, '..', rel);
|
|
assertParity(rel, fs.readFileSync(file, 'utf8'), 'typescript');
|
|
});
|
|
|
|
it('files with parse errors defer to the wasm extractor (recovery is encoding-dependent)', () => {
|
|
// tree-sitter error RECOVERY differs between UTF-8 (native) and UTF-16
|
|
// (web-tree-sitter) parsing — same grammar, same core version — so the
|
|
// kernel defers any erroring file to keep routing graph-neutral.
|
|
const broken = 'export function f( {\n return }} 12 (\n';
|
|
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
|
delete process.env.CODEGRAPH_KERNEL;
|
|
expect(tryKernelExtract('src/broken.ts', broken, 'typescript')).toBeNull();
|
|
// The seam still serves the file — through the wasm path.
|
|
process.env.CODEGRAPH_KERNEL = '0';
|
|
const viaWasm = extractFromSource('src/broken.ts', broken, 'typescript');
|
|
delete process.env.CODEGRAPH_KERNEL;
|
|
expect(viaWasm.nodes.some((n) => n.kind === 'file')).toBe(true);
|
|
});
|
|
|
|
it('typescript fixture parsed as plain typescript variant', () => {
|
|
// Same content through the non-tsx grammar exercises the typescript
|
|
// (vs tsx) LangSpec pairing.
|
|
const file = path.join(__dirname, '..', 'src/extraction/kernel/index.ts');
|
|
assertParity('src/extraction/kernel/index.ts', fs.readFileSync(file, 'utf8'), 'typescript');
|
|
});
|
|
});
|