|
1 | 1 | /** @jsxImportSource @opentui/solid */ |
2 | 2 | import { BoxRenderable, type Renderable } from "@opentui/core" |
3 | 3 | import { testRender } from "@opentui/solid" |
4 | | -import type { PermissionRequest } from "@opencode/client" |
5 | 4 | import { expect, test } from "bun:test" |
6 | | -import { createSignal } from "solid-js" |
7 | | -import { ConfigProvider, type Info } from "../../../src/config" |
8 | | -import { ClientProvider } from "../../../src/context/client" |
9 | | -import { DataProvider } from "../../../src/context/data" |
| 5 | +import { ConfigProvider } from "../../../src/config" |
10 | 6 | import { Keymap } from "../../../src/context/keymap" |
11 | | -import { LocationProvider } from "../../../src/context/location" |
12 | 7 | import { ThemeProvider } from "../../../src/context/theme" |
13 | | -import { ToastProvider } from "../../../src/ui/toast" |
14 | | -import { PermissionPrompt } from "../../../src/routes/session/permission" |
| 8 | +import { SessionQuestion } from "../../../src/routes/session/permission" |
15 | 9 | import { emptyThemeSource } from "../../fixture/fixture" |
16 | | -import { TestTuiContexts } from "../../fixture/tui-environment" |
17 | 10 | import { createTuiResolvedConfig } from "../../fixture/tui-runtime" |
18 | | -import { createApi, createEventStream, createFetch } from "../../fixture/tui-client" |
19 | 11 |
|
20 | 12 | test.each([ |
21 | 13 | { config: undefined, height: 15 }, |
22 | 14 | { config: { max_height: 30 }, height: 30 }, |
23 | | - { config: { default_expanded: false }, height: 15 }, |
24 | | -])("renders a collapsed permission prompt with $config", async ({ config, height }) => { |
25 | | - const prompt = await mountPrompt(config) |
| 15 | +])("limits the question panel to max_height=$height", async ({ config, height }) => { |
| 16 | + const app = await mountQuestion(config) |
26 | 17 | try { |
27 | | - expect(dialog(prompt.app.renderer.root)?.height).toBe(height) |
28 | | - expect(prompt.app.captureCharFrame()).toContain("fullscreen") |
29 | | - expect(prompt.app.captureCharFrame()).toContain("Allow once") |
| 18 | + expect(dialog(app.renderer.root)?.height).toBe(height) |
| 19 | + expect(app.captureCharFrame()).toContain("fullscreen") |
30 | 20 | } finally { |
31 | | - prompt.app.renderer.destroy() |
| 21 | + app.renderer.destroy() |
32 | 22 | } |
33 | 23 | }) |
34 | 24 |
|
35 | 25 | test("opens fullscreen by default and toggles back to the configured height", async () => { |
36 | | - const prompt = await mountPrompt({ max_height: 20, default_expanded: true }) |
| 26 | + const app = await mountQuestion({ max_height: 20, default_expanded: true }) |
37 | 27 | try { |
38 | | - expect(dialog(prompt.app.renderer.root)?.height).toBeGreaterThan(30) |
39 | | - expect(prompt.app.captureCharFrame()).toContain("minimize") |
| 28 | + expect(dialog(app.renderer.root)?.height).toBeGreaterThan(30) |
| 29 | + expect(app.captureCharFrame()).toContain("minimize") |
40 | 30 |
|
41 | | - prompt.app.mockInput.pressKey("f", { ctrl: true }) |
42 | | - await prompt.app.waitForFrame((frame) => !frame.includes("minimize")) |
43 | | - expect(dialog(prompt.app.renderer.root)?.height).toBe(20) |
44 | | - expect(prompt.app.captureCharFrame()).toContain("fullscreen") |
| 31 | + app.mockInput.pressKey("f", { ctrl: true }) |
| 32 | + await app.waitForFrame((frame) => !frame.includes("minimize")) |
| 33 | + expect(dialog(app.renderer.root)?.height).toBe(20) |
| 34 | + expect(app.captureCharFrame()).toContain("fullscreen") |
45 | 35 |
|
46 | | - prompt.app.mockInput.pressKey("f", { ctrl: true }) |
47 | | - await prompt.app.waitForFrame((frame) => frame.includes("minimize")) |
48 | | - expect(dialog(prompt.app.renderer.root)?.height).toBeGreaterThan(30) |
| 36 | + app.mockInput.pressKey("f", { ctrl: true }) |
| 37 | + await app.waitForFrame((frame) => frame.includes("minimize")) |
| 38 | + expect(dialog(app.renderer.root)?.height).toBeGreaterThan(30) |
49 | 39 | } finally { |
50 | | - prompt.app.renderer.destroy() |
| 40 | + app.renderer.destroy() |
51 | 41 | } |
52 | 42 | }) |
53 | 43 |
|
54 | | -test.each([{ expanded: false }, { expanded: true }])( |
55 | | - "restores default_expanded=$expanded for each new request", |
56 | | - async ({ expanded }) => { |
57 | | - const prompt = await mountPrompt({ default_expanded: expanded }) |
58 | | - try { |
59 | | - prompt.app.mockInput.pressKey("f", { ctrl: true }) |
60 | | - await prompt.app.waitForFrame((frame) => frame.includes(expanded ? "fullscreen" : "minimize")) |
61 | | - |
62 | | - prompt.setRequest((request) => ({ ...request, metadata: { parentDir: "/other" } })) |
63 | | - await prompt.app.waitForFrame((frame) => frame.includes(expanded ? "fullscreen" : "minimize")) |
64 | | - |
65 | | - prompt.setRequest((request) => ({ ...request, id: "per_2" })) |
66 | | - await prompt.app.waitForFrame((frame) => frame.includes(expanded ? "minimize" : "fullscreen")) |
67 | | - if (expanded) expect(dialog(prompt.app.renderer.root)?.height).toBeGreaterThan(30) |
68 | | - if (!expanded) expect(dialog(prompt.app.renderer.root)?.height).toBe(15) |
69 | | - } finally { |
70 | | - prompt.app.renderer.destroy() |
71 | | - } |
72 | | - }, |
73 | | -) |
74 | | - |
75 | | -async function mountPrompt(config: Info["permission_prompt"]) { |
76 | | - const events = createEventStream() |
77 | | - const calls = createFetch(undefined, events) |
78 | | - const [request, setRequest] = createSignal<PermissionRequest>({ |
79 | | - id: "per_1", |
80 | | - sessionID: "ses_1", |
81 | | - action: "external_directory", |
82 | | - resources: Array.from({ length: 60 }, (_, index) => `/external/file-${index}`), |
83 | | - metadata: { parentDir: "/external" }, |
84 | | - save: ["/external/*"], |
85 | | - }) |
86 | | - |
87 | | - function Content() { |
88 | | - return ( |
89 | | - <box height="100%" justifyContent="flex-end"> |
90 | | - <box flexShrink={0}> |
91 | | - <PermissionPrompt request={request()} /> |
92 | | - </box> |
93 | | - </box> |
94 | | - ) |
| 44 | +test("questions without fullscreen stay collapsed", async () => { |
| 45 | + const app = await mountQuestion({ default_expanded: true }, { fullscreen: false }) |
| 46 | + try { |
| 47 | + expect(dialog(app.renderer.root)?.height).toBe(15) |
| 48 | + expect(app.captureCharFrame()).not.toContain("fullscreen") |
| 49 | + expect(app.captureCharFrame()).not.toContain("minimize") |
| 50 | + } finally { |
| 51 | + app.renderer.destroy() |
95 | 52 | } |
| 53 | +}) |
96 | 54 |
|
| 55 | +async function mountQuestion( |
| 56 | + permissionPrompt?: { max_height?: number; default_expanded?: boolean }, |
| 57 | + options?: { fullscreen?: boolean }, |
| 58 | +) { |
97 | 59 | const app = await testRender( |
98 | 60 | () => ( |
99 | | - <TestTuiContexts> |
100 | | - <ConfigProvider config={createTuiResolvedConfig({ permission_prompt: config })}> |
101 | | - <Keymap.Provider> |
102 | | - <ThemeProvider mode="dark" source={emptyThemeSource}> |
103 | | - <ToastProvider> |
104 | | - <ClientProvider api={createApi(calls.fetch)}> |
105 | | - <DataProvider> |
106 | | - <LocationProvider> |
107 | | - <Content /> |
108 | | - </LocationProvider> |
109 | | - </DataProvider> |
110 | | - </ClientProvider> |
111 | | - </ToastProvider> |
112 | | - </ThemeProvider> |
113 | | - </Keymap.Provider> |
114 | | - </ConfigProvider> |
115 | | - </TestTuiContexts> |
| 61 | + <ConfigProvider config={createTuiResolvedConfig({ session: { permission_prompt: permissionPrompt } })}> |
| 62 | + <Keymap.Provider> |
| 63 | + <ThemeProvider mode="dark" source={emptyThemeSource}> |
| 64 | + <box height="100%" justifyContent="flex-end"> |
| 65 | + <box flexShrink={0}> |
| 66 | + <SessionQuestion |
| 67 | + title="Permission required" |
| 68 | + instance="per_1" |
| 69 | + body={ |
| 70 | + <box paddingLeft={1}> |
| 71 | + {Array.from({ length: 60 }, (_, index) => ( |
| 72 | + <text>/external/file-{index}</text> |
| 73 | + ))} |
| 74 | + </box> |
| 75 | + } |
| 76 | + options={options?.fullscreen === false ? { move: "Choose directory" } : { once: "Allow once" }} |
| 77 | + escapeKey={options?.fullscreen === false ? undefined : "once"} |
| 78 | + fullscreen={options?.fullscreen !== false} |
| 79 | + onSelect={() => {}} |
| 80 | + /> |
| 81 | + </box> |
| 82 | + </box> |
| 83 | + </ThemeProvider> |
| 84 | + </Keymap.Provider> |
| 85 | + </ConfigProvider> |
116 | 86 | ), |
117 | 87 | { width: 100, height: 40, kittyKeyboard: true }, |
118 | 88 | ) |
119 | 89 | app.renderer.start() |
120 | 90 | await app.waitForFrame((frame) => frame.includes("Permission required")) |
121 | | - return { app, setRequest } |
| 91 | + return app |
122 | 92 | } |
123 | 93 |
|
124 | 94 | function dialog(root: Renderable): BoxRenderable | undefined { |
|
0 commit comments