@@ -162,7 +162,7 @@ describe("AgentsClient", () => {
162162 ) . rejects . toThrow ( "fetch failed" ) ;
163163 } ) ;
164164
165- it ( "re-throws OneCLIRequestError without wrapping" , async ( ) => {
165+ it ( "re-throws OneCLIRequestError on 500 without wrapping" , async ( ) => {
166166 fetchSpy = vi . spyOn ( globalThis , "fetch" ) . mockResolvedValue (
167167 new Response ( "" , { status : 500 , statusText : "Internal Server Error" } ) ,
168168 ) ;
@@ -180,4 +180,88 @@ describe("AgentsClient", () => {
180180 expect ( ( err as OneCLIRequestError ) . name ) . toBe ( "OneCLIRequestError" ) ;
181181 } ) ;
182182 } ) ;
183+
184+ describe ( "ensureAgent" , ( ) => {
185+ it ( "returns created: true when agent is newly created" , async ( ) => {
186+ fetchSpy = vi . spyOn ( globalThis , "fetch" ) . mockResolvedValue (
187+ new Response ( JSON . stringify ( MOCK_AGENT ) , { status : 201 } ) ,
188+ ) ;
189+
190+ const client = new AgentsClient (
191+ "http://localhost:3000" ,
192+ "oc_test" ,
193+ 5000 ,
194+ ) ;
195+ const result = await client . ensureAgent ( {
196+ name : "My Agent" ,
197+ identifier : "my-agent" ,
198+ } ) ;
199+
200+ expect ( result ) . toEqual ( {
201+ name : "My Agent" ,
202+ identifier : "my-agent" ,
203+ created : true ,
204+ } ) ;
205+ } ) ;
206+
207+ it ( "returns created: false when agent already exists (409)" , async ( ) => {
208+ fetchSpy = vi . spyOn ( globalThis , "fetch" ) . mockResolvedValue (
209+ new Response (
210+ JSON . stringify ( { error : "identifier already exists" } ) ,
211+ { status : 409 , statusText : "Conflict" } ,
212+ ) ,
213+ ) ;
214+
215+ const client = new AgentsClient (
216+ "http://localhost:3000" ,
217+ "oc_test" ,
218+ 5000 ,
219+ ) ;
220+ const result = await client . ensureAgent ( {
221+ name : "My Agent" ,
222+ identifier : "my-agent" ,
223+ } ) ;
224+
225+ expect ( result ) . toEqual ( {
226+ name : "My Agent" ,
227+ identifier : "my-agent" ,
228+ created : false ,
229+ } ) ;
230+ } ) ;
231+
232+ it ( "throws on non-409 errors" , async ( ) => {
233+ fetchSpy = vi . spyOn ( globalThis , "fetch" ) . mockResolvedValue (
234+ new Response ( JSON . stringify ( { error : "Unauthorized" } ) , {
235+ status : 401 ,
236+ statusText : "Unauthorized" ,
237+ } ) ,
238+ ) ;
239+
240+ const client = new AgentsClient (
241+ "http://localhost:3000" ,
242+ "oc_bad" ,
243+ 5000 ,
244+ ) ;
245+
246+ await expect (
247+ client . ensureAgent ( { name : "Test" , identifier : "test" } ) ,
248+ ) . rejects . toThrow ( OneCLIRequestError ) ;
249+ } ) ;
250+
251+ it ( "throws on network errors" , async ( ) => {
252+ fetchSpy = vi
253+ . spyOn ( globalThis , "fetch" )
254+ . mockRejectedValue ( new TypeError ( "fetch failed" ) ) ;
255+
256+ const client = new AgentsClient (
257+ "http://localhost:3000" ,
258+ "oc_test" ,
259+ 5000 ,
260+ ) ;
261+
262+ await expect (
263+ client . ensureAgent ( { name : "Test" , identifier : "test" } ) ,
264+ ) . rejects . toThrow ( OneCLIError ) ;
265+ } ) ;
266+ } ) ;
183267} ) ;
0 commit comments