|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { createTestRenderer } from "@opentui/core/testing" |
| 3 | +import { Effect, FileSystem } from "effect" |
| 4 | +import path from "node:path" |
| 5 | +import { Global } from "@opencode/util/global" |
| 6 | +import { createEventStream, createFetch, json } from "./fixture/tui-client" |
| 7 | +import { tmpdir } from "./fixture/fixture" |
| 8 | + |
| 9 | +// The session lives in another directory than the client's own location, so the client syncs |
| 10 | +// that location instead of the default one. |
| 11 | +const session = { |
| 12 | + id: "ses_test", |
| 13 | + title: "Location sync", |
| 14 | + projectID: "proj_test", |
| 15 | + location: { directory: "/tmp/opencode/other-clone" }, |
| 16 | + cost: 0, |
| 17 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 18 | + time: { created: 0, updated: 0 }, |
| 19 | +} |
| 20 | + |
| 21 | +const unavailable = "Session location unavailable" |
| 22 | + |
| 23 | +async function wait(predicate: () => boolean, timeout = 20_000) { |
| 24 | + const start = Date.now() |
| 25 | + while (!predicate()) { |
| 26 | + if (Date.now() - start > timeout) throw new Error("timed out waiting for condition") |
| 27 | + await Bun.sleep(10) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +async function boot(root: string, fail: (url: URL) => Response | undefined) { |
| 32 | + const setup = await createTestRenderer({ width: 100, height: 30, kittyKeyboard: true }) |
| 33 | + await setup.renderer.setupTerminal() |
| 34 | + setup.renderer.start() |
| 35 | + const events = createEventStream() |
| 36 | + const calls = createFetch(async (url) => { |
| 37 | + if (url.pathname === "/api/session") return json({ data: [session], cursor: {} }) |
| 38 | + if (url.pathname === "/api/session/ses_test") return json({ data: session }) |
| 39 | + if (url.pathname === "/api/session/ses_test/message") return json({ data: [], cursor: {} }) |
| 40 | + if (url.pathname === "/api/session/ses_test/inbox") return json({ data: [] }) |
| 41 | + if (url.pathname === "/api/session/ses_test/permission") return json({ data: [] }) |
| 42 | + return fail(url) |
| 43 | + }, events) |
| 44 | + const server = Bun.serve({ port: 0, idleTimeout: 0, fetch: (request) => calls.fetch(request) }) |
| 45 | + const { run } = await import("../src/app") |
| 46 | + const task = Effect.runPromise( |
| 47 | + run({ |
| 48 | + app: { name: "test", version: "test", channel: "test" }, |
| 49 | + server: { endpoint: { url: server.url.toString() } }, |
| 50 | + config: { get: async () => ({ animations: false }), update: async () => ({}) }, |
| 51 | + packages: { prepare: async () => ({ directory: "" }) }, |
| 52 | + terminalHandoff: async () => ({ renderer: setup.renderer, mode: "dark", complete: () => {} }), |
| 53 | + args: { sessionID: "ses_test" }, |
| 54 | + log: () => {}, |
| 55 | + }).pipe( |
| 56 | + Effect.provide( |
| 57 | + Global.layerWith({ |
| 58 | + home: path.join(root, "home"), |
| 59 | + data: path.join(root, "data"), |
| 60 | + cache: path.join(root, "cache"), |
| 61 | + config: path.join(root, "config"), |
| 62 | + state: path.join(root, "state"), |
| 63 | + bin: path.join(root, "cache", "bin"), |
| 64 | + log: path.join(root, "data", "log"), |
| 65 | + repos: path.join(root, "data", "repos"), |
| 66 | + tmp: path.join(root, "tmp"), |
| 67 | + }), |
| 68 | + ), |
| 69 | + Effect.provide(FileSystem.layerNoop({})), |
| 70 | + ), |
| 71 | + ) |
| 72 | + return { setup, task, server } |
| 73 | +} |
| 74 | + |
| 75 | +function serviceUnavailable() { |
| 76 | + return json( |
| 77 | + { _tag: "ServiceUnavailableError", message: "Location plugins did not finish activating", service: "plugin" }, |
| 78 | + { status: 503 }, |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +test("a location read that fails for a transient reason is retried", async () => { |
| 83 | + await using state = await tmpdir() |
| 84 | + let attempts = 0 |
| 85 | + const app = await boot(state.path, (url) => { |
| 86 | + if (url.pathname !== "/api/model") return undefined |
| 87 | + if (url.searchParams.get("location[directory]") !== session.location.directory) return undefined |
| 88 | + attempts++ |
| 89 | + return attempts === 1 ? serviceUnavailable() : undefined |
| 90 | + }) |
| 91 | + try { |
| 92 | + await wait(() => attempts >= 2 || app.setup.captureCharFrame().includes(unavailable)) |
| 93 | + expect(app.setup.captureCharFrame()).not.toContain(unavailable) |
| 94 | + expect(attempts).toBeGreaterThanOrEqual(2) |
| 95 | + } finally { |
| 96 | + if (!app.setup.renderer.isDestroyed) app.setup.renderer.destroy() |
| 97 | + await app.task.finally(() => app.server.stop(true)) |
| 98 | + } |
| 99 | +}) |
| 100 | + |
| 101 | +test("a location read that keeps failing keeps the composer", async () => { |
| 102 | + await using state = await tmpdir() |
| 103 | + let attempts = 0 |
| 104 | + const app = await boot(state.path, (url) => { |
| 105 | + if (url.pathname !== "/api/model") return undefined |
| 106 | + if (url.searchParams.get("location[directory]") !== session.location.directory) return undefined |
| 107 | + attempts++ |
| 108 | + return serviceUnavailable() |
| 109 | + }) |
| 110 | + try { |
| 111 | + // The retry gives up after three attempts. |
| 112 | + await wait(() => attempts >= 3 || app.setup.captureCharFrame().includes(unavailable)) |
| 113 | + await app.setup.waitForVisualIdle() |
| 114 | + const frame = app.setup.captureCharFrame() |
| 115 | + expect(frame).not.toContain(unavailable) |
| 116 | + expect(frame).not.toContain("Choose another directory to continue this session.") |
| 117 | + app.setup.mockInput.typeText("still typing") |
| 118 | + await app.setup.waitForVisualIdle() |
| 119 | + expect(app.setup.captureCharFrame()).toContain("still typing") |
| 120 | + } finally { |
| 121 | + if (!app.setup.renderer.isDestroyed) app.setup.renderer.destroy() |
| 122 | + await app.task.finally(() => app.server.stop(true)) |
| 123 | + } |
| 124 | +}) |
| 125 | + |
| 126 | +test("a location the server reports as missing asks for another directory", async () => { |
| 127 | + await using state = await tmpdir() |
| 128 | + let attempts = 0 |
| 129 | + const app = await boot(state.path, (url) => { |
| 130 | + if (url.pathname !== "/api/location") return undefined |
| 131 | + if (url.searchParams.get("location[directory]") !== session.location.directory) return undefined |
| 132 | + attempts++ |
| 133 | + return json( |
| 134 | + { |
| 135 | + _tag: "LocationNotFoundError", |
| 136 | + location: { directory: session.location.directory }, |
| 137 | + message: `Location not found: ${session.location.directory}`, |
| 138 | + }, |
| 139 | + { status: 404 }, |
| 140 | + ) |
| 141 | + }) |
| 142 | + try { |
| 143 | + await app.setup.waitForFrame((frame) => frame.includes(unavailable)) |
| 144 | + const frame = app.setup.captureCharFrame() |
| 145 | + expect(frame).toContain(session.location.directory) |
| 146 | + expect(frame).toContain("Choose another directory to continue this session.") |
| 147 | + } finally { |
| 148 | + if (!app.setup.renderer.isDestroyed) app.setup.renderer.destroy() |
| 149 | + await app.task.finally(() => app.server.stop(true)) |
| 150 | + } |
| 151 | +}) |
0 commit comments