GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — opencode/packages/app/src/workspaces/inventory.test.ts at dev · drpr/opencode

Expand file tree

/

inventory.test.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
151 lines (129 loc) · 5.52 KB
/

inventory.test.ts

Copy path

File metadata and controls

151 lines (129 loc) · 5.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { describe, expect, test } from "bun:test"
import { QueryClient } from "@tanstack/solid-query"
import type { WorktreeDirectory } from "@opencode/client/promise"
import { createWorktreeInventory, withWorktreeInventory, worktreeInventoryKey } from "./inventory"
import { ServerScope } from "@/runtime/server/scope"
import { normalizeProjectInfo, updateProjectInfo } from "@/runtime/server/global-sync/utils"
function setup(list: (directory: string) => Promise<WorktreeDirectory[]>) {
const client = new QueryClient()
const refreshed = Promise.withResolvers<void>()
const refreshes: string[] = []
const calls: string[] = []
const updates: Array<[string, WorktreeDirectory[]]> = []
const inventory = createWorktreeInventory({
scope: ServerScope.local,
queryClient: client,
api: () => ({
list: (input) => {
const directory = input.projectID
calls.push(directory)
return list(directory)
},
refresh: (input) => {
refreshes.push(input.projectID)
return refreshed.promise
},
}),
updated: (directory, items) => {
updates.push([directory, items])
},
})
return { client, calls, updates, inventory, refreshed, refreshes }
}
describe("createWorktreeInventory", () => {
test("lists a project, shares in-flight work, and publishes each result", async () => {
const gate = Promise.withResolvers<void>()
const setupResult = setup(async (directory) => {
await gate.promise
return [{ directory }, { directory: `${directory}/feature`, strategy: "git" }]
})
const first = setupResult.inventory.list("/repo")
const second = setupResult.inventory.list("/repo")
expect(setupResult.calls).toEqual(["/repo"])
gate.resolve()
expect(await first).toHaveLength(2)
expect(await second).toHaveLength(2)
await setupResult.inventory.list("/repo")
expect(setupResult.calls).toEqual(["/repo", "/repo"])
expect(setupResult.updates).toEqual([
["/repo", [{ directory: "/repo" }, { directory: "/repo/feature", strategy: "git" }]],
["/repo", [{ directory: "/repo" }, { directory: "/repo/feature", strategy: "git" }]],
])
expect(setupResult.inventory.cached("/repo")).toHaveLength(2)
expect(setupResult.refreshes).toEqual([])
setupResult.client.clear()
})
test("list reads saved inventory without discovery", async () => {
const setupResult = setup(async (directory) => [{ directory }])
await setupResult.inventory.list("/opened")
expect(setupResult.calls).toEqual(["/opened"])
expect(setupResult.refreshes).toEqual([])
setupResult.client.clear()
})
test("a failed list is not cached and never rejects the caller", async () => {
let fail = true
const setupResult = setup(async (directory) => {
if (fail) throw new Error("Location unavailable")
return [{ directory }]
})
expect(await setupResult.inventory.list("/repo")).toBeUndefined()
expect(setupResult.inventory.cached("/repo")).toBeUndefined()
fail = false
expect(await setupResult.inventory.list("/repo")).toEqual([{ directory: "/repo" }])
expect(setupResult.calls).toEqual(["/repo", "/repo"])
setupResult.client.clear()
})
test("keys are partitioned by server and use opaque project IDs", () => {
const remote = "https://remote.example" as typeof ServerScope.local
expect(worktreeInventoryKey(ServerScope.local, "project")).not.toEqual(
worktreeInventoryKey(ServerScope.local, "project/"),
)
expect(worktreeInventoryKey(ServerScope.local, "/repo")).not.toEqual(worktreeInventoryKey(remote, "/repo"))
})
test("refresh discovers without changing cached inventory until the next list", async () => {
const rows = [{ directory: "/repo" }]
const result = setup(async () => [...rows])
expect(await result.inventory.list("project")).toEqual(rows)
const pending = result.inventory.refresh("project")
rows.push({ directory: "/external" })
expect(result.inventory.cached("project")).toEqual([{ directory: "/repo" }])
result.refreshed.resolve()
expect(await pending).toBeUndefined()
expect(result.calls).toEqual(["project"])
expect(result.refreshes).toEqual(["project"])
expect(await result.inventory.list("project")).toEqual(rows)
result.client.clear()
})
})
describe("withWorktreeInventory", () => {
const metadata = {
id: "project",
canonical: "/repo",
name: "Before",
time: { created: 1, updated: 1, active: 1 },
sandboxes: [],
}
test("derives the workspace list from the inventory, excluding the project root", () => {
const worktrees = [
{ directory: "/repo/" },
{ directory: "/repo/feature", strategy: "git" },
{ directory: "/elsewhere" },
]
expect(withWorktreeInventory(normalizeProjectInfo(metadata), worktrees)).toMatchObject({
worktree: "/repo",
sandboxes: ["/repo/feature", "/elsewhere"],
worktrees,
})
})
test("leaves metadata untouched without an inventory and survives metadata updates", () => {
const project = normalizeProjectInfo(metadata)
expect(withWorktreeInventory(project, undefined)).toBe(project)
const cached = [{ directory: "/repo" }, { directory: "/repo/feature", strategy: "git" }]
const updated = updateProjectInfo(
{ ...withWorktreeInventory(project, cached), worktree: "/old-repo" },
{ ...metadata, name: "After" },
)
expect(updated).toMatchObject({ worktree: "/repo", worktrees: cached })
expect(withWorktreeInventory(updated, cached)).toMatchObject({ name: "After", sandboxes: ["/repo/feature"] })
})
})
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.