|
| 1 | +/** |
| 2 | + * Copyright 2026 GoodRx, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { NextRequest } from 'next/server'; |
| 18 | + |
| 19 | +jest.mock('server/lib/get-user', () => ({ |
| 20 | + getRequestUserIdentity: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +jest.mock('server/services/agent/AdminService', () => ({ |
| 24 | + __esModule: true, |
| 25 | + default: { |
| 26 | + getThreadConversation: jest.fn(), |
| 27 | + }, |
| 28 | +})); |
| 29 | + |
| 30 | +import { GET } from './route'; |
| 31 | +import { getRequestUserIdentity } from 'server/lib/get-user'; |
| 32 | +import AgentAdminService from 'server/services/agent/AdminService'; |
| 33 | + |
| 34 | +const mockGetRequestUserIdentity = getRequestUserIdentity as jest.Mock; |
| 35 | +const mockGetThreadConversation = AgentAdminService.getThreadConversation as jest.Mock; |
| 36 | + |
| 37 | +function makeRequest(): NextRequest { |
| 38 | + return { |
| 39 | + headers: new Headers([['x-request-id', 'req-test']]), |
| 40 | + nextUrl: new URL('http://localhost/api/v2/ai/admin/agent/threads/thread-1/conversation'), |
| 41 | + } as unknown as NextRequest; |
| 42 | +} |
| 43 | + |
| 44 | +describe('GET /api/v2/ai/admin/agent/threads/[threadId]/conversation', () => { |
| 45 | + beforeEach(() => { |
| 46 | + jest.clearAllMocks(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns 401 when the requester is not authenticated', async () => { |
| 50 | + mockGetRequestUserIdentity.mockReturnValue(null); |
| 51 | + |
| 52 | + const response = await GET(makeRequest(), { params: { threadId: 'thread-1' } }); |
| 53 | + |
| 54 | + expect(response.status).toBe(401); |
| 55 | + await expect(response.json()).resolves.toMatchObject({ |
| 56 | + error: { message: 'Unauthorized' }, |
| 57 | + }); |
| 58 | + expect(mockGetThreadConversation).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns the canonical admin replay payload from the service', async () => { |
| 62 | + mockGetRequestUserIdentity.mockReturnValue({ |
| 63 | + userId: 'sample-admin', |
| 64 | + githubUsername: 'sample-admin', |
| 65 | + }); |
| 66 | + mockGetThreadConversation.mockResolvedValue({ |
| 67 | + session: { id: 'session-1' }, |
| 68 | + thread: { id: 'thread-1' }, |
| 69 | + messages: [ |
| 70 | + { |
| 71 | + id: 'message-1', |
| 72 | + role: 'user', |
| 73 | + parts: [{ type: 'text', text: 'Hi' }], |
| 74 | + }, |
| 75 | + ], |
| 76 | + runs: [], |
| 77 | + events: [], |
| 78 | + pendingActions: [], |
| 79 | + toolExecutions: [ |
| 80 | + { |
| 81 | + id: 'tool-1', |
| 82 | + toolCallId: 'tool-call-1', |
| 83 | + }, |
| 84 | + ], |
| 85 | + }); |
| 86 | + |
| 87 | + const response = await GET(makeRequest(), { params: { threadId: 'thread-1' } }); |
| 88 | + const body = await response.json(); |
| 89 | + |
| 90 | + expect(response.status).toBe(200); |
| 91 | + expect(mockGetThreadConversation).toHaveBeenCalledWith('thread-1'); |
| 92 | + expect(body.data.messages[0]).toEqual({ |
| 93 | + id: 'message-1', |
| 94 | + role: 'user', |
| 95 | + parts: [{ type: 'text', text: 'Hi' }], |
| 96 | + }); |
| 97 | + expect(body.data.toolExecutions[0]).toEqual({ |
| 98 | + id: 'tool-1', |
| 99 | + toolCallId: 'tool-call-1', |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it.each(['Agent thread not found', 'Agent session not found'])('returns 404 for %s', async (message) => { |
| 104 | + mockGetRequestUserIdentity.mockReturnValue({ |
| 105 | + userId: 'sample-admin', |
| 106 | + githubUsername: 'sample-admin', |
| 107 | + }); |
| 108 | + mockGetThreadConversation.mockRejectedValue(new Error(message)); |
| 109 | + |
| 110 | + const response = await GET(makeRequest(), { params: { threadId: 'missing-thread' } }); |
| 111 | + |
| 112 | + expect(response.status).toBe(404); |
| 113 | + await expect(response.json()).resolves.toMatchObject({ |
| 114 | + error: { message }, |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments