|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { DeployOrchestrator } from '../../src/domain/deploy/orchestrator.js'; |
| 3 | +import { FakeRemoteExecutor } from '../testing/fake-executor.js'; |
| 4 | +import { assembleConfig } from '../../src/config/assembly.js'; |
| 5 | + |
| 6 | +vi.mock('execa', () => ({ |
| 7 | + execa: vi.fn().mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 }), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('fs-extra', () => ({ |
| 11 | + pathExists: vi.fn().mockResolvedValue(false), |
| 12 | +})); |
| 13 | + |
| 14 | +describe('DeployOrchestrator — multi-app', () => { |
| 15 | + it('deploys a backend app and a frontend app under the same workspace', async () => { |
| 16 | + const executor = new FakeRemoteExecutor(); |
| 17 | + |
| 18 | + executor |
| 19 | + // mkdir per app |
| 20 | + .when((cmd) => cmd.includes('mkdir -p') && cmd.includes('/api/'), { stdout: '', exitCode: 0 }) |
| 21 | + .when((cmd) => cmd.includes('mkdir -p') && cmd.includes('/web/'), { stdout: '', exitCode: 0 }) |
| 22 | + // symlink |
| 23 | + .when((cmd) => cmd.includes('ln -sfn'), { stdout: '', exitCode: 0 }) |
| 24 | + // mv |
| 25 | + .when((cmd) => cmd.includes('mv -Tf'), { stdout: '', exitCode: 0 }) |
| 26 | + // releases.json read |
| 27 | + .when((cmd) => cmd.includes('cat') && cmd.includes('releases.json'), { stdout: '[]', exitCode: 0 }) |
| 28 | + // releases.json write |
| 29 | + .when((cmd) => cmd.includes('releases.json') && cmd.includes('base64'), { stdout: '', exitCode: 0 }) |
| 30 | + // ls |
| 31 | + .when((cmd) => cmd.includes('ls -1t'), { stdout: '', exitCode: 0 }) |
| 32 | + // lock |
| 33 | + .when((cmd) => cmd.includes('deploy.lock'), { stdout: 'OK', exitCode: 0 }) |
| 34 | + // health check on the backend |
| 35 | + .when((cmd) => cmd.includes('curl') && cmd.includes('localhost:3000'), { stdout: '200 42', exitCode: 0 }) |
| 36 | + // pm2 jlist — both apps must be online |
| 37 | + .when((cmd) => cmd.includes('pm2 jlist'), { |
| 38 | + stdout: JSON.stringify([ |
| 39 | + { name: 'api', pm2_env: { status: 'online', restart_time: 0 } }, |
| 40 | + ]), |
| 41 | + exitCode: 0, |
| 42 | + }); |
| 43 | + |
| 44 | + const config = assembleConfig({ |
| 45 | + apps: [ |
| 46 | + { |
| 47 | + name: 'api', |
| 48 | + appType: 'backend', |
| 49 | + domain: 'api.example.com', |
| 50 | + pm2: { apps: [{ name: 'api', port: 3000 }] }, |
| 51 | + healthCheck: { enabled: true, path: '/health', timeout: 30, retries: 3, startupDelay: 0 }, |
| 52 | + envFile: '.env.api', |
| 53 | + keepReleases: 5, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'web', |
| 57 | + appType: 'frontend', |
| 58 | + domain: 'www.example.com', |
| 59 | + keepReleases: 3, |
| 60 | + }, |
| 61 | + ], |
| 62 | + ssh: { host: '1.2.3.4', user: 'deploy', port: 22 }, |
| 63 | + remotePath: '/var/www/app', |
| 64 | + }); |
| 65 | + |
| 66 | + const { DeployLock } = await import('../../src/domain/release/manager.js'); |
| 67 | + const { HealthCheckService } = await import('../../src/services/health.service.js'); |
| 68 | + const { CaddyService } = await import('../../src/services/caddy.service.js'); |
| 69 | + |
| 70 | + const orchestrator = new DeployOrchestrator( |
| 71 | + config, |
| 72 | + executor, |
| 73 | + new DeployLock(executor, config.remotePath), |
| 74 | + new HealthCheckService(executor, config), |
| 75 | + new CaddyService(executor, config), |
| 76 | + ); |
| 77 | + |
| 78 | + await orchestrator.deploy({ cwd: '/test', skipBuild: false }); |
| 79 | + |
| 80 | + const history = executor.getHistory(); |
| 81 | + |
| 82 | + // Lock was acquired |
| 83 | + const lockAcquire = history.find((h) => h.command.includes('deploy.lock')); |
| 84 | + expect(lockAcquire).toBeDefined(); |
| 85 | + |
| 86 | + // Lock was released |
| 87 | + const lockRelease = history.filter((h) => h.command.includes('rm -f') && h.command.includes('deploy.lock')); |
| 88 | + expect(lockRelease.length).toBeGreaterThan(0); |
| 89 | + |
| 90 | + // Backend release dir was created |
| 91 | + const apiMkdir = history.filter((h) => h.command.includes('mkdir -p') && h.command.includes('/api/')); |
| 92 | + expect(apiMkdir.length).toBeGreaterThan(0); |
| 93 | + |
| 94 | + // Frontend release dir was created |
| 95 | + const webMkdir = history.filter((h) => h.command.includes('mkdir -p') && h.command.includes('/web/')); |
| 96 | + expect(webMkdir.length).toBeGreaterThan(0); |
| 97 | + |
| 98 | + // Health check ran for the backend (frontend has none) |
| 99 | + const health = history.filter((h) => h.command.includes('curl') && h.command.includes('localhost:3000')); |
| 100 | + expect(health.length).toBeGreaterThan(0); |
| 101 | + }); |
| 102 | +}); |
0 commit comments