Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

6.2 KB
Raw Blame
import { Command, InvalidArgumentError } from 'commander';
import { describe, expect, it } from 'vitest';
import { getCliProgram } from '../../bin/helpers/cli-program.js';
import { validateNumberInput } from '../../bin/utils/validate.js';

describe('CLI options', () => {
  const program = getCliProgram();

  it.each([{ args: [] }, { args: ['--no-restore-last-url'] }])(
    'keeps URL restoration on by default and parses the opt-out: $args',
    ({ args }) => {
      const option = program.options.find(
        (item) => item.long === '--no-restore-last-url',
      );
      expect(option).toBeDefined();
      const command = new Command().addOption(option!);
      command.parse(args, { from: 'user' });
      expect(command.opts().restoreLastUrl).toBe(args.length === 0);
      expect(command.getOptionValueSource('restoreLastUrl')).toBe(
        args.length === 0 ? 'default' : 'cli',
      );
    },
  );

  it('describes --icon platform formats including Icon Composer packages', () => {
    const option = program.options.find((item) => item.long === '--icon');
    expect(option).toBeDefined();
    expect(option?.description).toContain('.icon');
    expect(option?.description).toContain('.icns');
    expect(option?.description).toContain('.ico');
    expect(option?.description).toContain('.png');
  });

  it('shows meta options in help', () => {
    const help = program.helpInformation();

    expect(help).toContain('-h, --help');
    expect(help).toContain('-v, --version');
  });

  it('shows advanced options in help', () => {
    const help = program.helpInformation();

    expect(help).toContain('--enable-find');
    expect(help).toContain('--internal-url-regex');
    expect(help).toContain('--hide-on-close');
  });

  it('registers hidden --multi-window option', () => {
    const option = program.options.find(
      (item) => item.long === '--multi-window',
    );

    expect(option).toBeDefined();
    expect(option?.defaultValue).toBe(false);
  });

  it('registers hidden --basic-auth as a runtime opt-in', () => {
    const option = program.options.find((item) => item.long === '--basic-auth');

    expect(option).toBeDefined();
    expect(option?.defaultValue).toBe(false);
    expect(option?.hidden).toBe(true);
    expect(option?.required).toBe(false);
    expect(option?.optional).toBe(false);
  });

  it('registers password autosave as a hidden Windows opt-in', () => {
    const option = program.options.find(
      (item) => item.long === '--password-autosave',
    );
    expect(option?.defaultValue).toBe(false);
    expect(option?.hidden).toBe(true);
    expect(option?.required).toBe(false);
  });

  it('exposes --internal-url-regex option', () => {
    const option = program.options.find(
      (item) => item.long === '--internal-url-regex',
    );

    expect(option).toBeDefined();
    expect(option?.defaultValue).toBe('');
    expect(option?.hidden).toBeFalsy();
  });

  it('exposes --safe-domain option', () => {
    const option = program.options.find(
      (item) => item.long === '--safe-domain',
    );

    expect(option).toBeDefined();
    expect(option?.defaultValue).toBe('');
    expect(option?.hidden).toBeFalsy();
  });

  it('exposes --force-internal-navigation option', () => {
    const option = program.options.find(
      (item) => item.long === '--force-internal-navigation',
    );

    expect(option).toBeDefined();
    expect(option?.defaultValue).toBe(false);
    expect(option?.hidden).toBeFalsy();
  });

  it('exposes --new-window option', () => {
    const option = program.options.find((item) => item.long === '--new-window');

    expect(option).toBeDefined();
    expect(option?.defaultValue).toBe(false);
    expect(option?.hidden).toBeFalsy();
  });

  it('registers hidden --identifier option', () => {
    const option = program.options.find((item) => item.long === '--identifier');

    expect(option).toBeDefined();
    expect(option?.hidden).toBe(true);
  });

  it('registers hidden --install option', () => {
    const option = program.options.find((item) => item.long === '--install');

    expect(option).toBeDefined();
    expect(option?.defaultValue).toBe(false);
    expect(option?.hidden).toBe(true);
  });

  it('registers hidden --enable-find option', () => {
    const option = program.options.find(
      (item) => item.long === '--enable-find',
    );

    expect(option).toBeDefined();
    expect(option?.defaultValue).toBe(false);
    expect(option?.hidden).toBe(true);
  });

  it('rejects malformed zoom values instead of truncating them', () => {
    const option = program.options.find((item) => item.long === '--zoom');

    expect(option).toBeDefined();
    expect(option?.parseArg?.('80', undefined)).toBe(80);
    expect(() => option?.parseArg?.('80abc', undefined)).toThrow(
      '--zoom must be an integer between 50 and 200',
    );
    // Fractional in-range values cannot deserialize into the Rust u32 zoom
    // field, so they must be rejected rather than forwarded to pake.json.
    expect(() => option?.parseArg?.('99.5', undefined)).toThrow(
      '--zoom must be an integer between 50 and 200',
    );
  });

  it('reports rejected zoom and hide-on-close values as invalid arguments', () => {
    // Commander only treats InvalidArgumentError as a parse error, which
    // bin/cli.ts reports as INVALID_INPUT (exit 2, JSON result with --json).
    for (const [flag, value] of [
      ['--zoom', '99.5'],
      ['--hide-on-close', 'maybe'],
    ]) {
      const option = program.options.find((item) => item.long === flag);
      expect(option).toBeDefined();
      expect(() => option?.parseArg?.(value, undefined)).toThrow(
        InvalidArgumentError,
      );
    }
  });

  it('rejects non-finite numeric option values', () => {
    expect(() => validateNumberInput('Infinity')).toThrow('Not a number.');
    expect(() => validateNumberInput('-Infinity')).toThrow('Not a number.');
    expect(validateNumberInput('1200')).toBe(1200);
  });

  it('rejects blank numeric option values', () => {
    expect(() => validateNumberInput('')).toThrow('Not a number.');
    expect(() => validateNumberInput('   ')).toThrow('Not a number.');
  });

  it('rejects negative numeric option values', () => {
    expect(() => validateNumberInput('-100')).toThrow('Must not be negative.');
    expect(validateNumberInput('0')).toBe(0);
  });
});