|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { createTestRenderer } from "@opentui/core/testing" |
| 3 | +import { Effect, FileSystem } from "effect" |
| 4 | +import { Global } from "@opencode/util/global" |
| 5 | +import { createEventStream, createFetch, directory, json } from "./fixture/tui-client" |
| 6 | +import { tmpdir } from "./fixture/fixture" |
| 7 | + |
| 8 | +test("a failed shell command in an existing session toasts and restores the composer", async () => { |
| 9 | + await using state = await tmpdir() |
| 10 | + const setup = await createTestRenderer({ width: 100, height: 30, kittyKeyboard: true }) |
| 11 | + await setup.renderer.setupTerminal() |
| 12 | + setup.renderer.start() |
| 13 | + const ready = Promise.withResolvers<void>() |
| 14 | + const shell = Promise.withResolvers<string>() |
| 15 | + const events = createEventStream() |
| 16 | + // The draft stash is process-wide, so reruns use a fresh session for an empty composer. |
| 17 | + const sessionID = `ses_shell_failure_${crypto.randomUUID()}` |
| 18 | + const location = { directory, project: { id: "project", directory, canonical: directory } } |
| 19 | + const calls = createFetch(async (url, request) => { |
| 20 | + if (url.pathname === `/api/session/${sessionID}`) |
| 21 | + return json({ |
| 22 | + data: { |
| 23 | + id: sessionID, |
| 24 | + projectID: "project", |
| 25 | + title: "Shell fixture", |
| 26 | + agent: "build", |
| 27 | + model: { providerID: "demo", id: "model" }, |
| 28 | + location: { directory }, |
| 29 | + cost: 0, |
| 30 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 31 | + time: { created: 0, updated: 0 }, |
| 32 | + }, |
| 33 | + }) |
| 34 | + if (url.pathname === `/api/session/${sessionID}/message`) return json({ data: [], cursor: {} }) |
| 35 | + if (url.pathname === `/api/session/${sessionID}/inbox` || url.pathname === `/api/session/${sessionID}/permission`) |
| 36 | + return json({ data: [] }) |
| 37 | + if (url.pathname === "/api/agent") |
| 38 | + return json({ location, data: [{ id: "build", mode: "primary", hidden: false, permissions: [] }] }) |
| 39 | + if (url.pathname === "/api/provider") return json({ location, data: [{ id: "demo", name: "Demo" }] }) |
| 40 | + if (url.pathname === "/api/model") |
| 41 | + return json({ location, data: [{ id: "model", providerID: "demo", name: "Demo Model", variants: [] }] }) |
| 42 | + if (url.pathname === `/api/session/${sessionID}/shell`) { |
| 43 | + shell.resolve((await request.json()).command) |
| 44 | + return json({ message: "Shell unavailable" }, { status: 400 }) |
| 45 | + } |
| 46 | + return undefined |
| 47 | + }, events) |
| 48 | + const server = Bun.serve({ port: 0, fetch: (request) => calls.fetch(request) }) |
| 49 | + const { run } = await import("../src/app") |
| 50 | + const task = Effect.runPromise( |
| 51 | + run({ |
| 52 | + app: { name: "test", version: "test", channel: "test" }, |
| 53 | + server: { endpoint: { url: server.url.toString() } }, |
| 54 | + config: { get: async () => ({ animations: false }), update: async () => ({}) }, |
| 55 | + packages: { prepare: async () => ({ directory: "" }) }, |
| 56 | + terminalHandoff: async () => ({ renderer: setup.renderer, mode: "dark", complete: ready.resolve }), |
| 57 | + args: { sessionID }, |
| 58 | + log: () => {}, |
| 59 | + }).pipe(Effect.provide(Global.layerWith({ state: state.path })), Effect.provide(FileSystem.layerNoop({}))), |
| 60 | + ) |
| 61 | + try { |
| 62 | + await ready.promise |
| 63 | + await setup.waitForFrame((frame) => frame.includes("Demo Model")) |
| 64 | + await setup.mockInput.typeText("!") |
| 65 | + await setup.mockInput.typeText("echo restored") |
| 66 | + setup.mockInput.pressEnter() |
| 67 | + expect(await shell.promise).toBe("echo restored") |
| 68 | + const frame = await setup.waitForFrame((frame) => frame.includes("Shell unavailable")) |
| 69 | + expect(frame).toContain("Failed to run shell command") |
| 70 | + expect(frame).toContain("echo restored") |
| 71 | + } finally { |
| 72 | + setup.renderer.destroy() |
| 73 | + await task |
| 74 | + await server.stop() |
| 75 | + } |
| 76 | +}) |
0 commit comments