ui-map-api.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /**
  2. * `GET /api/map` — the module aggregation behind the Map (CG-49).
  3. *
  4. * Against a real indexed fixture over a real loopback server, like the rest of
  5. * the viewer's API suite. The fixture is shaped to produce exactly the things
  6. * the endpoint has to get right and that a synthetic payload cannot prove:
  7. *
  8. * - a façade (`src/index.ts`) that must stay its own box rather than being
  9. * folded in with the loose type declarations beside it,
  10. * - real `imports` edges, so the `declared` subset is not always equal to the
  11. * raw count and the layering has something trustworthy to rest on,
  12. * - a two-file import cycle, so the file-level cycle report has a component to
  13. * find,
  14. * - a test directory, so the `test` flag and the root default can be checked.
  15. *
  16. * The pure layout — layering, cycle-breaking, ports — is tested without a
  17. * server in `ui-map-model.test.ts`.
  18. */
  19. import { describe, it, expect, beforeAll, afterAll } from 'vitest';
  20. import * as http from 'http';
  21. import * as fs from 'fs';
  22. import * as os from 'os';
  23. import * as path from 'path';
  24. import CodeGraph from '../src/index';
  25. import { createGraphApi, startUiServer, type GraphApi, type UiServerHandle } from '../src/ui-server';
  26. import { moduleIdFor, normalizeRoot, pickDefaultRoot, resetMapCache } from '../src/ui-server/api/map';
  27. let server: UiServerHandle;
  28. let api: GraphApi;
  29. let tempDir: string;
  30. let projectRoot: string;
  31. function request(requestPath: string): Promise<{ status: number; body: string; type?: string }> {
  32. return new Promise((resolve, reject) => {
  33. const req = http.request(
  34. {
  35. host: '127.0.0.1',
  36. port: server.port,
  37. path: requestPath,
  38. method: 'GET',
  39. headers: { Host: `127.0.0.1:${server.port}` },
  40. setHost: false,
  41. },
  42. (res) => {
  43. const chunks: Buffer[] = [];
  44. res.on('data', (c: Buffer) => chunks.push(c));
  45. res.on('end', () =>
  46. resolve({
  47. status: res.statusCode ?? 0,
  48. body: Buffer.concat(chunks).toString('utf-8'),
  49. type: res.headers['content-type'],
  50. })
  51. );
  52. }
  53. );
  54. req.on('error', reject);
  55. req.end();
  56. });
  57. }
  58. async function getMap(query = ''): Promise<any> {
  59. const res = await request(`/api/map${query}`);
  60. expect(res.type).toBe('application/json; charset=utf-8');
  61. expect(res.status).toBe(200);
  62. return JSON.parse(res.body);
  63. }
  64. function write(root: string, rel: string, body: string): void {
  65. const full = path.join(root, rel);
  66. fs.mkdirSync(path.dirname(full), { recursive: true });
  67. fs.writeFileSync(full, body);
  68. }
  69. beforeAll(async () => {
  70. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-ui-map-'));
  71. projectRoot = path.join(tempDir, 'project');
  72. write(projectRoot, 'src/types.ts', `export interface Row {\n id: string;\n}\n`);
  73. write(
  74. projectRoot,
  75. 'src/db/schema.ts',
  76. `export const TABLES = ['rows'];\n`
  77. );
  78. // db -> core, the LIGHT direction of the mutual pair below.
  79. write(
  80. projectRoot,
  81. 'src/db/store.ts',
  82. `import { Row } from '../types';
  83. import { normalise } from '../core/util';
  84. export class Store {
  85. rows: Row[] = [];
  86. put(row: Row): void {
  87. this.rows.push(normalise(row));
  88. }
  89. }
  90. `
  91. );
  92. // util <-> store is a deliberate two-file import cycle: it gives the file
  93. // cycle report a component to find and the module graph a mutual pair.
  94. write(
  95. projectRoot,
  96. 'src/core/util.ts',
  97. `import { Row } from '../types';
  98. import { Store } from '../db/store';
  99. export function normalise(row: Row): Row {
  100. return { id: row.id.trim() };
  101. }
  102. export function count(store: Store): number {
  103. return store.rows.length;
  104. }
  105. `
  106. );
  107. // Two directory levels under `src`, so depth=2 has something real to split.
  108. write(
  109. projectRoot,
  110. 'src/core/passes/trim.ts',
  111. `import { Row } from '../../types';
  112. export function trim(row: Row): Row {
  113. return { id: row.id.slice(0, 8) };
  114. }
  115. `
  116. );
  117. // core -> db, several times over: the HEAVY direction.
  118. write(
  119. projectRoot,
  120. 'src/core/engine.ts',
  121. `import { Store } from '../db/store';
  122. import { TABLES } from '../db/schema';
  123. import { trim } from './passes/trim';
  124. import { Row } from '../types';
  125. export class Engine {
  126. store = new Store();
  127. boot(): string[] {
  128. return TABLES;
  129. }
  130. add(row: Row): void {
  131. this.store.put(trim(row));
  132. this.store.put(row);
  133. }
  134. }
  135. `
  136. );
  137. write(
  138. projectRoot,
  139. 'src/api/handler.ts',
  140. `import { Engine } from '../core/engine';
  141. import { Row } from '../types';
  142. export function handle(engine: Engine, row: Row): void {
  143. engine.add(row);
  144. }
  145. `
  146. );
  147. write(
  148. projectRoot,
  149. 'src/api/routes.ts',
  150. `import { Engine } from '../core/engine';
  151. import { handle } from './handler';
  152. export function route(engine: Engine): void {
  153. handle(engine, { id: 'x' });
  154. }
  155. `
  156. );
  157. write(
  158. projectRoot,
  159. 'src/index.ts',
  160. `import { Engine } from './core/engine';
  161. import { route } from './api/routes';
  162. export function start(): void {
  163. route(new Engine());
  164. }
  165. `
  166. );
  167. write(
  168. projectRoot,
  169. '__tests__/engine.test.ts',
  170. `import { Engine } from '../src/core/engine';
  171. export function testBoot(): string[] {
  172. return new Engine().boot();
  173. }
  174. `
  175. );
  176. const cg = CodeGraph.initSync(projectRoot, {
  177. config: { include: ['src/**/*.ts', '__tests__/**/*.ts'], exclude: [] },
  178. });
  179. await cg.indexAll();
  180. cg.resolveReferences();
  181. cg.close();
  182. const viewerDir = path.join(tempDir, 'viewer');
  183. fs.mkdirSync(viewerDir, { recursive: true });
  184. fs.writeFileSync(path.join(viewerDir, 'index.html'), '<!doctype html><div id="app"></div>');
  185. resetMapCache();
  186. api = createGraphApi({ projectRoot });
  187. server = await startUiServer({ projectRoot, viewerDir, port: 0, api: api.handler });
  188. }, 120_000);
  189. afterAll(async () => {
  190. api?.close();
  191. await server?.close();
  192. resetMapCache();
  193. if (tempDir && fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
  194. });
  195. describe('moduleIdFor', () => {
  196. it('names a module after the first `depth` segments under the root', () => {
  197. expect(moduleIdFor('src/core/engine.ts', 'src', 1)).toEqual({ id: 'src/core', facade: false });
  198. expect(moduleIdFor('src/a/b/c.ts', 'src', 2)).toEqual({ id: 'src/a/b', facade: false });
  199. expect(moduleIdFor('a/b/c.ts', '', 1)).toEqual({ id: 'a', facade: false });
  200. });
  201. it('keeps a façade as its own box and buckets the other loose files', () => {
  202. expect(moduleIdFor('src/index.ts', 'src', 1)).toEqual({ id: 'src/index.ts', facade: true });
  203. expect(moduleIdFor('src/lib.rs', 'src', 1)?.facade).toBe(true);
  204. expect(moduleIdFor('pkg/__init__.py', 'pkg', 1)?.facade).toBe(true);
  205. expect(moduleIdFor('src/types.ts', 'src', 1)).toEqual({
  206. id: 'src/(root files)',
  207. facade: false,
  208. });
  209. expect(moduleIdFor('types.ts', '', 1)).toEqual({ id: '(root files)', facade: false });
  210. });
  211. it('buckets a loose file into the directory it is actually in, not the top one', () => {
  212. // Two segments at depth 2 is a loose file inside `src/a`, so it belongs to
  213. // that directory's bucket. Folding it into `src/(root files)` would claim a
  214. // file lives somewhere it does not.
  215. expect(moduleIdFor('src/a/loose.ts', 'src', 2)).toEqual({
  216. id: 'src/a/(root files)',
  217. facade: false,
  218. });
  219. });
  220. it('returns null for a file outside the root', () => {
  221. expect(moduleIdFor('__tests__/x.test.ts', 'src', 1)).toBeNull();
  222. // A sibling whose name merely starts with the root is not under it.
  223. expect(moduleIdFor('srcx/y.ts', 'src', 1)).toBeNull();
  224. });
  225. });
  226. describe('normalizeRoot', () => {
  227. it('treats `src`, `src/` and `./src` as one root', () => {
  228. expect(normalizeRoot('src')).toBe('src');
  229. expect(normalizeRoot('src/')).toBe('src');
  230. expect(normalizeRoot('./src')).toBe('src');
  231. expect(normalizeRoot('src\\')).toBe('src');
  232. });
  233. it('treats the repository root as the empty string however it is written', () => {
  234. expect(normalizeRoot('')).toBe('');
  235. expect(normalizeRoot('.')).toBe('');
  236. expect(normalizeRoot('/')).toBe('');
  237. expect(normalizeRoot(undefined)).toBe('');
  238. });
  239. });
  240. describe('pickDefaultRoot', () => {
  241. it('picks the directory holding a clear majority of the non-test symbols', () => {
  242. expect(
  243. pickDefaultRoot([
  244. { path: 'src/a.ts', symbols: 80, test: false },
  245. { path: 'scripts/b.ts', symbols: 5, test: false },
  246. { path: '__tests__/c.ts', symbols: 900, test: true },
  247. ])
  248. ).toBe('src');
  249. });
  250. it('falls back to the repository root when no directory dominates', () => {
  251. expect(
  252. pickDefaultRoot([
  253. { path: 'a/one.ts', symbols: 10, test: false },
  254. { path: 'b/two.ts', symbols: 10, test: false },
  255. { path: 'c/three.ts', symbols: 10, test: false },
  256. ])
  257. ).toBe('');
  258. expect(pickDefaultRoot([{ path: 'flat.ts', symbols: 4, test: false }])).toBe('');
  259. });
  260. });
  261. describe('GET /api/map', () => {
  262. it('is listed by the API index', async () => {
  263. const res = await request('/api');
  264. const body = JSON.parse(res.body);
  265. expect(body.endpoints.map((e: any) => e.path)).toContain('/api/map');
  266. });
  267. it('opens on the source directory and keeps the façade its own box', async () => {
  268. const map = await getMap();
  269. expect(map.root).toBe('src');
  270. expect(map.depth).toBe(1);
  271. const ids = map.modules.map((m: any) => m.id);
  272. expect(ids).toEqual(['src/(root files)', 'src/api', 'src/core', 'src/db', 'src/index.ts']);
  273. expect(map.modules.find((m: any) => m.id === 'src/core').files).toBe(3);
  274. const facade = map.modules.find((m: any) => m.id === 'src/index.ts');
  275. expect(facade.facade).toBe(true);
  276. expect(facade.files).toBe(1);
  277. expect(facade.symbols).toBeGreaterThan(0);
  278. // Nothing under `src` is a test, so the default root already excludes them.
  279. expect(map.modules.every((m: any) => m.test === false)).toBe(true);
  280. });
  281. it('offers every top-level directory as a root, plus the repository itself', async () => {
  282. const map = await getMap();
  283. expect(map.roots[0]).toEqual({ root: '', label: 'whole repository', files: map.index.files });
  284. expect(map.roots.map((r: any) => r.root)).toEqual(
  285. expect.arrayContaining(['', 'src', '__tests__'])
  286. );
  287. });
  288. it('counts cross-module edges only, with a declared subset and named pairs', async () => {
  289. const map = await getMap();
  290. const link = map.links.find((l: any) => l.source === 'src/api' && l.target === 'src/core');
  291. expect(link).toBeTruthy();
  292. expect(link.count).toBeGreaterThan(0);
  293. // Every kind's count has to add up to the link's own count, or the tooltip
  294. // and the stroke width are describing two different things.
  295. expect(link.byKind.reduce((sum: number, k: any) => sum + k.count, 0)).toBe(link.count);
  296. // `import { Engine }` is a declared dependency; it must survive as one.
  297. expect(link.declared).toBeGreaterThan(0);
  298. expect(link.declared).toBeLessThanOrEqual(link.count);
  299. expect(link.topPairs.length).toBeGreaterThan(0);
  300. expect(link.topPairs.length).toBeLessThanOrEqual(4);
  301. expect(link.topPairs.every((p: any) => p.declared <= p.count)).toBe(true);
  302. // No module ever links to itself: same-module edges are not dependencies.
  303. expect(map.links.every((l: any) => l.source !== l.target)).toBe(true);
  304. });
  305. it('keeps the heavier direction of a mutual pair heavier', async () => {
  306. const map = await getMap();
  307. const coreToDb = map.links.find((l: any) => l.source === 'src/core' && l.target === 'src/db');
  308. const dbToCore = map.links.find((l: any) => l.source === 'src/db' && l.target === 'src/core');
  309. expect(coreToDb).toBeTruthy();
  310. expect(dbToCore).toBeTruthy();
  311. expect(coreToDb.count).toBeGreaterThan(dbToCore.count);
  312. });
  313. it('reports the file-level cycle the fixture contains', async () => {
  314. const map = await getMap();
  315. expect(map.cycles.total).toBeGreaterThanOrEqual(1);
  316. const knot = map.cycles.items.find((c: any) =>
  317. c.files.includes('src/core/util.ts') && c.files.includes('src/db/store.ts')
  318. );
  319. expect(knot, JSON.stringify(map.cycles)).toBeTruthy();
  320. expect(knot.size).toBe(knot.files.length);
  321. expect(knot.modules).toEqual(expect.arrayContaining(['src/core', 'src/db']));
  322. expect(map.cycles.shown).toBe(map.cycles.items.length);
  323. });
  324. it('lists each module\'s files, capped, with the true total beside them', async () => {
  325. const map = await getMap();
  326. for (const module of map.modules) {
  327. expect(module.fileList.total).toBe(module.files);
  328. expect(module.fileList.shown).toBe(module.fileList.items.length);
  329. expect(module.fileList.truncated).toBe(module.fileList.shown < module.fileList.total);
  330. expect(module.fileList.items).toEqual([...module.fileList.items].sort());
  331. }
  332. // A module's files are everything BELOW it, not just the files directly in
  333. // it: `src/core` at depth 1 owns `src/core/passes/trim.ts` too, and the
  334. // panel's list has to match the count on the box.
  335. const core = map.modules.find((m: any) => m.id === 'src/core');
  336. expect(core.fileList.items).toEqual([
  337. 'src/core/engine.ts',
  338. 'src/core/passes/trim.ts',
  339. 'src/core/util.ts',
  340. ]);
  341. });
  342. it('says how many references the confidence floor excluded', async () => {
  343. const map = await getMap();
  344. expect(map.excluded.confidenceBelow).toBe(0.6);
  345. expect(map.excluded.uncertainEdges).toBeGreaterThanOrEqual(0);
  346. });
  347. it('answers the whole repository, where the tests are a test module', async () => {
  348. const map = await getMap('?root=&depth=1');
  349. expect(map.root).toBe('');
  350. const ids = map.modules.map((m: any) => m.id);
  351. expect(ids).toEqual(expect.arrayContaining(['src', '__tests__']));
  352. expect(map.modules.find((m: any) => m.id === '__tests__').test).toBe(true);
  353. expect(map.modules.find((m: any) => m.id === 'src').test).toBe(false);
  354. expect(map.links.some((l: any) => l.source === '__tests__' && l.target === 'src')).toBe(true);
  355. });
  356. it('splits deeper when asked, and `src/` is the same root as `src`', async () => {
  357. const deep = await getMap('?root=src&depth=2');
  358. const ids = deep.modules.map((m: any) => m.id);
  359. // A directory two levels down becomes its own box; a file loose one level
  360. // down joins that level's bucket rather than being promoted to a module.
  361. expect(ids).toContain('src/core/passes');
  362. expect(ids).toContain('src/core/(root files)');
  363. expect(ids).toContain('src/api/(root files)');
  364. expect(ids).not.toContain('src/core');
  365. const slashed = await getMap('?root=src%2F&depth=2');
  366. expect(slashed.modules).toEqual(deep.modules);
  367. });
  368. it('rejects an out-of-range depth as JSON, not as a crash', async () => {
  369. const res = await request('/api/map?depth=9');
  370. expect(res.status).toBe(400);
  371. expect(res.type).toBe('application/json; charset=utf-8');
  372. const body = JSON.parse(res.body);
  373. expect(body.code).toBe('bad-request');
  374. expect(body.error).toContain('depth');
  375. });
  376. it('serves the second identical request from the cache, byte for byte', async () => {
  377. // Other cases in this file have already warmed `src` at depth 1; the point
  378. // here is the first-then-second transition, so start from a cold cache.
  379. resetMapCache();
  380. const first = await getMap('?root=src&depth=1');
  381. const second = await getMap('?root=src&depth=1');
  382. expect(first.timing.cached).toBe(false);
  383. expect(second.timing.cached).toBe(true);
  384. // Everything except the timing stamp must be identical — a map that is not
  385. // reproducible between two reloads is not a map of anything.
  386. const strip = (m: any) => JSON.stringify({ ...m, timing: undefined });
  387. expect(strip(second)).toBe(strip(first));
  388. });
  389. it('does not let one root\'s answer be served for another', async () => {
  390. const src = await getMap('?root=src&depth=1');
  391. const all = await getMap('?root=&depth=1');
  392. expect(all.root).toBe('');
  393. expect(all.modules.map((m: any) => m.id)).not.toEqual(src.modules.map((m: any) => m.id));
  394. });
  395. });