@@ -289,6 +289,132 @@ describe('HTMLMetadata', () => {
289289 } ) ;
290290 } ) ;
291291
292+ describe ( '#_getVersion' , ( ) => {
293+ function setVersionMeta ( id , publicUrl , canonicalHref ) {
294+ let html = '' ;
295+ if ( id ) {
296+ html += `<meta name="citation_id" content="${ id } ">` ;
297+ }
298+ if ( publicUrl ) {
299+ html += `<meta name="citation_public_url" content="${ publicUrl } ">` ;
300+ }
301+ if ( canonicalHref ) {
302+ html += `<link rel="canonical" href="${ canonicalHref } ">` ;
303+ }
304+ tempDocumentHead . innerHTML = html ;
305+ }
306+
307+ it ( 'returns version when all three sources agree' , ( ) => {
308+ setVersionMeta (
309+ 'doc-v3' ,
310+ 'https://example.com/doc-v3' ,
311+ 'https://example.com/doc-v3' ,
312+ ) ;
313+ const metadata = testDocument . getDocumentMetadata ( ) ;
314+ assert . equal ( metadata . version , 3 ) ;
315+ } ) ;
316+
317+ it ( 'returns null when sources have different versions' , ( ) => {
318+ setVersionMeta (
319+ 'doc-v3' ,
320+ 'https://example.com/doc-v5' ,
321+ 'https://example.com/doc-v3' ,
322+ ) ;
323+ const metadata = testDocument . getDocumentMetadata ( ) ;
324+ assert . isUndefined ( metadata . version ) ;
325+ } ) ;
326+
327+ it ( 'returns null when one source is missing a version' , ( ) => {
328+ setVersionMeta (
329+ 'doc-v3' ,
330+ 'https://example.com/doc' ,
331+ 'https://example.com/doc-v3' ,
332+ ) ;
333+ const metadata = testDocument . getDocumentMetadata ( ) ;
334+ assert . isUndefined ( metadata . version ) ;
335+ } ) ;
336+
337+ it ( 'returns null when no sources have a version' , ( ) => {
338+ setVersionMeta (
339+ 'doc-abc' ,
340+ 'https://example.com/doc' ,
341+ 'https://example.com/doc' ,
342+ ) ;
343+ const metadata = testDocument . getDocumentMetadata ( ) ;
344+ assert . isUndefined ( metadata . version ) ;
345+ } ) ;
346+
347+ it ( 'returns null when canonical link is missing' , ( ) => {
348+ tempDocumentHead . innerHTML = `
349+ <meta name="citation_id" content="doc-v3">
350+ <meta name="citation_public_url" content="https://example.com/doc-v3">
351+ ` ;
352+ const metadata = testDocument . getDocumentMetadata ( ) ;
353+ assert . isUndefined ( metadata . version ) ;
354+ } ) ;
355+
356+ it ( 'returns null when citation_id is missing' , ( ) => {
357+ tempDocumentHead . innerHTML = `
358+ <meta name="citation_public_url" content="https://example.com/doc-v3">
359+ <link rel="canonical" href="https://example.com/doc-v3">
360+ ` ;
361+ const metadata = testDocument . getDocumentMetadata ( ) ;
362+ assert . isUndefined ( metadata . version ) ;
363+ } ) ;
364+
365+ it ( 'accepts versions from v1 to v25' , ( ) => {
366+ setVersionMeta (
367+ 'doc-v1' ,
368+ 'https://example.com/doc-v1' ,
369+ 'https://example.com/doc-v1' ,
370+ ) ;
371+ assert . equal ( testDocument . getDocumentMetadata ( ) . version , 1 ) ;
372+
373+ setVersionMeta (
374+ 'doc-v25' ,
375+ 'https://example.com/doc-v25' ,
376+ 'https://example.com/doc-v25' ,
377+ ) ;
378+ assert . equal ( testDocument . getDocumentMetadata ( ) . version , 25 ) ;
379+ } ) ;
380+
381+ it ( 'rejects versions above 25' , ( ) => {
382+ setVersionMeta (
383+ 'doc-v26' ,
384+ 'https://example.com/doc-v26' ,
385+ 'https://example.com/doc-v26' ,
386+ ) ;
387+ assert . isUndefined ( testDocument . getDocumentMetadata ( ) . version ) ;
388+ } ) ;
389+
390+ it ( 'rejects version 0' , ( ) => {
391+ setVersionMeta (
392+ 'doc-v0' ,
393+ 'https://example.com/doc-v0' ,
394+ 'https://example.com/doc-v0' ,
395+ ) ;
396+ assert . isUndefined ( testDocument . getDocumentMetadata ( ) . version ) ;
397+ } ) ;
398+
399+ it ( 'is case insensitive for version suffix' , ( ) => {
400+ setVersionMeta (
401+ 'doc-V3' ,
402+ 'https://example.com/doc-V3' ,
403+ 'https://example.com/doc-V3' ,
404+ ) ;
405+ assert . equal ( testDocument . getDocumentMetadata ( ) . version , 3 ) ;
406+ } ) ;
407+
408+ it ( 'does not match version embedded in a word' , ( ) => {
409+ setVersionMeta (
410+ 'docrev3' ,
411+ 'https://example.com/docrev3' ,
412+ 'https://example.com/docrev3' ,
413+ ) ;
414+ assert . isUndefined ( testDocument . getDocumentMetadata ( ) . version ) ;
415+ } ) ;
416+ } ) ;
417+
292418 describe ( '#_absoluteUrl' , ( ) => {
293419 it ( 'should add the protocol when the url starts with two slashes' , ( ) => {
294420 const result = testDocument . _absoluteUrl ( '//example.com/' ) ;
0 commit comments