-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
test.js
277 lines (202 loc) · 8.93 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import fs from 'node:fs';
import test from 'ava';
import {fileTypeFromBuffer} from 'file-type';
import tempy from 'tempy';
import {execa} from 'execa';
import {fileIconToBuffer, fileIconToFile} from './index.js';
const processID = async app => {
const {stdout} = await execa('pgrep', [app]);
return Number.parseInt(stdout.split('\n')[0], 10);
};
const temporaryFile = () => tempy.file({extension: 'png'});
test('argument validation', async t => {
const finderPID = await processID('Finder');
await t.notThrowsAsync(fileIconToBuffer('Safari'));
await t.notThrowsAsync(fileIconToBuffer(finderPID));
await t.notThrowsAsync(fileIconToBuffer(['Safari', 'Finder']));
await t.notThrowsAsync(fileIconToBuffer(['Safari', finderPID]));
await t.throwsAsync(fileIconToBuffer({}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToBuffer([{}]), {instanceOf: TypeError});
await t.throwsAsync(fileIconToBuffer(['Safari', {}]), {instanceOf: TypeError});
await t.notThrowsAsync(fileIconToFile('Safari', {destination: temporaryFile()}));
await t.notThrowsAsync(fileIconToFile(finderPID, {destination: temporaryFile()}));
await t.notThrowsAsync(fileIconToFile(['Safari', 'Finder'], {destination: [temporaryFile(), temporaryFile()]}));
await t.notThrowsAsync(fileIconToFile(['Safari', finderPID], {destination: [temporaryFile(), temporaryFile()]}));
await t.throwsAsync(fileIconToFile(['Safari']), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile(['Safari'], {destination: temporaryFile()}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile(['Safari', 'Finder'], {destination: [temporaryFile()]}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile(['Safari', 'Finder'], {destination: [temporaryFile(), temporaryFile(), temporaryFile()]}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile({}, {destination: temporaryFile()}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile([{}], {destination: [temporaryFile()]}), {instanceOf: TypeError});
await t.throwsAsync(fileIconToFile(['Safari', {}], {destination: [temporaryFile(), temporaryFile()]}), {instanceOf: TypeError});
});
test('app name', async t => {
const buffer = await fileIconToBuffer('Safari');
const fileType = await fileTypeFromBuffer(buffer);
t.is(fileType.ext, 'png');
});
test('array of single app name', async t => {
const apps = ['Finder'];
const buffers = await fileIconToBuffer(apps);
const fileType = await fileTypeFromBuffer(buffers[0]);
t.is(buffers.length, 1);
t.is(fileType.ext, 'png');
});
test('array of multiple app names', async t => {
const apps = ['Finder', 'Safari'];
const buffers = await fileIconToBuffer(apps);
t.is(buffers.length, 2);
const fileType1 = await fileTypeFromBuffer(buffers[0]);
t.is(fileType1.ext, 'png');
const fileType2 = await fileTypeFromBuffer(buffers[1]);
t.is(fileType2.ext, 'png');
});
test('app bundle ID', async t => {
const buffer = await fileIconToBuffer('com.apple.Safari');
const fileType = await fileTypeFromBuffer(buffer);
t.is(fileType.ext, 'png');
});
test('array of single app bundle ID', async t => {
const apps = ['com.apple.Finder'];
const buffers = await fileIconToBuffer(apps);
t.is(buffers.length, 1);
const fileType = await fileTypeFromBuffer(buffers[0]);
t.is(fileType.ext, 'png');
});
test('array of multiple app bundle IDs', async t => {
const ids = ['com.apple.Finder', 'com.apple.Safari'];
const buffers = await fileIconToBuffer(ids);
t.is(buffers.length, 2);
const fileType1 = await fileTypeFromBuffer(buffers[0]);
t.is(fileType1.ext, 'png');
const fileType2 = await fileTypeFromBuffer(buffers[1]);
t.is(fileType2.ext, 'png');
});
test('app process ID', async t => {
const finderPID = await processID('Finder');
const buffer = await fileIconToBuffer(finderPID);
const fileType = await fileTypeFromBuffer(buffer);
t.is(fileType.ext, 'png');
});
test('array of single app process ID', async t => {
const finderPID = await processID('Finder');
const pids = [finderPID];
const buffers = await fileIconToBuffer(pids);
t.is(buffers.length, 1);
const fileType = await fileTypeFromBuffer(buffers[0]);
t.is(fileType.ext, 'png');
});
test('array of multiple app process IDs', async t => {
const apps = ['Finder', 'loginwindow'];
const pids = await Promise.all(apps.map(app => processID(app)));
const buffers = await fileIconToBuffer(pids);
t.is(buffers.length, 2);
const fileType1 = await fileTypeFromBuffer(buffers[0]);
t.is(fileType1.ext, 'png');
const fileType2 = await fileTypeFromBuffer(buffers[1]);
t.is(fileType2.ext, 'png');
});
test('array of mixed types', async t => {
const loginwindowPID = await processID('loginwindow');
const apps = ['com.apple.Finder', loginwindowPID, 'Safari'];
const buffers = await fileIconToBuffer(apps);
t.is(buffers.length, 3);
const fileType1 = await fileTypeFromBuffer(buffers[0]);
t.is(fileType1.ext, 'png');
const fileType2 = await fileTypeFromBuffer(buffers[1]);
t.is(fileType2.ext, 'png');
const fileType3 = await fileTypeFromBuffer(buffers[2]);
t.is(fileType3.ext, 'png');
});
test('file path', async t => {
const buffer = await fileIconToBuffer('/Applications/Safari.app');
const fileType = await fileTypeFromBuffer(buffer);
t.is(fileType.ext, 'png');
});
test('write file single app name', async t => {
const destination = temporaryFile();
await fileIconToFile('Safari', {destination});
const icon = fs.readFileSync(destination);
const fileType = await fileTypeFromBuffer(icon);
t.is(fileType.ext, 'png');
});
test('write file array of single app name', async t => {
const destination = [temporaryFile()];
await fileIconToFile(['Safari'], {destination});
t.is(destination.length, 1);
const fileType = await fileTypeFromBuffer(fs.readFileSync(destination[0]));
t.is(fileType.ext, 'png');
});
test('write files array of app names', async t => {
const destination = [temporaryFile(), temporaryFile()];
await fileIconToFile(['Safari', 'Finder'], {destination});
t.is(destination.length, 2);
const fileType1 = await fileTypeFromBuffer(fs.readFileSync(destination[0]));
t.is(fileType1.ext, 'png');
const fileType2 = await fileTypeFromBuffer(fs.readFileSync(destination[1]));
t.is(fileType2.ext, 'png');
});
test('write file single app bundle ID', async t => {
const destination = temporaryFile();
await fileIconToFile('com.apple.Safari', {destination});
const icon = fs.readFileSync(destination);
const fileType = await fileTypeFromBuffer(icon);
t.is(fileType.ext, 'png');
});
test('write file array of single app bundle ID', async t => {
const destination = [temporaryFile()];
await fileIconToFile(['com.apple.Safari'], {destination});
t.is(destination.length, 1);
const fileType = await fileTypeFromBuffer(fs.readFileSync(destination[0]));
t.is(fileType.ext, 'png');
});
test('write files array of app bundle IDs', async t => {
const destination = [temporaryFile(), temporaryFile()];
await fileIconToFile(['com.apple.Safari', 'com.apple.Finder'], {destination});
t.is(destination.length, 2);
const fileType1 = await fileTypeFromBuffer(fs.readFileSync(destination[0]));
t.is(fileType1.ext, 'png');
const fileType2 = await fileTypeFromBuffer(fs.readFileSync(destination[1]));
t.is(fileType2.ext, 'png');
});
test('write file single app process ID', async t => {
const destination = temporaryFile();
const pid = await processID('Finder');
await fileIconToFile(pid, {destination});
const icon = fs.readFileSync(destination);
const fileType = await fileTypeFromBuffer(icon);
t.is(fileType.ext, 'png');
});
test('write file array of single app process ID', async t => {
const apps = ['Finder'];
const destination = [temporaryFile()];
const pids = await Promise.all(apps.map(app => processID(app)));
await fileIconToFile(pids, {destination});
t.is(destination.length, 1);
const fileType = await fileTypeFromBuffer(fs.readFileSync(destination[0]));
t.is(fileType.ext, 'png');
});
test('write files array of app process IDs', async t => {
const apps = ['Finder', 'loginwindow'];
const destination = [temporaryFile(), temporaryFile()];
const pids = await Promise.all(apps.map(app => processID(app)));
await fileIconToFile(pids, {destination});
t.is(destination.length, 2);
const fileType1 = await fileTypeFromBuffer(fs.readFileSync(destination[0]));
t.is(fileType1.ext, 'png');
const fileType2 = await fileTypeFromBuffer(fs.readFileSync(destination[1]));
t.is(fileType2.ext, 'png');
});
test('write files array of mixed types', async t => {
const loginwindowPID = await processID('loginwindow');
const apps = ['com.apple.Finder', loginwindowPID, 'Safari'];
const destination = [temporaryFile(), temporaryFile(), temporaryFile()];
await fileIconToFile(apps, {destination});
t.is(destination.length, 3);
const fileType1 = await fileTypeFromBuffer(fs.readFileSync(destination[0]));
t.is(fileType1.ext, 'png');
const fileType2 = await fileTypeFromBuffer(fs.readFileSync(destination[1]));
t.is(fileType2.ext, 'png');
const fileType3 = await fileTypeFromBuffer(fs.readFileSync(destination[2]));
t.is(fileType3.ext, 'png');
});