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/opencode/test/plugin/install.test.ts at dev · sashankh/opencode Expand file tree/ Copy pathinstall.test.tsMore file actions 570 lines (506 loc) · 17.2 KB / Copy pathinstall.test.tsFile metadata and controls570 lines (506 loc) · 17.2 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 import { describe, expect, test } from "bun:test" import fs from "fs/promises" import path from "path" import { parse as parseJsonc } from "jsonc-parser" import { Filesystem } from "@/util/filesystem" import { createPlugTask, type PlugCtx, type PlugDeps } from "../../src/cli/cmd/plug" import { tmpdir } from "../fixture/fixture" function deps(global: string, target: string | Error): PlugDeps { return { spinner: () => ({ start() {}, stop() {}, }), log: { error() {}, info() {}, success() {}, }, resolve: async () => { if (target instanceof Error) throw target return target }, readText: (file) => Filesystem.readText(file), write: async (file, text) => { await Filesystem.write(file, text) }, exists: (file) => Filesystem.exists(file), files: (dir, name) => [path.join(dir, `${name}.jsonc`), path.join(dir, `${name}.json`)], global, } } function ctx(dir: string): PlugCtx { return { vcs: "git", worktree: dir, directory: dir, } } function ctxDir(dir: string, worktree: string): PlugCtx { return { vcs: "none", worktree, directory: dir, } } function ctxRoot(dir: string): PlugCtx { return { vcs: "git", worktree: "/", directory: dir, } } async function plugin( dir: string, kinds?: Array<"server" | "tui">, opts?: { server?: Record<string, unknown> tui?: Record<string, unknown> }, themes?: string[], ) { const p = path.join(dir, "plugin") const server = kinds?.includes("server") ?? false const tui = kinds?.includes("tui") ?? false const exports: Record<string, unknown> = {} if (server) { exports["./server"] = opts?.server ? { import: "./server.js", config: opts.server, } : "./server.js" } if (tui) { exports["./tui"] = opts?.tui ? { import: "./tui.js", config: opts.tui, } : "./tui.js" } await fs.mkdir(p, { recursive: true }) await Bun.write( path.join(p, "package.json"), JSON.stringify( { name: "acme", version: "1.0.0", ...(server ? { main: "./server.js" } : {}), ...(Object.keys(exports).length ? { exports } : {}), ...(themes?.length ? { "oc-themes": themes } : {}), }, null, 2, ), ) return p } async function read(file: string) { return Filesystem.readJson<{ plugin?: unknown[] }>(file) } describe("plugin.install.task", () => { test("writes both server and tui config entries", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server", "tui"]) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const server = await read(path.join(tmp.path, ".opencode", "opencode.jsonc")) const tui = await read(path.join(tmp.path, ".opencode", "tui.jsonc")) }) test("writes default options from exports config metadata", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server", "tui"], { server: { custom: true, other: false }, tui: { compact: true }, }) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const server = await read(path.join(tmp.path, ".opencode", "opencode.jsonc")) const tui = await read(path.join(tmp.path, ".opencode", "tui.jsonc")) }) test("preserves JSONC comments when adding plugins to server and tui config", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server", "tui"]) const cfg = path.join(tmp.path, ".opencode") const server = path.join(cfg, "opencode.jsonc") const tui = path.join(cfg, "tui.jsonc") await fs.mkdir(cfg, { recursive: true }) await Bun.write( server, `{ // server head "plugin": [ // server keep ], // server tail "model": "x" } `, ) await Bun.write( tui, `{ // tui head "plugin": [ // tui keep ], // tui tail "theme": "opencode" } `, ) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const serverText = await fs.readFile(server, "utf8") const tuiText = await fs.readFile(tui, "utf8") expect(serverText).toContain("// server head") expect(serverText).toContain("// server keep") expect(serverText).toContain("// server tail") expect(tuiText).toContain("// tui head") expect(tuiText).toContain("// tui keep") expect(tuiText).toContain("// tui tail") const serverJson = parseJsonc(serverText) as { plugin?: unknown[] } const tuiJson = parseJsonc(tuiText) as { plugin?: unknown[] } }) test("preserves JSONC comments when force replacing plugin version", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const cfg = path.join(tmp.path, ".opencode", "opencode.jsonc") await fs.mkdir(path.dirname(cfg), { recursive: true }) await Bun.write( cfg, `{ "plugin": [ // keep this note ] } `, ) const run = createPlugTask( { force: true, }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const text = await fs.readFile(cfg, "utf8") expect(text).toContain("// keep this note") const json = parseJsonc(text) as { plugin?: unknown[] } }) test("supports resolver target pointing to a file", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const file = path.join(target, "index.js") await Bun.write(file, "export {}") const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), file), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const server = await read(path.join(tmp.path, ".opencode", "opencode.jsonc")) }) test("does not change configured package version without force", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const cfg = path.join(tmp.path, ".opencode", "opencode.json") await fs.mkdir(path.dirname(cfg), { recursive: true }) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const json = await read(cfg) }) test("does not change scoped package version without force", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const cfg = path.join(tmp.path, ".opencode", "opencode.json") await fs.mkdir(path.dirname(cfg), { recursive: true }) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const json = await read(cfg) }) test("keeps file plugin entries and still adds npm plugin", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const cfg = path.join(tmp.path, ".opencode", "opencode.json") await fs.mkdir(path.dirname(cfg), { recursive: true }) await Bun.write(cfg, JSON.stringify({ plugin: ["file:///tmp/acme.ts"] }, null, 2)) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const json = await read(cfg) }) test("force replaces configured package version and keeps tuple options", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const cfg = path.join(tmp.path, ".opencode", "opencode.json") await fs.mkdir(path.dirname(cfg), { recursive: true }) await Bun.write( cfg, JSON.stringify( { }, null, 2, ), ) const run = createPlugTask( { force: true, }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const json = await read(cfg) }) test("writes to global scope when global flag is set", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const global = path.join(tmp.path, "global") const run = createPlugTask( { global: true, }, deps(global, target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) expect(await Filesystem.exists(path.join(global, "opencode.jsonc"))).toBe(true) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false) }) test("writes local scope under directory when vcs is not git", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const directory = path.join(tmp.path, "dir") const worktree = path.join(tmp.path, "worktree") await fs.mkdir(directory, { recursive: true }) await fs.mkdir(worktree, { recursive: true }) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctxDir(directory, worktree)) expect(ok).toBe(true) expect(await Filesystem.exists(path.join(directory, ".opencode", "opencode.jsonc"))).toBe(true) expect(await Filesystem.exists(path.join(worktree, ".opencode", "opencode.jsonc"))).toBe(false) }) test("writes local scope under directory when worktree is root slash", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const directory = path.join(tmp.path, "dir") await fs.mkdir(directory, { recursive: true }) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctxRoot(directory)) expect(ok).toBe(true) expect(await Filesystem.exists(path.join(directory, ".opencode", "opencode.jsonc"))).toBe(true) }) test("writes tui local scope under directory when worktree is root slash", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["tui"]) const directory = path.join(tmp.path, "dir") await fs.mkdir(directory, { recursive: true }) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctxRoot(directory)) expect(ok).toBe(true) expect(await Filesystem.exists(path.join(directory, ".opencode", "tui.jsonc"))).toBe(true) }) test("writes only tui config for tui-only plugins", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["tui"]) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "tui.jsonc"))).toBe(true) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false) }) test("writes tui config for oc-themes-only packages", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, undefined, undefined, ["themes/forest.json"]) await fs.mkdir(path.join(target, "themes"), { recursive: true }) await Bun.write(path.join(target, "themes", "forest.json"), JSON.stringify({ theme: { text: "#fff" } }, null, 2)) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "tui.jsonc"))).toBe(true) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false) const tui = await read(path.join(tmp.path, ".opencode", "tui.jsonc")) }) test("returns false for oc-themes outside plugin directory", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, undefined, undefined, ["../outside.json"]) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(false) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "tui.jsonc"))).toBe(false) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false) }) test("force replaces version in both server and tui configs", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server", "tui"]) const server = path.join(tmp.path, ".opencode", "opencode.json") const tui = path.join(tmp.path, ".opencode", "tui.json") await fs.mkdir(path.dirname(server), { recursive: true }) await Bun.write(server, JSON.stringify({ plugin: ["[email protected]", "[email protected]"] }, null, 2)) await Bun.write(tui, JSON.stringify({ plugin: [["[email protected]", { mode: "safe" }], "[email protected]"] }, null, 2)) const run = createPlugTask( { force: true, }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(true) const serverJson = await read(server) const tuiJson = await read(tui) }) test("returns false and keeps config unchanged for invalid JSONC", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path, ["server"]) const cfg = path.join(tmp.path, ".opencode", "opencode.jsonc") await fs.mkdir(path.dirname(cfg), { recursive: true }) await Bun.write(cfg, bad) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(false) expect(await fs.readFile(cfg, "utf8")).toBe(bad) }) test("returns false when manifest declares no supported targets", async () => { await using tmp = await tmpdir() const target = await plugin(tmp.path) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(false) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "tui.jsonc"))).toBe(false) }) test("returns false when manifest cannot be read", async () => { await using tmp = await tmpdir() const target = path.join(tmp.path, "plugin") await fs.mkdir(target, { recursive: true }) const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), target), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(false) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false) }) test("returns false when install fails", async () => { await using tmp = await tmpdir() const run = createPlugTask( { }, deps(path.join(tmp.path, "global"), new Error("boom")), ) const ok = await run(ctx(tmp.path)) expect(ok).toBe(false) expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false) }) }) |
| 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. |