Przeglądaj źródła

Merge pull request #17655 from overleaf/em-doc-length-fix

Fix doc length reporting in document-updater

GitOrigin-RevId: 37a9c35e3510f7b5be233fff817c89eeff1cb296
Eric Mc Sween 2 lat temu
rodzic
commit
19937e3e7e

+ 4 - 1
services/document-updater/app/js/UpdateManager.js

@@ -320,7 +320,10 @@ const UpdateManager = {
     historyRangesSupport
   ) {
     let docLength = _.reduce(lines, (chars, line) => chars + line.length, 0)
-    docLength += lines.length - 1 // count newline characters
+    // Add newline characters. Lines are joined by newlines, but the last line
+    // doesn't include a newline. We must make a special case for an empty list
+    // so that it doesn't report a doc length of -1.
+    docLength += Math.max(lines.length - 1, 0)
     let historyDocLength = docLength
     for (const change of ranges.changes ?? []) {
       if ('d' in change.op) {

+ 23 - 0
services/document-updater/test/unit/js/UpdateManager/UpdateManagerTests.js

@@ -678,6 +678,29 @@ describe('UpdateManager', function () {
         },
       ])
     })
+
+    it('should calculate the right doc length for an empty document', function () {
+      this.historyUpdates = [{ v: 42, op: [{ i: 'foobar', p: 0 }] }]
+      this.UpdateManager._adjustHistoryUpdatesMetadata(
+        this.historyUpdates,
+        this.pathname,
+        this.projectHistoryId,
+        [],
+        {},
+        false
+      )
+      this.historyUpdates.should.deep.equal([
+        {
+          projectHistoryId: this.projectHistoryId,
+          v: 42,
+          op: [{ i: 'foobar', p: 0 }],
+          meta: {
+            pathname: this.pathname,
+            doc_length: 0,
+          },
+        },
+      ])
+    })
   })
 
   describe('lockUpdatesAndDo', function () {