ui-server-api.test.ts 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /**
  2. * The `codegraph ui` read-only JSON API (CG-42).
  3. *
  4. * Everything runs against a real indexed fixture project over a real loopback
  5. * server — no mocks — because the properties worth pinning are the ones that
  6. * only exist end to end: the drift verdict comes from hashing bytes on disk
  7. * against what the index stored, the refusals come from the same chokepoint the
  8. * static server uses, and the caps only matter once a symbol really does have
  9. * hundreds of callers.
  10. *
  11. * The fixture is built to produce each of those: a call chain three deep, a
  12. * test file that reaches it, a type used only as a type, an import that cannot
  13. * resolve, and one deliberately hot function with 500 callers.
  14. */
  15. import { describe, it, expect, beforeAll, afterAll } from 'vitest';
  16. import * as http from 'http';
  17. import * as fs from 'fs';
  18. import * as os from 'os';
  19. import * as path from 'path';
  20. import CodeGraph from '../src/index';
  21. import { createGraphApi, startUiServer, type GraphApi, type UiServerHandle } from '../src/ui-server';
  22. interface Response {
  23. status: number;
  24. headers: http.IncomingHttpHeaders;
  25. body: string;
  26. }
  27. let server: UiServerHandle;
  28. let api: GraphApi;
  29. let tempDir: string;
  30. let projectRoot: string;
  31. let viewerDir: string;
  32. /**
  33. * One request against a live server, with the loopback `Host` the boundary
  34. * wants. Written with `http.request` rather than `fetch` so the `Host` header
  35. * is ours to set — undici treats it as forbidden.
  36. */
  37. function requestOn(port: number, requestPath: string, method = 'GET'): Promise<Response> {
  38. return new Promise((resolve, reject) => {
  39. const req = http.request(
  40. {
  41. host: '127.0.0.1',
  42. port,
  43. path: requestPath,
  44. method,
  45. headers: { Host: `127.0.0.1:${port}` },
  46. setHost: false,
  47. },
  48. (res) => {
  49. const chunks: Buffer[] = [];
  50. res.on('data', (c: Buffer) => chunks.push(c));
  51. res.on('end', () =>
  52. resolve({
  53. status: res.statusCode ?? 0,
  54. headers: res.headers,
  55. body: Buffer.concat(chunks).toString('utf-8'),
  56. })
  57. );
  58. }
  59. );
  60. req.on('error', reject);
  61. req.end();
  62. });
  63. }
  64. /** The same, against the main fixture's server. */
  65. function request(requestPath: string, method = 'GET'): Promise<Response> {
  66. return requestOn(server.port, requestPath, method);
  67. }
  68. /**
  69. * Payloads are read as `any` on purpose: these tests assert the JSON contract
  70. * the viewer sees over the wire, so typing them against the server's own
  71. * interfaces would only prove the server agrees with itself.
  72. */
  73. async function getJson(requestPath: string): Promise<any> {
  74. const res = await request(requestPath);
  75. expect(res.headers['content-type']).toBe('application/json; charset=utf-8');
  76. return JSON.parse(res.body);
  77. }
  78. async function getStatusAndJson(requestPath: string): Promise<{ status: number; body: any }> {
  79. const res = await request(requestPath);
  80. expect(res.headers['content-type']).toBe('application/json; charset=utf-8');
  81. return { status: res.status, body: JSON.parse(res.body) };
  82. }
  83. /** Find a symbol in the fixture by name, through the API itself. */
  84. async function idOf(name: string, kind?: string): Promise<string> {
  85. const search = await getJson(`/api/search?q=${encodeURIComponent(name)}`);
  86. const hit = search.results.items.find(
  87. (r: any) => r.name === name && (kind === undefined || r.kind === kind)
  88. );
  89. expect(hit, `no ${kind ?? 'symbol'} named ${name} in the fixture`).toBeTruthy();
  90. return hit.id as string;
  91. }
  92. beforeAll(async () => {
  93. tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-ui-api-'));
  94. projectRoot = path.join(tempDir, 'project');
  95. const srcDir = path.join(projectRoot, 'src');
  96. const testsDir = path.join(projectRoot, '__tests__');
  97. fs.mkdirSync(srcDir, { recursive: true });
  98. fs.mkdirSync(testsDir, { recursive: true });
  99. fs.writeFileSync(
  100. path.join(srcDir, 'types.ts'),
  101. `export interface Config {
  102. ttlMs: number;
  103. label: string;
  104. }
  105. export type CacheKey = string;
  106. `
  107. );
  108. fs.writeFileSync(
  109. path.join(srcDir, 'cache.ts'),
  110. `import { Config, CacheKey } from './types';
  111. export class Cache {
  112. private store = new Map<string, string>();
  113. private config: Config;
  114. constructor(config: Config) {
  115. this.config = config;
  116. }
  117. read(key: CacheKey): string | undefined {
  118. return this.store.get(key);
  119. }
  120. write(key: CacheKey, value: string): void {
  121. this.store.set(key, value);
  122. }
  123. }
  124. `
  125. );
  126. fs.writeFileSync(
  127. path.join(srcDir, 'service.ts'),
  128. `import { Cache } from './cache';
  129. import { Config } from './types';
  130. // Not in the index: a package that was never installed here.
  131. import { serialize } from 'some-external-package';
  132. export class Service {
  133. private cache: Cache;
  134. constructor(config: Config) {
  135. this.cache = new Cache(config);
  136. }
  137. load(key: string): string {
  138. const hit = this.cache.read(key);
  139. if (hit !== undefined) return hit;
  140. const fresh = serialize(key);
  141. this.cache.write(key, fresh);
  142. return fresh;
  143. }
  144. }
  145. `
  146. );
  147. fs.writeFileSync(
  148. path.join(srcDir, 'handler.ts'),
  149. `import { Service } from './service';
  150. export function handleRequest(service: Service, key: string): string {
  151. return service.load(key);
  152. }
  153. `
  154. );
  155. // Module-level statements: the engine records them as edges out of the FILE
  156. // node, which is the only reason `/api/entrypoints` can see an executable
  157. // root at all. Nothing else in the fixture runs anything on the way down.
  158. fs.writeFileSync(
  159. path.join(srcDir, 'main.ts'),
  160. `import { Service } from './service';
  161. import { handleRequest } from './handler';
  162. const service = new Service({ ttlMs: 5, label: 'main' });
  163. const first = handleRequest(service, 'boot');
  164. const second = service.load('warm');
  165. export const started = [first, second];
  166. `
  167. );
  168. // 500 callers into one function: the N+1 and capping behaviour only shows up
  169. // at this scale, and the fixture keeps CI honest without needing the engine's
  170. // own index to be present.
  171. const callers = Array.from(
  172. { length: 500 },
  173. (_, i) => `export function caller${i}(): number {\n return hot(${i});\n}`
  174. ).join('\n\n');
  175. fs.writeFileSync(
  176. path.join(srcDir, 'hot.ts'),
  177. `export function hot(n: number): number {
  178. return n * 2;
  179. }
  180. ${callers}
  181. `
  182. );
  183. // CRLF on purpose: tree-sitter numbers rows by `\n`, so a CRLF file must come
  184. // back with the same line numbers the graph recorded — and without the stray
  185. // `\r` rendering at the end of every line. This is what a Windows checkout
  186. // with core.autocrlf looks like, and it is decided by bytes, not by the OS.
  187. fs.writeFileSync(
  188. path.join(srcDir, 'crlf.ts'),
  189. ['export function windowsStyle(n: number): number {', ' return n + 1;', '}', ''].join('\r\n')
  190. );
  191. fs.writeFileSync(
  192. path.join(testsDir, 'service.test.ts'),
  193. `import { Service } from '../src/service';
  194. export function testLoadsThroughCache(): void {
  195. const service = new Service({ ttlMs: 1, label: 'x' });
  196. service.load('k');
  197. }
  198. // Module level, on purpose: a test file that RUNS something must still be
  199. // excluded from the entry points.
  200. testLoadsThroughCache();
  201. `
  202. );
  203. const cg = CodeGraph.initSync(projectRoot, {
  204. config: { include: ['src/**/*.ts', '__tests__/**/*.ts'], exclude: [] },
  205. });
  206. await cg.indexAll();
  207. cg.resolveReferences();
  208. // Hand the index over: the API opens its own read-only connection, which is
  209. // also what happens in production (the CLI never shares an instance).
  210. cg.close();
  211. viewerDir = path.join(tempDir, 'viewer');
  212. fs.mkdirSync(viewerDir, { recursive: true });
  213. fs.writeFileSync(path.join(viewerDir, 'index.html'), '<!doctype html><div id="app"></div>');
  214. api = createGraphApi({ projectRoot });
  215. server = await startUiServer({ projectRoot, viewerDir, port: 0, api: api.handler });
  216. }, 120_000);
  217. afterAll(async () => {
  218. api?.close();
  219. await server?.close();
  220. if (tempDir && fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
  221. });
  222. describe('GET /api', () => {
  223. it('lists the endpoints it answers', async () => {
  224. const body = await getJson('/api');
  225. expect(body.readOnly).toBe(true);
  226. const paths = body.endpoints.map((e: any) => e.path);
  227. expect(paths).toEqual(
  228. expect.arrayContaining([
  229. '/api/stats',
  230. '/api/search',
  231. '/api/node/<id>',
  232. '/api/source',
  233. '/api/file/<path>',
  234. '/api/routes',
  235. ])
  236. );
  237. });
  238. it('404s an unknown endpoint as JSON, never as the app shell', async () => {
  239. const { status, body } = await getStatusAndJson('/api/nope');
  240. expect(status).toBe(404);
  241. expect(body.code).toBe('not-found');
  242. });
  243. it('answers HEAD with the headers and no body', async () => {
  244. const res = await request('/api/stats', 'HEAD');
  245. expect(res.status).toBe(200);
  246. expect(res.headers['content-type']).toBe('application/json; charset=utf-8');
  247. expect(Number(res.headers['content-length'])).toBeGreaterThan(0);
  248. expect(res.body).toBe('');
  249. });
  250. });
  251. describe('GET /api/stats', () => {
  252. it('reports the project, the index state and the graph counts', async () => {
  253. const body = await getJson('/api/stats');
  254. expect(body.project.root).toBe(projectRoot);
  255. expect(body.project.name).toBe('project');
  256. expect(body.index.state).toBe('complete');
  257. expect(body.index.stale).toBe(false);
  258. expect(typeof body.index.lastIndexedAt).toBe('number');
  259. expect(body.index.backend).toBe('node-sqlite');
  260. expect(typeof body.index.extractionVersion).toBe('number');
  261. expect(body.graph.nodes).toBeGreaterThan(0);
  262. expect(body.graph.edges).toBeGreaterThan(0);
  263. expect(body.graph.files).toBeGreaterThanOrEqual(6);
  264. expect(body.graph.nodesByKind.class).toBeGreaterThanOrEqual(2);
  265. expect(body.graph.filesByLanguage.typescript).toBeGreaterThanOrEqual(6);
  266. // The thresholds travel with the data so the viewer's copy cannot drift.
  267. expect(body.thresholds).toEqual({ hub: 40, uncertainBelow: 0.6 });
  268. });
  269. it('reports a blast-radius scale the widest symbol in the index reaches', async () => {
  270. const body = await getJson('/api/stats');
  271. const scale = body.blastScale;
  272. // `hot` is called by 500 distinct functions and nothing else in the fixture
  273. // comes close, so the exact maximum is knowable here.
  274. expect(scale.maxDirect).toBe(500);
  275. // Its radius is at least its own callers; the sample is capped, so the
  276. // count is a floor and the flag says so rather than claiming exhaustive.
  277. expect(scale.maxWithinHops).toBeGreaterThanOrEqual(500);
  278. expect(scale.hops).toBe(3);
  279. expect(scale.sampled).toBeGreaterThan(0);
  280. expect(scale.sampled).toBeLessThanOrEqual(24);
  281. expect(scale.estimated).toBe(true);
  282. });
  283. it('serves the scale from cache — the second call does not re-traverse', async () => {
  284. const first = await getJson('/api/stats');
  285. const started = Date.now();
  286. const second = await getJson('/api/stats');
  287. expect(second.blastScale).toEqual(first.blastScale);
  288. // 24 depth-3 traversals over a 500-caller graph are not free; a cached
  289. // answer is. The margin is wide because this is a smoke test for the
  290. // memo existing at all, not a benchmark.
  291. expect(Date.now() - started).toBeLessThan(250);
  292. });
  293. });
  294. describe('GET /api/search', () => {
  295. it('ranks exact over prefix over substring, and groups by kind', async () => {
  296. const body = await getJson('/api/search?q=Cache');
  297. const first = body.results.items[0];
  298. expect(first.name).toBe('Cache');
  299. expect(first.kind).toBe('class');
  300. expect(first.matchKind).toBe('exact');
  301. const ranks = body.results.items.map((r: any) => r.matchKind);
  302. const order = ['exact', 'prefix', 'substring', 'qualified', 'file', 'related'];
  303. const asNumbers = ranks.map((r: string) => order.indexOf(r));
  304. expect(asNumbers).toEqual([...asNumbers].sort((a, b) => a - b));
  305. // Flattening the groups reproduces the flat ranking, so the palette can use
  306. // either without them disagreeing.
  307. const flattened = body.groups.flatMap((g: any) => g.items.map((i: any) => i.id));
  308. expect(new Set(flattened)).toEqual(new Set(body.results.items.map((r: any) => r.id)));
  309. for (const group of body.groups) expect(group.count).toBe(group.items.length);
  310. });
  311. it('returns a signature and a file:line for every result', async () => {
  312. const body = await getJson('/api/search?q=handleRequest');
  313. const hit = body.results.items.find((r: any) => r.name === 'handleRequest');
  314. expect(hit.file).toBe('src/handler.ts');
  315. expect(hit.line).toBeGreaterThan(0);
  316. expect(hit.endLine).toBeGreaterThanOrEqual(hit.line);
  317. expect(hit.signature).toContain('service');
  318. expect(hit.qualifiedName).toBeTruthy();
  319. expect(hit.language).toBe('typescript');
  320. });
  321. it('finds a mid-name match FTS tokens cannot', async () => {
  322. const body = await getJson('/api/search?q=quest');
  323. const names = body.results.items.map((r: any) => r.name);
  324. expect(names).toContain('handleRequest');
  325. const hit = body.results.items.find((r: any) => r.name === 'handleRequest');
  326. expect(hit.matchKind).toBe('substring');
  327. });
  328. it('honours the kind: filter grammar', async () => {
  329. const body = await getJson('/api/search?q=' + encodeURIComponent('kind:class Cache'));
  330. expect(body.filters.kinds).toEqual(['class']);
  331. expect(body.results.items.every((r: any) => r.kind === 'class')).toBe(true);
  332. });
  333. it('marks test files so the palette can rank them down', async () => {
  334. const body = await getJson('/api/search?q=testLoadsThroughCache');
  335. const hit = body.results.items.find((r: any) => r.name === 'testLoadsThroughCache');
  336. expect(hit.test).toBe(true);
  337. });
  338. it('answers an empty search box with nothing, and a missing q with 400', async () => {
  339. const empty = await getStatusAndJson('/api/search?q=');
  340. expect(empty.status).toBe(200);
  341. expect(empty.body.results.total).toBe(0);
  342. expect(empty.body.groups).toEqual([]);
  343. const missing = await getStatusAndJson('/api/search');
  344. expect(missing.status).toBe(400);
  345. expect(missing.body.code).toBe('bad-request');
  346. });
  347. it('returns an empty result set for a name nothing has', async () => {
  348. const body = await getJson('/api/search?q=zzznotasymbolanywhere');
  349. expect(body.results.total).toBe(0);
  350. });
  351. });
  352. describe('GET /api/node/<id>', () => {
  353. it('returns the symbol, its ancestors and its members in source order', async () => {
  354. const body = await getJson(`/api/node/${await idOf('Cache', 'class')}`);
  355. expect(body.node.name).toBe('Cache');
  356. expect(body.node.kind).toBe('class');
  357. expect(body.node.file).toBe('src/cache.ts');
  358. expect(body.node.lines).toBe(body.node.endLine - body.node.line + 1);
  359. expect(body.node.exported).toBe(true);
  360. // Outermost first: the file, then anything between it and the symbol.
  361. expect(body.ancestors[0].kind).toBe('file');
  362. expect(body.ancestors[0].file).toBe('src/cache.ts');
  363. const members = body.members.items.map((m: any) => m.name);
  364. expect(members).toEqual(expect.arrayContaining(['read', 'write', 'store', 'config']));
  365. const lines = body.members.items.map((m: any) => m.line);
  366. expect(lines).toEqual([...lines].sort((a, b) => a - b));
  367. for (const member of body.members.items) {
  368. expect(member.parentId).toBe(body.node.id);
  369. expect(member.depth).toBe(1);
  370. }
  371. expect(body.members.total).toBe(body.members.shown);
  372. });
  373. it('gives every member its own fan-in and fan-out — the outline is the body', async () => {
  374. const body = await getJson(`/api/node/${await idOf('Cache', 'class')}`);
  375. const byName = new Map(body.members.items.map((m: any) => [m.name, m]));
  376. for (const member of body.members.items) {
  377. expect(typeof member.fanIn).toBe('number');
  378. expect(typeof member.fanOut).toBe('number');
  379. expect(member.fanIn).toBeGreaterThanOrEqual(0);
  380. expect(member.fanOut).toBeGreaterThanOrEqual(0);
  381. }
  382. // `Service.load` calls both, and `Cache` contains them: at least the
  383. // containment edge plus one call each. Without these numbers a 700-line
  384. // class's outline cannot say which member carries weight.
  385. expect((byName.get('read') as any).fanIn).toBeGreaterThanOrEqual(2);
  386. expect((byName.get('write') as any).fanIn).toBeGreaterThanOrEqual(2);
  387. // The class itself calls nothing — its methods do, which is exactly why
  388. // the per-member counts have to come from the members.
  389. expect(body.counts.callees).toBe(0);
  390. expect(body.members.items.some((m: any) => m.fanOut > 0)).toBe(true);
  391. });
  392. it('nests a file outline one level deeper, so a class shows its methods', async () => {
  393. const body = await getJson(`/api/node/${await idOf('cache.ts', 'file')}`);
  394. const byDepth = new Map<number, string[]>();
  395. for (const member of body.members.items) {
  396. byDepth.set(member.depth, [...(byDepth.get(member.depth) ?? []), member.name]);
  397. }
  398. expect(byDepth.get(1)).toContain('Cache');
  399. expect(byDepth.get(2)).toEqual(expect.arrayContaining(['read', 'write']));
  400. });
  401. it('groups incoming edges by the calling symbol, with their call sites', async () => {
  402. const readId = await idOf('read', 'method');
  403. const body = await getJson(`/api/node/${readId}`);
  404. const fromLoad = body.incoming.items.find((r: any) => r.node.name === 'load');
  405. expect(fromLoad, 'Service.load should call Cache.read').toBeTruthy();
  406. expect(fromLoad.node.file).toBe('src/service.ts');
  407. expect(fromLoad.edgeKinds).toContain('calls');
  408. expect(fromLoad.edgeCount).toBeGreaterThanOrEqual(1);
  409. expect(fromLoad.lines.length).toBeGreaterThanOrEqual(1);
  410. expect(fromLoad.lines).toEqual([...fromLoad.lines].sort((a: number, b: number) => a - b));
  411. expect(typeof fromLoad.fanIn).toBe('number');
  412. expect(fromLoad.hub).toBe(false);
  413. });
  414. it('carries every edge attribute the viewer draws with', async () => {
  415. const body = await getJson(`/api/node/${await idOf('read', 'method')}`);
  416. const relation = body.incoming.items.find((r: any) => r.node.name === 'load');
  417. const edge = relation.edges[0];
  418. expect(edge.kind).toBe('calls');
  419. expect(typeof edge.line).toBe('number');
  420. expect(typeof edge.col).toBe('number');
  421. expect(typeof edge.confidence).toBe('number');
  422. expect(typeof edge.resolvedBy).toBe('string');
  423. // Confidence decides the uncertain fold; the group agrees with its edges.
  424. expect(relation.confidence).toBe(
  425. Math.max(...relation.edges.map((e: any) => e.confidence ?? -1))
  426. );
  427. expect(relation.uncertain).toBe(relation.confidence < 0.6);
  428. expect(relation.synthesized).toBe(false);
  429. });
  430. it('groups outgoing edges by the called symbol, ordered by call site', async () => {
  431. const body = await getJson(`/api/node/${await idOf('load', 'method')}`);
  432. const names = body.outgoing.items.map((r: any) => r.node.name);
  433. expect(names).toEqual(expect.arrayContaining(['read', 'write']));
  434. const firstLines = body.outgoing.items
  435. .map((r: any) => r.lines[0])
  436. .filter((l: number | undefined) => l !== undefined);
  437. expect(firstLines).toEqual([...firstLines].sort((a, b) => a - b));
  438. });
  439. it('splits type references out of the callee rail', async () => {
  440. // Type edges attach to the MEMBER that names the type, not to its class:
  441. // `Service`'s constructor is where `Config` and `new Cache(...)` both live,
  442. // which makes it the one place both halves of the split are visible.
  443. const service = await getJson(`/api/node/${await idOf('Service', 'class')}`);
  444. const ctor = service.members.items.find((m: any) => m.name === 'constructor');
  445. const body = await getJson(`/api/node/${ctor.id}`);
  446. const typeNames = body.typesUsed.map((t: any) => t.node.name);
  447. expect(typeNames).toContain('Config');
  448. expect(body.typesUsed.every((t: any) => t.edgeKinds.includes('references'))).toBe(true);
  449. // A type reference is not also a callee row...
  450. expect(body.outgoing.items.map((r: any) => r.node.name)).not.toContain('Config');
  451. // ...but a class reached by any other edge kind still is: `new Cache(...)`
  452. // is an `instantiates` edge, and moving it would hide a real dependency.
  453. const instantiated = body.outgoing.items.find((r: any) => r.node.name === 'Cache');
  454. expect(instantiated).toBeTruthy();
  455. expect(instantiated.edgeKinds).toContain('instantiates');
  456. });
  457. it('summarizes which tests reach the symbol', async () => {
  458. const reached = await getJson(`/api/node/${await idOf('load', 'method')}`);
  459. expect(reached.tests.reached).toBe(true);
  460. expect(reached.tests.hops).toBe(1);
  461. expect(reached.tests.files).toContain('__tests__/service.test.ts');
  462. expect(reached.tests.fileCount).toBeGreaterThanOrEqual(1);
  463. expect(reached.tests.files.length).toBeLessThanOrEqual(6);
  464. expect(reached.tests.exhaustive).toBe(true);
  465. const unreached = await getJson(`/api/node/${await idOf('hot', 'function')}`);
  466. expect(unreached.tests.reached).toBe(false);
  467. expect(unreached.tests.hops).toBeNull();
  468. expect(unreached.tests.files).toEqual([]);
  469. });
  470. it('counts the calls that leave the index instead of hiding them', async () => {
  471. const body = await getJson(`/api/node/${await idOf('load', 'method')}`);
  472. expect(body.outsideIndex.total).toBeGreaterThan(0);
  473. const names = body.outsideIndex.samples.map((s: any) => s.name);
  474. expect(names).toContain('serialize');
  475. for (const sample of body.outsideIndex.samples) {
  476. expect(typeof sample.line).toBe('number');
  477. expect(typeof sample.kind).toBe('string');
  478. }
  479. });
  480. it('summarizes the blast radius at three hops', async () => {
  481. const body = await getJson(`/api/node/${await idOf('read', 'method')}`);
  482. expect(body.blast.hops).toBe(3);
  483. expect(body.blast.direct).toBe(body.counts.callers);
  484. // load → handleRequest / the test both sit inside three hops of Cache.read.
  485. expect(body.blast.withinHops).toBeGreaterThan(body.blast.direct);
  486. expect(body.blast.files).toBeGreaterThanOrEqual(2);
  487. expect(body.blast.testFiles).toBeGreaterThanOrEqual(1);
  488. expect(body.blast.routes).toBe(0);
  489. expect(body.blast.topFiles[0].symbols).toBeGreaterThanOrEqual(1);
  490. });
  491. it('keeps every count equal to the list it labels', async () => {
  492. const body = await getJson(`/api/node/${await idOf('read', 'method')}`);
  493. expect(body.counts.callers).toBe(body.incoming.total);
  494. expect(body.counts.callees).toBe(body.outgoing.total);
  495. expect(body.counts.typesUsed).toBe(body.typesUsed.length);
  496. expect(body.counts.members).toBe(body.members.total);
  497. expect(body.blast.direct).toBe(body.counts.callers);
  498. });
  499. it('reports fan-in, fan-out and the hub flag', async () => {
  500. const quiet = await getJson(`/api/node/${await idOf('write', 'method')}`);
  501. expect(quiet.counts.hub).toBe(false);
  502. expect(quiet.counts.callers).toBeLessThan(40);
  503. expect(quiet.counts.fanIn).toBeGreaterThanOrEqual(quiet.counts.callers);
  504. const hot = await getJson(`/api/node/${await idOf('hot', 'function')}`);
  505. expect(hot.counts.hub).toBe(true);
  506. expect(hot.counts.callers).toBeGreaterThanOrEqual(500);
  507. });
  508. it('flags nothing as drifted while the fixture is untouched', async () => {
  509. const body = await getJson(`/api/node/${await idOf('read', 'method')}`);
  510. expect(body.drift).toBe(false);
  511. });
  512. it('404s an id that names nothing, and 400s an empty one', async () => {
  513. const missing = await getStatusAndJson('/api/node/method:notarealid');
  514. expect(missing.status).toBe(404);
  515. expect(missing.body.code).toBe('not-found');
  516. expect(missing.body.hint).toBeTruthy();
  517. const empty = await getStatusAndJson('/api/node/');
  518. expect(empty.status).toBe(400);
  519. });
  520. });
  521. describe('GET /api/node/<id> — the busiest symbol', () => {
  522. it('caps the caller list, keeps the true total, and stays fast', async () => {
  523. const hotId = await idOf('hot', 'function');
  524. await request(`/api/node/${hotId}`); // warm the connection and the caches
  525. const started = performance.now();
  526. const res = await request(`/api/node/${hotId}`);
  527. const elapsed = performance.now() - started;
  528. expect(res.status).toBe(200);
  529. const body = JSON.parse(res.body);
  530. expect(body.incoming.total).toBeGreaterThanOrEqual(500);
  531. expect(body.incoming.shown).toBe(300);
  532. expect(body.incoming.truncated).toBe(true);
  533. expect(body.incoming.items).toHaveLength(300);
  534. // Grouped: one row per calling symbol, each carrying its own call sites.
  535. expect(new Set(body.incoming.items.map((r: any) => r.node.id)).size).toBe(300);
  536. expect(body.counts.callers).toBeGreaterThanOrEqual(500);
  537. expect(body.blast.direct).toBe(body.counts.callers);
  538. // 500 callers resolved one query at a time would be nowhere near this.
  539. expect(elapsed).toBeLessThan(100);
  540. });
  541. });
  542. describe('GET /api/source', () => {
  543. it('returns the requested slice with the index line numbering', async () => {
  544. const body = await getJson('/api/source?file=src/cache.ts&from=1&to=3');
  545. expect(body.drift).toBe(false);
  546. expect(body.file).toBe('src/cache.ts');
  547. expect(body.language).toBe('typescript');
  548. expect(body.from).toBe(1);
  549. expect(body.to).toBe(3);
  550. expect(body.lines).toHaveLength(3);
  551. expect(body.lines[0]).toContain("import { Config, CacheKey } from './types'");
  552. expect(body.totalLines).toBeGreaterThan(3);
  553. expect(body.truncated).toBe(false);
  554. });
  555. it('serves the whole file when no range is given', async () => {
  556. const body = await getJson('/api/source?file=src/handler.ts');
  557. expect(body.from).toBe(1);
  558. expect(body.to).toBe(body.totalLines);
  559. expect(body.lines).toHaveLength(body.totalLines);
  560. });
  561. it('slices exactly the lines a symbol claims', async () => {
  562. const node = await getJson(`/api/node/${await idOf('handleRequest', 'function')}`);
  563. const body = await getJson(
  564. `/api/source?file=${node.node.file}&from=${node.node.line}&to=${node.node.endLine}`
  565. );
  566. expect(body.lines[0]).toContain('handleRequest');
  567. expect(body.lines).toHaveLength(node.node.lines);
  568. });
  569. it('carries the classified source beside the lines, one entry per line', async () => {
  570. const body = await getJson('/api/source?file=src/cache.ts&from=1&to=3');
  571. // Highlighting rides with the slice rather than behind its own endpoint:
  572. // the two are only ever wanted together, and a second round-trip would let
  573. // the code block paint unhighlighted source and then reflow it.
  574. expect(body.highlight).toBeTruthy();
  575. expect(body.highlight.classes).toEqual([
  576. 'other',
  577. 'ident',
  578. 'comment',
  579. 'string',
  580. 'keyword',
  581. 'number',
  582. ]);
  583. expect(body.highlight.lines).toHaveLength(body.lines.length);
  584. // Every line's tokens reproduce that line exactly — the code block renders
  585. // these, not the raw string.
  586. for (let i = 0; i < body.lines.length; i++) {
  587. const rebuilt = body.highlight.lines[i].map(([, text]: [number, string]) => text).join('');
  588. expect(rebuilt).toBe(body.lines[i]);
  589. }
  590. });
  591. it('refuses to slice a file that changed on disk after the last sync', async () => {
  592. const target = path.join(projectRoot, 'src', 'handler.ts');
  593. const original = fs.readFileSync(target);
  594. try {
  595. fs.writeFileSync(target, Buffer.concat([Buffer.from('// a new first line\n'), original]));
  596. const body = await getJson('/api/source?file=src/handler.ts&from=1&to=3');
  597. expect(body.drift).toBe(true);
  598. // The whole point: no slice, rather than a slice of the wrong lines.
  599. expect(body.lines).toBeUndefined();
  600. // And nothing to render it with either — a highlight with no source is
  601. // just a second way to draw the wrong lines.
  602. expect(body.highlight).toBeUndefined();
  603. expect(body.reason).toContain('changed on disk after the last index sync');
  604. // And every screen that renders indexed line ranges is told.
  605. const node = await getJson(`/api/node/${await idOf('handleRequest', 'function')}`);
  606. expect(node.drift).toBe(true);
  607. const file = await getJson('/api/file/src/handler.ts');
  608. expect(file.drift).toBe(true);
  609. } finally {
  610. fs.writeFileSync(target, original);
  611. }
  612. });
  613. it('does not call an identical rewrite drift', async () => {
  614. const target = path.join(projectRoot, 'src', 'handler.ts');
  615. const original = fs.readFileSync(target);
  616. // Same bytes, new mtime — what a checkout or a formatter no-op looks like.
  617. fs.writeFileSync(target, original);
  618. const body = await getJson('/api/source?file=src/handler.ts&from=1&to=2');
  619. expect(body.drift).toBe(false);
  620. expect(body.lines).toHaveLength(2);
  621. });
  622. it('keeps a CRLF file on the index line numbering, without the stray carriage returns', async () => {
  623. const node = await getJson(`/api/node/${await idOf('windowsStyle', 'function')}`);
  624. expect(node.node.file).toBe('src/crlf.ts');
  625. const body = await getJson('/api/source?file=src/crlf.ts');
  626. expect(body.drift).toBe(false);
  627. expect(body.totalLines).toBe(3);
  628. expect(body.lines).toEqual([
  629. 'export function windowsStyle(n: number): number {',
  630. ' return n + 1;',
  631. '}',
  632. ]);
  633. expect(body.lines.some((l: string) => l.includes('\r'))).toBe(false);
  634. // The symbol's indexed range still names its own body.
  635. const slice = await getJson(
  636. `/api/source?file=src/crlf.ts&from=${node.node.line}&to=${node.node.endLine}`
  637. );
  638. expect(slice.lines[0]).toContain('windowsStyle');
  639. });
  640. it('refuses a path that escapes the project', async () => {
  641. const traversal = await getStatusAndJson(
  642. '/api/source?file=' + encodeURIComponent('../../../etc/passwd')
  643. );
  644. expect(traversal.status).toBe(403);
  645. expect(traversal.body.code).toBe('refused');
  646. const absolute = await getStatusAndJson(
  647. '/api/source?file=' + encodeURIComponent('/etc/passwd')
  648. );
  649. expect(absolute.status).toBe(403);
  650. expect(absolute.body.code).toBe('refused');
  651. expect(absolute.body.error).toContain('absolute');
  652. });
  653. it('refuses a NUL byte in the path', async () => {
  654. const { status, body } = await getStatusAndJson(
  655. '/api/source?file=' + encodeURIComponent('src/cache.ts\u0000.png')
  656. );
  657. expect(status).toBe(403);
  658. expect(body.code).toBe('refused');
  659. });
  660. it('404s a file that exists but is not indexed', async () => {
  661. fs.writeFileSync(path.join(projectRoot, 'notes.md'), '# not indexed\n');
  662. const { status, body } = await getStatusAndJson('/api/source?file=notes.md');
  663. expect(status).toBe(404);
  664. expect(body.code).toBe('not-found');
  665. expect(body.hint).toContain('index');
  666. });
  667. it('rejects a range that names nothing', async () => {
  668. const past = await getStatusAndJson('/api/source?file=src/handler.ts&from=99999');
  669. expect(past.status).toBe(400);
  670. expect(past.body.error).toContain('past the end');
  671. const backwards = await getStatusAndJson('/api/source?file=src/handler.ts&from=10&to=4');
  672. expect(backwards.status).toBe(400);
  673. const nonNumeric = await getStatusAndJson('/api/source?file=src/handler.ts&from=abc');
  674. expect(nonNumeric.status).toBe(400);
  675. });
  676. });
  677. describe('GET /api/file/<path>', () => {
  678. it('returns the file record and its outline in source order', async () => {
  679. const body = await getJson('/api/file/src/cache.ts');
  680. expect(body.file.path).toBe('src/cache.ts');
  681. expect(body.file.language).toBe('typescript');
  682. expect(body.file.size).toBeGreaterThan(0);
  683. expect(body.file.contentHash).toMatch(/^[0-9a-f]{64}$/);
  684. expect(body.file.generated).toBe(false);
  685. expect(body.file.test).toBe(false);
  686. expect(body.file.id).toMatch(/^file:/);
  687. expect(body.drift).toBe(false);
  688. const lines = body.outline.items.map((o: any) => o.line);
  689. expect(lines).toEqual([...lines].sort((a, b) => a - b));
  690. const cacheRow = body.outline.items.find((o: any) => o.name === 'Cache');
  691. expect(cacheRow.depth).toBe(0);
  692. expect(cacheRow.parentId).toBeNull();
  693. const readRow = body.outline.items.find((o: any) => o.name === 'read');
  694. expect(readRow.depth).toBe(1);
  695. expect(readRow.parentId).toBe(cacheRow.id);
  696. expect(readRow.fanIn).toBeGreaterThanOrEqual(1);
  697. expect(typeof readRow.fanOut).toBe('number');
  698. // The file node is the subject, not a row; imports have their own rail.
  699. expect(body.outline.items.some((o: any) => o.kind === 'file')).toBe(false);
  700. expect(body.outline.items.some((o: any) => o.kind === 'import')).toBe(false);
  701. });
  702. it('maps imports and imported-by to files', async () => {
  703. const body = await getJson('/api/file/src/cache.ts');
  704. const importedByFiles = body.importedBy.items.map((r: any) => r.file);
  705. expect(importedByFiles).toContain('src/service.ts');
  706. const importFiles = body.imports.items.map((r: any) => r.file);
  707. expect(importFiles).toContain('src/types.ts');
  708. // Never itself: same-file `imports` edges (the import declarations) are dropped.
  709. expect(importFiles).not.toContain('src/cache.ts');
  710. expect(importedByFiles).not.toContain('src/cache.ts');
  711. const typesRow = body.imports.items.find((r: any) => r.file === 'src/types.ts');
  712. expect(typesRow.symbolCount).toBeGreaterThanOrEqual(1);
  713. expect(typesRow.symbols[0].name).toBeTruthy();
  714. expect(typesRow.symbols[0].id).toBeTruthy();
  715. expect(typesRow.test).toBe(false);
  716. });
  717. it('names the imports that never resolved rather than dropping them', async () => {
  718. const body = await getJson('/api/file/src/service.ts');
  719. const names = body.unresolvedImports.map((u: any) => u.name);
  720. expect(names).toContain('some-external-package');
  721. });
  722. it('reports the wider cross-file relationship too', async () => {
  723. const body = await getJson('/api/file/src/cache.ts');
  724. expect(body.dependents).toContain('src/service.ts');
  725. expect(body.dependencies).toContain('src/types.ts');
  726. });
  727. it('404s a file that is not in the index and refuses one outside the project', async () => {
  728. const missing = await getStatusAndJson('/api/file/src/nope.ts');
  729. expect(missing.status).toBe(404);
  730. expect(missing.body.code).toBe('not-found');
  731. const outside = await getStatusAndJson(
  732. '/api/file/' + encodeURIComponent('/etc/passwd')
  733. );
  734. expect(outside.status).toBe(403);
  735. expect(outside.body.code).toBe('refused');
  736. });
  737. });
  738. describe('GET /api/routes', () => {
  739. it('says plainly that this project is not a routed app', async () => {
  740. const body = await getJson('/api/routes');
  741. expect(body.routed).toBe(false);
  742. expect(body.entries).toEqual([]);
  743. expect(body.routeCount).toBe(0);
  744. expect(body.shown).toBe(0);
  745. expect(body.truncated).toBe(false);
  746. });
  747. it('refuses a limit the manifest cannot answer truthfully', async () => {
  748. // Below three, the engine's manifest reports every routed project as
  749. // unrouted — a wrong answer, so the parameter is refused instead.
  750. for (const limit of ['0', '2', '-1', 'abc']) {
  751. const { status, body } = await getStatusAndJson(`/api/routes?limit=${limit}`);
  752. expect(status, `limit=${limit}`).toBe(400);
  753. expect(body.code).toBe('bad-request');
  754. }
  755. });
  756. describe('a project that IS routed', () => {
  757. let routedApi: GraphApi;
  758. let routedServer: UiServerHandle;
  759. beforeAll(async () => {
  760. const routedRoot = path.join(tempDir, 'routed');
  761. fs.mkdirSync(path.join(routedRoot, 'src'), { recursive: true });
  762. fs.writeFileSync(
  763. path.join(routedRoot, 'src', 'routes.ts'),
  764. `import express from 'express';
  765. const app = express();
  766. export function listUsers(req: any, res: any): void { res.json([]); }
  767. export function getUser(req: any, res: any): void { res.json({}); }
  768. export function createUser(req: any, res: any): void { res.json({}); }
  769. export function deleteUser(req: any, res: any): void { res.json({}); }
  770. app.get('/users', listUsers);
  771. app.get('/users/:id', getUser);
  772. app.post('/users', createUser);
  773. app.delete('/users/:id', deleteUser);
  774. export default app;
  775. `
  776. );
  777. const routedCg = CodeGraph.initSync(routedRoot, {
  778. config: { include: ['src/**/*.ts'], exclude: [] },
  779. });
  780. await routedCg.indexAll();
  781. routedCg.resolveReferences();
  782. routedCg.close();
  783. routedApi = createGraphApi({ projectRoot: routedRoot });
  784. routedServer = await startUiServer({
  785. projectRoot: routedRoot,
  786. viewerDir,
  787. port: 0,
  788. api: routedApi.handler,
  789. });
  790. }, 120_000);
  791. afterAll(async () => {
  792. routedApi?.close();
  793. await routedServer?.close();
  794. });
  795. it('maps each URL to its handler, with a node id to navigate to', async () => {
  796. const res = await requestOn(routedServer.port, '/api/routes');
  797. const body = JSON.parse(res.body);
  798. expect(body.routed).toBe(true);
  799. expect(body.routeCount).toBe(4);
  800. expect(body.shown).toBe(4);
  801. expect(body.truncated).toBe(false);
  802. expect(body.topHandlerFile).toBe('src/routes.ts');
  803. expect(body.topHandlerFileCount).toBe(4);
  804. const urls = body.entries.map((e: any) => e.url);
  805. expect(urls).toEqual(
  806. expect.arrayContaining(['GET /users', 'GET /users/:id', 'POST /users', 'DELETE /users/:id'])
  807. );
  808. const listUsers = body.entries.find((e: any) => e.url === 'GET /users');
  809. expect(listUsers.handler).toBe('listUsers');
  810. expect(listUsers.handlerKind).toBe('function');
  811. expect(listUsers.file).toBe('src/routes.ts');
  812. expect(listUsers.line).toBeGreaterThan(0);
  813. // The manifest carries no ids of its own; resolving them is what makes a
  814. // route row clickable, so it has to actually resolve.
  815. expect(listUsers.handlerId).toBeTruthy();
  816. const handler = JSON.parse(
  817. (await requestOn(routedServer.port, `/api/node/${listUsers.handlerId}`)).body
  818. );
  819. expect(handler.node.name).toBe('listUsers');
  820. });
  821. it('offers its routes as entry points, ahead of anything derived', async () => {
  822. const res = await requestOn(routedServer.port, '/api/entrypoints');
  823. const body = JSON.parse(res.body);
  824. expect(body.routes.routed).toBe(true);
  825. expect(body.routes.routeCount).toBe(4);
  826. const urls = body.routes.items.map((e: any) => e.url);
  827. expect(urls).toEqual(
  828. expect.arrayContaining(['GET /users', 'GET /users/:id', 'POST /users', 'DELETE /users/:id'])
  829. );
  830. // A route row has to be navigable, or it is a label.
  831. expect(body.routes.items.every((e: any) => e.handlerId)).toBe(true);
  832. });
  833. it('honours the limit and says when it cut the list', async () => {
  834. const res = await requestOn(routedServer.port, '/api/routes?limit=3');
  835. const body = JSON.parse(res.body);
  836. expect(body.routed).toBe(true);
  837. expect(body.entries).toHaveLength(3);
  838. expect(body.shown).toBe(3);
  839. expect(body.truncated).toBe(true);
  840. // The headline count is the whole graph's, not the page's.
  841. expect(body.routeCount).toBe(4);
  842. });
  843. });
  844. });
  845. /**
  846. * The acceptance bar from the issue, against the engine's OWN index rather than
  847. * a fixture: `LRUCache.get` in `src/resolution/lru-cache.ts`, 500+ callers.
  848. *
  849. * `.codegraph/` is gitignored, so this only runs on a machine that has indexed
  850. * this repository. The fixture test above covers the same properties in CI; this
  851. * one is the check against the real, messy graph the number came from.
  852. */
  853. describe.runIf(CodeGraph.isInitialized(path.resolve(__dirname, '..')))(
  854. "the engine's own busiest symbol",
  855. () => {
  856. const repoRoot = path.resolve(__dirname, '..');
  857. let repoApi: GraphApi;
  858. let repoServer: UiServerHandle;
  859. beforeAll(async () => {
  860. repoApi = createGraphApi({ projectRoot: repoRoot });
  861. repoServer = await startUiServer({
  862. projectRoot: repoRoot,
  863. viewerDir,
  864. port: 0,
  865. api: repoApi.handler,
  866. });
  867. });
  868. afterAll(async () => {
  869. repoApi?.close();
  870. await repoServer?.close();
  871. });
  872. const repoGet = (requestPath: string): Promise<Response> =>
  873. requestOn(repoServer.port, requestPath);
  874. it('answers in under 100 ms with grouped, capped lists and correct counts', async () => {
  875. const search = JSON.parse(
  876. (await repoGet('/api/search?q=' + encodeURIComponent('LRUCache.get'))).body
  877. );
  878. const hit = search.results.items.find(
  879. (r: any) => r.name === 'get' && r.file.endsWith('src/resolution/lru-cache.ts')
  880. );
  881. expect(hit, 'LRUCache.get should be in the engine\'s own index').toBeTruthy();
  882. await repoGet(`/api/node/${hit.id}`); // warm
  883. const started = performance.now();
  884. const res = await repoGet(`/api/node/${hit.id}`);
  885. const elapsed = performance.now() - started;
  886. expect(res.status).toBe(200);
  887. const body = JSON.parse(res.body);
  888. expect(body.counts.fanIn).toBeGreaterThanOrEqual(500);
  889. expect(body.counts.hub).toBe(true);
  890. // Grouped by calling symbol, so the row count is the distinct-caller
  891. // count, never the edge count.
  892. expect(body.incoming.items).toHaveLength(body.incoming.shown);
  893. expect(body.incoming.shown).toBeLessThanOrEqual(300);
  894. expect(body.incoming.shown).toBe(Math.min(300, body.incoming.total));
  895. expect(body.incoming.truncated).toBe(body.incoming.total > 300);
  896. expect(new Set(body.incoming.items.map((r: any) => r.node.id)).size).toBe(
  897. body.incoming.shown
  898. );
  899. const edgesInRows = body.incoming.items.reduce(
  900. (sum: number, r: any) => sum + r.edgeCount,
  901. 0
  902. );
  903. expect(edgesInRows).toBeLessThanOrEqual(body.counts.fanIn);
  904. expect(body.blast.direct).toBe(body.counts.callers);
  905. expect(body.tests.reached).toBe(true);
  906. expect(elapsed).toBeLessThan(100);
  907. });
  908. }
  909. );
  910. describe('GET /api/entrypoints', () => {
  911. it('finds the file that runs something, and reports what it reaches', async () => {
  912. const body = await getJson('/api/entrypoints');
  913. const files = body.files.items.map((f: any) => f.file);
  914. expect(files).toContain('src/main.ts');
  915. const main = body.files.items.find((f: any) => f.file === 'src/main.ts');
  916. expect(main.kind).toBe('file');
  917. expect(main.id).toMatch(/^file:/);
  918. // `new Service(...)`, `handleRequest(...)` and `service.load(...)` all sit
  919. // at module level.
  920. expect(main.calls).toBeGreaterThanOrEqual(2);
  921. // It imports from service.ts and handler.ts, so it wires files together.
  922. expect(main.reaches).toBeGreaterThanOrEqual(2);
  923. expect(typeof main.dependents).toBe('number');
  924. });
  925. it('leaves test files out — "where do I start" never means a test', async () => {
  926. const body = await getJson('/api/entrypoints');
  927. for (const file of body.files.items) expect(file.test).toBe(false);
  928. // The fixture's test file calls its own helper at module level, so it IS a
  929. // candidate by the raw graph signal and is excluded deliberately.
  930. expect(body.files.items.map((f: any) => f.file)).not.toContain(
  931. '__tests__/service.test.ts'
  932. );
  933. for (const hub of body.hubs.items) expect(hub.test).toBe(false);
  934. });
  935. it('ranks the most depended-on symbols as hubs, with their dependent counts', async () => {
  936. const body = await getJson('/api/entrypoints');
  937. const hot = body.hubs.items.find((h: any) => h.name === 'hot');
  938. expect(hot, 'the 500-caller function should top the hubs').toBeTruthy();
  939. expect(hot.dependents).toBe(500);
  940. expect(body.hubs.items[0].name).toBe('hot');
  941. const counts = body.hubs.items.map((h: any) => h.dependents);
  942. expect(counts).toEqual([...counts].sort((a: number, b: number) => b - a));
  943. // A file or a bare import is structure, not somewhere to start reading.
  944. for (const hub of body.hubs.items) {
  945. expect(['file', 'import', 'export', 'parameter']).not.toContain(hub.kind);
  946. }
  947. });
  948. it('says a project without routes is not routed rather than failing', async () => {
  949. const body = await getJson('/api/entrypoints');
  950. expect(body.routes.routed).toBe(false);
  951. expect(body.routes.items).toEqual([]);
  952. expect(body.routes.routeCount).toBe(0);
  953. });
  954. it('honours limit, and keeps every list within it', async () => {
  955. const body = await getJson('/api/entrypoints?limit=1');
  956. expect(body.files.items.length).toBeLessThanOrEqual(1);
  957. expect(body.hubs.items.length).toBe(1);
  958. expect(body.hubs.total).toBeGreaterThanOrEqual(body.hubs.items.length);
  959. const bad = await getStatusAndJson('/api/entrypoints?limit=0');
  960. expect(bad.status).toBe(400);
  961. expect(bad.body.code).toBe('bad-request');
  962. });
  963. });
  964. describe('GET /api/nodes', () => {
  965. it('answers a batch of ids in the order asked, and says which are missing', async () => {
  966. const cacheId = await idOf('Cache', 'class');
  967. const loadId = await idOf('load', 'method');
  968. const body = await getJson(
  969. `/api/nodes?id=${encodeURIComponent(loadId)}&id=${encodeURIComponent(cacheId)}&id=method%3Anot-a-real-id`
  970. );
  971. expect(body.items.map((n: any) => n.id)).toEqual([loadId, cacheId]);
  972. expect(body.items[0].name).toBe('load');
  973. expect(body.items[1].name).toBe('Cache');
  974. expect(body.missing).toEqual(['method:not-a-real-id']);
  975. // The REF shape, not the Symbol view payload: a trail redraws six names,
  976. // not six rail sets.
  977. expect(body.items[0].incoming).toBeUndefined();
  978. expect(body.items[0].file).toBe('src/service.ts');
  979. });
  980. it('de-duplicates ids rather than answering twice', async () => {
  981. const cacheId = await idOf('Cache', 'class');
  982. const encoded = encodeURIComponent(cacheId);
  983. const body = await getJson(`/api/nodes?id=${encoded}&id=${encoded}`);
  984. expect(body.items).toHaveLength(1);
  985. });
  986. it('refuses an empty or oversized request with guidance', async () => {
  987. const none = await getStatusAndJson('/api/nodes');
  988. expect(none.status).toBe(400);
  989. expect(none.body.hint).toContain('id=');
  990. const ids = Array.from({ length: 61 }, (_, i) => `id=method%3A${i}`).join('&');
  991. const many = await getStatusAndJson(`/api/nodes?${ids}`);
  992. expect(many.status).toBe(400);
  993. expect(many.body.error).toContain('Too many ids');
  994. });
  995. });
  996. describe('an index that is not there', () => {
  997. it('answers with the same guidance the CLI prints, not a stack trace', async () => {
  998. const emptyRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-ui-noindex-'));
  999. const detached = createGraphApi({ projectRoot: emptyRoot });
  1000. const detachedServer = await startUiServer({
  1001. projectRoot: emptyRoot,
  1002. viewerDir,
  1003. port: 0,
  1004. api: detached.handler,
  1005. });
  1006. try {
  1007. const res = await requestOn(detachedServer.port, '/api/stats');
  1008. expect(res.status).toBe(503);
  1009. const body = JSON.parse(res.body);
  1010. expect(body.code).toBe('no-index');
  1011. expect(body.error).toContain('No CodeGraph index found');
  1012. expect(body.hint).toContain('codegraph init');
  1013. expect(body.error).not.toContain(' at ');
  1014. } finally {
  1015. detached.close();
  1016. await detachedServer.close();
  1017. fs.rmSync(emptyRoot, { recursive: true, force: true });
  1018. }
  1019. });
  1020. });