|
| 1 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +vi.mock('@/lib/config', () => ({ |
| 7 | + config: { openclawConfigPath: '' }, |
| 8 | +})) |
| 9 | + |
| 10 | +vi.mock('@/lib/logger', () => ({ |
| 11 | + logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() }, |
| 12 | +})) |
| 13 | + |
| 14 | +describe('registerMcAsDashboard', () => { |
| 15 | + const originalEnv = { ...process.env } |
| 16 | + let tempDir = '' |
| 17 | + let configPath = '' |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + tempDir = mkdtempSync(path.join(os.tmpdir(), 'mc-gateway-runtime-')) |
| 21 | + configPath = path.join(tempDir, 'openclaw.json') |
| 22 | + process.env = { ...originalEnv } |
| 23 | + |
| 24 | + const { config } = await import('@/lib/config') |
| 25 | + config.openclawConfigPath = configPath |
| 26 | + }) |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + process.env = { ...originalEnv } |
| 30 | + rmSync(tempDir, { recursive: true, force: true }) |
| 31 | + vi.resetModules() |
| 32 | + }) |
| 33 | + |
| 34 | + it('adds the Mission Control origin without disabling device auth', async () => { |
| 35 | + writeFileSync(configPath, JSON.stringify({ |
| 36 | + gateway: { |
| 37 | + controlUi: { |
| 38 | + allowedOrigins: ['https://existing.example.com'], |
| 39 | + dangerouslyDisableDeviceAuth: false, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, null, 2) + '\n', 'utf-8') |
| 43 | + |
| 44 | + const { registerMcAsDashboard } = await import('@/lib/gateway-runtime') |
| 45 | + const result = registerMcAsDashboard('https://mc.example.com/dashboard') |
| 46 | + |
| 47 | + expect(result).toEqual({ registered: true, alreadySet: false }) |
| 48 | + |
| 49 | + const updated = JSON.parse(readFileSync(configPath, 'utf-8')) |
| 50 | + expect(updated.gateway.controlUi.allowedOrigins).toEqual([ |
| 51 | + 'https://existing.example.com', |
| 52 | + 'https://mc.example.com', |
| 53 | + ]) |
| 54 | + expect(updated.gateway.controlUi.dangerouslyDisableDeviceAuth).toBe(false) |
| 55 | + }) |
| 56 | + |
| 57 | + it('does not rewrite config when the origin is already present', async () => { |
| 58 | + writeFileSync(configPath, JSON.stringify({ |
| 59 | + gateway: { |
| 60 | + controlUi: { |
| 61 | + allowedOrigins: ['https://mc.example.com'], |
| 62 | + dangerouslyDisableDeviceAuth: false, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, null, 2) + '\n', 'utf-8') |
| 66 | + |
| 67 | + const before = readFileSync(configPath, 'utf-8') |
| 68 | + const { registerMcAsDashboard } = await import('@/lib/gateway-runtime') |
| 69 | + const result = registerMcAsDashboard('https://mc.example.com/sessions') |
| 70 | + const after = readFileSync(configPath, 'utf-8') |
| 71 | + |
| 72 | + expect(result).toEqual({ registered: false, alreadySet: true }) |
| 73 | + expect(after).toBe(before) |
| 74 | + }) |
| 75 | +}) |
0 commit comments