kernel-csharp-parity.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * Kernel↔wasm C# extraction parity (R7b of the kernel migration).
  3. *
  4. * Asserts the native walker (codegraph-kernel/src/csharp.rs) produces the
  5. * SAME ExtractionResult as the wasm TreeSitterExtractor — nodes, edges, and
  6. * unresolved refs compared as canonicalized multisets — over the checked-in
  7. * torture fixtures:
  8. *
  9. * - Torture.cs — block namespace + nested/second-namespace quirks,
  10. * base_list shapes, records, properties (incl. the bare-identifier
  11. * signature loss and never-walked accessor bodies), fields/constants,
  12. * events/operators/indexer/destructor (no nodes, calls → class), ctor
  13. * initializer hole, explicit interface impl, local functions, the call
  14. * zoo (raw member-access texts, chained re-encode, `(myDel)(x)` conv,
  15. * `nameof`), instantiation shapes (incl. invisible `new()`/`new {}`/
  16. * arrays), static value reads, C# type refs, fn-ref candidates
  17. * (`+=` subscription, `this.X` bare-name form, initializer lists),
  18. * value-ref targets + local shadow prune, preprocessor passthrough.
  19. * - TortureFileScoped.cs — file-scoped namespace, alias-import quirks,
  20. * positional records with base args (`BaseDto(Name)` full-text extends),
  21. * C#12 primary-ctor base args (`(repo)` garbage extends preserved).
  22. * - TortureTopLevel.cs — top-level statements (zero-emission locals),
  23. * top-level local function, trailing partial class.
  24. *
  25. * CRLF variants are derived in-memory (#1329 docstring semantics). The
  26. * full-repo sweep lives in scripts/kernel-parity.mjs (serilog /
  27. * Newtonsoft.Json / jellyfin for the §5 gate); this suite keeps the invariant
  28. * alive in `npm test`. Skips when no kernel binary is staged;
  29. * CODEGRAPH_KERNEL_EXPECT=1 turns that into a failure (kernel-scaffold.test.ts).
  30. */
  31. import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
  32. import * as fs from 'fs';
  33. import * as path from 'path';
  34. import { extractFromSource } from '../src/extraction';
  35. import { initGrammars, loadGrammarsForLanguages } from '../src/extraction/grammars';
  36. import { tryKernelExtract, resetKernelForTests } from '../src/extraction/kernel';
  37. import type { ExtractionResult } from '../src/types';
  38. const KERNEL_PATH = path.join(
  39. __dirname,
  40. '..',
  41. 'codegraph-kernel',
  42. 'prebuilds',
  43. `${process.platform}-${process.arch}`,
  44. 'codegraph-kernel.node'
  45. );
  46. const kernelBuilt = fs.existsSync(KERNEL_PATH);
  47. const FIXTURE_DIR = path.join(__dirname, 'fixtures', 'kernel-parity');
  48. function canon(result: ExtractionResult): { nodes: string[]; edges: string[]; refs: string[] } {
  49. return {
  50. nodes: result.nodes
  51. .map(({ updatedAt: _u, ...n }) => JSON.stringify(n, Object.keys(n).sort()))
  52. .sort(),
  53. edges: result.edges.map((e) => JSON.stringify(e, Object.keys(e).sort())).sort(),
  54. refs: result.unresolvedReferences
  55. .map((r) => JSON.stringify(r, Object.keys(r).sort()))
  56. .sort(),
  57. };
  58. }
  59. const ENV_KEYS = ['CODEGRAPH_KERNEL', 'CODEGRAPH_KERNEL_LANGS'] as const;
  60. let savedEnv: Record<string, string | undefined>;
  61. describe.skipIf(!kernelBuilt)('kernel C# extraction parity', () => {
  62. beforeAll(async () => {
  63. await initGrammars();
  64. await loadGrammarsForLanguages(['csharp']);
  65. });
  66. beforeEach(() => {
  67. savedEnv = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
  68. resetKernelForTests();
  69. });
  70. afterEach(() => {
  71. for (const k of ENV_KEYS) {
  72. if (savedEnv[k] === undefined) delete process.env[k];
  73. else process.env[k] = savedEnv[k];
  74. }
  75. resetKernelForTests();
  76. });
  77. function assertParity(filePath: string, source: string, minNodes = 3): void {
  78. process.env.CODEGRAPH_KERNEL_LANGS = 'all';
  79. delete process.env.CODEGRAPH_KERNEL;
  80. const viaKernel = tryKernelExtract(filePath, source, 'csharp');
  81. expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
  82. process.env.CODEGRAPH_KERNEL = '0';
  83. const viaWasm = extractFromSource(filePath, source, 'csharp');
  84. delete process.env.CODEGRAPH_KERNEL;
  85. const k = canon(viaKernel!);
  86. const w = canon(viaWasm);
  87. expect(k.nodes, `${filePath}: nodes`).toEqual(w.nodes);
  88. expect(k.edges, `${filePath}: edges`).toEqual(w.edges);
  89. expect(k.refs, `${filePath}: refs`).toEqual(w.refs);
  90. expect(viaWasm.nodes.length).toBeGreaterThanOrEqual(minNodes);
  91. }
  92. const FIXTURES: Array<{ file: string; minNodes: number }> = [
  93. { file: 'Torture.cs', minNodes: 40 },
  94. { file: 'TortureFileScoped.cs', minNodes: 8 },
  95. { file: 'TortureTopLevel.cs', minNodes: 2 },
  96. ];
  97. for (const { file, minNodes } of FIXTURES) {
  98. it(`${file}: namespaces, records, calls, holes, refs`, () => {
  99. const src = fs.readFileSync(path.join(FIXTURE_DIR, file), 'utf8');
  100. assertParity(`fixtures/${file}`, src, minNodes);
  101. });
  102. // CRLF variant — the shape every Windows autocrlf checkout has. Derived in
  103. // memory so no platform or editor can silently normalize it away; pins the
  104. // JS-multiline-^ docstring semantics for `///` runs (#1329).
  105. it(`${file} CRLF parity`, () => {
  106. const src = fs.readFileSync(path.join(FIXTURE_DIR, file), 'utf8');
  107. const crlf = src.replace(/(?<!\r)\n/g, '\r\n');
  108. assertParity(`fixtures/${file} (crlf)`, crlf, minNodes);
  109. });
  110. }
  111. // Cheap unit pins for the quirks a future grammar bump would silently move
  112. // (checklist §fixtures item 6) — parity is the assertion; the wasm arm is
  113. // the behavior oracle.
  114. const MICROS: Array<{ name: string; source: string; minNodes: number }> = [
  115. {
  116. name: 'alias-import to a qualified target keeps generic args in moduleName',
  117. source: 'using Coll = System.Collections.Generic.Dictionary<string, int>;\n',
  118. minNodes: 2,
  119. },
  120. {
  121. name: 'alias-import to a bare identifier captures the ALIAS name',
  122. source: 'using Short = SomeType;\n',
  123. minNodes: 2,
  124. },
  125. {
  126. name: 'C#12 primary-ctor base args emit the garbage `(repo)` extends ref',
  127. source: 'public class Svc(IRepo repo) : Base(repo), IThing { }\n',
  128. minNodes: 2,
  129. },
  130. {
  131. name: 'enum underlying type emits an extends ref named `byte`',
  132. source: 'public enum E : byte { A = 1, B }\n',
  133. minNodes: 4,
  134. },
  135. {
  136. name: 'nameof(...) emits a calls ref named `nameof`',
  137. source: 'public class C { void M() { var n = nameof(C); } }\n',
  138. minNodes: 3,
  139. },
  140. {
  141. name: 'this./base. callee prefixes are kept raw',
  142. source: 'public class C { void M() { this.Run(1); base.Go(); } }\n',
  143. minNodes: 3,
  144. },
  145. {
  146. name: 'bare-identifier-typed property loses its type in the signature',
  147. source: 'public class C { public Widget Parent { get; set; } }\n',
  148. minNodes: 3,
  149. },
  150. {
  151. name: 'bodiless struct mints no node; bodiless record still does',
  152. source: 'public record Empty;\n',
  153. minNodes: 2,
  154. },
  155. ];
  156. for (const m of MICROS) {
  157. it(`micro: ${m.name}`, () => {
  158. assertParity(`micro/${m.name.replace(/[^a-z0-9]+/gi, '-')}.cs`, m.source, m.minNodes);
  159. });
  160. }
  161. it('files with parse errors defer to the wasm extractor (recovery is encoding-dependent)', () => {
  162. const broken = 'class F { void M( { return }} 12 (\n';
  163. process.env.CODEGRAPH_KERNEL_LANGS = 'all';
  164. delete process.env.CODEGRAPH_KERNEL;
  165. expect(tryKernelExtract('src/Broken.cs', broken, 'csharp')).toBeNull();
  166. process.env.CODEGRAPH_KERNEL = '0';
  167. const viaWasm = extractFromSource('src/Broken.cs', broken, 'csharp');
  168. delete process.env.CODEGRAPH_KERNEL;
  169. expect(viaWasm.nodes.some((n) => n.kind === 'file')).toBe(true);
  170. });
  171. });