-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.js
435 lines (345 loc) · 15.2 KB
/
index.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/// <reference path="types.js" />
import test from 'node:test'
import assert from 'node:assert/strict'
import { sep } from 'path'
import fs from 'fs/promises'
import log from 'loglevel'
import { globSync } from 'glob'
import StreamZip from 'node-stream-zip'
import * as dotenv from 'dotenv'
import { WACZ } from './index.js'
import { FIXTURES_PATH, PAGES_DIR_FIXTURES_PATH, PAGES_FIXTURE_PATH, EXTRA_PAGES_FIXTURE_PATH, LOG_DIR_FIXTURES_PATH, LOG_FILE_FIXTURE_PATH, CDXJ_DIR_FIXTURES_PATH } from './constants.js'
import { assertSHA256WithPrefix, assertValidWACZSignatureFormat } from './utils/assertions.js' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
// Loads env vars from .env if provided
dotenv.config()
/**
* Path to *.warc.gz files in the fixture folder.
* @constant
*/
const FIXTURE_INPUT = `${FIXTURES_PATH}${sep}*.warc.gz`
test('WACZ constructor throws if options.log is provided but not Console-API compatible.', async (_t) => {
const scenarios = [true, 'foo', {}, Buffer.alloc(0), 12, () => {}]
for (const log of scenarios) {
assert.throws(() => new WACZ({ input: FIXTURE_INPUT, log }))
}
})
test('WACZ constructor accepts a Console-API compatible object for options.log.', async (_t) => {
const archive = new WACZ({ input: FIXTURE_INPUT, log })
assert.equal(archive.log, log)
})
test('WACZ constructor throws if options.input is absent or invalid.', async (_t) => {
const scenarios = [null, true, 'foo', {}, Buffer.alloc(0), 12, () => {}, './']
for (const input of scenarios) {
assert.throws(() => new WACZ({ input }))
}
})
test('WACZ constructor accepts options.input if it is either a string or array.', async (_t) => {
const scenarios = [FIXTURE_INPUT, [FIXTURE_INPUT]]
for (const input of scenarios) {
assert.doesNotThrow(() => new WACZ({ input }))
}
})
test('WACZ constructor throws if options.output is invalid.', async (_t) => {
const scenarios = ['foo', true, {}, Buffer.alloc(0), 12, () => {}, './', 'test.zip']
for (const output of scenarios) {
assert.throws(() => new WACZ({ input: FIXTURE_INPUT, output }))
}
})
test('WACZ constructor ignores options.detectPages if invalid.', async (_t) => {
const scenarios = ['foo', {}, Buffer.alloc(0), 12, () => {}]
for (const detectPages of scenarios) {
const archive = new WACZ({ input: FIXTURE_INPUT, detectPages })
assert.equal(archive.detectPages, true)
}
})
test('WACZ constructor accounts for options.detectPages if valid.', async (_t) => {
const archive = new WACZ({ input: FIXTURE_INPUT, detectPages: false })
assert.equal(archive.detectPages, false)
})
test('WACZ constructor accounts for options.pagesDir if provided.', async (_t) => {
const archive = new WACZ({ input: FIXTURE_INPUT, pagesDir: PAGES_DIR_FIXTURES_PATH })
assert.equal(archive.detectPages, false)
assert.equal(archive.pagesDir, PAGES_DIR_FIXTURES_PATH)
})
test('WACZ constructor ignores options.indexFromWARCs if invalid.', async (_t) => {
const scenarios = ['foo', {}, Buffer.alloc(0), 12, () => {}]
for (const indexFromWARCs of scenarios) {
const archive = new WACZ({ input: FIXTURE_INPUT, indexFromWARCs })
assert.equal(archive.indexFromWARCs, true)
}
})
test('WACZ constructor accounts for options.indexFromWARCs if valid.', async (_t) => {
const archive = new WACZ({ input: FIXTURE_INPUT, indexFromWARCs: false })
assert.equal(archive.indexFromWARCs, false)
})
test('WACZ constructor ignores options.url if invalid.', async (_t) => {
const scenarios = ['foo', {}, Buffer.alloc(0), 12, () => {}]
for (const url of scenarios) {
const archive = new WACZ({ input: FIXTURE_INPUT, url })
assert.equal(archive.url, null)
}
})
test('WACZ constructor accounts for options.url if valid.', async (_t) => {
const url = 'https://lil.law.harvard.edu'
const archive = new WACZ({ input: FIXTURE_INPUT, url })
assert.equal(archive.url, url)
})
test('WACZ constructor ignores options.ts if invalid.', async (_t) => {
const scenarios = ['YESTERDAY', 'foo', () => {}]
for (const ts of scenarios) {
const archive = new WACZ({ input: FIXTURE_INPUT })
const defaultTs = archive.ts
archive.filterNonBlockingOptions({ ts })
assert.equal(archive.ts, defaultTs)
}
})
test('WACZ constructor accounts for options.ts if valid.', async (_t) => {
const ts = new Date().toISOString()
const archive = new WACZ({ input: FIXTURE_INPUT, ts })
assert.equal(archive.ts, ts)
})
test('WACZ constructor accounts for options.title if provided.', async (_t) => {
const archive = new WACZ({ input: FIXTURE_INPUT, title: 'FOO' })
assert.equal(archive.title, 'FOO')
})
test('WACZ constructor accounts for options.description if provided.', async (_t) => {
const archive = new WACZ({ input: FIXTURE_INPUT, description: 'FOO' })
assert.equal(archive.description, 'FOO')
})
test('WACZ constructor ignores options.signingUrl if invalid.', async (_t) => {
const scenarios = ['foo', {}, Buffer.alloc(0), 12, () => {}]
for (const signingUrl of scenarios) {
const archive = new WACZ({ input: FIXTURE_INPUT, signingUrl })
assert.equal(archive.signingUrl, null)
}
})
test('WACZ constructor accounts for options.signingUrl if valid.', async (_t) => {
const signingUrl = 'https://lil.law.harvard.edu'
const archive = new WACZ({ input: FIXTURE_INPUT, signingUrl })
assert.equal(archive.signingUrl, signingUrl)
})
test('WACZ constructor ignores options.signingUrl if invalid.', async (_t) => {
const scenarios = ['foo', {}, Buffer.alloc(0), 12, () => {}]
for (const signingUrl of scenarios) {
const archive = new WACZ({ input: FIXTURE_INPUT, signingUrl })
assert.equal(archive.signingUrl, null)
}
})
test('WACZ constructor accounts for options.signingToken if provided alongside options.signingUrl.', async (_t) => {
const scenarios = [
{ signingUrl: 'https://lil.law.harvard.edu', signingToken: 'FOO', shouldHaveToken: true },
{ signingUrl: null, signingToken: 'FOO', shouldHaveToken: false }
]
for (const scenario of scenarios) {
const archive = new WACZ({
input: FIXTURE_INPUT,
signingUrl: scenario.signingUrl,
signingToken: scenario.signingToken
})
if (scenario.shouldHaveToken) {
assert.equal(archive.signingToken, scenario.signingToken)
} else {
assert.equal(archive.signingToken, null)
}
}
})
test('WACZ constructor accounts for options.datapackageExtras if provided.', async (_t) => {
const datapackageExtras = { foo: 'bar' }
const archive = new WACZ({ input: FIXTURE_INPUT, datapackageExtras })
assert.equal(archive.datapackageExtras, datapackageExtras)
})
test('WACZ constructor accounts for options.logDir if valid.', async (_t) => {
const archive = new WACZ({ input: FIXTURE_INPUT, logDir: LOG_DIR_FIXTURES_PATH })
assert.equal(archive.logDir, LOG_DIR_FIXTURES_PATH)
})
test('addPage adds entry to pagesTree and turns detectPages off.', async (_t) => {
const archive = new WACZ({ input: FIXTURE_INPUT })
assert.equal(archive.detectPages, true)
assert.equal(archive.pagesTree.length, 0)
archive.addPage('https://lil.law.harvard.edu', 'LIL')
assert.equal(archive.detectPages, false)
assert.equal(archive.pagesTree.length, 1)
})
test('addCDXJ adds entry to cdxTree and turns indexFromWARCs off.', async (_t) => {
const archive = new WACZ({ input: FIXTURE_INPUT })
assert.equal(archive.indexFromWARCs, true)
assert.equal(archive.cdxTree.length, 0)
archive.addCDXJ('net,webrecorder)/ 20240307070734 {"url":"https://webrecorder.net/","mime":"text/html","status":200,"digest":"16966a2a2909825ad1d9a6f1b2f4833c8fe43428cb9920d0f974bd7b3d73c31d","length":3941,"offset":0,"filename":"rec-8bc4bd095683-20240307070734658-0.warc.gz"}')
assert.equal(archive.indexFromWARCs, false)
assert.equal(archive.cdxTree.length, 1)
})
// Note: if `TEST_SIGNING_URL` / `TEST_SIGNING_TOKEN` are present, this will also test the signing feature.
test('WACZ.process runs the entire process and writes a valid .wacz to disk, accounting for options.', async (_t) => {
//
// Preparation step: create WACZ out of .warc.gz files in "fixtures" folder.
//
const options = {
input: FIXTURE_INPUT,
output: '../tmp.wacz',
url: 'https://lil.law.harvard.edu',
title: 'WACZ Title',
description: 'WACZ Description',
ts: '2023-02-22T12:00:00Z',
datapackageExtras: { context: 'Testing' },
signingUrl: process.env?.TEST_SIGNING_URL,
signingToken: process.env?.TEST_SIGNING_TOKEN
}
const archive = new WACZ(options)
// Test adding extra files
await archive.addFileToZip(
Buffer.from('HELLO WORLD'),
'hello.txt'
)
await archive.process(false)
//
// Load up resulting WACZ to check that everything worked
//
const zip = new StreamZip.async({ file: options.output }) // eslint-disable-line
const zipEntries = await zip.entries()
//
// Indexes should be present
//
// NOTE: A test for the ZipNum Shared Index feature would require additional / larger fixtures.
assert(await zip.entryData('indexes/index.cdx'))
//
// `hello.txt` should be present
//
assert(await zip.entryData('hello.txt'))
//
// There should be as many .warc.gz files as there are in the fixtures folder.
//
let warcCount = 0
for (const entry of Object.values(zipEntries)) {
if (entry.name.endsWith('.warc.gz')) {
warcCount += 1
// Loosely check that it is indeed a .warc.gz
const data = await zip.entryData(entry.name)
assert.equal(data[0], 0x1f)
assert.equal(data[1], 0x8b)
}
}
assert.equal(warcCount, globSync(FIXTURE_INPUT).length)
//
// datapackage.json should be present, valid, and hold the data we passed to it.
//
const datapackage = JSON.parse(await zip.entryData('datapackage.json'))
assert.equal(datapackage.title, options.title)
assert.equal(datapackage.profile, 'data-package')
assert.equal(datapackage.description, options.description)
assert.equal(datapackage.mainPageUrl, options.url)
assert.equal(datapackage.mainPageDate, new Date(options.ts).toISOString())
assert.deepEqual(datapackage.extras, options.datapackageExtras)
assert.deepEqual(
datapackage.resources,
archive.resources.filter(entry => entry.name !== 'datapackage.json')
)
//
// datapackage-digest.json should be present and valid
//
const datapackageDigest = JSON.parse(await zip.entryData('datapackage-digest.json'))
assert(datapackageDigest.hash)
assert.doesNotThrow(() => assertSHA256WithPrefix(datapackageDigest.hash))
assert(datapackageDigest.path, 'datapackage.json')
// Extra: if `TEST_SIGNING_URL` was provided, check signature
if (process.env?.TEST_SIGNING_URL) {
assert.doesNotThrow(() => assertValidWACZSignatureFormat(datapackageDigest.signedData))
}
//
// All lines in pages.jsonl should be valid JSON in the format we expect.
//
const pagesJSONL = (await zip.entryData('pages/pages.jsonl')).toString('utf-8')
let pagesCount = 0
for (const entry of pagesJSONL.split('\n')) {
if (!entry.startsWith('{')) {
continue
}
const page = JSON.parse(entry)
// First line
if (page?.format) {
assert.equal(page.format, 'json-pages-1.0')
assert(page.id)
assert(page.title)
continue
}
// All other lines
assert(page.url)
assert(page.id)
if (page?.ts) {
assert.doesNotThrow(() => new Date(page.ts))
}
pagesCount += 1
}
assert.notEqual(pagesCount, 0)
// Delete temp file
await fs.unlink(options.output)
})
test('WACZ.process with pagesDir option creates valid WACZ with provided pages files.', async (_t) => {
const options = {
input: FIXTURE_INPUT,
output: '../tmp.wacz',
url: 'https://lil.law.harvard.edu',
title: 'WACZ Title',
description: 'WACZ Description',
pagesDir: PAGES_DIR_FIXTURES_PATH,
logDir: LOG_DIR_FIXTURES_PATH
}
const archive = new WACZ(options)
await archive.process(false)
const zip = new StreamZip.async({ file: options.output }) // eslint-disable-line
// Files in fixtures directories that are invalid JSONL or have wrong extensions
// should not be copied into the WACZ.
assert.rejects(async () => await zip.entryData('pages/invalid.jsonl'))
assert.rejects(async () => await zip.entryData('pages/invalid.txt'))
assert.rejects(async () => await zip.entryData('logs/invalid.md'))
// pages/pages.jsonl and pages/extraPages.jsonl should have same hash as fixtures
// they were copied from.
const datapackage = JSON.parse(await zip.entryData('datapackage.json'))
const datapackagePages = datapackage.resources.filter(entry => entry.path === 'pages/pages.jsonl')[0]
const pagesFixtureHash = await archive.sha256(PAGES_FIXTURE_PATH)
assert.equal(datapackagePages.hash, pagesFixtureHash)
const datapackageExtraPages = datapackage.resources.filter(entry => entry.path === 'pages/extraPages.jsonl')[0]
const extraPagesFixtureHash = await archive.sha256(EXTRA_PAGES_FIXTURE_PATH)
assert.equal(datapackageExtraPages.hash, extraPagesFixtureHash)
// log file provided in logDir option should have same hash as fixture
const datapackageLogFile = datapackage.resources.filter(entry => entry.path === 'logs/sample.log')[0]
const logFileFixtureHash = await archive.sha256(LOG_FILE_FIXTURE_PATH)
assert.equal(datapackageLogFile.hash, logFileFixtureHash)
// Delete temp file
await fs.unlink(options.output)
})
test('WACZ.process with cdxj option creates valid WACZ with index from provided CDXJ files.', async (_t) => {
const options = {
input: FIXTURE_INPUT,
output: '../tmp.wacz',
url: 'https://lil.law.harvard.edu',
title: 'WACZ Title',
description: 'WACZ Description',
pagesDir: PAGES_DIR_FIXTURES_PATH,
cdxjDir: CDXJ_DIR_FIXTURES_PATH
}
const archive = new WACZ(options)
await archive.process(false)
const zip = new StreamZip.async({ file: options.output }) // eslint-disable-line
//
// Indexes should be present
//
// NOTE: A test for the ZipNum Shared Index feature would require additional / larger fixtures.
assert(await zip.entryData('indexes/index.cdx'))
// Check index contests
const combinedCDX = (await zip.entryData('indexes/index.cdx')).toString('utf-8')
let pageIndex = 0
for (const entry of combinedCDX.split('\n')) {
if (pageIndex === 0) {
assert.equal(entry, 'net,webrecorder)/ 20240814070734 {"url":"https://webrecorder.net/","mime":"text/html","status":200,"digest":"16966a2a2909825ad1d9a6f1b2f4833c8fe43428cb9920d0f974bd7b3d73c31d","length":3941,"offset":0,"filename":"rec-8bc4bd095683-20240307070734658-0.warc.gz"}')
} else if (pageIndex === 1) {
assert.equal(entry, 'net,webrecorder)/assets/favicon.ico 20240814162442 {"url":"https://webrecorder.net/assets/favicon.ico","mime":"image/vnd.microsoft.icon","status":200,"digest":"e39a17af5d611f3a36784bc70128f93c10a7f5db03626d3030edf2ee1772e328","length":15398,"offset":87313,"filename":"rec-de8ca7249fc0-20240814162441960-0.warc.gz"}')
} else if (pageIndex === 2) {
assert.equal(entry, 'net,webrecorder)/assets/wr-logo.svg 20240814162441 {"url":"https://webrecorder.net/assets/wr-logo.svg","mime":"image/svg","status":200,"digest":"00c5957f7c97b2e79433fe607bdb47ecb3837a7ee7b603849e7cfb52dcc5f4c7","length":2041,"offset":19176,"filename":"rec-de8ca7249fc0-20240814162441960-0.warc.gz"}')
} else {
assert.equal(entry, '')
}
pageIndex += 1
}
// Delete temp file
await fs.unlink(options.output)
})