|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { convertPromptToMessages } from '../../../src/tracing/vercel-ai/utils'; |
| 3 | + |
| 4 | +describe('vercel-ai-utils', () => { |
| 5 | + describe('convertPromptToMessages', () => { |
| 6 | + it('should convert a prompt with system to a messages array', () => { |
| 7 | + expect( |
| 8 | + convertPromptToMessages( |
| 9 | + JSON.stringify({ |
| 10 | + system: 'You are a friendly robot', |
| 11 | + prompt: 'Hello, robot', |
| 12 | + }), |
| 13 | + ), |
| 14 | + ).toStrictEqual([ |
| 15 | + { role: 'system', content: 'You are a friendly robot' }, |
| 16 | + { role: 'user', content: 'Hello, robot' }, |
| 17 | + ]); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should convert a system prompt to a messages array', () => { |
| 21 | + expect( |
| 22 | + convertPromptToMessages( |
| 23 | + JSON.stringify({ |
| 24 | + system: 'You are a friendly robot', |
| 25 | + }), |
| 26 | + ), |
| 27 | + ).toStrictEqual([{ role: 'system', content: 'You are a friendly robot' }]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should convert a user only prompt to a messages array', () => { |
| 31 | + expect( |
| 32 | + convertPromptToMessages( |
| 33 | + JSON.stringify({ |
| 34 | + prompt: 'Hello, robot', |
| 35 | + }), |
| 36 | + ), |
| 37 | + ).toStrictEqual([{ role: 'user', content: 'Hello, robot' }]); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should ignore unexpected data', () => { |
| 41 | + expect( |
| 42 | + convertPromptToMessages( |
| 43 | + JSON.stringify({ |
| 44 | + randomField: 'Hello, robot', |
| 45 | + nothing: 'that we know how to handle', |
| 46 | + }), |
| 47 | + ), |
| 48 | + ).toBe(undefined); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should not break on invalid json', () => { |
| 52 | + expect(convertPromptToMessages('this is not json')).toBe(undefined); |
| 53 | + }); |
| 54 | + }); |
| 55 | +}); |
0 commit comments