|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { Hono } from 'hono'; |
| 3 | +import type { AppEnv } from '../types'; |
| 4 | +import { createMockEnv, createMockSandbox, suppressConsole } from '../test-utils'; |
| 5 | +import type { Process } from '@cloudflare/sandbox'; |
| 6 | + |
| 7 | +import { api } from './api'; |
| 8 | + |
| 9 | +function createFullMockProcess(overrides: Partial<Process> = {}): Process { |
| 10 | + return { |
| 11 | + id: 'test-id', |
| 12 | + command: 'openclaw gateway', |
| 13 | + status: 'running', |
| 14 | + startTime: new Date(), |
| 15 | + endTime: undefined, |
| 16 | + exitCode: undefined, |
| 17 | + waitForPort: vi.fn(), |
| 18 | + kill: vi.fn(), |
| 19 | + getLogs: vi.fn().mockResolvedValue({ stdout: '', stderr: '' }), |
| 20 | + ...overrides, |
| 21 | + } as Process; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Build a test app that sets up sandbox on context and mounts the api routes. |
| 26 | + */ |
| 27 | +function createTestApp(mockSandbox: ReturnType<typeof createMockSandbox>) { |
| 28 | + const app = new Hono<AppEnv>(); |
| 29 | + |
| 30 | + // Middleware: inject sandbox into context (mirrors src/index.ts) |
| 31 | + app.use('*', async (c, next) => { |
| 32 | + c.set('sandbox', mockSandbox.sandbox); |
| 33 | + await next(); |
| 34 | + }); |
| 35 | + |
| 36 | + // Mount the api routes |
| 37 | + app.route('/api', api); |
| 38 | + |
| 39 | + return app; |
| 40 | +} |
| 41 | + |
| 42 | +function makeRequest(app: Hono<AppEnv>, path: string, env: Record<string, unknown> = {}) { |
| 43 | + const mockEnv = createMockEnv({ DEV_MODE: 'true', ...env }); |
| 44 | + return app.request(path, {}, mockEnv); |
| 45 | +} |
| 46 | + |
| 47 | +describe('GET /api/admin/gateway/status', () => { |
| 48 | + beforeEach(() => { |
| 49 | + suppressConsole(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns stopped when no gateway process found', async () => { |
| 53 | + const mockSandbox = createMockSandbox({ processes: [] }); |
| 54 | + const app = createTestApp(mockSandbox); |
| 55 | + |
| 56 | + const res = await makeRequest(app, '/api/admin/gateway/status'); |
| 57 | + expect(res.status).toBe(200); |
| 58 | + |
| 59 | + const body = await res.json(); |
| 60 | + expect(body.status).toBe('stopped'); |
| 61 | + expect(body.processId).toBeUndefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns running when process exists and port responds', async () => { |
| 65 | + const gatewayProcess = createFullMockProcess({ |
| 66 | + id: 'gw-123', |
| 67 | + command: 'openclaw gateway --port 18789', |
| 68 | + status: 'running', |
| 69 | + }); |
| 70 | + const mockSandbox = createMockSandbox(); |
| 71 | + mockSandbox.listProcessesMock.mockResolvedValue([gatewayProcess]); |
| 72 | + mockSandbox.containerFetchMock.mockResolvedValue(new Response('OK', { status: 200 })); |
| 73 | + |
| 74 | + const app = createTestApp(mockSandbox); |
| 75 | + const res = await makeRequest(app, '/api/admin/gateway/status'); |
| 76 | + expect(res.status).toBe(200); |
| 77 | + |
| 78 | + const body = await res.json(); |
| 79 | + expect(body.status).toBe('running'); |
| 80 | + expect(body.processId).toBe('gw-123'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('returns starting when process exists but port does not respond', async () => { |
| 84 | + const gatewayProcess = createFullMockProcess({ |
| 85 | + id: 'gw-456', |
| 86 | + command: '/usr/local/bin/start-openclaw.sh', |
| 87 | + status: 'starting', |
| 88 | + }); |
| 89 | + const mockSandbox = createMockSandbox(); |
| 90 | + mockSandbox.listProcessesMock.mockResolvedValue([gatewayProcess]); |
| 91 | + mockSandbox.containerFetchMock.mockRejectedValue(new Error('Connection refused')); |
| 92 | + |
| 93 | + const app = createTestApp(mockSandbox); |
| 94 | + const res = await makeRequest(app, '/api/admin/gateway/status'); |
| 95 | + expect(res.status).toBe(200); |
| 96 | + |
| 97 | + const body = await res.json(); |
| 98 | + expect(body.status).toBe('starting'); |
| 99 | + expect(body.processId).toBe('gw-456'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns stopped when listProcesses fails gracefully', async () => { |
| 103 | + const mockSandbox = createMockSandbox(); |
| 104 | + mockSandbox.listProcessesMock.mockRejectedValue(new Error('Sandbox unavailable')); |
| 105 | + // findExistingMoltbotProcess catches listProcesses errors and returns null |
| 106 | + const app = createTestApp(mockSandbox); |
| 107 | + const res = await makeRequest(app, '/api/admin/gateway/status'); |
| 108 | + expect(res.status).toBe(200); |
| 109 | + |
| 110 | + const body = await res.json(); |
| 111 | + expect(body.status).toBe('stopped'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('returns running even when containerFetch returns a non-200 status', async () => { |
| 115 | + const gatewayProcess = createFullMockProcess({ |
| 116 | + id: 'gw-789', |
| 117 | + command: 'openclaw gateway', |
| 118 | + status: 'running', |
| 119 | + }); |
| 120 | + const mockSandbox = createMockSandbox(); |
| 121 | + mockSandbox.listProcessesMock.mockResolvedValue([gatewayProcess]); |
| 122 | + // Gateway responds with 404 — still means port is up |
| 123 | + mockSandbox.containerFetchMock.mockResolvedValue(new Response('Not Found', { status: 404 })); |
| 124 | + |
| 125 | + const app = createTestApp(mockSandbox); |
| 126 | + const res = await makeRequest(app, '/api/admin/gateway/status'); |
| 127 | + expect(res.status).toBe(200); |
| 128 | + |
| 129 | + const body = await res.json(); |
| 130 | + expect(body.status).toBe('running'); |
| 131 | + expect(body.processId).toBe('gw-789'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('ignores CLI command processes and returns stopped', async () => { |
| 135 | + const cliProcess = createFullMockProcess({ |
| 136 | + id: 'cli-1', |
| 137 | + command: 'openclaw devices list --json', |
| 138 | + status: 'running', |
| 139 | + }); |
| 140 | + const mockSandbox = createMockSandbox(); |
| 141 | + mockSandbox.listProcessesMock.mockResolvedValue([cliProcess]); |
| 142 | + |
| 143 | + const app = createTestApp(mockSandbox); |
| 144 | + const res = await makeRequest(app, '/api/admin/gateway/status'); |
| 145 | + expect(res.status).toBe(200); |
| 146 | + |
| 147 | + const body = await res.json(); |
| 148 | + expect(body.status).toBe('stopped'); |
| 149 | + }); |
| 150 | +}); |
0 commit comments