rate.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { expect, test, vi } from 'vitest'
  2. // End of third-party imports
  3. import rate from '../../pages/api/ai/feedback/rate'
  4. import { sanitizeMessagePart } from '../ai/tools/tool-sanitizer'
  5. vi.mock('../ai/tools/tool-sanitizer', () => ({
  6. sanitizeMessagePart: vi.fn((part) => part),
  7. }))
  8. test('rate calls the tool sanitizer', async () => {
  9. const mockReq = {
  10. method: 'POST',
  11. headers: {
  12. authorization: 'Bearer test-token',
  13. },
  14. body: {
  15. rating: 'negative',
  16. messages: [
  17. {
  18. role: 'assistant',
  19. parts: [
  20. {
  21. type: 'tool-execute_sql',
  22. state: 'output-available',
  23. output: 'test output',
  24. },
  25. ],
  26. },
  27. ],
  28. messageId: 'test-message-id',
  29. projectRef: 'test-project',
  30. orgSlug: 'test-org',
  31. reason: 'The response was not helpful',
  32. },
  33. on: vi.fn(),
  34. }
  35. const mockRes = {
  36. status: vi.fn(() => mockRes),
  37. json: vi.fn(() => mockRes),
  38. setHeader: vi.fn(() => mockRes),
  39. }
  40. vi.mock('@/lib/ai/ai-details', () => ({
  41. getOrgAIDetails: vi.fn().mockResolvedValue({
  42. aiOptInLevel: 'schema_and_log_and_data',
  43. hasAccessToAdvanceModel: true,
  44. isDpaSigned: false,
  45. }),
  46. getProjectAIDetails: vi.fn().mockResolvedValue({
  47. region: 'us-east-1',
  48. isSensitive: false,
  49. }),
  50. }))
  51. vi.mock('@/lib/ai/model', () => ({
  52. getModel: vi.fn().mockResolvedValue({
  53. modelParams: { model: {} },
  54. }),
  55. }))
  56. vi.mock('ai', () => ({
  57. generateText: vi.fn().mockResolvedValue({
  58. output: {
  59. category: 'sql_generation',
  60. },
  61. }),
  62. Output: { object: vi.fn() },
  63. }))
  64. vi.mock('@/components/ui/AIAssistantPanel/Message.utils', () => ({
  65. rateMessageResponseSchema: {},
  66. }))
  67. await rate(mockReq as any, mockRes as any)
  68. expect(sanitizeMessagePart).toHaveBeenCalled()
  69. })