-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathfile_stats.test.js
56 lines (47 loc) · 1.73 KB
/
file_stats.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
import child_process from "child_process";
import fs from "fs";
test("ensure that stats file is modified", async () => {
const child = child_process.exec(
"docker run -v $PWD/test-crawls:/crawls webrecorder/browsertrix-crawler crawl --url https://webrecorder.net/ --generateWACZ --text --limit 3 --exclude community --collection file-stats --statsFilename progress.json",
);
// detect crawler exit
let crawler_exited = false;
child.on("exit", function () {
crawler_exited = true;
});
// helper function to sleep
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
// wait for stats file creation up to 30 secs (to not wait indefinitely)
let counter = 0;
while (!fs.existsSync("test-crawls/progress.json")) {
await sleep(100);
counter++;
expect(counter < 300).toBe(true);
}
// get initial modification time
const initial_mtime = fs.fstatSync(
fs.openSync("test-crawls/progress.json", "r"),
).mtime;
// wait for crawler exit
while (!crawler_exited) {
await sleep(100);
}
// get final modification time
const final_mtime = fs.fstatSync(
fs.openSync("test-crawls/progress.json", "r"),
).mtime;
// compare initial and final modification time
const diff = Math.abs(final_mtime - initial_mtime);
expect(diff > 0).toBe(true);
});
test("check that stats file format is correct", () => {
const data = fs.readFileSync("test-crawls/progress.json", "utf8");
const dataJSON = JSON.parse(data);
expect(dataJSON.crawled).toEqual(3);
expect(dataJSON.total).toEqual(3);
expect(dataJSON.pending).toEqual(0);
expect(dataJSON.failed).toEqual(0);
expect(dataJSON.limit.max).toEqual(3);
expect(dataJSON.limit.hit).toBe(true);
expect(dataJSON.pendingPages.length).toEqual(0);
});