-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
174 lines (146 loc) · 3.84 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
'use strict';
const {createServer} = require('http');
const {promisify} = require('util');
const getChromeTabs = require('.');
const clearModule = require('clear-module');
const compact = require('lodash/compact');
const last = require('lodash/last');
const map = require('lodash/map');
const pretendPlatform = require('pretend-platform');
const puppeteer = require('puppeteer');
const test = require('tape');
test('getChromeTabs()', async t => {
const server = createServer((req, res) => {
const title = last(compact(req.url.split('/')));
res.writeHead(200, {
'content-type': 'text/html',
'content-length': 69 + title.length
});
res.end(`<!doctype html><html><head><title>${title}</title></head><body></body></html>`);
});
await promisify(server.listen.bind(server))(3018);
const browser = await puppeteer.launch({
headless: false,
args: ['http://127.0.0.1:3018/foo/']
});
const context = await browser.createIncognitoBrowserContext({headless: false});
try {
await (await context.newPage()).goto('http://127.0.0.1:3018/bar/');
await (await context.newPage()).goto('http://127.0.0.1:3018/baz/');
const tabs = await getChromeTabs({app: 'chromium'});
t.equal(
tabs.length,
3,
'should list open Chrome tabs.'
);
t.deepEqual(
map(tabs, 'windowIndex'),
[0, 0, 1],
'should set number of window order to each item.'
);
t.deepEqual(
map(tabs, 'url'),
['http://127.0.0.1:3018/bar/', 'http://127.0.0.1:3018/baz/', 'http://127.0.0.1:3018/foo/'],
'should set page URL to each item.'
);
t.deepEqual(
map(tabs, 'title'),
['bar', 'baz', 'foo'],
'should set page title to each item.'
);
t.deepEqual(
map(tabs, 'active'),
[false, true, true],
'should set tab active state to each item.'
);
} catch (err) {
t.fail(err.stack);
} finally {
await Promise.all([
browser.close(),
promisify(server.close.bind(server))()
]);
}
try {
await getChromeTabs();
t.fail('Unexpectedly succeeded.');
} catch (err) {
t.equal(
err.toString(),
'Error: Tried to get tabs of Chrome, but Chrome is currently not running.',
'should fail when Chrome is not running.'
);
}
t.end();
});
test('Argument validation', async t => {
const fail = t.fail.bind(t, 'Unexpectedly succeeded.');
try {
await getChromeTabs(new Uint32Array());
fail();
} catch (err) {
t.equal(
err.toString(),
'TypeError: Expected an <Object> to specify get-chrome-tabs option, but got Uint32Array [].',
'should fail when it takes a non-object argument.'
);
}
try {
await getChromeTabs({app: -0});
fail();
} catch (err) {
t.equal(
err.toString(),
'TypeError: Expected `app` option to be either \'canary\' or \'chromium\', but got a non-string value -0 (number).',
'should fail when `app` option is not a string.'
);
}
try {
await getChromeTabs({app: 'dartium'});
fail();
} catch (err) {
t.equal(
err.toString(),
'RangeError: Expected `app` option to be either \'canary\' or \'chromium\', but got neither of them \'dartium\'.',
'should fail when `app` option is an unsupported value.'
);
}
try {
await getChromeTabs({}, {});
fail();
} catch (err) {
t.equal(
err.toString(),
'RangeError: Expected 0 or 1 argument ([<Object>]), but got 2 arguments.',
'should fail when it takes too many arguments.'
);
}
t.end();
});
test('getChromeTabs() where `osascript` command doesn\'t exist', async t => {
process.env.PATH = '/none/exists';
try {
await require('.')({});
} catch ({code}) {
t.equal(
code,
'ENOENT',
'should fail.'
);
}
t.end();
});
test('getChromeTabs() on a non-macOS environment', async t => {
clearModule('.');
pretendPlatform('linux');
try {
await require('.')();
} catch (err) {
t.equal(
err.toString(),
'Error: get-chrome-tabs only supports macOS, but the current platform is Linux.',
'should fail.'
);
}
t.end();
});