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/plugin/test/host.test.ts at dev · erickwill/opencode Expand file tree/ Copy pathhost.test.tsMore file actions 260 lines (244 loc) · 8.84 KB / Copy pathhost.test.tsFile metadata and controls260 lines (244 loc) · 8.84 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 import assert from "node:assert/strict" import { mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises" import { tmpdir } from "node:os" import path from "node:path" import { describe, it } from "node:test" import { pathToFileURL } from "node:url" import { Host } from "../src/host.js" // Every entrypoint throws if evaluated: resolution must never execute plugins. const source = 'throw new Error("Plugin code must not run during resolution")' const name = "@fixture/plugin" async function fixture(files: Record<string, string>, installed = false) { const root = await realpath(await mkdtemp(path.join(tmpdir(), "opencode-host-"))) const directory = installed ? path.join(root, "node_modules", name) : root await Promise.all( Object.entries(files).map(async ([file, content]) => { await mkdir(path.dirname(path.join(directory, file)), { recursive: true }) await writeFile(path.join(directory, file), content) }), ) return { target: { directory, ...(installed ? { name } : {}) }, url: (file: string) => pathToFileURL(path.join(directory, file)).href, [Symbol.asyncDispose]: () => rm(root, { recursive: true, force: true }), } } describe("Host.resolve", () => { it("resolves conventional entrypoints without package.json", async () => { await using plugin = await fixture({ "index.ts": source, "tui.tsx": source, "rpc.ts": source }) assert.deepEqual(Host.resolve(plugin.target), { server: plugin.url("index.ts"), tui: plugin.url("tui.tsx"), rpc: plugin.url("rpc.ts"), gui: undefined, }) }) it("resolves a TUI-only directory without an index or package.json", async () => { await using plugin = await fixture({ "tui.tsx": source }) assert.deepEqual(Host.resolve(plugin.target), { server: undefined, tui: plugin.url("tui.tsx"), rpc: undefined, gui: undefined, }) }) for (const main of [undefined, "lib/backend.js"]) { it(`resolves packages without exports using ${main ?? "the default index"}`, async () => { await using plugin = await fixture( { "package.json": JSON.stringify({ name, main }), [main ?? "index.js"]: source, "tui.js": source, "rpc.js": source, }, true, ) assert.deepEqual(Host.resolve(plugin.target), { server: plugin.url(main ?? "index.js"), tui: plugin.url("tui.js"), rpc: plugin.url("rpc.js"), gui: undefined, }) }) } it("resolves local conventional entrypoints with package.json but no exports", async () => { await using plugin = await fixture({ "package.json": JSON.stringify({ name, type: "module" }), "index.js": source, "tui.js": source, "rpc.js": source, }) assert.deepEqual(Host.resolve(plugin.target), { server: plugin.url("index.js"), tui: plugin.url("tui.js"), rpc: plugin.url("rpc.js"), gui: undefined, }) }) it("honors exports and prefers the explicit server entrypoint over the root", async () => { await using plugin = await fixture( { "package.json": JSON.stringify({ name, exports: { ".": "./dist/root.js", "./server": "./dist/backend.js", "./tui": "./dist/terminal.js", "./rpc": "./dist/contract.js", }, }), "dist/root.js": source, "dist/backend.js": source, "dist/terminal.js": source, "dist/contract.js": source, }, true, ) assert.deepEqual(Host.resolve(plugin.target), { server: plugin.url("dist/backend.js"), tui: plugin.url("dist/terminal.js"), rpc: plugin.url("dist/contract.js"), gui: undefined, }) }) it("falls back to the root export and uses import rather than require conditions", async () => { await using plugin = await fixture( { "package.json": JSON.stringify({ name, exports: { ".": { import: "./dist/server.mjs", require: "./dist/server.cjs" }, "./tui": { import: "./dist/tui.mjs", require: "./dist/tui.cjs" }, }, }), "dist/server.mjs": source, "dist/server.cjs": source, "dist/tui.mjs": source, "dist/tui.cjs": source, }, true, ) assert.deepEqual(Host.resolve(plugin.target), { server: plugin.url("dist/server.mjs"), tui: plugin.url("dist/tui.mjs"), rpc: undefined, gui: undefined, }) }) it("supports TUI-only exports without falling back to unexported files", async () => { await using plugin = await fixture( { "package.json": JSON.stringify({ name, exports: { "./tui": "./dist/terminal.js" } }), "dist/terminal.js": source, "index.js": source, "server.js": source, "rpc.js": source, }, true, ) assert.deepEqual(Host.resolve(plugin.target), { server: undefined, tui: plugin.url("dist/terminal.js"), rpc: undefined, gui: undefined, }) }) it("resolves the GUI entrypoint from exports beside the server and TUI entrypoints", async () => { await using plugin = await fixture( { "package.json": JSON.stringify({ name, exports: { ".": "./dist/server.js", "./tui": "./dist/tui.js", "./gui-experimental": "./dist/gui-experimental.cjs", }, }), "dist/server.js": source, "dist/tui.js": source, "dist/gui-experimental.cjs": source, }, true, ) assert.deepEqual(Host.resolve(plugin.target), { server: plugin.url("dist/server.js"), tui: plugin.url("dist/tui.js"), rpc: undefined, gui: plugin.url("dist/gui-experimental.cjs"), }) }) it("resolves a local directory through its package.json exports, as the published package", async () => { await using plugin = await fixture({ "package.json": JSON.stringify({ name: "opencode-acme-plugin", exports: { ".": "./src/index.ts", "./gui-experimental": "./dist/gui-experimental.cjs" }, }), "src/index.ts": source, "dist/gui-experimental.cjs": source, // Conventional root files are not part of the exports, so they do not load. "tui.tsx": source, "gui-experimental.cjs": source, }) assert.deepEqual(Host.resolve(plugin.target), { server: plugin.url("src/index.ts"), tui: undefined, rpc: undefined, gui: plugin.url("dist/gui-experimental.cjs"), }) }) it("names a plugin without a server part after its package, or its local directory", async () => { await using named = await fixture({ "package.json": JSON.stringify({ name: "@acme/opencode-gui" }) }) await using unnamed = await fixture({ "tui.tsx": source }) await using published = await fixture({ "package.json": JSON.stringify({ name }) }, true) assert.equal(Host.packageID(named.target), "@acme/opencode-gui") assert.equal(Host.packageID(unnamed.target), path.basename(unnamed.target.directory)) assert.equal(Host.packageID(published.target), name) }) it("treats a package.json that does not parse as missing, so resolution falls back to root files", async () => { await using plugin = await fixture({ "package.json": '{ "name": "bad", ', "index.ts": source }) assert.deepEqual(Host.resolve(plugin.target), { server: plugin.url("index.ts"), tui: undefined, rpc: undefined, gui: undefined, }) assert.equal(Host.packageID(plugin.target), path.basename(plugin.target.directory)) }) it("resolves the conventional GUI entrypoint in a local directory", async () => { await using plugin = await fixture({ "index.ts": source, "gui-experimental.js": source }) assert.deepEqual(Host.resolve(plugin.target), { server: plugin.url("index.ts"), tui: undefined, rpc: undefined, gui: plugin.url("gui-experimental.js"), }) }) it("does not treat TypeScript or ES module GUI sources as the CommonJS bundle", async () => { await using local = await fixture({ "index.ts": source, "gui.tsx": source }) await using published = await fixture( { "package.json": JSON.stringify({ name, exports: { ".": "./dist/server.js", "./gui-experimental": "./dist/gui-experimental.mjs" }, }), "dist/server.js": source, "dist/gui-experimental.mjs": source, }, true, ) assert.equal(Host.resolve(local.target).gui, undefined) assert.equal(Host.resolve(published.target).gui, undefined) }) it("does not return an exported entrypoint whose file is missing", async () => { await using plugin = await fixture( { "package.json": JSON.stringify({ name, exports: { "./tui": "./missing.js" } }) }, true, ) assert.deepEqual(Host.resolve(plugin.target), { server: undefined, tui: undefined, rpc: undefined, gui: undefined, }) }) }) |
| 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. |