aboutsummaryrefslogtreecommitdiff
path: root/test/git.spec.ts
blob: 23548ef14b530a5ddd64819df6cb3bc5396a9314 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import validator from 'validator';
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../src';

const seededRuns = [
  {
    seed: 42,
    expectations: {
      branch: {
        noArgs: 'array-transmit',
      },
      commitEntry: {
        noArgs: '',
      },
      commitMessage: {
        noArgs: 'navigate neural capacitor',
      },
      commitSha: {
        noArgs: '5cf2bc99272107d592ba00fbdf302f2949806048',
      },
      shortSha: {
        noArgs: '5cf2bc9',
      },
    },
  },
  {
    seed: 1337,
    expectations: {
      branch: {
        noArgs: 'port-quantify',
      },
      commitEntry: {
        noArgs: '',
      },
      commitMessage: {
        noArgs: 'compress multi-byte panel',
      },
      commitSha: {
        noArgs: '48234870538945f4b41c61a52bf27dccc0576698',
      },
      shortSha: {
        noArgs: '4823487',
      },
    },
  },
  {
    seed: 1211,
    expectations: {
      branch: {
        noArgs: 'capacitor-connect',
      },
      commitEntry: {
        noArgs: '',
      },
      commitMessage: {
        noArgs: 'reboot online circuit',
      },
      commitSha: {
        noArgs: 'e7ec32f0a2a3c652bbd0caabde64dfdf379e3259',
      },
      shortSha: {
        noArgs: 'e7ec32f',
      },
    },
  },
];

const NON_SEEDED_BASED_RUN = 5;

const functionNames = [
  'branch',
  'commitEntry',
  'commitMessage',
  'commitSha',
  'shortSha',
];

describe('git', () => {
  afterEach(() => {
    faker.locale = 'en';
  });

  for (const { seed, expectations } of seededRuns) {
    describe(`seed: ${seed}`, () => {
      for (const functionName of functionNames) {
        if (functionName === 'commitEntry') {
          it.todo(`${functionName}()`);
          continue;
        }

        it(`${functionName}()`, () => {
          faker.seed(seed);

          const actual = faker.git[functionName]();
          expect(actual).toEqual(expectations[functionName].noArgs);
        });
      }
    });
  }

  // Create and log-back the seed for debug purposes
  faker.seed(Math.ceil(Math.random() * 1_000_000_000));

  describe(`random seeded tests for seed ${faker.seedValue}`, () => {
    for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
      describe('branch()', () => {
        it('should return a random branch', () => {
          const branch = faker.git.branch();

          expect(branch).toBeTruthy();
          expect(typeof branch).toBe('string');
          expect(branch).satisfy(validator.isSlug);
        });
      });

      describe('commitEntry', () => {
        it('should return a random commitEntry', () => {
          const commitEntry = faker.git.commitEntry();

          expect(commitEntry).toBeTruthy();
          expect(typeof commitEntry).toBe('string');

          const parts = commitEntry.split(/\r?\n/);

          expect(parts.length).greaterThanOrEqual(6);
          expect(parts.length).lessThanOrEqual(7);

          expect(parts[0]).match(/^commit [a-f0-9]+$/);
          if (parts.length === 7) {
            expect(parts[1]).match(/^Merge: [a-f0-9]+ [a-f0-9]+$/);
            expect(parts[2]).match(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/);
            expect(parts[3]).match(/^Date: .+$/);
            expect(parts[4]).toBe('');
            expect(parts[5]).match(/^\s{4}.+$/);
          } else {
            expect(parts[1]).match(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/);
            expect(parts[2]).match(/^Date: .+$/);
            expect(parts[3]).toBe('');
            expect(parts[4]).match(/^\s{4}.+$/);
          }
        });
      });

      describe('commitMessage', () => {
        it('should return a random commitMessage', () => {
          const commitMessage = faker.git.commitMessage();

          expect(commitMessage).toBeTruthy();
          expect(typeof commitMessage).toBe('string');

          const parts = commitMessage.split(' ');
          expect(parts.length).greaterThanOrEqual(3);
        });
      });

      describe('commitSha', () => {
        it('should return a random commitSha', () => {
          const commitSha = faker.git.commitSha();

          expect(commitSha).toBeTruthy();
          expect(typeof commitSha).toBe('string');
          expect(commitSha).satisfy(validator.isHexadecimal);
          expect(commitSha).toHaveLength(40);
        });
      });

      describe('shortSha', () => {
        it('should return a random shortSha', () => {
          const shortSha = faker.git.shortSha();

          expect(shortSha).toBeTruthy();
          expect(typeof shortSha).toBe('string');
          expect(shortSha).satisfy(validator.isHexadecimal);
          expect(shortSha).toHaveLength(7);
        });
      });
    }
  });
});