Skip to content

Commit 8b1380c

Browse files
Revert "Revert "Make changes and add tests""
This reverts commit a776616.
1 parent a81b8b1 commit 8b1380c

3 files changed

Lines changed: 210 additions & 3 deletions

File tree

src/annotator/integrations/test/html-metadata-test.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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/');

src/sidebar/store/modules/frames.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,16 @@ function searchUrisForFrame(frame: Frame): string[] {
188188
});
189189
}
190190

191-
// If the document has a version, append :v0:v{version} to each URI
191+
// If the document has a version, append a version suffix to each URI.
192+
// v0 is the base version and should not be appended.
193+
// Only v1 includes v0 annotations (`:v0:v1`), other versions only search their own (`:v{version}`).
192194
const version = frame.metadata?.version;
193-
if (version) {
194-
uris = uris.map(uri => `${uri}:v0:v${version}`);
195+
if (version && version > 0) {
196+
if (version === 1) {
197+
uris = uris.map(uri => `${uri}:v0:v1`);
198+
} else {
199+
uris = uris.map(uri => `${uri}:v${version}`);
200+
}
195201
}
196202

197203
return uris;

src/sidebar/store/modules/test/frames-test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,81 @@ describe('sidebar/store/modules/frames', () => {
213213
],
214214
searchUris: ['https://publisher.org/article.html', 'doi:10.1.1/1234'],
215215
},
216+
{
217+
when: 'version 1 includes v0 annotations',
218+
frames: [
219+
{
220+
uri: 'https://publisher.org/article.html',
221+
metadata: {
222+
version: 1,
223+
link: [],
224+
},
225+
},
226+
],
227+
searchUris: ['https://publisher.org/article.html:v0:v1'],
228+
},
229+
{
230+
when: 'version > 1 only searches its own version',
231+
frames: [
232+
{
233+
uri: 'https://publisher.org/article.html',
234+
metadata: {
235+
version: 3,
236+
link: [],
237+
},
238+
},
239+
],
240+
searchUris: ['https://publisher.org/article.html:v3'],
241+
},
242+
{
243+
when: 'a PDF frame with version > 1 only searches its own version',
244+
frames: [
245+
{
246+
uri: 'https://publisher.org/article.pdf',
247+
metadata: {
248+
documentFingerprint: '1234',
249+
version: 5,
250+
link: [
251+
{
252+
href: 'urn:x-pdf:1234',
253+
},
254+
{
255+
href: 'https://publisher.org/article.pdf?from_meta_link=1',
256+
},
257+
],
258+
},
259+
},
260+
],
261+
searchUris: [
262+
'urn:x-pdf:1234:v5',
263+
'https://publisher.org/article.pdf?from_meta_link=1:v5',
264+
],
265+
},
266+
{
267+
when: 'the document metadata has no version',
268+
frames: [
269+
{
270+
uri: 'https://publisher.org/article.html',
271+
metadata: {
272+
link: [],
273+
},
274+
},
275+
],
276+
searchUris: ['https://publisher.org/article.html'],
277+
},
278+
{
279+
when: 'the document metadata has version 0 (base version)',
280+
frames: [
281+
{
282+
uri: 'https://publisher.org/article.html',
283+
metadata: {
284+
version: 0,
285+
link: [],
286+
},
287+
},
288+
],
289+
searchUris: ['https://publisher.org/article.html'],
290+
},
216291
].forEach(testCase => {
217292
it(testCase.when, () => {
218293
testCase.frames.forEach(frame => {

0 commit comments

Comments
 (0)