1+ // Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
2+ // Use of this source code is governed by a MIT license that can be found in the LICENSE file.
3+ // SPDX-License-Identifier: MIT
4+
5+ using Bogus ;
6+ using FluentAssertions ;
7+ using FluentAssertions . Execution ;
8+ using NSubstitute ;
9+ using System . Text . Json ;
10+ using Deepgram . Models . Authenticate . v1 ;
11+ using Deepgram . Models . Agent . v2 . WebSocket ;
12+ using Deepgram . Clients . Agent . v2 . WebSocket ;
13+
14+ namespace Deepgram . Tests . UnitTests . ClientTests ;
15+
16+ public class AgentClientTests
17+ {
18+ DeepgramWsClientOptions _options ;
19+ string _apiKey ;
20+
21+ [ SetUp ]
22+ public void Setup ( )
23+ {
24+ _apiKey = new Faker ( ) . Random . Guid ( ) . ToString ( ) ;
25+ _options = new DeepgramWsClientOptions ( _apiKey )
26+ {
27+ OnPrem = true ,
28+ } ;
29+ }
30+
31+ [ Test ]
32+ public async Task SendInjectUserMessage_With_String_Should_Send_Message ( )
33+ {
34+ // Input and Output
35+ var content = "Hello! Can you hear me?" ;
36+ var agentClient = Substitute . For < Client > ( _apiKey , _options ) ;
37+
38+ // Mock the SendMessageImmediately method
39+ agentClient . When ( x => x . SendMessageImmediately ( Arg . Any < byte [ ] > ( ) , Arg . Any < int > ( ) , Arg . Any < CancellationTokenSource > ( ) ) )
40+ . DoNotCallBase ( ) ;
41+
42+ // Act
43+ await agentClient . SendInjectUserMessage ( content ) ;
44+
45+ // Assert
46+ await agentClient . Received ( 1 ) . SendMessageImmediately ( Arg . Any < byte [ ] > ( ) , Arg . Any < int > ( ) , Arg . Any < CancellationTokenSource > ( ) ) ;
47+ }
48+
49+ [ Test ]
50+ public async Task SendInjectUserMessage_With_Schema_Should_Send_Message ( )
51+ {
52+ // Input and Output
53+ var schema = new InjectUserMessageSchema
54+ {
55+ Content = "Hello! Can you hear me?"
56+ } ;
57+ var agentClient = Substitute . For < Client > ( _apiKey , _options ) ;
58+
59+ // Mock the SendMessageImmediately method
60+ agentClient . When ( x => x . SendMessageImmediately ( Arg . Any < byte [ ] > ( ) , Arg . Any < int > ( ) , Arg . Any < CancellationTokenSource > ( ) ) )
61+ . DoNotCallBase ( ) ;
62+
63+ // Act
64+ await agentClient . SendInjectUserMessage ( schema ) ;
65+
66+ // Assert
67+ await agentClient . Received ( 1 ) . SendMessageImmediately ( Arg . Any < byte [ ] > ( ) , Arg . Any < int > ( ) , Arg . Any < CancellationTokenSource > ( ) ) ;
68+ }
69+
70+ [ Test ]
71+ public async Task SendInjectUserMessage_With_Null_String_Should_Throw_ArgumentException ( )
72+ {
73+ // Input and Output
74+ string ? content = null ;
75+ var agentClient = new Client ( _apiKey , _options ) ;
76+
77+ // Act & Assert
78+ var exception = await agentClient . Invoking ( y => y . SendInjectUserMessage ( content ! ) )
79+ . Should ( ) . ThrowAsync < ArgumentException > ( )
80+ . WithMessage ( "Content cannot be null or empty*" ) ;
81+ exception . And . ParamName . Should ( ) . Be ( "content" ) ;
82+ }
83+
84+ [ Test ]
85+ public async Task SendInjectUserMessage_With_Empty_String_Should_Throw_ArgumentException ( )
86+ {
87+ // Input and Output
88+ var content = "" ;
89+ var agentClient = new Client ( _apiKey , _options ) ;
90+
91+ // Act & Assert
92+ var exception = await agentClient . Invoking ( y => y . SendInjectUserMessage ( content ) )
93+ . Should ( ) . ThrowAsync < ArgumentException > ( )
94+ . WithMessage ( "Content cannot be null or empty*" ) ;
95+ exception . And . ParamName . Should ( ) . Be ( "content" ) ;
96+ }
97+
98+ [ Test ]
99+ public async Task SendInjectUserMessage_With_Whitespace_String_Should_Throw_ArgumentException ( )
100+ {
101+ // Input and Output
102+ var content = " " ;
103+ var agentClient = new Client ( _apiKey , _options ) ;
104+
105+ // Act & Assert
106+ var exception = await agentClient . Invoking ( y => y . SendInjectUserMessage ( content ) )
107+ . Should ( ) . ThrowAsync < ArgumentException > ( )
108+ . WithMessage ( "Content cannot be null or empty*" ) ;
109+ exception . And . ParamName . Should ( ) . Be ( "content" ) ;
110+ }
111+
112+ [ Test ]
113+ public async Task SendInjectUserMessage_With_Null_Schema_Should_Throw_ArgumentNullException ( )
114+ {
115+ // Input and Output
116+ InjectUserMessageSchema ? schema = null ;
117+ var agentClient = new Client ( _apiKey , _options ) ;
118+
119+ // Act & Assert
120+ var exception = await agentClient . Invoking ( y => y . SendInjectUserMessage ( schema ! ) )
121+ . Should ( ) . ThrowAsync < ArgumentNullException > ( ) ;
122+ exception . And . ParamName . Should ( ) . Be ( "injectUserMessageSchema" ) ;
123+ }
124+
125+ [ Test ]
126+ public async Task SendInjectUserMessage_With_Schema_Null_Content_Should_Throw_ArgumentException ( )
127+ {
128+ // Input and Output
129+ var schema = new InjectUserMessageSchema
130+ {
131+ Content = null
132+ } ;
133+ var agentClient = new Client ( _apiKey , _options ) ;
134+
135+ // Act & Assert
136+ var exception = await agentClient . Invoking ( y => y . SendInjectUserMessage ( schema ) )
137+ . Should ( ) . ThrowAsync < ArgumentException > ( )
138+ . WithMessage ( "Content cannot be null or empty*" ) ;
139+ exception . And . ParamName . Should ( ) . Be ( "Content" ) ;
140+ }
141+
142+ [ Test ]
143+ public async Task SendInjectUserMessage_With_Schema_Empty_Content_Should_Throw_ArgumentException ( )
144+ {
145+ // Input and Output
146+ var schema = new InjectUserMessageSchema
147+ {
148+ Content = ""
149+ } ;
150+ var agentClient = new Client ( _apiKey , _options ) ;
151+
152+ // Act & Assert
153+ var exception = await agentClient . Invoking ( y => y . SendInjectUserMessage ( schema ) )
154+ . Should ( ) . ThrowAsync < ArgumentException > ( )
155+ . WithMessage ( "Content cannot be null or empty*" ) ;
156+ exception . And . ParamName . Should ( ) . Be ( "Content" ) ;
157+ }
158+
159+ [ Test ]
160+ public void InjectUserMessageSchema_Should_Have_Correct_Type ( )
161+ {
162+ // Input and Output
163+ var schema = new InjectUserMessageSchema
164+ {
165+ Content = "Test message"
166+ } ;
167+
168+ // Assert
169+ using ( new AssertionScope ( ) )
170+ {
171+ schema . Type . Should ( ) . Be ( "InjectUserMessage" ) ;
172+ schema . Content . Should ( ) . Be ( "Test message" ) ;
173+ }
174+ }
175+
176+ [ Test ]
177+ public void InjectUserMessageSchema_ToString_Should_Return_Valid_Json ( )
178+ {
179+ // Input and Output
180+ var schema = new InjectUserMessageSchema
181+ {
182+ Content = "Hello! Can you hear me?"
183+ } ;
184+
185+ // Act
186+ var result = schema . ToString ( ) ;
187+
188+ // Assert
189+ using ( new AssertionScope ( ) )
190+ {
191+ result . Should ( ) . NotBeNull ( ) ;
192+ result . Should ( ) . Contain ( "InjectUserMessage" ) ;
193+ result . Should ( ) . Contain ( "Hello! Can you hear me?" ) ;
194+
195+ // Verify it's valid JSON by parsing it
196+ var parsed = JsonDocument . Parse ( result ) ;
197+ parsed . RootElement . GetProperty ( "type" ) . GetString ( ) . Should ( ) . Be ( "InjectUserMessage" ) ;
198+ parsed . RootElement . GetProperty ( "content" ) . GetString ( ) . Should ( ) . Be ( "Hello! Can you hear me?" ) ;
199+ }
200+ }
201+ }
0 commit comments