|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import type { FileNode } from "@/runtime/server/types" |
| 3 | +import { |
| 4 | + canRefreshDirectory, |
| 5 | + createFileTreeState, |
| 6 | + DIRECTORY_LIST_TIMEOUT_MS, |
| 7 | + isDirectoryLoaded, |
| 8 | + isDirectoryLoading, |
| 9 | + selectDirectoryChildren, |
| 10 | + selectDirectoryError, |
| 11 | + transitionFileTree, |
| 12 | + type FileTreeEffect, |
| 13 | + type FileTreeEvent, |
| 14 | + type FileTreeState, |
| 15 | +} from "./tree-machine" |
| 16 | + |
| 17 | +// The server ends directory paths with the platform separator (`FileSystem.list` in packages/core/src/filesystem.ts). |
| 18 | +const file = (path: string): FileNode => ({ |
| 19 | + path, |
| 20 | + type: "file", |
| 21 | + name: path.split("/").at(-1) ?? path, |
| 22 | + absolute: `/repo/${path}`, |
| 23 | + ignored: false, |
| 24 | +}) |
| 25 | + |
| 26 | +const directory = (path: string): FileNode => ({ |
| 27 | + path: `${path}/`, |
| 28 | + type: "directory", |
| 29 | + name: path.split("/").at(-1) ?? path, |
| 30 | + absolute: `/repo/${path}`, |
| 31 | + ignored: false, |
| 32 | +}) |
| 33 | + |
| 34 | +function replay(events: readonly FileTreeEvent[], state = createFileTreeState()) { |
| 35 | + return events.reduce<{ state: FileTreeState; effects: FileTreeEffect[] }>( |
| 36 | + (current, event) => { |
| 37 | + const result = transitionFileTree(current.state, event) |
| 38 | + |
| 39 | + return { state: result.state, effects: [...current.effects, ...result.effects] } |
| 40 | + }, |
| 41 | + { state, effects: [] }, |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +const dir = (state: FileTreeState, path: string) => (state.status === "active" ? state.dirs[path] : undefined) |
| 46 | + |
| 47 | +const paths = (state: FileTreeState, path: string) => selectDirectoryChildren(state, path).map((node) => node.path) |
| 48 | + |
| 49 | +const list = (path: string, attempt: number): FileTreeEffect => ({ |
| 50 | + type: "list", |
| 51 | + dir: path, |
| 52 | + attempt, |
| 53 | + timeoutMs: DIRECTORY_LIST_TIMEOUT_MS, |
| 54 | +}) |
| 55 | + |
| 56 | +describe("file tree machine", () => { |
| 57 | + test("a forced reload during a listing runs one follow-up listing instead of joining it", () => { |
| 58 | + const started = replay([{ type: "list.requested", dir: "src", force: false }]) |
| 59 | + expect(started.effects).toEqual([list("src", 1)]) |
| 60 | + |
| 61 | + const queued = replay( |
| 62 | + [ |
| 63 | + { type: "list.requested", dir: "src", force: true }, |
| 64 | + { type: "list.requested", dir: "src", force: true }, |
| 65 | + ], |
| 66 | + started.state, |
| 67 | + ) |
| 68 | + |
| 69 | + expect(queued.effects).toEqual([]) |
| 70 | + expect(dir(queued.state, "src")).toMatchObject({ status: "loading", attempt: 1, dirty: true }) |
| 71 | + expect(transitionFileTree(queued.state, { type: "list.requested", dir: "src", force: true }).state).toBe( |
| 72 | + queued.state, |
| 73 | + ) |
| 74 | + |
| 75 | + // The first read happened before `b.ts` was created: its result shows at once, and the follow-up reads again. |
| 76 | + const first = transitionFileTree(queued.state, { |
| 77 | + type: "list.loaded", |
| 78 | + dir: "src", |
| 79 | + attempt: 1, |
| 80 | + nodes: [file("src/a.ts")], |
| 81 | + }) |
| 82 | + |
| 83 | + expect(paths(first.state, "src")).toEqual(["src/a.ts"]) |
| 84 | + expect(dir(first.state, "src")).toMatchObject({ status: "refreshing", attempt: 2, dirty: false }) |
| 85 | + expect(first.effects).toEqual([list("src", 2)]) |
| 86 | + |
| 87 | + const second = transitionFileTree(first.state, { |
| 88 | + type: "list.loaded", |
| 89 | + dir: "src", |
| 90 | + attempt: 2, |
| 91 | + nodes: [file("src/a.ts"), file("src/b.ts")], |
| 92 | + }) |
| 93 | + |
| 94 | + expect(paths(second.state, "src")).toEqual(["src/a.ts", "src/b.ts"]) |
| 95 | + expect(dir(second.state, "src")?.status).toBe("ready") |
| 96 | + expect(second.effects).toEqual([]) |
| 97 | + }) |
| 98 | + |
| 99 | + test("a watcher change during a directory's first listing can reload it", () => { |
| 100 | + const loading = replay([{ type: "dir.expanded", dir: "src", list: true }]).state |
| 101 | + |
| 102 | + expect(canRefreshDirectory(loading, "src")).toBe(true) |
| 103 | + expect(canRefreshDirectory(loading, "lib")).toBe(false) |
| 104 | + |
| 105 | + const failed = replay( |
| 106 | + [ |
| 107 | + { type: "list.requested", dir: "lib", force: false }, |
| 108 | + { type: "list.failed", dir: "lib", attempt: 2, error: "boom" }, |
| 109 | + ], |
| 110 | + loading, |
| 111 | + ).state |
| 112 | + |
| 113 | + expect(canRefreshDirectory(failed, "lib")).toBe(false) |
| 114 | + |
| 115 | + const queued = transitionFileTree(loading, { type: "list.requested", dir: "src", force: true }) |
| 116 | + |
| 117 | + const loaded = transitionFileTree(queued.state, { |
| 118 | + type: "list.loaded", |
| 119 | + dir: "src", |
| 120 | + attempt: 1, |
| 121 | + nodes: [file("src/a.ts")], |
| 122 | + }) |
| 123 | + |
| 124 | + expect(loaded.effects).toEqual([list("src", 2)]) |
| 125 | + }) |
| 126 | + |
| 127 | + test("results from before a workspace reset never land in the new workspace", () => { |
| 128 | + const before = replay([{ type: "list.requested", dir: "", force: false }]).state |
| 129 | + const reset = transitionFileTree(before, { type: "reset" }).state |
| 130 | + expect(reset).toMatchObject({ epoch: 1, issued: 1 }) |
| 131 | + |
| 132 | + const after = transitionFileTree(reset, { type: "list.requested", dir: "", force: false }) |
| 133 | + expect(after.effects).toEqual([list("", 2)]) |
| 134 | + |
| 135 | + const late: FileTreeEvent[] = [ |
| 136 | + { type: "list.loaded", dir: "", attempt: 1, nodes: [file("a.ts")] }, |
| 137 | + { type: "list.failed", dir: "", attempt: 1, error: "gone" }, |
| 138 | + { type: "list.timed-out", dir: "", attempt: 1 }, |
| 139 | + ] |
| 140 | + |
| 141 | + late.forEach((event) => { |
| 142 | + const result = transitionFileTree(after.state, event) |
| 143 | + expect(result.state).toBe(after.state) |
| 144 | + expect(result.effects).toEqual([]) |
| 145 | + }) |
| 146 | + }) |
| 147 | + |
| 148 | + test("a failed reload keeps the listed entries without an error, and a failed first listing reports it", () => { |
| 149 | + const ready = replay([ |
| 150 | + { type: "list.requested", dir: "src", force: false }, |
| 151 | + { type: "list.loaded", dir: "src", attempt: 1, nodes: [file("src/a.ts")] }, |
| 152 | + ]).state |
| 153 | + |
| 154 | + const reloaded = replay( |
| 155 | + [ |
| 156 | + { type: "list.requested", dir: "src", force: true }, |
| 157 | + { type: "list.failed", dir: "src", attempt: 2, error: "boom" }, |
| 158 | + ], |
| 159 | + ready, |
| 160 | + ) |
| 161 | + |
| 162 | + const entry = dir(reloaded.state, "src") |
| 163 | + expect(entry?.status).toBe("ready") |
| 164 | + expect(isDirectoryLoaded(entry)).toBe(true) |
| 165 | + expect(isDirectoryLoading(entry)).toBe(false) |
| 166 | + expect(selectDirectoryError(entry)).toBeUndefined() |
| 167 | + expect(paths(reloaded.state, "src")).toEqual(["src/a.ts"]) |
| 168 | + expect(reloaded.effects).toEqual([list("src", 2)]) |
| 169 | + |
| 170 | + const failed = replay([ |
| 171 | + { type: "list.requested", dir: "src", force: false }, |
| 172 | + { type: "list.failed", dir: "src", attempt: 1, error: "boom" }, |
| 173 | + ]) |
| 174 | + |
| 175 | + expect(isDirectoryLoaded(dir(failed.state, "src"))).toBe(false) |
| 176 | + expect(selectDirectoryError(dir(failed.state, "src"))).toBe("boom") |
| 177 | + expect(failed.effects.at(-1)).toEqual({ type: "notify.error", message: "boom" }) |
| 178 | + |
| 179 | + // Opening the directory again retries it. |
| 180 | + expect(transitionFileTree(failed.state, { type: "dir.expanded", dir: "src", list: true }).effects).toEqual([ |
| 181 | + list("src", 2), |
| 182 | + ]) |
| 183 | + }) |
| 184 | + |
| 185 | + test.each([ |
| 186 | + ["removed", [file("src/a.ts")]], |
| 187 | + ["replaced by a file", [file("src/a.ts"), file("src/sub")]], |
| 188 | + ])("a subdirectory %s drops its entries and its listing state", (_, next) => { |
| 189 | + const loaded = replay([ |
| 190 | + { type: "list.requested", dir: "src", force: false }, |
| 191 | + { type: "list.loaded", dir: "src", attempt: 1, nodes: [file("src/a.ts"), directory("src/sub")] }, |
| 192 | + { type: "dir.expanded", dir: "src/sub", list: true }, |
| 193 | + { type: "list.loaded", dir: "src/sub", attempt: 2, nodes: [file("src/sub/old.ts"), directory("src/sub/deep")] }, |
| 194 | + { type: "dir.expanded", dir: "src/sub/deep", list: true }, |
| 195 | + ]).state |
| 196 | + |
| 197 | + const refreshed = replay( |
| 198 | + [ |
| 199 | + { type: "list.requested", dir: "src", force: true }, |
| 200 | + { type: "list.loaded", dir: "src", attempt: 4, nodes: next }, |
| 201 | + ], |
| 202 | + loaded, |
| 203 | + ).state |
| 204 | + |
| 205 | + expect(refreshed.status === "active" && Object.keys(refreshed.dirs).sort()).toEqual(["", "src"]) |
| 206 | + expect(refreshed.status === "active" && Object.keys(refreshed.nodes).sort()).toEqual( |
| 207 | + next.map((node) => node.path).sort(), |
| 208 | + ) |
| 209 | + |
| 210 | + // The pruned in-flight listing of `src/sub/deep` lands nowhere. |
| 211 | + expect( |
| 212 | + transitionFileTree(refreshed, { type: "list.loaded", dir: "src/sub/deep", attempt: 3, nodes: [] }).state, |
| 213 | + ).toBe(refreshed) |
| 214 | + |
| 215 | + // A re-created directory lists again instead of showing as empty. |
| 216 | + expect(transitionFileTree(refreshed, { type: "dir.expanded", dir: "src/sub", list: true }).effects).toEqual([ |
| 217 | + list("src/sub", 5), |
| 218 | + ]) |
| 219 | + }) |
| 220 | + |
| 221 | + test("a timeout ends the listing: a first listing fails, a reload keeps its entries, a queued reload runs", () => { |
| 222 | + const loading = replay([{ type: "list.requested", dir: "src", force: false }]).state |
| 223 | + const failed = transitionFileTree(loading, { type: "list.timed-out", dir: "src", attempt: 1 }) |
| 224 | + expect(dir(failed.state, "src")).toEqual({ status: "failed", expanded: false, error: undefined }) |
| 225 | + expect(failed.effects).toEqual([{ type: "notify.error", message: "" }]) |
| 226 | + |
| 227 | + const refreshing = replay( |
| 228 | + [ |
| 229 | + { type: "list.loaded", dir: "src", attempt: 1, nodes: [file("src/a.ts")] }, |
| 230 | + { type: "list.requested", dir: "src", force: true }, |
| 231 | + ], |
| 232 | + loading, |
| 233 | + ).state |
| 234 | + |
| 235 | + const kept = transitionFileTree(refreshing, { type: "list.timed-out", dir: "src", attempt: 2 }) |
| 236 | + expect(dir(kept.state, "src")).toEqual({ status: "ready", expanded: false, children: ["src/a.ts"] }) |
| 237 | + expect(kept.effects).toEqual([]) |
| 238 | + |
| 239 | + const queued = transitionFileTree(refreshing, { type: "list.requested", dir: "src", force: true }).state |
| 240 | + const retried = transitionFileTree(queued, { type: "list.timed-out", dir: "src", attempt: 2 }) |
| 241 | + expect(dir(retried.state, "src")).toMatchObject({ status: "refreshing", attempt: 3, dirty: false }) |
| 242 | + expect(retried.effects).toEqual([list("src", 3)]) |
| 243 | + }) |
| 244 | + |
| 245 | + test("expanding and collapsing keep the listing and list only when asked", () => { |
| 246 | + const initial = createFileTreeState() |
| 247 | + |
| 248 | + const filtered = transitionFileTree(initial, { type: "dir.expanded", dir: "src", list: false }) |
| 249 | + expect(dir(filtered.state, "src")).toEqual({ status: "idle", expanded: true }) |
| 250 | + expect(filtered.effects).toEqual([]) |
| 251 | + expect(transitionFileTree(filtered.state, { type: "dir.expanded", dir: "src", list: false }).state).toBe( |
| 252 | + filtered.state, |
| 253 | + ) |
| 254 | + |
| 255 | + const collapsed = transitionFileTree(initial, { type: "dir.collapsed", dir: "src" }).state |
| 256 | + expect(dir(collapsed, "src")).toEqual({ status: "idle", expanded: false }) |
| 257 | + expect(transitionFileTree(collapsed, { type: "dir.collapsed", dir: "src" }).state).toBe(collapsed) |
| 258 | + |
| 259 | + const ready = replay( |
| 260 | + [ |
| 261 | + { type: "dir.expanded", dir: "src", list: true }, |
| 262 | + { type: "list.loaded", dir: "src", attempt: 1, nodes: [file("src/a.ts")] }, |
| 263 | + { type: "dir.collapsed", dir: "src" }, |
| 264 | + ], |
| 265 | + initial, |
| 266 | + ).state |
| 267 | + |
| 268 | + expect(paths(ready, "src")).toEqual(["src/a.ts"]) |
| 269 | + |
| 270 | + const reopened = transitionFileTree(ready, { type: "dir.expanded", dir: "src", list: true }) |
| 271 | + expect(dir(reopened.state, "src")).toMatchObject({ status: "ready", expanded: true }) |
| 272 | + expect(reopened.effects).toEqual([]) |
| 273 | + expect(transitionFileTree(reopened.state, { type: "list.requested", dir: "src", force: false }).state).toBe( |
| 274 | + reopened.state, |
| 275 | + ) |
| 276 | + }) |
| 277 | + |
| 278 | + test("an untouched tree ignores a reset, and a disposed tree ignores everything", () => { |
| 279 | + const initial = createFileTreeState() |
| 280 | + expect(transitionFileTree(initial, { type: "reset" }).state).toBe(initial) |
| 281 | + |
| 282 | + const loading = replay([{ type: "list.requested", dir: "src", force: false }]).state |
| 283 | + const disposed = transitionFileTree(loading, { type: "disposed" }).state |
| 284 | + expect(disposed).toEqual({ status: "disposed" }) |
| 285 | + expect(canRefreshDirectory(disposed, "src")).toBe(false) |
| 286 | + |
| 287 | + const events: FileTreeEvent[] = [ |
| 288 | + { type: "list.loaded", dir: "src", attempt: 1, nodes: [file("src/a.ts")] }, |
| 289 | + { type: "list.failed", dir: "src", attempt: 1, error: "boom" }, |
| 290 | + { type: "list.requested", dir: "src", force: true }, |
| 291 | + { type: "reset" }, |
| 292 | + ] |
| 293 | + |
| 294 | + events.forEach((event) => { |
| 295 | + const result = transitionFileTree(disposed, event) |
| 296 | + expect(result.state).toBe(disposed) |
| 297 | + expect(result.effects).toEqual([]) |
| 298 | + }) |
| 299 | + }) |
| 300 | +}) |
0 commit comments