uiLauncher.test.mjs 4.05 KB
import assert from 'node:assert/strict'
import test from 'node:test'
import { openMiniProgram, releaseMiniProgram } from './uiLauncher.mjs'

test('IT-OL-UI-010: connection mode reuses an existing loopback endpoint', async () => {
  const calls = []
  const miniProgram = { id: 'connected' }
  const automator = {
    async connect(options) {
      calls.push({ method: 'connect', options })
      return miniProgram
    },
    async launch() {
      throw new Error('launch should not be called')
    }
  }

  const result = await openMiniProgram(automator, {
    launchDevTools: false,
    wsEndpoint: 'ws://127.0.0.1:9420'
  })

  assert.equal(result, miniProgram)
  assert.deepEqual(calls, [
    {
      method: 'connect',
      options: { wsEndpoint: 'ws://127.0.0.1:9420' }
    }
  ])
})

test('IT-OL-UI-011: launch mode starts the approved project with an explicit CLI', async () => {
  const calls = []
  const miniProgram = { id: 'launched' }
  const automator = {
    async connect() {
      throw new Error('connect should not be called')
    },
    async launch(options) {
      calls.push({ method: 'launch', options })
      return miniProgram
    }
  }

  const result = await openMiniProgram(automator, {
    launchDevTools: true,
    cliPath: 'C:/tools/wechat/cli.bat',
    projectPath: 'C:/workspace/project',
    automationPort: 9420,
    servicePort: 32219,
    wsEndpoint: 'ws://127.0.0.1:9420'
  })

  assert.equal(result, miniProgram)
  assert.deepEqual(calls, [
    {
      method: 'launch',
      options: {
        cliPath: 'C:/tools/wechat/cli.bat',
        projectPath: 'C:/workspace/project',
        port: 9420,
        trustProject: true,
        args: ['--port', '32219', '--lang', 'zh']
      }
    }
  ])
})

test('IT-OL-UI-015: launch false-negative falls back to the requested endpoint', async () => {
  const calls = []
  const miniProgram = { id: 'connected-after-launch' }
  const automator = {
    async launch(options) {
      calls.push({ method: 'launch', options })
      throw new TypeError('d.on is not a function')
    },
    async connect(options) {
      calls.push({ method: 'connect', options })
      return miniProgram
    }
  }

  const result = await openMiniProgram(automator, {
    launchDevTools: true,
    cliPath: 'C:/tools/wechat/cli.bat',
    projectPath: 'C:/workspace/project',
    automationPort: 9420,
    servicePort: 32219,
    wsEndpoint: 'ws://127.0.0.1:9420'
  })

  assert.equal(result, miniProgram)
  assert.deepEqual(calls.map(call => call.method), ['launch', 'connect'])
  assert.deepEqual(calls[1].options, {
    wsEndpoint: 'ws://127.0.0.1:9420'
  })
})

test('IT-OL-UI-016: launch and connection failures retain both causes', async () => {
  const automator = {
    async launch() {
      throw new Error('launch failed')
    },
    async connect() {
      throw new Error('connect failed')
    }
  }

  await assert.rejects(
    () =>
      openMiniProgram(automator, {
        launchDevTools: true,
        cliPath: 'C:/tools/wechat/cli.bat',
        projectPath: 'C:/workspace/project',
        automationPort: 9420,
        servicePort: 32219,
        wsEndpoint: 'ws://127.0.0.1:9420'
      }),
    error => {
      assert.match(error.message, /ws:\/\/127\.0\.0\.1:9420/)
      assert.equal(error.cause.launchError.message, 'launch failed')
      assert.equal(error.cause.connectError.message, 'connect failed')
      return true
    }
  )
})

test('IT-OL-UI-017: connection mode disconnects without closing reused DevTools', async () => {
  const calls = []
  const miniProgram = {
    async close() {
      calls.push('close')
    },
    disconnect() {
      calls.push('disconnect')
    }
  }

  await releaseMiniProgram(miniProgram, { launchDevTools: false })

  assert.deepEqual(calls, ['disconnect'])
})

test('IT-OL-UI-018: launch mode closes the DevTools instance it started', async () => {
  const calls = []
  const miniProgram = {
    async close() {
      calls.push('close')
    },
    disconnect() {
      calls.push('disconnect')
    }
  }

  await releaseMiniProgram(miniProgram, { launchDevTools: true })

  assert.deepEqual(calls, ['close'])
})