From 632a563ed2eb6c8c838e21a43875740d69fe878a Mon Sep 17 00:00:00 2001 From: Domagoj Kriskovic Date: Thu, 2 Oct 2025 11:50:17 +0200 Subject: [PATCH] convert some helper functions in ProjectHistoryClient GitOrigin-RevId: a73207c4dfbf13cd96456e024adfa6a046002e00 --- .../test/acceptance/js/DeleteProjectTests.js | 6 +- .../test/acceptance/js/DiffTests.js | 6 +- .../test/acceptance/js/RetryTests.js | 4 +- .../js/helpers/ProjectHistoryClient.js | 75 ++++++------------- 4 files changed, 32 insertions(+), 59 deletions(-) diff --git a/services/project-history/test/acceptance/js/DeleteProjectTests.js b/services/project-history/test/acceptance/js/DeleteProjectTests.js index 56dc5da3f1..a8f449d0ba 100644 --- a/services/project-history/test/acceptance/js/DeleteProjectTests.js +++ b/services/project-history/test/acceptance/js/DeleteProjectTests.js @@ -28,7 +28,7 @@ describe('Deleting project', function () { describe('when the project has no pending updates', function () { it('successfully deletes the project', async function () { - await ProjectHistoryClient.promises.deleteProject(this.projectId) + await ProjectHistoryClient.deleteProject(this.projectId) }) }) @@ -44,11 +44,11 @@ describe('Deleting project', function () { this.projectId, Date.now() ) - await ProjectHistoryClient.promises.deleteProject(this.projectId) + await ProjectHistoryClient.deleteProject(this.projectId) }) it('clears pending updates', async function () { - const dump = await ProjectHistoryClient.promises.getDump(this.projectId) + const dump = await ProjectHistoryClient.getDump(this.projectId) expect(dump.updates).to.deep.equal([]) }) diff --git a/services/project-history/test/acceptance/js/DiffTests.js b/services/project-history/test/acceptance/js/DiffTests.js index 29cf259131..38e3b2cc58 100644 --- a/services/project-history/test/acceptance/js/DiffTests.js +++ b/services/project-history/test/acceptance/js/DiffTests.js @@ -101,7 +101,7 @@ describe('Diffs', function () { authors: [31], }) - const diff = await ProjectHistoryClient.promises.getDiff( + const diff = await ProjectHistoryClient.getDiff( this.projectId, 'foo.tex', 3, @@ -228,7 +228,7 @@ describe('Diffs', function () { authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }], }) - const diff = await ProjectHistoryClient.promises.getDiff( + const diff = await ProjectHistoryClient.getDiff( this.projectId, 'foo.tex', 4, @@ -367,7 +367,7 @@ describe('Diffs', function () { authors: [{ id: 31, email: 'james.allen@overleaf.com', name: 'James' }], }) - const diff = await ProjectHistoryClient.promises.getDiff( + const diff = await ProjectHistoryClient.getDiff( this.projectId, 'binary.tex', 3, diff --git a/services/project-history/test/acceptance/js/RetryTests.js b/services/project-history/test/acceptance/js/RetryTests.js index 53c29d758f..62efbbaa21 100644 --- a/services/project-history/test/acceptance/js/RetryTests.js +++ b/services/project-history/test/acceptance/js/RetryTests.js @@ -75,7 +75,7 @@ describe('Retrying failed projects', function () { this.project_id, update ) - await ProjectHistoryClient.promises.setFailure({ + await ProjectHistoryClient.setFailure({ project_id: this.project_id, attempts: 1, error: 'soft-error', @@ -143,7 +143,7 @@ describe('Retrying failed projects', function () { }, }, }) - await ProjectHistoryClient.promises.setFailure({ + await ProjectHistoryClient.setFailure({ project_id: this.project_id, attempts: 100, error: 'hard-error', diff --git a/services/project-history/test/acceptance/js/helpers/ProjectHistoryClient.js b/services/project-history/test/acceptance/js/helpers/ProjectHistoryClient.js index 1c6f9c3451..01e2c5952a 100644 --- a/services/project-history/test/acceptance/js/helpers/ProjectHistoryClient.js +++ b/services/project-history/test/acceptance/js/helpers/ProjectHistoryClient.js @@ -4,6 +4,7 @@ import Settings from '@overleaf/settings' import RedisWrapper from '@overleaf/redis-wrapper' import { db } from '../../../../app/js/mongodb.js' import { promisify } from '@overleaf/promise-utils' +import { fetchJsonWithResponse, fetchNothing } from '@overleaf/fetch-utils' const rclient = RedisWrapper.createClient(Settings.redis.project_history) const Keys = Settings.redis.project_history.key_schema @@ -69,25 +70,15 @@ export function getSummarizedUpdates(projectId, query, callback) { ) } -export function getDiff(projectId, pathname, from, to, callback) { - request.get( - { - url: `http://127.0.0.1:3054/project/${projectId}/diff`, - qs: { - pathname, - from, - to, - }, - json: true, - }, - (error, res, body) => { - if (error) { - return callback(error) - } - expect(res.statusCode).to.equal(200) - callback(error, body) - } - ) +export async function getDiff(projectId, pathname, from, to) { + const url = new URL(`http://127.0.0.1:3054/project/${projectId}/diff`) + url.searchParams.set('pathname', pathname) + url.searchParams.set('from', from) + url.searchParams.set('to', to) + + const { response, json } = await fetchJsonWithResponse(url.toString()) + expect(response.status).to.equal(200) + return json } export function getFileTreeDiff(projectId, from, to, callback) { @@ -300,16 +291,9 @@ export function deleteLabel(projectId, labelId, callback) { ) } -export function setFailure(failureEntry, callback) { - db.projectHistoryFailures.deleteOne( - { project_id: { $exists: true } }, - (err, result) => { - if (err) { - return callback(err) - } - db.projectHistoryFailures.insertOne(failureEntry, callback) - } - ) +export async function setFailure(failureEntry) { + await db.projectHistoryFailures.deleteOne({ project_id: { $exists: true } }) + return await db.projectHistoryFailures.insertOne(failureEntry) } export function getFailure(projectId, callback) { @@ -331,37 +315,26 @@ export function transferLabelOwnership(fromUser, toUser, callback) { ) } -export function getDump(projectId, callback) { - request.get( - `http://127.0.0.1:3054/project/${projectId}/dump`, - (err, res, body) => { - if (err) { - return callback(err) - } - expect(res.statusCode).to.equal(200) - callback(null, JSON.parse(body)) - } +export async function getDump(projectId) { + const { response, json } = await fetchJsonWithResponse( + `http://127.0.0.1:3054/project/${projectId}/dump` ) + expect(response.status).to.equal(200) + return json } -export function deleteProject(projectId, callback) { - request.delete(`http://127.0.0.1:3054/project/${projectId}`, (err, res) => { - if (err) { - return callback(err) - } - expect(res.statusCode).to.equal(204) - callback() - }) +export async function deleteProject(projectId) { + const response = await fetchNothing( + `http://127.0.0.1:3054/project/${projectId}`, + { method: 'DELETE' } + ) + expect(response.status).to.equal(204) } export const promises = { initializeProject: promisify(initializeProject), - deleteProject: promisify(deleteProject), pushRawUpdate: promisify(pushRawUpdate), setFirstOpTimestamp: promisify(setFirstOpTimestamp), getFirstOpTimestamp: promisify(getFirstOpTimestamp), - getDump: promisify(getDump), - setFailure: promisify(setFailure), - getDiff: promisify(getDiff), flushProject: promisify(flushProject), }