GettingAllDocsTests.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const { ObjectId } = require('mongodb-legacy')
  15. const async = require('async')
  16. const DocstoreApp = require('./helpers/DocstoreApp')
  17. const DocstoreClient = require('./helpers/DocstoreClient')
  18. describe('Getting all docs', function () {
  19. beforeEach(function (done) {
  20. this.project_id = new ObjectId()
  21. this.threadId1 = new ObjectId().toString()
  22. this.threadId2 = new ObjectId().toString()
  23. this.docs = [
  24. {
  25. _id: new ObjectId(),
  26. lines: ['one', 'two', 'three'],
  27. ranges: {
  28. comments: [
  29. { id: new ObjectId().toString(), op: { t: this.threadId1 } },
  30. ],
  31. changes: [
  32. {
  33. id: new ObjectId().toString(),
  34. metadata: { user_id: 'user-id-1' },
  35. },
  36. ],
  37. },
  38. rev: 2,
  39. },
  40. {
  41. _id: new ObjectId(),
  42. lines: ['aaa', 'bbb', 'ccc'],
  43. ranges: {
  44. changes: [
  45. {
  46. id: new ObjectId().toString(),
  47. metadata: { user_id: 'user-id-2' },
  48. },
  49. ],
  50. },
  51. rev: 4,
  52. },
  53. {
  54. _id: new ObjectId(),
  55. lines: ['111', '222', '333'],
  56. ranges: {
  57. comments: [
  58. { id: new ObjectId().toString(), op: { t: this.threadId2 } },
  59. ],
  60. changes: [
  61. {
  62. id: new ObjectId().toString(),
  63. metadata: { user_id: 'anonymous-user' },
  64. },
  65. ],
  66. },
  67. rev: 6,
  68. },
  69. ]
  70. this.fixedRanges = this.docs.map(doc => {
  71. if (!doc.ranges?.comments?.length) return doc.ranges
  72. return {
  73. ...doc.ranges,
  74. comments: [
  75. { ...doc.ranges.comments[0], id: doc.ranges.comments[0].op.t },
  76. ],
  77. }
  78. })
  79. this.deleted_doc = {
  80. _id: new ObjectId(),
  81. lines: ['deleted'],
  82. ranges: {
  83. comments: [{ id: new ObjectId().toString(), op: { t: 'thread-id-3' } }],
  84. changes: [
  85. { id: new ObjectId().toString(), metadata: { user_id: 'user-id-3' } },
  86. ],
  87. },
  88. rev: 8,
  89. }
  90. const version = 42
  91. const jobs = Array.from(this.docs).map(doc =>
  92. (doc => {
  93. return callback => {
  94. return DocstoreClient.createDoc(
  95. this.project_id,
  96. doc._id,
  97. doc.lines,
  98. version,
  99. doc.ranges,
  100. callback
  101. )
  102. }
  103. })(doc)
  104. )
  105. jobs.push(cb => {
  106. return DocstoreClient.createDoc(
  107. this.project_id,
  108. this.deleted_doc._id,
  109. this.deleted_doc.lines,
  110. version,
  111. this.deleted_doc.ranges,
  112. err => {
  113. if (err) return done(err)
  114. return DocstoreClient.deleteDoc(
  115. this.project_id,
  116. this.deleted_doc._id,
  117. cb
  118. )
  119. }
  120. )
  121. })
  122. jobs.unshift(cb => DocstoreApp.ensureRunning(cb))
  123. return async.series(jobs, done)
  124. })
  125. it('getAllDocs should return all the (non-deleted) docs', function (done) {
  126. return DocstoreClient.getAllDocs(this.project_id, (error, res, docs) => {
  127. if (error != null) {
  128. throw error
  129. }
  130. docs.length.should.equal(this.docs.length)
  131. for (let i = 0; i < docs.length; i++) {
  132. const doc = docs[i]
  133. doc.lines.should.deep.equal(this.docs[i].lines)
  134. }
  135. return done()
  136. })
  137. })
  138. it('getAllRanges should return all the (non-deleted) doc ranges', function (done) {
  139. return DocstoreClient.getAllRanges(this.project_id, (error, res, docs) => {
  140. if (error != null) {
  141. throw error
  142. }
  143. docs.length.should.equal(this.docs.length)
  144. for (let i = 0; i < docs.length; i++) {
  145. const doc = docs[i]
  146. doc.ranges.should.deep.equal(this.fixedRanges[i])
  147. }
  148. return done()
  149. })
  150. })
  151. it('getTrackedChangesUserIds should return all the user ids from (non-deleted) ranges', function (done) {
  152. DocstoreClient.getTrackedChangesUserIds(
  153. this.project_id,
  154. (error, res, userIds) => {
  155. if (error != null) {
  156. throw error
  157. }
  158. userIds.should.deep.equal(['user-id-1', 'user-id-2'])
  159. done()
  160. }
  161. )
  162. })
  163. it('getCommentThreadIds should return all the thread ids from (non-deleted) ranges', function (done) {
  164. DocstoreClient.getCommentThreadIds(
  165. this.project_id,
  166. (error, res, threadIds) => {
  167. if (error != null) {
  168. throw error
  169. }
  170. threadIds.should.deep.equal({
  171. [this.docs[0]._id.toString()]: [this.threadId1],
  172. [this.docs[2]._id.toString()]: [this.threadId2],
  173. })
  174. done()
  175. }
  176. )
  177. })
  178. })