GitNexus/gitnexus/test/unit/incremental-subgraph-extract.test.ts
MyShining 1147646518
feat(spring): model AOP transactions, caching, and security (#2783)
* feat(spring): model AOP advice and proxy behavior

* fix(spring): address AOP review findings

---------

Co-authored-by: Shining <xuenning@qiyi.com>
2026-08-01 17:22:12 +01:00

294 lines
12 KiB
TypeScript

/**
* Tests for incremental DB writeback subgraph extraction.
*
* Locks the Finding 1 fix (PR #1479 review): cross-file edges between
* two unchanged files MUST land in the writeback subgraph when a third
* (changed) file alters their cross-file resolution. The pre-fix
* behaviour silently dropped those edges, leaving stale rows in the DB.
*
* These tests use synthetic graphs constructed via createKnowledgeGraph
* directly — they don't run the parser, so they're cheap and stable.
*/
import { describe, it, expect } from 'vitest';
import type { GraphNode, GraphRelationship } from 'gitnexus-shared';
import { createKnowledgeGraph } from '../../src/core/graph/graph.js';
import {
extractChangedSubgraph,
computeEffectiveWriteSet,
} from '../../src/core/incremental/subgraph-extract.js';
import {
SPRING_AUTO_CONFIGURATION_SYNTHETIC_DESCRIPTION,
SPRING_AUTO_CONFIGURATION_SYNTHETIC_ID_PREFIX,
} from '../../src/core/ingestion/frameworks/spring/auto-configuration.js';
const makeFileNode = (id: string, filePath: string, label = 'Function'): GraphNode =>
({
id,
label,
properties: { filePath, name: id },
}) as unknown as GraphNode;
const makeWideNode = (id: string, label: 'Community' | 'Process'): GraphNode =>
({
id,
label,
properties: {},
}) as unknown as GraphNode;
const makeRel = (
id: string,
sourceId: string,
targetId: string,
type = 'CALLS',
reason = 'test',
): GraphRelationship =>
({
id,
sourceId,
targetId,
type,
reason,
properties: {},
}) as unknown as GraphRelationship;
describe('extractChangedSubgraph', () => {
it('includes nodes whose filePath is in the explicit toWriteSet', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('a', '/repo/a.ts'));
g.addNode(makeFileNode('c', '/repo/c.ts'));
const sub = extractChangedSubgraph(g, new Set(['/repo/c.ts']));
expect(sub.nodes.map((n) => n.id).sort()).toEqual(['c']);
});
it('always includes graph-wide nodes (Community, Process)', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('a', '/repo/a.ts'));
g.addNode(makeWideNode('comm-1', 'Community'));
g.addNode(makeWideNode('proc-1', 'Process'));
const sub = extractChangedSubgraph(g, new Set([])); // no files changed
expect(sub.nodes.map((n) => n.id).sort()).toEqual(['comm-1', 'proc-1']);
});
it('always includes Spring auto-configuration synthetic Class nodes', () => {
const g = createKnowledgeGraph();
g.addNode({
id: `${SPRING_AUTO_CONFIGURATION_SYNTHETIC_ID_PREFIX}com.example.ExternalAutoConfiguration`,
label: 'Class',
properties: {
name: 'ExternalAutoConfiguration',
filePath: '/repo/META-INF/spring.factories',
description: SPRING_AUTO_CONFIGURATION_SYNTHETIC_DESCRIPTION,
},
});
const sub = extractChangedSubgraph(g, new Set(['/repo/unrelated.ts']));
expect(sub.nodes.map((node) => node.id)).toEqual([
`${SPRING_AUTO_CONFIGURATION_SYNTHETIC_ID_PREFIX}com.example.ExternalAutoConfiguration`,
]);
});
it('includes a relationship when at least one endpoint is writable', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('a:fn', '/repo/a.ts'));
g.addNode(makeFileNode('c:fn', '/repo/c.ts'));
g.addRelationship(makeRel('e1', 'a:fn', 'c:fn', 'CALLS'));
// toWriteSet already includes A (the orchestrator expanded it via
// computeEffectiveWriteSet) — both endpoints writable, edge fires.
const sub = extractChangedSubgraph(g, new Set(['/repo/a.ts', '/repo/c.ts']));
expect(sub.nodes.map((n) => n.id).sort()).toEqual(['a:fn', 'c:fn']);
expect(sub.relationships.map((r) => r.id)).toEqual(['e1']);
});
it('skips a relationship entirely between unchanged files', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('x:fn', '/repo/x.ts'));
g.addNode(makeFileNode('y:fn', '/repo/y.ts'));
g.addRelationship(makeRel('e1', 'x:fn', 'y:fn', 'CALLS'));
const sub = extractChangedSubgraph(g, new Set(['/repo/c.ts']));
expect(sub.nodes).toEqual([]);
expect(sub.relationships).toEqual([]);
});
it('always includes TAINT_PATH edges even between two unchanged files (#2084 M4 U6)', () => {
// A cross-function TAINT_PATH whose endpoints (a.ts, c.ts) are both
// unchanged, but an intermediate function on the changed b.ts invalidated
// the flow. Endpoint-writability alone would skip it (stale finding);
// TAINT_PATH is graph-wide so it is always re-extracted (the orchestrator
// delete-alls the old rows first). A plain CALLS edge between the same
// unchanged files stays excluded — only TAINT_PATH gets this treatment.
const g = createKnowledgeGraph();
g.addNode(makeFileNode('a:handle', '/repo/a.ts'));
g.addNode(makeFileNode('c:sink', '/repo/c.ts'));
g.addRelationship(makeRel('tp1', 'a:handle', 'c:sink', 'TAINT_PATH'));
g.addRelationship(makeRel('call1', 'a:handle', 'c:sink', 'CALLS'));
const sub = extractChangedSubgraph(g, new Set(['/repo/b.ts']));
expect(sub.relationships.map((r) => r.id)).toEqual(['tp1']);
});
it('always includes INJECTS edges even between two unchanged files (#2200)', () => {
// A DI consumer→implementer INJECTS edge whose endpoints (consumer.java,
// impl.java) are both unchanged, but the interface (or a sibling
// implementer) on the changed third.java altered the fan-out.
// Endpoint-writability alone would strand the stale edge; INJECTS is
// graph-wide so it is always re-extracted (the orchestrator
// unconditionally delete-alls the old rows first). A plain CALLS edge
// between the same unchanged files stays excluded.
const g = createKnowledgeGraph();
g.addNode(makeFileNode('consumer:Class', '/repo/consumer.java'));
g.addNode(makeFileNode('impl:Class', '/repo/impl.java'));
g.addRelationship(makeRel('inj1', 'consumer:Class', 'impl:Class', 'INJECTS'));
g.addRelationship(makeRel('call1', 'consumer:Class', 'impl:Class', 'CALLS'));
const sub = extractChangedSubgraph(g, new Set(['/repo/third.java']));
expect(sub.relationships.map((r) => r.id)).toEqual(['inj1']);
});
it('always includes ADVISED_BY edges even between two unchanged files (#2416)', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('service:Method', '/repo/Service.java', 'Method'));
g.addNode(makeFileNode('aspect:Method', '/repo/Aspect.java', 'Method'));
g.addRelationship(
makeRel('advised1', 'service:Method', 'aspect:Method', 'ADVISED_BY', 'spring-aop:v1:{}'),
);
g.addRelationship(makeRel('call1', 'service:Method', 'aspect:Method', 'CALLS'));
const sub = extractChangedSubgraph(g, new Set(['/repo/AnnotationShadow.java']));
expect(sub.relationships.map((relationship) => relationship.id)).toEqual(['advised1']);
});
it('always includes Spring DECLARES edges between unchanged metadata and classes (#2415)', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('metadata:File', '/repo/META-INF/spring.factories', 'File'));
g.addNode(makeFileNode('config:Class', '/repo/AutoConfig.java', 'Class'));
g.addRelationship(
makeRel(
'declares1',
'metadata:File',
'config:Class',
'DECLARES',
'spring-auto-configuration-factory',
),
);
g.addRelationship(makeRel('call1', 'metadata:File', 'config:Class', 'CALLS'));
const sub = extractChangedSubgraph(g, new Set(['/repo/unrelated.ts']));
expect(sub.relationships.map((relationship) => relationship.id)).toEqual(['declares1']);
});
it('does not make another metadata system graph-wide just because it uses DECLARES', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('metadata:File', '/repo/META-INF/example.metadata', 'File'));
g.addNode(makeFileNode('target:Class', '/repo/Target.java', 'Class'));
g.addRelationship(
makeRel('declares1', 'metadata:File', 'target:Class', 'DECLARES', 'example-discovery'),
);
const sub = extractChangedSubgraph(g, new Set(['/repo/unrelated.ts']));
expect(sub.relationships).toEqual([]);
});
});
describe('computeEffectiveWriteSet (Finding 1)', () => {
it('does not expand through graph-wide ADVISED_BY edges rebuilt by their owner phase', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('aspect:advice', '/repo/Aspect.java', 'Method'));
for (let index = 0; index < 100; index += 1) {
const serviceId = `service:${index}`;
g.addNode(makeFileNode(serviceId, `/repo/Service${index}.java`, 'Method'));
g.addRelationship(
makeRel(`advised:${index}`, serviceId, 'aspect:advice', 'ADVISED_BY', 'spring-aop:v1:{}'),
);
}
const effective = computeEffectiveWriteSet(g, new Set(['/repo/Aspect.java']));
expect([...effective]).toEqual(['/repo/Aspect.java']);
});
it('barrel re-export — expands the writable set to the consumer file', () => {
// Scenario: file C (a barrel) used to re-export from B; now re-exports
// from D. File A is unchanged byte-wise but its CALLS to foo() now
// resolve to D instead of B. Both A and D are unchanged at the file
// level — but A's edges have shifted.
//
// Pre-fix: toWriteSet={C} → A's nodes not deleted, A→D edge not
// inserted (neither endpoint writable). DB ends up with
// stale A→B and missing A→D.
// Post-fix: the new graph has A→C (A still imports the barrel), so
// A crosses the writable boundary and joins the effective
// write set. deleteNodesForFile(A) then clears the stale
// rows and the subgraph carries the new A→D edge.
const g = createKnowledgeGraph();
g.addNode(makeFileNode('a:fn', '/repo/a.ts'));
g.addNode(makeFileNode('b:fn', '/repo/b.ts'));
g.addNode(makeFileNode('c:re-export', '/repo/c.ts'));
g.addNode(makeFileNode('d:fn', '/repo/d.ts'));
g.addRelationship(makeRel('e1', 'a:fn', 'c:re-export', 'IMPORTS'));
g.addRelationship(makeRel('e2', 'a:fn', 'd:fn', 'CALLS'));
const effective = computeEffectiveWriteSet(g, new Set(['/repo/c.ts']));
expect([...effective].sort()).toEqual(['/repo/a.ts', '/repo/c.ts']);
});
it('picks up edges pointing INTO the changed file (symmetric case)', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('b:fn', '/repo/b.ts'));
g.addNode(makeFileNode('c:fn', '/repo/c.ts'));
g.addRelationship(makeRel('e1', 'b:fn', 'c:fn', 'CALLS'));
const effective = computeEffectiveWriteSet(g, new Set(['/repo/c.ts']));
expect([...effective].sort()).toEqual(['/repo/b.ts', '/repo/c.ts']);
});
it('does not expand when no edge crosses the writable boundary', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('x:fn', '/repo/x.ts'));
g.addNode(makeFileNode('y:fn', '/repo/y.ts'));
g.addRelationship(makeRel('e1', 'x:fn', 'y:fn', 'CALLS'));
const effective = computeEffectiveWriteSet(g, new Set(['/repo/c.ts']));
expect([...effective].sort()).toEqual(['/repo/c.ts']);
});
it('ignores edges to graph-wide nodes (no filePath)', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('a:fn', '/repo/a.ts'));
g.addNode(makeWideNode('comm-1', 'Community'));
g.addRelationship(makeRel('e1', 'a:fn', 'comm-1', 'BELONGS_TO'));
const effective = computeEffectiveWriteSet(g, new Set(['/repo/a.ts']));
expect([...effective].sort()).toEqual(['/repo/a.ts']);
});
it('does not mutate the input set', () => {
const g = createKnowledgeGraph();
g.addNode(makeFileNode('a:fn', '/repo/a.ts'));
g.addNode(makeFileNode('c:fn', '/repo/c.ts'));
g.addRelationship(makeRel('e1', 'a:fn', 'c:fn', 'CALLS'));
const input = new Set(['/repo/c.ts']);
computeEffectiveWriteSet(g, input);
expect([...input]).toEqual(['/repo/c.ts']);
});
});