provider.spec.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { existsSync } from 'node:fs'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { afterEach, expect, it, vi } from 'vitest'
  4. import { mountSessionMcp } from '@deepseek-ai/dsh-experimental-browser-use-runtime/mcp'
  5. import * as Provider from '../src/index.ts'
  6. vi.mock('@deepseek-ai/dsh-experimental-browser-use-runtime/mcp', async importOriginal => ({
  7. ...await importOriginal<typeof import('@deepseek-ai/dsh-experimental-browser-use-runtime/mcp')>(),
  8. mountSessionMcp: vi.fn(),
  9. }))
  10. afterEach(() => vi.clearAllMocks())
  11. it('resolves the installed DevTools CLI and explicitly selects isolated headed or headless launch', () => {
  12. Provider.apply(new Context(), Provider.Config({ mode: 'launch', executablePath: '/custom/chrome', toolCallTimeoutMs: 456 }))
  13. const options = vi.mocked(mountSessionMcp).mock.calls[0]![1]
  14. expect(existsSync(options.args[0]!)).toBe(true)
  15. expect(options).toMatchObject({ name: 'chrome-devtools-mcp', exclusive: false, command: process.execPath, toolCallTimeoutMs: 456 })
  16. expect(options.args.slice(1)).toEqual(['--no-usage-statistics', '--isolated', '--headless=true', '--executable-path', '/custom/chrome'])
  17. Provider.apply(new Context(), Provider.Config({ mode: 'launch', headless: false }))
  18. expect(vi.mocked(mountSessionMcp).mock.calls[1]![1].args.slice(1)).toEqual(['--no-usage-statistics', '--isolated', '--headless=false'])
  19. expect('default' in Provider).toBe(false)
  20. })
  21. it.each([
  22. ['http://127.0.0.1:9222', '--browser-url'],
  23. ['wss://browser.example/devtools/browser/id', '--ws-endpoint'],
  24. ])('attaches to %s without passing launch flags', (endpoint, flag) => {
  25. Provider.apply(new Context(), Provider.Config({ mode: 'attach', endpoint }))
  26. const options = vi.mocked(mountSessionMcp).mock.calls[0]![1]
  27. expect(options.exclusive).toBe(true)
  28. expect(options.args.slice(1)).toEqual(['--no-usage-statistics', flag, endpoint])
  29. })
  30. it('rejects trailing endpoint garbage before mounting any browser resources', () => {
  31. expect(() => { Provider.apply(new Context(), Provider.Config({ mode: 'attach', endpoint: 'http://localhost trailing-junk' })) }).toThrow('browser endpoint')
  32. expect(mountSessionMcp).not.toHaveBeenCalled()
  33. })