Skip to content

A full-featured download manager.

5.8 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { PluginEngineVersionTooOld, PluginManifestInvalid } from './errors'
import { parseManifest, semverSatisfies } from './parse'

describe('plugin engine version ranges', () => {
  it.each([
    ['2.0.0-beta.9', false],
    ['2.0.0-beta.44', false],
    ['2.0.0-beta.45', true],
    ['2.0.0-beta.46', true],
    ['2.0.0-beta.100', true],
    ['2.0.0-alpha.99', false],
    ['2.0.0-rc.1', true],
    ['2.0.0', true],
    ['2.0.1', true],
    ['3.0.0', false],
  ])('checks %s against a beta floor: %s', (version, expected) => {
    expect(semverSatisfies(version, '>=2.0.0-beta.45 <3.0.0')).toBe(expected)
  })

  it.each([
    ['2.0.0-beta.45', '>2.0.0-beta.45', false],
    ['2.0.0-beta.46', '>2.0.0-beta.45', true],
    ['2.0.0-beta.44', '<2.0.0-beta.45', true],
    ['2.0.0-beta.45', '<2.0.0-beta.45', false],
    ['2.0.0-beta.45', '<=2.0.0-beta.45', true],
    ['2.0.0', '<=2.0.0-beta.45', false],
    ['2.0.0-beta.45', '=2.0.0-beta.45', true],
    ['2.0.0', '=2.0.0-beta.45', false],
    ['2.0.0-beta.45+local.1', '2.0.0-beta.45+build.2', true],
    ['2.0.0-beta.9', '^2.0.0-beta.10', false],
    ['2.0.0', '^2.0.0-beta.45', true],
    ['3.0.0', '^2.0.0-beta.45', false],
    ['2.0.0-beta.44', '~2.0.0-beta.45', false],
    ['2.0.1', '~2.0.0-beta.45', true],
    ['2.1.0', '~2.0.0-beta.45', false],
    ['2.0.0-beta.45', '>=2.0.0-beta.45 <2.0.0', true],
    ['2.0.0', '>=2.0.0-beta.45 <2.0.0', false],
    ['2.0.0-beta.45', '>=2.0.0 <=2.0.0-beta.46', false],
  ])('%s satisfies %s: %s', (version, range, expected) => {
    expect(semverSatisfies(version, range)).toBe(expected)
  })

  it('preserves the release-only API ranges used by existing builtin plugins', () => {
    expect(semverSatisfies('2.0.0-beta.45', '>=2.0.0 <3.0.0')).toBe(true)
    expect(semverSatisfies('2.0.0-beta.45', '>=2.0.1 <3.0.0')).toBe(false)
    expect(semverSatisfies('3.0.0-beta.1', '>=2.0.0 <3.0.0')).toBe(false)
    expect(semverSatisfies('2.0.0-beta.45', '*')).toBe(true)
  })

  it.each(['2.0', '2.0.0-beta.', '2.0.0-beta.045', '2.0.0.45', '2.0.0+'])(
    'rejects malformed version %s instead of ignoring its suffix',
    (version) => {
      expect(() => semverSatisfies(version, '>=2.0.0-beta.45')).toThrow(
        PluginManifestInvalid
      )
      expect(() => semverSatisfies('2.0.0-beta.45', `>=${version}`)).toThrow(
        PluginManifestInvalid
      )
    }
  )
})

const VALID_JSON = JSON.stringify({
  manifestVersion: 1,
  id: 'alice.demo',
  name: 'Demo',
  version: '1.0.0',
  description: 'd',
  categories: ['integration'],
  engines: { motrix: '>=2.0.0 <3.0.0' },
  main: 'dist/plugin.js',
  permissions: [],
  activationEvents: ['onStartup'],
  contributes: {},
})

describe('parseManifest', () => {
  it('parses a valid manifest', () => {
    const r = parseManifest(VALID_JSON, { hostVersion: '2.5.0' })
    expect(r.manifest.id).toBe('alice.demo')
  })

  it('throws PluginEngineVersionTooOld when host version below range', () => {
    expect(() => parseManifest(VALID_JSON, { hostVersion: '1.9.0' })).toThrow(
      PluginEngineVersionTooOld
    )
  })

  it('enforces a beta minimum during manifest loading and accepts the final release', () => {
    const manifest = JSON.stringify({
      ...JSON.parse(VALID_JSON),
      engines: { motrix: '>=2.0.0-beta.45' },
    })
    expect(() =>
      parseManifest(manifest, { hostVersion: '2.0.0-beta.44' })
    ).toThrow(PluginEngineVersionTooOld)
    expect(
      parseManifest(manifest, { hostVersion: '2.0.0-beta.45' }).manifest.id
    ).toBe('alice.demo')
    expect(parseManifest(manifest, { hostVersion: '2.0.0' }).manifest.id).toBe(
      'alice.demo'
    )
  })

  it('throws PluginManifestInvalid for syntactic JSON error', () => {
    expect(() => parseManifest('{ not json', { hostVersion: '2.5.0' })).toThrow(
      PluginManifestInvalid
    )
  })

  it('throws PluginManifestInvalid for schema violation', () => {
    expect(() =>
      parseManifest(JSON.stringify({ ...JSON.parse(VALID_JSON), id: 'BAD' }), {
        hostVersion: '2.5.0',
      })
    ).toThrow(PluginManifestInvalid)
  })

  it('engines check runs BEFORE schema check', () => {
    const bad = JSON.stringify({
      ...JSON.parse(VALID_JSON),
      id: 'BAD',
      engines: { motrix: '>=99.0.0' },
    })
    expect(() => parseManifest(bad, { hostVersion: '2.5.0' })).toThrow(
      PluginEngineVersionTooOld
    )
  })

  it('warnings collected for unknown contributes keys', () => {
    const m = JSON.stringify({
      ...JSON.parse(VALID_JSON),
      contributes: { themes: {} },
    })
    const r = parseManifest(m, { hostVersion: '2.5.0' })
    expect(r.warnings.some((w) => w.code === 'unknown-contributes-key')).toBe(
      true
    )
  })

  describe('reserved publisher (origin gate)', () => {
    const RESERVED = JSON.stringify({
      ...JSON.parse(VALID_JSON),
      id: 'motrix.demo',
    })

    it('rejects motrix.* when origin is community (default)', () => {
      expect(() => parseManifest(RESERVED, { hostVersion: '2.5.0' })).toThrow(
        PluginManifestInvalid
      )
    })

    it('rejects motrix.* when origin is explicitly community', () => {
      expect(() =>
        parseManifest(RESERVED, {
          hostVersion: '2.5.0',
          origin: 'community',
        })
      ).toThrow(PluginManifestInvalid)
    })

    it('accepts motrix.* when origin is builtin', () => {
      const r = parseManifest(RESERVED, {
        hostVersion: '2.5.0',
        origin: 'builtin',
      })
      expect(r.manifest.id).toBe('motrix.demo')
    })

    it('builtin origin still rejects bad schema (motrix.* with invalid version)', () => {
      const bad = JSON.stringify({
        ...JSON.parse(RESERVED),
        version: 'not-semver',
      })
      expect(() =>
        parseManifest(bad, { hostVersion: '2.5.0', origin: 'builtin' })
      ).toThrow(PluginManifestInvalid)
    })
  })
})