|
| 1 | +import { describe, test, expect } from 'bun:test' |
| 2 | +import { readFileSync } from 'node:fs' |
| 3 | +import { resolve } from 'node:path' |
| 4 | + |
| 5 | +const ROOT = resolve(import.meta.dirname, '..', '..', '..') |
| 6 | +const API_DIR = resolve(ROOT, 'apps', 'api') |
| 7 | + |
| 8 | +function readFile(path: string): string { |
| 9 | + return readFileSync(resolve(ROOT, path), 'utf-8') |
| 10 | +} |
| 11 | + |
| 12 | +describe('deploy.yml', () => { |
| 13 | + const workflow = readFile('.github/workflows/deploy.yml') |
| 14 | + |
| 15 | + test('triggers on push to main and manual dispatch', () => { |
| 16 | + expect(workflow).toContain('branches: [main]') |
| 17 | + expect(workflow).toContain('workflow_dispatch') |
| 18 | + }) |
| 19 | + |
| 20 | + test('has migrate job that runs before deploys', () => { |
| 21 | + expect(workflow).toContain('migrate:') |
| 22 | + expect(workflow).toContain('Run database migrations') |
| 23 | + expect(workflow).toContain('bun run db:migrate:run') |
| 24 | + expect(workflow).toContain('DATABASE_URL') |
| 25 | + }) |
| 26 | + |
| 27 | + test('has deploy-api job using Fly.io', () => { |
| 28 | + expect(workflow).toContain('deploy-api:') |
| 29 | + expect(workflow).toContain('needs: [migrate]') |
| 30 | + expect(workflow).toContain('superfly/flyctl-actions/setup-flyctl') |
| 31 | + expect(workflow).toContain('flyctl deploy') |
| 32 | + expect(workflow).toContain('FLY_API_TOKEN') |
| 33 | + }) |
| 34 | + |
| 35 | + test('deploy-api references correct Dockerfile and fly.toml paths', () => { |
| 36 | + expect(workflow).toContain('--config apps/api/fly.toml') |
| 37 | + expect(workflow).toContain('--dockerfile apps/api/Dockerfile') |
| 38 | + }) |
| 39 | + |
| 40 | + test('has deploy-web job using Vercel', () => { |
| 41 | + expect(workflow).toContain('deploy-web:') |
| 42 | + expect(workflow).toContain('vercel deploy --prod') |
| 43 | + expect(workflow).toContain('VERCEL_TOKEN') |
| 44 | + expect(workflow).toContain('VERCEL_ORG_ID') |
| 45 | + expect(workflow).toContain('VERCEL_PROJECT_ID') |
| 46 | + }) |
| 47 | + |
| 48 | + test('both deploy jobs depend on migrate', () => { |
| 49 | + const apiNeeds = workflow.match(/deploy-api:[\s\S]*?needs:\s*\[migrate\]/) |
| 50 | + const webNeeds = workflow.match(/deploy-web:[\s\S]*?needs:\s*\[migrate\]/) |
| 51 | + expect(apiNeeds).not.toBeNull() |
| 52 | + expect(webNeeds).not.toBeNull() |
| 53 | + }) |
| 54 | + |
| 55 | + test('all jobs use production environment', () => { |
| 56 | + const envMatches = workflow.match(/environment:\s*production/g) |
| 57 | + expect(envMatches).not.toBeNull() |
| 58 | + expect(envMatches!.length).toBeGreaterThanOrEqual(3) |
| 59 | + }) |
| 60 | + |
| 61 | + test('has no TODO placeholders', () => { |
| 62 | + expect(workflow).not.toContain('TODO') |
| 63 | + expect(workflow).not.toMatch(/echo\s+["']TODO/) |
| 64 | + }) |
| 65 | +}) |
| 66 | + |
| 67 | +describe('Dockerfile', () => { |
| 68 | + const dockerfile = readFileSync(resolve(API_DIR, 'Dockerfile'), 'utf-8') |
| 69 | + |
| 70 | + test('uses multi-stage build', () => { |
| 71 | + const fromStatements = dockerfile.match(/^FROM\s/gm) |
| 72 | + expect(fromStatements).not.toBeNull() |
| 73 | + expect(fromStatements!.length).toBeGreaterThanOrEqual(3) |
| 74 | + }) |
| 75 | + |
| 76 | + test('uses official Bun image', () => { |
| 77 | + expect(dockerfile).toContain('FROM oven/bun:1') |
| 78 | + }) |
| 79 | + |
| 80 | + test('installs dependencies with frozen lockfile', () => { |
| 81 | + expect(dockerfile).toContain('bun install --frozen-lockfile') |
| 82 | + }) |
| 83 | + |
| 84 | + test('builds contract and api packages', () => { |
| 85 | + expect(dockerfile).toContain('--filter @sandchest/contract build') |
| 86 | + expect(dockerfile).toContain('--filter @sandchest/api build') |
| 87 | + }) |
| 88 | + |
| 89 | + test('production stage uses slim image', () => { |
| 90 | + expect(dockerfile).toContain('FROM oven/bun:1-slim') |
| 91 | + }) |
| 92 | + |
| 93 | + test('sets NODE_ENV to production', () => { |
| 94 | + expect(dockerfile).toContain('ENV NODE_ENV=production') |
| 95 | + }) |
| 96 | + |
| 97 | + test('exposes correct port', () => { |
| 98 | + expect(dockerfile).toContain('EXPOSE 3001') |
| 99 | + }) |
| 100 | + |
| 101 | + test('runs compiled output', () => { |
| 102 | + expect(dockerfile).toContain('apps/api/dist/index.js') |
| 103 | + }) |
| 104 | + |
| 105 | + test('copies workspace package.json files for dependency install', () => { |
| 106 | + expect(dockerfile).toContain('COPY packages/contract/package.json') |
| 107 | + expect(dockerfile).toContain('COPY packages/db/package.json') |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +describe('fly.toml', () => { |
| 112 | + const flytoml = readFileSync(resolve(API_DIR, 'fly.toml'), 'utf-8') |
| 113 | + |
| 114 | + test('sets app name', () => { |
| 115 | + expect(flytoml).toContain('app = "sandchest-api"') |
| 116 | + }) |
| 117 | + |
| 118 | + test('configures HTTP service on port 3001', () => { |
| 119 | + expect(flytoml).toContain('internal_port = 3001') |
| 120 | + expect(flytoml).toContain('force_https = true') |
| 121 | + }) |
| 122 | + |
| 123 | + test('has health check on /healthz', () => { |
| 124 | + expect(flytoml).toContain('path = "/healthz"') |
| 125 | + }) |
| 126 | + |
| 127 | + test('configures auto-start and minimum machines', () => { |
| 128 | + expect(flytoml).toContain('auto_start_machines = true') |
| 129 | + expect(flytoml).toContain('min_machines_running = 1') |
| 130 | + }) |
| 131 | + |
| 132 | + test('references correct Dockerfile', () => { |
| 133 | + expect(flytoml).toContain('dockerfile = "Dockerfile"') |
| 134 | + }) |
| 135 | +}) |
0 commit comments