GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — opencode/packages/cli/script/service-smoke.ts at dev · jonBoone/opencode Expand file tree/ Copy pathservice-smoke.tsMore file actions 190 lines (175 loc) · 8.59 KB / Copy pathservice-smoke.tsFile metadata and controls190 lines (175 loc) · 8.59 KB 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 #!/usr/bin/env bun import { NodeFileSystem } from "@effect/platform-node" import { Service } from "@opencode/client/effect/service" import { ServerInfo } from "@opencode/protocol/groups/server" import { Effect, Schema } from "effect" import fs from "node:fs/promises" import os from "node:os" import path from "node:path" const nodeBuild = process.argv.includes("--node") const target = `cli${nodeBuild ? "-node" : ""}-${process.platform === "win32" ? "windows" : process.platform}-${process.arch}` const directory = path.join(import.meta.dir, "..", "dist", ...(nodeBuild ? ["node"] : []), target, "bin") const binary = path.join( directory, `${nodeBuild ? "opencode2-node" : "opencode"}${process.platform === "win32" ? ".exe" : ""}`, ) if (!(await Bun.file(binary).exists())) throw new Error(`Missing compiled CLI in ${directory}`) const root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), "opencode-service-smoke-"))) const env = { ...process.env, HOME: root, USERPROFILE: root, OPENCODE_DB: path.join(root, "opencode.db"), OPENCODE_TEST_HOME: root, XDG_CACHE_HOME: path.join(root, "cache"), XDG_CONFIG_HOME: path.join(root, "config"), XDG_DATA_HOME: path.join(root, "data"), XDG_STATE_HOME: path.join(root, "state"), } const processes: Array<ReturnType<typeof Bun.spawn>> = [] const errors: Array<Promise<string>> = [] let failure: unknown try { await fs.mkdir(path.join(root, ".opencode", "plugins"), { recursive: true }) spawnService() spawnService() const registration = await waitForRegistration() const info = await Schema.decodeUnknownPromise(Service.Info)(await Bun.file(registration).json()) if (info.id === undefined || info.password === undefined) throw new Error("Registration is missing service identity") const credential = btoa(`opencode:${info.password}`) const headers = { authorization: "Basic " + credential } const token = encodeURIComponent(credential) const serverInfo = await waitForReady(info.url, headers) if (serverInfo.pid !== info.pid) throw new Error("Server info does not match registration") const tokenInfo = await fetch(new URL(`/api/info?auth_token=${token}`, info.url), { signal: AbortSignal.timeout(5_000), }) if (tokenInfo.status !== 200) throw new Error("Compiled service rejected query authentication") const tokenOpenApi = await fetch(new URL(`/openapi.json?auth_token=${token}`, info.url), { signal: AbortSignal.timeout(5_000), }) if (tokenOpenApi.status !== 200) throw new Error("Compiled application rejected query authentication") await verifyWebUi(info.url) if ((await pluginIDs(info.url, headers)).includes("smoke")) throw new Error("Smoke plugin existed before creation") const plugin = path.join(root, ".opencode", "plugins", "smoke.ts") await fs.writeFile(plugin, pluginSource()) await waitForPlugin(info.url, headers, plugin) const unauthorizedInfo = await fetch(new URL("/api/info", info.url), { signal: AbortSignal.timeout(5_000), }) if (unauthorizedInfo.status !== 401) throw new Error("Compiled service exposed info without authentication") const unauthorizedOpenApi = await fetch(new URL("/openapi.json", info.url), { signal: AbortSignal.timeout(5_000), }) if (unauthorizedOpenApi.status !== 401) throw new Error("Compiled service exposed application routes without authentication") const stopRoute = await fetch(new URL("/api/service/stop", info.url), { method: "POST", headers: { ...headers, "content-type": "application/json" }, body: JSON.stringify({ instanceID: info.id }), signal: AbortSignal.timeout(5_000), }) if (stopRoute.status !== 404) throw new Error("Compiled service exposed the removed HTTP stop route") const winner = processes.find((process) => process.pid === info.pid) const loser = processes.find((process) => process.pid !== info.pid) if (!winner || !loser) throw new Error("Compiled contenders did not elect one registered owner") if (!(await exitsWithin(loser, 10_000))) throw new Error("Losing compiled contender did not exit") await Effect.runPromise(Service.stop({ file: registration }).pipe(Effect.provide(NodeFileSystem.layer))) if (!(await exitsWithin(winner, 10_000))) throw new Error("Compiled service did not stop") for (let attempt = 0; attempt < 200 && (await Bun.file(registration).exists()); attempt++) await Bun.sleep(25) if (await Bun.file(registration).exists()) throw new Error("Compiled service registration was not removed") } catch (cause) { failure = cause } finally { processes.forEach((process) => process.kill()) await Promise.all(processes.map((process) => process.exited)) if (failure) errors.push(fs.readFile(path.join(root, "data", "opencode", "log", "opencode.log"), "utf8").catch(() => "")) } const output = await Promise.all(errors) // Windows can retain directory handles briefly after the service processes exit. await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }).catch((cause: unknown) => { console.error("Failed to remove service smoke-test directory", cause) failure ??= cause }) if (failure) throw new Error(output.filter(Boolean).join("\n") || "Compiled service lifecycle smoke test failed", { cause: failure, }) function spawnService() { const process = Bun.spawn([binary, "serve", "--service"], { env, stdout: "ignore", stderr: "pipe" }) processes.push(process) errors.push(new Response(process.stderr).text()) return process } async function waitForRegistration() { const directory = path.join(root, "state", "opencode") for (let attempt = 0; attempt < 400; attempt++) { const files = await fs.readdir(directory).catch(() => []) const file = files.find( (file) => file === "service.json" || (file.startsWith("service-") && file.endsWith(".json")), ) if (file) return path.join(directory, file) await Bun.sleep(25) } throw new Error("Compiled service did not publish registration") } // Reads the web UI from the binary's embedded archive: the shell, and the entry script it names. async function verifyWebUi(url: string) { const shell = await fetch(url, { signal: AbortSignal.timeout(5_000) }) const html = await shell.text() if (shell.status !== 200 || !html.includes("<html")) throw new Error("Compiled service did not serve the web UI") const script = html.match(/<script[^>]*\bsrc="(\/_assets\/[^"]+\.js)"/)?.[1] if (!script) throw new Error("Compiled web UI names no entry script") const entry = await fetch(new URL(script, url), { signal: AbortSignal.timeout(5_000) }) if (entry.status !== 200 || (await entry.text()).length === 0) throw new Error(`Compiled service did not serve ${script}`) } async function waitForReady(url: string, headers: HeadersInit) { const deadline = Date.now() + 20_000 while (Date.now() < deadline) { const response = await fetch(new URL("/api/info", url), { headers, signal: AbortSignal.timeout(1_000), }).catch(() => undefined) if (response?.ok) return Schema.decodeUnknownPromise(ServerInfo)(await response.json()) await Bun.sleep(25) } throw new Error("Compiled service did not become ready") } function exitsWithin(process: Bun.Subprocess, milliseconds: number) { return new Promise<boolean>((resolve) => { const timeout = setTimeout(() => resolve(false), milliseconds) process.exited.then(() => { clearTimeout(timeout) resolve(true) }) }) } function pluginSource() { return 'export default { id: "smoke", setup: async () => {} }\n' } async function pluginIDs(url: string, headers: HeadersInit) { const endpoint = new URL("/api/plugin", url) endpoint.searchParams.set("location[directory]", root) const response = await fetch(endpoint, { headers, signal: AbortSignal.timeout(5_000) }) const body: unknown = await response.json() if (typeof body !== "object" || body === null || !("data" in body) || !Array.isArray(body.data)) { throw new Error("Compiled service returned an invalid plugin list") } return body.data.flatMap((plugin) => typeof plugin === "object" && plugin !== null && "id" in plugin && typeof plugin.id === "string" ? [plugin.id] : [], ) } async function waitForPlugin(url: string, headers: HeadersInit, plugin: string) { const deadline = Date.now() + 10_000 let attempt = 0 while (Date.now() < deadline) { if ((await pluginIDs(url, headers)).includes("smoke")) return await Bun.sleep(25) // Native watchers may coalesce a single creation edge. Keep changing valid source so // the smoke proves that a later native event is delivered. if (++attempt % 10 === 0) await fs.writeFile(plugin, `${pluginSource()}// watcher retry ${attempt}\n`) } throw new Error("Compiled service did not discover the created plugin") } |
| 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. |