|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { memory } from '@sim/db/schema' |
| 6 | +import { |
| 7 | + createMockRequest, |
| 8 | + hybridAuthMockFns, |
| 9 | + queueTableRows, |
| 10 | + resetDbChainMock, |
| 11 | +} from '@sim/testing' |
| 12 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 13 | +import { AuthType } from '@/lib/auth/hybrid' |
| 14 | +import { |
| 15 | + PRIVATE_TOOL_METADATA_REQUEST_HEADER, |
| 16 | + PRIVATE_TOOL_METADATA_RESPONSE_HEADER, |
| 17 | + RESOLVED_SECRET_PROVENANCE_FIELD, |
| 18 | + RESOLVED_SECRET_PROVENANCE_METADATA_V1, |
| 19 | +} from '@/lib/execution/private-tool-metadata' |
| 20 | + |
| 21 | +const { mockCheckWorkspaceAccess } = vi.hoisted(() => ({ |
| 22 | + mockCheckWorkspaceAccess: vi.fn(), |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/lib/workspaces/permissions/utils', () => ({ |
| 26 | + checkWorkspaceAccess: mockCheckWorkspaceAccess, |
| 27 | +})) |
| 28 | + |
| 29 | +import { GET } from '@/app/api/memory/[id]/route' |
| 30 | + |
| 31 | +const WORKSPACE_ID = '11111111-1111-4111-8111-111111111111' |
| 32 | +const CONTEXT = { params: Promise.resolve({ id: 'missing-conversation' }) } |
| 33 | + |
| 34 | +describe('GET /api/memory/[id]', () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks() |
| 37 | + resetDbChainMock() |
| 38 | + hybridAuthMockFns.mockCheckInternalAuth.mockResolvedValue({ |
| 39 | + success: true, |
| 40 | + userId: 'user-1', |
| 41 | + authType: AuthType.INTERNAL_JWT, |
| 42 | + }) |
| 43 | + mockCheckWorkspaceAccess.mockResolvedValue({ exists: true, hasAccess: true }) |
| 44 | + queueTableRows(memory, []) |
| 45 | + }) |
| 46 | + |
| 47 | + it('returns verified exact-empty metadata when a tool lookup has no matching memory', async () => { |
| 48 | + const response = await GET( |
| 49 | + createMockRequest( |
| 50 | + 'GET', |
| 51 | + undefined, |
| 52 | + { |
| 53 | + [PRIVATE_TOOL_METADATA_REQUEST_HEADER]: RESOLVED_SECRET_PROVENANCE_METADATA_V1, |
| 54 | + }, |
| 55 | + `http://localhost:3000/api/memory/missing-conversation?workspaceId=${WORKSPACE_ID}` |
| 56 | + ), |
| 57 | + CONTEXT |
| 58 | + ) |
| 59 | + |
| 60 | + expect(response.status).toBe(200) |
| 61 | + expect(response.headers.get(PRIVATE_TOOL_METADATA_RESPONSE_HEADER)).toBe( |
| 62 | + RESOLVED_SECRET_PROVENANCE_METADATA_V1 |
| 63 | + ) |
| 64 | + expect(await response.json()).toEqual({ |
| 65 | + success: true, |
| 66 | + data: null, |
| 67 | + [RESOLVED_SECRET_PROVENANCE_FIELD]: { |
| 68 | + version: 1, |
| 69 | + complete: true, |
| 70 | + entries: [], |
| 71 | + scope: { userId: 'user-1', workspaceId: WORKSPACE_ID }, |
| 72 | + }, |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + it('preserves the existing headerless empty response for ordinary API callers', async () => { |
| 77 | + const response = await GET( |
| 78 | + createMockRequest( |
| 79 | + 'GET', |
| 80 | + undefined, |
| 81 | + {}, |
| 82 | + `http://localhost:3000/api/memory/missing-conversation?workspaceId=${WORKSPACE_ID}` |
| 83 | + ), |
| 84 | + CONTEXT |
| 85 | + ) |
| 86 | + |
| 87 | + expect(response.status).toBe(200) |
| 88 | + expect(response.headers.get(PRIVATE_TOOL_METADATA_RESPONSE_HEADER)).toBeNull() |
| 89 | + expect(await response.json()).toEqual({ success: true, data: null }) |
| 90 | + }) |
| 91 | +}) |
0 commit comments