This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-support.js
426 lines (359 loc) · 13.7 KB
/
test-support.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
/*
* Copyright (C) 2017 The University of Sydney Library
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict'
const BigInt = require('big-integer')
const jsv = require('jsverify')
const { calcCheckDigit } = require('@sydneyunilibrary/sierra-record-check-digit')
const _DEFAULT_JSV_ASSERT_OPTIONS = Object.freeze({ tests: process.env['JSV_TESTS'] || 100 })
function chaiProperty(...jsverifyPropertyArgs) {
const [ name, ...arbs ] = jsverifyPropertyArgs
const propCheckFn = arbs.pop()
const jsvAssertOptions = (
typeof arbs[arbs.length - 1] === 'object'
&& typeof(arbs[arbs.length - 1].generator) !== 'function'
? Object.assign({}, _DEFAULT_JSV_ASSERT_OPTIONS, arbs.pop())
: Object.assign({}, _DEFAULT_JSV_ASSERT_OPTIONS) // Grr. jsv.assert mutates jsvAssertOptions
)
it(name, function () {
jsv.assert(
jsv.forall(...arbs, (...values) => {
propCheckFn(...values)
return true
}),
jsvAssertOptions
)
})
}
function _arbitraryFromGenerator(gen) {
return jsv.bless({ generator: gen })
}
function _combineArbitraries(fn, ...arbitraries) {
return _arbitraryFromGenerator(
jsv.generator.combine(...arbitraries.map(a => a.generator), fn)
)
}
function _joinArbitraries(sep, ...arbitraries) {
return _combineArbitraries((...parts) => parts.join(sep), ...arbitraries)
}
function _fixedSizeArrayGenerator(len, gen) {
return jsv.generator.bless(
(size) => {
let arr = new Array(len)
for (let i = 0; i < len; i++) {
arr[i] = gen(size)
}
return arr
}
)
}
function _variableSizeArrayGenerator(minLen, maxLen, gen) {
return jsv.generator.bless(
(size) => {
let len = jsv.random(minLen, maxLen)
let arr = new Array(len)
for (let i = 0; i < len; i++) {
arr[i] = gen(size)
}
return arr
}
)
}
const NEVER = Symbol('NEVER')
const SOMETIMES = Symbol('SOMETIMES')
const ALWAYS = Symbol('ALWAYS')
const COMPLETELY_INVALID_ID = (
jsv.oneof([
jsv.oneof([
jsv.bool,
jsv.datetime,
jsv.falsy,
jsv.number,
jsv.unit,
jsv.dict(jsv.string),
jsv.array(jsv.string),
]),
jsv.suchthat(jsv.string, _1 => (
! /^\d{12,}$/.test(_1) &&
! /^\.?([boicaprnveltj])?[1-9]\d{5,6}([0-9x])?(@[a-z0-9]{1,5})?$/.test(_1) &&
! /^(https:\/\/[-%._~!$&'()*+,;=a-zA-Z0-9]+\/[-/%._~!$&'()*+,;=:@a-zA-Z0-9]+\/)?v4\/(authorities|bibs|invoices|items|orders|patrons)\/[1-9]\d{5,6}(@[a-z0-9]{1,5})?/.test(_1)
))
])
)
const INVALID_REC_NUM = jsv.oneof([ jsv.integer(0, 99999), jsv.integer(10000000, Number.MAX_SAFE_INTEGER) ])
const INVALID_RECORD_TYPE_CODE = jsv.elements('dfghkmqsuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split(''))
const API_INCOMPATIBLE_RECORD_TYPE_CODE = jsv.elements([ 'c', 'r', 'v', 'e', 'l', 't', 'j' ])
const DIGIT_CHAR = jsv.elements([ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ])
const LOWER_ALPHA_DIGIT_CHAR = jsv.elements('abcdefghijklmnopqrstuvwxyz0123456789'.split(''))
const CHECK_DIGIT = jsv.elements([ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'x' ])
const ALL_RECORD_TYPE_CODE = jsv.elements([ 'b', 'o', 'i', 'c', 'a', 'p', 'r', 'n', 'v', 'e', 'l', 't', 'j' ])
const API_COMPATIBLE_TYPE_CODE = jsv.elements([ 'a', 'b', 'n', 'i', 'o', 'p' ])
const V4_API_RECORD_TYPES = jsv.elements(['authorities', 'bibs', 'invoices', 'items', 'orders', 'patrons'])
const V5_API_RECORD_TYPES = jsv.elements(['authorities', 'bibs', 'invoices', 'items', 'orders', 'patrons'])
const CAMPUS_CODE = (
_arbitraryFromGenerator(_variableSizeArrayGenerator(1, 5, LOWER_ALPHA_DIGIT_CHAR.generator).map(_1 => _1.join('')))
)
const API_HOST = (
jsv.suchthat(
jsv.nearray(jsv.elements("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~".split(''))),
_1 => (
// Don't generate hostname that potentially look like API versions
_1[0] !== 'v'
)
)
.smap(_1 => _1.join(''), _1 => _1.split(''))
)
const API_PATH = (
jsv.suchthat(
jsv.nearray(jsv.elements("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:@/".split(''))),
_1 => (
// Don't single character paths
_1.length !== 1 &&
// Don't generate paths that start with a forward slash
_1[0] !== '/' &&
// Don't generate paths that potentially look like API versions
_1[0] !== 'v' &&
// Don't generate paths that contain consecutive forward slashes
!_1.reduce((acc, val, idx, arr) => acc || (val === '/' && arr[idx + 1] === '/'), false)
)
)
.smap(
_1 => {
let s = _1.join('')
/* Conditions are negated so the most common case (has / at neither start nor end) is tested first */
if (!s.startsWith('/') && !s.endsWith('/')) {
s = `/${s}/`
} else if (!s.startsWith('/')) {
s = `/${s}`
} else if (!s.endsWith('/')) {
s = `${s}/`
}
return s
},
_1 => (
/* Don't know if original had a / at end or start, assume most common case of it having had neither. */
_1.slice(1, -1).split('')
)
)
)
function arbitraryInitialPeriod(when = SOMETIMES) {
switch (when) {
case ALWAYS:
return jsv.constant('.')
case NEVER:
return jsv.unit
case SOMETIMES:
return jsv.oneof([arbitraryInitialPeriod(ALWAYS), jsv.unit])
default:
throw new Error(`initialPeriod is not ALWAYS, NEVER or SOMETIMES: ${when}`)
}
}
function arbitraryRecordTypeCode(apiCompatibleOnly = false) {
return apiCompatibleOnly ? API_COMPATIBLE_TYPE_CODE : ALL_RECORD_TYPE_CODE
}
function arbitraryRecNum(size = undefined) {
if (size === undefined) {
return jsv.oneof([arbitraryRecNum(6), arbitraryRecNum(7)])
} else {
return jsv.suchthat(
_arbitraryFromGenerator(_fixedSizeArrayGenerator(size, DIGIT_CHAR.generator).map(_1 => _1.join(''))),
_1 => !_1.startsWith('0')
)
}
}
function arbitraryVirtualRecordPart(when) {
switch (when) {
case ALWAYS:
return CAMPUS_CODE.smap(_1 => `@${_1}`, _1 => _1.split(1))
case NEVER:
return jsv.unit
case SOMETIMES:
return jsv.oneof([arbitraryVirtualRecordPart(ALWAYS), jsv.unit])
default:
throw new Error(`virtual is not ALWAYS, NEVER or SOMETIMES: ${when}`)
}
}
function arbitraryRecordNumber({ size = undefined, virtual = SOMETIMES } = {}) {
return _joinArbitraries('', arbitraryRecNum(size), arbitraryVirtualRecordPart(virtual))
}
function arbitraryWeakRecordKey({ size = undefined, initialPeriod = SOMETIMES, virtual = SOMETIMES, ambiguous = SOMETIMES, apiCompatibleOnly = false } = {}) {
if (size === undefined) {
switch (ambiguous) {
case ALWAYS:
return arbitraryWeakRecordKey({ size: 7, initialPeriod, apiCompatibleOnly, virtual, ambiguous })
case NEVER:
return arbitraryWeakRecordKey({ size: 6, initialPeriod, apiCompatibleOnly, virtual, ambiguous })
case SOMETIMES:
return jsv.oneof([
arbitraryWeakRecordKey({ size: 6, initialPeriod, apiCompatibleOnly, virtual, ambiguous }),
arbitraryWeakRecordKey({ size: 7, initialPeriod, apiCompatibleOnly, virtual, ambiguous }),
])
default:
throw new Error(`ambiguous is not ALWAYS, NEVER or SOMETIMES: ${ambiguous}`)
}
} else if (ambiguous === SOMETIMES || (size === 6 && ambiguous === NEVER) || (size === 7 && ambiguous === ALWAYS)) {
return _joinArbitraries(
'',
arbitraryInitialPeriod(initialPeriod),
arbitraryRecordTypeCode(apiCompatibleOnly),
arbitraryRecNum(size),
arbitraryVirtualRecordPart(virtual),
)
} else {
return jsv.unit
}
}
function arbitraryStrongRecordKey({ size = undefined, initialPeriod = SOMETIMES, virtual = SOMETIMES, ambiguous = SOMETIMES, apiCompatibleOnly = false } = {}) {
switch (size) {
case undefined:
if (ambiguous === ALWAYS) {
return arbitraryStrongRecordKey({ size: 6, initialPeriod, virtual, ambiguous, apiCompatibleOnly })
} else {
return jsv.oneof(
arbitraryStrongRecordKey({ size: 6, initialPeriod, virtual, ambiguous, apiCompatibleOnly }),
arbitraryStrongRecordKey({ size: 7, initialPeriod, virtual, ambiguous, apiCompatibleOnly }),
)
}
case 6:
return jsv.suchthat(
_combineArbitraries(
( ip, rtc, rn, vp ) => {
const cd = calcCheckDigit(rn)
return (
ambiguous === NEVER && cd !== 'x'
? undefined
: ambiguous === ALWAYS && cd === 'x'
? undefined
: [ ip, rtc, rn, cd, vp ].join('')
)
},
arbitraryInitialPeriod(initialPeriod),
arbitraryRecordTypeCode(apiCompatibleOnly),
arbitraryRecNum(size),
arbitraryVirtualRecordPart(virtual),
),
_1 => _1 !== undefined
)
case 7:
if (ambiguous === ALWAYS) {
throw new Error('Ambiguous 7 digit strong record keys are impossible')
} else {
return _combineArbitraries(
( ip, rtc, rn, vp ) => {
const cd = calcCheckDigit(rn)
return [ ip, rtc, rn, cd, vp ].join('')
},
arbitraryInitialPeriod(initialPeriod),
arbitraryRecordTypeCode(apiCompatibleOnly),
arbitraryRecNum(size),
arbitraryVirtualRecordPart(virtual),
)
}
default:
throw new Error(`size is not undefined, 6 or 7: ${size}`)
}
}
function arbitraryDatabaseId({ size = undefined, virtual = SOMETIMES, apiCompatibleOnly = false } = {}) {
const virtualRange = (
virtual === ALWAYS ? [ 1, 0xFFFF ]
: virtual === NEVER ? [ 0, 0 ]
: virtual === SOMETIMES ? [ 0, 0xFFFF ]
: undefined
)
if (!virtualRange) {
throw new Error(`virtual is not ALWAYS, NEVER or SOMETIMES: ${virtual}`)
}
const recNumRange = (
size === undefined ? [ 100000, 9999999 ]
: size === 6 ? [ 100000, 999999 ]
: size === 7 ? [ 1000000, 9999999 ]
: undefined
)
if (!recNumRange) {
throw new Error(`size is not undefined, 6 or 7: ${size}`)
}
return _arbitraryFromGenerator(
jsv.generator.combine(
jsv.integer(...virtualRange).generator,
arbitraryRecordTypeCode(apiCompatibleOnly).generator,
jsv.integer(...recNumRange).generator,
(campusId, recordTypeCode, recNum) => (
BigInt(campusId).shiftLeft(48)
.add(BigInt(recordTypeCode.codePointAt(0)).shiftLeft(32))
.add(BigInt(recNum))
.toString()
)
)
)
}
function arbitraryRelativeV4ApiUrl({ size = undefined, virtual = SOMETIMES } = {}) {
return _joinArbitraries('/', jsv.constant('/v4'), V4_API_RECORD_TYPES, arbitraryRecordNumber({ size, virtual }))
}
function arbitraryAbsoluteV4ApiUrl({ size = undefined, virtual = SOMETIMES, sierraApiHost = undefined, sierraApiPath = undefined } = {}) {
const arbitraryHost = sierraApiHost === undefined ? API_HOST : jsv.constant(sierraApiHost)
const arbitraryPath = sierraApiPath === undefined ? API_PATH : jsv.constant(sierraApiPath)
return _joinArbitraries('', jsv.constant('https://'), arbitraryHost, arbitraryPath, jsv.constant('v4/'),
V4_API_RECORD_TYPES, jsv.constant('/'), arbitraryRecordNumber({ size, virtual }))
}
function arbitraryRelativeV5ApiUrl({ size = undefined, virtual = SOMETIMES } = {}) {
return _joinArbitraries('/', jsv.constant('/v5'), V5_API_RECORD_TYPES, arbitraryRecordNumber({ size, virtual }))
}
function arbitraryAbsoluteV5ApiUrl({ size = undefined, virtual = SOMETIMES, sierraApiHost = undefined, sierraApiPath = undefined } = {}) {
const arbitraryHost = sierraApiHost === undefined ? API_HOST : jsv.constant(sierraApiHost)
const arbitraryPath = sierraApiPath === undefined ? API_PATH : jsv.constant(sierraApiPath)
return _joinArbitraries('', jsv.constant('https://'), arbitraryHost, arbitraryPath, jsv.constant('v5/'),
V5_API_RECORD_TYPES, jsv.constant('/'), arbitraryRecordNumber({ size, virtual }))
}
const _WHITESPACE = jsv.elements([ '\f', '\n', '\r', '\t', '\v' ])
function arbitraryWhitespaceString({ minLength = 0, maxLength = 50 } = {}) {
return _arbitraryFromGenerator(
_variableSizeArrayGenerator(minLength, maxLength, _WHITESPACE.generator).map(_1 => _1.join('')))
}
module.exports = {
chaiProperty,
arbitrary: Object.freeze({
NEVER,
SOMETIMES,
ALWAYS,
COMPLETELY_INVALID_ID,
INVALID_REC_NUM,
INVALID_RECORD_TYPE_CODE,
API_INCOMPATIBLE_RECORD_TYPE_CODE,
DIGIT_CHAR,
LOWER_ALPHA_DIGIT_CHAR,
CHECK_DIGIT,
ALL_RECORD_TYPE_CODE,
API_COMPATIBLE_TYPE_CODE,
V4_API_RECORD_TYPES,
V5_API_RECORD_TYPES,
CAMPUS_CODE,
API_HOST,
API_PATH,
initialPeriod: arbitraryInitialPeriod,
recordTypeCode: arbitraryRecordTypeCode,
recNum: arbitraryRecNum,
virtualRecordPart: arbitraryVirtualRecordPart,
recordNumber: arbitraryRecordNumber,
weakRecordKey: arbitraryWeakRecordKey,
strongRecordKey: arbitraryStrongRecordKey,
databaseId: arbitraryDatabaseId,
relativeV4ApiUrl: arbitraryRelativeV4ApiUrl,
absoluteV4ApiUrl: arbitraryAbsoluteV4ApiUrl,
relativeV5ApiUrl: arbitraryRelativeV5ApiUrl,
absoluteV5ApiUrl: arbitraryAbsoluteV5ApiUrl,
whitespaceString: arbitraryWhitespaceString,
}),
}