const assert = require('assert'); const fs = require('fs'); const vm = require('vm'); const bootstrapContext = {}; vm.runInNewContext(fs.readFileSync('plugin/bootstrap.js', 'utf8'), bootstrapContext); const plugin = bootstrapContext.KindleZoteroImporter; const maintainImportedAnnotations = plugin.maintainImportedAnnotations; assert.equal( plugin.mergeComments('[r] this is a red note\n\nthis is a red note', ''), 'this is a red note' ); assert.equal(plugin.mergeComments('[o]', ''), ''); assert.equal(plugin.mergeComments('[citation] keep this', ''), '[citation] keep this'); assert.equal( plugin.commentForExistingAnnotation( { annotationComment: 'old note', annotationTags: [{ name: 'kindle-import' }] }, 'replacement note', 'old note' ), 'replacement note' ); assert.equal( plugin.commentForExistingAnnotation( { annotationComment: 'old note\n\nmanual addition', annotationTags: [{ name: 'kindle-import' }] }, 'replacement note', 'old note' ), 'manual addition\n\nreplacement note' ); assert.equal( plugin.commentForExistingAnnotation( { annotationComment: 'old first\n\nold second\n\nmanual addition', annotationTags: [{ name: 'kindle-import' }] }, 'new note', 'old first\n\nold second' ), 'manual addition\n\nnew note' ); assert.equal( plugin.commentForExistingAnnotation( { annotationComment: 'manual note', annotationTags: [] }, 'imported note' ), 'manual note\n\nimported note' ); const base = { annotationText: 'forged its might, should be enough to discredit', annotationPosition: JSON.stringify({ type: 'FragmentSelector', value: 'epubcfi(/6/12!/4/2/8,/1:1142,/1:1255)', }), }; const extended = { annotationText: 'forged its might, should be enough to discredit, even in the eyes of philosophers', annotationPosition: JSON.stringify({ type: 'FragmentSelector', value: 'epubcfi(/6/12!/4/2/8,/1:1142,/1:1300)', }), }; assert.equal(plugin.areImportedDuplicates(base, extended), true); assert.equal( plugin.areImportedDuplicates(base, { ...base, annotationPosition: JSON.stringify({ type: 'FragmentSelector', value: 'epubcfi(/6/12!/4/2/8,/1:2000,/1:2050)', }), }), false ); assert.equal( plugin.areImportedDuplicates(base, { ...base, annotationText: 'OCR-corrected text at the identical annotation position', }), true ); assert.equal( plugin.areImportedDuplicates( { annotationText: 'A multiline PDF selection long enough to compare', annotationPosition: JSON.stringify({ pageIndex: 2, rects: [[10, 20, 100, 30]] }), }, { annotationText: 'A multiline PDF selection long enough to compare with an extended ending', annotationPosition: JSON.stringify({ pageIndex: 2, rects: [[10, 20, 100, 30], [10, 40, 120, 50]] }), } ), true ); const managerHtml = fs.readFileSync('plugin/manager.html', 'utf8'); const scripts = [...managerHtml.matchAll(/