GitNexus/gitnexus/test/unit/graph-identity.test.ts
Gergő Magyar ef126ea207
Some checks are pending
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / Classify release event (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / ci (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
refactor(scope-resolution): put import visibility behind one interface (#3908)
* test(java): pin on-demand import behaviour to JLS scenarios

Fourteen scenarios for `import pkg.*;` taken from JLS SE 21 (7.5.2,
6.4.1, 6.5.5.1, 6.5.4.2, 6.6.1), each asserting the exact CALLS, EXTENDS
and IMPORTS edges the resolver emits today. Nine agree with the JLS. Five
are labelled as known semantic gaps (a name supplied by one on-demand
package resolves only for the class link, accessibility is not applied,
member types of a class are not imported on demand); they are pinned so a
change of import representation cannot move them by accident, not
endorsed.

Baseline for the import-group memory work, which must leave every row
unchanged.

Refs #3695

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* refactor(scope-resolution): put import visibility behind one interface

`ScopeResolutionIndexes.imports` was a public map of edge arrays, so every
reader depended on one stored edge per imported file. It is now an
`ImportVisibility`: the only way to ask what a scope imports, with one
operation per question a reader has.

- `edgesAt`: the imports a reader can select by name, definition or kind
- `reaches` / `reachesDirectory`: does this scope import that file or any
  file of that directory
- `membership`: the same question asked repeatedly at one scope
- `forEachTarget`: every imported file in statement order, with the edge
  that describes how it is imported
- `scopes`: every scope finalize recorded

`createEagerImportVisibility` implements it over the edge map finalize
already produces; storage is unchanged.

All 26 readers of the map are migrated to the operation they need. The
two file-grounding checks ask `reaches`; inheritance disambiguation asks
`reaches` and `reachesDirectory`; File->File IMPORTS emission and the Java
on-demand fallback use `forEachTarget`; unique-name property narrowing
asks a cached `membership` per importing scope instead of building a set
of imported files for every file up front; readers that select by name or
kind, and each language's own provider code, use `edgesAt`.

No intended semantic change. Test fixtures that built an index by hand
wrap their edge map with the eager implementation; no expected result
changes. The import-emission benchmark gains `--map-input` for an emitter
that predates the interface.

Refs #3695

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* test(scope-resolution): contract suite for ImportVisibility and a graph-identity helper

The contract is checked against a description of each import relation
(per scope, the statements as written and the files each resolves to),
not against any implementation's storage: 11 hand-written relations and
60 from a seeded generator, covering duplicates, overlaps, self-targets,
unresolved targets, empty lists and explicit imports interleaved with
wildcards. Each case compares scope order, recorded-versus-absent scopes,
the imports selectable by name, definition or kind, ordered targets with
the metadata a consumer reads, and every membership answer. A second
implementation runs the same cases by passing its builder.

`graphIdentity` gives a canonical form of a knowledge graph (node ids,
labels and properties; relationship ids, types, endpoints and properties)
for comparing two runs. It normalizes storage order only.

Refs #3695

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* test(scope-resolution): address review notes on the contract tests

- The duplicate-target coverage check compared target arrays by identity.
  It now compares them by content, which is what "the same list written
  twice" means.
- The contract helper's header named a function that does not exist; it
  now says how to run the cases against an implementation.
- The JLS scenario comment said every importer uses `T` three ways; S12
  and S14 do not, and the comment says so.

Tests and comments only.

Refs #3695

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-13 19:47:00 +00:00

56 lines
2.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import type { GraphNode, GraphRelationship } from 'gitnexus-shared';
import { graphIdentity } from '../helpers/graph-identity.js';
const node = (id: string, properties: Record<string, unknown>): GraphNode =>
({ id, label: 'Class', properties }) as GraphNode;
const rel = (id: string, extra: Partial<GraphRelationship> = {}): GraphRelationship => ({
id,
sourceId: 'a',
targetId: 'b',
type: 'CALLS',
confidence: 1,
reason: 'r',
...extra,
});
const graph = (nodes: GraphNode[], relationships: GraphRelationship[]) => ({
iterNodes: () => nodes,
iterRelationships: () => relationships,
});
describe('graphIdentity', () => {
const nodes = [node('a', { name: 'A', filePath: 'a.ts' }), node('b', { name: 'B' })];
const rels = [rel('1'), rel('2', { targetId: 'a' })];
const base = graphIdentity(graph(nodes, rels));
it('ignores the order nodes and relationships are stored in', () => {
expect(graphIdentity(graph([...nodes].reverse(), [...rels].reverse()))).toEqual(base);
});
it('ignores the order of property keys', () => {
const reordered = [node('a', { filePath: 'a.ts', name: 'A' }), nodes[1]!];
expect(graphIdentity(graph(reordered, rels))).toEqual(base);
});
it.each([
{ change: 'an endpoint', relationships: [rel('1', { targetId: 'c' }), rels[1]!] },
{ change: 'an edge type', relationships: [rel('1', { type: 'IMPORTS' }), rels[1]!] },
{ change: 'a confidence', relationships: [rel('1', { confidence: 0.5 }), rels[1]!] },
{ change: 'a reason', relationships: [rel('1', { reason: 'other' }), rels[1]!] },
{ change: 'a missing edge', relationships: [rels[0]!] },
{ change: 'a duplicated edge', relationships: [...rels, rels[0]!] },
])('tells graphs apart by $change', ({ relationships }) => {
expect(graphIdentity(graph(nodes, relationships))).not.toEqual(base);
});
it('tells graphs apart by a node property', () => {
const changed = [node('a', { name: 'A', filePath: 'other.ts' }), nodes[1]!];
expect(graphIdentity(graph(changed, rels))).not.toEqual(base);
});
it('keeps the order of array-valued properties, which is content', () => {
const first = graphIdentity(graph([node('a', { params: ['x', 'y'] })], []));
const second = graphIdentity(graph([node('a', { params: ['y', 'x'] })], []));
expect(second).not.toEqual(first);
});
});