tui-surface-host.test.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. import { describe, expect, it, vi } from 'vitest';
  2. import type { Terminal, TUI } from '../../src/tui/engine/public.js';
  3. import type { Component } from '../../src/tui/rendering/component.js';
  4. import {
  5. TuiSurfaceHost,
  6. type TuiFeatureScreen,
  7. type TuiSurfaceHostOptions,
  8. } from '../../src/tui/shell/surface-host.js';
  9. import {
  10. TuiOverlayRegularFeaturePresenter,
  11. type TuiRegularFeaturePresenter,
  12. } from '../../src/tui/shell/regular-feature-presenter.js';
  13. import { TuiInteractionSurface } from '../../src/tui/shell/interaction-surface.js';
  14. import { TuiInlinePanelHost } from '../../src/tui/shell/inline-panel.js';
  15. import { TuiActivityLine } from '../../src/tui/shell/activity-line.js';
  16. function component(lines: readonly string[]): Component {
  17. return {
  18. render: () => lines,
  19. invalidate: () => undefined,
  20. };
  21. }
  22. function screen(id: string, render: (width: number) => readonly string[]): TuiFeatureScreen {
  23. const layoutRoot: Component = { render, invalidate: () => undefined };
  24. return {
  25. id,
  26. layoutRoot,
  27. render: (width) => [...render(width)],
  28. invalidate: () => undefined,
  29. };
  30. }
  31. function createSurfaceHost(
  32. options: Omit<TuiSurfaceHostOptions, 'regularFeaturePresenter'> & {
  33. regularFeaturePresenter?: TuiRegularFeaturePresenter;
  34. },
  35. ): TuiSurfaceHost {
  36. return new TuiSurfaceHost({
  37. regularFeaturePresenter: {
  38. show: (_screen, focus) => ({ focus, close: () => undefined }),
  39. },
  40. ...options,
  41. });
  42. }
  43. function createRecordingPresenter(): {
  44. presenter: TuiRegularFeaturePresenter;
  45. presentations: Array<{
  46. id: string;
  47. focus: Component;
  48. close: ReturnType<typeof vi.fn>;
  49. }>;
  50. } {
  51. const presentations: Array<{
  52. id: string;
  53. focus: Component;
  54. close: ReturnType<typeof vi.fn>;
  55. }> = [];
  56. return {
  57. presenter: {
  58. show: (feature, focus) => {
  59. const presentation = { id: feature.id, focus, close: vi.fn() };
  60. presentations.push(presentation);
  61. return presentation;
  62. },
  63. },
  64. presentations,
  65. };
  66. }
  67. describe('TuiSurfaceHost', () => {
  68. it.each(['regular', 'fullscreen'] as const)(
  69. 'passes current terminal size to leaf features in %s',
  70. (mode) => {
  71. const terminal = { rows: 30 } as Terminal;
  72. const overlays: Component[] = [];
  73. const presenter = new TuiOverlayRegularFeaturePresenter(
  74. terminal,
  75. {
  76. showOverlay: (overlay: Component) => {
  77. overlays.push(overlay);
  78. return { hide: vi.fn() };
  79. },
  80. } as unknown as TUI,
  81. vi.fn(),
  82. );
  83. const renderViewport = vi.fn((width: number, height: number) => [`${width}x${height}`]);
  84. const host = createSurfaceHost({
  85. chat: { component: component(['chat']), focus: component([]) },
  86. chatMode: mode,
  87. mode: () => mode,
  88. viewportRows: () => terminal.rows,
  89. regularFeaturePresenter: presenter,
  90. setFocus: vi.fn(),
  91. requestRender: vi.fn(),
  92. });
  93. host.pushFeature({ screen: { ...screen('history', () => ['unbounded']), renderViewport } });
  94. const render = () => (mode === 'regular' ? overlays[0]!.render(40) : host.render(40));
  95. expect(render()).toEqual(
  96. mode === 'regular' ? ['40x30', ...Array(29).fill('')] : ['40x30'],
  97. );
  98. terminal.rows = 8;
  99. expect(render()).toEqual(mode === 'regular' ? ['40x8', ...Array(7).fill('')] : ['40x8']);
  100. host.dispose();
  101. },
  102. );
  103. it('invalidates only the mounted chat root in regular mode', () => {
  104. const chatInvalidate = vi.fn();
  105. const featureInvalidate = vi.fn();
  106. const host = createSurfaceHost({
  107. chat: {
  108. component: { render: () => ['chat'], invalidate: chatInvalidate },
  109. focus: component([]),
  110. },
  111. setFocus: vi.fn(),
  112. requestRender: vi.fn(),
  113. });
  114. const handle = host.pushFeature({
  115. screen: {
  116. ...screen('sessions', () => ['sessions']),
  117. invalidate: featureInvalidate,
  118. },
  119. });
  120. expect(chatInvalidate).not.toHaveBeenCalled();
  121. expect(featureInvalidate).toHaveBeenCalledOnce();
  122. chatInvalidate.mockClear();
  123. featureInvalidate.mockClear();
  124. host.invalidate();
  125. expect(chatInvalidate).toHaveBeenCalledOnce();
  126. expect(featureInvalidate).not.toHaveBeenCalled();
  127. chatInvalidate.mockClear();
  128. expect(handle.close()).toBe(true);
  129. expect(chatInvalidate).not.toHaveBeenCalled();
  130. });
  131. it('invalidates only the active fullscreen feature', () => {
  132. const chatInvalidate = vi.fn();
  133. const firstInvalidate = vi.fn();
  134. const secondInvalidate = vi.fn();
  135. const host = createSurfaceHost({
  136. chat: {
  137. component: { render: () => ['chat'], invalidate: chatInvalidate },
  138. focus: component([]),
  139. },
  140. chatMode: 'fullscreen',
  141. mode: () => 'fullscreen',
  142. setFocus: vi.fn(),
  143. requestRender: vi.fn(),
  144. });
  145. host.pushFeature({
  146. screen: { ...screen('first', () => ['first']), invalidate: firstInvalidate },
  147. });
  148. host.pushFeature({
  149. screen: { ...screen('second', () => ['second']), invalidate: secondInvalidate },
  150. });
  151. chatInvalidate.mockClear();
  152. firstInvalidate.mockClear();
  153. secondInvalidate.mockClear();
  154. host.invalidate();
  155. expect(chatInvalidate).not.toHaveBeenCalled();
  156. expect(firstInvalidate).not.toHaveBeenCalled();
  157. expect(secondInvalidate).toHaveBeenCalledOnce();
  158. });
  159. it('lets a feature screen own invalidation of its Pi layout root', () => {
  160. const overlays: Component[] = [];
  161. const tui = {
  162. showOverlay: vi.fn((overlay: Component) => {
  163. overlays.push(overlay);
  164. return { hide: vi.fn() };
  165. }),
  166. } as unknown as TUI;
  167. const layoutInvalidate = vi.fn();
  168. const screenInvalidate = vi.fn(() => layoutInvalidate());
  169. const feature = {
  170. id: 'sessions',
  171. layoutRoot: { render: () => ['layout'], invalidate: layoutInvalidate },
  172. render: () => ['sessions'],
  173. invalidate: screenInvalidate,
  174. };
  175. const presenter = new TuiOverlayRegularFeaturePresenter({ rows: 24 } as Terminal, tui, vi.fn());
  176. presenter.show(feature, feature);
  177. overlays[0]?.invalidate();
  178. expect(screenInvalidate).toHaveBeenCalledOnce();
  179. expect(layoutInvalidate).toHaveBeenCalledOnce();
  180. });
  181. it('stops periodic terminal renders while an interaction owns the viewport', () => {
  182. vi.useFakeTimers();
  183. try {
  184. const requestActivityRender = vi.fn();
  185. const activity = new TuiActivityLine(
  186. { phase: 'running', runId: 'turn-1' },
  187. { requestRender: requestActivityRender },
  188. );
  189. const host = new TuiInlinePanelHost();
  190. const surfaces = createSurfaceHost({
  191. chat: { component: component(['chat']), focus: component([]) },
  192. setFocus: vi.fn(),
  193. requestRender: vi.fn(),
  194. });
  195. const interaction = new TuiInteractionSurface(host, surfaces, vi.fn(), (active) =>
  196. activity.setAnimationPaused(active),
  197. );
  198. const panel = component(['question']);
  199. vi.advanceTimersByTime(160);
  200. expect(requestActivityRender).toHaveBeenCalledTimes(2);
  201. interaction.show(panel);
  202. requestActivityRender.mockClear();
  203. vi.advanceTimersByTime(320);
  204. expect(requestActivityRender).not.toHaveBeenCalled();
  205. interaction.close(panel);
  206. vi.advanceTimersByTime(80);
  207. expect(requestActivityRender).toHaveBeenCalledOnce();
  208. activity.dispose();
  209. } finally {
  210. vi.useRealTimers();
  211. }
  212. });
  213. it('keeps the default chat surface inline and persistent', () => {
  214. const chat = component(['chat']);
  215. const focus = component([]);
  216. const host = createSurfaceHost({
  217. chat: { component: chat, focus },
  218. setFocus: vi.fn(),
  219. requestRender: vi.fn(),
  220. });
  221. expect(host.getActiveSurface()).toEqual({ kind: 'chat', id: 'chat' });
  222. expect(host.render(40)).toEqual(['chat']);
  223. });
  224. it('presents a regular feature over the persistent chat document and restores chat focus on close', () => {
  225. const chat = component(['chat']);
  226. const chatFocus = component([]);
  227. const featureFocus = component([]);
  228. const setFocus = vi.fn();
  229. const requestRender = vi.fn();
  230. const { presenter, presentations } = createRecordingPresenter();
  231. const host = createSurfaceHost({
  232. chat: { component: chat, focus: chatFocus },
  233. regularFeaturePresenter: presenter,
  234. setFocus,
  235. requestRender,
  236. });
  237. const feature = screen('sessions', () => ['header', 'body', 'footer']);
  238. const handle = host.pushFeature({ screen: feature, focus: featureFocus });
  239. expect(host.getActiveSurface()).toEqual({ kind: 'feature', id: 'sessions' });
  240. expect(host.render(40)).toEqual(['chat']);
  241. expect(presentations.map(({ id }) => id)).toEqual(['sessions']);
  242. expect(setFocus).toHaveBeenLastCalledWith(featureFocus);
  243. expect(requestRender).toHaveBeenLastCalledWith();
  244. expect(handle.close()).toBe(true);
  245. expect(host.getActiveSurface()).toEqual({ kind: 'chat', id: 'chat' });
  246. expect(host.render(40)).toEqual(['chat']);
  247. expect(presentations[0]?.close).toHaveBeenCalledOnce();
  248. expect(setFocus).toHaveBeenLastCalledWith(chatFocus);
  249. expect(requestRender).toHaveBeenLastCalledWith();
  250. });
  251. it('uses a central stack for nested feature screens', () => {
  252. const chatFocus = component([]);
  253. const firstFocus = component([]);
  254. const secondFocus = component([]);
  255. const setFocus = vi.fn();
  256. const host = createSurfaceHost({
  257. chat: { component: component(['chat']), focus: chatFocus },
  258. setFocus,
  259. requestRender: vi.fn(),
  260. });
  261. const first = host.pushFeature({
  262. screen: screen('sessions', () => ['sessions']),
  263. focus: firstFocus,
  264. });
  265. const second = host.pushFeature({
  266. screen: screen('session-detail', () => ['detail']),
  267. focus: secondFocus,
  268. });
  269. expect(first.close()).toBe(false);
  270. expect(second.close()).toBe(true);
  271. expect(host.getActiveSurface()).toEqual({ kind: 'feature', id: 'sessions' });
  272. expect(setFocus).toHaveBeenLastCalledWith(firstFocus);
  273. expect(first.close()).toBe(true);
  274. expect(setFocus).toHaveBeenLastCalledWith(chatFocus);
  275. });
  276. it('keeps a regular renderer stable while a feature stack opens and closes', () => {
  277. let mode: 'regular' | 'fullscreen' = 'regular';
  278. const switchMode = vi.fn((next: typeof mode) => {
  279. mode = next;
  280. return true;
  281. });
  282. const host = createSurfaceHost({
  283. chat: { component: component(['chat']), focus: component([]) },
  284. mode: () => mode,
  285. setFocus: vi.fn(),
  286. requestRender: vi.fn(),
  287. switchMode,
  288. });
  289. const first = host.pushFeature({ screen: screen('first', () => ['first']) });
  290. const second = host.pushFeature({ screen: screen('second', () => ['second']) });
  291. expect(mode).toBe('regular');
  292. expect(switchMode).not.toHaveBeenCalled();
  293. second.close();
  294. first.close();
  295. expect(mode).toBe('regular');
  296. expect(switchMode).not.toHaveBeenCalled();
  297. });
  298. it('keeps chat and feature screens fullscreen when fullscreen is the configured chat mode', () => {
  299. const switchMode = vi.fn(() => true);
  300. const host = createSurfaceHost({
  301. chat: { component: component(['chat']), focus: component([]) },
  302. chatMode: 'fullscreen',
  303. mode: () => 'fullscreen',
  304. setFocus: vi.fn(),
  305. requestRender: vi.fn(),
  306. switchMode,
  307. });
  308. const feature = host.pushFeature({ screen: screen('sessions', () => ['sessions']) });
  309. expect(host.getChatMode()).toBe('fullscreen');
  310. expect(switchMode).not.toHaveBeenCalled();
  311. expect(feature.close()).toBe(true);
  312. expect(host.getActiveSurface()).toEqual({ kind: 'chat', id: 'chat' });
  313. expect(switchMode).not.toHaveBeenCalled();
  314. });
  315. it('switches the visible chat immediately when its configured mode changes', () => {
  316. const switchMode = vi.fn(() => true);
  317. const host = createSurfaceHost({
  318. chat: { component: component(['chat']), focus: component([]) },
  319. setFocus: vi.fn(),
  320. requestRender: vi.fn(),
  321. switchMode,
  322. });
  323. expect(host.setChatMode('fullscreen')).toBe(true);
  324. expect(host.getChatMode()).toBe('fullscreen');
  325. expect(switchMode).toHaveBeenCalledOnce();
  326. expect(switchMode).toHaveBeenCalledWith('fullscreen');
  327. });
  328. it('changes the renderer immediately while keeping the active feature in the selected mode', () => {
  329. let mode: 'regular' | 'fullscreen' = 'fullscreen';
  330. const switchMode = vi.fn((next: typeof mode) => {
  331. mode = next;
  332. return true;
  333. });
  334. const { presenter, presentations } = createRecordingPresenter();
  335. const host = createSurfaceHost({
  336. chat: { component: component(['chat']), focus: component([]) },
  337. chatMode: 'fullscreen',
  338. mode: () => mode,
  339. regularFeaturePresenter: presenter,
  340. setFocus: vi.fn(),
  341. requestRender: vi.fn(),
  342. switchMode,
  343. });
  344. const feature = host.pushFeature({ screen: screen('sessions', () => ['sessions']) });
  345. expect(host.setChatMode('regular')).toBe(true);
  346. expect(host.getChatMode()).toBe('regular');
  347. expect(switchMode).toHaveBeenCalledOnce();
  348. expect(switchMode).toHaveBeenCalledWith('regular');
  349. expect(presentations.map(({ id }) => id)).toEqual(['sessions']);
  350. expect(feature.close()).toBe(true);
  351. expect(switchMode).toHaveBeenCalledOnce();
  352. expect(presentations[0]?.close).toHaveBeenCalledOnce();
  353. });
  354. it('rolls back the configured chat mode when Pi refuses the renderer switch', () => {
  355. const switchMode = vi.fn(() => false);
  356. const host = createSurfaceHost({
  357. chat: { component: component(['chat']), focus: component([]) },
  358. setFocus: vi.fn(),
  359. requestRender: vi.fn(),
  360. switchMode,
  361. });
  362. expect(host.setChatMode('fullscreen')).toBe(false);
  363. expect(host.getChatMode()).toBe('regular');
  364. });
  365. it('rolls back the configured chat mode when the renderer switch throws', () => {
  366. const host = createSurfaceHost({
  367. chat: { component: component(['chat']), focus: component([]) },
  368. setFocus: vi.fn(),
  369. requestRender: vi.fn(),
  370. switchMode: () => {
  371. throw new Error('terminal restart failed');
  372. },
  373. });
  374. expect(() => host.setChatMode('fullscreen')).toThrow('terminal restart failed');
  375. expect(host.getChatMode()).toBe('regular');
  376. expect(host.getActiveSurface()).toEqual({ kind: 'chat', id: 'chat' });
  377. });
  378. it('restores fullscreen when a regular feature cannot be presented after a mode change', () => {
  379. let mode: 'regular' | 'fullscreen' = 'fullscreen';
  380. const switchMode = vi.fn((next: typeof mode) => {
  381. mode = next;
  382. return true;
  383. });
  384. const host = createSurfaceHost({
  385. chat: { component: component(['chat']), focus: component([]) },
  386. chatMode: 'fullscreen',
  387. mode: () => mode,
  388. regularFeaturePresenter: {
  389. show: () => {
  390. throw new Error('regular feature presentation failed');
  391. },
  392. },
  393. setFocus: vi.fn(),
  394. requestRender: vi.fn(),
  395. switchMode,
  396. });
  397. host.pushFeature({ screen: screen('sessions', () => ['sessions']) });
  398. expect(() => host.setChatMode('regular')).toThrow('regular feature presentation failed');
  399. expect(host.getChatMode()).toBe('fullscreen');
  400. expect(mode).toBe('fullscreen');
  401. expect(switchMode.mock.calls.map(([next]) => next)).toEqual(['regular', 'fullscreen']);
  402. expect(host.getActiveSurface()).toEqual({ kind: 'feature', id: 'sessions' });
  403. });
  404. it('does not mutate the feature stack when the regular presenter refuses a feature', () => {
  405. const host = createSurfaceHost({
  406. chat: { component: component(['chat']), focus: component([]) },
  407. regularFeaturePresenter: {
  408. show: () => {
  409. throw new Error('regular feature presentation failed');
  410. },
  411. },
  412. setFocus: vi.fn(),
  413. requestRender: vi.fn(),
  414. });
  415. const feature = screen('sessions', () => ['sessions']);
  416. expect(() => host.pushFeature({ screen: feature })).toThrow(
  417. 'regular feature presentation failed',
  418. );
  419. expect(host.getActiveSurface()).toEqual({ kind: 'chat', id: 'chat' });
  420. expect(host.render(40)).toEqual(['chat']);
  421. });
  422. it('restores a nested feature stack when presenting the previous feature throws on close', () => {
  423. let failNextPresentation = false;
  424. const presentations: Array<{ id: string; close: ReturnType<typeof vi.fn> }> = [];
  425. const presenter: TuiRegularFeaturePresenter = {
  426. show: (feature, focus) => {
  427. if (failNextPresentation) {
  428. failNextPresentation = false;
  429. throw new Error('regular feature presentation failed');
  430. }
  431. const presentation = { id: feature.id, focus, close: vi.fn() };
  432. presentations.push(presentation);
  433. return presentation;
  434. },
  435. };
  436. const firstDispose = vi.fn();
  437. const secondDispose = vi.fn();
  438. const host = createSurfaceHost({
  439. chat: { component: component(['chat']), focus: component([]) },
  440. regularFeaturePresenter: presenter,
  441. setFocus: vi.fn(),
  442. requestRender: vi.fn(),
  443. });
  444. const first = host.pushFeature({
  445. screen: { ...screen('first', () => ['first']), dispose: firstDispose },
  446. });
  447. const second = host.pushFeature({
  448. screen: { ...screen('second', () => ['second']), dispose: secondDispose },
  449. });
  450. failNextPresentation = true;
  451. expect(() => second.close()).toThrow('regular feature presentation failed');
  452. expect(host.getActiveSurface()).toEqual({ kind: 'feature', id: 'second' });
  453. expect(secondDispose).not.toHaveBeenCalled();
  454. expect(presentations.at(-1)?.id).toBe('second');
  455. expect(second.close()).toBe(true);
  456. expect(host.getActiveSurface()).toEqual({ kind: 'feature', id: 'first' });
  457. expect(secondDispose).toHaveBeenCalledOnce();
  458. expect(first.close()).toBe(true);
  459. expect(firstDispose).toHaveBeenCalledOnce();
  460. });
  461. it('defers chat focus changes until the feature stack closes', () => {
  462. const initialChatFocus = component([]);
  463. const pendingDecisionFocus = component([]);
  464. const featureFocus = component([]);
  465. const setFocus = vi.fn();
  466. const host = createSurfaceHost({
  467. chat: { component: component(['chat']), focus: initialChatFocus },
  468. setFocus,
  469. requestRender: vi.fn(),
  470. });
  471. const feature = host.pushFeature({
  472. screen: screen('sessions', () => ['sessions']),
  473. focus: featureFocus,
  474. });
  475. host.setChatFocus(pendingDecisionFocus);
  476. expect(setFocus).toHaveBeenCalledTimes(1);
  477. expect(setFocus).toHaveBeenLastCalledWith(featureFocus);
  478. feature.close();
  479. expect(setFocus).toHaveBeenLastCalledWith(pendingDecisionFocus);
  480. });
  481. it('restores the highest-priority chat layer around feature navigation', () => {
  482. const composer = component([]);
  483. const interactionFocus = component([]);
  484. const modalFocus = component([]);
  485. const featureFocus = component([]);
  486. const setFocus = vi.fn();
  487. const host = createSurfaceHost({
  488. chat: { component: component(['chat']), focus: composer },
  489. setFocus,
  490. requestRender: vi.fn(),
  491. });
  492. const interaction = host.pushChatLayer({
  493. id: 'interaction',
  494. focus: interactionFocus,
  495. priority: 100,
  496. });
  497. const modal = host.pushChatLayer({
  498. id: 'modal',
  499. focus: modalFocus,
  500. priority: 200,
  501. });
  502. const feature = host.pushFeature({
  503. screen: screen('sessions', () => ['sessions']),
  504. focus: featureFocus,
  505. });
  506. expect(setFocus).toHaveBeenLastCalledWith(featureFocus);
  507. feature.close();
  508. expect(setFocus).toHaveBeenLastCalledWith(modalFocus);
  509. modal.close();
  510. expect(setFocus).toHaveBeenLastCalledWith(interactionFocus);
  511. interaction.close();
  512. expect(setFocus).toHaveBeenLastCalledWith(composer);
  513. });
  514. it('temporarily lets a blocking chat layer preempt a feature screen', () => {
  515. const composer = component([]);
  516. const featureFocus = component([]);
  517. const decisionFocus = component([]);
  518. const setFocus = vi.fn();
  519. const switchMode = vi.fn(() => true);
  520. const { presenter, presentations } = createRecordingPresenter();
  521. const host = createSurfaceHost({
  522. chat: { component: component(['chat decision']), focus: composer },
  523. regularFeaturePresenter: presenter,
  524. setFocus,
  525. requestRender: vi.fn(),
  526. switchMode,
  527. });
  528. host.pushFeature({
  529. screen: screen('transcript', () => ['transcript inspector']),
  530. focus: featureFocus,
  531. });
  532. const decision = host.pushChatLayer({
  533. id: 'interaction',
  534. focus: decisionFocus,
  535. priority: 100,
  536. preemptsFeatures: true,
  537. });
  538. expect(host.getActiveSurface()).toEqual({ kind: 'chat', id: 'chat' });
  539. expect(host.render(40)[0]).toBe('chat decision');
  540. expect(setFocus).toHaveBeenLastCalledWith(decisionFocus);
  541. expect(presentations.map(({ id }) => id)).toEqual(['transcript']);
  542. expect(presentations[0]?.close).toHaveBeenCalledOnce();
  543. expect(switchMode).not.toHaveBeenCalled();
  544. decision.close();
  545. expect(host.getActiveSurface()).toEqual({ kind: 'feature', id: 'transcript' });
  546. expect(host.render(40)[0]).toBe('chat decision');
  547. expect(setFocus).toHaveBeenLastCalledWith(featureFocus);
  548. expect(presentations.map(({ id }) => id)).toEqual(['transcript', 'transcript']);
  549. expect(switchMode).not.toHaveBeenCalled();
  550. });
  551. it('replaces an active interaction without changing the configured fullscreen renderer', () => {
  552. let mode: 'regular' | 'fullscreen' = 'fullscreen';
  553. const switchMode = vi.fn((next: typeof mode) => {
  554. mode = next;
  555. return true;
  556. });
  557. const inline = new TuiInlinePanelHost();
  558. const first = component(['first decision']);
  559. const second = component(['second decision']);
  560. const setFocus = vi.fn();
  561. const followFullscreenBottom = vi.fn();
  562. const host = createSurfaceHost({
  563. chat: { component: inline, focus: component([]) },
  564. chatMode: 'fullscreen',
  565. mode: () => mode,
  566. setFocus,
  567. requestRender: vi.fn(),
  568. switchMode,
  569. });
  570. host.pushFeature({ screen: screen('transcript', () => ['transcript']) });
  571. const interaction = new TuiInteractionSurface(
  572. inline,
  573. host,
  574. vi.fn(),
  575. undefined,
  576. followFullscreenBottom,
  577. );
  578. interaction.show(first);
  579. expect(followFullscreenBottom).toHaveBeenCalledOnce();
  580. expect(mode).toBe('fullscreen');
  581. expect(switchMode).not.toHaveBeenCalled();
  582. interaction.show(second);
  583. expect(followFullscreenBottom).toHaveBeenCalledTimes(2);
  584. expect(switchMode).not.toHaveBeenCalled();
  585. expect(inline.current()).toBe(second);
  586. expect(setFocus).toHaveBeenLastCalledWith(second);
  587. });
  588. it('keeps an interaction mounted when restoring its preempted regular feature throws', () => {
  589. let failNextPresentation = false;
  590. const inline = new TuiInlinePanelHost();
  591. const host = createSurfaceHost({
  592. chat: { component: inline, focus: component([]) },
  593. regularFeaturePresenter: {
  594. show: (_feature, focus) => {
  595. if (failNextPresentation) {
  596. failNextPresentation = false;
  597. throw new Error('feature restore failed');
  598. }
  599. return { focus, close: () => undefined };
  600. },
  601. },
  602. setFocus: vi.fn(),
  603. requestRender: vi.fn(),
  604. });
  605. host.pushFeature({ screen: screen('transcript', () => ['transcript']) });
  606. const interaction = new TuiInteractionSurface(inline, host, vi.fn());
  607. const activePanel = component(['active decision']);
  608. interaction.show(activePanel);
  609. expect(host.getActiveSurface()).toEqual({ kind: 'chat', id: 'chat' });
  610. failNextPresentation = true;
  611. expect(() => interaction.close(activePanel)).toThrow('feature restore failed');
  612. expect(inline.isActive(activePanel)).toBe(true);
  613. expect(host.getActiveSurface()).toEqual({ kind: 'chat', id: 'chat' });
  614. expect(interaction.close(activePanel)).toBe(true);
  615. expect(inline.isActive()).toBe(false);
  616. expect(host.getActiveSurface()).toEqual({ kind: 'feature', id: 'transcript' });
  617. });
  618. it('keeps a blocking chat layer in the configured fullscreen chat mode', () => {
  619. const switchMode = vi.fn(() => true);
  620. const host = createSurfaceHost({
  621. chat: { component: component(['chat']), focus: component([]) },
  622. chatMode: 'fullscreen',
  623. mode: () => 'fullscreen',
  624. setFocus: vi.fn(),
  625. requestRender: vi.fn(),
  626. switchMode,
  627. });
  628. host.pushFeature({ screen: screen('transcript', () => ['transcript']) });
  629. const interaction = host.pushChatLayer({
  630. id: 'interaction',
  631. focus: component([]),
  632. priority: 100,
  633. preemptsFeatures: true,
  634. });
  635. expect(host.getActiveSurface()).toEqual({ kind: 'chat', id: 'chat' });
  636. expect(switchMode).not.toHaveBeenCalled();
  637. expect(interaction.close()).toBe(true);
  638. expect(host.getActiveSurface()).toEqual({ kind: 'feature', id: 'transcript' });
  639. expect(switchMode).not.toHaveBeenCalled();
  640. });
  641. it('passes the complete feature document to the Pi layout tree', () => {
  642. const host = createSurfaceHost({
  643. chat: { component: component(['chat']), focus: component([]) },
  644. chatMode: 'fullscreen',
  645. mode: () => 'fullscreen',
  646. setFocus: vi.fn(),
  647. requestRender: vi.fn(),
  648. });
  649. host.pushFeature({
  650. screen: screen('diff', () => ['one', 'two', 'three', 'four']),
  651. focus: component([]),
  652. });
  653. expect(host.render(40)).toEqual(['one', 'two', 'three', 'four']);
  654. });
  655. it('disposes feature screens and distinct focus owners exactly once', () => {
  656. const screenDispose = vi.fn();
  657. const focusDispose = vi.fn();
  658. const feature = {
  659. ...screen('sessions', () => ['sessions']),
  660. dispose: screenDispose,
  661. };
  662. const focus = {
  663. ...component([]),
  664. dispose: focusDispose,
  665. };
  666. const host = createSurfaceHost({
  667. chat: { component: component(['chat']), focus: component([]) },
  668. setFocus: vi.fn(),
  669. requestRender: vi.fn(),
  670. });
  671. const handle = host.pushFeature({ screen: feature, focus });
  672. expect(handle.close()).toBe(true);
  673. expect(handle.close()).toBe(false);
  674. expect(screenDispose).toHaveBeenCalledOnce();
  675. expect(focusDispose).toHaveBeenCalledOnce();
  676. });
  677. it('disposes every feature when clearing a nested stack', () => {
  678. const disposals = [vi.fn(), vi.fn()];
  679. const host = createSurfaceHost({
  680. chat: { component: component(['chat']), focus: component([]) },
  681. setFocus: vi.fn(),
  682. requestRender: vi.fn(),
  683. });
  684. host.pushFeature({
  685. screen: { ...screen('first', () => ['first']), dispose: disposals[0] },
  686. });
  687. host.pushFeature({
  688. screen: { ...screen('second', () => ['second']), dispose: disposals[1] },
  689. });
  690. host.clearFeatures();
  691. expect(disposals[0]).toHaveBeenCalledOnce();
  692. expect(disposals[1]).toHaveBeenCalledOnce();
  693. });
  694. });