ui-server-api.test.ts 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. // 500 callers into one function: the N+1 and capping behaviour only shows up
  156. // at this scale, and the fixture keeps CI honest without needing the engine's
  157. // own index to be present.
  158. const callers = Array.from(
  159. { length: 500 },
  160. (_, i) => `export function caller${i}(): number {\n return hot(${i});\n}`
  161. ).join('\n\n');
  162. fs.writeFileSync(
  163. path.join(srcDir, 'hot.ts'),
  164. `export function hot(n: number): number {
  165. return n * 2;
  166. }
  167. ${callers}
  168. `
  169. );
  170. fs.writeFileSync(
  171. path.join(testsDir, 'service.test.ts'),
  172. `import { Service } from '../src/service';
  173. export function testLoadsThroughCache(): void {
  174. const service = new Service({ ttlMs: 1, label: 'x' });
  175. service.load('k');
  176. }
  177. `
  178. );
  179. const cg = CodeGraph.initSync(projectRoot, {
  180. config: { include: ['src/**/*.ts', '__tests__/**/*.ts'], exclude: [] },
  181. });
  182. await cg.indexAll();
  183. cg.resolveReferences();
  184. // Hand the index over: the API opens its own read-only connection, which is
  185. // also what happens in production (the CLI never shares an instance).
  186. cg.close();
  187. viewerDir = path.join(tempDir, 'viewer');
  188. fs.mkdirSync(viewerDir, { recursive: true });
  189. fs.writeFileSync(path.join(viewerDir, 'index.html'), '<!doctype html><div id="app"></div>');
  190. api = createGraphApi({ projectRoot });
  191. server = await startUiServer({ projectRoot, viewerDir, port: 0, api: api.handler });
  192. }, 120_000);
  193. afterAll(async () => {
  194. api?.close();
  195. await server?.close();
  196. if (tempDir && fs.existsSync(tempDir)) fs.rmSync(tempDir, { recursive: true, force: true });
  197. });
  198. describe('GET /api', () => {
  199. it('lists the endpoints it answers', async () => {
  200. const body = await getJson('/api');
  201. expect(body.readOnly).toBe(true);
  202. const paths = body.endpoints.map((e: any) => e.path);
  203. expect(paths).toEqual(
  204. expect.arrayContaining([
  205. '/api/stats',
  206. '/api/search',
  207. '/api/node/<id>',
  208. '/api/source',
  209. '/api/file/<path>',
  210. '/api/routes',
  211. ])
  212. );
  213. });
  214. it('404s an unknown endpoint as JSON, never as the app shell', async () => {
  215. const { status, body } = await getStatusAndJson('/api/nope');
  216. expect(status).toBe(404);
  217. expect(body.code).toBe('not-found');
  218. });
  219. it('answers HEAD with the headers and no body', async () => {
  220. const res = await request('/api/stats', 'HEAD');
  221. expect(res.status).toBe(200);
  222. expect(res.headers['content-type']).toBe('application/json; charset=utf-8');
  223. expect(Number(res.headers['content-length'])).toBeGreaterThan(0);
  224. expect(res.body).toBe('');
  225. });
  226. });
  227. describe('GET /api/stats', () => {
  228. it('reports the project, the index state and the graph counts', async () => {
  229. const body = await getJson('/api/stats');
  230. expect(body.project.root).toBe(projectRoot);
  231. expect(body.project.name).toBe('project');
  232. expect(body.index.state).toBe('complete');
  233. expect(body.index.stale).toBe(false);
  234. expect(typeof body.index.lastIndexedAt).toBe('number');
  235. expect(body.index.backend).toBe('node-sqlite');
  236. expect(typeof body.index.extractionVersion).toBe('number');
  237. expect(body.graph.nodes).toBeGreaterThan(0);
  238. expect(body.graph.edges).toBeGreaterThan(0);
  239. expect(body.graph.files).toBeGreaterThanOrEqual(6);
  240. expect(body.graph.nodesByKind.class).toBeGreaterThanOrEqual(2);
  241. expect(body.graph.filesByLanguage.typescript).toBeGreaterThanOrEqual(6);
  242. // The thresholds travel with the data so the viewer's copy cannot drift.
  243. expect(body.thresholds).toEqual({ hub: 40, uncertainBelow: 0.6 });
  244. });
  245. });
  246. describe('GET /api/search', () => {
  247. it('ranks exact over prefix over substring, and groups by kind', async () => {
  248. const body = await getJson('/api/search?q=Cache');
  249. const first = body.results.items[0];
  250. expect(first.name).toBe('Cache');
  251. expect(first.kind).toBe('class');
  252. expect(first.matchKind).toBe('exact');
  253. const ranks = body.results.items.map((r: any) => r.matchKind);
  254. const order = ['exact', 'prefix', 'substring', 'qualified', 'file', 'related'];
  255. const asNumbers = ranks.map((r: string) => order.indexOf(r));
  256. expect(asNumbers).toEqual([...asNumbers].sort((a, b) => a - b));
  257. // Flattening the groups reproduces the flat ranking, so the palette can use
  258. // either without them disagreeing.
  259. const flattened = body.groups.flatMap((g: any) => g.items.map((i: any) => i.id));
  260. expect(new Set(flattened)).toEqual(new Set(body.results.items.map((r: any) => r.id)));
  261. for (const group of body.groups) expect(group.count).toBe(group.items.length);
  262. });
  263. it('returns a signature and a file:line for every result', async () => {
  264. const body = await getJson('/api/search?q=handleRequest');
  265. const hit = body.results.items.find((r: any) => r.name === 'handleRequest');
  266. expect(hit.file).toBe('src/handler.ts');
  267. expect(hit.line).toBeGreaterThan(0);
  268. expect(hit.endLine).toBeGreaterThanOrEqual(hit.line);
  269. expect(hit.signature).toContain('service');
  270. expect(hit.qualifiedName).toBeTruthy();
  271. expect(hit.language).toBe('typescript');
  272. });
  273. it('finds a mid-name match FTS tokens cannot', async () => {
  274. const body = await getJson('/api/search?q=quest');
  275. const names = body.results.items.map((r: any) => r.name);
  276. expect(names).toContain('handleRequest');
  277. const hit = body.results.items.find((r: any) => r.name === 'handleRequest');
  278. expect(hit.matchKind).toBe('substring');
  279. });
  280. it('honours the kind: filter grammar', async () => {
  281. const body = await getJson('/api/search?q=' + encodeURIComponent('kind:class Cache'));
  282. expect(body.filters.kinds).toEqual(['class']);
  283. expect(body.results.items.every((r: any) => r.kind === 'class')).toBe(true);
  284. });
  285. it('marks test files so the palette can rank them down', async () => {
  286. const body = await getJson('/api/search?q=testLoadsThroughCache');
  287. const hit = body.results.items.find((r: any) => r.name === 'testLoadsThroughCache');
  288. expect(hit.test).toBe(true);
  289. });
  290. it('answers an empty search box with nothing, and a missing q with 400', async () => {
  291. const empty = await getStatusAndJson('/api/search?q=');
  292. expect(empty.status).toBe(200);
  293. expect(empty.body.results.total).toBe(0);
  294. expect(empty.body.groups).toEqual([]);
  295. const missing = await getStatusAndJson('/api/search');
  296. expect(missing.status).toBe(400);
  297. expect(missing.body.code).toBe('bad-request');
  298. });
  299. it('returns an empty result set for a name nothing has', async () => {
  300. const body = await getJson('/api/search?q=zzznotasymbolanywhere');
  301. expect(body.results.total).toBe(0);
  302. });
  303. });
  304. describe('GET /api/node/<id>', () => {
  305. it('returns the symbol, its ancestors and its members in source order', async () => {
  306. const body = await getJson(`/api/node/${await idOf('Cache', 'class')}`);
  307. expect(body.node.name).toBe('Cache');
  308. expect(body.node.kind).toBe('class');
  309. expect(body.node.file).toBe('src/cache.ts');
  310. expect(body.node.lines).toBe(body.node.endLine - body.node.line + 1);
  311. expect(body.node.exported).toBe(true);
  312. // Outermost first: the file, then anything between it and the symbol.
  313. expect(body.ancestors[0].kind).toBe('file');
  314. expect(body.ancestors[0].file).toBe('src/cache.ts');
  315. const members = body.members.items.map((m: any) => m.name);
  316. expect(members).toEqual(expect.arrayContaining(['read', 'write', 'store', 'config']));
  317. const lines = body.members.items.map((m: any) => m.line);
  318. expect(lines).toEqual([...lines].sort((a, b) => a - b));
  319. for (const member of body.members.items) {
  320. expect(member.parentId).toBe(body.node.id);
  321. expect(member.depth).toBe(1);
  322. }
  323. expect(body.members.total).toBe(body.members.shown);
  324. });
  325. it('nests a file outline one level deeper, so a class shows its methods', async () => {
  326. const body = await getJson(`/api/node/${await idOf('cache.ts', 'file')}`);
  327. const byDepth = new Map<number, string[]>();
  328. for (const member of body.members.items) {
  329. byDepth.set(member.depth, [...(byDepth.get(member.depth) ?? []), member.name]);
  330. }
  331. expect(byDepth.get(1)).toContain('Cache');
  332. expect(byDepth.get(2)).toEqual(expect.arrayContaining(['read', 'write']));
  333. });
  334. it('groups incoming edges by the calling symbol, with their call sites', async () => {
  335. const readId = await idOf('read', 'method');
  336. const body = await getJson(`/api/node/${readId}`);
  337. const fromLoad = body.incoming.items.find((r: any) => r.node.name === 'load');
  338. expect(fromLoad, 'Service.load should call Cache.read').toBeTruthy();
  339. expect(fromLoad.node.file).toBe('src/service.ts');
  340. expect(fromLoad.edgeKinds).toContain('calls');
  341. expect(fromLoad.edgeCount).toBeGreaterThanOrEqual(1);
  342. expect(fromLoad.lines.length).toBeGreaterThanOrEqual(1);
  343. expect(fromLoad.lines).toEqual([...fromLoad.lines].sort((a: number, b: number) => a - b));
  344. expect(typeof fromLoad.fanIn).toBe('number');
  345. expect(fromLoad.hub).toBe(false);
  346. });
  347. it('carries every edge attribute the viewer draws with', async () => {
  348. const body = await getJson(`/api/node/${await idOf('read', 'method')}`);
  349. const relation = body.incoming.items.find((r: any) => r.node.name === 'load');
  350. const edge = relation.edges[0];
  351. expect(edge.kind).toBe('calls');
  352. expect(typeof edge.line).toBe('number');
  353. expect(typeof edge.col).toBe('number');
  354. expect(typeof edge.confidence).toBe('number');
  355. expect(typeof edge.resolvedBy).toBe('string');
  356. // Confidence decides the uncertain fold; the group agrees with its edges.
  357. expect(relation.confidence).toBe(
  358. Math.max(...relation.edges.map((e: any) => e.confidence ?? -1))
  359. );
  360. expect(relation.uncertain).toBe(relation.confidence < 0.6);
  361. expect(relation.synthesized).toBe(false);
  362. });
  363. it('groups outgoing edges by the called symbol, ordered by call site', async () => {
  364. const body = await getJson(`/api/node/${await idOf('load', 'method')}`);
  365. const names = body.outgoing.items.map((r: any) => r.node.name);
  366. expect(names).toEqual(expect.arrayContaining(['read', 'write']));
  367. const firstLines = body.outgoing.items
  368. .map((r: any) => r.lines[0])
  369. .filter((l: number | undefined) => l !== undefined);
  370. expect(firstLines).toEqual([...firstLines].sort((a, b) => a - b));
  371. });
  372. it('splits type references out of the callee rail', async () => {
  373. // Type edges attach to the MEMBER that names the type, not to its class:
  374. // `Service`'s constructor is where `Config` and `new Cache(...)` both live,
  375. // which makes it the one place both halves of the split are visible.
  376. const service = await getJson(`/api/node/${await idOf('Service', 'class')}`);
  377. const ctor = service.members.items.find((m: any) => m.name === 'constructor');
  378. const body = await getJson(`/api/node/${ctor.id}`);
  379. const typeNames = body.typesUsed.map((t: any) => t.node.name);
  380. expect(typeNames).toContain('Config');
  381. expect(body.typesUsed.every((t: any) => t.edgeKinds.includes('references'))).toBe(true);
  382. // A type reference is not also a callee row...
  383. expect(body.outgoing.items.map((r: any) => r.node.name)).not.toContain('Config');
  384. // ...but a class reached by any other edge kind still is: `new Cache(...)`
  385. // is an `instantiates` edge, and moving it would hide a real dependency.
  386. const instantiated = body.outgoing.items.find((r: any) => r.node.name === 'Cache');
  387. expect(instantiated).toBeTruthy();
  388. expect(instantiated.edgeKinds).toContain('instantiates');
  389. });
  390. it('summarizes which tests reach the symbol', async () => {
  391. const reached = await getJson(`/api/node/${await idOf('load', 'method')}`);
  392. expect(reached.tests.reached).toBe(true);
  393. expect(reached.tests.hops).toBe(1);
  394. expect(reached.tests.files).toContain('__tests__/service.test.ts');
  395. expect(reached.tests.fileCount).toBeGreaterThanOrEqual(1);
  396. expect(reached.tests.files.length).toBeLessThanOrEqual(6);
  397. expect(reached.tests.exhaustive).toBe(true);
  398. const unreached = await getJson(`/api/node/${await idOf('hot', 'function')}`);
  399. expect(unreached.tests.reached).toBe(false);
  400. expect(unreached.tests.hops).toBeNull();
  401. expect(unreached.tests.files).toEqual([]);
  402. });
  403. it('counts the calls that leave the index instead of hiding them', async () => {
  404. const body = await getJson(`/api/node/${await idOf('load', 'method')}`);
  405. expect(body.outsideIndex.total).toBeGreaterThan(0);
  406. const names = body.outsideIndex.samples.map((s: any) => s.name);
  407. expect(names).toContain('serialize');
  408. for (const sample of body.outsideIndex.samples) {
  409. expect(typeof sample.line).toBe('number');
  410. expect(typeof sample.kind).toBe('string');
  411. }
  412. });
  413. it('summarizes the blast radius at three hops', async () => {
  414. const body = await getJson(`/api/node/${await idOf('read', 'method')}`);
  415. expect(body.blast.hops).toBe(3);
  416. expect(body.blast.direct).toBe(body.counts.callers);
  417. // load → handleRequest / the test both sit inside three hops of Cache.read.
  418. expect(body.blast.withinHops).toBeGreaterThan(body.blast.direct);
  419. expect(body.blast.files).toBeGreaterThanOrEqual(2);
  420. expect(body.blast.testFiles).toBeGreaterThanOrEqual(1);
  421. expect(body.blast.routes).toBe(0);
  422. expect(body.blast.topFiles[0].symbols).toBeGreaterThanOrEqual(1);
  423. });
  424. it('keeps every count equal to the list it labels', async () => {
  425. const body = await getJson(`/api/node/${await idOf('read', 'method')}`);
  426. expect(body.counts.callers).toBe(body.incoming.total);
  427. expect(body.counts.callees).toBe(body.outgoing.total);
  428. expect(body.counts.typesUsed).toBe(body.typesUsed.length);
  429. expect(body.counts.members).toBe(body.members.total);
  430. expect(body.blast.direct).toBe(body.counts.callers);
  431. });
  432. it('reports fan-in, fan-out and the hub flag', async () => {
  433. const quiet = await getJson(`/api/node/${await idOf('write', 'method')}`);
  434. expect(quiet.counts.hub).toBe(false);
  435. expect(quiet.counts.callers).toBeLessThan(40);
  436. expect(quiet.counts.fanIn).toBeGreaterThanOrEqual(quiet.counts.callers);
  437. const hot = await getJson(`/api/node/${await idOf('hot', 'function')}`);
  438. expect(hot.counts.hub).toBe(true);
  439. expect(hot.counts.callers).toBeGreaterThanOrEqual(500);
  440. });
  441. it('flags nothing as drifted while the fixture is untouched', async () => {
  442. const body = await getJson(`/api/node/${await idOf('read', 'method')}`);
  443. expect(body.drift).toBe(false);
  444. });
  445. it('404s an id that names nothing, and 400s an empty one', async () => {
  446. const missing = await getStatusAndJson('/api/node/method:notarealid');
  447. expect(missing.status).toBe(404);
  448. expect(missing.body.code).toBe('not-found');
  449. expect(missing.body.hint).toBeTruthy();
  450. const empty = await getStatusAndJson('/api/node/');
  451. expect(empty.status).toBe(400);
  452. });
  453. });
  454. describe('GET /api/node/<id> — the busiest symbol', () => {
  455. it('caps the caller list, keeps the true total, and stays fast', async () => {
  456. const hotId = await idOf('hot', 'function');
  457. await request(`/api/node/${hotId}`); // warm the connection and the caches
  458. const started = performance.now();
  459. const res = await request(`/api/node/${hotId}`);
  460. const elapsed = performance.now() - started;
  461. expect(res.status).toBe(200);
  462. const body = JSON.parse(res.body);
  463. expect(body.incoming.total).toBeGreaterThanOrEqual(500);
  464. expect(body.incoming.shown).toBe(300);
  465. expect(body.incoming.truncated).toBe(true);
  466. expect(body.incoming.items).toHaveLength(300);
  467. // Grouped: one row per calling symbol, each carrying its own call sites.
  468. expect(new Set(body.incoming.items.map((r: any) => r.node.id)).size).toBe(300);
  469. expect(body.counts.callers).toBeGreaterThanOrEqual(500);
  470. expect(body.blast.direct).toBe(body.counts.callers);
  471. // 500 callers resolved one query at a time would be nowhere near this.
  472. expect(elapsed).toBeLessThan(100);
  473. });
  474. });
  475. describe('GET /api/source', () => {
  476. it('returns the requested slice with the index line numbering', async () => {
  477. const body = await getJson('/api/source?file=src/cache.ts&from=1&to=3');
  478. expect(body.drift).toBe(false);
  479. expect(body.file).toBe('src/cache.ts');
  480. expect(body.language).toBe('typescript');
  481. expect(body.from).toBe(1);
  482. expect(body.to).toBe(3);
  483. expect(body.lines).toHaveLength(3);
  484. expect(body.lines[0]).toContain("import { Config, CacheKey } from './types'");
  485. expect(body.totalLines).toBeGreaterThan(3);
  486. expect(body.truncated).toBe(false);
  487. });
  488. it('serves the whole file when no range is given', async () => {
  489. const body = await getJson('/api/source?file=src/handler.ts');
  490. expect(body.from).toBe(1);
  491. expect(body.to).toBe(body.totalLines);
  492. expect(body.lines).toHaveLength(body.totalLines);
  493. });
  494. it('slices exactly the lines a symbol claims', async () => {
  495. const node = await getJson(`/api/node/${await idOf('handleRequest', 'function')}`);
  496. const body = await getJson(
  497. `/api/source?file=${node.node.file}&from=${node.node.line}&to=${node.node.endLine}`
  498. );
  499. expect(body.lines[0]).toContain('handleRequest');
  500. expect(body.lines).toHaveLength(node.node.lines);
  501. });
  502. it('refuses to slice a file that changed on disk after the last sync', async () => {
  503. const target = path.join(projectRoot, 'src', 'handler.ts');
  504. const original = fs.readFileSync(target);
  505. try {
  506. fs.writeFileSync(target, Buffer.concat([Buffer.from('// a new first line\n'), original]));
  507. const body = await getJson('/api/source?file=src/handler.ts&from=1&to=3');
  508. expect(body.drift).toBe(true);
  509. // The whole point: no slice, rather than a slice of the wrong lines.
  510. expect(body.lines).toBeUndefined();
  511. expect(body.reason).toContain('changed on disk after the last index sync');
  512. // And every screen that renders indexed line ranges is told.
  513. const node = await getJson(`/api/node/${await idOf('handleRequest', 'function')}`);
  514. expect(node.drift).toBe(true);
  515. const file = await getJson('/api/file/src/handler.ts');
  516. expect(file.drift).toBe(true);
  517. } finally {
  518. fs.writeFileSync(target, original);
  519. }
  520. });
  521. it('does not call an identical rewrite drift', async () => {
  522. const target = path.join(projectRoot, 'src', 'handler.ts');
  523. const original = fs.readFileSync(target);
  524. // Same bytes, new mtime — what a checkout or a formatter no-op looks like.
  525. fs.writeFileSync(target, original);
  526. const body = await getJson('/api/source?file=src/handler.ts&from=1&to=2');
  527. expect(body.drift).toBe(false);
  528. expect(body.lines).toHaveLength(2);
  529. });
  530. it('refuses a path that escapes the project', async () => {
  531. const traversal = await getStatusAndJson(
  532. '/api/source?file=' + encodeURIComponent('../../../etc/passwd')
  533. );
  534. expect(traversal.status).toBe(403);
  535. expect(traversal.body.code).toBe('refused');
  536. const absolute = await getStatusAndJson(
  537. '/api/source?file=' + encodeURIComponent('/etc/passwd')
  538. );
  539. expect(absolute.status).toBe(403);
  540. expect(absolute.body.code).toBe('refused');
  541. expect(absolute.body.error).toContain('absolute');
  542. });
  543. it('refuses a NUL byte in the path', async () => {
  544. const { status, body } = await getStatusAndJson(
  545. '/api/source?file=' + encodeURIComponent('src/cache.ts\u0000.png')
  546. );
  547. expect(status).toBe(403);
  548. expect(body.code).toBe('refused');
  549. });
  550. it('404s a file that exists but is not indexed', async () => {
  551. fs.writeFileSync(path.join(projectRoot, 'notes.md'), '# not indexed\n');
  552. const { status, body } = await getStatusAndJson('/api/source?file=notes.md');
  553. expect(status).toBe(404);
  554. expect(body.code).toBe('not-found');
  555. expect(body.hint).toContain('index');
  556. });
  557. it('rejects a range that names nothing', async () => {
  558. const past = await getStatusAndJson('/api/source?file=src/handler.ts&from=99999');
  559. expect(past.status).toBe(400);
  560. expect(past.body.error).toContain('past the end');
  561. const backwards = await getStatusAndJson('/api/source?file=src/handler.ts&from=10&to=4');
  562. expect(backwards.status).toBe(400);
  563. const nonNumeric = await getStatusAndJson('/api/source?file=src/handler.ts&from=abc');
  564. expect(nonNumeric.status).toBe(400);
  565. });
  566. });
  567. describe('GET /api/file/<path>', () => {
  568. it('returns the file record and its outline in source order', async () => {
  569. const body = await getJson('/api/file/src/cache.ts');
  570. expect(body.file.path).toBe('src/cache.ts');
  571. expect(body.file.language).toBe('typescript');
  572. expect(body.file.size).toBeGreaterThan(0);
  573. expect(body.file.contentHash).toMatch(/^[0-9a-f]{64}$/);
  574. expect(body.file.generated).toBe(false);
  575. expect(body.file.test).toBe(false);
  576. expect(body.file.id).toMatch(/^file:/);
  577. expect(body.drift).toBe(false);
  578. const lines = body.outline.items.map((o: any) => o.line);
  579. expect(lines).toEqual([...lines].sort((a, b) => a - b));
  580. const cacheRow = body.outline.items.find((o: any) => o.name === 'Cache');
  581. expect(cacheRow.depth).toBe(0);
  582. expect(cacheRow.parentId).toBeNull();
  583. const readRow = body.outline.items.find((o: any) => o.name === 'read');
  584. expect(readRow.depth).toBe(1);
  585. expect(readRow.parentId).toBe(cacheRow.id);
  586. expect(readRow.fanIn).toBeGreaterThanOrEqual(1);
  587. expect(typeof readRow.fanOut).toBe('number');
  588. // The file node is the subject, not a row; imports have their own rail.
  589. expect(body.outline.items.some((o: any) => o.kind === 'file')).toBe(false);
  590. expect(body.outline.items.some((o: any) => o.kind === 'import')).toBe(false);
  591. });
  592. it('maps imports and imported-by to files', async () => {
  593. const body = await getJson('/api/file/src/cache.ts');
  594. const importedByFiles = body.importedBy.items.map((r: any) => r.file);
  595. expect(importedByFiles).toContain('src/service.ts');
  596. const importFiles = body.imports.items.map((r: any) => r.file);
  597. expect(importFiles).toContain('src/types.ts');
  598. // Never itself: same-file `imports` edges (the import declarations) are dropped.
  599. expect(importFiles).not.toContain('src/cache.ts');
  600. expect(importedByFiles).not.toContain('src/cache.ts');
  601. const typesRow = body.imports.items.find((r: any) => r.file === 'src/types.ts');
  602. expect(typesRow.symbolCount).toBeGreaterThanOrEqual(1);
  603. expect(typesRow.symbols[0].name).toBeTruthy();
  604. expect(typesRow.symbols[0].id).toBeTruthy();
  605. expect(typesRow.test).toBe(false);
  606. });
  607. it('names the imports that never resolved rather than dropping them', async () => {
  608. const body = await getJson('/api/file/src/service.ts');
  609. const names = body.unresolvedImports.map((u: any) => u.name);
  610. expect(names).toContain('some-external-package');
  611. });
  612. it('reports the wider cross-file relationship too', async () => {
  613. const body = await getJson('/api/file/src/cache.ts');
  614. expect(body.dependents).toContain('src/service.ts');
  615. expect(body.dependencies).toContain('src/types.ts');
  616. });
  617. it('404s a file that is not in the index and refuses one outside the project', async () => {
  618. const missing = await getStatusAndJson('/api/file/src/nope.ts');
  619. expect(missing.status).toBe(404);
  620. expect(missing.body.code).toBe('not-found');
  621. const outside = await getStatusAndJson(
  622. '/api/file/' + encodeURIComponent('/etc/passwd')
  623. );
  624. expect(outside.status).toBe(403);
  625. expect(outside.body.code).toBe('refused');
  626. });
  627. });
  628. describe('GET /api/routes', () => {
  629. it('says plainly that this project is not a routed app', async () => {
  630. const body = await getJson('/api/routes');
  631. expect(body.routed).toBe(false);
  632. expect(body.entries).toEqual([]);
  633. expect(body.routeCount).toBe(0);
  634. expect(body.shown).toBe(0);
  635. expect(body.truncated).toBe(false);
  636. });
  637. it('refuses a limit the manifest cannot answer truthfully', async () => {
  638. // Below three, the engine's manifest reports every routed project as
  639. // unrouted — a wrong answer, so the parameter is refused instead.
  640. for (const limit of ['0', '2', '-1', 'abc']) {
  641. const { status, body } = await getStatusAndJson(`/api/routes?limit=${limit}`);
  642. expect(status, `limit=${limit}`).toBe(400);
  643. expect(body.code).toBe('bad-request');
  644. }
  645. });
  646. describe('a project that IS routed', () => {
  647. let routedApi: GraphApi;
  648. let routedServer: UiServerHandle;
  649. beforeAll(async () => {
  650. const routedRoot = path.join(tempDir, 'routed');
  651. fs.mkdirSync(path.join(routedRoot, 'src'), { recursive: true });
  652. fs.writeFileSync(
  653. path.join(routedRoot, 'src', 'routes.ts'),
  654. `import express from 'express';
  655. const app = express();
  656. export function listUsers(req: any, res: any): void { res.json([]); }
  657. export function getUser(req: any, res: any): void { res.json({}); }
  658. export function createUser(req: any, res: any): void { res.json({}); }
  659. export function deleteUser(req: any, res: any): void { res.json({}); }
  660. app.get('/users', listUsers);
  661. app.get('/users/:id', getUser);
  662. app.post('/users', createUser);
  663. app.delete('/users/:id', deleteUser);
  664. export default app;
  665. `
  666. );
  667. const routedCg = CodeGraph.initSync(routedRoot, {
  668. config: { include: ['src/**/*.ts'], exclude: [] },
  669. });
  670. await routedCg.indexAll();
  671. routedCg.resolveReferences();
  672. routedCg.close();
  673. routedApi = createGraphApi({ projectRoot: routedRoot });
  674. routedServer = await startUiServer({
  675. projectRoot: routedRoot,
  676. viewerDir,
  677. port: 0,
  678. api: routedApi.handler,
  679. });
  680. }, 120_000);
  681. afterAll(async () => {
  682. routedApi?.close();
  683. await routedServer?.close();
  684. });
  685. it('maps each URL to its handler, with a node id to navigate to', async () => {
  686. const res = await requestOn(routedServer.port, '/api/routes');
  687. const body = JSON.parse(res.body);
  688. expect(body.routed).toBe(true);
  689. expect(body.routeCount).toBe(4);
  690. expect(body.shown).toBe(4);
  691. expect(body.truncated).toBe(false);
  692. expect(body.topHandlerFile).toBe('src/routes.ts');
  693. expect(body.topHandlerFileCount).toBe(4);
  694. const urls = body.entries.map((e: any) => e.url);
  695. expect(urls).toEqual(
  696. expect.arrayContaining(['GET /users', 'GET /users/:id', 'POST /users', 'DELETE /users/:id'])
  697. );
  698. const listUsers = body.entries.find((e: any) => e.url === 'GET /users');
  699. expect(listUsers.handler).toBe('listUsers');
  700. expect(listUsers.handlerKind).toBe('function');
  701. expect(listUsers.file).toBe('src/routes.ts');
  702. expect(listUsers.line).toBeGreaterThan(0);
  703. // The manifest carries no ids of its own; resolving them is what makes a
  704. // route row clickable, so it has to actually resolve.
  705. expect(listUsers.handlerId).toBeTruthy();
  706. const handler = JSON.parse(
  707. (await requestOn(routedServer.port, `/api/node/${listUsers.handlerId}`)).body
  708. );
  709. expect(handler.node.name).toBe('listUsers');
  710. });
  711. it('honours the limit and says when it cut the list', async () => {
  712. const res = await requestOn(routedServer.port, '/api/routes?limit=3');
  713. const body = JSON.parse(res.body);
  714. expect(body.routed).toBe(true);
  715. expect(body.entries).toHaveLength(3);
  716. expect(body.shown).toBe(3);
  717. expect(body.truncated).toBe(true);
  718. // The headline count is the whole graph's, not the page's.
  719. expect(body.routeCount).toBe(4);
  720. });
  721. });
  722. });
  723. /**
  724. * The acceptance bar from the issue, against the engine's OWN index rather than
  725. * a fixture: `LRUCache.get` in `src/resolution/lru-cache.ts`, 500+ callers.
  726. *
  727. * `.codegraph/` is gitignored, so this only runs on a machine that has indexed
  728. * this repository. The fixture test above covers the same properties in CI; this
  729. * one is the check against the real, messy graph the number came from.
  730. */
  731. describe.runIf(CodeGraph.isInitialized(path.resolve(__dirname, '..')))(
  732. "the engine's own busiest symbol",
  733. () => {
  734. const repoRoot = path.resolve(__dirname, '..');
  735. let repoApi: GraphApi;
  736. let repoServer: UiServerHandle;
  737. beforeAll(async () => {
  738. repoApi = createGraphApi({ projectRoot: repoRoot });
  739. repoServer = await startUiServer({
  740. projectRoot: repoRoot,
  741. viewerDir,
  742. port: 0,
  743. api: repoApi.handler,
  744. });
  745. });
  746. afterAll(async () => {
  747. repoApi?.close();
  748. await repoServer?.close();
  749. });
  750. const repoGet = (requestPath: string): Promise<Response> =>
  751. requestOn(repoServer.port, requestPath);
  752. it('answers in under 100 ms with grouped, capped lists and correct counts', async () => {
  753. const search = JSON.parse(
  754. (await repoGet('/api/search?q=' + encodeURIComponent('LRUCache.get'))).body
  755. );
  756. const hit = search.results.items.find(
  757. (r: any) => r.name === 'get' && r.file.endsWith('src/resolution/lru-cache.ts')
  758. );
  759. expect(hit, 'LRUCache.get should be in the engine\'s own index').toBeTruthy();
  760. await repoGet(`/api/node/${hit.id}`); // warm
  761. const started = performance.now();
  762. const res = await repoGet(`/api/node/${hit.id}`);
  763. const elapsed = performance.now() - started;
  764. expect(res.status).toBe(200);
  765. const body = JSON.parse(res.body);
  766. expect(body.counts.fanIn).toBeGreaterThanOrEqual(500);
  767. expect(body.counts.hub).toBe(true);
  768. // Grouped by calling symbol, so the row count is the distinct-caller
  769. // count, never the edge count.
  770. expect(body.incoming.items).toHaveLength(body.incoming.shown);
  771. expect(body.incoming.shown).toBeLessThanOrEqual(300);
  772. expect(body.incoming.shown).toBe(Math.min(300, body.incoming.total));
  773. expect(body.incoming.truncated).toBe(body.incoming.total > 300);
  774. expect(new Set(body.incoming.items.map((r: any) => r.node.id)).size).toBe(
  775. body.incoming.shown
  776. );
  777. const edgesInRows = body.incoming.items.reduce(
  778. (sum: number, r: any) => sum + r.edgeCount,
  779. 0
  780. );
  781. expect(edgesInRows).toBeLessThanOrEqual(body.counts.fanIn);
  782. expect(body.blast.direct).toBe(body.counts.callers);
  783. expect(body.tests.reached).toBe(true);
  784. expect(elapsed).toBeLessThan(100);
  785. });
  786. }
  787. );
  788. describe('an index that is not there', () => {
  789. it('answers with the same guidance the CLI prints, not a stack trace', async () => {
  790. const emptyRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-ui-noindex-'));
  791. const detached = createGraphApi({ projectRoot: emptyRoot });
  792. const detachedServer = await startUiServer({
  793. projectRoot: emptyRoot,
  794. viewerDir,
  795. port: 0,
  796. api: detached.handler,
  797. });
  798. try {
  799. const res = await requestOn(detachedServer.port, '/api/stats');
  800. expect(res.status).toBe(503);
  801. const body = JSON.parse(res.body);
  802. expect(body.code).toBe('no-index');
  803. expect(body.error).toContain('No CodeGraph index found');
  804. expect(body.hint).toContain('codegraph init');
  805. expect(body.error).not.toContain(' at ');
  806. } finally {
  807. detached.close();
  808. await detachedServer.close();
  809. fs.rmSync(emptyRoot, { recursive: true, force: true });
  810. }
  811. });
  812. });