ui-file-model.test.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * The File view's models, without a browser (CG-46).
  3. *
  4. * The decision under test throughout is the rails' source of truth: they are
  5. * built from `dependencies` / `dependents` — the engine's own
  6. * `getFileDependencies` / `getFileDependents` — and merely *decorated* with
  7. * the `imports` rows. Getting that backwards is not a cosmetic bug: it silently
  8. * understates what a change to the file would reach, which is the only reason
  9. * the screen exists.
  10. *
  11. * The geometry-free sibling of `ui-symbol-model.test.ts` and
  12. * `ui-search-model.test.ts`.
  13. */
  14. import { describe, it, expect } from 'vitest';
  15. import {
  16. buildFileOutline,
  17. buildFileRail,
  18. fileMetaLine,
  19. formatBytes,
  20. looksLikeTest,
  21. OUTLINE_ROW_HEIGHT,
  22. OUTLINE_VIRTUAL_THRESHOLD,
  23. } from '../ui/src/lib/file-model';
  24. import type { WireFilePayload, WireImportRow, WireOutlineEntry } from '../ui/src/lib/api';
  25. /* ------------------------------------------------------------- fixtures -- */
  26. function importRow(over: Partial<WireImportRow> = {}): WireImportRow {
  27. const symbols = over.symbols ?? [
  28. { id: 'class:Q', name: 'QueryBuilder', kind: 'class', line: 219 },
  29. ];
  30. return {
  31. file: over.file ?? 'src/db/queries.ts',
  32. test: over.test ?? false,
  33. symbols,
  34. symbolCount: over.symbolCount ?? symbols.length,
  35. };
  36. }
  37. function entry(over: Partial<WireOutlineEntry> = {}): WireOutlineEntry {
  38. return {
  39. id: over.id ?? 'method:x',
  40. kind: 'method',
  41. name: 'traverseBFS',
  42. qualifiedName: 'GraphTraverser.traverseBFS',
  43. file: 'src/graph/traversal.ts',
  44. line: 48,
  45. endLine: 150,
  46. language: 'typescript',
  47. test: false,
  48. parentId: 'class:GraphTraverser',
  49. depth: 1,
  50. fanIn: 3,
  51. fanOut: 7,
  52. ...over,
  53. } as WireOutlineEntry;
  54. }
  55. function payload(over: Partial<WireFilePayload> = {}): WireFilePayload {
  56. return {
  57. file: {
  58. path: 'src/graph/traversal.ts',
  59. language: 'typescript',
  60. size: 24216,
  61. modifiedAt: 1,
  62. indexedAt: 2,
  63. contentHash: 'abc',
  64. nodeCount: 26,
  65. generated: false,
  66. test: false,
  67. errors: [],
  68. id: 'file:src/graph/traversal.ts',
  69. },
  70. topLevel: { calls: 0 },
  71. drift: false,
  72. outline: { total: 0, shown: 0, truncated: false, items: [] },
  73. imports: { total: 0, shown: 0, truncated: false, items: [] },
  74. importedBy: { total: 0, shown: 0, truncated: false, items: [] },
  75. unresolvedImports: [],
  76. dependencies: [],
  77. dependents: [],
  78. ...over,
  79. } as WireFilePayload;
  80. }
  81. /* ----------------------------------------------------------------- rail -- */
  82. describe('the import rails', () => {
  83. it('counts every dependency, not just the ones an import statement named', () => {
  84. // The real shape on this repo: traversal.ts imports two files and depends
  85. // on four — it reaches the LRU cache through a call with no import.
  86. const rail = buildFileRail(
  87. [
  88. 'src/db/queries.ts',
  89. 'src/resolution/lru-cache.ts',
  90. 'src/types.ts',
  91. 'scripts/agent-eval/probe.mjs',
  92. ],
  93. [importRow({ file: 'src/db/queries.ts' }), importRow({ file: 'src/types.ts' })]
  94. );
  95. expect(rail.total).toBe(4);
  96. expect(rail.rows).toHaveLength(4);
  97. expect(rail.rows.filter((r) => r.imported).map((r) => r.path)).toEqual([
  98. 'src/db/queries.ts',
  99. 'src/types.ts',
  100. ]);
  101. expect(rail.rows.find((r) => r.path === 'src/resolution/lru-cache.ts')?.imported).toBe(false);
  102. });
  103. it('names the symbols an import row carries, on the row for that file', () => {
  104. const rail = buildFileRail(
  105. ['src/db/queries.ts'],
  106. [
  107. importRow({
  108. symbols: [
  109. { id: 'class:Q', name: 'QueryBuilder', kind: 'class', line: 219 },
  110. { id: 'iface:R', name: 'Row', kind: 'interface', line: 12 },
  111. ],
  112. }),
  113. ]
  114. );
  115. expect(rail.rows[0]?.symbols.map((s) => s.name)).toEqual(['QueryBuilder', 'Row']);
  116. expect(rail.rows[0]?.symbolCount).toBe(2);
  117. });
  118. it('does not count a file node as a named symbol', () => {
  119. // An `importedBy` edge's far end is the importing file's own file node, so
  120. // its "symbols" repeat the path already in the row. A `1` there would be a
  121. // count of nothing.
  122. const rail = buildFileRail(
  123. ['src/index.ts'],
  124. [
  125. importRow({
  126. file: 'src/index.ts',
  127. symbols: [{ id: 'file:src/index.ts', name: 'index.ts', kind: 'file', line: 1 }],
  128. }),
  129. ]
  130. );
  131. expect(rail.rows[0]?.symbolCount).toBe(0);
  132. expect(rail.rows[0]?.imported).toBe(true);
  133. });
  134. it('sorts production files before tests, each alphabetically', () => {
  135. const rail = buildFileRail(
  136. ['src/z.ts', '__tests__/graph.test.ts', 'src/a.ts', '__tests__/a.test.ts'],
  137. []
  138. );
  139. expect(rail.rows.map((r) => r.path)).toEqual([
  140. 'src/a.ts',
  141. 'src/z.ts',
  142. '__tests__/a.test.ts',
  143. '__tests__/graph.test.ts',
  144. ]);
  145. expect(rail.testCount).toBe(2);
  146. });
  147. it('trusts the server about what is a test, and falls back to the path', () => {
  148. const rail = buildFileRail(
  149. ['src/looks-normal.ts', 'src/other.ts'],
  150. // The server can see more than a path; a row it marks wins.
  151. [importRow({ file: 'src/looks-normal.ts', test: true })]
  152. );
  153. expect(rail.rows[0]?.path).toBe('src/other.ts');
  154. expect(rail.rows[1]?.test).toBe(true);
  155. });
  156. it('de-duplicates a file the engine listed twice', () => {
  157. const rail = buildFileRail(['src/a.ts', 'src/a.ts'], []);
  158. expect(rail.rows).toHaveLength(1);
  159. expect(rail.total).toBe(1);
  160. });
  161. it('folds unresolved imports by name, keeping every line', () => {
  162. const rail = buildFileRail(
  163. [],
  164. [],
  165. [
  166. { name: 'node:fs', line: 12 },
  167. { name: 'react', line: 3 },
  168. { name: 'node:fs', line: 4 },
  169. ]
  170. );
  171. expect(rail.outside).toEqual([
  172. { name: 'node:fs', lines: [4, 12] },
  173. { name: 'react', lines: [3] },
  174. ]);
  175. // Outside-index rows never inflate the dependency count.
  176. expect(rail.total).toBe(0);
  177. });
  178. });
  179. describe('looksLikeTest', () => {
  180. it('recognises the shapes an unnamed dependency can arrive in', () => {
  181. expect(looksLikeTest('__tests__/graph.test.ts')).toBe(true);
  182. expect(looksLikeTest('src/service.spec.ts')).toBe(true);
  183. expect(looksLikeTest('test/helper.go')).toBe(true);
  184. expect(looksLikeTest('__tests__/fixtures/app/main.ts')).toBe(true);
  185. });
  186. it('errs towards production — misfiling a real file is the worse mistake', () => {
  187. expect(looksLikeTest('src/latest.ts')).toBe(false);
  188. expect(looksLikeTest('src/protest/index.ts')).toBe(false);
  189. expect(looksLikeTest('src/testing-library.ts')).toBe(false);
  190. });
  191. });
  192. /* -------------------------------------------------------------- outline -- */
  193. describe('the file outline', () => {
  194. it('keeps the server order and indents by depth', () => {
  195. const rows = buildFileOutline(
  196. payload({
  197. outline: {
  198. total: 3,
  199. shown: 3,
  200. truncated: false,
  201. items: [
  202. entry({ id: 'class:C', kind: 'class', name: 'GraphTraverser', depth: 0, line: 34 }),
  203. entry({ id: 'method:m', depth: 1, line: 48 }),
  204. entry({ id: 'prop:p', kind: 'property', name: 'queries', depth: 1, line: 35 }),
  205. ],
  206. },
  207. })
  208. );
  209. expect(rows.map((r) => r.entry.id)).toEqual(['class:C', 'method:m', 'prop:p']);
  210. expect(rows.map((r) => r.indent)).toEqual([0, 1, 1]);
  211. });
  212. it('dims data rather than behaviour', () => {
  213. const rows = buildFileOutline(
  214. payload({
  215. outline: {
  216. total: 4,
  217. shown: 4,
  218. truncated: false,
  219. items: [
  220. entry({ id: 'a', kind: 'property' }),
  221. entry({ id: 'b', kind: 'enum_member' }),
  222. entry({ id: 'c', kind: 'method' }),
  223. entry({ id: 'd', kind: 'class' }),
  224. ],
  225. },
  226. })
  227. );
  228. expect(rows.map((r) => r.dimmed)).toEqual([true, true, false, false]);
  229. });
  230. it('clamps the indent so a deeply nested closure stays in its column', () => {
  231. const rows = buildFileOutline(
  232. payload({
  233. outline: {
  234. total: 1,
  235. shown: 1,
  236. truncated: false,
  237. items: [entry({ depth: 9 })],
  238. },
  239. })
  240. );
  241. expect(rows[0]?.indent).toBe(3);
  242. });
  243. it('windows past a threshold that leaves ordinary files alone', () => {
  244. // 135 symbols in this repo's biggest hand-written file (src/mcp/tools.ts);
  245. // 1,681 in the generated fixture that motivated the window.
  246. expect(OUTLINE_VIRTUAL_THRESHOLD).toBeGreaterThan(135);
  247. expect(OUTLINE_ROW_HEIGHT).toBeGreaterThan(0);
  248. });
  249. });
  250. /* --------------------------------------------------------------- header -- */
  251. describe('the header line', () => {
  252. it('counts the outline, not the file record', () => {
  253. // nodeCount includes the file node and its import declarations; neither is
  254. // a row, and a header disagreeing with the list under it is unresolvable.
  255. const line = fileMetaLine(
  256. payload({
  257. file: { ...payload().file, nodeCount: 26 },
  258. outline: { total: 23, shown: 23, truncated: false, items: [] },
  259. })
  260. );
  261. expect(line).toBe('typescript · 23.6 KB · 23 symbols');
  262. });
  263. it('tags a generated file and a test file', () => {
  264. const line = fileMetaLine(
  265. payload({
  266. file: { ...payload().file, generated: true, test: true, size: 1024 },
  267. outline: { total: 1, shown: 1, truncated: false, items: [] },
  268. })
  269. );
  270. expect(line).toBe('typescript · 1.0 KB · 1 symbol · generated · test');
  271. });
  272. it('formats sizes for scale, never for accounting', () => {
  273. expect(formatBytes(0)).toBe('0 B');
  274. expect(formatBytes(999)).toBe('999 B');
  275. expect(formatBytes(24216)).toBe('23.6 KB');
  276. expect(formatBytes(5 * 1024 * 1024)).toBe('5.0 MB');
  277. expect(formatBytes(Number.NaN)).toBe('—');
  278. });
  279. });