~glacambre/firenvim

ref: 6c5ec95986171814433de7a7396a5ab757f53381 firenvim/tests/_common.ts -rw-r--r-- 15.3 KiB
6c5ec959glacambre package-lock.json: version bump Firenvim 2 years ago
                                                                                
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
const fs = require("fs");
const path = require("path");
const webdriver = require("selenium-webdriver");
const Until = webdriver.until;
const By = webdriver.By;

import { readVimrc, writeVimrc } from "./_vimrc";

jest.setTimeout(40000)

export const pagesDir = path.resolve(path.join("tests", "pages"));
export const extensionDir = path.resolve("target");

export function getNewestFileMatching(directory: string, pattern: string | RegExp) {
        return new Promise((resolve, reject) => {
                fs.readdir(directory, (err: Error, filenames: string[]) => {
                        if (err) {
                                return reject(err);
                        }
                        return resolve(filenames);
                })
                // Keep only files matching pattern
        }).then((names: string[]) => names.filter(name => name.match(pattern)))
        // Get their stat struct
                .then(names => Promise.all(names.map(name => new Promise((resolve, reject) => {
                        const fpath = path.join(directory, name)
                        fs.stat(fpath, (err: Error, stats: { path: string, mtime: number }) => {
                                if (err) {
                                        reject(err);
                                }
                                stats.path = fpath;
                                return resolve(stats);
                        })
                }))))
        // Sort by most recent and keep first
                .then((stats: any[]) => (stats.sort((stat1, stat2) => stat2.mtime - stat1.mtime)[0] || {}).path)
}

const keyDelay = 100;

function sendKeys(driver: any, keys: any[]) {
        return keys.reduce((prom, key) => prom
                .then((action: any) => action.sendKeys(key))
                .then((action: any) => action.pause(keyDelay))
                , Promise.resolve(driver.actions())).then((action: any) => action.perform());
}

function loadLocalPage(driver: any, page: string) {
        return driver.get("file://" + path.join(pagesDir, page))
                .then(() => driver.executeScript("document.documentElement.focus()"));
}

export async function testTxties(driver: any) {
        console.log("Navigating to txti.es…");
        await driver.get("http://txti.es");
        console.log("Locating textarea…");
        const input = await driver.wait(Until.elementLocated(By.id("content-input")));
        await driver.executeScript("arguments[0].scrollIntoView(true);", input);
        console.log("Clicking on input…");
        await driver.actions().click(input).perform();
        console.log("Waiting for span to be created…");
        const span = await driver.wait(Until.elementLocated(By.css("body > span:nth-child(7)")));
        await driver.sleep(1000);
        console.log("Typing things…");
        await sendKeys(driver, "aTest".split("")
                .concat(webdriver.Key.ESCAPE)
                .concat(":wq!".split(""))
                .concat(webdriver.Key.ENTER)
        );
        console.log("Waiting for span to be removed…");
        await driver.wait(Until.stalenessOf(span));
        console.log("Waiting for value update…");
        await driver.wait(async () => (await input.getAttribute("value")) === "Test");
}

export async function testModifiers(driver: any) {
        await loadLocalPage(driver, "simple.html");
        console.log("Locating textarea…");
        const input = await driver.wait(Until.elementLocated(By.id("content-input")));
        await driver.executeScript("arguments[0].scrollIntoView(true);", input);
        console.log("Clicking on input…");
        await driver.actions().click(input).perform();
        console.log("Waiting for span to be created…");
        const span = await driver.wait(Until.elementLocated(By.css("body > span:nth-child(2)")));
        await driver.sleep(1000);
        console.log("Typing <C-v><C-a><C-v><A-v><C-v><D-a>…");
        await driver.actions()
                .keyDown("a")
                .keyUp("a")
                .pause(keyDelay)
                .keyDown(webdriver.Key.CONTROL)
                .keyDown("v")
                .keyUp("v")
                .pause(keyDelay)
                .keyDown("a")
                .keyUp("a")
                .pause(keyDelay)
                .keyDown("v")
                .keyUp("v")
                .keyUp(webdriver.Key.CONTROL)
                .pause(keyDelay)
                .keyDown(webdriver.Key.ALT)
                .keyDown("a")
                .keyUp("a")
                .keyUp(webdriver.Key.ALT)
                .pause(keyDelay)
                .keyDown(webdriver.Key.CONTROL)
                .keyDown("v")
                .keyUp("v")
                .keyUp(webdriver.Key.CONTROL)
                .pause(keyDelay)
                .keyDown(webdriver.Key.COMMAND)
                .keyDown("a")
                .keyUp("a")
                .keyUp(webdriver.Key.COMMAND)
                .pause(keyDelay)
                .perform();
        await driver.sleep(1000);
        console.log("Writing keycodes.");
        await sendKeys(driver, [webdriver.Key.ESCAPE]
                       .concat(":wq!".split(""))
                       .concat(webdriver.Key.ENTER))
        console.log("Waiting for span to be removed from page…");
        await driver.wait(Until.stalenessOf(span));
        console.log("Waiting for value update…");
        await driver.sleep(1000);
        await driver.wait(async () => ["\u0011<M-q><D-q>", "\u0001<M-a><D-a>"].includes(await input.getAttribute("value")));
}

export async function testCodemirror(driver: any) {
        console.log("Navigating to codemirror.net…");
        await driver.get("https://codemirror.net");
        console.log("Looking for codemirror div…");
        const input = await driver.wait(Until.elementLocated(By.css("div.CodeMirror")));
        await driver.executeScript("arguments[0].scrollIntoView(true);", input);
        console.log("Clicking on input…");
        await driver.actions().click(input).perform();
        console.log("Waiting for span to be created…");
        const span = await driver.wait(Until.elementLocated(By.css("body > span:nth-child(3)")));
        await driver.sleep(1000);
        console.log("Typing stuff…");
        await sendKeys(driver, "iTest".split("")
                .concat(webdriver.Key.ESCAPE)
                .concat(":wq!".split(""))
                .concat(webdriver.Key.ENTER)
        );
        console.log("Waiting for span to be removed from page…");
        await driver.wait(Until.stalenessOf(span));
        console.log("Waiting for value update…");
        await driver.wait(async () => /Test<!--/.test(await input.getAttribute("innerText")));
}

export async function testAce(driver: any) {
        console.log("Navigating to ace.c9.io…");
        await driver.get("https://ace.c9.io");
        console.log("Looking for ace div…");
        const input = await driver.wait(Until.elementLocated(By.css("div.ace_content")));
        await driver.executeScript("arguments[0].scrollIntoView(true);", input);
        console.log("Clicking on input…");
        await driver.actions().click(input).perform();
        console.log("Waiting for span to be created…");
        const span = await driver.wait(Until.elementLocated(By.css("body > span:nth-child(10)")));
        await driver.sleep(1000);
        console.log("Typing stuff…");
        await sendKeys(driver, "ATest".split("")
                .concat(webdriver.Key.ESCAPE)
                .concat(":wq!".split(""))
                .concat(webdriver.Key.ENTER)
        );
        console.log("Waiting for span to be removed from page…");
        await driver.wait(Until.stalenessOf(span));
        console.log("Waiting for value update…");
        await driver.wait(async () => /\/\*\*Test/.test(await input.getAttribute("innerText")));
}

export async function testDynamicTextareas(driver: any) {
        await loadLocalPage(driver, "dynamic.html");
        console.log("Locating button…");
        const btn = await driver.wait(Until.elementLocated(By.id("insert-textarea")));
        await driver.executeScript("arguments[0].scrollIntoView(true);", btn);
        console.log("Clicking on btn…");
        await driver.actions().click(btn).perform();
        console.log("Waiting for span to be created…");
        const span = await driver.wait(Until.elementLocated(By.css("body > span:nth-child(4)")));
        await driver.sleep(1000);
        console.log("Typing things…");
        await sendKeys(driver, "aTest".split("")
                .concat(webdriver.Key.ESCAPE)
                .concat(":wq!".split(""))
                .concat(webdriver.Key.ENTER)
        );
        console.log("Waiting for span to be removed…");
        await driver.wait(Until.stalenessOf(span));
        console.log("Waiting for value update…");
        const txtarea = await driver.wait(Until.elementLocated(By.css("body > textarea")));
        await driver.wait(async () => (await txtarea.getAttribute("value")) === "Test");
}

export async function testNestedDynamicTextareas(driver: any) {
        await loadLocalPage(driver, "dynamic_nested.html");
        console.log("Locating button…");
        const btn = await driver.wait(Until.elementLocated(By.id("insert-textarea")));
        await driver.executeScript("arguments[0].scrollIntoView(true);", btn);
        console.log("Clicking on btn…");
        await driver.actions().click(btn).perform();
        console.log("Waiting for span to be created…");
        const span = await driver.wait(Until.elementLocated(By.css("body > span:nth-child(4)")));
        await driver.sleep(1000);
        console.log("Typing things…");
        await sendKeys(driver, "aTest".split("")
                .concat(webdriver.Key.ESCAPE)
                .concat(":wq!".split(""))
                .concat(webdriver.Key.ENTER)
        );
        console.log("Waiting for span to be removed…");
        await driver.wait(Until.stalenessOf(span));
        console.log("Waiting for value update…");
        const txtarea = await driver.wait(Until.elementLocated(By.css("body > div:nth-child(3) > textarea:nth-child(1)")));
        await driver.wait(async () => (await txtarea.getAttribute("value")) === "Test");
}


// Purges a preloaded instance by creating a new frame, focusing it and quitting it
export async function killPreloadedInstance(driver: any) {
        console.log("Killing preloaded instance.");
        const id = "firenvim-" + Math.round(Math.random() * 1000);
        await driver.executeScript(`
                const txtarea = document.createElement("textarea");
                txtarea.id = "${id}";
                document.body.appendChild(txtarea);
                txtarea.scrollIntoView(true);`);
        const txtarea = await driver.wait(Until.elementLocated(By.id(id)));
        await driver.actions().click(txtarea).perform();
        await driver.actions()
                .keyDown(webdriver.Key.CONTROL)
                .keyDown("e")
                .pause(keyDelay)
                .keyUp("e")
                .keyUp(webdriver.Key.CONTROL)
                .perform();
        await driver.sleep(1000);
        await driver.executeScript(`
                const elem = document.getElementById("${id}");
                elem.parentElement.removeChild(elem);
        `);
};

export async function testVimrcFailure(driver: any) {
        // First, write buggy vimrc
        console.log("Backing up vimrc…");
        const backup = await readVimrc();
        console.log("Overwriting it…");
        await writeVimrc("call");
        await loadLocalPage(driver, "simple.html");
        await killPreloadedInstance(driver);
        // We can restore our vimrc
        await writeVimrc(backup);
        // Reload, to get the buggy instance
        await loadLocalPage(driver, "simple.html");
        console.log("Locating textarea…");
        const input = await driver.wait(Until.elementLocated(By.id("content-input")));
        await driver.executeScript("arguments[0].scrollIntoView(true);", input);
        console.log("Clicking on input…");
        await driver.actions().click(input).perform();
        console.log("Waiting for span to be created…");
        const span = await driver.wait(Until.elementLocated(By.css("body > span:nth-child(2)")));
        // The firenvim frame should disappear after a second
        console.log("Waiting for span to disappear…");
        await driver.wait(Until.stalenessOf(span));
};

export async function testManualNvimify(driver: any) {
        await loadLocalPage(driver, "simple.html");
        console.log("Backing up vimrc…");
        const backup = await readVimrc();
        console.log("Overwriting it…");
        await writeVimrc(`
let g:firenvim_config = {
    \\ 'localSettings': {
        \\ '.*': {
            \\ 'selector': '',
            \\ 'priority': 0,
        \\ }
    \\ }
\\ }
${backup}
                `);
        await killPreloadedInstance(driver);
        await loadLocalPage(driver, "simple.html");
        const input = await driver.wait(Until.elementLocated(By.id("content-input")));
        await driver.executeScript("arguments[0].scrollIntoView(true);", input);
        console.log("Clicking on input…");
        await driver.actions().click(input).perform();
        console.log("Making sure a frame didn't pop up.");
        await driver.sleep(1000);
        await driver.findElement(By.css("body > span:nth-child(2)"))
                .catch((): void => undefined)
                .then((e: any) => {
                        if (e !== undefined) {
                                throw new Error("Frame automatically created while disabled by config.");
                        }
                });
        await driver.actions()
                .keyDown(webdriver.Key.CONTROL)
                .keyDown("e")
                .pause(keyDelay)
                .keyUp("e")
                .keyUp(webdriver.Key.CONTROL)
                .perform();
        const span = await driver.wait(Until.elementLocated(By.css("body > span:nth-child(2)")));
        await driver.sleep(1000);
        console.log("Typing things…");
        await sendKeys(driver, "aTest".split("")
                .concat(webdriver.Key.ESCAPE)
                .concat(":wq!".split(""))
                .concat(webdriver.Key.ENTER)
        );
        console.log("Waiting for span to be removed…");
        await driver.wait(Until.stalenessOf(span));
        console.log("Waiting for value update…");
        await driver.wait(async () => (await input.getAttribute("value")) === "Test");
        // Unfocus element
        await driver.executeScript("arguments[0].blur();", input);
        await driver.sleep(1000);
        await driver.actions().click(input).perform();
        console.log("Making sure a frame didn't pop up.");
        await driver.sleep(1000);
        await driver.findElement(By.css("body > span:nth-child(2)"))
                .catch((): void => undefined)
                .then((e: any) => {
                        if (e !== undefined) {
                                throw new Error("Frame automatically created while disabled by config.");
                        }
                });
        await writeVimrc(backup);
        await killPreloadedInstance(driver);
};

export async function killDriver(driver: any) {
        try {
                await driver.close()
        } catch(e) {}
        try {
                await driver.quit()
        } catch(e) {}
}