GitNexus/gitnexus/test/integration/resolvers/dart-coverage.test.ts
gitnexus-release-bot[bot] 47d63a0601
chore(vendor): update tree-sitter-dart to 1.0.0-g0038280 (#3633)
* chore(vendor): update tree-sitter-dart to 1.0.0-g0038280

* chore(vendor): rebuild native prebuilds (tree-sitter-dart)

Built by https://github.com/abhigyanpatwari/GitNexus/actions/runs/39212887661

* fix(dart): use native extension type parsing

The upgraded grammar parses extension types directly. The compatibility
rewrite loses generic extension bodies and implements clauses, so capture
the native nodes and register their member ownership instead.

Refresh the Dart capture fingerprint after comparing the old grammar and
emitter over the same fixture corpus. Keep the scaling budget unchanged.

Refs #3633

* chore(vendor): rebuild native prebuilds (tree-sitter-dart)

Built by https://github.com/abhigyanpatwari/GitNexus/actions/runs/39239767139

* fix: repair Dart parity and checkpoint regressions

Remove Dart from the preprocessing fixture inventory after retiring its
hook. Use the parsed body boundary when reading extension-type heritage
so named-record representations retain their implements clauses.

Seed the WAL recovery fixture with the normal checkpoint threshold, then
force one-byte checkpoints only for the deliberately blocked write.
Require an actual error exit instead of accepting a subprocess timeout.

Refs #3633

* chore(vendor): rebuild native prebuilds (tree-sitter-dart)

Built by https://github.com/abhigyanpatwari/GitNexus/actions/runs/39248441688

---------

Co-authored-by: gitnexus-release-bot[bot] <gitnexus-release-bot[bot]@users.noreply.github.com>
Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-10-19 15:41:34 +01:00

286 lines
11 KiB
TypeScript

/**
* Regression tests for Dart scope-resolution / structure coverage gaps
* (issue #1919). Mirrors python-parsing-coverage.test.ts: the F28 scope-capture
* assertions exercise emitDartScopeCaptures directly, and a pipeline check
* verifies the TypeAlias symbol exists end-to-end.
*
* F28 — old-style function typedef (`typedef int Cmp(int a, int b);`) was never
* captured: DART_SCOPE_QUERY had no type_alias rule, and DART_QUERIES only
* captured the new-style (`=`-anchored) form. Both forms must now surface as a
* type-alias declaration / TypeAlias symbol.
*
* #1919 review CF2 — the GENERIC forms (`typedef int Cmp2<T>(T a, T b);` and
* `typedef Mapper<T> = T Function(T);`) were still dropped: a generic
* type_parameters node sits between the alias name and the next anchor, so the
* non-generic adjacency patterns never matched. Standalone generic patterns now
* capture them too.
*/
import { describe, it, expect, beforeAll } from 'vitest';
import path from 'path';
import { emitDartScopeCaptures } from '../../../src/core/ingestion/languages/dart/captures.js';
import {
FIXTURES,
edgeSet,
findDanglingEdges,
getNodesByLabel,
getRelationships,
runPipelineFromRepo,
type PipelineResult,
} from './helpers.js';
import {
isLanguageAvailable,
loadParser,
loadLanguage,
} from '../../../src/core/tree-sitter/parser-loader.js';
import { SupportedLanguages } from '../../../src/config/supported-languages.js';
import type { CaptureMatch } from 'gitnexus-shared';
import { getDartParser } from '../../../src/core/ingestion/languages/dart/query.js';
let dartAvailable = isLanguageAvailable(SupportedLanguages.Dart);
if (dartAvailable) {
try {
await loadParser();
await loadLanguage(SupportedLanguages.Dart);
} catch {
dartAvailable = false;
}
}
const TYPEDEFS = `typedef int Cmp(int a, int b);
typedef int Cmp2<T>(T a, T b);
typedef Pred = bool Function(int);
typedef Mapper<T> = T Function(T);
typedef int _Internal(int);`;
const EXTENSION_TYPES = `class Identifiable {}
class SequenceLike<T> {}
class Comparator<A, B> {}
extension type const UserId(String value) implements Identifiable {
String describe() => value;
}
extension type const EmptyId(String value) {}
extension type Celsius(double degrees) {
double toFahrenheit() => degrees * 9 / 5 + 32;
}
extension type Box<T>(List<T> value) implements SequenceLike<T> {
T first() => value.first;
}
extension type Pair(String value) implements Comparator<String, int> {
String describePair() => value;
}
extension Fancy on String {
int get doubledLength => length * 2;
String shout() => toUpperCase();
}`;
/** All @declaration.type_alias matches, as (name) tuples. */
function typeAliasNames(src: string): string[] {
const matches = emitDartScopeCaptures(src, 'test.dart') as CaptureMatch[];
return matches
.filter((m) => m['@declaration.type_alias'] !== undefined)
.map((m) => m['@declaration.name']?.text)
.filter((n): n is string => Boolean(n));
}
/** All class-like Dart declaration matches, as names. */
function classDeclarationNames(src: string): string[] {
const matches = emitDartScopeCaptures(src, 'test.dart') as CaptureMatch[];
return matches
.filter((m) => m['@declaration.class'] !== undefined)
.map((m) => m['@declaration.name']?.text)
.filter((n): n is string => Boolean(n));
}
/** Synthetic Dart implements markers, as marker payloads. */
function heritageImports(src: string): string[] {
const matches = emitDartScopeCaptures(src, 'test.dart') as CaptureMatch[];
return matches.map((m) => m['@import.heritage']?.text).filter((n): n is string => Boolean(n));
}
// ---------------------------------------------------------------------------
// F28 — typedef capture (scope layer)
// ---------------------------------------------------------------------------
describe.skipIf(!dartAvailable)('F28 — Dart typedef capture (scope layer)', () => {
it('captures the old-style function typedef as a type-alias declaration', () => {
const names = typeAliasNames(TYPEDEFS);
expect(names).toContain('Cmp');
});
it('still captures the new-style typedef (regression)', () => {
const names = typeAliasNames(TYPEDEFS);
expect(names).toContain('Pred');
});
it('captures a private old-style typedef', () => {
const names = typeAliasNames(TYPEDEFS);
expect(names).toContain('_Internal');
});
it('captures the generic old-style typedef (CF2)', () => {
const names = typeAliasNames(TYPEDEFS);
expect(names).toContain('Cmp2');
});
it('captures the generic new-style typedef (CF2)', () => {
const names = typeAliasNames(TYPEDEFS);
expect(names).toContain('Mapper');
});
it('emits exactly one declaration per typedef (no double-match)', () => {
const names = typeAliasNames(TYPEDEFS);
expect(names.sort()).toEqual(['Cmp', 'Cmp2', 'Pred', 'Mapper', '_Internal'].sort());
});
});
// ---------------------------------------------------------------------------
// #2538 — extension type declarations (scope layer)
// ---------------------------------------------------------------------------
describe.skipIf(!dartAvailable)('Dart extension type declarations (scope layer)', () => {
it('parses native extension types without errors and preserves declaration positions', () => {
const tree = getDartParser().parse(EXTENSION_TYPES);
expect(tree.rootNode.hasError).toBe(false);
const matches = emitDartScopeCaptures(EXTENSION_TYPES, 'test.dart', tree);
const declarations = matches.filter((match) => match['@declaration.class']);
for (const name of ['UserId', 'EmptyId', 'Celsius', 'Box', 'Pair', 'Fancy']) {
const declaration = declarations.find((match) => match['@declaration.name']?.text === name);
const precedingLines = EXTENSION_TYPES.slice(0, EXTENSION_TYPES.indexOf(name)).split('\n');
expect(declaration?.['@declaration.name']?.range).toMatchObject({
startLine: precedingLines.length,
startCol: precedingLines.at(-1)?.length,
});
}
});
it('captures generic extension type parameters', () => {
const matches = emitDartScopeCaptures(EXTENSION_TYPES, 'test.dart');
const box = matches.find((match) => match['@declaration.name']?.text === 'Box');
expect(box?.['@declaration.type-parameters']?.text).toBe('<T>');
});
it('binds this inside extension type methods to the owning type', () => {
const matches = emitDartScopeCaptures(EXTENSION_TYPES, 'test.dart');
const receivers = matches
.filter((match) => match['@type-binding.name']?.text === 'this')
.map((match) => match['@type-binding.type']?.text);
expect(receivers).toEqual(
expect.arrayContaining(['UserId', 'Celsius', 'Box', 'Pair', 'Fancy']),
);
});
it('captures extension types as class-like declarations', () => {
const names = classDeclarationNames(EXTENSION_TYPES);
expect(names).toEqual(
expect.arrayContaining(['UserId', 'EmptyId', 'Celsius', 'Box', 'Pair', 'Fancy']),
);
});
it('captures implements clauses on extension types as heritage markers', () => {
const imports = heritageImports(EXTENSION_TYPES);
expect(imports).toEqual(
expect.arrayContaining([
'__heritage__:implements:Identifiable:UserId',
'__heritage__:implements:SequenceLike:Box',
'__heritage__:implements:Comparator:Pair',
]),
);
expect(imports).not.toContain('__heritage__:implements:int:Pair');
});
it('captures implements clauses after named-record representation types', () => {
const imports = heritageImports(
'extension type RecordId(({String id}) value) implements Identifiable, Comparator<String, int> {}',
);
expect(imports).toEqual([
'__heritage__:implements:Identifiable:RecordId',
'__heritage__:implements:Comparator:RecordId',
]);
});
});
// ---------------------------------------------------------------------------
// F28 — typedef symbols exist end-to-end (structure phase)
// ---------------------------------------------------------------------------
describe.skipIf(!dartAvailable)('F28 — Dart typedef symbols (end-to-end)', () => {
let result: PipelineResult;
beforeAll(async () => {
result = await runPipelineFromRepo(path.join(FIXTURES, 'dart-coverage'), () => {});
}, 60000);
it('creates TypeAlias nodes for old-style, new-style, generic, and private typedefs', () => {
const aliases = getNodesByLabel(result, 'TypeAlias');
expect(aliases).toContain('Cmp'); // old-style (covers F28)
expect(aliases).toContain('Cmp2'); // generic old-style (covers CF2)
expect(aliases).toContain('Pred'); // new-style (regression)
expect(aliases).toContain('Mapper'); // generic new-style (covers CF2)
expect(aliases).toContain('_Internal'); // private old-style
});
it('emits exactly one TypeAlias per typedef (no duplicates)', () => {
const aliases = getNodesByLabel(result, 'TypeAlias');
const fromFixture = aliases.filter((n) =>
['Cmp', 'Cmp2', 'Pred', 'Mapper', '_Internal'].includes(n),
);
expect(fromFixture.sort()).toEqual(['Cmp', 'Cmp2', 'Mapper', 'Pred', '_Internal'].sort());
});
});
// ---------------------------------------------------------------------------
// #2538 — extension type symbols exist end-to-end (structure phase)
// ---------------------------------------------------------------------------
describe.skipIf(!dartAvailable)('Dart extension type symbols (end-to-end)', () => {
let result: PipelineResult;
beforeAll(async () => {
result = await runPipelineFromRepo(path.join(FIXTURES, 'dart-extension-types'), () => {});
}, 60000);
it('creates Class nodes for extension type declarations', () => {
const classes = getNodesByLabel(result, 'Class');
expect(classes).toEqual(
expect.arrayContaining(['UserId', 'EmptyId', 'Celsius', 'Box', 'Pair', 'Fancy']),
);
});
it('emits IMPLEMENTS edges for extension type implements clauses', () => {
const implementsEdges = edgeSet(getRelationships(result, 'IMPLEMENTS'));
expect(implementsEdges).toEqual(
expect.arrayContaining(['Box → SequenceLike', 'Pair → Comparator', 'UserId → Identifiable']),
);
expect(implementsEdges).not.toContain('Pair → int');
});
it('keeps extension type methods owned by their extension type symbol', () => {
const methods = getNodesByLabel(result, 'Method');
expect(methods).toEqual(
expect.arrayContaining(['describe', 'toFahrenheit', 'first', 'describePair', 'shout']),
);
const hasMethod = edgeSet(getRelationships(result, 'HAS_METHOD'));
expect(hasMethod).toEqual(
expect.arrayContaining([
'UserId → describe',
'Celsius → toFahrenheit',
'Box → first',
'Pair → describePair',
'Fancy → shout',
]),
);
});
it('does not leave dangling method ownership edges', () => {
expect(findDanglingEdges(result, ['HAS_METHOD', 'IMPLEMENTS'])).toEqual([]);
});
});