|
| 1 | +package openairt_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + openairt "github.com/WqyJh/go-openai-realtime" |
| 8 | + "github.com/WqyJh/jsontools" |
| 9 | + "github.com/sashabaranov/go-openai" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCreateSessionRequest(t *testing.T) { |
| 14 | + data := `{ |
| 15 | + "model": "gpt-4o-realtime-preview-2024-12-17", |
| 16 | + "modalities": ["audio", "text"], |
| 17 | + "instructions": "You are a friendly assistant." |
| 18 | +}` |
| 19 | + expected := openairt.CreateSessionRequest{ |
| 20 | + Model: openairt.GPT4oRealtimePreview20241217, |
| 21 | + ClientSession: openairt.ClientSession{ |
| 22 | + Modalities: []openairt.Modality{ |
| 23 | + openairt.ModalityAudio, |
| 24 | + openairt.ModalityText, |
| 25 | + }, |
| 26 | + Instructions: "You are a friendly assistant.", |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + var actual openairt.CreateSessionRequest |
| 31 | + err := json.Unmarshal([]byte(data), &actual) |
| 32 | + require.NoError(t, err) |
| 33 | + require.Equal(t, expected, actual) |
| 34 | + |
| 35 | + actualBytes, err := json.Marshal(actual) |
| 36 | + require.NoError(t, err) |
| 37 | + jsontools.RequireJSONEq(t, data, string(actualBytes)) |
| 38 | +} |
| 39 | + |
| 40 | +func TestCreateSessionResponse(t *testing.T) { |
| 41 | + data := `{ |
| 42 | + "id": "sess_001", |
| 43 | + "object": "realtime.session", |
| 44 | + "model": "gpt-4o-realtime-preview-2024-12-17", |
| 45 | + "modalities": ["audio", "text"], |
| 46 | + "instructions": "You are a friendly assistant.", |
| 47 | + "voice": "alloy", |
| 48 | + "input_audio_format": "pcm16", |
| 49 | + "output_audio_format": "pcm16", |
| 50 | + "input_audio_transcription": { |
| 51 | + "model": "whisper-1" |
| 52 | + }, |
| 53 | + "turn_detection": null, |
| 54 | + "tools": [], |
| 55 | + "tool_choice": "none", |
| 56 | + "temperature": 0.7, |
| 57 | + "max_response_output_tokens": 200, |
| 58 | + "client_secret": { |
| 59 | + "value": "ek_abc123", |
| 60 | + "expires_at": 1234567890 |
| 61 | + } |
| 62 | +} |
| 63 | +` |
| 64 | + temperature := float32(0.7) |
| 65 | + expected := openairt.CreateSessionResponse{ |
| 66 | + ClientSecret: openairt.ClientSecret{ |
| 67 | + Value: "ek_abc123", |
| 68 | + ExpiresAt: 1234567890, |
| 69 | + }, |
| 70 | + ServerSession: openairt.ServerSession{ |
| 71 | + ID: "sess_001", |
| 72 | + Object: "realtime.session", |
| 73 | + Model: openairt.GPT4oRealtimePreview20241217, |
| 74 | + Modalities: []openairt.Modality{ |
| 75 | + openairt.ModalityAudio, |
| 76 | + openairt.ModalityText, |
| 77 | + }, |
| 78 | + Instructions: "You are a friendly assistant.", |
| 79 | + Voice: openairt.VoiceAlloy, |
| 80 | + InputAudioFormat: openairt.AudioFormatPcm16, |
| 81 | + OutputAudioFormat: openairt.AudioFormatPcm16, |
| 82 | + InputAudioTranscription: &openairt.InputAudioTranscription{ |
| 83 | + Model: openai.Whisper1, |
| 84 | + }, |
| 85 | + TurnDetection: nil, |
| 86 | + Tools: []openairt.Tool{}, |
| 87 | + ToolChoice: openairt.ServerToolChoice{String: openairt.ToolChoiceNone}, |
| 88 | + Temperature: &temperature, |
| 89 | + MaxOutputTokens: openairt.IntOrInf(200), |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + var actual openairt.CreateSessionResponse |
| 94 | + err := json.Unmarshal([]byte(data), &actual) |
| 95 | + require.NoError(t, err) |
| 96 | + require.Equal(t, expected, actual) |
| 97 | +} |
0 commit comments