UNPKG

opds-web-client

Version:
251 lines (250 loc) 13.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var chai_1 = require("chai"); var opds_feed_parser_1 = require("opds-feed-parser"); var factory = require("./OPDSFactory"); var OPDSDataAdapter_1 = require("../OPDSDataAdapter"); var sanitizeHtml = require("dompurify").sanitize; describe("OPDSDataAdapter", function () { it("extracts book info", function () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; var largeImageLink = factory.artworkLink({ href: "https://dlotdqc6pnwqb.cloudfront.net/3M/crrmnr9/cover.jpg", rel: "http://opds-spec.org/image" }); var thumbImageLink = factory.artworkLink({ href: "http://example.com/testthumb.jpg", rel: "http://opds-spec.org/image/thumbnail" }); var openAccessLink = factory.acquisitionLink({ href: "http://example.com/open.epub", rel: opds_feed_parser_1.OPDSAcquisitionLink.OPEN_ACCESS_REL }); var borrowLink = factory.acquisitionLink({ href: "http://example.com/borrow", rel: opds_feed_parser_1.OPDSAcquisitionLink.BORROW_REL, availability: { availability: "unavailable" }, holds: { total: 20, position: 5 }, copies: { total: 2, available: 0 } }); var fulfillmentLink = factory.acquisitionLink({ href: "http://example.com/fulfill", rel: opds_feed_parser_1.OPDSAcquisitionLink.GENERIC_REL, type: "application/atom+xml;type=entry;profile=opds-catalog", indirectAcquisitions: [ { type: "text/html;profile=http://librarysimplified.org/terms/profiles/streaming-media" } ] }); var collectionLink = factory.collectionLink({ href: "collection%20url", rel: opds_feed_parser_1.OPDSCollectionLink.REL, title: "collection title" }); var entry = factory.entry({ id: "urn:librarysimplified.org/terms/id/3M%20ID/crrmnr9", title: "The Mayan Secrets", authors: [ factory.contributor({ name: "Clive Cussler" }), factory.contributor({ name: "Thomas Perry" }) ], contributors: [factory.contributor({ name: "contributor" })], subtitle: "A Sam and Remi Fargo Adventure", summary: factory.summary({ content: "&lt;b&gt;Sam and Remi Fargo race for treasure&#8212;and survival&#8212;in this lightning-paced new adventure from #1&lt;i&gt; New York Times&lt;/i&gt; bestselling author Clive Cussler.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Husband-and-wife team Sam and Remi Fargo are in Mexico when they come upon a remarkable discovery&#8212;the mummified remainsof a man clutching an ancient sealed pot. Within the pot is a Mayan book larger than any known before.&lt;br /&gt;&lt;br /&gt;The book contains astonishing information about the Mayans, their cities, and about mankind itself. The secrets are so powerful that some people would do anything to possess them&#8212;as the Fargos are about to find out. Many men and women are going to die for that book.<script>alert('danger!');</script>" }), categories: [ factory.category({ label: "label" }), factory.category({ term: "no label" }), factory.category({ label: "label 2" }) ], links: [ largeImageLink, thumbImageLink, openAccessLink, borrowLink, fulfillmentLink, collectionLink ], issued: "2014-06-08", publisher: "Fake Publisher", series: { name: "Fake Series", position: 2 }, language: "en" }); var acquisitionFeed = factory.acquisitionFeed({ id: "some id", entries: [entry], unparsed: "unparsed data" }); var collection = (0, OPDSDataAdapter_1.feedToCollection)(acquisitionFeed, "http://test-url.com"); (0, chai_1.expect)(collection.books.length).to.equal(0); (0, chai_1.expect)(collection.lanes.length).to.equal(1); (0, chai_1.expect)(collection.lanes[0].url).to.equal(collectionLink.href); (0, chai_1.expect)(collection.raw).to.equal("unparsed data"); var book = collection.lanes[0].books[0]; (0, chai_1.expect)(book.id).to.equal(entry.id); (0, chai_1.expect)(book.title).to.equal(entry.title); (0, chai_1.expect)((_a = book.authors) === null || _a === void 0 ? void 0 : _a.length).to.equal(2); (0, chai_1.expect)((_b = book.authors) === null || _b === void 0 ? void 0 : _b[0]).to.equal(entry.authors[0].name); (0, chai_1.expect)((_c = book.authors) === null || _c === void 0 ? void 0 : _c[1]).to.equal(entry.authors[1].name); (0, chai_1.expect)((_d = book.contributors) === null || _d === void 0 ? void 0 : _d.length).to.equal(1); (0, chai_1.expect)((_e = book.contributors) === null || _e === void 0 ? void 0 : _e[0]).to.equal(entry.contributors[0].name); (0, chai_1.expect)((_f = book.series) === null || _f === void 0 ? void 0 : _f.name).to.equal(entry.series.name); (0, chai_1.expect)((_g = book.series) === null || _g === void 0 ? void 0 : _g.position).to.equal(entry.series.position); (0, chai_1.expect)(book.subtitle).to.equal(entry.subtitle); (0, chai_1.expect)(book.summary).to.equal(sanitizeHtml(entry.summary.content)); (0, chai_1.expect)(book.summary).to.contain("Many men and women are going to die for that book."); (0, chai_1.expect)(book.summary).not.to.contain("script"); (0, chai_1.expect)(book.summary).not.to.contain("danger"); (0, chai_1.expect)((_h = book.categories) === null || _h === void 0 ? void 0 : _h.length).to.equal(2); (0, chai_1.expect)(book.categories).to.contain("label"); (0, chai_1.expect)(book.categories).to.contain("label 2"); (0, chai_1.expect)(book.imageUrl).to.equal(thumbImageLink.href); (0, chai_1.expect)(book.publisher).to.equal("Fake Publisher"); (0, chai_1.expect)(book.published).to.equal("June 8, 2014"); (0, chai_1.expect)(book.language).to.equal("en"); (0, chai_1.expect)((_j = book.openAccessLinks) === null || _j === void 0 ? void 0 : _j[0].url).to.equal(openAccessLink.href); (0, chai_1.expect)(book.borrowUrl).to.equal(borrowLink.href); (0, chai_1.expect)((_k = book.allBorrowLinks) === null || _k === void 0 ? void 0 : _k[0].url).to.equal(borrowLink.href); (0, chai_1.expect)((_l = book.fulfillmentLinks) === null || _l === void 0 ? void 0 : _l[0].url).to.equal(fulfillmentLink.href); (0, chai_1.expect)((_m = book.fulfillmentLinks) === null || _m === void 0 ? void 0 : _m[0].type).to.equal(fulfillmentLink.type); (0, chai_1.expect)((_o = book.fulfillmentLinks) === null || _o === void 0 ? void 0 : _o[0].indirectType).to.equal(fulfillmentLink.indirectAcquisitions[0].type); (0, chai_1.expect)(book.availability).to.equal(borrowLink.availability); (0, chai_1.expect)(book.holds).to.equal(borrowLink.holds); (0, chai_1.expect)(book.copies).to.equal(borrowLink.copies); }); it("extracts navigation link info", function () { var navigationLink = factory.link({ href: "href" }); var linkEntry = factory.entry({ id: "feed.xml", title: "Feed", links: [navigationLink] }); var navigationFeed = factory.navigationFeed({ id: "some id", entries: [linkEntry] }); var collection = (0, OPDSDataAdapter_1.feedToCollection)(navigationFeed, "http://test-url.com"); (0, chai_1.expect)(collection.navigationLinks.length).to.equal(1); var link = collection.navigationLinks[0]; (0, chai_1.expect)(link.id).to.equal(linkEntry.id); (0, chai_1.expect)(link.text).to.equal(linkEntry.title); (0, chai_1.expect)(link.url).to.equal(navigationLink.href); }); it("extracts facet groups", function () { var _a, _b, _c; var facetLinks = [ factory.facetLink({ href: "href1", title: "title 1", facetGroup: "group A", activeFacet: true }), factory.facetLink({ href: "href2", title: "title 2", facetGroup: "group B", activeFacet: false }), factory.facetLink({ href: "href3", title: "title 3", facetGroup: "group A" }) ]; var acquisitionFeed = factory.acquisitionFeed({ id: "some id", entries: [], links: facetLinks }); var collection = (0, OPDSDataAdapter_1.feedToCollection)(acquisitionFeed, "http://test-url.com"); (0, chai_1.expect)((_a = collection.facetGroups) === null || _a === void 0 ? void 0 : _a.length).to.equal(2); var groupA = (_b = collection.facetGroups) === null || _b === void 0 ? void 0 : _b[0]; (0, chai_1.expect)(groupA === null || groupA === void 0 ? void 0 : groupA.label).to.equal("group A"); (0, chai_1.expect)(groupA === null || groupA === void 0 ? void 0 : groupA.facets.length).to.equal(2); var groupB = (_c = collection.facetGroups) === null || _c === void 0 ? void 0 : _c[1]; (0, chai_1.expect)(groupB === null || groupB === void 0 ? void 0 : groupB.label).to.equal("group B"); (0, chai_1.expect)(groupB === null || groupB === void 0 ? void 0 : groupB.facets.length).to.equal(1); var facet1 = groupA === null || groupA === void 0 ? void 0 : groupA.facets[0]; (0, chai_1.expect)(facet1 === null || facet1 === void 0 ? void 0 : facet1.label).to.equal("title 1"); (0, chai_1.expect)(facet1 === null || facet1 === void 0 ? void 0 : facet1.active).to.be.ok; (0, chai_1.expect)(facet1 === null || facet1 === void 0 ? void 0 : facet1.href).to.equal("href1"); var facet2 = groupB === null || groupB === void 0 ? void 0 : groupB.facets[0]; (0, chai_1.expect)(facet2 === null || facet2 === void 0 ? void 0 : facet2.label).to.equal("title 2"); (0, chai_1.expect)(facet2 === null || facet2 === void 0 ? void 0 : facet2.active).not.to.be.ok; (0, chai_1.expect)(facet2 === null || facet2 === void 0 ? void 0 : facet2.href).to.equal("href2"); var facet3 = groupA === null || groupA === void 0 ? void 0 : groupA.facets[1]; (0, chai_1.expect)(facet3 === null || facet3 === void 0 ? void 0 : facet3.label).to.equal("title 3"); (0, chai_1.expect)(facet3 === null || facet3 === void 0 ? void 0 : facet3.active).not.to.be.ok; (0, chai_1.expect)(facet3 === null || facet3 === void 0 ? void 0 : facet3.href).to.equal("href3"); }); it("extracts search link", function () { var _a; var searchLink = factory.searchLink({ href: "search%20url" }); var navigationFeed = factory.navigationFeed({ id: "some id", entries: [], links: [searchLink] }); var collection = (0, OPDSDataAdapter_1.feedToCollection)(navigationFeed, "http://test-url.com"); (0, chai_1.expect)(collection.search).to.be.ok; (0, chai_1.expect)((_a = collection.search) === null || _a === void 0 ? void 0 : _a.url).to.equal(searchLink.href); }); it("extracts next page url", function () { var nextLink = factory.link({ href: "href", rel: "next" }); var acquisitionFeed = factory.acquisitionFeed({ id: "some id", entries: [], links: [nextLink] }); var collection = (0, OPDSDataAdapter_1.feedToCollection)(acquisitionFeed, "http://test-url.com"); (0, chai_1.expect)(collection.nextPageUrl).to.be.ok; (0, chai_1.expect)(collection.nextPageUrl).to.equal("href"); }); it("extracts shelf url", function () { var shelfLink = factory.shelfLink({ href: "loans", rel: opds_feed_parser_1.OPDSShelfLink.REL }); var acquisitionFeed = factory.acquisitionFeed({ id: "some id", entries: [], links: [shelfLink] }); var collection = (0, OPDSDataAdapter_1.feedToCollection)(acquisitionFeed, "http://test-url.com"); (0, chai_1.expect)(collection.shelfUrl).to.equal(shelfLink.href); }); it("extracts top-level links", function () { var _a, _b, _c; var aboutLink = factory.link({ href: "about", rel: "about" }); var termsLink = factory.link({ href: "terms", rel: "terms-of-service" }); var acquisitionFeed = factory.acquisitionFeed({ id: "some id", entries: [], links: [aboutLink, termsLink] }); var collection = (0, OPDSDataAdapter_1.feedToCollection)(acquisitionFeed, "http://test-url.com"); (0, chai_1.expect)((_a = collection.links) === null || _a === void 0 ? void 0 : _a.length).to.equal(2); var urls = (_b = collection.links) === null || _b === void 0 ? void 0 : _b.map(function (link) { return link.url; }).sort(); var types = (_c = collection.links) === null || _c === void 0 ? void 0 : _c.map(function (link) { return link.type; }).sort(); (0, chai_1.expect)(urls).to.deep.equal(["about", "terms"]); (0, chai_1.expect)(types).to.deep.equal(["about", "terms-of-service"]); }); });