Skip to content

A full-featured download manager.

15.3 KB
Raw Blame
import type { Transport } from '@renderer/lib/transport/types'
import { Commands } from '@shared/protocol/commands'
import { Events } from '@shared/protocol/events'
import { Queries } from '@shared/protocol/queries'
import { taskCreateRequestSchema } from '@shared/schemas/add-task'
import { DEFAULT_APP_SETTINGS } from '@shared/schemas/app-settings'
import {
  type DirectoryPreferences,
  DirectoryPreferencesSchema,
  GetDirectoryPreferencesRequestSchema,
  MutateDirectoryPreferencesRequestSchema,
} from '@shared/schemas/directory-preferences'
import {
  downloadsSettingsSchema,
  saveDownloadsSettingsRequestSchema,
} from '@shared/schemas/downloads-settings'
import { DEFAULT_ENGINE_SETTINGS } from '@shared/schemas/engine-settings'
import {
  GeneralSettingsAppSchema,
  GetGeneralSettingsDraftRequestSchema,
  SaveGeneralSettingsRequestSchema,
} from '@shared/schemas/general-settings'
import {
  CreateServerDirectoryRequestSchema,
  ListServerDirectoriesRequestSchema,
  ValidateServerDirectoryRequestSchema,
} from '@shared/schemas/server-directory'
import { createDefaultSpeedLimitSettings } from '@shared/schemas/speed-limit'
import { v7 as uuidv7 } from 'uuid'

const folders = new Map<string, string[]>([
  ['/', ['archive', 'downloads', 'home']],
  ['/home', ['operator']],
  ['/home/operator', ['Desktop', 'Documents', 'Downloads']],
  ['/home/operator/Desktop', []],
  ['/home/operator/Documents', []],
  ['/home/operator/Downloads', []],
  ['/downloads', ['.hidden', 'Empty', 'Movies', 'Music']],
  ['/downloads/.hidden', []],
  ['/downloads/Empty', []],
  ['/downloads/Movies', ['Classics']],
  ['/downloads/Movies/Classics', []],
  ['/downloads/Music', []],
  [
    '/archive',
    Array.from(
      { length: 800 },
      (_, index) => `Folder ${String(index).padStart(4, '0')}`
    ),
  ],
])
for (const name of folders.get('/archive') ?? [])
  folders.set(`/archive/${name}`, [])

const settings = {
  app: {
    ...structuredClone(DEFAULT_APP_SETTINGS),
    defaultSaveDir: '/downloads',
    autofillClipboardLinks: false,
  },
  engine: structuredClone(DEFAULT_ENGINE_SETTINGS),
  speedLimit: createDefaultSpeedLimitSettings('binary'),
}

const unrestricted = new URLSearchParams(location.search).has('unrestricted')
const listeners = new Map<string, Set<(...args: unknown[]) => void>>()
const canVisit = (path: string) =>
  folders.has(path) &&
  (unrestricted ||
    ['/downloads', '/archive'].some(
      (root) => path === root || path.startsWith(`${root}/`)
    ))
let generalRevision = uuidv7()
const generalSnapshot = () => ({
  revision: generalRevision,
  app: GeneralSettingsAppSchema.parse(settings.app),
  directoryPreferences: preferences(),
})
const downloadsSnapshot = () => ({
  revision: generalRevision,
  settings: downloadsSettingsSchema.parse(settings),
  directoryPreferences: preferences(),
})
const preferences = () => structuredClone(settings.app.directoryPreferences)
function setPreferences(value: DirectoryPreferences) {
  const next = DirectoryPreferencesSchema.parse(value)
  if (JSON.stringify(next) === JSON.stringify(preferences())) return
  settings.app.directoryPreferences = next
  generalRevision = uuidv7()
  for (const listener of listeners.get(Events.DirectoryPreferencesChanged) ??
    [])
    listener(preferences())
}

export const fixtureState = {
  modifiedAtByPath: {} as Record<string, number | undefined>,
  setDirectoryChildren(path: string, children: string[]) {
    folders.set(path, [...children])
    for (const name of children) {
      const childPath = `${path === '/' ? '' : path}/${name}`
      if (!folders.has(childPath)) folders.set(childPath, [])
    }
  },
  calls: [] as { channel: string; args: unknown[] }[],
  interactions: [] as {
    type: string
    key?: string
    tag?: string
    label?: string
    time: number
    activeTag?: string
    activeLabel?: string
    stack?: string
  }[],
  createFailure: false,
  taskCreateFailure: false,
  mutationFailure: false,
  nativePickerResult: null as string | null,
  nativePickerCalls: 0,
  setPreferences,
  getPreferences: preferences,
  getSettings: () => structuredClone(settings),
  holdChannel: null as string | null,
  releaseRequest: null as (() => void) | null,
}

const error = (code: string) => ({ ok: false, error: { code } })

function mergePatch<T extends object>(current: T, patch: object): T {
  const merged = { ...current } as Record<string, unknown>
  for (const [key, value] of Object.entries(patch)) {
    const original = merged[key]
    merged[key] =
      value &&
      typeof value === 'object' &&
      !Array.isArray(value) &&
      original &&
      typeof original === 'object' &&
      !Array.isArray(original)
        ? mergePatch(original, value)
        : value
  }
  return merged as T
}

// Only RPC data and unrelated transport events are replaced. The browser runs
// the actual add-task form, picker, Base UI dialogs, and their keyboard handlers.
export const transport: Transport = {
  platform:
    new URLSearchParams(location.search).get('transportPlatform') === 'linux'
      ? 'linux'
      : 'web',
  on(channel, listener) {
    if (!listeners.has(channel)) listeners.set(channel, new Set())
    listeners.get(channel)?.add(listener)
  },
  off(channel, listener) {
    listeners.get(channel)?.delete(listener)
  },
  async invoke(channel, ...args) {
    fixtureState.calls.push({ channel, args })
    if (fixtureState.holdChannel === channel) {
      await new Promise<void>((resolve) => {
        fixtureState.releaseRequest = () => {
          fixtureState.releaseRequest = null
          resolve()
        }
      })
    }
    switch (channel) {
      case Queries.GetSettings:
        return structuredClone(settings)
      case Queries.ListTasks:
        return []
      case Queries.ListAllowedSaveDirs:
        return {
          paths: unrestricted
            ? []
            : [{ path: '/downloads' }, { path: '/archive' }],
          defaultPath: '/downloads',
          allowCustom: true,
        }
      case Queries.GetGeneralSettingsDraft:
        GetGeneralSettingsDraftRequestSchema.parse(args[0])
        return { ok: true, value: generalSnapshot() }
      case Queries.GetDownloadsSettingsDraft:
        GetGeneralSettingsDraftRequestSchema.parse(args[0])
        return { ok: true, value: downloadsSnapshot() }
      case Queries.GetDirectoryPreferences:
        GetDirectoryPreferencesRequestSchema.parse(args[0])
        return { ok: true, value: preferences() }
      case Queries.ListServerDirectoryLocations: {
        GetDirectoryPreferencesRequestSchema.parse(args[0])
        const common = [
          { kind: 'default', path: '/downloads' },
          { kind: 'home', path: '/home/operator' },
          { kind: 'desktop', path: '/home/operator/Desktop' },
          { kind: 'documents', path: '/home/operator/Documents' },
          { kind: 'downloads', path: '/home/operator/Downloads' },
          { kind: 'root', path: '/' },
        ].filter((entry) => canVisit(entry.path))
        const entries = (paths: string[]) =>
          paths.filter(canVisit).map((path) => ({
            name: path.split('/').at(-1) || '/',
            path,
            sourcePaths: [path],
          }))
        return {
          ok: true,
          value: {
            common,
            favorites: entries(settings.app.directoryPreferences.favorites),
            recent: entries(settings.app.directoryPreferences.recent),
          },
        }
      }
      case Commands.MutateDirectoryPreferences: {
        const action = MutateDirectoryPreferencesRequestSchema.parse(args[0])
        if (fixtureState.mutationFailure) return error('unavailable')
        const next = preferences()
        if (
          action.action === 'addFavorite' ||
          action.action === 'recordRecent'
        ) {
          if (!canVisit(action.path)) return error('notFound')
          if (action.action === 'addFavorite') {
            if (!next.favorites.includes(action.path)) {
              if (next.favorites.length >= 20) return error('limitReached')
              next.favorites.push(action.path)
            }
          } else
            next.recent = [
              action.path,
              ...next.recent.filter((path) => path !== action.path),
            ].slice(0, 10)
        } else if (action.action === 'removeFavorite') {
          next.favorites = next.favorites.filter(
            (path) => !action.paths.includes(path)
          )
        } else if (action.action === 'removeRecent') {
          next.recent = next.recent.filter(
            (path) => !action.paths.includes(path)
          )
        } else next.recent = []
        setPreferences(next)
        return { ok: true, value: preferences() }
      }
      case Commands.SaveGeneralSettings: {
        const parsed = SaveGeneralSettingsRequestSchema.safeParse(args[0])
        if (!parsed.success) return error('invalidPath')
        if (fixtureState.mutationFailure) return error('unavailable')
        const { app, directories, expectedRevision } = parsed.data
        if (expectedRevision !== generalRevision)
          return { ...error('conflict'), snapshot: generalSnapshot() }
        if (
          (app.defaultSaveDir !== undefined && !canVisit(app.defaultSaveDir)) ||
          directories.addFavorites.some((path) => !canVisit(path))
        )
          return error('notFound')
        // The fixture applies the draft against current committed records in
        // one step, including events received while this request was held.
        const current = preferences()
        const next = {
          favorites: current.favorites.filter(
            (path) => !directories.removeFavorites.includes(path)
          ),
          recent: current.recent.filter(
            (path) => !directories.removeRecent.includes(path)
          ),
        }
        for (const path of directories.addFavorites)
          if (!next.favorites.includes(path)) next.favorites.push(path)
        if (next.favorites.length > 20) return error('limitReached')
        settings.app = { ...settings.app, ...app, directoryPreferences: next }
        if (JSON.stringify(current) !== JSON.stringify(next))
          setPreferences(next)
        generalRevision = uuidv7()
        return { ok: true, value: generalSnapshot() }
      }
      case Commands.SaveDownloadsSettings: {
        const parsed = saveDownloadsSettingsRequestSchema.safeParse(args[0])
        if (!parsed.success) return error('invalidPath')
        if (fixtureState.mutationFailure) return error('unavailable')
        const { settings: patch, directories, expectedRevision } = parsed.data
        if (expectedRevision !== generalRevision)
          return { ...error('conflict'), snapshot: downloadsSnapshot() }
        if (
          (patch.app?.defaultSaveDir !== undefined &&
            !canVisit(patch.app.defaultSaveDir)) ||
          directories.addFavorites.some((path) => !canVisit(path))
        )
          return error('notFound')
        const current = preferences()
        const next = {
          favorites: [
            ...new Set([
              ...current.favorites.filter(
                (path) => !directories.removeFavorites.includes(path)
              ),
              ...directories.addFavorites,
            ]),
          ],
          recent: current.recent.filter(
            (path) => !directories.removeRecent.includes(path)
          ),
        }
        if (next.favorites.length > 20) return error('limitReached')
        Object.assign(settings, mergePatch(settings, patch))
        if (JSON.stringify(current) !== JSON.stringify(next))
          setPreferences(next)
        // Even an empty committed draft fences a timed-out predecessor.
        generalRevision = uuidv7()
        return {
          ok: true,
          value: downloadsSnapshot(),
          update: { saved: true, requiresRestart: false },
        }
      }
      case Queries.ListServerDirectories: {
        const { path, showHidden } = ListServerDirectoriesRequestSchema.parse(
          args[0]
        )
        const children = folders.get(path)
        if (!children || !canVisit(path))
          return error(
            path.startsWith('/downloads/') || path.startsWith('/archive/')
              ? 'notFound'
              : 'outsideRoots'
          )
        const segments = path.split('/').filter(Boolean)
        return {
          ok: true,
          value: {
            path,
            parentPath:
              path === '/' || (!unrestricted && segments.length === 1)
                ? null
                : path.slice(0, path.lastIndexOf('/')) || '/',
            breadcrumbs: [
              ...(unrestricted ? [{ name: '/', path: '/' }] : []),
              ...segments.map((name, index) => ({
                name,
                path: `/${segments.slice(0, index + 1).join('/')}`,
              })),
            ],
            entries: children
              .filter((name) => showHidden || !name.startsWith('.'))
              .map((name) => {
                const childPath = `${path === '/' ? '' : path}/${name}`
                const modifiedAt = fixtureState.modifiedAtByPath[childPath]
                return {
                  name,
                  path: childPath,
                  ...(Number.isFinite(modifiedAt) ? { modifiedAt } : {}),
                }
              }),
            truncated: false,
            canCreate: true,
          },
        }
      }
      case Queries.ValidateServerDirectory: {
        const { path } = ValidateServerDirectoryRequestSchema.parse(args[0])
        return folders.has(path)
          ? { ok: true, value: { path } }
          : error('notFound')
      }
      case Commands.CreateServerDirectory: {
        const { parentPath, name } = CreateServerDirectoryRequestSchema.parse(
          args[0]
        )
        if (fixtureState.createFailure) return error('permissionDenied')
        const children = folders.get(parentPath)
        if (!children) return error('notFound')
        if (children.includes(name)) return error('alreadyExists')
        const path = `${parentPath}/${name}`
        folders.set(path, [])
        children.push(name)
        children.sort((left, right) =>
          left.localeCompare(right, 'en', { numeric: true })
        )
        return { ok: true, value: { name, path } }
      }
      case Commands.UpdateSettings: {
        const patch = args[0] as { app?: Partial<typeof settings.app> }
        settings.app = {
          ...settings.app,
          ...patch.app,
          directoryPreferences: preferences(),
        }
        return { saved: true, requiresRestart: false, changedRestartKeys: [] }
      }
      case Commands.CreateTask: {
        const request = taskCreateRequestSchema.parse(args[0])
        if (fixtureState.taskCreateFailure)
          throw new Error('The fixture rejected this download.')
        if (!canVisit(request.saveDir))
          throw new Error('The download folder is unavailable.')
        // Both hosts record an accepted request's saveDir and broadcast the
        // shared preferences event; rejected submissions never write history.
        if (!fixtureState.mutationFailure) {
          const current = preferences()
          setPreferences({
            ...current,
            recent: [
              request.saveDir,
              ...current.recent.filter((path) => path !== request.saveDir),
            ].slice(0, 10),
          })
        }
        return {
          outcome: 'created',
          gid: '0123456789abcdef',
          taskId: 'fixture-created-task',
        }
      }
      default:
        throw new Error(`Unhandled harness RPC: ${channel}`)
    }
  },
}