Skip to content

A full-featured download manager.

81.4 KB
Raw Blame
import { mkdir, mkdtemp, realpath, rm, symlink } from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { NOOP_TASK_ACTIVITY_RECORDER } from '@core/activity'
import type { Aria2RpcClient } from '@core/engine/aria2/aria2-rpc-client'
import type { EngineAdapter } from '@core/engine/engine-adapter'
import type { EngineSupervisor } from '@core/engine/engine-supervisor'
import type { EventBus } from '@core/events/event-bus'
import { NotificationCenter } from '@core/notifications/notification-center'
import type { CapabilityHost } from '@core/plugin/capabilities/interface'
import type { GrantsManager } from '@core/plugin/grants/grants-manager'
import type { ActivationDispatcher } from '@core/plugin/host/activation-dispatcher'
import type { PluginHost } from '@core/plugin/host/plugin-host'
import type { PluginInstaller } from '@core/plugin/install/plugin-installer'
import { PluginEngineVersionTooOld } from '@core/plugin/manifest/errors'
import type { PluginRegistry } from '@core/plugin/plugin-registry'
import type { RegistryClient } from '@core/plugin/registry/registry-client'
import type { PluginStateStore } from '@core/plugin/state/plugin-state-store'
import { AppliedDownloadProxyPolicy } from '@core/proxy/applied-download-proxy-policy'
import { MotrixDatabase } from '@core/session/motrix-database'
import { SettingsManager } from '@core/settings/settings-manager'
import type { FileCleanupService } from '@core/task/file-cleanup-service'
import type { FinalNamePicker } from '@core/task/final-name-picker'
import { TaskManager } from '@core/task/task-manager'
import type { TorrentMetaStore } from '@core/task/torrent-meta-store'
import type { MagnetTracker } from '@core/torrent/magnet-tracker'
import type { TrackerManager } from '@core/tracker'
import { Commands } from '@shared/protocol/commands'
import { Events } from '@shared/protocol/events'
import { Queries } from '@shared/protocol/queries'
import {
  TaskInstancePhase,
  TaskKind,
  TaskStatus,
  TaskType,
  TransitionPhase,
} from '@shared/types/task'
import { makeMediaMetaStoreStub } from '@test-utils/media-meta-store'
import { makeDownloadTask } from '@test-utils/task'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createServerDownloadPathPolicy } from '../download-path-policy'
import { makeServerFfmpegDetect } from '../plugin/ffmpeg-detect-server'
import type { ServerPluginInstallService } from '../plugin/install-service'
import { installServerFfmpeg } from '../plugin/verified-ffmpeg-server'
import { ServerDirectoryService } from '../server-directory-service'
import type { ServerCommandContext } from './commands'
import { buildServerCommandHandlers } from './commands'
import { buildServerQueryHandlers } from './queries'

vi.mock('../plugin/verified-ffmpeg-server', () => ({
  installServerFfmpeg: vi.fn(),
  probeServerFfmpeg: vi.fn(),
}))
vi.mock('../plugin/ffmpeg-detect-server', () => ({
  makeServerFfmpegDetect: vi.fn(() => async () => ({
    active: null,
    candidates: [],
  })),
}))

const PROXY_OFF = {
  enabled: false,
  protocol: 'http' as const,
  host: '',
  port: 8080,
  user: '',
  password: '',
  bypass: [] as string[],
  scopes: {
    download: false,
    updateApp: false,
    updateTrackers: false,
    plugins: false,
  },
}
const PROXY_ON = { ...PROXY_OFF, enabled: true, host: 'p.example', port: 80 }

function makeFakeCtx() {
  const setEnabled = vi.fn()
  const refreshState = vi.fn()
  return {
    updateService: {
      check: vi.fn().mockResolvedValue(undefined),
      setChannel: vi.fn(),
    },
    supervisor: {
      stop: vi.fn().mockResolvedValue(undefined),
      start: vi.fn().mockResolvedValue(undefined),
      applyEngineSettings: vi.fn().mockResolvedValue(undefined),
      applyAsyncDns: vi.fn().mockResolvedValue(undefined),
      applyDefaultSaveDir: vi.fn().mockResolvedValue(undefined),
      waitUntilReady: vi.fn().mockResolvedValue(undefined),
      assertReady: vi.fn(),
    } as unknown as EngineSupervisor,
    dnsFallback: { reset: vi.fn() },
    settingsManager: {
      mutateDirectoryPreferences: vi.fn().mockResolvedValue({
        ok: true,
        value: { favorites: [], recent: [] },
      }),
      get: vi.fn(),
      update: vi.fn(),
      acceptDisclaimer: vi.fn().mockResolvedValue({ saved: true }),
      removePluginConfig: vi.fn().mockResolvedValue(undefined),
      getApp: vi.fn(() => ({
        defaultSaveDir: '/tmp',
        magnetFileSelection: true,
      })),
      getEngine: vi.fn(() => ({ userAgent: 'Motrix/Test' })),
    } as unknown as SettingsManager,
    geoipManager: {
      triggerUpdate: vi.fn(),
    },
    rpcClient: {} as Aria2RpcClient,
    adapter: {} as EngineAdapter,
    trackerManager: {
      applySelectionChange: vi.fn().mockResolvedValue(undefined),
      syncAndCurate: vi.fn().mockResolvedValue(undefined),
      applySourcesChange: vi.fn().mockResolvedValue(undefined),
      applyBlacklistChange: vi.fn().mockResolvedValue(undefined),
      applySyncScheduleChange: vi.fn(),
    } as unknown as TrackerManager,
    bridgeControl: {
      setEnabled: vi.fn().mockResolvedValue(undefined),
      restart: vi.fn().mockResolvedValue(undefined),
    },
    aria2BinaryPath: '/usr/bin/aria2c',
    finalNamePicker: {} as FinalNamePicker,
    mediaMetaStore: makeMediaMetaStoreStub(),
    torrentMetaStore: {} as TorrentMetaStore,
    taskManager: {
      getById: vi.fn(() => undefined),
    } as unknown as TaskManager,
    fileCleanupService: {} as FileCleanupService,
    eventBus: { emit: vi.fn() } as unknown as EventBus,
    proxyApplier: {
      apply: vi.fn().mockResolvedValue({ downloadProxy: 'unchanged' }),
      applyAll: vi.fn().mockResolvedValue({ downloadProxy: 'unchanged' }),
    },
    appliedDownloadProxyPolicy: new AppliedDownloadProxyPolicy({ noProxy: '' }),
    motrixDatabase: {
      deleteMetadata: vi.fn(),
    } as unknown as MotrixDatabase,
    notificationCenter: {
      notify: vi.fn(() => ({ fresh: true })),
      markRead: vi.fn(() => true),
      markAllRead: vi.fn(() => 0),
      delete: vi.fn(() => true),
      clear: vi.fn(() => 0),
    } as unknown as NotificationCenter,
    taskPersistence: {
      runExclusivePersistence: vi.fn(
        async (operation: () => unknown | Promise<unknown>) => operation()
      ),
    },
    pluginRegistry: {
      refreshState,
      list: vi.fn(() => []),
    } as unknown as PluginRegistry,
    registryClient: {
      refresh: vi.fn().mockResolvedValue(undefined),
      list: vi.fn().mockResolvedValue([]),
    } as unknown as RegistryClient,
    hostVersion: '2.0',
    pluginStateStore: {
      setEnabled,
      get: vi.fn(() => ({
        status: 'inactive',
        enabled: Boolean(setEnabled.mock.calls.at(-1)?.[1]),
      })),
    } as unknown as PluginStateStore,
    pluginHost: {
      enable: vi.fn(async (id: string) => {
        setEnabled(id, true)
        refreshState(id)
      }),
      deactivate: vi.fn().mockResolvedValue(undefined),
      disable: vi.fn(async (id: string) => {
        setEnabled(id, false)
        refreshState(id)
      }),
    } as unknown as PluginHost,
    pluginInstaller: {
      commit: vi.fn(),
      cancel: vi.fn(),
      uninstall: vi.fn(),
    } as unknown as PluginInstaller,
    pluginInstallService: {
      stage: vi.fn(),
    } as unknown as ServerPluginInstallService,
    pluginGrants: { updateGrants: vi.fn() } as unknown as GrantsManager,
    capabilityHost: {
      secrets: {
        available: vi.fn(() => false),
        encrypt: vi.fn(),
      },
      configFor: vi.fn(() => ({ applyExternalChange: vi.fn() })),
      clearLog: vi.fn(),
      setLogVerbose: vi.fn(),
    } as unknown as CapabilityHost,
    userDataDir: '/tmp/userdata',
    pluginsDir: '/tmp/userdata/plugins',
    pluginActivation: {
      dispatch: vi.fn().mockResolvedValue(undefined),
    } as unknown as ActivationDispatcher,
    magnetTracker: {
      getFileSelection: vi.fn().mockResolvedValue(undefined),
      submit: vi.fn().mockResolvedValue(undefined),
      retryMetadata: vi.fn().mockResolvedValue(undefined),
      cancel: vi.fn().mockResolvedValue('removed'),
      observe: vi.fn().mockReturnValue(false),
      dispose: vi.fn(),
      primeFromDatabase: vi.fn(),
    } as unknown as MagnetTracker,
    activityRecorder: NOOP_TASK_ACTIVITY_RECORDER,
    publishTaskUpdate: vi.fn(),
    publishTaskUpdateNow: vi.fn(),
    runTaskMutation: vi.fn(
      async (_taskIds: readonly string[], operation: () => Promise<unknown>) =>
        operation()
    ),
    downloadPathPolicy: {
      allowedSaveDirs: ['/downloads'],
      prepareSaveDir: vi.fn(async (requested: string) => requested),
      authorizeDirectory: vi.fn(),
    },
    serverDirectoryService: {
      create: vi.fn(),
      resolvePreferenceDirectory: vi.fn(async (value: string) => value),
    },
  }
}

describe('server application update checks', () => {
  it('applies the persisted channel to subsequent version checks', async () => {
    const ctx = makeFakeCtx()
    const base = makeSettings(PROXY_OFF)
    let settings = { ...base, app: { ...base.app, updateChannel: 'stable' } }
    vi.mocked(ctx.settingsManager.get).mockImplementation(
      () => settings as never
    )
    vi.mocked(ctx.settingsManager.getApp).mockImplementation(
      () => settings.app as never
    )
    vi.mocked(ctx.settingsManager.update).mockImplementation(async () => {
      settings = {
        ...settings,
        app: { ...settings.app, updateChannel: 'beta' },
      }
      return {
        saved: true,
        requiresRestart: false,
        changedRestartKeys: [],
        requiresAppRestart: false,
        changedAppRestartKeys: [],
      }
    })
    const handlers = buildServerCommandHandlers(
      ctx as unknown as ServerCommandContext
    )
    await handlers[Commands.UpdateSettings]?.({
      app: { updateChannel: 'beta' },
    })
    expect(ctx.updateService.setChannel).toHaveBeenCalledWith('beta')
  })

  it('checks through the server service and never exposes download or installation', async () => {
    const ctx = makeFakeCtx()
    const handlers = buildServerCommandHandlers(
      ctx as unknown as ServerCommandContext
    )
    await expect(handlers[Commands.CheckForUpdates]?.()).resolves.toEqual({
      ok: true,
    })
    expect(ctx.updateService.check).toHaveBeenCalledOnce()
    for (const command of [Commands.DownloadUpdate, Commands.InstallUpdate]) {
      await expect(handlers[command]?.()).rejects.toMatchObject({
        code: 'APP_UPDATE_MANAGED',
      })
    }
    await expect(
      handlers[Commands.CheckForUpdates]?.({ url: 'https://invalid.test' })
    ).rejects.toThrow()
    expect(ctx.updateService.check).toHaveBeenCalledOnce()
  })
})

describe('server FFmpeg installation', () => {
  it('does not open a server settings directory through web commands', async () => {
    const handlers = buildServerCommandHandlers(
      makeFakeCtx() as ServerCommandContext
    )
    await expect(handlers[Commands.OpenSettingsDirectory]?.()).resolves.toBe(
      false
    )
    await expect(
      handlers[Commands.OpenSettingsDirectory]?.({ path: '/tmp' })
    ).rejects.toThrow()
  })

  it('rejects caller-supplied URLs or paths before starting an install', async () => {
    vi.mocked(installServerFfmpeg).mockClear()
    const handlers = buildServerCommandHandlers(
      makeFakeCtx() as ServerCommandContext
    )
    await expect(
      handlers[Commands.InstallFfmpeg]?.({ url: 'https://evil.test' })
    ).rejects.toThrow()
    expect(installServerFfmpeg).not.toHaveBeenCalled()
  })

  it('installs on the server, broadcasts progress, and refreshes media capabilities', async () => {
    const ctx = makeFakeCtx()
    const refreshDetection = vi
      .fn()
      .mockResolvedValue({ active: null, candidates: [] })
    const trackAsyncWork = vi.fn(async (operation: () => Promise<unknown>) =>
      operation()
    )
    const result = { ok: true as const, releaseVersion: '9.0.2-motrix.2' }
    const status = {
      phase: 'downloading' as const,
      bytesReceived: 50,
      bytesTotal: 100,
      percent: 0.5,
      releaseVersion: null,
      directory: null,
      error: null,
    }
    vi.mocked(installServerFfmpeg).mockImplementationOnce(
      async (_directory, onStatus) => {
        onStatus?.(status)
        return result
      }
    )
    const handlers = buildServerCommandHandlers({
      ...ctx,
      trackAsyncWork,
      capabilityHost: { ffmpeg: { refreshDetection } },
    } as unknown as ServerCommandContext)
    await expect(handlers[Commands.InstallFfmpeg]?.()).resolves.toEqual(result)
    expect(trackAsyncWork).toHaveBeenCalledOnce()
    expect(installServerFfmpeg).toHaveBeenLastCalledWith(
      ctx.userDataDir,
      expect.any(Function)
    )
    expect(ctx.eventBus.emit).toHaveBeenCalledWith(
      Events.FfmpegInstallStatusChanged,
      status
    )
    expect(refreshDetection).toHaveBeenCalledWith(
      vi.mocked(makeServerFfmpegDetect).mock.results.at(-1)?.value
    )
  })

  it.each([false, true])(
    'preserves install results when detection fails (installed: %s)',
    async (installed) => {
      const refreshDetection = vi
        .fn()
        .mockRejectedValue(new Error('probe unavailable'))
      const result = installed
        ? { ok: true as const, releaseVersion: '9.0.2-motrix.2' }
        : { ok: false as const, error: 'verification' as const }
      vi.mocked(installServerFfmpeg).mockResolvedValueOnce(result)
      const handlers = buildServerCommandHandlers({
        ...makeFakeCtx(),
        capabilityHost: { ffmpeg: { refreshDetection } },
      } as unknown as ServerCommandContext)
      await expect(handlers[Commands.InstallFfmpeg]?.()).resolves.toEqual(
        result
      )
      expect(refreshDetection).toHaveBeenCalledTimes(installed ? 1 : 0)
    }
  )
})

function makeSettings(
  proxy: typeof PROXY_OFF,
  tracker: {
    sourcesEnabled?: boolean
    blacklistEnabled?: boolean
    autoSync?: boolean
    syncIntervalHours?: number
    maxTrackerCount?: number
  } = {},
  engine: { dnsMode?: 'auto' | 'system' | 'engine'; split?: number } = {},
  app: { defaultSaveDir?: string; browserBridgeEnabled?: boolean } = {},
  bridge: { fixedPort?: 'auto' | number } = {}
) {
  return {
    app: {
      defaultSaveDir: '/downloads',
      browserBridgeEnabled: true,
      ...app,
    },
    bridge: { fixedPort: 'auto' as const, ...bridge },
    proxy,
    tracker: {
      sourcesEnabled: true,
      blacklistEnabled: true,
      autoSync: true,
      syncIntervalHours: 24,
      ...tracker,
    },
    engine: {
      dnsMode: 'auto',
      ...engine,
    },
  }
}

describe('server disclaimer language', () => {
  it('persists system and waits for locale application before reporting success', async () => {
    let finish!: () => void
    const applyLocale = vi.fn(
      () =>
        new Promise<void>((resolve) => {
          finish = resolve
        })
    )
    const ctx = makeFakeCtx()
    const setDisclaimerLanguage = vi.fn().mockResolvedValue({ saved: true })
    const handlers = buildServerCommandHandlers({
      ...ctx,
      settingsManager: { ...ctx.settingsManager, setDisclaimerLanguage },
      applyLocale,
    } as unknown as ServerCommandContext)
    let settled = false
    const pending = handlers[Commands.SetDisclaimerLanguage]?.('system').then(
      (value) => {
        settled = true
        return value
      }
    )
    await vi.waitFor(() => expect(applyLocale).toHaveBeenCalledWith('system'))
    expect(setDisclaimerLanguage).toHaveBeenCalledWith('system')
    expect(settled).toBe(false)
    finish()
    await expect(pending).resolves.toEqual({ ok: true })
    await expect(
      handlers[Commands.SetDisclaimerLanguage]?.('unknown')
    ).rejects.toThrow()
    expect(setDisclaimerLanguage).toHaveBeenCalledOnce()
  })
})

describe('server Commands.UpdateSettings', () => {
  it.each(['zh-CN', 'system'])(
    'awaits locale application and permits retrying saved %s',
    async (language) => {
      const base = makeSettings(PROXY_OFF)
      const current = { ...base, app: { ...base.app, language } }
      let rejectLocale!: (error: Error) => void
      const applyLocale = vi
        .fn()
        .mockImplementationOnce(
          () =>
            new Promise<void>((_, reject) => {
              rejectLocale = reject
            })
        )
        .mockResolvedValue(undefined)
      const ctx = { ...makeFakeCtx(), applyLocale }
      vi.mocked(ctx.settingsManager.get).mockReturnValue(current as never)
      vi.mocked(ctx.settingsManager.update).mockResolvedValue({
        saved: true,
        requiresRestart: false,
        changedRestartKeys: [],
        requiresAppRestart: false,
        changedAppRestartKeys: [],
      })
      const update = buildServerCommandHandlers(
        ctx as unknown as ServerCommandContext
      )[Commands.UpdateSettings]!
      let settled = false
      const pending = update({ app: { language } }).then((value) => {
        settled = true
        return value
      })
      await vi.waitFor(() => expect(applyLocale).toHaveBeenCalledWith(language))
      expect(settled).toBe(false)
      rejectLocale(new Error('locale apply failed'))
      await expect(pending).resolves.toMatchObject({
        saved: true,
        applicationFailed: true,
      })
      await expect(update({ app: { language } })).resolves.toMatchObject({
        saved: true,
      })
      expect(applyLocale).toHaveBeenCalledTimes(2)
    }
  )

  it('uses Server path policy before one General commit and does not apply partial fields on an outside-root destination', async () => {
    const root = await mkdtemp(path.join(os.tmpdir(), 'motrix-server-general-'))
    try {
      const allowed = path.join(root, 'allowed')
      const outside = path.join(root, 'outside')
      await mkdir(outside)
      const policy = await createServerDownloadPathPolicy({
        defaultSaveDir: allowed,
        allowedSaveDirsValue: allowed,
      })
      const destination = path.join(allowed, 'destination')
      await mkdir(destination)
      const manager = new SettingsManager(path.join(root, 'settings.json'), {
        defaultSaveDir: allowed,
      })
      await manager.load()
      const ctx = {
        ...makeFakeCtx(),
        settingsManager: manager,
        downloadPathPolicy: policy,
        serverDirectoryService: new ServerDirectoryService(policy),
      }
      const save = buildServerCommandHandlers(
        ctx as unknown as ServerCommandContext
      )[Commands.SaveGeneralSettings]
      expect(
        await save?.({
          expectedRevision: manager.getGeneralSettingsSnapshot().revision,
          app: { defaultSaveDir: destination, notifyOnComplete: false },
          directories: {
            addFavorites: [destination],
            removeFavorites: [],
            removeRecent: [],
          },
        })
      ).toMatchObject({
        ok: true,
        value: {
          directoryPreferences: { favorites: [destination], recent: [] },
        },
      })
      expect(manager.getApp()).toMatchObject({
        defaultSaveDir: await realpath(destination),
        notifyOnComplete: false,
      })
      expect(
        ctx.supervisor.applyDefaultSaveDir
      ).toHaveBeenCalledExactlyOnceWith(await realpath(destination))
      vi.mocked(ctx.supervisor.applyDefaultSaveDir).mockClear()
      const before = structuredClone(manager.getApp())
      expect(
        await save?.({
          expectedRevision: manager.getGeneralSettingsSnapshot().revision,
          app: { defaultSaveDir: outside, notifyOnComplete: true },
          directories: {
            addFavorites: [],
            removeFavorites: [destination],
            removeRecent: [],
          },
        })
      ).toEqual({ ok: false, error: { code: 'outsideRoots' } })
      expect(
        await save?.({
          expectedRevision: manager.getGeneralSettingsSnapshot().revision,
          app: { notifyOnComplete: true },
          directories: {
            addFavorites: [outside],
            removeFavorites: [destination],
            removeRecent: [],
          },
        })
      ).toEqual({ ok: false, error: { code: 'outsideRoots' } })
      expect(manager.getApp()).toEqual(before)
      expect(ctx.supervisor.applyDefaultSaveDir).not.toHaveBeenCalled()
      const authorize = vi.spyOn(
        ctx.serverDirectoryService,
        'resolvePreferenceDirectory'
      )
      expect(
        await save?.({
          expectedRevision: manager.getGeneralSettingsSnapshot().revision,
          app: { defaultSaveDir: destination, extra: true },
          directories: {
            addFavorites: [destination],
            removeFavorites: [],
            removeRecent: [],
          },
        })
      ).toEqual({ ok: false, error: { code: 'invalidPath' } })
      expect(authorize).not.toHaveBeenCalled()
    } finally {
      await rm(root, { recursive: true, force: true })
    }
  })

  it('preserves latest queue preferences against stale/invalid app patches through Server handlers', async () => {
    const root = await mkdtemp(
      path.join(os.tmpdir(), 'motrix-server-preferences-')
    )
    try {
      const policy = await createServerDownloadPathPolicy({
        defaultSaveDir: root,
        allowedSaveDirsValue: root,
      })
      const manager = new SettingsManager(path.join(root, 'settings.json'), {
        defaultSaveDir: root,
      })
      await manager.load()
      const staleApp = manager.getApp()
      const ctx = {
        ...makeFakeCtx(),
        settingsManager: manager,
        downloadPathPolicy: policy,
        serverDirectoryService: new ServerDirectoryService(policy),
      }
      const handlers = buildServerCommandHandlers(
        ctx as unknown as ServerCommandContext
      )
      expect(
        await handlers[Commands.MutateDirectoryPreferences]?.({
          action: 'addFavorite',
          path: root,
        })
      ).toEqual({ ok: true, value: { favorites: [root], recent: [] } })
      await handlers[Commands.UpdateSettings]?.({
        app: { ...staleApp, theme: 'dark' },
      })
      await handlers[Commands.UpdateSettings]?.({
        app: {
          directoryPreferences: { corrupt: true },
          notifyOnComplete: false,
        },
      })
      expect(manager.getApp()).toMatchObject({
        theme: 'dark',
        notifyOnComplete: false,
        directoryPreferences: { favorites: [root], recent: [] },
      })
      const queries = buildServerQueryHandlers(
        ctx as unknown as Parameters<typeof buildServerQueryHandlers>[0]
      )
      expect(await queries[Queries.GetDirectoryPreferences]?.({})).toEqual({
        ok: true,
        value: { favorites: [root], recent: [] },
      })
      expect(
        await queries[Queries.ListServerDirectoryLocations]?.({})
      ).toMatchObject({
        ok: true,
        value: { favorites: [{ path: root, sourcePaths: [root] }] },
      })
      expect(
        await handlers[Commands.MutateDirectoryPreferences]?.({
          action: 'recordRecent',
          path: path.dirname(root),
        })
      ).toEqual({ ok: false, error: { code: 'outsideRoots' } })
      expect(
        await handlers[Commands.MutateDirectoryPreferences]?.({
          action: 'removeFavorite',
          paths: [root],
        })
      ).toEqual({ ok: true, value: { favorites: [], recent: [] } })
      const target = path.join(root, 'target')
      const alias = path.join(root, 'alias')
      await mkdir(target)
      await symlink(target, alias)
      await handlers[Commands.MutateDirectoryPreferences]?.({
        action: 'addFavorite',
        path: alias,
      })
      await handlers[Commands.MutateDirectoryPreferences]?.({
        action: 'addFavorite',
        path: target,
      })
      expect(
        await queries[Queries.ListServerDirectoryLocations]?.({})
      ).toMatchObject({
        ok: true,
        value: { favorites: [{ path: alias, sourcePaths: [alias, target] }] },
      })
      expect(
        await handlers[Commands.MutateDirectoryPreferences]?.({
          action: 'removeFavorite',
          paths: [alias, target],
        })
      ).toEqual({ ok: true, value: { favorites: [], recent: [] } })
    } finally {
      await rm(root, { recursive: true, force: true })
    }
  })

  it('round-trips selected root/internal aliases and whitespace through real settings Apply, reload, bootstrap and default preparation', async () => {
    const temporary = await mkdtemp(
      path.join(os.tmpdir(), 'motrix-picker-settings-')
    )
    try {
      const realRoot = path.join(temporary, 'real')
      const alias = path.join(temporary, 'alias')
      await mkdir(realRoot)
      await symlink(realRoot, alias)
      await mkdir(path.join(realRoot, 'target'))
      await symlink(
        path.join(realRoot, 'target'),
        path.join(realRoot, 'internal')
      )
      const policy = await createServerDownloadPathPolicy({
        defaultSaveDir: alias,
        allowedSaveDirsValue: alias,
      })
      const service = new ServerDirectoryService(policy)
      const settingsPath = path.join(temporary, 'settings.json')
      const manager = new SettingsManager(settingsPath, {
        defaultSaveDir: alias,
      })
      await manager.load()
      const ctx = {
        ...makeFakeCtx(),
        downloadPathPolicy: policy,
        serverDirectoryService: service,
        settingsManager: manager,
      }
      const handlers = buildServerCommandHandlers(
        ctx as unknown as ServerCommandContext
      )
      for (const name of process.platform === 'win32'
        ? ['Movies']
        : ['Movies', 'Movies ', ' ']) {
        const logical = path.join(alias, 'internal', name)
        expect(
          (
            await service.create({
              parentPath: path.join(alias, 'internal'),
              name,
            })
          ).ok
        ).toBe(true)
        expect(await service.validate({ path: logical })).toEqual({
          ok: true,
          value: { path: logical },
        })
        await handlers[Commands.UpdateSettings]?.({
          app: { defaultSaveDir: logical },
        })
        const canonical = await realpath(logical)
        expect(manager.getApp().defaultSaveDir).toBe(canonical)
        const reloaded = new SettingsManager(settingsPath, {
          defaultSaveDir: alias,
        })
        await reloaded.load()
        expect(reloaded.getApp().defaultSaveDir).toBe(canonical)
        const queries = buildServerQueryHandlers({
          ...ctx,
          settingsManager: reloaded,
        } as unknown as Parameters<typeof buildServerQueryHandlers>[0])
        const bootstrap = await queries[Queries.ListAllowedSaveDirs]?.()
        expect(bootstrap).toMatchObject({
          defaultPath: canonical,
          paths: [{ path: alias }],
        })
        expect(await service.validate({ path: canonical })).toEqual({
          ok: true,
          value: { path: path.join(alias, 'target', name) },
        })
        const restarted = await createServerDownloadPathPolicy({
          defaultSaveDir: reloaded.getApp().defaultSaveDir,
          allowedSaveDirsValue: alias,
        })
        expect(await restarted.prepareSaveDir('')).toBe(canonical)
        expect(
          await policy.prepareSaveDir(reloaded.getApp().defaultSaveDir)
        ).toBe(canonical)
      }
      await expect(
        policy.prepareSaveDir(path.join(temporary, 'outside'))
      ).rejects.toThrow('outside MOTRIX_ALLOWED_SAVE_DIRS')
    } finally {
      await rm(temporary, { recursive: true, force: true })
    }
  })

  it('delegates folder creation to the directory service', async () => {
    const ctx = makeFakeCtx()
    const request = { parentPath: '/downloads', name: 'new' }
    const result = { ok: true, value: { path: '/downloads/new', name: 'new' } }
    ctx.serverDirectoryService.create.mockResolvedValue(result)
    const handlers = buildServerCommandHandlers(
      ctx as unknown as ServerCommandContext
    )
    expect(await handlers[Commands.CreateServerDirectory]?.(request)).toEqual(
      result
    )
    expect(ctx.serverDirectoryService.create).toHaveBeenCalledExactlyOnceWith(
      request
    )
  })
  it('does not invoke proxyApplier when proxy unchanged', async () => {
    const ctx = makeFakeCtx()
    const settings = makeSettings(PROXY_OFF)
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>).mockReturnValue(
      settings
    )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await handlers[Commands.UpdateSettings]?.({})
    expect(ctx.proxyApplier.apply).not.toHaveBeenCalled()
    expect(ctx.supervisor.stop).not.toHaveBeenCalled()
  })

  it('reasserts every proxy scope when proxy fields changed', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(makeSettings(PROXY_OFF))
      .mockReturnValue(makeSettings(PROXY_ON))
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await handlers[Commands.UpdateSettings]?.({ proxy: PROXY_ON })
    expect(ctx.proxyApplier.applyAll).toHaveBeenCalledWith(PROXY_ON)
    expect(ctx.proxyApplier.apply).not.toHaveBeenCalled()
  })

  it('commits the exact route returned by a successful proxy hot-apply', async () => {
    const ctx = makeFakeCtx()
    const policy = new AppliedDownloadProxyPolicy({ noProxy: '' })
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(makeSettings(PROXY_OFF))
      .mockReturnValue(makeSettings(PROXY_ON))
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    ;(ctx.proxyApplier.applyAll as ReturnType<typeof vi.fn>).mockResolvedValue({
      downloadProxy: 'applied',
      appliedProxy: {
        allProxy: 'http://bridge-user:[email protected]:43123',
        noProxy: 'localhost',
      },
    })
    const handlers = buildServerCommandHandlers({
      ...ctx,
      appliedDownloadProxyPolicy: policy,
    } as Parameters<typeof buildServerCommandHandlers>[0])

    await handlers[Commands.UpdateSettings]?.({ proxy: PROXY_ON })

    expect(policy.snapshot()).toEqual({
      proxy: 'http://bridge-user:[email protected]:43123',
      noProxy: 'localhost',
    })
  })

  it('leaves the route unavailable when proxy hot-apply throws', async () => {
    const ctx = makeFakeCtx()
    const policy = new AppliedDownloadProxyPolicy({ noProxy: '' })
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(makeSettings(PROXY_OFF))
      .mockReturnValue(makeSettings(PROXY_ON))
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    ;(ctx.proxyApplier.applyAll as ReturnType<typeof vi.fn>).mockRejectedValue(
      new Error('RPC failed')
    )
    const handlers = buildServerCommandHandlers({
      ...ctx,
      appliedDownloadProxyPolicy: policy,
    } as Parameters<typeof buildServerCommandHandlers>[0])

    await expect(
      handlers[Commands.UpdateSettings]?.({ proxy: PROXY_ON })
    ).resolves.toMatchObject({ applicationFailed: true })
    expect(policy.snapshot()).toBeNull()
  })

  it('force-reapplies the same proxy after a failed hot apply', async () => {
    const ctx = makeFakeCtx()
    const policy = new AppliedDownloadProxyPolicy({ noProxy: '' })
    const before = makeSettings(PROXY_OFF)
    const after = makeSettings(PROXY_ON)
    let current = before
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>).mockImplementation(
      () => current
    )
    ;(
      ctx.settingsManager.update as ReturnType<typeof vi.fn>
    ).mockImplementation(async () => {
      current = after
      return { ok: true, requiresRestart: false, changedRestartKeys: [] }
    })
    ;(ctx.proxyApplier.applyAll as ReturnType<typeof vi.fn>)
      .mockRejectedValueOnce(new Error('RPC failed'))
      .mockResolvedValueOnce({
        downloadProxy: 'applied',
        appliedProxy: {
          allProxy: 'http://p.example:80',
          noProxy: '',
        },
      })
    const handlers = buildServerCommandHandlers({
      ...ctx,
      appliedDownloadProxyPolicy: policy,
    } as Parameters<typeof buildServerCommandHandlers>[0])

    await expect(
      handlers[Commands.UpdateSettings]?.({ proxy: PROXY_ON })
    ).resolves.toMatchObject({ applicationFailed: true })
    expect(policy.snapshot()).toBeNull()

    await expect(
      handlers[Commands.UpdateSettings]?.({ proxy: PROXY_ON })
    ).resolves.toBeDefined()
    expect(ctx.proxyApplier.apply).not.toHaveBeenCalled()
    expect(ctx.proxyApplier.applyAll).toHaveBeenCalledTimes(2)
    expect(ctx.proxyApplier.applyAll).toHaveBeenLastCalledWith(PROXY_ON)
    expect(policy.snapshot()).toEqual({
      proxy: 'http://p.example:80',
      noProxy: '',
    })
  })

  it('does not lose a download proxy when concurrent updates change different scopes', async () => {
    const ctx = makeFakeCtx()
    const policy = new AppliedDownloadProxyPolicy({ noProxy: '' })
    const proxyA = {
      ...PROXY_ON,
      scopes: {
        download: true,
        updateApp: false,
        updateTrackers: false,
        plugins: false,
      },
    }
    const proxyB = {
      ...proxyA,
      scopes: {
        download: true,
        updateApp: true,
        updateTrackers: false,
        plugins: false,
      },
    }
    const before = makeSettings(PROXY_OFF)
    const afterA = makeSettings(proxyA)
    const afterB = makeSettings(proxyB)
    let current = before
    let releaseReader!: () => void
    let readerStarted = false
    const readerGate = new Promise<void>((resolve) => {
      releaseReader = resolve
    })
    const reader = policy.runWithSnapshot(async () => {
      readerStarted = true
      await readerGate
    })
    await vi.waitFor(() => expect(readerStarted).toBe(true))
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>).mockImplementation(
      () => current
    )
    ;(
      ctx.settingsManager.update as ReturnType<typeof vi.fn>
    ).mockImplementation(async (partial: { proxy?: { scopes?: object } }) => {
      current = partial.proxy?.scopes === proxyA.scopes ? afterA : afterB
      return { ok: true, requiresRestart: false, changedRestartKeys: [] }
    })
    ;(ctx.proxyApplier.applyAll as ReturnType<typeof vi.fn>).mockImplementation(
      async (next) => ({
        downloadProxy: 'applied',
        appliedProxy: {
          allProxy: `http://${next.host}:${next.port}`,
          noProxy: '',
        },
      })
    )
    const handlers = buildServerCommandHandlers({
      ...ctx,
      appliedDownloadProxyPolicy: policy,
    } as Parameters<typeof buildServerCommandHandlers>[0])

    const first = handlers[Commands.UpdateSettings]?.({ proxy: proxyA })
    await vi.waitFor(() => expect(current).toBe(afterA))
    const second = handlers[Commands.UpdateSettings]?.({ proxy: proxyB })
    await vi.waitFor(() => expect(current).toBe(afterB))
    releaseReader()
    await Promise.all([reader, first, second])

    expect(ctx.proxyApplier.apply).not.toHaveBeenCalled()
    expect(ctx.proxyApplier.applyAll).toHaveBeenCalledExactlyOnceWith(proxyB)
    expect(policy.snapshot()).toEqual({
      proxy: 'http://p.example:80',
      noProxy: '',
    })
  })

  it('hot-applies a changed default save directory', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {}, {}, { defaultSaveDir: '/downloads/old' })
      )
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {}, {}, { defaultSaveDir: '/downloads/new' })
      )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.UpdateSettings]?.({
      app: { defaultSaveDir: '/downloads/new' },
    })

    expect(ctx.supervisor.applyDefaultSaveDir).toHaveBeenCalledExactlyOnceWith(
      '/downloads/new'
    )
    expect(ctx.bridgeControl.restart).not.toHaveBeenCalled()
    expect(ctx.bridgeControl.setEnabled).not.toHaveBeenCalled()
  })

  it('hot-applies the browser bridge master switch', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {}, {}, { browserBridgeEnabled: true })
      )
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {}, {}, { browserBridgeEnabled: false })
      )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.UpdateSettings]?.({
      app: { browserBridgeEnabled: false },
    })

    expect(ctx.bridgeControl.setEnabled).toHaveBeenCalledExactlyOnceWith(false)
    expect(ctx.bridgeControl.restart).not.toHaveBeenCalled()
  })

  it('restarts an enabled bridge when its fixed port changes', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(makeSettings(PROXY_OFF))
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {}, {}, {}, { fixedPort: 18080 })
      )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.UpdateSettings]?.({ bridge: { fixedPort: 18080 } })

    expect(ctx.bridgeControl.restart).toHaveBeenCalledOnce()
  })

  it('hot-applies async-dns and resets the fallback latch when dnsMode changes', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(makeSettings(PROXY_OFF, {}, { dnsMode: 'auto' }))
      .mockReturnValueOnce(makeSettings(PROXY_OFF, {}, { dnsMode: 'system' }))
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await handlers[Commands.UpdateSettings]?.({ engine: { dnsMode: 'system' } })
    expect(ctx.supervisor.applyAsyncDns).toHaveBeenCalledExactlyOnceWith(false)
    expect(ctx.dnsFallback.reset).toHaveBeenCalledOnce()
    expect(ctx.supervisor.stop).not.toHaveBeenCalled()
  })

  it('leaves async-dns alone when dnsMode is unchanged', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>).mockReturnValue(
      makeSettings(PROXY_OFF)
    )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await handlers[Commands.UpdateSettings]?.({})
    expect(ctx.supervisor.applyAsyncDns).not.toHaveBeenCalled()
    expect(ctx.dnsFallback.reset).not.toHaveBeenCalled()
  })

  it('publishes a restart reminder without stopping the engine', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>).mockReturnValue(
      makeSettings(PROXY_OFF)
    )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: true, changedRestartKeys: ['rpcPort'] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await handlers[Commands.UpdateSettings]?.({ engine: { rpcPort: 9000 } })
    expect(ctx.supervisor.stop).not.toHaveBeenCalled()
    expect(ctx.supervisor.start).not.toHaveBeenCalled()
    expect(ctx.notificationCenter.notify).toHaveBeenCalledWith(
      expect.objectContaining({
        kind: 'engine-restart-required',
        severity: 'warning',
      })
    )
    expect(ctx.eventBus.emit).toHaveBeenCalledWith(
      Events.EngineRestartRequired,
      { changedKeys: ['rpcPort'] }
    )
  })

  it('hot-applies runtime engine settings without a restart reminder', async () => {
    const ctx = makeFakeCtx()
    const before = makeSettings(PROXY_OFF, {}, { dnsMode: 'auto', split: 16 })
    const after = makeSettings(PROXY_OFF, {}, { dnsMode: 'auto', split: 32 })
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(before)
      .mockReturnValueOnce(after)
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.UpdateSettings]?.({ engine: { split: 32 } })

    expect(ctx.supervisor.applyEngineSettings).toHaveBeenCalledWith(
      before.engine,
      after.engine
    )
    expect(ctx.notificationCenter.notify).not.toHaveBeenCalled()
  })

  it('reselects when the configured cap changes without fetching sources', async () => {
    const ctx = makeFakeCtx()
    const before = makeSettings(PROXY_OFF, { maxTrackerCount: 50 })
    const after = makeSettings(PROXY_OFF, { maxTrackerCount: 10 })
    const settingsManager = {
      ...ctx.settingsManager,
      get: vi.fn().mockReturnValueOnce(before).mockReturnValueOnce(after),
      update: vi.fn().mockResolvedValue({
        ok: true,
        requiresRestart: false,
        changedRestartKeys: [],
      }),
    }
    const handlers = buildServerCommandHandlers({
      ...ctx,
      settingsManager,
    } as unknown as ServerCommandContext)
    await handlers[Commands.UpdateSettings]?.({
      tracker: { maxTrackerCount: 10 },
    })
    expect(ctx.trackerManager.applySelectionChange).toHaveBeenCalledOnce()
    expect(ctx.trackerManager.syncAndCurate).not.toHaveBeenCalled()
    await handlers[Commands.RetryTrackerSources]?.()
    expect(ctx.trackerManager.syncAndCurate).toHaveBeenCalledWith('retry')
  })

  it('calls trackerManager.applySourcesChange when sourcesEnabled changes', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {
          sourcesEnabled: true,
          blacklistEnabled: true,
        })
      )
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {
          sourcesEnabled: false,
          blacklistEnabled: true,
        })
      )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await handlers[Commands.UpdateSettings]?.({
      tracker: { sourcesEnabled: false },
    })
    expect(ctx.trackerManager.applySourcesChange).toHaveBeenCalledWith(false)
    expect(ctx.trackerManager.applyBlacklistChange).not.toHaveBeenCalled()
  })

  it('calls trackerManager.applyBlacklistChange when blacklistEnabled changes', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {
          sourcesEnabled: true,
          blacklistEnabled: true,
        })
      )
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {
          sourcesEnabled: true,
          blacklistEnabled: false,
        })
      )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await handlers[Commands.UpdateSettings]?.({
      tracker: { blacklistEnabled: false },
    })
    expect(ctx.trackerManager.applyBlacklistChange).toHaveBeenCalledWith(false)
    expect(ctx.trackerManager.applySourcesChange).not.toHaveBeenCalled()
  })

  it('calls both apply* methods when both toggle in one patch', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {
          sourcesEnabled: true,
          blacklistEnabled: true,
        })
      )
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, {
          sourcesEnabled: false,
          blacklistEnabled: false,
        })
      )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await handlers[Commands.UpdateSettings]?.({
      tracker: { sourcesEnabled: false, blacklistEnabled: false },
    })
    expect(ctx.trackerManager.applySourcesChange).toHaveBeenCalledWith(false)
    expect(ctx.trackerManager.applyBlacklistChange).toHaveBeenCalledWith(false)
  })

  it('does not call apply* methods when tracker toggles unchanged', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>).mockReturnValue(
      makeSettings(PROXY_OFF, { sourcesEnabled: true, blacklistEnabled: true })
    )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await handlers[Commands.UpdateSettings]?.({})
    expect(ctx.trackerManager.applySourcesChange).not.toHaveBeenCalled()
    expect(ctx.trackerManager.applyBlacklistChange).not.toHaveBeenCalled()
    expect(ctx.trackerManager.applySyncScheduleChange).not.toHaveBeenCalled()
  })

  it('re-arms tracker auto-sync when its enablement or interval changes', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>)
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, { autoSync: true, syncIntervalHours: 24 })
      )
      .mockReturnValueOnce(
        makeSettings(PROXY_OFF, { autoSync: false, syncIntervalHours: 12 })
      )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.UpdateSettings]?.({
      tracker: { autoSync: false, syncIntervalHours: 12 },
    })

    expect(ctx.trackerManager.applySyncScheduleChange).toHaveBeenCalledOnce()
  })

  it('rejects a disallowed save directory before committing or changing the bridge', async () => {
    const ctx = makeFakeCtx()
    const oldSettings = new SettingsManager('/unused-settings.json').get()
    oldSettings.app.defaultSaveDir = '/downloads/old'
    vi.mocked(ctx.settingsManager.get).mockReturnValue(oldSettings)
    vi.mocked(ctx.downloadPathPolicy.prepareSaveDir).mockRejectedValue(
      new Error('outside allowed roots')
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await expect(
      handlers[Commands.UpdateSettings]?.({
        app: { defaultSaveDir: '/outside' },
      })
    ).rejects.toThrow('outside allowed roots')
    expect(ctx.settingsManager.update).not.toHaveBeenCalled()
    expect(ctx.settingsManager.get().app.defaultSaveDir).toBe('/downloads/old')
    expect(ctx.supervisor.applyDefaultSaveDir).not.toHaveBeenCalled()
    expect(ctx.bridgeControl.restart).not.toHaveBeenCalled()
    expect(ctx.bridgeControl.setEnabled).not.toHaveBeenCalled()
  })

  it('validates a default save directory before persisting settings', async () => {
    const ctx = makeFakeCtx()
    ;(
      ctx.downloadPathPolicy.prepareSaveDir as ReturnType<typeof vi.fn>
    ).mockResolvedValue('/downloads/media-canonical')
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>).mockReturnValue(
      makeSettings(PROXY_OFF)
    )
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      { ok: true, requiresRestart: false, changedRestartKeys: [] }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.UpdateSettings]?.({
      app: { defaultSaveDir: '/downloads/media' },
    })

    expect(ctx.downloadPathPolicy.prepareSaveDir).toHaveBeenCalledWith(
      '/downloads/media'
    )
    expect(ctx.settingsManager.update).toHaveBeenCalledWith({
      app: { defaultSaveDir: '/downloads/media-canonical' },
    })
  })
})

describe('server Commands.UpdateGeoIPDatabase', () => {
  it('delegates the update to the shared GeoIP manager', async () => {
    const ctx = makeFakeCtx()
    const status = {
      enabled: true,
      hasDatabase: true,
      loaded: true,
      lastUpdatedAt: 1_800_000_000_000,
      databaseVersion: 'v1',
      sizeBytes: 9_000_000,
      isDownloading: false,
      lastError: null,
    }
    ctx.geoipManager.triggerUpdate.mockResolvedValue(status)
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await expect(handlers[Commands.UpdateGeoIPDatabase]?.()).resolves.toBe(
      status
    )
    expect(ctx.geoipManager.triggerUpdate).toHaveBeenCalledOnce()
  })
})

describe('server Commands.CreateTask magnet metadata selection', () => {
  it('returns file selection to the requesting WebUI without broadcasting', async () => {
    const ctx = makeFakeCtx()
    const selection = { taskId: 'ready-1', torrentBase64: 'dG9ycmVudA==' }
    vi.mocked(ctx.magnetTracker.getFileSelection).mockResolvedValue(
      selection as never
    )
    const handlers = buildServerCommandHandlers(
      ctx as unknown as ServerCommandContext
    )
    await expect(
      handlers[Commands.ReopenMagnetFileSelection]?.('ready-1')
    ).resolves.toEqual({
      ok: true,
      selection,
    })
    expect(ctx.magnetTracker.getFileSelection).toHaveBeenCalledWith('ready-1')
    expect(ctx.eventBus.emit).not.toHaveBeenCalled()
  })

  it('returns a stale selection as null', async () => {
    const handlers = buildServerCommandHandlers(
      makeFakeCtx() as unknown as ServerCommandContext
    )
    await expect(
      handlers[Commands.ReopenMagnetFileSelection]?.('finished-1')
    ).resolves.toEqual({
      ok: true,
      selection: null,
    })
  })
  it('submits bare magnet to metadata selection when enabled', async () => {
    const ctx = makeFakeCtx()
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    const result = await handlers[Commands.CreateTask]?.({
      type: 'bt',
      payload: {
        kind: 'magnet',
        uri: 'magnet:?xt=urn:btih:a03e3f9a05341aa336e9d9d3f06b33cddafe0bdc',
      },
      selectedFiles: [],
      saveDir: '/downloads',
    })

    expect(result).toEqual({ ok: true })
    expect(ctx.magnetTracker.submit).toHaveBeenCalledWith(
      'magnet:?xt=urn:btih:a03e3f9a05341aa336e9d9d3f06b33cddafe0bdc',
      '/downloads'
    )
    expect(ctx.downloadPathPolicy.prepareSaveDir).toHaveBeenCalledWith(
      '/downloads'
    )
  })

  it('admits a selection swap through the canonical task lock', async () => {
    const ctx = makeFakeCtx()
    ctx.motrixDatabase = {
      ...ctx.motrixDatabase,
      getTask: vi.fn(() => null),
    } as unknown as MotrixDatabase
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await expect(
      handlers[Commands.CreateTask]?.({
        type: 'bt',
        payload: { kind: 'torrent-base64', base64: 'AAAA' },
        selectedFiles: [0],
        saveDir: '/downloads',
        existingTaskId: 'metadata-task',
      })
    ).rejects.toMatchObject({ code: 'TASK_NOT_FOUND' })

    expect(ctx.runTaskMutation).toHaveBeenCalledWith(
      ['metadata-task'],
      expect.any(Function)
    )
  })
})

describe('server plural task commands', () => {
  it.each(['singular', 'plural'] as const)(
    'controls media segments through %s Web commands',
    async (arity) => {
      const ctx = makeFakeCtx()
      ctx.taskManager = new TaskManager()
      ctx.taskManager.set(
        'media',
        makeDownloadTask({
          id: 'media',
          engineTaskId: '',
          kind: TaskKind.Hls,
          status: TaskStatus.Downloading,
        })
      )
      ctx.adapter.pauseTask = vi.fn(async () => {})
      ctx.adapter.resumeTask = vi.fn(async () => {})
      let gids = ['video-segment', 'audio-segment']
      const handlers = buildServerCommandHandlers({
        ...ctx,
        persistTask: vi.fn(async () => {}),
        getMediaSegmentGids: () => gids,
      } as ServerCommandContext)
      await handlers[
        arity === 'singular' ? Commands.PauseTask : Commands.PauseTasks
      ]!(arity === 'singular' ? 'media' : ['media'])
      expect(ctx.adapter.pauseTask).toHaveBeenCalledWith('video-segment')
      expect(ctx.adapter.pauseTask).toHaveBeenCalledWith('audio-segment')
      expect(ctx.taskManager.getById('media')?.status).toBe(TaskStatus.Paused)
      gids = ['next-segment']
      await handlers[
        arity === 'singular' ? Commands.ResumeTask : Commands.ResumeTasks
      ]!(arity === 'singular' ? 'media' : ['media'])
      expect(ctx.adapter.resumeTask).toHaveBeenCalledExactlyOnceWith(
        'next-segment'
      )
      expect(ctx.taskManager.getById('media')?.status).toBe(
        TaskStatus.Downloading
      )
    }
  )

  it.each(['singular', 'plural'] as const)(
    'awaits media cancellation before %s Web removal deletes the parent',
    async (arity) => {
      const ctx = makeFakeCtx()
      ctx.taskManager = new TaskManager()
      ctx.taskManager.set(
        'media',
        makeDownloadTask({
          id: 'media',
          engineTaskId: '',
          kind: TaskKind.Hls,
          status: TaskStatus.Downloading,
        })
      )
      ctx.motrixDatabase.deleteTask = vi.fn()
      let release!: () => void
      const draining = new Promise<void>((resolve) => {
        release = resolve
      })
      const cancelMedia = vi.fn(() => draining)
      const handlers = buildServerCommandHandlers({
        ...ctx,
        cancelMedia,
      } as ServerCommandContext)
      const pending =
        arity === 'singular'
          ? handlers[Commands.RemoveTask]!({
              taskId: 'media',
              deleteWithFiles: false,
            })
          : handlers[Commands.RemoveTasks]!({
              taskIds: ['media'],
              deleteWithFiles: false,
            })
      await vi.waitFor(() =>
        expect(cancelMedia).toHaveBeenCalledExactlyOnceWith('media')
      )
      expect(ctx.taskManager.getById('media')).toBeDefined()
      expect(ctx.motrixDatabase.deleteTask).not.toHaveBeenCalled()
      release()
      await pending
      expect(ctx.taskManager.getById('media')).toBeUndefined()
      expect(ctx.motrixDatabase.deleteTask).toHaveBeenCalledWith('media')
    }
  )

  it('MoveTasks validates the payload and changes the actual engine queue', async () => {
    const ctx = makeFakeCtx()
    const queue = ['first', 'second']
    vi.mocked(ctx.taskManager.getById).mockReturnValue({
      id: 'task-second',
      engineTaskId: 'second',
      status: TaskStatus.Paused,
    } as never)
    Object.assign(ctx.adapter, {
      listWaitingTaskIds: vi.fn(async () => [...queue]),
      changePosition: vi.fn(async () => {
        queue.reverse()
        return 0
      }),
    })
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await expect(
      handlers[Commands.MoveTasks]?.({
        taskIds: ['task-second'],
        direction: 'up',
      })
    ).resolves.toEqual({ moved: ['task-second'], unchanged: [], failed: [] })
    expect(queue).toEqual(['second', 'first'])
    await expect(
      handlers[Commands.MoveTasks]?.({ taskIds: [], direction: 'up' })
    ).rejects.toThrow()
    await expect(
      handlers[Commands.MoveTasks]?.({
        taskIds: ['task-second'],
        direction: 'sideways',
      })
    ).rejects.toThrow()
  })

  it('PauseTasks fans out per id and returns the IPC-safe bulk result', async () => {
    const ctx = makeFakeCtx()
    const tasks = new Map([
      [
        't-1',
        {
          id: 't-1',
          engineTaskId: 'gid-1',
          status: TaskStatus.Downloading,
          instances: [],
        },
      ],
      [
        't-2',
        {
          id: 't-2',
          engineTaskId: 'gid-2',
          status: TaskStatus.Downloading,
          instances: [],
        },
      ],
    ])
    vi.mocked(ctx.taskManager.getById).mockImplementation(
      (id: string) => tasks.get(id) as never
    )
    ;(ctx.taskManager as unknown as { set: unknown }).set = vi.fn()
    ctx.adapter.pauseTask = vi.fn(async (gid: string) => {
      if (gid === 'gid-2') throw new Error('engine rejected')
    })
    ctx.adapter.getTaskStatus = vi.fn().mockResolvedValue(null)
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await expect(
      handlers[Commands.PauseTasks]?.(['t-1', 't-2'])
    ).resolves.toEqual({
      succeeded: ['t-1'],
      failed: [{ taskId: 't-2', reason: 'engine rejected' }],
    })
  })

  it('PauseTasks rejects a malformed payload', async () => {
    const ctx = makeFakeCtx()
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await expect(handlers[Commands.PauseTasks]?.([])).rejects.toThrow()
  })

  it('RetryTasks routes unresolved magnet metadata to MagnetTracker', async () => {
    const ctx = makeFakeCtx()
    vi.mocked(ctx.taskManager.getById).mockReturnValue({
      id: 'magnet-timeout',
      status: TaskStatus.Error,
      type: TaskType.Magnet,
      torrentMetaPath: null,
      instances: [
        {
          phase: TaskInstancePhase.MagnetMetadataResolution,
          uris: ['magnet:?xt=urn:btih:timeout'],
        },
      ],
    } as never)
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await expect(
      handlers[Commands.RetryTasks]?.(['magnet-timeout'])
    ).resolves.toEqual({ succeeded: ['magnet-timeout'], failed: [] })

    expect(ctx.magnetTracker.retryMetadata).toHaveBeenCalledWith(
      'magnet-timeout'
    )
  })
})

describe('server Commands.StopSeedingTask', () => {
  it('stops seeding and persists Completed (web parity with desktop)', async () => {
    const persistTask = vi.fn(async () => {})
    const ctx = { ...makeFakeCtx(), persistTask }
    vi.mocked(ctx.taskManager.getById).mockReturnValue({
      id: 't-seed',
      engineTaskId: 'gid-seed',
      status: TaskStatus.Seeding,
      type: TaskType.Bt,
      finishedAt: null,
      errorMessage: null,
      errorCode: null,
      instances: [],
    } as never)
    ;(ctx.taskManager as unknown as { set: unknown }).set = vi.fn()
    ctx.adapter.forceRemoveTask = vi.fn().mockResolvedValue(undefined)
    ctx.adapter.getTaskStatus = vi.fn().mockResolvedValue(null)
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await expect(
      handlers[Commands.StopSeedingTask]?.('t-seed')
    ).resolves.toEqual({ ok: true })

    expect(ctx.adapter.forceRemoveTask).toHaveBeenCalledWith('gid-seed')
    expect(persistTask).toHaveBeenCalledWith(
      expect.objectContaining({ id: 't-seed', status: TaskStatus.Completed })
    )
  })
})

describe('server Commands.RemoveTask mutation admission', () => {
  it('routes removal through the mutation lock and Session persistence queue', async () => {
    const ctx = makeFakeCtx()
    ctx.taskManager = {
      getById: vi.fn(() => ({
        id: 'remove-me',
        engineTaskId: 'gid-remove',
        diskPath: '/downloads/remove-me',
        type: TaskType.Http,
        status: TaskStatus.Completed,
        torrentMetaPath: null,
        instances: [],
      })),
      getAll: vi.fn(() => []),
      remove: vi.fn(),
    } as unknown as TaskManager
    ctx.adapter = {
      removeDownloadResult: vi.fn().mockResolvedValue(undefined),
    } as unknown as EngineAdapter
    ctx.fileCleanupService = {
      cleanup: vi.fn().mockResolvedValue(undefined),
    } as unknown as FileCleanupService
    ctx.torrentMetaStore = {
      remove: vi.fn().mockResolvedValue(undefined),
    } as unknown as TorrentMetaStore
    ctx.motrixDatabase = {
      deleteTask: vi.fn(),
      getTask: vi.fn().mockReturnValue(null),
      saveTaskWithInstances: vi.fn(),
    } as unknown as MotrixDatabase
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await expect(
      handlers[Commands.RemoveTask]?.({
        taskId: 'remove-me',
        deleteWithFiles: false,
      })
    ).resolves.toEqual({ ok: true })

    expect(ctx.runTaskMutation).toHaveBeenCalledWith(
      ['remove-me'],
      expect.any(Function)
    )
    expect(ctx.taskPersistence.runExclusivePersistence).toHaveBeenCalledOnce()
  })
})

describe('server Commands.ReAddTask', () => {
  it('is registered and drives reAddTask through the mutation lock', async () => {
    const ctx = makeFakeCtx()
    ctx.taskManager = {
      getById: vi.fn(() => ({
        id: 'retry-me',
        engineTaskId: 'gid-retry',
        kind: TaskKind.Bt,
        type: TaskType.Bt,
        status: TaskStatus.Error,
        torrentMetaPath: '/sidecar/retry-me.torrent',
        diskPath: '/downloads/retry-me.motrix',
        finalPath: '/downloads/retry-me',
        finalName: 'retry-me',
        uris: [],
        bt: null,
        instances: [],
      })),
      getAll: vi.fn(() => []),
      reserveEngineTaskId: vi.fn(),
      setReservedEngineTaskOwner: vi.fn(),
      releaseEngineTaskIdReservation: vi.fn(() => true),
      retireEngineTaskIdReservation: vi.fn(() => true),
      set: vi.fn(),
    } as unknown as TaskManager
    ctx.adapter = {
      getEngineTaskOptions: vi.fn().mockResolvedValue(null),
      forceRemoveTask: vi.fn().mockResolvedValue(undefined),
      removeDownloadResult: vi.fn().mockResolvedValue(undefined),
      addTorrent: vi.fn(async ({ gid }: { gid?: string }) => gid ?? ''),
    } as unknown as EngineAdapter
    ctx.torrentMetaStore = {
      read: vi.fn().mockResolvedValue(new Uint8Array([0x64, 0x38])),
    } as unknown as TorrentMetaStore
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    expect(handlers[Commands.ReAddTask]).toBeDefined()
    await expect(handlers[Commands.ReAddTask]?.('retry-me')).resolves.toEqual({
      ok: true,
    })

    expect(ctx.runTaskMutation).toHaveBeenCalledWith(
      ['retry-me'],
      expect.any(Function)
    )
    expect(ctx.adapter.addTorrent).toHaveBeenCalledOnce()
  })
})

describe('server Commands.UpdatePluginConfig', () => {
  function makePluginCtx(
    opts: {
      secretFields?: string[]
      priorConfig?: Record<string, unknown>
    } = {}
  ) {
    const { secretFields = [], priorConfig = {} } = opts
    const properties: Record<string, { secret?: boolean }> = {}
    for (const key of secretFields) {
      properties[key] = { secret: true }
    }
    const manifest = {
      id: 'test-plugin',
      contributes: {
        configuration: {
          schema: { properties },
        },
      },
    }
    const applyExternalChange = vi.fn()
    const configFor = vi.fn(() => ({ applyExternalChange }))
    const encrypt = vi.fn(async (v: string) => `cipher:${v}`)
    const capabilityHost = {
      secrets: {
        available: vi.fn(() => true),
        encrypt,
      },
      configFor,
    }
    const ctx = makeFakeCtx()
    ;(ctx.settingsManager.get as ReturnType<typeof vi.fn>).mockReturnValue({
      plugins: { 'test-plugin': priorConfig },
    })
    ;(ctx.settingsManager.update as ReturnType<typeof vi.fn>).mockResolvedValue(
      {
        ok: true,
        requiresRestart: false,
        changedRestartKeys: [],
      }
    )
    const pluginRegistry = {
      get: vi.fn((id: string) =>
        id === 'test-plugin' ? { manifest } : undefined
      ),
    }
    return {
      ctx: {
        ...ctx,
        pluginRegistry: pluginRegistry as unknown as PluginRegistry,
        capabilityHost: capabilityHost as unknown as CapabilityHost,
      } as ServerCommandContext,
      settingsManager: ctx.settingsManager,
      eventBus: ctx.eventBus,
      applyExternalChange,
      encrypt,
    }
  }

  it('persists config and emits PluginConfigChanged', async () => {
    const { ctx, settingsManager, eventBus, applyExternalChange } =
      makePluginCtx()
    const handlers = buildServerCommandHandlers(ctx)
    const result = await handlers[Commands.UpdatePluginConfig]?.({
      pluginId: 'test-plugin',
      patch: { timeout: 60 },
    })
    expect(result).toEqual({ ok: true })
    expect(settingsManager.update).toHaveBeenCalledWith({
      plugins: { 'test-plugin': { timeout: 60 } },
    })
    expect(eventBus.emit).toHaveBeenCalledWith(
      Events.PluginConfigChanged,
      expect.objectContaining({ pluginId: 'test-plugin' })
    )
    expect(applyExternalChange).toHaveBeenCalledWith([
      { key: 'timeout', value: 60, previous: undefined },
    ])
  })

  it('encrypts secret-flagged string fields before persisting', async () => {
    const { ctx, settingsManager, encrypt } = makePluginCtx({
      secretFields: ['apiKey'],
    })
    const handlers = buildServerCommandHandlers(ctx)
    await handlers[Commands.UpdatePluginConfig]?.({
      pluginId: 'test-plugin',
      patch: { apiKey: 'plaintext', timeout: 60 },
    })
    expect(encrypt).toHaveBeenCalledWith('plaintext')
    expect(settingsManager.update).toHaveBeenCalledWith({
      plugins: {
        'test-plugin': { apiKey: 'cipher:plaintext', timeout: 60 },
      },
    })
  })

  it('throws when plugin not found in registry', async () => {
    const { ctx } = makePluginCtx()
    const handlers = buildServerCommandHandlers(ctx)
    await expect(
      handlers[Commands.UpdatePluginConfig]?.({
        pluginId: 'no-such-plugin',
        patch: {},
      })
    ).rejects.toMatchObject({ code: 'PLUGIN_MANIFEST_INVALID' })
  })
})

describe('server plugin enable/disable commands', () => {
  it('keeps a plugin disabled when a runtime dependency is missing', async () => {
    const ctx = makeFakeCtx()
    vi.spyOn(ctx.pluginHost, 'enable').mockRejectedValueOnce(
      new Error('plugin.runtime.ffmpegUnavailable')
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await expect(
      handlers[Commands.EnablePlugin]?.('test-plugin')
    ).rejects.toThrow('plugin.runtime.ffmpegUnavailable')
    expect(ctx.pluginStateStore.setEnabled).not.toHaveBeenCalled()
    expect(ctx.eventBus.emit).not.toHaveBeenCalled()
    await handlers[Commands.EnablePlugin]?.('test-plugin')
    expect(ctx.pluginStateStore.setEnabled).toHaveBeenCalledWith(
      'test-plugin',
      true
    )
  })

  it('emits contribution index changes when enabling a plugin', async () => {
    const ctx = makeFakeCtx()
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.EnablePlugin]?.('test-plugin')

    expect(ctx.eventBus.emit).toHaveBeenCalledWith(
      Events.PluginStatusChanged,
      expect.objectContaining({ id: 'test-plugin', enabled: true })
    )
    expect(ctx.eventBus.emit).toHaveBeenCalledWith(
      Events.ContributionIndexChanged
    )
  })

  it('refreshes the registry state after enabling so list() returns fresh data', async () => {
    const ctx = makeFakeCtx()
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.EnablePlugin]?.('test-plugin')

    expect(ctx.pluginStateStore.setEnabled).toHaveBeenCalledWith(
      'test-plugin',
      true
    )
    expect(
      (
        ctx.pluginRegistry as unknown as {
          refreshState: ReturnType<typeof vi.fn>
        }
      ).refreshState
    ).toHaveBeenCalledWith('test-plugin')
  })

  it('emits contribution index changes when disabling a plugin', async () => {
    const ctx = makeFakeCtx()
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.DisablePlugin]?.('test-plugin')

    expect(ctx.pluginHost.disable).toHaveBeenCalledWith(
      'test-plugin',
      'plugin.user_disabled',
      'disabled',
      { recordError: false }
    )
    expect(ctx.eventBus.emit).toHaveBeenCalledWith(
      Events.PluginStatusChanged,
      expect.objectContaining({ id: 'test-plugin', enabled: false })
    )
    expect(ctx.eventBus.emit).toHaveBeenCalledWith(
      Events.ContributionIndexChanged
    )
  })

  it('refreshes the registry state after disabling so list() returns fresh data', async () => {
    const ctx = makeFakeCtx()
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.DisablePlugin]?.('test-plugin')

    expect(ctx.pluginStateStore.setEnabled).toHaveBeenCalledWith(
      'test-plugin',
      false
    )
    expect(
      (
        ctx.pluginRegistry as unknown as {
          refreshState: ReturnType<typeof vi.fn>
        }
      ).refreshState
    ).toHaveBeenCalledWith('test-plugin')
  })
})

describe('server plugin lifecycle commands', () => {
  it('returns compatibility details without emitting consent or installation events', async () => {
    const ctx = makeFakeCtx()
    ;(
      ctx.pluginInstallService.stage as ReturnType<typeof vi.fn>
    ).mockRejectedValueOnce(
      new PluginEngineVersionTooOld('>=2.0.0-beta.47 <3.0.0', '2.0.0-beta.46')
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )
    await expect(
      handlers[Commands.InstallPlugin]?.({
        sourceType: 'registry',
        pluginId: 'motrix.media-merge',
      })
    ).resolves.toEqual({
      incompatible: {
        required: '>=2.0.0-beta.47 <3.0.0',
        hostVersion: '2.0.0-beta.46',
      },
    })
    expect(ctx.eventBus.emit).not.toHaveBeenCalled()
  })

  it('stages an install and publishes the consent request', async () => {
    const ctx = makeFakeCtx()
    ;(
      ctx.pluginInstallService.stage as ReturnType<typeof vi.fn>
    ).mockResolvedValue({
      stagingId: 's1',
      consent: { manifest: { id: 'test.plugin' } },
      committed: false,
    })
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await expect(
      handlers[Commands.InstallPlugin]?.({
        sourceType: 'registry',
        pluginId: 'test.plugin',
      })
    ).resolves.toMatchObject({ stagingId: 's1', committed: false })
    expect(ctx.pluginInstallService.stage).toHaveBeenCalledWith(
      {
        sourceType: 'registry',
        pluginId: 'test.plugin',
      },
      ctx.pluginHost
    )
    expect(ctx.eventBus.emit).toHaveBeenCalledWith(
      Events.PluginInstallConsentRequested,
      expect.objectContaining({ stagingId: 's1' })
    )
  })

  it('commits consent grants and publishes installation', async () => {
    const ctx = makeFakeCtx()
    ;(ctx.pluginInstaller.commit as ReturnType<typeof vi.fn>).mockResolvedValue(
      { pluginId: 'test.plugin' }
    )
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await expect(
      handlers[Commands.ConfirmPluginInstall]?.({
        stagingId: 's1',
        grants: { notify: 'denied' },
      })
    ).resolves.toEqual({ ok: true, pluginId: 'test.plugin' })
    expect(ctx.pluginInstaller.commit).toHaveBeenCalledWith(
      's1',
      { notify: 'denied' },
      ctx.pluginHost
    )
    expect(ctx.eventBus.emit).toHaveBeenCalledWith(Events.PluginInstalled, {
      pluginId: 'test.plugin',
    })
  })

  it('cancels staging, updates grants, and uninstalls through the installer', async () => {
    const ctx = makeFakeCtx()
    ;(
      ctx.pluginGrants.updateGrants as ReturnType<typeof vi.fn>
    ).mockResolvedValue({ notify: 'granted' })
    const handlers = buildServerCommandHandlers(
      ctx as Parameters<typeof buildServerCommandHandlers>[0]
    )

    await handlers[Commands.CancelPluginInstall]?.({ stagingId: 's1' })
    await expect(
      handlers[Commands.UpdatePluginGrants]?.({
        pluginId: 'test.plugin',
        patch: { notify: 'granted' },
      })
    ).resolves.toEqual({ ok: true, grants: { notify: 'granted' } })
    await handlers[Commands.UninstallPlugin]?.({ pluginId: 'test.plugin' })

    expect(ctx.pluginInstaller.cancel).toHaveBeenCalledWith('s1')
    expect(ctx.pluginInstaller.uninstall).toHaveBeenCalledWith(
      'test.plugin',
      ctx.pluginHost
    )
    expect(ctx.settingsManager.removePluginConfig).toHaveBeenCalledWith(
      'test.plugin'
    )
    expect(ctx.eventBus.emit).toHaveBeenCalledWith(Events.PluginUninstalled, {
      pluginId: 'test.plugin',
    })
  })
})

describe('buildServerCommandHandlers — notification center', () => {
  let db: MotrixDatabase
  let notificationCenter: NotificationCenter

  beforeEach(() => {
    db = new MotrixDatabase(':memory:')
    db.init()
    notificationCenter = new NotificationCenter({
      store: db,
      emit: vi.fn(),
      log: { warn: vi.fn(), error: vi.fn() },
    })
  })

  afterEach(() => {
    db.close()
  })

  it('MarkNotificationRead and MarkAllNotificationsRead round-trip against a real center', async () => {
    notificationCenter.notify({
      sourceKey: 'src-1',
      kind: 'task-error',
      severity: 'error',
      titleKey: 'notification.title',
    })
    const [row] = notificationCenter.list()
    const ctx = makeFakeCtx()
    const handlers = buildServerCommandHandlers({
      ...ctx,
      notificationCenter,
    } as unknown as ServerCommandContext)

    await expect(
      handlers[Commands.MarkNotificationRead]?.(row?.id)
    ).resolves.toBe(true)
    expect(notificationCenter.unreadCount()).toBe(0)

    notificationCenter.notify({
      sourceKey: 'src-2',
      kind: 'task-error',
      severity: 'error',
      titleKey: 'notification.title',
    })
    await expect(handlers[Commands.MarkAllNotificationsRead]?.()).resolves.toBe(
      1
    )
    expect(notificationCenter.unreadCount()).toBe(0)
  })

  it('DeleteNotification and ClearNotifications round-trip against a real center', async () => {
    notificationCenter.notify({
      sourceKey: 'src-1',
      kind: 'task-error',
      severity: 'error',
      titleKey: 'notification.title',
    })
    const [row] = notificationCenter.list()
    const ctx = makeFakeCtx()
    const handlers = buildServerCommandHandlers({
      ...ctx,
      notificationCenter,
    } as unknown as ServerCommandContext)

    await expect(
      handlers[Commands.DeleteNotification]?.(row?.id)
    ).resolves.toBe(true)
    expect(notificationCenter.list()).toHaveLength(0)

    notificationCenter.notify({
      sourceKey: 'src-2',
      kind: 'task-error',
      severity: 'error',
      titleKey: 'notification.title',
    })
    await expect(handlers[Commands.ClearNotifications]?.()).resolves.toBe(1)
    expect(notificationCenter.list()).toHaveLength(0)
  })
})

describe('server finalize retry wiring', () => {
  it.each([Commands.ReAddTask, Commands.RetryTasks])(
    'routes %s to recovery for an interrupted finalize',
    async (command) => {
      const ctx = makeFakeCtx() as unknown as ServerCommandContext
      const task = makeDownloadTask({
        id: 'interrupted-finalize',
        type: TaskType.Bt,
        status: TaskStatus.Error,
        transitionPhase: TransitionPhase.Renaming,
      })
      vi.spyOn(ctx.taskManager, 'getById').mockReturnValue(task)
      const recoverFinalization = vi.fn().mockResolvedValue(undefined)
      const handlers = buildServerCommandHandlers({
        ...ctx,
        recoverFinalization,
      })
      await handlers[command]?.(
        command === Commands.ReAddTask ? task.id : [task.id]
      )
      expect(recoverFinalization).toHaveBeenCalledExactlyOnceWith(task.id)
    }
  )
})

describe('server disclaimer acceptance', () => {
  it('starts the tracker schedule only after consent is saved', async () => {
    const ctx = makeFakeCtx()
    let resolve!: (
      value: Awaited<ReturnType<SettingsManager['acceptDisclaimer']>>
    ) => void
    vi.mocked(ctx.settingsManager.acceptDisclaimer).mockReturnValueOnce(
      new Promise((done) => {
        resolve = done
      })
    )
    const handlers = buildServerCommandHandlers(
      ctx as unknown as ServerCommandContext
    )
    const accepting = handlers[Commands.AcceptDisclaimer]?.()
    expect(ctx.trackerManager.applySyncScheduleChange).not.toHaveBeenCalled()
    resolve({
      saved: true,
      requiresRestart: false,
      changedRestartKeys: [],
      requiresAppRestart: false,
      changedAppRestartKeys: [],
    })
    await accepting
    expect(ctx.trackerManager.applySyncScheduleChange).toHaveBeenCalledOnce()
  })

  it('does not start tracker sync if consent cannot be saved', async () => {
    const ctx = makeFakeCtx()
    vi.mocked(ctx.settingsManager.acceptDisclaimer).mockRejectedValueOnce(
      new Error('disk full')
    )
    const handlers = buildServerCommandHandlers(
      ctx as unknown as ServerCommandContext
    )
    await expect(handlers[Commands.AcceptDisclaimer]?.()).rejects.toThrow(
      'disk full'
    )
    expect(ctx.trackerManager.applySyncScheduleChange).not.toHaveBeenCalled()
  })
})

describe('host-owned task directory history', () => {
  it('returns an accepted magnet while the directory write is pending', async () => {
    const ctx = makeFakeCtx()
    let finish!: (value: unknown) => void
    const record = vi.fn(
      () =>
        new Promise((resolve) => {
          finish = resolve
        })
    )
    ctx.settingsManager.mutateDirectoryPreferences = record as never
    const handlers = buildServerCommandHandlers(
      ctx as unknown as Parameters<typeof buildServerCommandHandlers>[0]
    )
    const request = {
      type: 'bt',
      payload: {
        kind: 'magnet',
        uri: 'magnet:?xt=urn:btih:a03e3f9a05341aa336e9d9d3f06b33cddafe0bdc',
      },
      selectedFiles: [],
      saveDir: '/tmp',
    }
    await expect(handlers[Commands.CreateTask]?.(request)).resolves.toEqual({
      ok: true,
    })
    await vi.waitFor(() =>
      expect(record).toHaveBeenCalledExactlyOnceWith({
        action: 'recordRecent',
        path: '/tmp',
      })
    )
    finish({ ok: true, value: { favorites: [], recent: [] } })
  })
})

describe('plugin diagnostic mode command', () => {
  it.each([true, false])(
    'returns authoritative state after setting verbose=%s',
    async (verbose) => {
      const state = { verbose, expiresAt: verbose ? 3_610_000 : null }
      const setLogVerbose = vi.fn()
      const getLogState = vi.fn(() => state)
      const handlers = buildServerCommandHandlers({
        ...makeFakeCtx(),
        capabilityHost: { setLogVerbose, getLogState },
      } as unknown as ServerCommandContext)
      await expect(
        handlers[Commands.SetPluginLogVerbose]?.({
          pluginId: 'alice.demo',
          verbose,
        })
      ).resolves.toEqual(state)
      expect(setLogVerbose).toHaveBeenCalledWith('alice.demo', verbose)
      expect(getLogState).toHaveBeenCalledWith('alice.demo')
      setLogVerbose.mockClear()
      for (const payload of [
        null,
        {},
        { pluginId: 'alice.demo', verbose: 'true' },
      ]) {
        await expect(
          handlers[Commands.SetPluginLogVerbose]?.(payload)
        ).rejects.toThrow()
      }
      expect(setLogVerbose).not.toHaveBeenCalled()
    }
  )
})

describe('BT storage command contract', () => {
  it('requires a writable relocation destination before stopping or inspecting any files', async () => {
    const ctx = makeFakeCtx() as ServerCommandContext
    const prepareSaveDir = vi.fn(async () => {
      throw new Error('outside allowed roots')
    })
    ctx.downloadPathPolicy = { ...ctx.downloadPathPolicy, prepareSaveDir }
    ctx.btStorageRuntime = {
      commit: vi.fn(),
      withArtifactLease: vi.fn(),
      hasPendingFinalization: vi.fn(),
    }
    ctx.persistBtStorage = vi.fn()
    ctx.adapter.forceRemoveTask = vi.fn()
    vi.mocked(ctx.taskManager.getById).mockReturnValue(
      makeDownloadTask({
        id: 'bt',
        type: TaskType.Bt,
        status: TaskStatus.Completed,
        finalPath: '/old/file',
        infoHash: 'a'.repeat(40),
        torrentMetaPath: '/metadata.torrent',
      })
    )
    const handlers = buildServerCommandHandlers(ctx)
    expect(
      await handlers[Commands.ChangeBtStorage]?.({
        taskId: 'bt',
        mode: 'locate',
        directory: '/forbidden',
        name: 'file',
      })
    ).toEqual({ error: 'failed' })
    expect(prepareSaveDir).toHaveBeenCalledWith('/forbidden')
    expect(ctx.adapter.forceRemoveTask).not.toHaveBeenCalled()
    expect(ctx.persistBtStorage).not.toHaveBeenCalled()
  })

  it('validates destination requests before invoking storage', async () => {
    const handlers = buildServerCommandHandlers(
      makeFakeCtx() as ServerCommandContext
    )
    await expect(
      handlers[Commands.ChangeBtStorage]?.({ taskId: 'bt', mode: 'move' })
    ).rejects.toThrow()
  })
  it('reports unavailable storage and accepts a harmless cancellation', async () => {
    const handlers = buildServerCommandHandlers(
      makeFakeCtx() as ServerCommandContext
    )
    expect(
      await handlers[Commands.ChangeBtStorage]?.({
        taskId: 'bt',
        mode: 'recheck',
      })
    ).toEqual({ error: 'unavailable' })
    expect(await handlers[Commands.CancelBtStorage]?.('bt')).toEqual({
      ok: true,
    })
  })
})