|
| 1 | +import { type ListMemoryRecordsOptions, listMemoryRecords } from '../list-memory-records'; |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +const { mockSend, capturedInput } = vi.hoisted(() => { |
| 5 | + const captured: { value: unknown } = { value: null }; |
| 6 | + return { |
| 7 | + mockSend: vi.fn(), |
| 8 | + capturedInput: captured, |
| 9 | + }; |
| 10 | +}); |
| 11 | + |
| 12 | +vi.mock('@aws-sdk/client-bedrock-agentcore', () => ({ |
| 13 | + BedrockAgentCoreClient: class { |
| 14 | + send = mockSend; |
| 15 | + }, |
| 16 | + ListMemoryRecordsCommand: class { |
| 17 | + constructor(public input: unknown) { |
| 18 | + capturedInput.value = input; |
| 19 | + } |
| 20 | + }, |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock('../../../aws', () => ({ |
| 24 | + getCredentialProvider: vi.fn().mockReturnValue({}), |
| 25 | +})); |
| 26 | + |
| 27 | +describe('listMemoryRecords', () => { |
| 28 | + afterEach(() => { |
| 29 | + vi.clearAllMocks(); |
| 30 | + capturedInput.value = null; |
| 31 | + }); |
| 32 | + |
| 33 | + it('rejects when both namespace and namespacePath are provided', async () => { |
| 34 | + // Bypassing the discriminated union to simulate a caller from JS or the web UI. |
| 35 | + const options = { |
| 36 | + region: 'us-west-2', |
| 37 | + memoryId: 'mem-1', |
| 38 | + namespace: '/a/', |
| 39 | + namespacePath: '/b/', |
| 40 | + } as unknown as ListMemoryRecordsOptions; |
| 41 | + |
| 42 | + const result = await listMemoryRecords(options); |
| 43 | + |
| 44 | + expect(result.success).toBe(false); |
| 45 | + expect((result as { error: Error }).error.message).toContain('mutually exclusive'); |
| 46 | + expect(mockSend).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('rejects when neither namespace nor namespacePath is provided', async () => { |
| 50 | + const options = { region: 'us-west-2', memoryId: 'mem-1' } as unknown as ListMemoryRecordsOptions; |
| 51 | + |
| 52 | + const result = await listMemoryRecords(options); |
| 53 | + |
| 54 | + expect(result.success).toBe(false); |
| 55 | + expect((result as { error: Error }).error.message).toContain('Either'); |
| 56 | + expect(mockSend).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('rejects when namespace is an empty string (treated as not provided)', async () => { |
| 60 | + const options = { region: 'us-west-2', memoryId: 'mem-1', namespace: '' } as unknown as ListMemoryRecordsOptions; |
| 61 | + |
| 62 | + const result = await listMemoryRecords(options); |
| 63 | + |
| 64 | + expect(result.success).toBe(false); |
| 65 | + expect((result as { error: Error }).error.message).toContain('Either'); |
| 66 | + expect(mockSend).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('forwards namespace to the SDK when only namespace is provided', async () => { |
| 70 | + mockSend.mockResolvedValueOnce({ memoryRecordSummaries: [], nextToken: undefined }); |
| 71 | + |
| 72 | + const result = await listMemoryRecords({ |
| 73 | + region: 'us-west-2', |
| 74 | + memoryId: 'mem-1', |
| 75 | + namespace: '/users/42/facts', |
| 76 | + }); |
| 77 | + |
| 78 | + expect(result.success).toBe(true); |
| 79 | + expect(capturedInput.value).toMatchObject({ |
| 80 | + memoryId: 'mem-1', |
| 81 | + namespace: '/users/42/facts', |
| 82 | + maxResults: 50, |
| 83 | + }); |
| 84 | + expect((capturedInput.value as { namespacePath?: string }).namespacePath).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('forwards namespacePath to the SDK when only namespacePath is provided', async () => { |
| 88 | + mockSend.mockResolvedValueOnce({ memoryRecordSummaries: [], nextToken: undefined }); |
| 89 | + |
| 90 | + const result = await listMemoryRecords({ |
| 91 | + region: 'us-west-2', |
| 92 | + memoryId: 'mem-1', |
| 93 | + namespacePath: '/users/42/', |
| 94 | + }); |
| 95 | + |
| 96 | + expect(result.success).toBe(true); |
| 97 | + expect(capturedInput.value).toMatchObject({ |
| 98 | + memoryId: 'mem-1', |
| 99 | + namespacePath: '/users/42/', |
| 100 | + }); |
| 101 | + expect((capturedInput.value as { namespace?: string }).namespace).toBeUndefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('parses memory record summaries into the result shape', async () => { |
| 105 | + const createdAt = new Date('2026-05-13T00:00:00Z'); |
| 106 | + mockSend.mockResolvedValueOnce({ |
| 107 | + memoryRecordSummaries: [ |
| 108 | + { |
| 109 | + memoryRecordId: 'rec-1', |
| 110 | + content: { text: 'hello' }, |
| 111 | + memoryStrategyId: 'strat-1', |
| 112 | + namespaces: ['/users/42/facts'], |
| 113 | + createdAt, |
| 114 | + score: 0.87, |
| 115 | + metadata: { source: { stringValue: 'chat' } }, |
| 116 | + }, |
| 117 | + ], |
| 118 | + nextToken: 'next', |
| 119 | + }); |
| 120 | + |
| 121 | + const result = await listMemoryRecords({ |
| 122 | + region: 'us-west-2', |
| 123 | + memoryId: 'mem-1', |
| 124 | + namespace: '/users/42/facts', |
| 125 | + }); |
| 126 | + |
| 127 | + expect(result.success).toBe(true); |
| 128 | + if (!result.success) return; |
| 129 | + expect(result.nextToken).toBe('next'); |
| 130 | + expect(result.records).toEqual([ |
| 131 | + { |
| 132 | + memoryRecordId: 'rec-1', |
| 133 | + content: 'hello', |
| 134 | + memoryStrategyId: 'strat-1', |
| 135 | + namespaces: ['/users/42/facts'], |
| 136 | + createdAt: createdAt.toISOString(), |
| 137 | + score: 0.87, |
| 138 | + metadata: { source: 'chat' }, |
| 139 | + }, |
| 140 | + ]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('maps ResourceNotFoundException to a user-friendly error', async () => { |
| 144 | + const err = new Error('not found'); |
| 145 | + err.name = 'ResourceNotFoundException'; |
| 146 | + mockSend.mockRejectedValueOnce(err); |
| 147 | + |
| 148 | + const result = await listMemoryRecords({ |
| 149 | + region: 'us-west-2', |
| 150 | + memoryId: 'missing', |
| 151 | + namespace: '/a/', |
| 152 | + }); |
| 153 | + |
| 154 | + expect(result.success).toBe(false); |
| 155 | + expect((result as { error: Error }).error.message).toContain("Memory 'missing' not found"); |
| 156 | + }); |
| 157 | + |
| 158 | + it('returns the SDK error message for other failures', async () => { |
| 159 | + mockSend.mockRejectedValueOnce(new Error('network down')); |
| 160 | + |
| 161 | + const result = await listMemoryRecords({ |
| 162 | + region: 'us-west-2', |
| 163 | + memoryId: 'mem-1', |
| 164 | + namespace: '/a/', |
| 165 | + }); |
| 166 | + |
| 167 | + expect(result.success).toBe(false); |
| 168 | + expect((result as { error: Error }).error.message).toBe('network down'); |
| 169 | + }); |
| 170 | +}); |
0 commit comments