GitNexus/gitnexus/test/helpers/graph-identity.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

42 lines
1.6 KiB
TypeScript

/**
* Canonical identity of a knowledge graph, for comparing two runs.
*
* Two graphs are the same when they hold the same nodes (id, label and every
* property) and the same relationships (id, type, both endpoints and every
* property). The order in which a graph happens to store or emit them is not
* part of its identity, so both lists are sorted; property keys are sorted for
* the same reason. Nothing else is normalized: a changed confidence, reason or
* evidence entry is a different graph.
*
* Order that affects computation (for example the order a pass visits import
* targets) is asserted where that order is produced, not here.
*/
import type { GraphNode, GraphRelationship } from 'gitnexus-shared';
interface IterableGraph {
iterNodes(): Iterable<GraphNode>;
iterRelationships(): Iterable<GraphRelationship>;
}
export interface GraphIdentity {
readonly nodes: readonly string[];
readonly relationships: readonly string[];
}
/** JSON with object keys sorted at every depth; array order is preserved. */
function stableJson(value: unknown): string {
return JSON.stringify(value, (_key, inner: unknown) =>
inner !== null && typeof inner === 'object' && !Array.isArray(inner)
? Object.fromEntries(
Object.entries(inner as Record<string, unknown>).sort(([a], [b]) => (a < b ? -1 : 1)),
)
: inner,
);
}
export function graphIdentity(graph: IterableGraph): GraphIdentity {
return {
nodes: [...graph.iterNodes()].map((node) => stableJson(node)).sort(),
relationships: [...graph.iterRelationships()].map((rel) => stableJson(rel)).sort(),
};
}