@zyro-sec/server 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +15 -21
- package/script/build.ts +28 -0
- package/script/pack.ts +5 -0
- package/script/workerd-probe.ts +59 -0
- package/src/api.ts +12 -0
- package/src/auth.ts +70 -0
- package/src/content-headers.ts +32 -0
- package/src/cors.ts +34 -0
- package/src/event-feed.ts +93 -0
- package/src/fetch.ts +67 -0
- package/src/handlers/agent.ts +23 -0
- package/src/handlers/artifact.ts +242 -0
- package/src/handlers/automation.ts +63 -0
- package/src/handlers/blob.ts +55 -0
- package/src/handlers/command.ts +8 -0
- package/src/handlers/config.ts +26 -0
- package/src/handlers/credential.ts +87 -0
- package/src/handlers/debug.ts +57 -0
- package/src/handlers/event.ts +38 -0
- package/src/handlers/form.ts +15 -0
- package/src/handlers/fs.ts +180 -0
- package/src/handlers/generate.ts +117 -0
- package/src/handlers/integration.ts +310 -0
- package/src/handlers/job.ts +29 -0
- package/src/handlers/location.ts +33 -0
- package/src/handlers/machine.ts +336 -0
- package/src/handlers/mcp.ts +160 -0
- package/src/handlers/media.ts +44 -0
- package/src/handlers/message.ts +82 -0
- package/src/handlers/migration.ts +13 -0
- package/src/handlers/model.ts +25 -0
- package/src/handlers/permission.ts +131 -0
- package/src/handlers/persistent-pty.ts +285 -0
- package/src/handlers/plugin.ts +97 -0
- package/src/handlers/project.ts +23 -0
- package/src/handlers/provider.ts +32 -0
- package/src/handlers/pty-socket.ts +47 -0
- package/src/handlers/pty.ts +181 -0
- package/src/handlers/push.ts +67 -0
- package/src/handlers/reference.ts +8 -0
- package/src/handlers/rpc.ts +31 -0
- package/src/handlers/server.ts +58 -0
- package/src/handlers/session-error.ts +47 -0
- package/src/handlers/session.ts +722 -0
- package/src/handlers/shell.ts +72 -0
- package/src/handlers/skill.ts +34 -0
- package/src/handlers/usage.ts +23 -0
- package/src/handlers/vcs.ts +104 -0
- package/src/handlers/voice.ts +50 -0
- package/src/handlers/websearch.ts +50 -0
- package/src/handlers/worktree.ts +60 -0
- package/src/handlers.ts +69 -0
- package/src/location.ts +86 -0
- package/src/media-uri.ts +13 -0
- package/src/middleware/authorization.ts +111 -0
- package/src/middleware/defect-response.ts +39 -0
- package/src/middleware/form-location.ts +44 -0
- package/src/middleware/schema-error.ts +49 -0
- package/src/middleware/session-location.ts +30 -0
- package/src/options.ts +61 -0
- package/src/pairing.ts +36 -0
- package/src/process.ts +274 -0
- package/src/range.ts +16 -0
- package/src/request-site.ts +36 -0
- package/src/request-tracing.ts +9 -0
- package/src/routes.ts +261 -0
- package/src/server-info.ts +41 -0
- package/src/service-status.ts +30 -0
- package/src/workerd.ts +184 -0
- package/test/artifact.test.ts +224 -0
- package/test/auth.test.ts +24 -0
- package/test/automation.test.ts +315 -0
- package/test/client-mcp.test.ts +369 -0
- package/test/codemode-api.test.ts +88 -0
- package/test/config.test.ts +104 -0
- package/test/cors.test.ts +24 -0
- package/test/defect-response.test.ts +83 -0
- package/test/event-feed.test.ts +201 -0
- package/test/fetch-option.test.ts +104 -0
- package/test/fetch.test.ts +698 -0
- package/test/fixture/mcp-starts.cjs +20 -0
- package/test/fixture/routes.ts +37 -0
- package/test/fixture/server.ts +20 -0
- package/test/fixture/worktree-delegate/index.ts +13 -0
- package/test/fixture/worktree-plugin/index.ts +47 -0
- package/test/flag-channel.test.ts +126 -0
- package/test/fs.test.ts +439 -0
- package/test/generate.test.ts +132 -0
- package/test/handler-policy.test.ts +43 -0
- package/test/heap-dump.test.ts +137 -0
- package/test/integration-policy.test.ts +93 -0
- package/test/job.test.ts +186 -0
- package/test/machine-iroh.test.ts +259 -0
- package/test/machine-link.test.ts +1234 -0
- package/test/machine-routes.test.ts +198 -0
- package/test/media.test.ts +343 -0
- package/test/message.test.ts +105 -0
- package/test/model.test.ts +62 -0
- package/test/options.test.ts +42 -0
- package/test/permission-saved.test.ts +73 -0
- package/test/persistent-pty.test.ts +1007 -0
- package/test/process.test.ts +261 -0
- package/test/provider.test.ts +88 -0
- package/test/pty-socket.property.test.ts +240 -0
- package/test/pty-socket.test.ts +89 -0
- package/test/pty-transport.test.ts +200 -0
- package/test/push-routes.test.ts +157 -0
- package/test/request-site.test.ts +71 -0
- package/test/request-tracing.test.ts +87 -0
- package/test/rpc-handler-errors.test.ts +76 -0
- package/test/schema-error.test.ts +68 -0
- package/test/service-status.test.ts +34 -0
- package/test/session-admit-location.test.ts +92 -0
- package/test/session-create.test.ts +65 -0
- package/test/session-diff.test.ts +100 -0
- package/test/session-form.test.ts +50 -0
- package/test/session-import.test.ts +152 -0
- package/test/session-instances.test.ts +335 -0
- package/test/session-unarchive.test.ts +73 -0
- package/test/session-update.test.ts +66 -0
- package/test/skill.test.ts +57 -0
- package/test/sse-failure.test.ts +71 -0
- package/test/usage.test.ts +88 -0
- package/test/vcs.test.ts +356 -0
- package/test/video-generate.test.ts +131 -0
- package/test/voice.test.ts +170 -0
- package/test/workerd.test.ts +227 -0
- package/test/worktree.test.ts +333 -0
- package/tsconfig.build.json +15 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import fs from "node:fs/promises"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { expect } from "bun:test"
|
|
4
|
+
import { UsageLimits } from "@zyro-sec/core/usage-limits"
|
|
5
|
+
import { Money } from "@zyro-sec/schema/money"
|
|
6
|
+
import { makeLocationNode } from "@zyro-sec/util/effect/app-node"
|
|
7
|
+
import { Effect, Layer } from "effect"
|
|
8
|
+
import { tmpdir } from "../../core/test/fixture/tmpdir"
|
|
9
|
+
import { it } from "../../core/test/lib/effect"
|
|
10
|
+
import { ServerFetch } from "../src/fetch"
|
|
11
|
+
|
|
12
|
+
/** Requests a location's usage limits from a server whose UsageLimits answers with `get`. */
|
|
13
|
+
const request = (get: UsageLimits.Interface["get"]) =>
|
|
14
|
+
Effect.gen(function* () {
|
|
15
|
+
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-usage-endpoint-")))
|
|
16
|
+
const global = path.join(tmp.path, "global")
|
|
17
|
+
yield* Effect.promise(() => fs.mkdir(global))
|
|
18
|
+
const handler = yield* ServerFetch.make(
|
|
19
|
+
{
|
|
20
|
+
database: { path: ":memory:" },
|
|
21
|
+
config: { directory: global },
|
|
22
|
+
fs: { filewatcher: false },
|
|
23
|
+
models: { fetch: false },
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
overrides: [
|
|
27
|
+
UsageLimits.node.replace(
|
|
28
|
+
makeLocationNode({
|
|
29
|
+
service: UsageLimits.Service,
|
|
30
|
+
layer: Layer.succeed(UsageLimits.Service, UsageLimits.Service.of({ get })),
|
|
31
|
+
deps: [],
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
)
|
|
37
|
+
const url = new URL("/api/experimental/usage/limits", "http://opencode.local")
|
|
38
|
+
url.searchParams.set("location[directory]", tmp.path)
|
|
39
|
+
const response = yield* Effect.promise(() => handler(new Request(url)))
|
|
40
|
+
return { status: response.status, body: yield* Effect.promise(() => response.json()) }
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it.live("returns the usage limits of the OpenCode Console workspace a location is signed in to", () =>
|
|
44
|
+
Effect.gen(function* () {
|
|
45
|
+
const limits: UsageLimits.Info = {
|
|
46
|
+
workspace: { id: "wrk_anomaly", usageURL: "https://opencode.ai/console/wrk_anomaly/activity/overview" },
|
|
47
|
+
budgets: [
|
|
48
|
+
{
|
|
49
|
+
id: "rule_monthly",
|
|
50
|
+
limit: Money.USD.make(50),
|
|
51
|
+
spent: Money.USD.make(12.5),
|
|
52
|
+
scope: "each",
|
|
53
|
+
per: "set",
|
|
54
|
+
coverage: "all",
|
|
55
|
+
models: [],
|
|
56
|
+
vendors: [],
|
|
57
|
+
picked: false,
|
|
58
|
+
shared: false,
|
|
59
|
+
resetsAt: 1793491200000,
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
}
|
|
63
|
+
const result = yield* request(() => Effect.succeed(limits))
|
|
64
|
+
|
|
65
|
+
expect(result.status).toBe(200)
|
|
66
|
+
expect(result.body).toMatchObject({ data: limits })
|
|
67
|
+
}),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
it.live("returns null for a location that isn't signed in to OpenCode Console", () =>
|
|
71
|
+
Effect.gen(function* () {
|
|
72
|
+
const result = yield* request(() => Effect.succeed(undefined))
|
|
73
|
+
|
|
74
|
+
expect(result.status).toBe(200)
|
|
75
|
+
expect(result.body).toMatchObject({ data: null })
|
|
76
|
+
}),
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
it.live("reports usage limits Console can't provide as unavailable", () =>
|
|
80
|
+
Effect.gen(function* () {
|
|
81
|
+
const result = yield* request(() =>
|
|
82
|
+
Effect.fail(new UsageLimits.UnavailableError({ message: "OpenCode Console budgets are unavailable" })),
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
expect(result.status).toBe(503)
|
|
86
|
+
expect(result.body).toMatchObject({ message: "OpenCode Console budgets are unavailable", service: "opencode" })
|
|
87
|
+
}),
|
|
88
|
+
)
|
package/test/vcs.test.ts
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import path from "node:path"
|
|
2
|
+
import { $ } from "bun"
|
|
3
|
+
import { describe, expect } from "bun:test"
|
|
4
|
+
import { SdkPlugins } from "@zyro-sec/core/plugin/sdk"
|
|
5
|
+
import { Effect, Layer, Schedule } from "effect"
|
|
6
|
+
import { tmpdir } from "../../core/test/fixture/tmpdir"
|
|
7
|
+
import { it } from "../../core/test/lib/effect"
|
|
8
|
+
import { startServer } from "./fixture/server"
|
|
9
|
+
import { serveRoutes } from "./fixture/routes"
|
|
10
|
+
import { ServerFetch } from "../src/fetch"
|
|
11
|
+
|
|
12
|
+
it.live(
|
|
13
|
+
"initializes Git in a markerless project and refreshes its location",
|
|
14
|
+
() =>
|
|
15
|
+
Effect.gen(function* () {
|
|
16
|
+
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-vcs-init-")))
|
|
17
|
+
yield* Effect.promise(() => Bun.write(path.join(tmp.path, "hello.txt"), "hello\n"))
|
|
18
|
+
const server = yield* serveRoutes({
|
|
19
|
+
database: { path: ":memory:" },
|
|
20
|
+
config: { directory: path.join(tmp.path, "config") },
|
|
21
|
+
fs: { filewatcher: false },
|
|
22
|
+
models: { fetch: false },
|
|
23
|
+
})
|
|
24
|
+
yield* server.pluginsLoaded(tmp.path)
|
|
25
|
+
const url = new URL("/api/vcs/init", "http://opencode.local")
|
|
26
|
+
url.searchParams.set("location[directory]", tmp.path)
|
|
27
|
+
const before = yield* Effect.promise(() =>
|
|
28
|
+
server.fetch(new URL(`/api/location${url.search}`, "http://opencode.local")),
|
|
29
|
+
)
|
|
30
|
+
expect(before.status).toBe(200)
|
|
31
|
+
const original = yield* Effect.promise(() => before.json())
|
|
32
|
+
const initialized = yield* Effect.promise(() => server.fetch(url, { method: "POST" }))
|
|
33
|
+
expect(initialized.status).toBe(204)
|
|
34
|
+
expect(yield* Effect.promise(() => $`git -C ${tmp.path} rev-parse --is-inside-work-tree`.text())).toBe("true\n")
|
|
35
|
+
url.pathname = "/api/location"
|
|
36
|
+
const location = yield* Effect.promise(() => server.fetch(url))
|
|
37
|
+
expect(location.status).toBe(200)
|
|
38
|
+
const refreshed = yield* Effect.promise(() => location.json())
|
|
39
|
+
expect(refreshed).toMatchObject({ project: { directory: tmp.path } })
|
|
40
|
+
expect(refreshed.project.id).not.toBe(original.project.id)
|
|
41
|
+
const projects = yield* Effect.promise(() => server.fetch(new URL("/api/project", "http://opencode.local")))
|
|
42
|
+
expect(yield* Effect.promise(() => projects.json())).toContainEqual(
|
|
43
|
+
expect.objectContaining({ id: refreshed.project.id, vcs: "git" }),
|
|
44
|
+
)
|
|
45
|
+
url.pathname = "/api/vcs"
|
|
46
|
+
const vcs = yield* Effect.tryPromise({
|
|
47
|
+
try: async () => {
|
|
48
|
+
const response = await server.fetch(url)
|
|
49
|
+
const body: unknown = await response.json()
|
|
50
|
+
if (!isRecord(body) || !isRecord(body.data) || body.data.provider !== "git")
|
|
51
|
+
throw new Error("Git provider not ready")
|
|
52
|
+
return body
|
|
53
|
+
},
|
|
54
|
+
catch: (cause) => cause,
|
|
55
|
+
}).pipe(Effect.retry(Schedule.spaced("10 millis")), Effect.timeout("2 seconds"))
|
|
56
|
+
expect(vcs).toMatchObject({ data: { provider: "git" } })
|
|
57
|
+
url.pathname = "/api/vcs/diff"
|
|
58
|
+
url.searchParams.set("mode", "working")
|
|
59
|
+
const diff = yield* Effect.promise(() => server.fetch(url))
|
|
60
|
+
expect(diff.status).toBe(200)
|
|
61
|
+
expect(yield* Effect.promise(() => diff.json())).toMatchObject({
|
|
62
|
+
data: expect.arrayContaining([expect.objectContaining({ file: "hello.txt" })]),
|
|
63
|
+
})
|
|
64
|
+
url.searchParams.delete("mode")
|
|
65
|
+
url.pathname = "/api/vcs/init"
|
|
66
|
+
const repeated = yield* Effect.promise(() => server.fetch(url, { method: "POST" }))
|
|
67
|
+
expect(repeated.status).toBe(409)
|
|
68
|
+
}),
|
|
69
|
+
15_000,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
it.live(
|
|
73
|
+
"does not create a project directory while initializing Git",
|
|
74
|
+
() =>
|
|
75
|
+
Effect.gen(function* () {
|
|
76
|
+
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-vcs-missing-")))
|
|
77
|
+
const server = yield* startServer(path.join(tmp.path, "config"))
|
|
78
|
+
const directory = path.join(tmp.path, "absent")
|
|
79
|
+
const url = new URL("/api/vcs/init", server.base)
|
|
80
|
+
url.searchParams.set("location[directory]", directory)
|
|
81
|
+
const response = yield* Effect.promise(() => fetch(url, { method: "POST", headers: server.headers }))
|
|
82
|
+
expect(response.status).not.toBe(204)
|
|
83
|
+
expect(yield* Effect.promise(() => Bun.file(path.join(directory, ".git", "HEAD")).exists())).toBe(false)
|
|
84
|
+
}),
|
|
85
|
+
15_000,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
it.live(
|
|
89
|
+
"accepts an explicit Git provider and rejects unknown providers without initializing",
|
|
90
|
+
() =>
|
|
91
|
+
Effect.gen(function* () {
|
|
92
|
+
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-vcs-provider-")))
|
|
93
|
+
const server = yield* serveRoutes({
|
|
94
|
+
database: { path: ":memory:" },
|
|
95
|
+
config: { directory: path.join(tmp.path, "config") },
|
|
96
|
+
fs: { filewatcher: false },
|
|
97
|
+
models: { fetch: false },
|
|
98
|
+
})
|
|
99
|
+
yield* server.pluginsLoaded(tmp.path)
|
|
100
|
+
const url = new URL("/api/vcs/init", "http://opencode.local")
|
|
101
|
+
url.searchParams.set("location[directory]", tmp.path)
|
|
102
|
+
url.searchParams.set("provider", "unknown")
|
|
103
|
+
const unsupported = yield* Effect.promise(() => server.fetch(url, { method: "POST" }))
|
|
104
|
+
expect(unsupported.status).toBe(400)
|
|
105
|
+
expect(yield* Effect.promise(() => unsupported.json())).toMatchObject({
|
|
106
|
+
_tag: "InvalidRequestError",
|
|
107
|
+
field: "provider",
|
|
108
|
+
})
|
|
109
|
+
expect(yield* Effect.promise(() => Bun.file(path.join(tmp.path, ".git", "HEAD")).exists())).toBe(false)
|
|
110
|
+
expect(yield* Effect.promise(() => Bun.file(path.join(tmp.path, ".hg", "requires")).exists())).toBe(false)
|
|
111
|
+
url.searchParams.set("provider", "git")
|
|
112
|
+
const initialized = yield* Effect.promise(() => server.fetch(url, { method: "POST" }))
|
|
113
|
+
expect(initialized.status).toBe(204)
|
|
114
|
+
expect(yield* Effect.promise(() => Bun.file(path.join(tmp.path, ".git", "HEAD")).exists())).toBe(true)
|
|
115
|
+
}),
|
|
116
|
+
15_000,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
it.live("returns 501 when a registered VCS provider has no initializer", () =>
|
|
120
|
+
Effect.gen(function* () {
|
|
121
|
+
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-vcs-no-init-")))
|
|
122
|
+
const server = yield* serveRoutes(
|
|
123
|
+
{
|
|
124
|
+
database: { path: ":memory:" },
|
|
125
|
+
config: { directory: tmp.path },
|
|
126
|
+
fs: { filewatcher: false },
|
|
127
|
+
models: { fetch: false },
|
|
128
|
+
},
|
|
129
|
+
[
|
|
130
|
+
SdkPlugins.node.replace(
|
|
131
|
+
Layer.succeed(
|
|
132
|
+
SdkPlugins.Service,
|
|
133
|
+
SdkPlugins.Service.of({
|
|
134
|
+
register: () => Effect.void,
|
|
135
|
+
all: () => [
|
|
136
|
+
{
|
|
137
|
+
id: "read-only-vcs",
|
|
138
|
+
revision: "test",
|
|
139
|
+
effect: (ctx) =>
|
|
140
|
+
ctx.vcs
|
|
141
|
+
.transform((editor) => {
|
|
142
|
+
editor.add({
|
|
143
|
+
id: "read-only",
|
|
144
|
+
name: "Read-only VCS",
|
|
145
|
+
info: () => Effect.succeed({ branch: {} }),
|
|
146
|
+
branches: () => Effect.succeed([]),
|
|
147
|
+
status: () => Effect.succeed([]),
|
|
148
|
+
diff: () => Effect.succeed([]),
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
.pipe(Effect.asVoid),
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
}),
|
|
155
|
+
),
|
|
156
|
+
),
|
|
157
|
+
],
|
|
158
|
+
)
|
|
159
|
+
yield* server.pluginsLoaded(tmp.path)
|
|
160
|
+
const url = new URL("http://opencode.local/api/vcs/init")
|
|
161
|
+
url.searchParams.set("location[directory]", tmp.path)
|
|
162
|
+
url.searchParams.set("provider", "read-only")
|
|
163
|
+
const response = yield* Effect.promise(() => server.handler(new Request(url, { method: "POST" })))
|
|
164
|
+
expect(response.status).toBe(501)
|
|
165
|
+
expect(yield* Effect.promise(() => response.json())).toMatchObject({
|
|
166
|
+
_tag: "VcsInitNotSupportedError",
|
|
167
|
+
providerID: "read-only",
|
|
168
|
+
})
|
|
169
|
+
expect(yield* Effect.promise(() => Bun.file(path.join(tmp.path, ".git", "HEAD")).exists())).toBe(false)
|
|
170
|
+
expect(yield* Effect.promise(() => Bun.file(path.join(tmp.path, ".hg", "requires")).exists())).toBe(false)
|
|
171
|
+
}),
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
const describeHg = Bun.which("hg") ? describe : describe.skip
|
|
175
|
+
|
|
176
|
+
describeHg("Mercurial initialization", () => {
|
|
177
|
+
it.live(
|
|
178
|
+
"initializes and serves an untracked-file diff through the Hg provider",
|
|
179
|
+
() =>
|
|
180
|
+
Effect.gen(function* () {
|
|
181
|
+
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-vcs-hg-init-")))
|
|
182
|
+
yield* Effect.promise(() => Bun.write(path.join(tmp.path, "hello.txt"), "hello\n"))
|
|
183
|
+
const server = yield* serveRoutes({
|
|
184
|
+
database: { path: ":memory:" },
|
|
185
|
+
config: { directory: path.join(tmp.path, "config") },
|
|
186
|
+
fs: { filewatcher: false },
|
|
187
|
+
models: { fetch: false },
|
|
188
|
+
})
|
|
189
|
+
yield* server.pluginsLoaded(tmp.path)
|
|
190
|
+
const url = new URL("/api/vcs/init", "http://opencode.local")
|
|
191
|
+
url.searchParams.set("location[directory]", tmp.path)
|
|
192
|
+
url.searchParams.set("provider", "hg")
|
|
193
|
+
const initialized = yield* Effect.promise(() => server.fetch(url, { method: "POST" }))
|
|
194
|
+
expect(initialized.status).toBe(204)
|
|
195
|
+
expect(yield* Effect.promise(() => Bun.file(path.join(tmp.path, ".hg", "requires")).exists())).toBe(true)
|
|
196
|
+
url.pathname = "/api/vcs"
|
|
197
|
+
const info = yield* Effect.tryPromise({
|
|
198
|
+
try: async () => {
|
|
199
|
+
const response = await server.fetch(url)
|
|
200
|
+
const body: unknown = await response.json()
|
|
201
|
+
if (!isRecord(body) || !isRecord(body.data) || body.data.provider !== "hg")
|
|
202
|
+
throw new Error("Mercurial provider not ready")
|
|
203
|
+
return body
|
|
204
|
+
},
|
|
205
|
+
catch: (cause) => cause,
|
|
206
|
+
}).pipe(Effect.retry(Schedule.spaced("10 millis")), Effect.timeout("2 seconds"))
|
|
207
|
+
expect(info).toMatchObject({ data: { provider: "hg" } })
|
|
208
|
+
url.pathname = "/api/vcs/diff"
|
|
209
|
+
url.searchParams.set("mode", "working")
|
|
210
|
+
const diff = yield* Effect.promise(() => server.fetch(url))
|
|
211
|
+
expect(diff.status).toBe(200)
|
|
212
|
+
expect(yield* Effect.promise(() => diff.json())).toMatchObject({
|
|
213
|
+
data: expect.arrayContaining([expect.objectContaining({ file: "hello.txt" })]),
|
|
214
|
+
})
|
|
215
|
+
url.pathname = "/api/vcs/init"
|
|
216
|
+
url.searchParams.delete("mode")
|
|
217
|
+
url.searchParams.set("provider", "git")
|
|
218
|
+
const repeated = yield* Effect.promise(() => server.fetch(url, { method: "POST" }))
|
|
219
|
+
expect(repeated.status).toBe(409)
|
|
220
|
+
}),
|
|
221
|
+
15_000,
|
|
222
|
+
)
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it.live(
|
|
226
|
+
"serves lazy review bases, committed diffs, and unavailable-base errors",
|
|
227
|
+
() =>
|
|
228
|
+
Effect.gen(function* () {
|
|
229
|
+
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-vcs-endpoint-")))
|
|
230
|
+
yield* Effect.promise(async () => {
|
|
231
|
+
await $`git init -b main`.cwd(tmp.path).quiet()
|
|
232
|
+
await $`git config commit.gpgsign false`.cwd(tmp.path).quiet()
|
|
233
|
+
await $`git config user.email test@opencode.test`.cwd(tmp.path).quiet()
|
|
234
|
+
await $`git config user.name Test`.cwd(tmp.path).quiet()
|
|
235
|
+
await Bun.write(path.join(tmp.path, "file.txt"), "base\n")
|
|
236
|
+
await $`git add .`.cwd(tmp.path).quiet()
|
|
237
|
+
await $`git commit -m initial`.cwd(tmp.path).quiet()
|
|
238
|
+
await $`git checkout -b feature main`.cwd(tmp.path).quiet()
|
|
239
|
+
await Bun.write(path.join(tmp.path, "file.txt"), "committed\n")
|
|
240
|
+
await $`git commit -am feature`.cwd(tmp.path).quiet()
|
|
241
|
+
await Bun.write(path.join(tmp.path, "file.txt"), "dirty\n")
|
|
242
|
+
})
|
|
243
|
+
const server = yield* startServer(path.join(tmp.path, "config"))
|
|
244
|
+
const url = new URL("/api/vcs/base", server.base)
|
|
245
|
+
url.searchParams.set("location[directory]", tmp.path)
|
|
246
|
+
const base = yield* Effect.tryPromise({
|
|
247
|
+
try: async () => {
|
|
248
|
+
const response = await fetch(url, { headers: server.headers })
|
|
249
|
+
const body: unknown = await response.json()
|
|
250
|
+
if (!isRecord(body) || !isRecord(body.data)) throw new Error("VCS provider not ready")
|
|
251
|
+
return body
|
|
252
|
+
},
|
|
253
|
+
catch: (cause) => cause,
|
|
254
|
+
}).pipe(Effect.retry(Schedule.spaced("10 millis")), Effect.timeout("2 seconds"))
|
|
255
|
+
expect(base).toMatchObject({
|
|
256
|
+
data: { name: "main", ref: "refs/heads/main", source: "reflog" },
|
|
257
|
+
})
|
|
258
|
+
yield* Effect.promise(() => $`git branch -m ambiguous`.cwd(tmp.path).quiet())
|
|
259
|
+
const ambiguous = yield* Effect.promise(() => fetch(url, { headers: server.headers }))
|
|
260
|
+
expect(ambiguous.status).toBe(503)
|
|
261
|
+
expect(yield* Effect.promise(() => ambiguous.json())).toMatchObject({
|
|
262
|
+
_tag: "ServiceUnavailableError",
|
|
263
|
+
message: "Choose a review base",
|
|
264
|
+
})
|
|
265
|
+
url.pathname = "/api/vcs/diff"
|
|
266
|
+
url.searchParams.set("mode", "committed")
|
|
267
|
+
url.searchParams.set("base", "refs/heads/main")
|
|
268
|
+
const diff = yield* Effect.promise(() => fetch(url, { headers: server.headers }))
|
|
269
|
+
expect(diff.status).toBe(200)
|
|
270
|
+
expect(yield* Effect.promise(() => diff.json())).toMatchObject({
|
|
271
|
+
data: [{ file: "file.txt", patch: expect.stringContaining("-base\n+committed"), additions: 1, deletions: 1 }],
|
|
272
|
+
})
|
|
273
|
+
url.searchParams.set("base", "missing")
|
|
274
|
+
const unavailable = yield* Effect.promise(() => fetch(url, { headers: server.headers }))
|
|
275
|
+
expect(unavailable.status).toBe(503)
|
|
276
|
+
expect(yield* Effect.promise(() => unavailable.json())).toMatchObject({
|
|
277
|
+
_tag: "ServiceUnavailableError",
|
|
278
|
+
service: "vcs",
|
|
279
|
+
})
|
|
280
|
+
yield* Effect.promise(() => $`git branch -D main`.cwd(tmp.path).quiet())
|
|
281
|
+
url.searchParams.delete("base")
|
|
282
|
+
url.searchParams.set("mode", "branch")
|
|
283
|
+
const noBase = yield* Effect.promise(() => fetch(url, { headers: server.headers }))
|
|
284
|
+
expect(noBase.status).toBe(503)
|
|
285
|
+
expect(yield* Effect.promise(() => noBase.json())).toMatchObject({
|
|
286
|
+
_tag: "ServiceUnavailableError",
|
|
287
|
+
service: "vcs",
|
|
288
|
+
message: "No review base available",
|
|
289
|
+
})
|
|
290
|
+
}),
|
|
291
|
+
20_000,
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
it.live("maps a failing base provider to HTTP 503 instead of null metadata", () =>
|
|
295
|
+
Effect.gen(function* () {
|
|
296
|
+
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-vcs-failure-")))
|
|
297
|
+
const handler = yield* ServerFetch.make(
|
|
298
|
+
{
|
|
299
|
+
database: { path: ":memory:" },
|
|
300
|
+
config: { directory: tmp.path },
|
|
301
|
+
fs: { filewatcher: false },
|
|
302
|
+
models: { fetch: false },
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
overrides: [
|
|
306
|
+
SdkPlugins.node.replace(
|
|
307
|
+
Layer.succeed(
|
|
308
|
+
SdkPlugins.Service,
|
|
309
|
+
SdkPlugins.Service.of({
|
|
310
|
+
register: () => Effect.void,
|
|
311
|
+
all: () => [
|
|
312
|
+
{
|
|
313
|
+
id: "failing-vcs",
|
|
314
|
+
revision: "test",
|
|
315
|
+
effect: (ctx) =>
|
|
316
|
+
ctx.vcs
|
|
317
|
+
.transform((editor) => {
|
|
318
|
+
editor.add({
|
|
319
|
+
id: "failing",
|
|
320
|
+
name: "Failing VCS",
|
|
321
|
+
info: () => Effect.succeed({ branch: {} }),
|
|
322
|
+
branches: () => Effect.succeed([]),
|
|
323
|
+
status: () => Effect.succeed([]),
|
|
324
|
+
diff: () => Effect.succeed([]),
|
|
325
|
+
base: () => Effect.fail(new Error("provider failure")),
|
|
326
|
+
})
|
|
327
|
+
editor.default.set("failing")
|
|
328
|
+
})
|
|
329
|
+
.pipe(Effect.asVoid),
|
|
330
|
+
},
|
|
331
|
+
],
|
|
332
|
+
}),
|
|
333
|
+
),
|
|
334
|
+
),
|
|
335
|
+
],
|
|
336
|
+
},
|
|
337
|
+
)
|
|
338
|
+
const url = new URL("http://opencode.local/api/vcs/base")
|
|
339
|
+
url.searchParams.set("location[directory]", tmp.path)
|
|
340
|
+
const response = yield* Effect.promise(() => handler(new Request(url))).pipe(
|
|
341
|
+
Effect.filterOrFail((response) => response.status === 503),
|
|
342
|
+
Effect.retry(Schedule.spaced("10 millis")),
|
|
343
|
+
Effect.timeout("2 seconds"),
|
|
344
|
+
)
|
|
345
|
+
expect(response.status).toBe(503)
|
|
346
|
+
expect(yield* Effect.promise(() => response.json())).toMatchObject({
|
|
347
|
+
_tag: "ServiceUnavailableError",
|
|
348
|
+
service: "vcs",
|
|
349
|
+
message: "VCS provider could not resolve a review base",
|
|
350
|
+
})
|
|
351
|
+
}),
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
355
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
356
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import fs from "node:fs/promises"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { expect } from "bun:test"
|
|
4
|
+
import { Generate } from "@zyro-sec/core/generate"
|
|
5
|
+
import { makeLocationNode } from "@zyro-sec/util/effect/app-node"
|
|
6
|
+
import { Global } from "@zyro-sec/util/global"
|
|
7
|
+
import { Effect, Layer, Stream } from "effect"
|
|
8
|
+
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
9
|
+
import { it } from "../../core/test/lib/effect"
|
|
10
|
+
import { eventually } from "../../core/test/lib/eventually"
|
|
11
|
+
import { ServerFetch } from "../src/fetch"
|
|
12
|
+
|
|
13
|
+
// An MP4 `ftyp` box, so the type is read from the bytes: "0123456789" are the first ten bytes.
|
|
14
|
+
const video = Uint8Array.from([0, 0, 0, 0x14, ...new TextEncoder().encode("ftypmp42"), 0, 0, 0, 0, 1, 2, 3, 4])
|
|
15
|
+
|
|
16
|
+
// A provider whose jobs finish on the first status read.
|
|
17
|
+
const generate = makeLocationNode({
|
|
18
|
+
service: Generate.Service,
|
|
19
|
+
layer: Layer.succeed(
|
|
20
|
+
Generate.Service,
|
|
21
|
+
Generate.Service.of({
|
|
22
|
+
text: () => Effect.die("unused"),
|
|
23
|
+
image: () => Effect.die("unused"),
|
|
24
|
+
decisions: () => Effect.die("unused"),
|
|
25
|
+
video: {
|
|
26
|
+
start: () => Effect.succeed({ id: "provider_1" }),
|
|
27
|
+
status: () => Effect.succeed({ status: "completed" }),
|
|
28
|
+
result: () => Effect.succeed([{ mime: "video/mp4", stream: Stream.make(video) }]),
|
|
29
|
+
cancel: () => Effect.void,
|
|
30
|
+
},
|
|
31
|
+
}),
|
|
32
|
+
),
|
|
33
|
+
deps: [],
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const serve = (dir: string) =>
|
|
37
|
+
ServerFetch.make(
|
|
38
|
+
{
|
|
39
|
+
app: { version: "test" },
|
|
40
|
+
database: { path: path.join(dir, "opencode.db") },
|
|
41
|
+
config: {
|
|
42
|
+
directory: dir,
|
|
43
|
+
project: false,
|
|
44
|
+
},
|
|
45
|
+
models: { fetch: false },
|
|
46
|
+
fs: { filewatcher: false },
|
|
47
|
+
},
|
|
48
|
+
// Blobs go under the test's own data directory.
|
|
49
|
+
{
|
|
50
|
+
overrides: [
|
|
51
|
+
Global.node.replace(Global.layerWith({ data: path.join(dir, "data"), config: dir })),
|
|
52
|
+
Generate.node.replace(generate),
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
const base = "http://opencode.local/api/experimental"
|
|
58
|
+
const unknown = `video_${"0".repeat(12)}${"A".repeat(14)}`
|
|
59
|
+
|
|
60
|
+
it.live("generates a video in the background and serves its blob with Range and ETag support", () =>
|
|
61
|
+
Effect.gen(function* () {
|
|
62
|
+
const dir = (yield* tmpdirScoped("opencode-video-route-")).path
|
|
63
|
+
const handler = yield* serve(dir)
|
|
64
|
+
const call = (url: string, init?: RequestInit) => Effect.promise(() => handler(new Request(url, init)))
|
|
65
|
+
|
|
66
|
+
const started = yield* call(`${base}/generate/video`, {
|
|
67
|
+
method: "POST",
|
|
68
|
+
headers: { "content-type": "application/json" },
|
|
69
|
+
body: JSON.stringify({ model: { providerID: "test", id: "video" }, prompt: "a cat" }),
|
|
70
|
+
})
|
|
71
|
+
expect(started.status).toBe(200)
|
|
72
|
+
const { data } = (yield* Effect.promise(() => started.json())) as { data: { id: string; status: string } }
|
|
73
|
+
expect(data).toMatchObject({ status: "running" })
|
|
74
|
+
expect(data.id.startsWith("video_")).toBe(true)
|
|
75
|
+
|
|
76
|
+
const completed = yield* eventually(
|
|
77
|
+
call(`${base}/generate/video/${data.id}`).pipe(
|
|
78
|
+
Effect.flatMap((response) => Effect.promise(() => response.json())),
|
|
79
|
+
),
|
|
80
|
+
(body: { data: Record<string, unknown> }) => body.data.status !== "running",
|
|
81
|
+
)
|
|
82
|
+
expect(completed.data).toEqual({
|
|
83
|
+
id: data.id,
|
|
84
|
+
status: "completed",
|
|
85
|
+
blobs: [{ id: expect.stringMatching(/^blob_/), mime: "video/mp4", bytes: 20 }],
|
|
86
|
+
})
|
|
87
|
+
const blob = (completed.data.blobs as { id: string }[])[0].id
|
|
88
|
+
const route = `${base}/blob/${blob}`
|
|
89
|
+
|
|
90
|
+
const full = yield* call(route)
|
|
91
|
+
expect(full.status).toBe(200)
|
|
92
|
+
expect(full.headers.get("content-type")).toBe("video/mp4")
|
|
93
|
+
expect(full.headers.get("accept-ranges")).toBe("bytes")
|
|
94
|
+
expect(full.headers.get("etag")).toBe(`"${blob}"`)
|
|
95
|
+
expect(full.headers.get("content-disposition")).toBe("attachment")
|
|
96
|
+
expect(new Uint8Array(yield* Effect.promise(() => full.arrayBuffer()))).toEqual(video)
|
|
97
|
+
|
|
98
|
+
const slice = yield* call(route, { headers: { range: "bytes=2-5" } })
|
|
99
|
+
expect(slice.status).toBe(206)
|
|
100
|
+
expect(slice.headers.get("content-range")).toBe("bytes 2-5/20")
|
|
101
|
+
expect(new Uint8Array(yield* Effect.promise(() => slice.arrayBuffer()))).toEqual(video.slice(2, 6))
|
|
102
|
+
const suffix = yield* call(route, { headers: { range: "bytes=-3" } })
|
|
103
|
+
expect(new Uint8Array(yield* Effect.promise(() => suffix.arrayBuffer()))).toEqual(video.slice(17))
|
|
104
|
+
const unsatisfiable = yield* call(route, { headers: { range: "bytes=20-" } })
|
|
105
|
+
expect(unsatisfiable.status).toBe(416)
|
|
106
|
+
expect(unsatisfiable.headers.get("content-range")).toBe("bytes */20")
|
|
107
|
+
// A Range for another version of the bytes gets the whole body.
|
|
108
|
+
const stale = yield* call(route, { headers: { range: "bytes=2-5", "if-range": '"other"' } })
|
|
109
|
+
expect(stale.status).toBe(200)
|
|
110
|
+
|
|
111
|
+
const cached = yield* call(route, { headers: { "if-none-match": `"${blob}"` } })
|
|
112
|
+
expect(cached.status).toBe(304)
|
|
113
|
+
expect((yield* Effect.promise(() => cached.arrayBuffer())).byteLength).toBe(0)
|
|
114
|
+
|
|
115
|
+
// A restarted server reads the finished job back from its record.
|
|
116
|
+
const restarted = yield* serve(dir)
|
|
117
|
+
const reread = yield* Effect.promise(() => restarted(new Request(`${base}/generate/video/${data.id}`)))
|
|
118
|
+
expect(((yield* Effect.promise(() => reread.json())) as { data: unknown }).data).toEqual(completed.data)
|
|
119
|
+
|
|
120
|
+
// Once its file is gone, the blob and the job are gone.
|
|
121
|
+
yield* Effect.promise(() => fs.rm(path.join(dir, "data", "blobs", blob)))
|
|
122
|
+
expect((yield* call(route)).status).toBe(404)
|
|
123
|
+
expect((yield* call(`${base}/generate/video/${data.id}`)).status).toBe(404)
|
|
124
|
+
|
|
125
|
+
expect((yield* call(`${base}/generate/video/${unknown}`)).status).toBe(404)
|
|
126
|
+
// An ID that isn't one never reaches the disk.
|
|
127
|
+
expect((yield* call(`${base}/blob/blob_..%2F..%2Fsecret`)).status).toBe(400)
|
|
128
|
+
}).pipe(Effect.scoped),
|
|
129
|
+
// The first status check runs at the next 5-second check.
|
|
130
|
+
15_000,
|
|
131
|
+
)
|