Skip to content

A full-featured download manager.

17.0 KB
Raw Blame
import {
  ANIME_TRACKER_BLACKLIST_SOURCE,
  ANIME_TRACKER_DIRECT_SOURCE,
  ANIME_TRACKER_SOURCE,
  trackerSettingsSchema,
} from '@shared/schemas/tracker-settings'
import { describe, expect, it } from 'vitest'
import { CURRENT_SETTINGS_VERSION, migrate } from './migrations'
import { DEFAULT_MEDIA_SETTINGS } from './validators'

describe('migrate', () => {
  it('returns input unchanged when already at current version', () => {
    const input = { version: CURRENT_SETTINGS_VERSION, engine: {}, app: {} }
    const result = migrate(input)
    expect(result).toEqual(input)
  })

  it('adds version field when missing', () => {
    const input = { engine: {}, app: {} }
    const result = migrate(input as Record<string, unknown>)
    expect(result.version).toBe(CURRENT_SETTINGS_VERSION)
  })

  it('migrates from v0 (legacy format with general/download)', () => {
    const legacy = {
      general: {
        launchAtStartup: true,
        restoreOnCrash: true,
      },
      download: {
        defaultSaveDir: '/tmp/downloads',
        maxConcurrentTasks: 8,
        maxDownloadSpeed: 1000,
        maxUploadSpeed: 500,
      },
      plugins: {},
    }
    const result = migrate(legacy as Record<string, unknown>)
    const app = result.app as Record<string, unknown>
    const engine = result.engine as Record<string, unknown>
    expect(result.version).toBe(CURRENT_SETTINGS_VERSION)
    expect(app.launchAtStartup).toBe(true)
    expect(app.defaultSaveDir).toBe('/tmp/downloads')
    expect(engine.maxConcurrentDownloads).toBe(8)
    // v6→v7 strips these from engine and moves them to speedLimit.base
    expect(engine.maxOverallDownloadLimit).toBeUndefined()
    expect(engine.maxOverallUploadLimit).toBeUndefined()
    const speedLimit = result.speedLimit as Record<string, unknown>
    const base = speedLimit.base as Record<string, unknown>
    expect(base.download).toBe(1000)
    expect(base.upload).toBe(500)
    expect(result.plugins).toEqual({})
  })

  it('preserves plugins through migration', () => {
    const legacy = {
      general: { launchAtStartup: false, restoreOnCrash: true },
      download: {
        defaultSaveDir: '',
        maxConcurrentTasks: 5,
        maxDownloadSpeed: 0,
        maxUploadSpeed: 0,
      },
      plugins: { 'my-plugin': { enabled: true, order: 0, config: {} } },
    }
    const result = migrate(legacy as Record<string, unknown>)
    expect(result.plugins).toEqual({
      'my-plugin': { enabled: true, order: 0, config: {} },
    })
  })

  it('handles completely empty object', () => {
    const result = migrate({})
    expect(result.version).toBe(CURRENT_SETTINGS_VERSION)
    expect(result.engine).toBeUndefined()
    expect(result.app).toBeUndefined()
  })

  it('migrates from v1 to v2 (adds protocols)', () => {
    const v1Data = {
      version: 1,
      engine: { rpcPort: 16800 },
      app: { launchAtStartup: true, theme: 'dark' },
      plugins: {},
    }
    const result = migrate(v1Data as Record<string, unknown>)
    expect(result.version).toBe(CURRENT_SETTINGS_VERSION)
    const app = result.app as Record<string, unknown>
    expect(app.protocols).toEqual({ magnet: true })
    expect(app.launchAtStartup).toBe(true)
    expect(app.theme).toBe('dark')
  })
})

describe('migration v3 → v4', () => {
  it('targets version 12', () => {
    expect(CURRENT_SETTINGS_VERSION).toBe(13)
  })

  it('adds dhtListenPort defaulting to listenPort value', () => {
    const v3 = {
      version: 3,
      engine: { listenPort: 51413, rpcPort: 16800 },
      app: {},
      plugins: {},
    }
    const result = migrate(v3)
    expect(result.version).toBe(CURRENT_SETTINGS_VERSION)
    const engine = result.engine as Record<string, unknown>
    expect(engine.dhtListenPort).toBe(51413)
  })

  it('adds empty nat namespace', () => {
    const v3 = {
      version: 3,
      engine: { listenPort: 6881 },
      app: {},
      plugins: {},
    }
    const v4 = migrate(v3)
    expect(v4.nat).toEqual({})
  })

  it('preserves existing nat namespace if user pre-set values', () => {
    const v3 = {
      version: 3,
      engine: { listenPort: 6881 },
      app: {},
      nat: { enabled: false },
      plugins: {},
    }
    const v4 = migrate(v3)
    expect((v4.nat as Record<string, unknown>).enabled).toBe(false)
  })
})

describe('migration v4 → v5 (sqlite3 persistence)', () => {
  it('populates default sqlite3 fields on legacy settings without them', () => {
    const legacy = {
      version: 4,
      engine: {
        rpcPort: 16800,
        rpcSecret: 'motrix-secret',
        listenPort: 6881,
        dhtListenPort: 6881,
      },
      app: {},
      nat: {},
      plugins: {},
    }
    const migrated = migrate(legacy)
    const engine = migrated.engine as Record<string, unknown>
    expect(migrated.version).toBe(CURRENT_SETTINGS_VERSION)
    expect(engine.sqlite3Persistence).toBe(true)
    expect(engine.sqlite3DbPath).toBe('')
    expect(engine.sqlite3HistoryLimit).toBe(-1)
  })

  it('preserves existing sqlite3 fields if already present', () => {
    const legacy = {
      version: 4,
      engine: {
        rpcPort: 16800,
        sqlite3Persistence: false,
        sqlite3DbPath: '/var/lib/motrix/aria2.db',
        sqlite3HistoryLimit: 1000,
      },
      app: {},
      nat: {},
      plugins: {},
    }
    const migrated = migrate(legacy)
    const engine = migrated.engine as Record<string, unknown>
    expect(engine.sqlite3Persistence).toBe(false)
    expect(engine.sqlite3DbPath).toBe('/var/lib/motrix/aria2.db')
    expect(engine.sqlite3HistoryLimit).toBe(1000)
  })
})

describe('migration v5 → v6 (media namespace)', () => {
  it('migrates v5 → v6 by injecting media defaults', () => {
    const v5 = { version: 5, engine: {}, app: {} }
    const out = migrate(v5)
    expect(out.version).toBe(CURRENT_SETTINGS_VERSION)
    expect(out.media).toEqual(DEFAULT_MEDIA_SETTINGS)
  })

  it('preserves user-provided media on migration', () => {
    const v5WithMedia = {
      version: 5,
      media: {
        ffmpegBinaryPath: '/u/ffmpeg',
        ffmpegStagingMB: 2048,
        ffmpegOpTimeoutSec: 600,
      },
    }
    const out = migrate(v5WithMedia) as Record<string, unknown>
    expect(out.media).toEqual({
      ffmpegBinaryPath: '/u/ffmpeg',
      ffmpegStagingMB: 2048,
      ffmpegOpTimeoutSec: 600,
    })
  })
})

describe('migration v6 → v7 (speedLimit namespace)', () => {
  it('targets version 12', () => {
    expect(CURRENT_SETTINGS_VERSION).toBe(13)
  })

  it('v6→v7: maps a configured limit to base, turtle off', () => {
    const result = migrate({
      version: 6,
      engine: {
        maxOverallDownloadLimit: 1024000,
        maxOverallUploadLimit: 256000,
      },
    })
    expect(result.version).toBe(CURRENT_SETTINGS_VERSION)
    expect(
      (result.engine as Record<string, unknown>).maxOverallDownloadLimit
    ).toBeUndefined()
    expect(
      (result.engine as Record<string, unknown>).maxOverallUploadLimit
    ).toBeUndefined()
    expect(result.speedLimit).toMatchObject({
      turtle: 'off',
      base: { download: 1024000, upload: 256000 },
      alt: { download: 512 * 1024, upload: 64 * 1024 },
    })
  })

  it('v6→v7: only download limit set → base download, upload zero', () => {
    const result = migrate({
      version: 6,
      engine: { maxOverallDownloadLimit: 512000 },
    })
    expect(result.speedLimit).toMatchObject({
      turtle: 'off',
      base: { download: 512000, upload: 0 },
    })
  })

  it('v6→v7: only upload limit set → base upload, download zero', () => {
    const result = migrate({
      version: 6,
      engine: { maxOverallUploadLimit: 128000 },
    })
    expect(result.speedLimit).toMatchObject({
      turtle: 'off',
      base: { download: 0, upload: 128000 },
    })
  })

  it('v6→v7: no prior limit → base 0/0, turtle off', () => {
    const result = migrate({ version: 6, engine: {} })
    expect(result.speedLimit).toMatchObject({
      turtle: 'off',
      base: { download: 0, upload: 0 },
    })
  })
})

describe('migration v7 → v8 (application update channel)', () => {
  it('defaults existing users to stable', () => {
    const result = migrate({ version: 7, app: { theme: 'dark' } })

    expect(result.version).toBe(CURRENT_SETTINGS_VERSION)
    expect(result.app).toEqual({ theme: 'dark', updateChannel: 'stable' })
  })

  it.each(['stable', 'beta'] as const)(
    'preserves an already valid %s channel',
    (updateChannel) => {
      const result = migrate({ version: 7, app: { updateChannel } })

      expect(result.app).toEqual({ updateChannel })
    }
  )

  it('repairs an invalid persisted channel to stable', () => {
    const result = migrate({ version: 7, app: { updateChannel: 'alpha' } })

    expect(result.app).toEqual({ updateChannel: 'stable' })
  })
})

describe('migration v8 → v9 (performance profiles)', () => {
  it('moves the previous defaults to the automatic profile', () => {
    const result = migrate({
      version: 8,
      engine: {
        maxConnectionPerServer: 16,
        split: 16,
        minSplitSize: 10 * 1024 * 1024,
        diskCache: 64 * 1024 * 1024,
      },
    })

    expect(result.version).toBe(CURRENT_SETTINGS_VERSION)
    expect(result.engine).toMatchObject({ performanceProfile: 'auto' })
  })

  it('preserves tuned values through the custom profile', () => {
    const result = migrate({
      version: 8,
      engine: {
        maxConnectionPerServer: 24,
        split: 12,
        minSplitSize: 2 * 1024 * 1024,
        diskCache: 48 * 1024 * 1024,
      },
    })

    expect(result.engine).toMatchObject({
      performanceProfile: 'custom',
      maxConnectionPerServer: 24,
      split: 12,
      minSplitSize: 2 * 1024 * 1024,
      diskCache: 48 * 1024 * 1024,
    })
  })
})

const UUID_PATTERN =
  /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/

describe('migration v9 → v10 (bridge fixed port and instance id)', () => {
  it('seeds fixedPort auto and a UUID instanceId for a plain v9 document', () => {
    const result = migrate({ version: 9, app: { theme: 'dark' } })

    expect(result.version).toBe(CURRENT_SETTINGS_VERSION)
    const bridge = result.bridge as Record<string, unknown>
    expect(bridge.fixedPort).toBe('auto')
    expect(bridge.instanceId).toEqual(expect.stringMatching(UUID_PATTERN))
  })

  it('preserves an existing bridge object instead of clobbering it', () => {
    const result = migrate({
      version: 9,
      bridge: { fixedPort: 16803, instanceId: 'already-set-id' },
    })

    expect(result.bridge).toEqual({
      fixedPort: 16803,
      instanceId: 'already-set-id',
    })
  })

  it('mints a fresh instanceId only when one is not already present', () => {
    const result = migrate({ version: 9, bridge: { fixedPort: 16803 } })

    expect((result.bridge as Record<string, unknown>).fixedPort).toBe(16803)
    expect((result.bridge as Record<string, unknown>).instanceId).toEqual(
      expect.stringMatching(UUID_PATTERN)
    )
  })

  it('never regenerates instanceId once the document is already at v10', () => {
    const migratedOnce = migrate({ version: 9 })
    const instanceId = (migratedOnce.bridge as Record<string, unknown>)
      .instanceId
    // Pins that seeding actually minted an id: without this, a migration that
    // returned a bridge object with no instanceId would satisfy the equality
    // below vacuously (undefined === undefined).
    expect(instanceId).toEqual(expect.stringMatching(UUID_PATTERN))

    // A document at or beyond v10 never re-enters migrateV9ToV10.
    const migratedTwice = migrate(migratedOnce)

    expect((migratedTwice.bridge as Record<string, unknown>).instanceId).toBe(
      instanceId
    )
  })
})

describe('migration v10 → v11 (magnet metadata timeout)', () => {
  it('upgrades the former timeout default without mutating the input', () => {
    const input = {
      version: 10,
      engine: { magnetResolveTimeout: 120, dhtEnabled: false },
      app: { theme: 'dark' },
    }

    expect(migrate(input)).toEqual({
      ...input,
      version: CURRENT_SETTINGS_VERSION,
      engine: { ...input.engine, magnetResolveTimeout: 600 },
    })
    expect(input.engine.magnetResolveTimeout).toBe(120)
  })

  it.each([30, 90, 180, 300, 600])(
    'preserves an existing %i-second timeout',
    (magnetResolveTimeout) => {
      const engine = { magnetResolveTimeout }
      expect(migrate({ version: 10, engine }).engine).toEqual(engine)
    }
  )

  it('preserves an explicit 120-second choice after the upgrade', () => {
    const input = {
      version: CURRENT_SETTINGS_VERSION,
      engine: { magnetResolveTimeout: 120 },
    }
    expect(migrate(input)).toEqual(input)
  })
})

describe('migration v11 → v12 (optional anime tracker source)', () => {
  it('adds the disabled source while preserving existing choices and custom sources', () => {
    const sources = [
      {
        id: 'ngosang-best',
        url: 'https://example.test/best.txt',
        enabled: false,
      },
      { id: 'custom', url: 'https://example.test/custom.txt', enabled: true },
    ]
    const input = { version: 11, tracker: { autoSync: false, sources } }
    const result = migrate(input)
    expect(result.tracker).toEqual({
      autoSync: false,
      sources: [...sources, ANIME_TRACKER_SOURCE, ANIME_TRACKER_DIRECT_SOURCE],
    })
    expect(ANIME_TRACKER_SOURCE.enabled).toBe(false)
    expect(input.tracker.sources).toHaveLength(2)
    expect(migrate(result)).toEqual(result)
  })

  it.each([
    { ...ANIME_TRACKER_SOURCE, enabled: true },
    {
      ...ANIME_TRACKER_SOURCE,
      id: 'custom-anime',
      builtin: false,
      enabled: true,
    },
  ])('preserves an existing source with the same ID or URL', (source) => {
    const tracker = { sources: [source] }
    expect(migrate({ version: 11, tracker }).tracker).toEqual({
      sources: [source, ANIME_TRACKER_DIRECT_SOURCE],
    })
  })

  it('does not enable other builtins when the saved source list is empty', () => {
    const result = migrate({ version: 11, tracker: { sources: [] } })
    const tracker = trackerSettingsSchema.parse(result.tracker)
    expect(tracker.sources).toEqual([
      ANIME_TRACKER_SOURCE,
      ANIME_TRACKER_DIRECT_SOURCE,
    ])
    expect(tracker.sources.some((source) => source.enabled)).toBe(false)
  })

  it('lets missing source lists receive the complete defaults', () => {
    const result = migrate({ version: 11, tracker: { autoSync: false } })
    const tracker = trackerSettingsSchema.parse(result.tracker)
    expect(tracker.autoSync).toBe(false)
    expect(tracker.sources).toContainEqual(ANIME_TRACKER_SOURCE)
    expect(tracker.sources.some((source) => source.enabled)).toBe(true)
  })
})

describe('migration v12 → v13 (anime direct source and blacklist)', () => {
  it('adds the new sources without changing existing source choices or global settings', () => {
    const tracker = {
      autoSync: false,
      sourcesEnabled: false,
      blacklistEnabled: false,
      sources: [{ ...ANIME_TRACKER_SOURCE, enabled: true }],
      blacklistSources: [
        {
          id: 'custom-blacklist',
          url: 'https://example.test/bad.txt',
          enabled: false,
        },
      ],
    }
    const input = { version: 12, tracker }
    const result = migrate(input)
    expect(result).toEqual({
      version: 13,
      tracker: {
        ...tracker,
        sources: [...tracker.sources, ANIME_TRACKER_DIRECT_SOURCE],
        blacklistSources: [
          ...tracker.blacklistSources,
          ANIME_TRACKER_BLACKLIST_SOURCE,
        ],
      },
    })
    expect(tracker.sources).toHaveLength(1)
    expect(tracker.blacklistSources).toHaveLength(1)
    expect(migrate(result)).toEqual(result)
  })

  it.each(['id', 'url'] as const)(
    'preserves existing entries matched by %s, including custom sources',
    (match) => {
      const sources = [
        ANIME_TRACKER_DIRECT_SOURCE,
        ANIME_TRACKER_BLACKLIST_SOURCE,
      ].map((source) => ({
        ...source,
        id: match === 'id' ? source.id : `custom-${source.id}`,
        url:
          match === 'url'
            ? source.url
            : `https://example.test/${source.id}.txt`,
        label: 'My source',
        builtin: false,
        enabled: !source.enabled,
      }))
      const tracker = { sources: [sources[0]], blacklistSources: [sources[1]] }
      expect(migrate({ version: 12, tracker }).tracker).toEqual(tracker)
    }
  )

  it('only adds new entries to empty lists instead of restoring older defaults', () => {
    const result = migrate({
      version: 12,
      tracker: { sources: [], blacklistSources: [] },
    })
    expect(result.tracker).toEqual({
      sources: [ANIME_TRACKER_DIRECT_SOURCE],
      blacklistSources: [ANIME_TRACKER_BLACKLIST_SOURCE],
    })
  })

  it('lets the schema recover missing or malformed lists using the complete defaults', () => {
    const result = migrate({
      version: 12,
      tracker: { autoSync: false, sources: null, blacklistSources: 'invalid' },
    })
    const tracker = trackerSettingsSchema.parse(result.tracker)
    expect(tracker.autoSync).toBe(false)
    expect(tracker.sources).toContainEqual(ANIME_TRACKER_DIRECT_SOURCE)
    expect(tracker.sources).toContainEqual(ANIME_TRACKER_SOURCE)
    expect(tracker.blacklistSources).toContainEqual(
      ANIME_TRACKER_BLACKLIST_SOURCE
    )
  })
})