mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-12-05 00:27:26 +00:00
80 lines
2.9 KiB
TypeScript
80 lines
2.9 KiB
TypeScript
import { assert, expect, it } from 'vitest';
|
|
import { createEagerImportVisibility, type ImportEdge } from 'gitnexus-shared';
|
|
import { finalizeScopeModel } from '../../../../src/core/ingestion/finalize-orchestrator.js';
|
|
import { vueScopeResolver } from '../../../../src/core/ingestion/languages/vue/scope-resolver.js';
|
|
import { extractParsedFile } from '../../../../src/core/ingestion/scope-extractor-bridge.js';
|
|
import { buildTestGraph } from '../../../helpers/test-graph.js';
|
|
|
|
function buttonImport(targetFile: string | null): ImportEdge {
|
|
return {
|
|
kind: targetFile === null ? 'dynamic-unresolved' : 'alias',
|
|
localName: 'Button',
|
|
targetExportedName: 'default',
|
|
targetFile,
|
|
};
|
|
}
|
|
|
|
it.each([2, 8])(
|
|
'resolves template components in %i views with one pass over import scopes',
|
|
(count) => {
|
|
const views = Array.from({ length: count }, (_, i) => ({
|
|
view: `View${i}.vue`,
|
|
button: `Button${i}.vue`,
|
|
}));
|
|
const paths = ['Fallback.vue', ...views.flatMap(({ view, button }) => [view, button])];
|
|
const graph = buildTestGraph(
|
|
paths.map((filePath) => ({
|
|
id: `File:${filePath}`,
|
|
label: 'File',
|
|
name: filePath,
|
|
filePath,
|
|
})),
|
|
);
|
|
const source = '<script setup>function setupView() {}</script><template><Button /></template>';
|
|
const fileContents = new Map(views.map(({ view }) => [view, source]));
|
|
const importsByScope = new Map<string, ImportEdge[]>();
|
|
const files = views.map(({ view, button }) => {
|
|
const parsed = extractParsedFile(vueScopeResolver.languageProvider, source, view);
|
|
assert(parsed);
|
|
const inner = parsed.scopes.find((scope) => scope.kind === 'Function');
|
|
assert(inner);
|
|
// The last resolved binding for Button wins across scopes in this file.
|
|
importsByScope.set(parsed.moduleScope, [buttonImport('Fallback.vue')]);
|
|
importsByScope.set(inner.id, [
|
|
buttonImport('Fallback.vue'),
|
|
buttonImport(button),
|
|
buttonImport(null),
|
|
]);
|
|
return parsed;
|
|
});
|
|
|
|
const imports = createEagerImportVisibility(importsByScope);
|
|
let scopeVisits = 0;
|
|
const indexes = {
|
|
...finalizeScopeModel(files),
|
|
imports: {
|
|
...imports,
|
|
*scopes() {
|
|
for (const scope of imports.scopes()) {
|
|
scopeVisits++;
|
|
yield scope;
|
|
}
|
|
},
|
|
},
|
|
};
|
|
assert(vueScopeResolver.emitPostResolutionEdges);
|
|
vueScopeResolver.emitPostResolutionEdges(graph, files, new Map(), indexes, { fileContents });
|
|
|
|
expect([...graph.iterRelationships()]).toEqual(
|
|
views.map(({ view, button }) => ({
|
|
id: `CALLS:File:${view}:Button->File:${button}`,
|
|
sourceId: `File:${view}`,
|
|
targetId: `File:${button}`,
|
|
type: 'CALLS',
|
|
reason: 'vue-template-component',
|
|
confidence: 0.9,
|
|
})),
|
|
);
|
|
expect(scopeVisits).toBeLessThanOrEqual(importsByScope.size);
|
|
},
|
|
);
|