|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { createFileTreeStore } from "./tree-store" |
| 3 | +import type { FileNode } from "@opencode-ai/sdk/v2" |
| 4 | + |
| 5 | +function node(path: string, type: FileNode["type"] = "file"): FileNode { |
| 6 | + return { path, type, name: path.split("/").pop() ?? path, absolute: `/repo/${path}`, ignored: false } |
| 7 | +} |
| 8 | + |
| 9 | +function makeStore(listing: Record<string, FileNode[]>) { |
| 10 | + const calls: string[] = [] |
| 11 | + const store = createFileTreeStore({ |
| 12 | + scope: () => "", |
| 13 | + normalizeDir: (input) => input, |
| 14 | + list: (dir) => { |
| 15 | + calls.push(dir) |
| 16 | + return Promise.resolve(listing[dir] ?? []) |
| 17 | + }, |
| 18 | + onError: () => {}, |
| 19 | + }) |
| 20 | + return { store, calls } |
| 21 | +} |
| 22 | + |
| 23 | +describe("file tree store", () => { |
| 24 | + test("lists a directory once and caches it", async () => { |
| 25 | + const listing: Record<string, FileNode[]> = { "": [node("src", "directory")] } |
| 26 | + const { store, calls } = makeStore(listing) |
| 27 | + |
| 28 | + await store.listDir("") |
| 29 | + await store.listDir("") |
| 30 | + |
| 31 | + expect(calls).toEqual([""]) |
| 32 | + expect(store.children("")).toHaveLength(1) |
| 33 | + expect(store.loadedDirs()).toEqual([""]) |
| 34 | + }) |
| 35 | + |
| 36 | + test("refreshAll force-reloads loaded directories and picks up new entries", async () => { |
| 37 | + const listing: Record<string, FileNode[]> = { |
| 38 | + "": [node("src", "directory")], |
| 39 | + src: [node("src/a.ts")], |
| 40 | + } |
| 41 | + const { store, calls } = makeStore(listing) |
| 42 | + |
| 43 | + await store.listDir("") |
| 44 | + await store.listDir("src") |
| 45 | + expect(store.children("src")).toHaveLength(1) |
| 46 | + |
| 47 | + listing.src = [node("src/a.ts"), node("src/b.ts")] |
| 48 | + await store.refreshAll() |
| 49 | + |
| 50 | + expect(store.children("src")).toHaveLength(2) |
| 51 | + expect(calls).toEqual(["", "src", "", "src"]) |
| 52 | + }) |
| 53 | + |
| 54 | + test("refreshAll skips directories that were never loaded", async () => { |
| 55 | + const listing: Record<string, FileNode[]> = { |
| 56 | + "": [node("src", "directory")], |
| 57 | + src: [node("src/a.ts")], |
| 58 | + } |
| 59 | + const { store, calls } = makeStore(listing) |
| 60 | + |
| 61 | + await store.listDir("") |
| 62 | + await store.refreshAll() |
| 63 | + |
| 64 | + expect(calls).toEqual(["", ""]) |
| 65 | + expect(store.children("src")).toEqual([]) |
| 66 | + }) |
| 67 | + |
| 68 | + test("refreshAll resolves when nothing is loaded", async () => { |
| 69 | + const { store, calls } = makeStore({}) |
| 70 | + await store.refreshAll() |
| 71 | + expect(calls).toEqual([]) |
| 72 | + }) |
| 73 | +}) |
0 commit comments