From a11b5087a3cee906a18f4cd61fceaaeb1716341e Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Wed, 11 Sep 2024 01:47:16 +0530 Subject: [PATCH] page: fix backlinks() and transclusions() to work for missing and orphaned pages Fixes #76 Fixes #77 --- src/page.ts | 16 ++++------------ tests/page.test.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/page.ts b/src/page.ts index dd1517b..6f66e9f 100644 --- a/src/page.ts +++ b/src/page.ts @@ -265,12 +265,8 @@ export default function (bot: Mwn): MwnPageStatic { lhlimit: 'max', }) .then((jsons) => { - let pages = jsons.reduce((pages, json) => pages.concat(json.query.pages), []); - let page = pages[0]; - if (page.missing) { - return Promise.reject(new MwnError.MissingPage()); - } - return page.linkshere.map((pg: ApiPage) => pg.title); + let pages = jsons.reduce((pages, json) => pages.concat(json.query.pages), [] as ApiPage[]); + return (pages[0].linkshere || []).map((pg) => pg.title); }); } @@ -285,12 +281,8 @@ export default function (bot: Mwn): MwnPageStatic { tilimit: 'max', }) .then((jsons) => { - let pages = jsons.reduce((pages, json) => pages.concat(json.query.pages), []); - let page = pages[0]; - if (page.missing) { - return Promise.reject(new MwnError.MissingPage()); - } - return page.transcludedin.map((pg: ApiPage) => pg.title); + let pages = jsons.reduce((pages, json) => pages.concat(json.query.pages), [] as ApiPage[]); + return (pages[0].transcludedin || []).map((pg) => pg.title); }); } diff --git a/tests/page.test.js b/tests/page.test.js index b1c9865..98a4ce8 100644 --- a/tests/page.test.js +++ b/tests/page.test.js @@ -101,6 +101,13 @@ describe('Page', async function () { }); }); + it('backlinks for non-existing page', function () { + return new bot.Page('29wdijopsk239esijd123').backlinks().then((backlinks) => { + expect(backlinks).to.be.instanceOf(Array); + expect(backlinks.length).to.equal(0); + }); + }); + it('transclusions', function () { return page.transclusions().then((transclusions) => { expect(transclusions).to.be.instanceOf(Array); @@ -108,6 +115,13 @@ describe('Page', async function () { }); }); + it('transclusions for non-existing page', function () { + return new bot.Page('29wdijopsk239esijd123').transclusions().then((transclusions) => { + expect(transclusions).to.be.instanceOf(Array); + expect(transclusions.length).to.equal(0); + }); + }); + it('history', function () { return page.history().then((history) => { expect(history).to.be.instanceOf(Array);