From 64b444752a91ef09c0538d9316c884710d6ee94b Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 5 Mar 2025 06:33:36 -0500 Subject: [PATCH] Update "Find Full Text" tests for Zotero.HTTP.download() Downloads are now included in request() call counts --- test/tests/attachmentsTest.js | 54 +++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index c56619196e..9968d98ad6 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -925,7 +925,8 @@ describe("Zotero.Attachments", function() { await item.saveTx(); var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 2); + // doi.org, publisher, download + assert.equal(requestStub.callCount, 3); assert.isTrue(requestStub.getCall(0).calledWith('GET', 'https://doi.org/' + doi)); assert.ok(attachment); var json = attachment.toJSON(); @@ -961,7 +962,8 @@ describe("Zotero.Attachments", function() { await item.saveTx(); var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 2); + // doi.org, publisher, download + assert.equal(requestStub.callCount, 3); assert.isTrue(requestStub.getCall(0).calledWith('GET', 'https://doi.org/' + doi)); assert.ok(attachment); var json = attachment.toJSON(); @@ -979,7 +981,8 @@ describe("Zotero.Attachments", function() { await item.saveTx(); var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 1); + // URL, download + assert.equal(requestStub.callCount, 2); assert.isTrue(requestStub.calledWith('GET', url)); assert.ok(attachment); var json = attachment.toJSON(); @@ -996,6 +999,7 @@ describe("Zotero.Attachments", function() { item.setField('url', url); await item.saveTx(); var attachment = await Zotero.Attachments.addAvailableFile(item); + // URL, redirect target URL assert.equal(requestStub.callCount, 2); var call = requestStub.getCall(0); assert.isTrue(call.calledWith('GET', url)); @@ -1017,7 +1021,7 @@ describe("Zotero.Attachments", function() { await item.saveTx(); var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 3); + assert.equal(requestStub.callCount, 4); var call1 = requestStub.getCall(0); assert.isTrue(call1.calledWith('GET', 'https://doi.org/' + doi)); var call2 = requestStub.getCall(1); @@ -1041,7 +1045,7 @@ describe("Zotero.Attachments", function() { await item.saveTx(); var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 4); + assert.equal(requestStub.callCount, 5); // Check the DOI (and get nothing) var call = requestStub.getCall(0); assert.isTrue(call.calledWith('GET', 'https://doi.org/' + doi)); @@ -1097,8 +1101,9 @@ describe("Zotero.Attachments", function() { var attachments = await Zotero.Attachments.addAvailableFiles([item1, item2]); - assert.equal(requestStub.callCount, 2); - assert.isAbove(requestStubCallTimes[1] - requestStubCallTimes[0], 998); + // 2 URLs and 2 downloads + assert.equal(requestStub.callCount, 4); + assert.isAbove(requestStubCallTimes[2] - requestStubCallTimes[0], 998); // Make sure both items have attachments assert.equal(item1.numAttachments(), 1); assert.equal(item2.numAttachments(), 1); @@ -1119,7 +1124,7 @@ describe("Zotero.Attachments", function() { item2.setField('url', url2); await item2.saveTx(); - // DOI URL resolves to 'website2' domain without PDF + // DOI URL resolves to 'website2' domain with PDF var url3 = doiPrefix + doi6; var item3 = createUnsavedDataObject('item', { itemType: 'journalArticle' }); item3.setField('title', 'Test'); @@ -1128,19 +1133,22 @@ describe("Zotero.Attachments", function() { var attachments = await Zotero.Attachments.addAvailableFiles([item1, item2, item3]); - assert.equal(requestStub.callCount, 6); + assert.equal(requestStub.callCount, 8); assert.equal(requestStub.getCall(0).args[1], doiPrefix + doi1); assert.equal(requestStub.getCall(1).args[1], pageURL1); - assert.equal(requestStub.getCall(2).args[1], doiPrefix + doi4); + assert.equal(requestStub.getCall(2).args[1], pdfURL); + + assert.equal(requestStub.getCall(3).args[1], doiPrefix + doi4); // Should skip ahead to the next DOI - assert.equal(requestStub.getCall(3).args[1], doiPrefix + doi6); + assert.equal(requestStub.getCall(4).args[1], doiPrefix + doi6); // which is on a new domain - assert.equal(requestStub.getCall(4).args[1], pageURL8); + assert.equal(requestStub.getCall(5).args[1], pageURL8); + assert.equal(requestStub.getCall(6).args[1], pdfURL); // and then return to make 'website' request for DOI 4 - assert.equal(requestStub.getCall(5).args[1], pageURL4); + assert.equal(requestStub.getCall(7).args[1], pageURL4); // 'website' requests should be a second apart - assert.isAbove(requestStubCallTimes[5] - requestStubCallTimes[1], 995); + assert.isAbove(requestStubCallTimes[7] - requestStubCallTimes[1], 995); assert.equal(item1.numAttachments(), 1); assert.equal(item2.numAttachments(), 0); @@ -1162,10 +1170,13 @@ describe("Zotero.Attachments", function() { var attachments = await Zotero.Attachments.addAvailableFiles([item1, item2]); - assert.equal(requestStub.callCount, 3); + // 429, URL9, download, URL3, download + assert.equal(requestStub.callCount, 5); assert.equal(requestStub.getCall(0).args[1], pageURL9); assert.equal(requestStub.getCall(1).args[1], pageURL9); - assert.equal(requestStub.getCall(2).args[1], pageURL3); + assert.equal(requestStub.getCall(2).args[1], pdfURL); + assert.equal(requestStub.getCall(3).args[1], pageURL3); + assert.equal(requestStub.getCall(4).args[1], pdfURL); assert.isAbove(requestStubCallTimes[1] - requestStubCallTimes[0], 1999); // Make sure both items have attachments assert.equal(item1.numAttachments(), 1); @@ -1180,9 +1191,10 @@ describe("Zotero.Attachments", function() { await item.saveTx(); var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 2); + assert.equal(requestStub.callCount, 3); assert.equal(requestStub.getCall(0).args[1], pageURL10) assert.equal(requestStub.getCall(1).args[1], pageURL1) + assert.equal(requestStub.getCall(2).args[1], pdfURL) assert.ok(attachment); var json = attachment.toJSON(); assert.equal(json.url, pdfURL); @@ -1228,7 +1240,7 @@ describe("Zotero.Attachments", function() { var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 4); + assert.equal(requestStub.callCount, 5); var call = requestStub.getCall(0); assert.isTrue(call.calledWith('GET', 'https://doi.org/' + doi)); var call = requestStub.getCall(1); @@ -1266,7 +1278,7 @@ describe("Zotero.Attachments", function() { var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 4); + assert.equal(requestStub.callCount, 5); var call = requestStub.getCall(0); assert.isTrue(call.calledWith('GET', 'https://doi.org/' + doi)); var call = requestStub.getCall(1); @@ -1302,7 +1314,7 @@ describe("Zotero.Attachments", function() { var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 4); + assert.equal(requestStub.callCount, 5); var call = requestStub.getCall(0); assert.isTrue(call.calledWith('GET', 'https://doi.org/' + doi)); call = requestStub.getCall(1); @@ -1342,7 +1354,7 @@ describe("Zotero.Attachments", function() { var attachment = await Zotero.Attachments.addAvailableFile(item); - assert.equal(requestStub.callCount, 5); + assert.equal(requestStub.callCount, 6); var call = requestStub.getCall(0); assert.isTrue(call.calledWith('GET', 'https://doi.org/' + doi)); call = requestStub.getCall(1);