|
| 1 | +import { describe, test, expect, mock, beforeEach } from 'bun:test' |
| 2 | + |
| 3 | +const mockManuallyExtract = mock( |
| 4 | + (): Promise<any> => Promise.resolve({ success: true }), |
| 5 | +) |
| 6 | +const mockGetContent = mock( |
| 7 | + (): Promise<any> => Promise.resolve('# Session Summary\n\nDid some work.'), |
| 8 | +) |
| 9 | + |
| 10 | +mock.module( |
| 11 | + require.resolve('../../../services/SessionMemory/sessionMemory.js'), |
| 12 | + () => ({ |
| 13 | + manuallyExtractSessionMemory: mockManuallyExtract, |
| 14 | + }), |
| 15 | +) |
| 16 | +mock.module( |
| 17 | + require.resolve('../../../services/SessionMemory/sessionMemoryUtils.js'), |
| 18 | + () => ({ |
| 19 | + getSessionMemoryContent: mockGetContent, |
| 20 | + }), |
| 21 | +) |
| 22 | + |
| 23 | +const { default: summaryCommand } = await import('../index.js') |
| 24 | + |
| 25 | +const baseContext = { |
| 26 | + messages: [{ type: 'user', role: 'user', content: 'hello' }], |
| 27 | + options: { tools: [], mainLoopModel: 'test' }, |
| 28 | + setMessages: () => {}, |
| 29 | + onChangeAPIKey: () => {}, |
| 30 | +} as any |
| 31 | + |
| 32 | +async function callSummary(ctx = baseContext) { |
| 33 | + const mod = await summaryCommand.load() |
| 34 | + return mod.call('', ctx) |
| 35 | +} |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + mockManuallyExtract.mockReset() |
| 39 | + mockGetContent.mockReset() |
| 40 | + mockManuallyExtract.mockImplementation(() => |
| 41 | + Promise.resolve({ success: true }), |
| 42 | + ) |
| 43 | + mockGetContent.mockImplementation(() => |
| 44 | + Promise.resolve('# Session Summary\n\nDid some work.'), |
| 45 | + ) |
| 46 | +}) |
| 47 | + |
| 48 | +describe('summary command', () => { |
| 49 | + test('command metadata', () => { |
| 50 | + expect(summaryCommand.name).toBe('summary') |
| 51 | + expect(summaryCommand.type).toBe('local') |
| 52 | + expect(summaryCommand.isHidden).toBe(false) |
| 53 | + expect(typeof summaryCommand.load).toBe('function') |
| 54 | + }) |
| 55 | + |
| 56 | + test('refreshes and displays summary', async () => { |
| 57 | + const result = await callSummary() |
| 58 | + expect(result.type).toBe('text') |
| 59 | + expect((result as any).value).toContain('Session summary updated.') |
| 60 | + expect((result as any).value).toContain('Did some work.') |
| 61 | + expect(mockManuallyExtract).toHaveBeenCalled() |
| 62 | + }) |
| 63 | + |
| 64 | + test('handles extraction failure', async () => { |
| 65 | + mockManuallyExtract.mockImplementation(() => |
| 66 | + Promise.resolve({ success: false, error: 'timeout' }), |
| 67 | + ) |
| 68 | + const result = await callSummary() |
| 69 | + expect((result as any).value).toContain( |
| 70 | + 'Failed to generate session summary', |
| 71 | + ) |
| 72 | + expect((result as any).value).toContain('timeout') |
| 73 | + }) |
| 74 | + |
| 75 | + test('handles empty content after extraction', async () => { |
| 76 | + mockGetContent.mockImplementation(() => Promise.resolve('')) |
| 77 | + const result = await callSummary() |
| 78 | + expect((result as any).value).toContain('content is empty') |
| 79 | + }) |
| 80 | + |
| 81 | + test('handles null content after extraction', async () => { |
| 82 | + mockGetContent.mockImplementation(() => Promise.resolve(null)) |
| 83 | + const result = await callSummary() |
| 84 | + expect((result as any).value).toContain('content is empty') |
| 85 | + }) |
| 86 | + |
| 87 | + test('handles no messages', async () => { |
| 88 | + const result = await callSummary({ ...baseContext, messages: [] }) |
| 89 | + expect((result as any).value).toBe('No messages to summarize.') |
| 90 | + }) |
| 91 | +}) |
0 commit comments