Node.js based forum software built for the modern web
test: fix test failures
Julian Lam410845309957
diff --git a/src/controllers/activitypub/index.js b/src/controllers/activitypub/index.jsindex 9fea2cf847..b101692b50 100644--- a/src/controllers/activitypub/index.js+++ b/src/controllers/activitypub/index.js@@ -143,31 +143,43 @@ Controller.getOutbox = async (req, res) => {const last = paginate && !after && !before && `${nconf.get('url')}/uid/${uid}/outbox?before=0`;let activities;- if (!paginate || after || before) {- const limit = after ? parseInt(after, 10) - 1 : parseInt(before, 10) + 1;- const method = after ? 'getSortedSetRevRangeByScoreWithScores' : 'getSortedSetRangeByScoreWithScores';-- const [post, upvote, downvote, share] = await Promise.all([- db[method](`uid:${uid}:posts`, 0, 20, limit, `${after ? '-' : '+'}inf`),- db[method](`uid:${uid}:upvote`, 0, 20, limit, `${after ? '-' : '+'}inf`),- db[method](`uid:${uid}:downvote`, 0, 20, limit, `${after ? '-' : '+'}inf`),- db[method](`uid:${uid}:shares`, 0, 20, limit, `${after ? '-' : '+'}inf`),- ]);+ if (!paginate || after || before || !activities) {+ let post, upvote, downvote, share;++ if (!paginate) {+ // Simple range query for first page (no pagination params)+ [post, upvote, downvote, share] = await Promise.all([+ db.getSortedSetRangeWithScores(`uid:${uid}:posts`, 0, 19),+ db.getSortedSetRangeWithScores(`uid:${uid}:upvote`, 0, 19),+ db.getSortedSetRangeWithScores(`uid:${uid}:downvote`, 0, 19),+ db.getSortedSetRangeWithScores(`uid:${uid}:shares`, 0, 19),+ ]);+ } else {+ const limit = after ? parseInt(after, 10) - 1 : parseInt(before, 10) + 1;+ const method = after ? 'getSortedSetRevRangeByScoreWithScores' : 'getSortedSetRangeByScoreWithScores';++ [post, upvote, downvote, share] = await Promise.all([+ db[method](`uid:${uid}:posts`, 0, 20, limit, `${after ? '-' : '+'}inf`),+ db[method](`uid:${uid}:upvote`, 0, 20, limit, `${after ? '-' : '+'}inf`),+ db[method](`uid:${uid}:downvote`, 0, 20, limit, `${after ? '-' : '+'}inf`),+ db[method](`uid:${uid}:shares`, 0, 20, limit, `${after ? '-' : '+'}inf`),+ ]);+ }activities = [post.map(post => ({ ...post, type: 'post' })),upvote.map(upvote => ({ ...upvote, type: 'upvote' })),downvote.map(downvote => ({ ...downvote, type: 'downvote' })),share.map(share => ({ ...share, type: 'share' })),].flat().sort((a, b) => b.score - a.score);- if (after) {- activities = activities.slice(0, 20);- } else {+ if (!paginate || before) {activities = activities.slice(-20);+ } else {+ activities = activities.slice(0, 20);}if (activities.length) {prev = `${nconf.get('url')}/uid/${uid}/outbox?before=${activities[0].score}`;- next = `${nconf.get('url')}/uid/${uid}/outbox?after=${activities[19].score}`;+ next = `${nconf.get('url')}/uid/${uid}/outbox?after=${activities[activities.length - 1].score}`;let postsData = activities.filter((({ type }) => type === 'post'));postsData = await posts.getPostSummaryByPids(postsData.map(({ value }) => value), 0, { stripTags: false });diff --git a/src/controllers/helpers.js b/src/controllers/helpers.jsindex 4818e34999..f28fa4a40c 100644--- a/src/controllers/helpers.js+++ b/src/controllers/helpers.js@@ -485,6 +485,7 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {return res.sendStatus(statusCode);}+ console.log(statusCode, payload);if (String(statusCode).startsWith('2')) {if (res.req.loggedIn) {res.set('cache-control', 'private');@@ -538,7 +539,6 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {const returnPayload = await helpers.generateError(statusCode, message, res);returnPayload.response = response;-if (process.env.NODE_ENV === 'development') {const stack = payload instanceof Error ? payload.stack : new Error(String(payload)).stack;returnPayload.stack = stack;@@ -576,8 +576,8 @@ async function generateBannedResponse(res) {helpers.generateError = async (statusCode, message, res) => {async function translateMessage(message) {const { req } = res;- const settings = req.query.lang ? null : await user.getSettings(req.uid);- const language = String(req.query.lang || settings.userLang || meta.config.defaultLang);+ const settings = req?.query?.lang ? null : await user.getSettings(req.uid);+ const language = String(req?.query?.lang || settings.userLang || meta.config.defaultLang);return await translator.translate(message, language);}if (message && message.startsWith('[[')) {diff --git a/src/middleware/assert.js b/src/middleware/assert.jsindex f7cf7bdba0..2d920dfd29 100644--- a/src/middleware/assert.js+++ b/src/middleware/assert.js@@ -36,7 +36,7 @@ Assert.user = helpers.try(async (req, res, next) => {return next();}- controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-user]]'));+ await controllerHelpers.formatApiResponse(404, res, new Error('[[error:no-user]]'));});Assert.group = helpers.try(async (req, res, next) => {diff --git a/test/activitypub/notes.js b/test/activitypub/notes.jsindex 1c50153e63..6e89bd6ad9 100644--- a/test/activitypub/notes.js+++ b/test/activitypub/notes.js@@ -544,7 +544,7 @@ describe('Notes', () => {mainPid = postData.pid;});- beforeEach(() => {+ beforeEach(function () {mockBlockedDomain('blocked.example.org', 3);this._baseUrl = helpers.mocks._baseUrl;helpers.mocks._baseUrl = 'https://blocked.example.org';@@ -598,7 +598,7 @@ describe('Notes', () => {});describe('null case', () => {- beforeEach(() => {+ beforeEach(function () {this._baseUrl = helpers.mocks._baseUrl;helpers.mocks._baseUrl = 'https://allowed.example.org';});@@ -623,13 +623,14 @@ describe('Notes', () => {});describe('Severity 1 and 2', () => {- beforeEach(() => {+ beforeEach(function () {this._baseUrl = helpers.mocks._baseUrl;helpers.mocks._baseUrl = 'https://blocked.example.org';});afterEach(function () {helpers.mocks._baseUrl = this._baseUrl;+ console.log('setting back to ', this._baseUrl);});it('should NOT queue posts with severity 1 (suspend)', async () => {diff --git a/test/activitypub/out.js b/test/activitypub/out.jsindex 48da334216..f18f351eba 100644--- a/test/activitypub/out.js+++ b/test/activitypub/out.js@@ -56,6 +56,7 @@ describe('Outbound activities module', () => {describe('.topic() (remote topic; by cid)', () => {before(async function () {const { id: pid, note } = helpers.mocks.note();+ console.log(pid, note);const { cid } = await categories.create({ name: utils.generateUUID() });await activitypub.notes.assert(0, pid, { skipChecks: 1, cid });diff --git a/test/activitypub/outbox.js b/test/activitypub/outbox.jsindex 1682f411fe..474bf0f6ac 100644--- a/test/activitypub/outbox.js+++ b/test/activitypub/outbox.js@@ -120,11 +120,19 @@ describe('Outbox', () => {});it('should include upvote and downvote activities when present', async () => {- // Upvote the post- await posts.upvote(pid, uid);- // Downvote another post (create a second post to downvote)+ // Create another user's post to upvote (can't upvote own post)+ const otherUid = await user.create({ username: utils.generateUUID().slice(0, 8) });+ const { postData: otherPost } = await topics.post({+ uid: otherUid,+ cid,+ title: utils.generateUUID(),+ content: utils.generateUUID(),+ });+ // Upvote the other user's post+ await posts.upvote(otherPost.pid, uid);+ // Downvote another postconst { postData: secondPost } = await topics.post({- uid,+ uid: otherUid,cid,title: utils.generateUUID(),content: utils.generateUUID(),@@ -142,12 +150,12 @@ describe('Outbox', () => {assert(types.includes('Create'));const upvoteActivities = resBody.orderedItems.filter(- (a) => a.type === 'Like' && a.object.type === 'Note'+ (a) => a.type === 'Like' && typeof a.object === 'string');assert(upvoteActivities.length >= 1);const dislikeActivities = resBody.orderedItems.filter(- (a) => a.type === 'Dislike' && a.object.type === 'Note'+ (a) => a.type === 'Dislike' && typeof a.object === 'string');assert(dislikeActivities.length >= 1);});@@ -155,6 +163,7 @@ describe('Outbox', () => {it('should paginate when there are more than 20 items', async () => {// Create 25 postsfor (let i = 0; i < 25; i++) {+ // eslint-disable-next-line no-await-in-loopawait topics.post({uid,cid,