summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/uuids_spec.js
blob: a7770d37566f4608a81493a349b687653e7b8f0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { uuids } from '~/lib/utils/uuids';

const HEX = /[a-f0-9]/i;
const HEX_RE = HEX.source;
const UUIDV4 = new RegExp(
  `${HEX_RE}{8}-${HEX_RE}{4}-4${HEX_RE}{3}-[89ab]${HEX_RE}{3}-${HEX_RE}{12}`,
  'i',
);

describe('UUIDs Util', () => {
  describe('uuids', () => {
    const SEQUENCE_FOR_GITLAB_SEED = [
      'a1826a44-316c-480e-a93d-8cdfeb36617c',
      'e049db1f-a4cf-4cba-aa60-6d95e3b547dc',
      '6e3c737c-13a7-4380-b17d-601f187d7e69',
      'bee5cc7f-c486-45c0-8ad3-d1ac5402632d',
      'af248c9f-a3a6-4d4f-a311-fe151ffab25a',
    ];
    const SEQUENCE_FOR_12345_SEED = [
      'edfb51e2-e3e1-4de5-90fd-fd1d21760881',
      '2f154da4-0a2d-4da9-b45e-0ffed391517e',
      '91566d65-8836-4222-9875-9e1df4d0bb01',
      'f6ea6c76-7640-4d71-a736-9d3bec7a1a8e',
      'bfb85869-5fb9-4c5b-a750-5af727ac5576',
    ];

    it('returns version 4 UUIDs', () => {
      expect(uuids()[0]).toMatch(UUIDV4);
    });

    it('outputs an array of UUIDs', () => {
      const ids = uuids({ count: 11 });

      expect(ids.length).toEqual(11);
      expect(ids.every((id) => UUIDV4.test(id))).toEqual(true);
    });

    it.each`
      seeds                          | uuid
      ${['some', 'special', 'seed']} | ${'6fa53e51-0f70-4072-9c84-1c1eee1b9934'}
      ${['magic']}                   | ${'fafae8cd-7083-44f3-b82d-43b30bd27486'}
      ${['seeded']}                  | ${'e06ed291-46c5-4e42-836b-e7c772d48b49'}
      ${['GitLab']}                  | ${'a1826a44-316c-480e-a93d-8cdfeb36617c'}
      ${['JavaScript']}              | ${'12dfb297-1560-4c38-9775-7178ef8472fb'}
      ${[99, 169834, 2619]}          | ${'3ecc8ad6-5b7c-4c9b-94a8-c7271c2fa083'}
      ${[12]}                        | ${'2777374b-723b-469b-bd73-e586df964cfd'}
      ${[9876, 'mixed!', 7654]}      | ${'865212e0-4a16-4934-96f9-103cf36a6931'}
      ${[123, 1234, 12345, 6]}       | ${'40aa2ee6-0a11-4e67-8f09-72f5eba04244'}
      ${[0]}                         | ${'8c7f0aac-97c4-4a2f-b716-a675d821ccc0'}
    `(
      'should always output the UUID $uuid when the options.seeds argument is $seeds',
      ({ uuid, seeds }) => {
        expect(uuids({ seeds })[0]).toEqual(uuid);
      },
    );

    describe('unseeded UUID randomness', () => {
      const nonRandom = Array(6)
        .fill(0)
        .map((_, i) => uuids({ seeds: [i] })[0]);
      const random = uuids({ count: 6 });
      const moreRandom = uuids({ count: 6 });

      it('is different from a seeded result', () => {
        random.forEach((id, i) => {
          expect(id).not.toEqual(nonRandom[i]);
        });
      });

      it('is different from other random results', () => {
        random.forEach((id, i) => {
          expect(id).not.toEqual(moreRandom[i]);
        });
      });

      it('never produces any duplicates', () => {
        expect(new Set(random).size).toEqual(random.length);
      });
    });

    it.each`
      seed        | sequence
      ${'GitLab'} | ${SEQUENCE_FOR_GITLAB_SEED}
      ${12345}    | ${SEQUENCE_FOR_12345_SEED}
    `(
      'should output the same sequence of UUIDs for the given seed "$seed"',
      ({ seed, sequence }) => {
        expect(uuids({ seeds: [seed], count: 5 })).toEqual(sequence);
      },
    );
  });
});