forked from twilio-labs/open-pixel-art
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.test.js
108 lines (90 loc) · 3.29 KB
/
data.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
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
const path = require('path');
const fs = require('fs');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
async function loadJson(dataJsonFile) {
const filePath = path.resolve(__dirname, '../_data/', dataJsonFile);
const pixelJsonString = await readFile(filePath, 'utf8');
return JSON.parse(pixelJsonString);
}
describe('pixels', () => {
test('every username should only claim one pixel', async () => {
const pixels = await loadJson('pixels.json');
const usernameSet = new Set();
for (const pixel of pixels.data) {
if (pixel.username !== '<UNCLAIMED>') {
let username = usernameSet.has(pixel.username)
? pixel.username
: undefined;
expect(username).toBeUndefined();
}
usernameSet.add(pixel.username);
}
});
test('every pixel should be in the limits of the image', async () => {
const pixels = await loadJson('pixels.json');
const defaults = await loadJson('defaults.json');
for (const pixel of pixels.data) {
expect(pixel.x).toBeLessThan(defaults.image.width);
expect(pixel.x).toBeGreaterThanOrEqual(0);
expect(pixel.y).toBeLessThan(defaults.image.height);
expect(pixel.y).toBeGreaterThanOrEqual(0);
}
});
test('every pixel should have a color or a tileName property', async () => {
const pixels = await loadJson('pixels.json');
for (const pixel of pixels.data) {
const hasTileName = typeof pixel.tileName !== 'undefined';
const hasColor = typeof pixel.color !== 'undefined';
expect(hasTileName || hasColor).toBeTruthy();
}
});
test('every pixel should have a hex code color if present', async () => {
const pixels = await loadJson('pixels.json');
for (const pixel of pixels.data) {
const hasColor = typeof pixel.color !== 'undefined';
if (hasColor) {
expect(pixel.color).toMatch(/^#[0-9a-f]{6}$/i);
}
}
});
test("claimed name doesn't have brackets", async () => {
const pixels = await loadJson('pixels.json');
for (const pixel of pixels.data) {
if (pixel.username !== '<UNCLAIMED>')
expect(pixel.username).not.toMatch(/^\<.*\>$/);
}
});
test('no position should have more than one pixel', async () => {
const { data: pixels } = await loadJson('pixels.json');
const grouped = {};
pixels.forEach(pixel => {
const key = `{x: ${pixel.x}, y: ${pixel.y}}`;
grouped[key] = grouped[key] || [];
grouped[key].push(pixel);
});
const multiples = Object.entries(grouped)
.filter(([key, arr]) => arr.length > 1)
.map(([key, arr]) => arr);
expect(multiples).toMatchObject([]);
});
test('every pixel should be ordered from smallest to largest in y then x', async () => {
const pixels = await loadJson('pixels.json');
var lastPixel;
for (const pixel of pixels.data) {
if (lastPixel !== undefined) {
// Check if the current pixel is further along in either the x
// or the y axis
let invalidPixel = undefined;
let isValidPixelOrder =
pixel.y > lastPixel.y ||
(pixel.y >= lastPixel.y && pixel.x > lastPixel.x);
if (!isValidPixelOrder) {
invalidPixel = pixel;
}
expect(invalidPixel).toBeUndefined();
}
lastPixel = pixel;
}
});
});