|
1 | 1 | import { CoolifyClient } from '../lib/coolify-client.js'; |
2 | | -import type { |
3 | | - ServerInfo, |
4 | | - ServerResources, |
5 | | - Environment, |
6 | | - Application, |
7 | | - CreateApplicationRequest, |
8 | | -} from '../types/coolify.js'; |
| 2 | +import type { ServerInfo, ServerResources, Environment, Deployment } from '../types/coolify.js'; |
9 | 3 |
|
10 | 4 | // Mock fetch globally |
11 | 5 | const mockFetch = jest.fn(); |
@@ -300,158 +294,51 @@ describe('CoolifyClient', () => { |
300 | 294 | }); |
301 | 295 | }); |
302 | 296 |
|
303 | | - describe('Application Management', () => { |
304 | | - const mockApplication: Application = { |
305 | | - id: 1, |
306 | | - uuid: 'test-app-uuid', |
307 | | - name: 'test-app', |
308 | | - environment_uuid: 'test-env-uuid', |
309 | | - project_uuid: 'test-project-uuid', |
310 | | - git_repository: 'https://github.com/test/repo', |
311 | | - git_branch: 'main', |
312 | | - build_pack: 'nixpacks', |
313 | | - ports_exposes: '3000', |
314 | | - status: 'running', |
315 | | - created_at: '2024-03-05T12:00:00Z', |
316 | | - updated_at: '2024-03-05T12:00:00Z', |
317 | | - }; |
318 | | - |
319 | | - describe('listApplications', () => { |
320 | | - it('should fetch all applications when no environment UUID is provided', async () => { |
321 | | - mockFetch.mockResolvedValueOnce({ |
322 | | - ok: true, |
323 | | - json: async () => [mockApplication], |
324 | | - }); |
325 | | - |
326 | | - const result = await client.listApplications(); |
327 | | - |
328 | | - expect(result).toEqual([mockApplication]); |
329 | | - expect(mockFetch).toHaveBeenCalledWith( |
330 | | - 'http://test.coolify.io/api/v1/applications', |
331 | | - expect.objectContaining({ |
332 | | - headers: expect.objectContaining({ |
333 | | - Authorization: 'Bearer test-token', |
334 | | - }), |
335 | | - }), |
336 | | - ); |
337 | | - }); |
338 | | - |
339 | | - it('should fetch applications filtered by environment UUID', async () => { |
340 | | - mockFetch.mockResolvedValueOnce({ |
341 | | - ok: true, |
342 | | - json: async () => [mockApplication], |
343 | | - }); |
344 | | - |
345 | | - const result = await client.listApplications('test-env-uuid'); |
346 | | - |
347 | | - expect(result).toEqual([mockApplication]); |
348 | | - expect(mockFetch).toHaveBeenCalledWith( |
349 | | - 'http://test.coolify.io/api/v1/applications?environment_uuid=test-env-uuid', |
350 | | - expect.objectContaining({ |
351 | | - headers: expect.objectContaining({ |
352 | | - Authorization: 'Bearer test-token', |
353 | | - }), |
354 | | - }), |
355 | | - ); |
356 | | - }); |
357 | | - }); |
358 | | - |
359 | | - describe('getApplication', () => { |
360 | | - it('should fetch application details successfully', async () => { |
361 | | - mockFetch.mockResolvedValueOnce({ |
362 | | - ok: true, |
363 | | - json: async () => mockApplication, |
364 | | - }); |
365 | | - |
366 | | - const result = await client.getApplication('test-app-uuid'); |
| 297 | + describe('deployApplication', () => { |
| 298 | + it('should deploy an application', async () => { |
| 299 | + const mockDeployment: Deployment = { |
| 300 | + id: 1, |
| 301 | + uuid: 'test-deployment-uuid', |
| 302 | + application_uuid: 'test-app-uuid', |
| 303 | + status: 'running', |
| 304 | + created_at: '2024-03-20T12:00:00Z', |
| 305 | + updated_at: '2024-03-20T12:00:00Z', |
| 306 | + }; |
367 | 307 |
|
368 | | - expect(result).toEqual(mockApplication); |
369 | | - expect(mockFetch).toHaveBeenCalledWith( |
370 | | - 'http://test.coolify.io/api/v1/applications/test-app-uuid', |
371 | | - expect.objectContaining({ |
372 | | - headers: expect.objectContaining({ |
373 | | - Authorization: 'Bearer test-token', |
374 | | - }), |
375 | | - }), |
376 | | - ); |
| 308 | + mockFetch.mockResolvedValueOnce({ |
| 309 | + ok: true, |
| 310 | + json: () => Promise.resolve(mockDeployment), |
377 | 311 | }); |
378 | | - }); |
379 | | - |
380 | | - describe('createApplication', () => { |
381 | | - it('should create a new application successfully', async () => { |
382 | | - mockFetch.mockResolvedValueOnce({ |
383 | | - ok: true, |
384 | | - json: async () => mockApplication, |
385 | | - }); |
386 | 312 |
|
387 | | - const createRequest: CreateApplicationRequest = { |
388 | | - project_uuid: 'test-project-uuid', |
389 | | - environment_uuid: 'test-env-uuid', |
390 | | - git_repository: 'https://github.com/test/repo', |
391 | | - git_branch: 'main', |
392 | | - build_pack: 'nixpacks', |
393 | | - ports_exposes: '3000', |
394 | | - name: 'test-app', |
395 | | - }; |
396 | | - |
397 | | - const result = await client.createApplication(createRequest); |
398 | | - |
399 | | - expect(result).toEqual(mockApplication); |
400 | | - expect(mockFetch).toHaveBeenCalledWith( |
401 | | - 'http://test.coolify.io/api/v1/applications/public', |
402 | | - expect.objectContaining({ |
403 | | - method: 'POST', |
404 | | - body: JSON.stringify(createRequest), |
405 | | - headers: expect.objectContaining({ |
406 | | - Authorization: 'Bearer test-token', |
407 | | - }), |
| 313 | + const result = await client.deployApplication('test-app-uuid'); |
| 314 | + expect(result).toEqual(mockDeployment); |
| 315 | + expect(mockFetch).toHaveBeenCalledWith( |
| 316 | + 'http://test.coolify.io/api/v1/applications/test-app-uuid/deploy', |
| 317 | + expect.objectContaining({ |
| 318 | + method: 'POST', |
| 319 | + headers: expect.objectContaining({ |
| 320 | + Authorization: 'Bearer test-token', |
| 321 | + 'Content-Type': 'application/json', |
408 | 322 | }), |
409 | | - ); |
410 | | - }); |
| 323 | + }), |
| 324 | + ); |
411 | 325 | }); |
412 | 326 |
|
413 | | - describe('deleteApplication', () => { |
414 | | - it('should delete an application successfully', async () => { |
415 | | - mockFetch.mockResolvedValueOnce({ |
416 | | - ok: true, |
417 | | - json: async () => ({}), |
418 | | - }); |
419 | | - |
420 | | - await client.deleteApplication('test-app-uuid'); |
| 327 | + it('should handle errors when deploying an application', async () => { |
| 328 | + const errorResponse = { |
| 329 | + error: 'Error', |
| 330 | + status: 500, |
| 331 | + message: 'Failed to deploy application', |
| 332 | + }; |
421 | 333 |
|
422 | | - expect(mockFetch).toHaveBeenCalledWith( |
423 | | - 'http://test.coolify.io/api/v1/applications/test-app-uuid', |
424 | | - expect.objectContaining({ |
425 | | - method: 'DELETE', |
426 | | - headers: expect.objectContaining({ |
427 | | - Authorization: 'Bearer test-token', |
428 | | - }), |
429 | | - }), |
430 | | - ); |
| 334 | + mockFetch.mockResolvedValueOnce({ |
| 335 | + ok: false, |
| 336 | + json: () => Promise.resolve(errorResponse), |
431 | 337 | }); |
432 | | - }); |
433 | 338 |
|
434 | | - describe('deployApplication', () => { |
435 | | - it('should make a POST request to deploy an application', async () => { |
436 | | - const mockDeployment = { id: 'test-deployment' }; |
437 | | - mockFetch.mockResolvedValueOnce({ |
438 | | - ok: true, |
439 | | - json: async () => mockDeployment, |
440 | | - }); |
441 | | - |
442 | | - const result = await client.deployApplication('test-app-uuid'); |
443 | | - |
444 | | - expect(mockFetch).toHaveBeenCalledWith( |
445 | | - 'http://test.coolify.io/api/v1/applications/test-app-uuid/deploy', |
446 | | - expect.objectContaining({ |
447 | | - method: 'POST', |
448 | | - headers: expect.objectContaining({ |
449 | | - Authorization: 'Bearer test-token', |
450 | | - }), |
451 | | - }), |
452 | | - ); |
453 | | - expect(result).toEqual(mockDeployment); |
454 | | - }); |
| 339 | + await expect(client.deployApplication('test-app-uuid')).rejects.toThrow( |
| 340 | + 'Failed to deploy application', |
| 341 | + ); |
455 | 342 | }); |
456 | 343 | }); |
457 | 344 | }); |
0 commit comments