section.client.spec.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // @vitest-environment jsdom
  2. /**
  3. * What the section and its cards show: the empty line when no plugin
  4. * contributed one, a card that renders nothing while its namespace is
  5. * unavailable, and the save footer that decides when staged edits are written.
  6. */
  7. import { cleanup, fireEvent, render, screen } from '@testing-library/react'
  8. import { afterEach, describe, expect, it, vi } from 'vitest'
  9. import { bindSnapshotSelector } from '@deepseek-ai/dsh-client-web-react'
  10. import { createSnapshotStore } from '@deepseek-ai/dsh-client-runtime/client'
  11. import { AgentLoopCard } from '../src/client/AgentLoopCard.tsx'
  12. import type { AgentLoopCardProps } from '../src/client/AgentLoopCard.tsx'
  13. import { BashCard } from '../src/client/BashCard.tsx'
  14. import type { BashCardProps } from '../src/client/BashCard.tsx'
  15. import { ConfigurablePluginsTab } from '../src/client/ConfigurablePluginsTab.tsx'
  16. import type { ConfigurablePluginsTabProps } from '../src/client/ConfigurablePluginsTab.tsx'
  17. import { PluginsSettingsSection } from '../src/client/PluginsSettingsSection.tsx'
  18. import type { PluginsSettingsSectionProps, PluginsSettingsTabEntry } from '../src/client/PluginsSettingsSection.tsx'
  19. import { WebSearchCard } from '../src/client/WebSearchCard.tsx'
  20. import type { WebSearchCardProps } from '../src/client/WebSearchCard.tsx'
  21. import type { AgentLoopCardState } from '../src/client/agent-loop-card-controller.ts'
  22. import type { BashCardState } from '../src/client/bash-card-controller.ts'
  23. import type { CardFieldState, CardShell } from '../src/client/card-form.ts'
  24. import type { ConfigurablePluginsTabState } from '../src/client/tab-store.ts'
  25. import type { WebSearchCardState } from '../src/client/web-search-card-controller.ts'
  26. import { en } from '../src/client/locales.ts'
  27. afterEach(cleanup)
  28. const t = (key: keyof typeof en) => en[key]
  29. /** A settled form: nothing staged, everything served. */
  30. const settled: CardShell = {
  31. available: true,
  32. writable: true,
  33. dirty: false,
  34. invalid: false,
  35. saving: false,
  36. failed: false,
  37. }
  38. /** One control's state, defaulting to an inherited value. */
  39. function field(text: string, rest: Partial<CardFieldState> = {}): CardFieldState {
  40. return { text, overridden: false, invalid: false, ...rest }
  41. }
  42. function cardActions() {
  43. return { edit: vi.fn(), resetField: vi.fn(), save: vi.fn(), discard: vi.fn() }
  44. }
  45. function renderSection(rows: readonly PluginsSettingsTabEntry[]) {
  46. const props = {
  47. t,
  48. useTabs: (selector: (value: readonly PluginsSettingsTabEntry[]) => unknown) => selector(rows),
  49. renderSlot: (_name: string, _owner: unknown, options: { only?: string }) => (
  50. <span>{options.only}</span>
  51. ),
  52. } as unknown as PluginsSettingsSectionProps
  53. render(<PluginsSettingsSection {...props} />)
  54. }
  55. /**
  56. * Render the tab over the namespaces it was told to dispatch, with `cards`
  57. * standing in for the slot ledger: a key it names renders that text, and one
  58. * it does not renders nothing, exactly as an unclaimed key does.
  59. */
  60. function renderConfigurable(namespaces: string[], cards: Record<string, string> = {}, loaded = true) {
  61. const store = createSnapshotStore<ConfigurablePluginsTabState>({ loaded, namespaces })
  62. const props = {
  63. t,
  64. useConfigurablePlugins: bindSnapshotSelector(store),
  65. renderSlot: (_name: string, _owner: object, opts?: { entryKey?: string }) => {
  66. const card = opts?.entryKey === undefined ? undefined : cards[opts.entryKey]
  67. return card === undefined ? null : <li>{card}</li>
  68. },
  69. } as unknown as ConfigurablePluginsTabProps
  70. render(<ConfigurablePluginsTab {...props} />)
  71. }
  72. function renderBash(state: Partial<BashCardState> = {}) {
  73. const store = createSnapshotStore<BashCardState>({
  74. ...settled,
  75. timeoutMs: field('60000'),
  76. maxOutputBytes: field('64000'),
  77. ...state,
  78. })
  79. const actions = cardActions()
  80. const props = { ...actions, t, useBashCard: bindSnapshotSelector(store) } as unknown as BashCardProps
  81. render(<BashCard {...props} />)
  82. return actions
  83. }
  84. describe('PluginsSettingsSection', () => {
  85. it('says so when no plugin contributed a tab', () => {
  86. renderSection([])
  87. expect(screen.getByText(en.empty)).toBeTruthy()
  88. expect(screen.queryByRole('tab')).toBeNull()
  89. })
  90. it('defaults to the first ordered tab and mounts another only after selection', () => {
  91. renderSection([
  92. { id: 'configurable', order: 0, label: en.configurableTab },
  93. { id: 'all', order: 10, label: 'Plugin list' },
  94. ])
  95. const configurable = screen.getByRole('tab', { name: en.configurableTab })
  96. const all = screen.getByRole('tab', { name: 'Plugin list' })
  97. expect(configurable.getAttribute('aria-selected')).toBe('true')
  98. expect(screen.getByText('configurable')).toBeTruthy()
  99. expect(screen.queryByText('all')).toBeNull()
  100. fireEvent.click(all)
  101. expect(all.getAttribute('aria-selected')).toBe('true')
  102. expect(screen.getByText('all')).toBeTruthy()
  103. expect(screen.getByText('configurable').closest('[role="tabpanel"]')).toHaveProperty('hidden', true)
  104. fireEvent.click(configurable)
  105. expect(configurable.getAttribute('aria-selected')).toBe('true')
  106. expect(screen.getByText('all').closest('[role="tabpanel"]')).toHaveProperty('hidden', true)
  107. })
  108. it('leads with its own heading and intro', () => {
  109. renderSection([{ id: 'configurable', order: 0, label: en.configurableTab }])
  110. expect(screen.getByRole('heading', { name: en.title })).toBeTruthy()
  111. expect(screen.getByText(en.intro)).toBeTruthy()
  112. })
  113. it('moves focus and selection with standard horizontal tab keys', () => {
  114. renderSection([
  115. { id: 'configurable', order: 0, label: en.configurableTab },
  116. { id: 'all', order: 10, label: 'Plugin list' },
  117. { id: 'diagnostics', order: 20, label: 'Diagnostics' },
  118. ])
  119. const configurable = screen.getByRole('tab', { name: en.configurableTab })
  120. const all = screen.getByRole('tab', { name: 'Plugin list' })
  121. const diagnostics = screen.getByRole('tab', { name: 'Diagnostics' })
  122. expect(configurable.getAttribute('tabindex')).toBe('0')
  123. expect(all.getAttribute('tabindex')).toBe('-1')
  124. configurable.focus()
  125. fireEvent.keyDown(configurable, { key: 'ArrowRight' })
  126. expect(document.activeElement).toBe(all)
  127. expect(all.getAttribute('aria-selected')).toBe('true')
  128. fireEvent.keyDown(all, { key: 'End' })
  129. expect(document.activeElement).toBe(diagnostics)
  130. fireEvent.keyDown(diagnostics, { key: 'ArrowRight' })
  131. expect(document.activeElement).toBe(configurable)
  132. fireEvent.keyDown(configurable, { key: 'ArrowLeft' })
  133. expect(document.activeElement).toBe(diagnostics)
  134. fireEvent.keyDown(diagnostics, { key: 'Home' })
  135. expect(document.activeElement).toBe(configurable)
  136. fireEvent.keyDown(configurable, { key: 'Escape' })
  137. expect(document.activeElement).toBe(configurable)
  138. expect(configurable.getAttribute('aria-selected')).toBe('true')
  139. })
  140. })
  141. describe('ConfigurablePluginsTab', () => {
  142. it('says so when no plugin contributed a card', () => {
  143. renderConfigurable([], { bash: 'shell' })
  144. expect(screen.getByText(en.empty)).toBeTruthy()
  145. expect(screen.queryByText('shell')).toBeNull()
  146. })
  147. it('withholds the empty line until the Host has answered once', () => {
  148. // An unanswered read is not the statement that this deployment configures
  149. // no plugin; saying it anyway would flash a wrong answer on every open.
  150. renderConfigurable([], { bash: 'shell' }, false)
  151. expect(screen.queryByText(en.empty)).toBeNull()
  152. })
  153. it('dispatches one card per namespace, keyed by it', () => {
  154. renderConfigurable(['bash', 'agent-loop'], { bash: 'shell', 'agent-loop': 'loop' })
  155. expect(screen.getAllByRole('listitem').map(item => item.textContent)).toEqual(['shell', 'loop'])
  156. expect(screen.queryByText(en.empty)).toBeNull()
  157. })
  158. })
  159. describe('BashCard', () => {
  160. it('renders nothing while its namespace is unavailable', () => {
  161. const { container } = render(<div />)
  162. renderBash({ available: false })
  163. expect(container.textContent).toBe('')
  164. expect(screen.queryByText(en.bashTitle)).toBeNull()
  165. })
  166. it('shows the plugin and reveals its fields only once expanded', () => {
  167. renderBash()
  168. expect(screen.getByText(en.bashTitle)).toBeTruthy()
  169. expect(screen.queryByLabelText(en.bashTimeoutMs)).toBeNull()
  170. fireEvent.click(screen.getByText(en.bashTitle))
  171. expect(screen.getByLabelText(en.bashTimeoutMs)).toBeTruthy()
  172. expect(screen.getByLabelText(en.bashMaxOutputBytes)).toBeTruthy()
  173. })
  174. it('stages an edit instead of writing it', () => {
  175. const actions = renderBash()
  176. fireEvent.click(screen.getByText(en.bashTitle))
  177. fireEvent.change(screen.getByLabelText(en.bashTimeoutMs), { target: { value: '9000' } })
  178. expect(actions.edit).toHaveBeenCalledWith('timeoutMs', '9000')
  179. expect(actions.save).not.toHaveBeenCalled()
  180. })
  181. it('offers the reset for an overridden field only', () => {
  182. const actions = renderBash({ timeoutMs: field('9000', { overridden: true }) })
  183. fireEvent.click(screen.getByText(en.bashTitle))
  184. // One badge and one reset: the output cap is still inherited.
  185. expect(screen.getAllByText(en.overridden)).toHaveLength(1)
  186. fireEvent.click(screen.getByRole('button', { name: en.reset }))
  187. expect(actions.resetField).toHaveBeenCalledWith('timeoutMs')
  188. })
  189. it('addresses each of its two fields separately', () => {
  190. const actions = renderBash({ maxOutputBytes: field('64000', { overridden: true }) })
  191. fireEvent.click(screen.getByText(en.bashTitle))
  192. fireEvent.change(screen.getByLabelText(en.bashMaxOutputBytes), { target: { value: '1024' } })
  193. fireEvent.click(screen.getByRole('button', { name: en.reset }))
  194. expect(actions.edit).toHaveBeenCalledWith('maxOutputBytes', '1024')
  195. expect(actions.resetField).toHaveBeenCalledWith('maxOutputBytes')
  196. })
  197. it('keeps save and discard inert until something is staged', () => {
  198. renderBash()
  199. fireEvent.click(screen.getByText(en.bashTitle))
  200. expect(screen.getByRole('button', { name: en.save })).toHaveProperty('disabled', true)
  201. expect(screen.getByRole('button', { name: en.discard })).toHaveProperty('disabled', true)
  202. expect(screen.queryByText(en.unsaved)).toBeNull()
  203. })
  204. it('writes the staged edits when saved, and drops them when discarded', () => {
  205. const actions = renderBash({ dirty: true, timeoutMs: field('9000', { overridden: true }) })
  206. fireEvent.click(screen.getByText(en.bashTitle))
  207. fireEvent.click(screen.getByRole('button', { name: en.save }))
  208. fireEvent.click(screen.getByRole('button', { name: en.discard }))
  209. expect(actions.save).toHaveBeenCalledOnce()
  210. expect(actions.discard).toHaveBeenCalledOnce()
  211. })
  212. it('marks a card holding unsaved edits, collapsed or not', () => {
  213. renderBash({ dirty: true })
  214. expect(screen.getByText(en.unsaved)).toBeTruthy()
  215. })
  216. it('blocks the save while a draft is invalid, and says why', () => {
  217. renderBash({ dirty: true, invalid: true, timeoutMs: field('soon', { invalid: true }) })
  218. fireEvent.click(screen.getByText(en.bashTitle))
  219. expect(screen.getByRole('button', { name: en.save })).toHaveProperty('disabled', true)
  220. expect(screen.getByRole('button', { name: en.discard })).toHaveProperty('disabled', false)
  221. expect(screen.getByText(en.invalidNumber)).toBeTruthy()
  222. })
  223. it('reports a save in flight and refuses another', () => {
  224. renderBash({ dirty: true, saving: true })
  225. fireEvent.click(screen.getByText(en.bashTitle))
  226. expect(screen.getByRole('button', { name: en.saving })).toHaveProperty('disabled', true)
  227. expect(screen.getByRole('button', { name: en.discard })).toHaveProperty('disabled', true)
  228. })
  229. it('reports a save the deployment did not accept', () => {
  230. renderBash({ dirty: true, failed: true })
  231. fireEvent.click(screen.getByText(en.bashTitle))
  232. expect(screen.getByText(en.saveFailed)).toBeTruthy()
  233. })
  234. it('says the document is read-only and disables its controls', () => {
  235. renderBash({ writable: false })
  236. fireEvent.click(screen.getByText(en.bashTitle))
  237. expect(screen.getByRole('status')).toHaveProperty('textContent', en.readOnly)
  238. expect(screen.getByLabelText(en.bashTimeoutMs)).toHaveProperty('disabled', true)
  239. })
  240. it('collapses again on a second click', () => {
  241. renderBash()
  242. fireEvent.click(screen.getByText(en.bashTitle))
  243. expect(screen.getByLabelText(en.bashTimeoutMs)).toBeTruthy()
  244. fireEvent.click(screen.getByText(en.bashTitle))
  245. expect(screen.queryByLabelText(en.bashTimeoutMs)).toBeNull()
  246. })
  247. })
  248. describe('AgentLoopCard', () => {
  249. it('stages and saves the only field it owns', () => {
  250. const store = createSnapshotStore<AgentLoopCardState>({
  251. ...settled,
  252. dirty: true,
  253. maxParallelToolCalls: field('10'),
  254. })
  255. const actions = cardActions()
  256. const props = {
  257. ...actions,
  258. t,
  259. useAgentLoopCard: bindSnapshotSelector(store),
  260. } as unknown as AgentLoopCardProps
  261. render(<AgentLoopCard {...props} />)
  262. fireEvent.click(screen.getByText(en.agentLoopTitle))
  263. fireEvent.change(screen.getByLabelText(en.agentLoopMaxParallel), { target: { value: '2' } })
  264. fireEvent.click(screen.getByRole('button', { name: en.save }))
  265. expect(actions.edit).toHaveBeenCalledWith('maxParallelToolCalls', '2')
  266. expect(actions.save).toHaveBeenCalledOnce()
  267. })
  268. it('stages a reset for the field it owns', () => {
  269. const store = createSnapshotStore<AgentLoopCardState>({
  270. ...settled,
  271. maxParallelToolCalls: field('2', { overridden: true }),
  272. })
  273. const actions = cardActions()
  274. const props = {
  275. ...actions,
  276. t,
  277. useAgentLoopCard: bindSnapshotSelector(store),
  278. } as unknown as AgentLoopCardProps
  279. render(<AgentLoopCard {...props} />)
  280. fireEvent.click(screen.getByText(en.agentLoopTitle))
  281. fireEvent.click(screen.getByRole('button', { name: en.reset }))
  282. expect(actions.resetField).toHaveBeenCalledWith('maxParallelToolCalls')
  283. })
  284. })
  285. describe('WebSearchCard', () => {
  286. function renderWebSearch(state: Partial<WebSearchCardState> = {}) {
  287. const store = createSnapshotStore<WebSearchCardState>({
  288. ...settled,
  289. baseURL: field(''),
  290. maxUses: field('5'),
  291. apiKey: field(''),
  292. apiKeyConfigured: false,
  293. apiKeyWritable: true,
  294. ...state,
  295. })
  296. const actions = cardActions()
  297. const props = { ...actions, t, useWebSearchCard: bindSnapshotSelector(store) } as unknown as WebSearchCardProps
  298. render(<WebSearchCard {...props} />)
  299. return actions
  300. }
  301. it('reports whether a key is configured without ever showing one', () => {
  302. renderWebSearch({ apiKeyConfigured: true })
  303. fireEvent.click(screen.getByText(en.webSearchTitle))
  304. expect(screen.getByText(en.webSearchApiKeySet)).toBeTruthy()
  305. expect(screen.getByLabelText(en.webSearchApiKey)).toHaveProperty('type', 'password')
  306. })
  307. it('keeps the key control usable while the settings document is read-only', () => {
  308. const actions = renderWebSearch({ writable: false })
  309. fireEvent.click(screen.getByText(en.webSearchTitle))
  310. const key = screen.getByLabelText(en.webSearchApiKey)
  311. expect(key).toHaveProperty('disabled', false)
  312. expect(screen.getByLabelText(en.webSearchBaseUrl)).toHaveProperty('disabled', true)
  313. fireEvent.change(key, { target: { value: 'ds-secret' } })
  314. expect(actions.edit).toHaveBeenCalledWith('apiKey', 'ds-secret')
  315. })
  316. it('disables the key control when the reference itself is not writable', () => {
  317. // A key coming from the process environment: the settings document is
  318. // writable, the credential is not.
  319. renderWebSearch({ apiKeyConfigured: true, apiKeyWritable: false })
  320. fireEvent.click(screen.getByText(en.webSearchTitle))
  321. expect(screen.getByLabelText(en.webSearchApiKey)).toHaveProperty('disabled', true)
  322. expect(screen.getByLabelText(en.webSearchBaseUrl)).toHaveProperty('disabled', false)
  323. })
  324. it('stages the endpoint, the search budget, and their resets', () => {
  325. const actions = renderWebSearch({
  326. baseURL: field('https://search.test/v1', { overridden: true }),
  327. maxUses: field('3', { overridden: true }),
  328. })
  329. fireEvent.click(screen.getByText(en.webSearchTitle))
  330. fireEvent.change(screen.getByLabelText(en.webSearchBaseUrl), { target: { value: 'https://other.test' } })
  331. fireEvent.change(screen.getByLabelText(en.webSearchMaxUses), { target: { value: '4' } })
  332. const resets = screen.getAllByRole('button', { name: en.reset })
  333. expect(resets).toHaveLength(2)
  334. for (const reset of resets) fireEvent.click(reset)
  335. expect(actions.edit.mock.calls).toEqual([
  336. ['baseURL', 'https://other.test'],
  337. ['maxUses', '4'],
  338. ])
  339. expect(actions.resetField.mock.calls).toEqual([['baseURL'], ['maxUses']])
  340. })
  341. })