Skip to content

A full-featured download manager.

14.6 KB
Raw Blame
import { DEFAULT_APP_SETTINGS, DEFAULT_MEDIA_SETTINGS } from '@shared/schemas'
import '@test-utils/dom-animations'
import '@renderer/lib/i18n'
import '@testing-library/jest-dom/vitest'
import { toast } from '@renderer/components/ui/toast'
import { useOperatorSession } from '@renderer/lib/operator-auth'
import {
  CliInstallCapability,
  CliPackageManager,
  CliToolPhase,
  CliToolReason,
} from '@shared/types/cli-tool'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vitest'

vi.mock('@renderer/components/ui/toast', () => ({
  toast: { add: vi.fn(), close: vi.fn() },
}))

// Mutable so individual tests can flip the bridge's reported port status
// (Task 21) without redefining the whole `vi.mock` factory per test.
const bridgeStatus = vi.hoisted(() => ({
  current: {
    port: 16802,
    degraded: false,
    extensionPairingHealth: 'ready' as 'ready' | 'degraded',
    fixedPort: 'auto' as const,
    instanceId: 'test-instance',
  },
}))

vi.mock('@renderer/lib/transport', () => ({
  transport: {
    invoke: vi.fn((channel: string) => {
      if (
        channel === 'bridge:listPaired' ||
        channel === 'bridge:listTrusted' ||
        channel === 'bridge:listPendingPairRequests'
      ) {
        return Promise.resolve([])
      }
      if (channel === 'bridge:getStatus') {
        return Promise.resolve(bridgeStatus.current)
      }
      if (channel === 'query:getSettings')
        return Promise.resolve({
          app: DEFAULT_APP_SETTINGS,
          media: DEFAULT_MEDIA_SETTINGS,
        })
      if (channel === 'query:getFfmpegDetection') {
        return Promise.resolve({ active: null, candidates: [] })
      }
      if (channel === 'query:getCliToolStatus') {
        return Promise.resolve({
          phase: CliToolPhase.ManualOnly,
          capability: CliInstallCapability.ManualOnly,
          installCommand: 'npm install -g @motrix/cli@latest',
          packageManager: CliPackageManager.Npm,
          managerOptions: [
            {
              manager: CliPackageManager.Npm,
              installCommand: 'npm install -g @motrix/cli@latest',
              available: false,
            },
            {
              manager: CliPackageManager.Pnpm,
              installCommand: 'pnpm add -g @motrix/cli@latest',
              available: false,
            },
            {
              manager: CliPackageManager.Yarn,
              installCommand: 'yarn global add @motrix/cli@latest',
              available: false,
            },
            {
              manager: CliPackageManager.Bun,
              installCommand: 'bun add -g @motrix/cli@latest',
              available: false,
            },
            {
              manager: CliPackageManager.Volta,
              installCommand: 'volta install @motrix/cli@latest',
              available: false,
            },
          ],
          version: null,
          executablePath: null,
          nodeVersion: null,
          reason: CliToolReason.UnsupportedWeb,
          detail: null,
        })
      }
      return Promise.resolve({})
    }),
    on: vi.fn(),
    off: vi.fn(),
    platform: 'darwin',
  },
}))

// Base UI Switch needs ResizeObserver in jsdom.
if (typeof globalThis.ResizeObserver === 'undefined') {
  globalThis.ResizeObserver = class {
    observe() {}
    unobserve() {}
    disconnect() {}
  } as unknown as typeof ResizeObserver
}

import { transport } from '@renderer/lib/transport'
import { IntegrationDialog } from './integration-dialog'

describe('IntegrationDialog scaffold', () => {
  beforeEach(() => {
    useOperatorSession.setState({ status: null })
    vi.mocked(transport.invoke).mockClear()
    transport.platform = 'darwin'
    bridgeStatus.current = {
      port: 16802,
      degraded: false,
      extensionPairingHealth: 'ready',
      fixedPort: 'auto',
      instanceId: 'test-instance',
    }
  })

  it('explains fnOS pairing limitations without querying unsupported bridge services', async () => {
    transport.platform = 'web'
    useOperatorSession.setState({
      status: { mode: 'fnosGateway', authed: true, canLogout: false },
    })
    render(
      <IntegrationDialog
        open={true}
        onClose={() => {}}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )
    expect(
      await screen.findByText(
        'Client pairing is not available in this installation.'
      )
    ).toBeInTheDocument()
    await waitFor(() => expect(transport.invoke).toHaveBeenCalled())
    expect(
      vi
        .mocked(transport.invoke)
        .mock.calls.some(([channel]) => String(channel).startsWith('bridge:'))
    ).toBe(false)
    expect(
      screen.queryByRole('heading', { name: /browser extensions/i })
    ).not.toBeInTheDocument()
    expect(
      screen.queryByRole('heading', { name: /command-line tools/i })
    ).not.toBeInTheDocument()
    expect(
      screen.getByRole('heading', { name: /media tools/i })
    ).toBeInTheDocument()
  })

  it('shows CLI pairing independently of the platform and browser Extension capability', async () => {
    transport.platform = 'web'
    useOperatorSession.setState({
      status: {
        mode: 'fnosGateway',
        authed: true,
        canLogout: false,
        clientPairing: { cli: true, extension: false },
      },
    })
    render(
      <IntegrationDialog
        open={true}
        onClose={() => {}}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )
    expect(
      await screen.findByRole('heading', { name: /command-line tools/i })
    ).toBeInTheDocument()
    expect(
      screen.getByRole('switch', { name: 'Allow external clients to connect' })
    ).toBeInTheDocument()
    expect(
      screen.queryByRole('heading', { name: /browser extensions/i })
    ).not.toBeInTheDocument()
    expect(
      screen.getByText(
        'Browser extension pairing is not available in this installation.'
      )
    ).toBeInTheDocument()
    await waitFor(() =>
      expect(transport.invoke).toHaveBeenCalledWith(
        'bridge:listPendingPairRequests'
      )
    )
    expect(transport.invoke).not.toHaveBeenCalledWith('bridge:listTrusted')
    expect(transport.invoke).not.toHaveBeenCalledWith('bridge:getStatus')
  })

  it('renders the four top-level section headings', async () => {
    render(
      <IntegrationDialog
        open={true}
        onClose={() => {}}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )
    expect(
      await screen.findByRole('heading', { name: /default app/i })
    ).toBeTruthy()
    expect(
      screen.getByRole('heading', { name: /browser extensions/i })
    ).toBeTruthy()
    expect(
      screen.getByRole('heading', { name: /command-line tools/i })
    ).toBeTruthy()
    expect(screen.getByRole('heading', { name: /media tools/i })).toBeTruthy()
  })

  it('hides desktop protocol associations in the web client', async () => {
    transport.platform = 'web'
    render(
      <IntegrationDialog
        open={true}
        onClose={() => {}}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )

    expect(
      screen.queryByRole('heading', { name: /default app/i })
    ).not.toBeInTheDocument()
    expect(
      await screen.findByRole('heading', { name: /browser extensions/i })
    ).toBeInTheDocument()
  })

  it('places the local CLI card before paired remote tools', async () => {
    render(
      <IntegrationDialog
        open={true}
        onClose={() => {}}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )

    const local = await screen.findByText('Motrix command-line tool')
    const remote = screen.getByRole('heading', {
      name: /paired remote tools/i,
    })
    expect(
      local.compareDocumentPosition(remote) & Node.DOCUMENT_POSITION_FOLLOWING
    ).toBeTruthy()
    expect(
      screen.getByText(/Run motrix in a terminal to control this app/i)
    ).toBeTruthy()
  })

  it('renders the complete manual-only unsupported CLI recovery state', async () => {
    render(
      <IntegrationDialog
        open={true}
        onClose={() => {}}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )

    expect(await screen.findByRole('status')).toHaveTextContent(
      'Manual install'
    )
    expect(
      screen.getByRole('textbox', { name: 'Install command' })
    ).toHaveValue('npm install -g @motrix/cli@latest')
    expect(screen.getByRole('alert')).toHaveTextContent(
      /only in the desktop app/i
    )
  })

  it.each([true, false])(
    'shows the plugin restart reminder only after a successful FFmpeg save (%s)',
    async (saved) => {
      const original = vi.mocked(transport.invoke).getMockImplementation()!
      vi.mocked(transport.invoke).mockImplementation(
        async (channel, ...args) => {
          if (channel === 'command:updateSettings') {
            if (!saved) throw new Error('Save failed')
            return { saved: true }
          }
          return original(channel, ...args)
        }
      )
      const onClose = vi.fn()
      try {
        render(
          <IntegrationDialog
            open
            onClose={onClose}
            labelKey="settings.cards.integration.title"
            descKey="settings.cards.integration.desc"
          />
        )
        fireEvent.click(
          await screen.findByRole('button', { name: 'Show detection details' })
        )
        fireEvent.click(
          screen.getByRole('button', { name: 'Edit custom FFmpeg path' })
        )
        fireEvent.change(
          screen.getByRole('textbox', { name: 'Custom FFmpeg path' }),
          { target: { value: '/configured/ffmpeg' } }
        )
        expect(toast.add).not.toHaveBeenCalled()
        fireEvent.click(screen.getByRole('button', { name: 'Save' }))
        await waitFor(() =>
          expect(transport.invoke).toHaveBeenCalledWith(
            'command:updateSettings',
            { media: { ffmpegBinaryPath: '/configured/ffmpeg' } }
          )
        )
        if (saved) {
          await waitFor(() =>
            expect(toast.add).toHaveBeenCalledWith(
              expect.objectContaining({
                title: 'FFmpeg settings saved',
                description: expect.stringContaining(
                  'Restart Motrix for active plugins'
                ),
                timeout: 0,
              })
            )
          )
          expect(onClose).toHaveBeenCalled()
        } else {
          await screen.findByText('Couldn’t save your changes. Try again.')
          expect(toast.add).not.toHaveBeenCalled()
          expect(onClose).not.toHaveBeenCalled()
        }
      } finally {
        vi.mocked(transport.invoke).mockImplementation(original)
      }
    }
  )

  it('keeps the dialog open when the saved magnet association was rejected', async () => {
    const onClose = vi.fn()
    vi.mocked(transport.invoke).mockImplementation(async (channel: string) => {
      if (channel === 'query:getSettings') {
        return {
          app: {
            browserBridgeEnabled: false,
            protocols: { magnet: false },
          },
          media: {
            ffmpegBinaryPath: '',
            ffmpegStagingMB: 1024,
            ffmpegOpTimeoutSec: 300,
          },
        }
      }
      if (channel === 'command:updateSettings') {
        return { saved: true, protocolAssociationApplied: false }
      }
      if (
        channel === 'bridge:listPaired' ||
        channel === 'bridge:listTrusted' ||
        channel === 'bridge:listPendingPairRequests'
      ) {
        return []
      }
      if (channel === 'bridge:getStatus') {
        return bridgeStatus.current
      }
      if (channel === 'query:getSettings')
        return Promise.resolve({
          app: DEFAULT_APP_SETTINGS,
          media: DEFAULT_MEDIA_SETTINGS,
        })
      if (channel === 'query:getFfmpegDetection') {
        return { active: null, candidates: [] }
      }
      if (channel === 'query:getAppImageIntegrationStatus') {
        return { supported: false }
      }
      return {}
    })
    const user = userEvent.setup()
    render(
      <IntegrationDialog
        open={true}
        onClose={onClose}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )

    await user.click(
      await screen.findByRole('switch', {
        name: 'Open magnet links with Motrix',
      })
    )
    await user.click(screen.getByRole('button', { name: 'Save' }))

    expect(
      await screen.findByText(/desktop association could not be changed/i)
    ).toBeVisible()
    expect(onClose).not.toHaveBeenCalled()
  })

  it('surfaces the degraded-port notice in the browser extensions card when the bridge fell back to an ephemeral port (Task 21)', async () => {
    bridgeStatus.current = {
      port: 54321,
      degraded: true,
      extensionPairingHealth: 'ready',
      fixedPort: 'auto',
      instanceId: 'test-instance',
    }

    render(
      <IntegrationDialog
        open={true}
        onClose={() => {}}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )

    expect(
      await screen.findByText('Bridge running on a fallback port')
    ).toBeInTheDocument()
    expect(screen.getByText(/bound port 54321 instead/)).toBeInTheDocument()
  })

  it('shows no degraded-port notice when the bridge is running on its normal port', async () => {
    render(
      <IntegrationDialog
        open={true}
        onClose={() => {}}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )

    await screen.findByRole('heading', { name: /browser extensions/i })
    expect(
      screen.queryByText('Bridge running on a fallback port')
    ).not.toBeInTheDocument()
  })

  it('keeps a projection failure visible and warns that the paired list is incomplete', async () => {
    bridgeStatus.current = {
      port: 16802,
      degraded: false,
      extensionPairingHealth: 'degraded',
      fixedPort: 'auto',
      instanceId: 'test-instance',
    }

    render(
      <IntegrationDialog
        open={true}
        onClose={() => {}}
        labelKey="settings.cards.integration.title"
        descKey="settings.cards.integration.desc"
      />
    )

    expect(
      await screen.findByText('Extension access is temporarily closed')
    ).toBeInTheDocument()
    expect(
      screen.getByText(/do not rely on the list below/i)
    ).toBeInTheDocument()
  })
})