ui-server-api.test.ts 37 KB

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