GettingAllDocsTests.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.docs = [
  22. {
  23. _id: new ObjectId(),
  24. lines: ['one', 'two', 'three'],
  25. ranges: {
  26. comments: [
  27. { id: new ObjectId().toString(), op: { t: 'thread-id-1' } },
  28. ],
  29. changes: [
  30. {
  31. id: new ObjectId().toString(),
  32. metadata: { user_id: 'user-id-1' },
  33. },
  34. ],
  35. },
  36. rev: 2,
  37. },
  38. {
  39. _id: new ObjectId(),
  40. lines: ['aaa', 'bbb', 'ccc'],
  41. ranges: {
  42. changes: [
  43. {
  44. id: new ObjectId().toString(),
  45. metadata: { user_id: 'user-id-2' },
  46. },
  47. ],
  48. },
  49. rev: 4,
  50. },
  51. {
  52. _id: new ObjectId(),
  53. lines: ['111', '222', '333'],
  54. ranges: {
  55. comments: [
  56. { id: new ObjectId().toString(), op: { t: 'thread-id-2' } },
  57. ],
  58. changes: [
  59. {
  60. id: new ObjectId().toString(),
  61. metadata: { user_id: 'anonymous-user' },
  62. },
  63. ],
  64. },
  65. rev: 6,
  66. },
  67. ]
  68. this.deleted_doc = {
  69. _id: new ObjectId(),
  70. lines: ['deleted'],
  71. ranges: {
  72. comments: [{ id: new ObjectId().toString(), op: { t: 'thread-id-3' } }],
  73. changes: [
  74. { id: new ObjectId().toString(), metadata: { user_id: 'user-id-3' } },
  75. ],
  76. },
  77. rev: 8,
  78. }
  79. const version = 42
  80. const jobs = Array.from(this.docs).map(doc =>
  81. (doc => {
  82. return callback => {
  83. return DocstoreClient.createDoc(
  84. this.project_id,
  85. doc._id,
  86. doc.lines,
  87. version,
  88. doc.ranges,
  89. callback
  90. )
  91. }
  92. })(doc)
  93. )
  94. jobs.push(cb => {
  95. return DocstoreClient.createDoc(
  96. this.project_id,
  97. this.deleted_doc._id,
  98. this.deleted_doc.lines,
  99. version,
  100. this.deleted_doc.ranges,
  101. err => {
  102. if (err) return done(err)
  103. return DocstoreClient.deleteDoc(
  104. this.project_id,
  105. this.deleted_doc._id,
  106. cb
  107. )
  108. }
  109. )
  110. })
  111. jobs.unshift(cb => DocstoreApp.ensureRunning(cb))
  112. return async.series(jobs, done)
  113. })
  114. it('getAllDocs should return all the (non-deleted) docs', function (done) {
  115. return DocstoreClient.getAllDocs(this.project_id, (error, res, docs) => {
  116. if (error != null) {
  117. throw error
  118. }
  119. docs.length.should.equal(this.docs.length)
  120. for (let i = 0; i < docs.length; i++) {
  121. const doc = docs[i]
  122. doc.lines.should.deep.equal(this.docs[i].lines)
  123. }
  124. return done()
  125. })
  126. })
  127. it('getAllRanges should return all the (non-deleted) doc ranges', function (done) {
  128. return DocstoreClient.getAllRanges(this.project_id, (error, res, docs) => {
  129. if (error != null) {
  130. throw error
  131. }
  132. docs.length.should.equal(this.docs.length)
  133. for (let i = 0; i < docs.length; i++) {
  134. const doc = docs[i]
  135. doc.ranges.should.deep.equal(this.docs[i].ranges)
  136. }
  137. return done()
  138. })
  139. })
  140. it('getTrackedChangesUserIds should return all the user ids from (non-deleted) ranges', function (done) {
  141. DocstoreClient.getTrackedChangesUserIds(
  142. this.project_id,
  143. (error, res, userIds) => {
  144. if (error != null) {
  145. throw error
  146. }
  147. userIds.should.deep.equal(['user-id-1', 'user-id-2'])
  148. done()
  149. }
  150. )
  151. })
  152. it('getCommentThreadIds should return all the thread ids from (non-deleted) ranges', function (done) {
  153. DocstoreClient.getCommentThreadIds(
  154. this.project_id,
  155. (error, res, threadIds) => {
  156. if (error != null) {
  157. throw error
  158. }
  159. threadIds.should.deep.equal({
  160. [this.docs[0]._id.toString()]: ['thread-id-1'],
  161. [this.docs[2]._id.toString()]: ['thread-id-2'],
  162. })
  163. done()
  164. }
  165. )
  166. })
  167. })