tui-plugin-manager.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { stripVTControlCharacters } from 'node:util';
  2. import { describe, expect, it, vi } from 'vitest';
  3. import { TuiPluginManager } from '../../src/tui/features/plugin/manager.js';
  4. import { CURSOR_MARKER } from '../../src/tui/engine/public.js';
  5. import { stripAnsi, visibleWidth } from '../../src/tui/rendering/text.js';
  6. const plugins = [
  7. {
  8. pluginId: 'docs@official',
  9. name: 'docs',
  10. displayName: 'Documents',
  11. marketplace: 'official' as const,
  12. description: 'Create and edit documents',
  13. installed: true,
  14. enabled: true,
  15. capabilities: { appCount: 0, mcpServerCount: 0, skillCount: 1 },
  16. },
  17. {
  18. pluginId: 'calendar@official',
  19. name: 'calendar',
  20. displayName: 'Calendar',
  21. marketplace: 'official' as const,
  22. installed: false,
  23. enabled: false,
  24. capabilities: { appCount: 1, mcpServerCount: 0, skillCount: 0 },
  25. },
  26. {
  27. pluginId: 'notes@local',
  28. name: 'notes',
  29. displayName: 'Notes',
  30. marketplace: 'local' as const,
  31. installed: true,
  32. enabled: false,
  33. capabilities: { appCount: 0, mcpServerCount: 1, skillCount: 0 },
  34. },
  35. ];
  36. function createManager() {
  37. const options = {
  38. plugins,
  39. onInstall: vi.fn(async (plugin) => ({ ...plugin, installed: true, enabled: true })),
  40. onRemove: vi.fn(async (plugin) => ({ ...plugin, installed: false, enabled: false })),
  41. onSetEnabled: vi.fn(async (plugin, enabled) => ({ ...plugin, enabled })),
  42. onRefresh: vi.fn(async () => plugins),
  43. onCancel: vi.fn(),
  44. requestRender: vi.fn(),
  45. };
  46. return { manager: new TuiPluginManager(options), options };
  47. }
  48. describe('TuiPluginManager', () => {
  49. it('renders a searchable logical catalog with source tabs and bounded lines', () => {
  50. const { manager } = createManager();
  51. const lines = manager.render(74);
  52. const rendered = stripVTControlCharacters(lines.join('\n'));
  53. expect(lines.every((line) => visibleWidth(line) <= 74)).toBe(true);
  54. expect(rendered).toContain('Plugins');
  55. expect(rendered).toContain('[All Plugins]');
  56. expect(rendered).toContain('Installed (2)');
  57. expect(rendered).toContain('Documents');
  58. expect(rendered).toContain('Calendar');
  59. });
  60. it('filters by typing, cycles tabs, and mutates the selected Plugin', async () => {
  61. const { manager, options } = createManager();
  62. manager.handleInput('c');
  63. manager.handleInput('a');
  64. expect(stripVTControlCharacters(manager.render(80).join('\n'))).toContain('Calendar');
  65. expect(stripVTControlCharacters(manager.render(80).join('\n'))).not.toContain('Documents');
  66. manager.handleInput('\r');
  67. await vi.waitFor(() =>
  68. expect(options.onInstall).toHaveBeenCalledWith(expect.objectContaining({ name: 'calendar' })),
  69. );
  70. expect(stripVTControlCharacters(manager.render(80).join('\n'))).toContain('[*]');
  71. manager.handleInput('\x1b');
  72. manager.handleInput('\t');
  73. expect(stripVTControlCharacters(manager.render(80).join('\n'))).toContain('[Installed (3)]');
  74. manager.handleInput(' ');
  75. await vi.waitFor(() => expect(options.onSetEnabled).toHaveBeenCalled());
  76. });
  77. it('loads an initial search query without interpreting spaces as actions', () => {
  78. const { options } = createManager();
  79. const manager = new TuiPluginManager({ ...options, initialQuery: 'office tools' });
  80. const rendered = stripVTControlCharacters(manager.render(80).join('\n'));
  81. expect(rendered).toContain('Search: office tools');
  82. expect(options.onSetEnabled).not.toHaveBeenCalled();
  83. });
  84. it('delegates Delete to the active Pi search Input', () => {
  85. const { manager, options } = createManager();
  86. manager.handleInput('d');
  87. manager.handleInput('o');
  88. manager.handleInput('c');
  89. manager.handleInput('x');
  90. manager.handleInput('\u001B[D');
  91. manager.handleInput('\u001B[3~');
  92. manager.handleInput('\u0001');
  93. manager.handleInput('\u0004');
  94. const rendered = stripVTControlCharacters(manager.render(80).join('\n'));
  95. expect(rendered).toContain('Search: oc');
  96. expect(options.onSetEnabled).not.toHaveBeenCalled();
  97. expect(options.onRemove).not.toHaveBeenCalled();
  98. });
  99. it('keeps Space as a Plugin action after filtering', async () => {
  100. const { manager, options } = createManager();
  101. manager.handleInput('d');
  102. manager.handleInput('o');
  103. manager.handleInput('c');
  104. manager.handleInput(' ');
  105. await vi.waitFor(() =>
  106. expect(options.onSetEnabled).toHaveBeenCalledWith(
  107. expect.objectContaining({ name: 'docs' }),
  108. false,
  109. ),
  110. );
  111. const rendered = stripVTControlCharacters(manager.render(80).join('\n'));
  112. expect(rendered).toContain('Search: doc');
  113. expect(rendered).toContain('[-] Documents');
  114. });
  115. it.each(['\u001B[32;2u', '\u001B[27;2;32~'])(
  116. 'keeps Shift+Space available for multi-word search with %j',
  117. (shiftSpace) => {
  118. const { manager, options } = createManager();
  119. for (const character of 'create') manager.handleInput(character);
  120. manager.handleInput(shiftSpace);
  121. for (const character of 'documents') manager.handleInput(character);
  122. const rendered = stripVTControlCharacters(manager.render(120).join('\n'));
  123. expect(rendered).toContain('Search: create documents');
  124. expect(options.onSetEnabled).not.toHaveBeenCalled();
  125. },
  126. );
  127. it('keeps Space and Delete as Plugin actions while the Pi search Input is empty', async () => {
  128. const { manager, options } = createManager();
  129. manager.handleInput(' ');
  130. await vi.waitFor(() => expect(options.onSetEnabled).toHaveBeenCalledOnce());
  131. manager.handleInput('\u001B[3~');
  132. await vi.waitFor(() => expect(options.onRemove).toHaveBeenCalledOnce());
  133. });
  134. it('uses the Pi Input cursor marker and accepts an IME commit as one input event', () => {
  135. const { manager } = createManager();
  136. manager.focused = true;
  137. manager.handleInput('文档');
  138. manager.handleInput('\u001B[D');
  139. manager.handleInput('新');
  140. const lines = manager.render(80);
  141. const searchLine = lines.find((line) => stripVTControlCharacters(line).startsWith('Search: '));
  142. expect(searchLine).toBeDefined();
  143. expect(searchLine).toContain(CURSOR_MARKER);
  144. expect(stripAnsi(searchLine ?? '')).toContain('Search: 文新档');
  145. expect(visibleWidth((searchLine ?? '').slice(0, searchLine?.indexOf(CURSOR_MARKER)))).toBe(12);
  146. });
  147. it('shows mutation and refresh failures without losing state or remaining busy', async () => {
  148. const { options } = createManager();
  149. options.onSetEnabled
  150. .mockRejectedValueOnce(new Error('permission denied'))
  151. .mockImplementation(async (plugin, enabled) => ({ ...plugin, enabled }));
  152. options.onRefresh.mockRejectedValueOnce(new Error('registry unavailable'));
  153. const manager = new TuiPluginManager(options);
  154. manager.handleInput(' ');
  155. await vi.waitFor(() => expect(options.onSetEnabled).toHaveBeenCalledTimes(1));
  156. let rendered = stripVTControlCharacters(manager.render(80).join('\n'));
  157. expect(rendered).toContain("Couldn't update Documents");
  158. expect(rendered).toContain('[*] Documents');
  159. manager.handleInput(' ');
  160. await vi.waitFor(() => expect(options.onSetEnabled).toHaveBeenCalledTimes(2));
  161. rendered = stripVTControlCharacters(manager.render(80).join('\n'));
  162. expect(rendered).toContain('[-] Documents');
  163. manager.handleInput('\x12');
  164. await vi.waitFor(() => expect(options.onRefresh).toHaveBeenCalledTimes(1));
  165. rendered = stripVTControlCharacters(manager.render(80).join('\n'));
  166. expect(rendered).toContain("Couldn't refresh Plugins");
  167. expect(rendered).toContain('Documents');
  168. manager.handleInput('\x12');
  169. await vi.waitFor(() => expect(options.onRefresh).toHaveBeenCalledTimes(2));
  170. expect(stripVTControlCharacters(manager.render(80).join('\n'))).toContain(
  171. 'Plugin catalogs refreshed.',
  172. );
  173. });
  174. it('directs an incomplete Plugin login back through /login', async () => {
  175. const { options } = createManager();
  176. options.onSetEnabled.mockRejectedValueOnce(
  177. Object.assign(new Error('PLUGIN_AUTH_REQUIRED'), { code: 'PLUGIN_AUTH_REQUIRED' }),
  178. );
  179. const manager = new TuiPluginManager(options);
  180. manager.handleInput(' ');
  181. await vi.waitFor(() => expect(options.onSetEnabled).toHaveBeenCalledOnce());
  182. const rendered = stripVTControlCharacters(manager.render(100).join('\n'));
  183. expect(rendered).toContain('Run /login, then retry.');
  184. });
  185. it('ignores a mutation result after the manager is disposed', async () => {
  186. let resolveMutation: ((plugin: (typeof plugins)[number]) => void) | undefined;
  187. const mutation = new Promise<(typeof plugins)[number]>((resolve) => {
  188. resolveMutation = resolve;
  189. });
  190. const { manager, options } = createManager();
  191. options.onSetEnabled.mockReturnValueOnce(mutation);
  192. manager.handleInput(' ');
  193. await vi.waitFor(() => expect(options.onSetEnabled).toHaveBeenCalledOnce());
  194. const rendersBeforeDispose = options.requestRender.mock.calls.length;
  195. manager.dispose();
  196. const installedPlugin = plugins.find((plugin) => plugin.pluginId === 'docs@official');
  197. if (!installedPlugin) throw new Error('Missing installed Plugin fixture.');
  198. resolveMutation?.({ ...installedPlugin, enabled: false });
  199. await mutation;
  200. await Promise.resolve();
  201. expect(options.requestRender).toHaveBeenCalledTimes(rendersBeforeDispose);
  202. expect(stripVTControlCharacters(manager.render(80).join('\n'))).toContain('[*] Documents');
  203. });
  204. });