forked from harvard-lil/js-wacz
/
index.test.js
335 lines (266 loc) · 10.7 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
/// <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 } 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 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('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)
})