-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.js
77 lines (56 loc) · 1.77 KB
/
test.js
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
/* istanbul ignore next */
describe('yeast', function () {
'use strict';
var assume = require('assume')
, yeast = require('./');
function waitUntilNextMillisecond() {
var now = +new Date();
while (+new Date() === now) { /* do nothing */ }
}
it('is exported as an function', function () {
assume(yeast).is.a('function');
});
it('exposes the helper functions', function () {
assume(yeast.encode).is.a('function');
assume(yeast.decode).is.a('function');
});
it('returns strings', function () {
assume(yeast()).is.a('string');
});
it('prepends an iterated seed when previous id is the same', function () {
waitUntilNextMillisecond();
var ids = [yeast(), yeast(), yeast()];
assume(ids[0]).does.not.include('.');
assume(ids[1]).includes('.0');
assume(ids[2]).includes('.1');
});
it('resets the seed', function () {
waitUntilNextMillisecond();
var ids = [yeast(), yeast(), yeast()];
assume(ids[0]).does.not.include('.');
assume(ids[1]).includes('.0');
assume(ids[2]).includes('.1');
waitUntilNextMillisecond();
ids = [yeast(), yeast(), yeast()];
assume(ids[0]).does.not.include('.');
assume(ids[1]).includes('.0');
assume(ids[2]).includes('.1');
});
it('does not collide', function () {
var length = 30000
, ids = new Array(length)
, i;
for (i = 0; i < length; i++) ids[i] = yeast();
ids.sort();
for (i = 0; i < length - 1; i++) {
if (ids[i] === ids[i + 1]) throw new Error('Found a duplicate entry');
}
});
it('can convert the id to a timestamp', function () {
waitUntilNextMillisecond();
var now = +new Date()
, id = yeast();
assume(yeast.encode(now)).equals(id);
assume(yeast.decode(id)).equals(now);
});
});