background.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { describe, expect, mock, test } from 'bun:test';
  2. import type { PluginConfig } from '../config';
  3. import { createBackgroundTools } from './background';
  4. function createMockManager() {
  5. return {
  6. isAgentAllowed: mock(() => true),
  7. getAllowedSubagents: mock(() => ['oracle']),
  8. launch: mock(
  9. (opts: {
  10. agent: string;
  11. prompt: string;
  12. description: string;
  13. parentSessionId: string;
  14. }) => ({
  15. id: 'bg_test1234',
  16. sessionId: undefined,
  17. description: opts.description,
  18. agent: opts.agent,
  19. status: 'pending',
  20. startedAt: new Date(),
  21. config: { maxConcurrentStarts: 10 },
  22. parentSessionId: opts.parentSessionId,
  23. prompt: opts.prompt,
  24. }),
  25. ),
  26. getResult: mock(() => null),
  27. waitForCompletion: mock(async () => null),
  28. cancel: mock(() => 0),
  29. };
  30. }
  31. describe('createBackgroundTools displayName runtime aliasing', () => {
  32. test('resolves displayName alias for background_task direct invocation', async () => {
  33. const manager = createMockManager();
  34. const config: PluginConfig = {
  35. agents: {
  36. oracle: { displayName: 'advisor' },
  37. },
  38. };
  39. const tools = createBackgroundTools(
  40. {} as any,
  41. manager as any,
  42. undefined,
  43. config,
  44. );
  45. const result = await tools.background_task.execute(
  46. {
  47. agent: 'advisor',
  48. prompt: 'Analyze this architecture',
  49. description: 'Architecture analysis',
  50. },
  51. { sessionID: 'session-1' } as any,
  52. );
  53. expect(manager.isAgentAllowed).toHaveBeenCalledWith('session-1', 'oracle');
  54. expect(manager.launch).toHaveBeenCalledWith({
  55. agent: 'oracle',
  56. prompt: 'Analyze this architecture',
  57. description: 'Architecture analysis',
  58. parentSessionId: 'session-1',
  59. });
  60. expect(result).toContain('Agent: oracle');
  61. });
  62. test('keeps internal agent names working for background_task', async () => {
  63. const manager = createMockManager();
  64. const config: PluginConfig = {
  65. agents: {
  66. oracle: { displayName: 'advisor' },
  67. },
  68. };
  69. const tools = createBackgroundTools(
  70. {} as any,
  71. manager as any,
  72. undefined,
  73. config,
  74. );
  75. await tools.background_task.execute(
  76. {
  77. agent: 'oracle',
  78. prompt: 'Analyze this architecture',
  79. description: 'Architecture analysis',
  80. },
  81. { sessionID: 'session-1' } as any,
  82. );
  83. expect(manager.isAgentAllowed).toHaveBeenCalledWith('session-1', 'oracle');
  84. expect(manager.launch).toHaveBeenCalledWith({
  85. agent: 'oracle',
  86. prompt: 'Analyze this architecture',
  87. description: 'Architecture analysis',
  88. parentSessionId: 'session-1',
  89. });
  90. });
  91. });