Просмотр исходного кода

promisify GetChangesInChunkSince

GitOrigin-RevId: 82af36d7cb0301e3bf9527b219c67ceb6fc9b34f
Domagoj Kriskovic 10 месяцев назад
Родитель
Сommit
c1bfd2192d

+ 55 - 66
services/project-history/test/acceptance/js/GetChangesInChunkSince.js

@@ -16,76 +16,67 @@ const fixture = path => new URL(`../fixtures/${path}`, import.meta.url)
 
 describe('GetChangesInChunkSince', function () {
   let projectId, historyId
-  beforeEach(function (done) {
+  beforeEach(async function () {
     projectId = new ObjectId().toString()
     historyId = new ObjectId().toString()
-    ProjectHistoryApp.ensureRunning(error => {
-      if (error) throw error
+    await ProjectHistoryApp.promises.ensureRunning()
 
-      MockHistoryStore().post('/api/projects').reply(200, {
-        projectId: historyId,
-      })
-
-      ProjectHistoryClient.initializeProject(historyId, (error, olProject) => {
-        if (error) throw error
-        MockWeb()
-          .get(`/project/${projectId}/details`)
-          .reply(200, {
-            name: 'Test Project',
-            overleaf: { history: { id: olProject.id } },
-          })
-
-        MockHistoryStore()
-          .get(`/api/projects/${historyId}/latest/history`)
-          .replyWithFile(200, fixture('chunks/7-8.json'))
-        MockHistoryStore()
-          .get(`/api/projects/${historyId}/versions/7/history`)
-          .replyWithFile(200, fixture('chunks/7-8.json'))
-        MockHistoryStore()
-          .get(`/api/projects/${historyId}/versions/6/history`)
-          .replyWithFile(200, fixture('chunks/7-8.json'))
-        MockHistoryStore()
-          .get(`/api/projects/${historyId}/versions/5/history`)
-          .replyWithFile(200, fixture('chunks/4-6.json'))
-        MockHistoryStore()
-          .get(`/api/projects/${historyId}/versions/4/history`)
-          .replyWithFile(200, fixture('chunks/4-6.json'))
-        MockHistoryStore()
-          .get(`/api/projects/${historyId}/versions/3/history`)
-          .replyWithFile(200, fixture('chunks/4-6.json'))
-        MockHistoryStore()
-          .get(`/api/projects/${historyId}/versions/2/history`)
-          .replyWithFile(200, fixture('chunks/0-3.json'))
-        MockHistoryStore()
-          .get(`/api/projects/${historyId}/versions/1/history`)
-          .replyWithFile(200, fixture('chunks/0-3.json'))
-        MockHistoryStore()
-          .get(`/api/projects/${historyId}/versions/0/history`)
-          .replyWithFile(200, fixture('chunks/0-3.json'))
+    MockHistoryStore().post('/api/projects').reply(200, {
+      projectId: historyId,
+    })
 
-        done()
+    const olProject =
+      await ProjectHistoryClient.promises.initializeProject(historyId)
+    MockWeb()
+      .get(`/project/${projectId}/details`)
+      .reply(200, {
+        name: 'Test Project',
+        overleaf: { history: { id: olProject.id } },
       })
-    })
+
+    MockHistoryStore()
+      .get(`/api/projects/${historyId}/latest/history`)
+      .replyWithFile(200, fixture('chunks/7-8.json'))
+    MockHistoryStore()
+      .get(`/api/projects/${historyId}/versions/7/history`)
+      .replyWithFile(200, fixture('chunks/7-8.json'))
+    MockHistoryStore()
+      .get(`/api/projects/${historyId}/versions/6/history`)
+      .replyWithFile(200, fixture('chunks/7-8.json'))
+    MockHistoryStore()
+      .get(`/api/projects/${historyId}/versions/5/history`)
+      .replyWithFile(200, fixture('chunks/4-6.json'))
+    MockHistoryStore()
+      .get(`/api/projects/${historyId}/versions/4/history`)
+      .replyWithFile(200, fixture('chunks/4-6.json'))
+    MockHistoryStore()
+      .get(`/api/projects/${historyId}/versions/3/history`)
+      .replyWithFile(200, fixture('chunks/4-6.json'))
+    MockHistoryStore()
+      .get(`/api/projects/${historyId}/versions/2/history`)
+      .replyWithFile(200, fixture('chunks/0-3.json'))
+    MockHistoryStore()
+      .get(`/api/projects/${historyId}/versions/1/history`)
+      .replyWithFile(200, fixture('chunks/0-3.json'))
+    MockHistoryStore()
+      .get(`/api/projects/${historyId}/versions/0/history`)
+      .replyWithFile(200, fixture('chunks/0-3.json'))
   })
 
   afterEach(function () {
     nock.cleanAll()
   })
 
-  function expectChangesSince(version, n, changes, done) {
-    ProjectHistoryClient.getChangesInChunkSince(
+  async function expectChangesSince(version, n, changes) {
+    const { body } = await ProjectHistoryClient.getChangesInChunkSince(
       projectId,
       version,
-      {},
-      (error, got) => {
-        if (error) throw error
-        expect(got.latestStartVersion).to.equal(6)
-        expect(got.changes).to.have.length(n)
-        expect(got.changes.map(c => Core.Change.fromRaw(c))).to.deep.equal(
-          changes.map(c => Core.Change.fromRaw(c))
-        )
-        done()
-      }
+      {}
+    )
+    expect(body.latestStartVersion).to.equal(6)
+    expect(body.changes).to.have.length(n)
+    expect(body.changes.map(c => Core.Change.fromRaw(c))).to.deep.equal(
+      changes.map(c => Core.Change.fromRaw(c))
     )
   }
 
@@ -138,21 +129,19 @@ describe('GetChangesInChunkSince', function () {
   }
 
   for (const [since, { name, n, changes }] of Object.entries(cases)) {
-    it(name, function (done) {
-      expectChangesSince(since, n, changes, done)
+    it(name, async function () {
+      await expectChangesSince(since, n, changes)
     })
   }
 
-  it('should return an error when past the end version', function (done) {
-    ProjectHistoryClient.getChangesInChunkSince(
+  it('should return an error when past the end version', async function () {
+    const { statusCode } = await ProjectHistoryClient.getChangesInChunkSince(
       projectId,
       9,
-      { allowErrors: true },
-      (error, _body, statusCode) => {
-        if (error) throw error
-        expect(statusCode).to.equal(400)
-        done()
+      {
+        allowErrors: true,
       }
     )
+    expect(statusCode).to.equal(400)
   })
 })

+ 14 - 16
services/project-history/test/acceptance/js/helpers/ProjectHistoryClient.js

@@ -103,23 +103,21 @@ export async function getFileTreeDiff(projectId, from, to) {
   }
 }
 
-export function getChangesInChunkSince(projectId, since, options, callback) {
-  request.get(
-    {
-      url: `http://127.0.0.1:3054/project/${projectId}/changes-in-chunk`,
-      qs: {
-        since,
-      },
-      json: true,
-    },
-    (error, res, body) => {
-      if (error) return callback(error)
-      if (!options.allowErrors) {
-        expect(res.statusCode).to.equal(200)
-      }
-      callback(null, body, res.statusCode)
-    }
+export async function getChangesInChunkSince(projectId, since, options = {}) {
+  const url = new URL(
+    `http://127.0.0.1:3054/project/${projectId}/changes-in-chunk`
   )
+  url.searchParams.set('since', since)
+
+  try {
+    const { response, json } = await fetchJsonWithResponse(url.toString())
+    return { body: json, statusCode: response.status }
+  } catch (error) {
+    if (options.allowErrors && error instanceof RequestFailedError) {
+      return { body: null, statusCode: error.response.status }
+    }
+    throw error
+  }
 }
 
 export function getLatestSnapshot(projectId, callback) {