<commit_before>import { Cipher } from './simple-cipher';

describe('Random key generation', () => {
  xtest('generates keys at random', () => {
    // Strictly speaking, this is difficult to test with 100% certainty.
    // But, if you have a generator that generates 100-character-long
    // strings of lowercase letters at random, the odds of two consecutively
    // generated keys being identical are astronomically low.
    expect(new Cipher().key).not.toEqual(new Cipher().key);
  });
});

describe('Random key cipher', () => {
  const cipher = new Cipher();

  test('has a key made of letters', () => {
    expect(cipher.key).toMatch(/^[a-z]+$/);
  });

  xtest('has a key that is at least 100 characters long', () => {
    expect(cipher.key.length).toBeGreaterThanOrEqual(100);
  });

  // Here we take advantage of the fact that plaintext of "aaa..."
  // outputs the key. This is a critical problem with shift ciphers, some
  // characters will always output the key verbatim.
  xtest('can encode', () => {
    expect(cipher.encode('aaaaaaaaaa')).toEqual(cipher.key.substr(0, 10));
  });

  xtest('can decode', () => {
    expect(cipher.decode(cipher.key.substr(0, 10))).toEqual('aaaaaaaaaa');
  });

  xtest('is reversible', () => {
    const plaintext = 'abcdefghij';
    expect(cipher.decode(cipher.encode(plaintext))).toEqual(plaintext);
  });
});

describe('Incorrect key cipher', () => {
  xtest('throws an error with an all caps key', () => {
    expect(() => {
      new Cipher('ABCDEF');
    }).toThrow(new Error('Bad key'));
  });

  xtest('throws an error with a numeric key', () => {
    expect(() => {
      new Cipher('12345');
    }).toThrow(new Error('Bad key'));
  });

  xtest('throws an error with an empty key', () => {
    expect(() => {
      new Cipher('');
    }).toThrow(new Error('Bad key'));
  });

  xtest('throws an error with a leading space', () => {
    expect(() => {
      new Cipher(' leadingspace');
    }).toThrow(new Error('Bad key'));
  });

  xtest('throws an error with a punctuation mark', () => {
    expect(() => {
      new Cipher('hyphened-word');
    }).toThrow(new Error('Bad key'));<commit_msg>simple-cipher: Disallow mixed case key (#491)<commit_after>import { Cipher } from './simple-cipher';

describe('Random key generation', () => {
  xtest('generates keys at random', () => {
    // Strictly speaking, this is difficult to test with 100% certainty.
    // But, if you have a generator that generates 100-character-long
    // strings of lowercase letters at random, the odds of two consecutively
    // generated keys being identical are astronomically low.
    expect(new Cipher().key).not.toEqual(new Cipher().key);
  });
});

describe('Random key cipher', () => {
  const cipher = new Cipher();

  test('has a key made of letters', () => {
    expect(cipher.key).toMatch(/^[a-z]+$/);
  });

  xtest('has a key that is at least 100 characters long', () => {
    expect(cipher.key.length).toBeGreaterThanOrEqual(100);
  });

  // Here we take advantage of the fact that plaintext of "aaa..."
  // outputs the key. This is a critical problem with shift ciphers, some
  // characters will always output the key verbatim.
  xtest('can encode', () => {
    expect(cipher.encode('aaaaaaaaaa')).toEqual(cipher.key.substr(0, 10));
  });

  xtest('can decode', () => {
    expect(cipher.decode(cipher.key.substr(0, 10))).toEqual('aaaaaaaaaa');
  });

  xtest('is reversible', () => {
    const plaintext = 'abcdefghij';
    expect(cipher.decode(cipher.encode(plaintext))).toEqual(plaintext);
  });
});

describe('Incorrect key cipher', () => {
  xtest('throws an error with an all caps key', () => {
    expect(() => {
      new Cipher('ABCDEF');
    }).toThrow(new Error('Bad key'));
  });

  xtest('throws an error with a mixed-case key', () => {
    expect(() => {
      new Cipher('ABcdEF');
    }).toThrow(new Error('Bad key'));
  });

  xtest('throws an error with a numeric key', () => {
    expect(() => {
      new Cipher('12345');
    }).toThrow(new Error('Bad key'));
  });

  xtest('throws an error with an empty key', () => {
    expect(() => {
      new Cipher('');
    }).toThrow(new Error('Bad key'));
  });

  xtest('throws an error with a leading space', () => {
    expect(() => {
      new Cipher(' leadingspace');
    }).toThrow(new Error('Bad key'));
  });

  xtest('throws an error with a punctuation mark', () => {
    expect(() => {
      new Cipher('hyphened-word');
    }).toThrow(new Error('Bad key'));
{'fraction_non_alphanumeric': 0.1855647137447093, 'fraction_numerical': 0.010915571396747606, 'mean_word_length': 3.329797492767599, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4882987', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1351, 'n_words': 397}