/
util.js
188 lines (179 loc) · 7 KB
/
util.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
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
180
181
182
183
184
185
186
187
188
// Copyright 2013 Lovell Fuller and others.
// SPDX-License-Identifier: Apache-2.0
'use strict';
const assert = require('assert');
const semver = require('semver');
const sharp = require('../../');
describe('Utilities', function () {
describe('Cache', function () {
it('Can be disabled', function (done) {
queueMicrotask(() => {
sharp.cache(false);
const cache = sharp.cache(false);
assert.strictEqual(cache.memory.current, 0);
assert.strictEqual(cache.memory.max, 0);
assert.strictEqual(typeof cache.memory.high, 'number');
assert.strictEqual(cache.files.current, 0);
assert.strictEqual(cache.files.max, 0);
assert.strictEqual(cache.items.current, 0);
assert.strictEqual(cache.items.max, 0);
done();
});
});
it('Can be enabled with defaults', function () {
const cache = sharp.cache(true);
assert.strictEqual(cache.memory.max, 50);
assert.strictEqual(cache.files.max, 20);
assert.strictEqual(cache.items.max, 100);
});
it('Can be set to zero', function () {
const cache = sharp.cache({
memory: 0,
files: 0,
items: 0
});
assert.strictEqual(cache.memory.max, 0);
assert.strictEqual(cache.files.max, 0);
assert.strictEqual(cache.items.max, 0);
});
it('Can be set to a maximum of 10MB, 100 files and 1000 items', function () {
const cache = sharp.cache({
memory: 10,
files: 100,
items: 1000
});
assert.strictEqual(cache.memory.max, 10);
assert.strictEqual(cache.files.max, 100);
assert.strictEqual(cache.items.max, 1000);
});
it('Ignores invalid values', function () {
sharp.cache(true);
const cache = sharp.cache('spoons');
assert.strictEqual(cache.memory.max, 50);
assert.strictEqual(cache.files.max, 20);
assert.strictEqual(cache.items.max, 100);
});
});
describe('Concurrency', function () {
it('Can be set to use 16 threads', function () {
sharp.concurrency(16);
assert.strictEqual(16, sharp.concurrency());
});
it('Can be reset to default', function () {
sharp.concurrency(0);
assert.strictEqual(true, sharp.concurrency() > 0);
});
it('Ignores invalid values', function () {
const defaultConcurrency = sharp.concurrency();
sharp.concurrency('spoons');
assert.strictEqual(defaultConcurrency, sharp.concurrency());
});
});
describe('Counters', function () {
it('Have zero value at rest', (done) => {
queueMicrotask(() => {
const counters = sharp.counters();
assert.strictEqual(0, counters.queue);
assert.strictEqual(0, counters.process);
done();
});
});
});
describe('SIMD', function () {
it('Can get current state', function () {
const simd = sharp.simd();
assert.strictEqual(typeof simd, 'boolean');
});
it('Can disable', function () {
const simd = sharp.simd(false);
assert.strictEqual(simd, false);
});
it('Can attempt to enable', function () {
const simd = sharp.simd(true);
assert.strictEqual(typeof simd, 'boolean');
});
});
describe('Format', function () {
it('Contains expected attributes', function () {
assert.strictEqual('object', typeof sharp.format);
Object.keys(sharp.format).forEach(function (format) {
assert.strictEqual(true, 'id' in sharp.format[format]);
assert.strictEqual(format, sharp.format[format].id);
['input', 'output'].forEach(function (direction) {
assert.strictEqual(true, direction in sharp.format[format]);
assert.strictEqual('object', typeof sharp.format[format][direction]);
assert.strictEqual(true, [3, 4].includes(Object.keys(sharp.format[format][direction]).length));
assert.strictEqual(true, 'file' in sharp.format[format][direction]);
assert.strictEqual(true, 'buffer' in sharp.format[format][direction]);
assert.strictEqual(true, 'stream' in sharp.format[format][direction]);
assert.strictEqual('boolean', typeof sharp.format[format][direction].file);
assert.strictEqual('boolean', typeof sharp.format[format][direction].buffer);
assert.strictEqual('boolean', typeof sharp.format[format][direction].stream);
});
});
});
it('Raw file=false, buffer=true, stream=true', function () {
['input', 'output'].forEach(function (direction) {
assert.strictEqual(false, sharp.format.raw[direction].file);
assert.strictEqual(true, sharp.format.raw[direction].buffer);
assert.strictEqual(true, sharp.format.raw[direction].stream);
});
});
it('vips format supports filesystem only', function () {
['input', 'output'].forEach(function (direction) {
assert.strictEqual(true, sharp.format.vips[direction].file);
assert.strictEqual(false, sharp.format.vips[direction].buffer);
assert.strictEqual(false, sharp.format.vips[direction].stream);
});
});
it('input fileSuffix', function () {
assert.deepStrictEqual(['.jpg', '.jpeg', '.jpe', '.jfif'], sharp.format.jpeg.input.fileSuffix);
});
it('output alias', function () {
assert.deepStrictEqual(['jpe', 'jpg'], sharp.format.jpeg.output.alias);
});
});
describe('Versions', function () {
it('Contains expected attributes', function () {
assert.strictEqual('object', typeof sharp.versions);
assert(semver.valid(sharp.versions.vips));
assert(semver.valid(sharp.versions.sharp));
});
});
describe('Block', () => {
it('Can block a named operation', () => {
sharp.block({ operation: ['test'] });
});
it('Can unblock a named operation', () => {
sharp.unblock({ operation: ['test'] });
});
it('Invalid block operation throws', () => {
assert.throws(() => sharp.block(1),
/Expected object for options but received 1 of type number/
);
assert.throws(() => sharp.block({}),
/Expected Array<string> for operation but received undefined of type undefined/
);
assert.throws(() => sharp.block({ operation: 'fail' }),
/Expected Array<string> for operation but received fail of type string/
);
assert.throws(() => sharp.block({ operation: ['maybe', false] }),
/Expected Array<string> for operation but received maybe,false of type object/
);
});
it('Invalid unblock operation throws', () => {
assert.throws(() => sharp.unblock(1),
/Expected object for options but received 1 of type number/
);
assert.throws(() => sharp.unblock({}),
/Expected Array<string> for operation but received undefined of type undefined/
);
assert.throws(() => sharp.unblock({ operation: 'fail' }),
/Expected Array<string> for operation but received fail of type string/
);
assert.throws(() => sharp.unblock({ operation: ['maybe', false] }),
/Expected Array<string> for operation but received maybe,false of type object/
);
});
});
});