|
| 1 | +import { describe, expect, it, beforeEach, vi } from 'vitest'; |
| 2 | + |
| 3 | +const { convertToOutputItemMock, getInputItemsMock } = vi.hoisted(() => ({ |
| 4 | + convertToOutputItemMock: vi.fn(), |
| 5 | + getInputItemsMock: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock('../src/openaiResponsesModel', () => ({ |
| 9 | + convertToOutputItem: convertToOutputItemMock, |
| 10 | + getInputItems: getInputItemsMock, |
| 11 | +})); |
| 12 | + |
| 13 | +import { OpenAIConversationsSession } from '../src/memory/openaiConversationsSession'; |
| 14 | + |
| 15 | +describe('OpenAIConversationsSession', () => { |
| 16 | + beforeEach(() => { |
| 17 | + convertToOutputItemMock.mockReset(); |
| 18 | + getInputItemsMock.mockReset(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('enforces the item limit after converting response items', async () => { |
| 22 | + convertToOutputItemMock.mockImplementation((raw) => { |
| 23 | + const id = raw[0]?.id ?? 'response'; |
| 24 | + return [ |
| 25 | + { |
| 26 | + id: `${id}-msg-1`, |
| 27 | + type: 'message', |
| 28 | + role: 'assistant', |
| 29 | + content: [], |
| 30 | + }, |
| 31 | + { |
| 32 | + id: `${id}-msg-2`, |
| 33 | + type: 'message', |
| 34 | + role: 'assistant', |
| 35 | + content: [], |
| 36 | + }, |
| 37 | + { |
| 38 | + id: `${id}-msg-3`, |
| 39 | + type: 'message', |
| 40 | + role: 'assistant', |
| 41 | + content: [], |
| 42 | + }, |
| 43 | + ] as any; |
| 44 | + }); |
| 45 | + |
| 46 | + const items = [ |
| 47 | + { |
| 48 | + type: 'message', |
| 49 | + role: 'assistant', |
| 50 | + id: 'resp-1', |
| 51 | + content: [], |
| 52 | + }, |
| 53 | + { |
| 54 | + type: 'message', |
| 55 | + role: 'assistant', |
| 56 | + id: 'resp-0', |
| 57 | + content: [], |
| 58 | + }, |
| 59 | + ]; |
| 60 | + |
| 61 | + const list = vi.fn(() => ({ |
| 62 | + async *[Symbol.asyncIterator]() { |
| 63 | + for (const item of items) { |
| 64 | + yield item as any; |
| 65 | + } |
| 66 | + }, |
| 67 | + })); |
| 68 | + |
| 69 | + const session = new OpenAIConversationsSession({ |
| 70 | + client: { |
| 71 | + conversations: { |
| 72 | + items: { |
| 73 | + list, |
| 74 | + create: vi.fn(), |
| 75 | + delete: vi.fn(), |
| 76 | + }, |
| 77 | + create: vi.fn(), |
| 78 | + delete: vi.fn(), |
| 79 | + }, |
| 80 | + } as any, |
| 81 | + conversationId: 'conv-123', |
| 82 | + }); |
| 83 | + |
| 84 | + const result = await session.getItems(2); |
| 85 | + |
| 86 | + expect(list).toHaveBeenCalledWith('conv-123', { limit: 2, order: 'desc' }); |
| 87 | + expect(convertToOutputItemMock).toHaveBeenCalledTimes(1); |
| 88 | + expect(result).toHaveLength(2); |
| 89 | + expect(result.map((item: any) => item.id)).toEqual([ |
| 90 | + 'resp-1-msg-2', |
| 91 | + 'resp-1-msg-1', |
| 92 | + ]); |
| 93 | + }); |
| 94 | +}); |
0 commit comments