-
Notifications
You must be signed in to change notification settings - Fork 0
/
hvml.test.js
420 lines (369 loc) · 13.3 KB
/
hvml.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
/* eslint-disable global-require */
const skipIf = require( 'skip-if' );
const { execSync } = require( 'child_process' );
const { readFileSync } = require( 'fs' );
// test( 'opens files successfully', () => {} );
const JSON_LD = JSON.parse( readFileSync( './examples/hvml.jsonld', 'utf8' ) );
const Validation = require( './util/validation' );
// skipIf(condition, name, test)
const libxmljsUnavailable = ( () => {
let canParseXml = false;
try {
( require.resolve( 'libxmljs' ) );
canParseXml = true;
} catch ( error ) {
// eslint-disable-line no-empty
}
return !canParseXml;
} )();
const libxmljsAvailable = !libxmljsUnavailable;
const skipIfLibxmljsUnavailable = skipIf( () => libxmljsUnavailable );
const skipIfXmllintUnavailable = skipIf( () => {
let exitCode;
let error;
if ( libxmljsUnavailable ) {
return true;
}
try {
exitCode = execSync( 'xmllint', { "encoding": "utf8" } );
} catch ( err ) {
error = err;
exitCode = err.status;
}
return (
( exitCode === 127 )
|| !/xmllint \[options\] XMLfiles/.test( error.stdout )
);
} );
describe( 'HVML', () => {
describe( 'instantiates', () => {
beforeEach( () => {
jest.resetModules();
} );
test( 'from nothing', () => {
const { HVML } = require( './hvml' );
const hvml = new HVML();
return expect( hvml.ready ).resolves.toEqual( { "@context": JSON_LD['@context'] } );
} );
skipIfLibxmljsUnavailable( 'from XML', ( done ) => {
const { HVML } = require( './hvml' );
const hvml = new HVML( './examples/hvml.xml' );
expect.assertions( 2 );
expect( hvml.ready ).resolves.toEqual( expect.anything() );
hvml.ready.then( ( xml ) => {
expect( xml.constructor.name ).toBe( 'Document' );
done();
} );
} );
test( 'from XML: throws an error when libxmljs is not installed', () => {
if ( libxmljsAvailable ) {
jest.mock( 'libxmljs', () => {
throw new Error( `Cannot find module 'libxmljs'` );
} );
}
const { HVML } = require( './hvml' );
const hvml = new HVML( './examples/hvml.xml' );
return expect( hvml.ready ).rejects.toEqual( expect.anything() );
} );
test( 'from JSON-LD', () => {
const { HVML } = require( './hvml' );
const hvml = new HVML( './examples/hvml.jsonld' );
return expect( hvml.ready ).resolves.toEqual( JSON_LD );
} );
it( 'throws an error for unsupported file types', () => {
const { HVML } = require( './hvml' );
const hvml = new HVML( './examples/hvml.yaml' );
return expect( hvml.ready ).rejects.toEqual( expect.anything() );
} );
it( 'throws an error for nonexistent files', () => {
const { HVML } = require( './hvml' );
const hvml = new HVML( './gone.hvml' );
return expect( hvml.ready ).rejects.toEqual( expect.anything() );
} );
it( 'throws an error for nonexistent schema files', () => {
const { HVML } = require( './hvml' );
const hvml = new HVML( './examples/hvml.xml', {
"schemaPath": "rng/gone.rng",
} );
return expect( hvml.ready ).rejects.toEqual( expect.anything() );
} );
} );
describe( 'Returns data as JSON-LD', () => {
const { HVML } = require( './hvml' );
skipIfLibxmljsUnavailable( 'from XML', () => {
const hvml = new HVML( './examples/hvml.xml' );
return hvml.ready
.then( () => {
expect( hvml.toJson() ).toStrictEqual( JSON_LD );
} )
.catch( ( error ) => {
throw new Error( error.toString() );
} );
} );
it( 'from JSON-LD', () => {
const hvml = new HVML( './examples/hvml.jsonld' );
return hvml.ready
.then( () => {
expect( hvml.toJson() ).toStrictEqual( JSON_LD );
} )
.catch( ( error ) => {
throw new Error( error.toString() );
} );
} );
} );
// test( '_jsonifyChild', () => {
// const hvml = new HVML( './examples/hvml.xml' );
// const parsed = xml.parseXmlString(
// '<?xml version="1.0" encoding="UTF-8"?><foo><bar><baz /></bar><qux><quux /></qux></foo>',
// );
// const path = [];
//
// hvml.ready
// .then( () => {
// parsed.root().childNodes().forEach( ( node ) => {
// if ( node.type() === 'element' ) {
// // console.log( 'node', node.name() );
// // hvml._jsonifyChild( node, path, true );
// hvml._jsonifyChild( node );
// // console.log( 'path', path );
// console.log( 'hvml.json', hvml.json );
// }
// } );
// } );
// } );
describe( 'Validation', () => {
const { HVML } = require( './hvml' );
skipIfXmllintUnavailable( 'validates good HVML', ( done ) => {
const goodHvml = new HVML( './examples/hvml.xml' );
goodHvml.ready
.then( () => goodHvml.validate() )
.then( ( goodValidationResult ) => {
expect( goodValidationResult ).toStrictEqual( true );
done();
} )
.catch( ( error ) => {
expect( error ).toBeUndefined();
done();
} );
} );
describe( 'validates bad HVML', () => {
skipIfXmllintUnavailable( 'unexpected element', ( done ) => {
const badHvmlPath = './examples/legacy/redblue.ovml.xml';
const badHvml = new HVML( badHvmlPath );
badHvml.ready
.then( () => badHvml.validate() )
.then( ( badValidationResult ) => {
expect( badValidationResult ).toBeUndefined();
done();
} )
.catch( ( error ) => {
expect( error ).toStrictEqual( [{
"error": "Expecting element hvml, got ovml",
"expecting": "hvml",
"file": badHvmlPath,
"got": "ovml",
"line": "3",
"message": `${badHvmlPath}:3: element ovml: Relax-NG validity error : Expecting element hvml, got ovml`, // eslint-disable-line
"type": "validity",
}] );
done();
} );
} );
skipIfXmllintUnavailable( 'wrong namespace', ( done ) => {
const badHvmlPath = './examples/legacy/vlog.hvml';
const badHvml = new HVML( badHvmlPath );
badHvml.ready
.then( () => badHvml.validate() )
.then( ( badValidationResult ) => {
expect( badValidationResult ).toBeUndefined();
done();
} )
.catch( ( error ) => {
expect( error ).toStrictEqual( [{
"error": "Element hvml has wrong namespace: expecting https://hypervideo.tech/hvml#",
"element": "hvml",
"expecting": "https://hypervideo.tech/hvml#",
"file": badHvmlPath,
// "got": null,
"line": "2",
"message": `${badHvmlPath}:2: element hvml: Relax-NG validity error : Element hvml has wrong namespace: expecting https://hypervideo.tech/hvml#`,
"type": "validity error",
}] );
done();
} );
} );
skipIfXmllintUnavailable( 'missing namespace', ( done ) => {
const badHvmlPath = './examples/bad/missing-namespace.hvml';
const badHvml = new HVML( badHvmlPath );
badHvml.ready
.then( () => badHvml.validate() )
.then( ( badValidationResult ) => {
expect( badValidationResult ).toBeUndefined();
done();
} )
.catch( ( error ) => {
expect( error ).toStrictEqual( [{
"error": "Expecting a namespace for element hvml",
"element": "hvml",
"expecting": "https://hypervideo.tech/hvml#",
"file": badHvmlPath,
"got": null,
"line": "2",
"message": `${badHvmlPath}:2: element hvml: Relax-NG validity error : Expecting a namespace for element hvml`,
"type": "validity error",
}] );
done();
} );
} );
skipIfXmllintUnavailable( 'unexpected text', ( done ) => {
const badHvmlPath = './examples/bad/unexpected-text-children.hvml';
const badHvml = new HVML( badHvmlPath );
badHvml.ready
.then( () => badHvml.validate() )
.then( ( badValidationResult ) => {
expect( badValidationResult ).toBeUndefined();
done();
} )
.catch( ( error ) => {
expect( error ).toStrictEqual( [{
"error": "Did not expect text in element hvml content",
"element": "hvml",
"file": badHvmlPath,
"got": "Text",
"line": "2",
"message": `${badHvmlPath}:2: element hvml: Relax-NG validity error : Did not expect text in element hvml content`,
"type": "validity error",
}] );
done();
} );
} );
skipIfXmllintUnavailable( 'invalid attribute', ( done ) => {
const badHvmlPath = './examples/bad/invalid-attribute.hvml';
const badHvml = new HVML( badHvmlPath );
badHvml.ready
.then( () => badHvml.validate() )
.then( ( badValidationResult ) => {
expect( badValidationResult ).toBeUndefined();
done();
} )
.catch( ( error ) => {
expect( error ).toStrictEqual( [{
"error": "Invalid attribute x for element hvml",
"element": "hvml",
"file": badHvmlPath,
"got": "x",
"line": "2",
"message": `${badHvmlPath}:2: element hvml: Relax-NG validity error : Invalid attribute x for element hvml`,
"type": "validity error",
}] );
done();
} );
} );
skipIfXmllintUnavailable( 'unexpected element', ( done ) => {
const badHvmlPath = './examples/bad/unexpected-element.hvml';
const badHvml = new HVML( badHvmlPath );
badHvml.ready
.then( () => badHvml.validate() )
.then( ( badValidationResult ) => {
// expect( badValidationResult ).toBeUndefined();
console.log( 'badValidationResult', badValidationResult );
done();
} )
.catch( ( error ) => {
expect( error ).toStrictEqual( [{
"error": "Did not expect element big-chungus there",
// "element": "hvml",
"file": badHvmlPath,
"got": "big-chungus",
"line": "3",
"message": `${badHvmlPath}:3: element big-chungus: Relax-NG validity error : Did not expect element big-chungus there`,
"type": "validity error",
}] );
done();
} );
} );
} );
skipIfLibxmljsUnavailable( 'alerts user when trying to validate and xmllint path is inaccessible', ( done ) => {
const hvmlPath = './examples/hvml.xml';
const hvml = new HVML( hvmlPath );
hvml.ready
.then( () => hvml.validate( 'wtf' ) )
.catch( ( validationErrors ) => {
expect( validationErrors.message ).toBe( 'Optional dependency xmllint is not installed, so HVML::validate can not be used' );
done();
} );
} );
} );
describe( 'MOM Manipulation', () => {
const { HVML, Video } = require( './hvml' );
it( 'appends children', () => {
const hvml = new HVML();
const channel = new Video( {
"id": "welcome-to-my-channel",
} );
const secondChannel = new Video( {
"id": "welcome-to-my-second-channel",
} );
hvml.appendChild( channel );
hvml.appendChild( secondChannel );
expect( hvml.children.length ).toBe( 2 );
expect( Object.keys( hvml.children ).length ).toBe( 4 );
} );
it( 'throws an error when appending children of unexpected types', () => {
const hvml = new HVML();
const channel = {
"id": "welcome-to-my-channel",
};
let thrownError;
try {
hvml.appendChild( channel );
} catch ( error ) {
thrownError = error;
}
expect( thrownError.constructor ).toBe( Validation.EnumError );
expect( thrownError.message ).toBe( 'The following values are invalid for HVML.appendChild::child: [object Object]' );
} );
test( 'toMom', ( done ) => {
const hvml = new HVML( './examples/hvml.jsonld' );
hvml.ready.then( () => {
const MOM = hvml.toMom();
// console.error( 'hvml.toMom()', hvml.toMom() );
expect( MOM ).toBeInstanceOf( HVML );
expect( MOM ).toMatchObject( {
// <hvml>
// property: value, × n
"children": [
{
// <video>
"language": "_",
"region": "_",
"id": "ep-23",
"title": "Overnight Dance Party at the Museum of Fine Arts Boston",
"description": {
"xhtml": "<p>Full Facebook Live stream: https://www.facebook.com/hugh.guiney/videos/10100195051457860/</p><p>#mfaNOW #mfaLateNites</p>",
},
"children": [
{
// <presentation>
},
{
// <showing>
},
],
},
],
} );
done();
} );
} );
} );
} );
// describe( 'HVML Schema', () => {
// it( 'validates correct syntax', () => {
//
// } );
//
// it( 'reports incorrect syntax', () => {
//
// } );
// } );