|
1 | 1 | import { expect, mock, test } from "bun:test" |
| 2 | +import type { TuiPluginApi } from "@opencode-ai/plugin/tui" |
2 | 3 | import { createTestRenderer } from "@opentui/core/testing" |
3 | 4 | import { Effect } from "effect" |
4 | 5 | import { Global } from "@opencode-ai/core/global" |
5 | 6 | import { createTuiResolvedConfig } from "./fixture/tui-runtime" |
6 | | -import { createEventSource, createFetch, directory } from "./fixture/tui-sdk" |
| 7 | +import { createEventSource, createFetch, directory, json } from "./fixture/tui-sdk" |
7 | 8 |
|
8 | 9 | test("SIGHUP clears title and disposes scoped resources once", async () => { |
9 | 10 | const setup = await createTestRenderer({ width: 80, height: 24, useThread: false }) |
@@ -57,3 +58,70 @@ test("SIGHUP clears title and disposes scoped resources once", async () => { |
57 | 58 | mock.restore() |
58 | 59 | } |
59 | 60 | }) |
| 61 | + |
| 62 | +test("app.exit prints the session epilogue after scoped cleanup", async () => { |
| 63 | + const setup = await createTestRenderer({ width: 80, height: 24, useThread: false }) |
| 64 | + const core = await import("@opentui/core") |
| 65 | + mock.module("@opentui/core", () => ({ ...core, createCliRenderer: async () => setup.renderer })) |
| 66 | + const events = createEventSource() |
| 67 | + const calls = createFetch((url) => { |
| 68 | + if (url.pathname === "/session") |
| 69 | + return json([ |
| 70 | + { |
| 71 | + id: "dummy", |
| 72 | + title: "Demo session", |
| 73 | + slug: "dummy", |
| 74 | + projectID: "project", |
| 75 | + directory, |
| 76 | + version: "0.0.0-test", |
| 77 | + time: { created: 0, updated: 0 }, |
| 78 | + }, |
| 79 | + ]) |
| 80 | + }) |
| 81 | + const originalWrite = process.stdout.write.bind(process.stdout) |
| 82 | + let stdout = "" |
| 83 | + let api: TuiPluginApi | undefined |
| 84 | + let started!: () => void |
| 85 | + const ready = new Promise<void>((resolve) => { |
| 86 | + started = resolve |
| 87 | + }) |
| 88 | + |
| 89 | + process.stdout.write = ((chunk: string | Uint8Array) => { |
| 90 | + stdout += String(chunk) |
| 91 | + return true |
| 92 | + }) as typeof process.stdout.write |
| 93 | + |
| 94 | + try { |
| 95 | + const { run } = await import("../src/app") |
| 96 | + const task = Effect.runPromise( |
| 97 | + run({ |
| 98 | + url: "http://test", |
| 99 | + directory, |
| 100 | + config: createTuiResolvedConfig({ plugin_enabled: {} }), |
| 101 | + fetch: calls.fetch, |
| 102 | + events: events.source, |
| 103 | + args: { continue: true }, |
| 104 | + pluginHost: { |
| 105 | + async start(input) { |
| 106 | + api = input.api |
| 107 | + started() |
| 108 | + }, |
| 109 | + async dispose() {}, |
| 110 | + }, |
| 111 | + }).pipe(Effect.provide(Global.defaultLayer)), |
| 112 | + ) |
| 113 | + |
| 114 | + await ready |
| 115 | + await setup.renderOnce() |
| 116 | + await setup.renderOnce() |
| 117 | + api?.keymap.dispatchCommand("app.exit") |
| 118 | + await task |
| 119 | + |
| 120 | + expect(stdout).toContain("Demo session") |
| 121 | + expect(stdout).toContain("opencode -s dummy") |
| 122 | + } finally { |
| 123 | + process.stdout.write = originalWrite |
| 124 | + if (!setup.renderer.isDestroyed) setup.renderer.destroy() |
| 125 | + mock.restore() |
| 126 | + } |
| 127 | +}) |
0 commit comments