|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { Effect } from "effect" |
| 3 | +import { mkdir } from "node:fs/promises" |
| 4 | +import path from "node:path" |
| 5 | +import { pathToFileURL } from "node:url" |
| 6 | +import { Global } from "@opencode/util/global" |
| 7 | +import { Npm } from "@opencode/util/npm" |
| 8 | +import { LayerNode } from "@opencode/util/effect/layer-node" |
| 9 | +import { createAppFixture } from "./fixture/app" |
| 10 | +import { tmpdir } from "./fixture/fixture" |
| 11 | +import { directory, json } from "./fixture/tui-client" |
| 12 | + |
| 13 | +const npmLayer = (cache: string) => |
| 14 | + LayerNode.compile(Npm.node, { |
| 15 | + replacements: [Global.node.replace(Global.layerWith({ cache, state: path.join(cache, "state") }))], |
| 16 | + }) |
| 17 | + |
| 18 | +test.each([false, true])( |
| 19 | + "server Git plugins load with a separate TUI cache (cached: %s)", |
| 20 | + async (cached) => { |
| 21 | + await using tmp = await tmpdir() |
| 22 | + const repository = path.join(tmp.path, "repository") |
| 23 | + await mkdir(repository) |
| 24 | + await Bun.write( |
| 25 | + path.join(repository, "package.json"), |
| 26 | + JSON.stringify({ |
| 27 | + name: "fixture-git-tui", |
| 28 | + version: "1.0.0", |
| 29 | + type: "module", |
| 30 | + exports: { ".": "./index.js", "./tui": "./tui.js" }, |
| 31 | + }), |
| 32 | + ) |
| 33 | + await Bun.write(path.join(repository, "index.js"), "export default {}\n") |
| 34 | + await Bun.write( |
| 35 | + path.join(repository, "tui.js"), |
| 36 | + `export default { |
| 37 | + id: "fixture.git-tui", |
| 38 | + setup(context) { |
| 39 | + context.ui.toast.show({ message: "Git TUI plugin loaded", variant: "success" }) |
| 40 | + } |
| 41 | + }\n`, |
| 42 | + ) |
| 43 | + await Bun.$`git init -q -b fixture ${repository}` |
| 44 | + await Bun.$`git -C ${repository} add .` |
| 45 | + await Bun.$`git -C ${repository} -c user.name=fixture -c [email protected] commit -qm fixture` |
| 46 | + const target = `git+${pathToFileURL(repository).href}#fixture` |
| 47 | + |
| 48 | + await Effect.gen(function* () { |
| 49 | + const server = yield* Npm.Service.pipe(Effect.provide(npmLayer(path.join(tmp.path, "server-cache")))) |
| 50 | + const installed = yield* server.add(target) |
| 51 | + const client = yield* Npm.Service.pipe(Effect.provide(npmLayer(path.join(tmp.path, "tui-cache")))) |
| 52 | + if (cached) yield* client.add(target) |
| 53 | + const preparations: boolean[] = [] |
| 54 | + const inventories: string[] = [] |
| 55 | + yield* Effect.promise(async () => { |
| 56 | + await using setup = await createAppFixture({ |
| 57 | + config: { animations: false }, |
| 58 | + packages: { |
| 59 | + prepare: (spec, install = true) => { |
| 60 | + preparations.push(install) |
| 61 | + return Effect.runPromise(install ? client.add(spec) : client.resolve(spec)) |
| 62 | + }, |
| 63 | + }, |
| 64 | + fetch: (url) => { |
| 65 | + if (url.pathname !== "/api/plugin") return undefined |
| 66 | + inventories.push(url.pathname) |
| 67 | + return json({ |
| 68 | + location: { directory, project: { id: "proj_test", directory, canonical: directory } }, |
| 69 | + data: [ |
| 70 | + { |
| 71 | + id: "fixture.git-tui", |
| 72 | + source: { type: "package", target, version: installed.version }, |
| 73 | + features: { server: true, tui: true }, |
| 74 | + state: { status: "active" }, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }) |
| 78 | + }, |
| 79 | + }) |
| 80 | + await setup.ready |
| 81 | + await setup.waitFor(() => preparations.length > 0) |
| 82 | + expect(preparations).toEqual([true]) |
| 83 | + await setup.waitForFrame((frame) => frame.includes("Git TUI plugin loaded")) |
| 84 | + expect(preparations).toEqual([true]) |
| 85 | + const prepared = await Effect.runPromise(client.resolve(target)) |
| 86 | + expect(prepared.directory).not.toBe(installed.directory) |
| 87 | + expect(await Bun.file(path.join(prepared.directory, "tui.js")).exists()).toBe(true) |
| 88 | + |
| 89 | + setup.events.emit({ id: "evt_plugins", created: 1, type: "plugin.updated", data: {} }) |
| 90 | + await setup.waitFor(() => inventories.length > 1) |
| 91 | + await setup.renderOnce() |
| 92 | + expect(preparations).toEqual([true]) |
| 93 | + }) |
| 94 | + }).pipe(Effect.scoped, Effect.runPromise) |
| 95 | + }, |
| 96 | + 30_000, |
| 97 | +) |
0 commit comments