file-tree.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { beforeAll, describe, expect, mock, test } from "bun:test"
  2. let shouldListRoot: typeof import("./file-tree").shouldListRoot
  3. let shouldListExpanded: typeof import("./file-tree").shouldListExpanded
  4. let dirsToExpand: typeof import("./file-tree").dirsToExpand
  5. beforeAll(async () => {
  6. mock.module("@solidjs/router", () => ({
  7. useParams: () => ({}),
  8. }))
  9. mock.module("@/context/file", () => ({
  10. useFile: () => ({
  11. tree: {
  12. state: () => undefined,
  13. list: () => Promise.resolve(),
  14. children: () => [],
  15. expand: () => {},
  16. collapse: () => {},
  17. },
  18. }),
  19. }))
  20. mock.module("@opencode-ai/ui/collapsible", () => ({
  21. Collapsible: {
  22. Trigger: (props: { children?: unknown }) => props.children,
  23. Content: (props: { children?: unknown }) => props.children,
  24. },
  25. }))
  26. mock.module("@opencode-ai/ui/file-icon", () => ({ FileIcon: () => null }))
  27. mock.module("@opencode-ai/ui/icon", () => ({ Icon: () => null }))
  28. mock.module("@opencode-ai/ui/tooltip", () => ({ Tooltip: (props: { children?: unknown }) => props.children }))
  29. const mod = await import("./file-tree")
  30. shouldListRoot = mod.shouldListRoot
  31. shouldListExpanded = mod.shouldListExpanded
  32. dirsToExpand = mod.dirsToExpand
  33. })
  34. describe("file tree fetch discipline", () => {
  35. test("root lists on mount unless already loaded or loading", () => {
  36. expect(shouldListRoot({ level: 0 })).toBe(true)
  37. expect(shouldListRoot({ level: 0, dir: { loaded: true } })).toBe(false)
  38. expect(shouldListRoot({ level: 0, dir: { loading: true } })).toBe(false)
  39. expect(shouldListRoot({ level: 1 })).toBe(false)
  40. })
  41. test("nested dirs list only when expanded and stale", () => {
  42. expect(shouldListExpanded({ level: 1 })).toBe(false)
  43. expect(shouldListExpanded({ level: 1, dir: { expanded: false } })).toBe(false)
  44. expect(shouldListExpanded({ level: 1, dir: { expanded: true } })).toBe(true)
  45. expect(shouldListExpanded({ level: 1, dir: { expanded: true, loaded: true } })).toBe(false)
  46. expect(shouldListExpanded({ level: 1, dir: { expanded: true, loading: true } })).toBe(false)
  47. expect(shouldListExpanded({ level: 0, dir: { expanded: true } })).toBe(false)
  48. })
  49. test("allowed auto-expand picks only collapsed dirs", () => {
  50. const expanded = new Set<string>()
  51. const filter = { dirs: new Set(["src", "src/components"]) }
  52. const first = dirsToExpand({
  53. level: 0,
  54. filter,
  55. expanded: (dir) => expanded.has(dir),
  56. })
  57. expect(first).toEqual(["src", "src/components"])
  58. for (const dir of first) expanded.add(dir)
  59. const second = dirsToExpand({
  60. level: 0,
  61. filter,
  62. expanded: (dir) => expanded.has(dir),
  63. })
  64. expect(second).toEqual([])
  65. expect(dirsToExpand({ level: 1, filter, expanded: () => false })).toEqual([])
  66. })
  67. })