GitNexus/gitnexus/test/unit/fetch-reason-parsing.test.ts
samuel mbabhazi 9772bcd24f
feat(api_impact): list the callers of the requesting function as indirect consumers (#3720)
* feat(api_impact): list the callers of the requesting function as indirect consumers

The FETCHES edge is file-level, so api_impact saw only the file that sends
the request. The files that call its request function never mention the URL
and were missing from the report (#3644).

The edge's reason now names the callables whose spans contain the request
sites, one `|via:<id>` field each, under the containment rule the R3-6 sink
set already uses; that rule moves into EnclosingCallableIndex so both stay
one. Several sites of one file for one route now share the edge instead of
colliding on its id. api_impact follows the CALLS edges into those callables
one level up and reports the callers as `indirectConsumers`, apart from the
direct consumers and outside the risk level. Direct consumer files and the
handler file are left out. An index built before the attribution carries no
field. Expo navigation sites carry their real line so they attribute to the
component that contains them.

Closes #3644

* fix(api_impact): attribute a request site by its AST enclosing callable

A helper defined on the line of the request has the smaller span, so the
line-only lookup named it and the callers of the real requesting function
vanished from indirectConsumers. The parser now records the enclosing
callable of each fetch site and of each wrapper call (through the call's
position), and attribution prefers that identity; the span lookup remains
for sites found by text scan. The same preference applies to the R3-6 sink
set. A real analyze of the fixture followed by api_impact pins the helper
case and the wrapper case.

* test(bench): rebaseline receiver resolution for the indirect-consumers fixture

The api-impact-indirect-consumers fixture adds three receiver-unresolved
sites to the corpus: two `.then(...)` calls on the untyped return of
`fetch(...)` and of the local wrapper `apiFetch(...)`, and one
`req.query.path` read inside an inline Express handler with an untyped
`req`. Call drops 138 -> 140, reads 30 -> 31, all kinds 168 -> 171. No
shape-arm state and no existing site changed.

* fix(api_impact): preserve module-scope request attribution

Keep AST-confirmed module scope distinct from missing attribution for direct
fetches, HTTP clients, and wrapper calls. Prevent overlapping callable spans
from inventing indirect consumers or process sinks.

Preserve span fallback for callable forms the generic identity walk cannot
name. Add eight regression cases, including real analyze/api_impact coverage.
Rebaseline receiver-resolution counts for the two then calls and one property
read added by this PR's existing fixtures; shape expectations are unchanged.

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Gergő Magyar <gergomagyar@icloud.com>
2026-10-25 18:29:09 +00:00

144 lines
5.4 KiB
TypeScript

/**
* Unit Tests: Fetch reason field parsing
*
* Tests the reason field format used by processNextjsFetchRoutes and
* parsed by fetchRoutesWithConsumers. Verifies that multi-fetch count
* encoding and keys extraction work correctly with the updated regex.
*/
import { describe, it, expect } from 'vitest';
/**
* Extracted parsing logic matching fetchRoutesWithConsumers in local-backend.ts.
* This mirrors the regex patterns used at runtime.
*/
function parseReasonField(fetchReason: string | null): {
accessedKeys?: string[];
fetchCount?: number;
viaSymbolIds?: string[];
} {
let accessedKeys: string[] | undefined;
let fetchCount: number | undefined;
let viaSymbolIds: string[] | undefined;
if (fetchReason) {
const keysMatch = fetchReason.match(/\|keys:([^|]+)/);
if (keysMatch) {
accessedKeys = keysMatch[1].split(',').filter((k) => k.length > 0);
}
const fetchesMatch = fetchReason.match(/\|fetches:(\d+)/);
if (fetchesMatch) {
fetchCount = parseInt(fetchesMatch[1], 10);
}
const via = [...fetchReason.matchAll(/\|via:([^|]+)/g)].map((m) => m[1]);
if (via.length > 0) viaSymbolIds = via;
}
return {
...(accessedKeys ? { accessedKeys } : {}),
...(fetchCount && fetchCount > 1 ? { fetchCount } : {}),
...(viaSymbolIds ? { viaSymbolIds } : {}),
};
}
describe('fetch reason field parsing', () => {
it('parses basic reason with no keys or fetches', () => {
const result = parseReasonField('fetch-url-match');
expect(result.accessedKeys).toBeUndefined();
expect(result.fetchCount).toBeUndefined();
});
it('parses keys without fetches suffix', () => {
const result = parseReasonField('fetch-url-match|keys:data,pagination');
expect(result.accessedKeys).toEqual(['data', 'pagination']);
expect(result.fetchCount).toBeUndefined();
});
it('parses keys with fetches suffix', () => {
const result = parseReasonField('fetch-url-match|keys:data,pagination|fetches:3');
expect(result.accessedKeys).toEqual(['data', 'pagination']);
expect(result.fetchCount).toBe(3);
});
it('parses fetches without keys', () => {
const result = parseReasonField('fetch-url-match|fetches:2');
expect(result.accessedKeys).toBeUndefined();
expect(result.fetchCount).toBe(2);
});
it('does not set fetchCount when value is 1', () => {
// fetchCount=1 means single route, no need to flag
const result = parseReasonField('fetch-url-match|keys:data|fetches:1');
expect(result.accessedKeys).toEqual(['data']);
expect(result.fetchCount).toBeUndefined();
});
it('handles null reason', () => {
const result = parseReasonField(null);
expect(result.accessedKeys).toBeUndefined();
expect(result.fetchCount).toBeUndefined();
});
it('old greedy regex would have included |fetches: in keys — new regex does not', () => {
// This is the regression test: the old regex /\|keys:(.+)$/ would match
// "data,pagination|fetches:3" as keys, including the fetches suffix
const reason = 'fetch-url-match|keys:data,pagination|fetches:3';
const result = parseReasonField(reason);
// Keys should NOT contain "fetches:3" or "pagination|fetches:3"
expect(result.accessedKeys).not.toContain('pagination|fetches:3');
expect(result.accessedKeys).toEqual(['data', 'pagination']);
});
// #3644: one field per requesting function; ids carry ':' and '#'.
it('parses every via field, after keys and fetches', () => {
const result = parseReasonField(
'fetch-url-match|keys:data|fetches:2|via:Function:src/api.ts:getGrants#0|via:Method:src/api.ts:Api.list#1',
);
expect(result.accessedKeys).toEqual(['data']);
expect(result.fetchCount).toBe(2);
expect(result.viaSymbolIds).toEqual([
'Function:src/api.ts:getGrants#0',
'Method:src/api.ts:Api.list#1',
]);
});
it('leaves viaSymbolIds out of a reason written before the attribution', () => {
const result = parseReasonField('fetch-url-match|keys:data,pagination|fetches:3');
expect(result.viaSymbolIds).toBeUndefined();
expect(result.accessedKeys).toEqual(['data', 'pagination']);
});
});
describe('confidence derivation from fetchCount', () => {
it('high confidence when fetchCount is undefined (single fetch)', () => {
const fetchCount: number | undefined = undefined;
const isMultiFetch = (fetchCount ?? 1) > 1;
expect(isMultiFetch).toBe(false);
});
it('low confidence when fetchCount > 1', () => {
const fetchCount = 3;
const isMultiFetch = (fetchCount ?? 1) > 1;
expect(isMultiFetch).toBe(true);
});
});
describe('middlewareDetection partial flag', () => {
it('flags partial when middleware exists and handler has multiple routes', () => {
const middleware = ['withAuth'];
const handlerRouteCount = 2;
const middlewarePartial = middleware.length > 0 && handlerRouteCount > 1;
expect(middlewarePartial).toBe(true);
});
it('does not flag partial when middleware exists but handler has single route', () => {
const middleware = ['withAuth'];
const handlerRouteCount = 1;
const middlewarePartial = middleware.length > 0 && handlerRouteCount > 1;
expect(middlewarePartial).toBe(false);
});
it('does not flag partial when no middleware even with multiple routes', () => {
const middleware: string[] = [];
const handlerRouteCount = 3;
const middlewarePartial = middleware.length > 0 && handlerRouteCount > 1;
expect(middlewarePartial).toBe(false);
});
});