Skip to content

AI-native design editor. Open-source Figma alternative.

5.8 KB
Raw Blame
import { describe, expect, test } from 'bun:test'
import { Readable } from 'node:stream'

import {
  DevMCPConfigurationSyntaxError,
  DevMCPConfigurationTooLargeError,
  configurationsMatch,
  createAutomationEnvironment,
  devMCPConfigurationErrorStatus,
  readDevMCPConfiguration,
  waitForAutomationHealth
} from '@/app/automation/bridge/vite-plugin'
import {
  parseDevMCPConfiguration,
  type DevMCPConfiguration
} from '@/app/automation/mcp/dev-control'

import { fetchStub } from '#tests/helpers/fetch'

describe('MCP Vite development server', () => {
  test('passes an explicit empty auth token when authentication is disabled', () => {
    const env = createAutomationEnvironment({
      authToken: 'development-token',
      baseEnv: { OPENPENCIL_MCP_AUTH_TOKEN: 'inherited-token' },
      configuration: {
        authenticationEnabled: false,
        rootDirectory: '/designs',
        disabledTools: ['eval', 'delete_node'],
        scope: 'selection'
      },
      corsOrigin: 'http://localhost:1420',
      discoveryPath: '/tmp/mcp.json',
      httpPort: 7600,
      socketPath: '/tmp/open-pencil.sock'
    })

    expect(env.OPENPENCIL_MCP_AUTH_TOKEN).toBe('')
    expect(env.OPENPENCIL_MCP_ROOT).toBe('/designs')
    expect(env.OPENPENCIL_MCP_DISABLED_TOOLS).toBe('eval,delete_node')
    expect(env.OPENPENCIL_MCP_DISCOVERY_PATH).toBe('/tmp/mcp.json')
    expect(env.OPENPENCIL_MCP_SCOPE).toBe('selection')
  })

  test('normalizes and validates typed disabled tool configuration', () => {
    expect(
      parseDevMCPConfiguration({
        authenticationEnabled: true,
        rootDirectory: '/designs',
        disabledTools: [' eval ', 'delete_node', 'eval'],
        scope: 'document'
      })
    ).toEqual({
      authenticationEnabled: true,
      rootDirectory: '/designs',
      disabledTools: ['eval', 'delete_node'],
      scope: 'document'
    })
    expect(
      parseDevMCPConfiguration({
        authenticationEnabled: true,
        rootDirectory: '',
        disabledTools: ['invalid tool'],
        scope: 'document'
      })
    ).toBeNull()
  })

  test('decodes UTF-8 configuration split across request chunks', async () => {
    const body = Buffer.from(
      JSON.stringify({
        authenticationEnabled: true,
        rootDirectory: '/设计',
        disabledTools: [],
        scope: 'document'
      })
    )
    const split = body.indexOf(Buffer.from('设')) + 1
    const request = Readable.from([body.subarray(0, split), body.subarray(split)])

    await expect(readDevMCPConfiguration(request as never)).resolves.toEqual({
      authenticationEnabled: true,
      rootDirectory: '/设计',
      disabledTools: [],
      scope: 'document'
    })
  })

  test('does not restart MCP for unchanged configuration', () => {
    const configuration: DevMCPConfiguration = {
      authenticationEnabled: true,
      rootDirectory: '/designs',
      disabledTools: ['eval', 'delete_node'],
      scope: 'document'
    }
    expect(configurationsMatch(configuration, structuredClone(configuration))).toBe(true)
    expect(
      configurationsMatch(configuration, { ...configuration, disabledTools: ['delete_node'] })
    ).toBe(false)
    expect(configurationsMatch(configuration, { ...configuration, scope: 'selection' })).toBe(false)
  })

  test('waits through transient Portless responses until MCP is healthy', async () => {
    const statuses = [404, 404, 200]
    const requests: string[] = []
    await waitForAutomationHealth(
      'wss://feature.mcp.open-pencil.localhost',
      fetchStub(async (input) => {
        requests.push(String(input))
        return new Response(null, { status: statuses.shift() ?? 500 })
      })
    )

    expect(requests).toEqual([
      'https://feature.mcp.open-pencil.localhost/health',
      'https://feature.mcp.open-pencil.localhost/health',
      'https://feature.mcp.open-pencil.localhost/health'
    ])
  })

  test('health probes do not send credentials', async () => {
    await waitForAutomationHealth(
      'ws://localhost:7682',
      fetchStub(async (_input, init) => {
        expect(new Headers(init?.headers).has('authorization')).toBe(false)
        return new Response(null)
      })
    )
  })

  test('propagates child exit while the request is in flight', async () => {
    let running = true
    await expect(
      waitForAutomationHealth(
        'ws://localhost:7682',
        fetchStub(async () => {
          running = false
          return new Response(null)
        }),
        {
          assertRunning() {
            if (!running) throw new Error('child exited in flight')
          }
        }
      )
    ).rejects.toThrow('child exited in flight')
  })

  test('rejects a failed child before probing another endpoint', async () => {
    let requested = false
    await expect(
      waitForAutomationHealth(
        'ws://localhost:7682',
        fetchStub(async () => {
          requested = true
          return new Response(null)
        }),
        {
          assertRunning() {
            throw new Error('child exited')
          }
        }
      )
    ).rejects.toThrow('child exited')
    expect(requested).toBe(false)
  })

  test('classifies malformed and oversized configuration requests', async () => {
    const malformed = Readable.from(['{'])
    const malformedError = await readDevMCPConfiguration(malformed as never).catch(
      (error: unknown) => error
    )
    expect(malformedError).toBeInstanceOf(DevMCPConfigurationSyntaxError)
    expect(devMCPConfigurationErrorStatus(malformedError)).toBe(400)

    const oversized = Readable.from([Buffer.alloc(70_001)])
    const oversizedError = await readDevMCPConfiguration(oversized as never).catch(
      (error: unknown) => error
    )
    expect(oversizedError).toBeInstanceOf(DevMCPConfigurationTooLargeError)
    expect(devMCPConfigurationErrorStatus(oversizedError)).toBe(413)
    expect(devMCPConfigurationErrorStatus(new Error('restart failed'))).toBe(500)
  })
})