GettingAllDocsTests.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const { ObjectId } = require('mongodb-legacy')
  2. const async = require('async')
  3. const DocstoreApp = require('./helpers/DocstoreApp')
  4. const { callbackify } = require('node:util')
  5. const DocstoreClient = require('./helpers/DocstoreClient')
  6. describe('Getting all docs', function () {
  7. beforeEach(function (done) {
  8. this.project_id = new ObjectId()
  9. this.threadId1 = new ObjectId().toString()
  10. this.threadId2 = new ObjectId().toString()
  11. this.docs = [
  12. {
  13. _id: new ObjectId(),
  14. lines: ['one', 'two', 'three'],
  15. ranges: {
  16. comments: [
  17. { id: new ObjectId().toString(), op: { t: this.threadId1 } },
  18. ],
  19. changes: [
  20. {
  21. id: new ObjectId().toString(),
  22. metadata: { user_id: 'user-id-1' },
  23. },
  24. ],
  25. },
  26. rev: 2,
  27. },
  28. {
  29. _id: new ObjectId(),
  30. lines: ['aaa', 'bbb', 'ccc'],
  31. ranges: {
  32. changes: [
  33. {
  34. id: new ObjectId().toString(),
  35. metadata: { user_id: 'user-id-2' },
  36. },
  37. ],
  38. },
  39. rev: 4,
  40. },
  41. {
  42. _id: new ObjectId(),
  43. lines: ['111', '222', '333'],
  44. ranges: {
  45. comments: [
  46. { id: new ObjectId().toString(), op: { t: this.threadId2 } },
  47. ],
  48. changes: [
  49. {
  50. id: new ObjectId().toString(),
  51. metadata: { user_id: 'anonymous-user' },
  52. },
  53. ],
  54. },
  55. rev: 6,
  56. },
  57. ]
  58. this.fixedRanges = this.docs.map(doc => {
  59. if (!doc.ranges?.comments?.length) return doc.ranges
  60. return {
  61. ...doc.ranges,
  62. comments: [
  63. { ...doc.ranges.comments[0], id: doc.ranges.comments[0].op.t },
  64. ],
  65. }
  66. })
  67. this.deleted_doc = {
  68. _id: new ObjectId(),
  69. lines: ['deleted'],
  70. ranges: {
  71. comments: [{ id: new ObjectId().toString(), op: { t: 'thread-id-3' } }],
  72. changes: [
  73. { id: new ObjectId().toString(), metadata: { user_id: 'user-id-3' } },
  74. ],
  75. },
  76. rev: 8,
  77. }
  78. const version = 42
  79. const jobs = this.docs.map(doc =>
  80. (
  81. doc => callback =>
  82. callbackify(DocstoreClient.createDoc)(
  83. this.project_id,
  84. doc._id,
  85. doc.lines,
  86. version,
  87. doc.ranges,
  88. callback
  89. )
  90. )(doc)
  91. )
  92. jobs.push(cb =>
  93. callbackify(DocstoreClient.createDoc)(
  94. this.project_id,
  95. this.deleted_doc._id,
  96. this.deleted_doc.lines,
  97. version,
  98. this.deleted_doc.ranges,
  99. err => {
  100. if (err) return done(err)
  101. callbackify(DocstoreClient.deleteDoc)(
  102. this.project_id,
  103. this.deleted_doc._id,
  104. cb
  105. )
  106. }
  107. )
  108. )
  109. jobs.unshift(cb => callbackify(DocstoreApp.ensureRunning)(cb))
  110. async.series(jobs, done)
  111. })
  112. it('getAllDocs should return all the (non-deleted) docs', async function () {
  113. const docs = await DocstoreClient.getAllDocs(this.project_id)
  114. docs.length.should.equal(this.docs.length)
  115. for (let i = 0; i < docs.length; i++) {
  116. const doc = docs[i]
  117. doc.lines.should.deep.equal(this.docs[i].lines)
  118. }
  119. })
  120. it('getAllRanges should return all the (non-deleted) doc ranges', async function () {
  121. const docs = await DocstoreClient.getAllRanges(this.project_id)
  122. docs.length.should.equal(this.docs.length)
  123. for (let i = 0; i < docs.length; i++) {
  124. const doc = docs[i]
  125. doc.ranges.should.deep.equal(this.fixedRanges[i])
  126. }
  127. })
  128. it('getTrackedChangesUserIds should return all the user ids from (non-deleted) ranges', async function () {
  129. const userIds = await DocstoreClient.getTrackedChangesUserIds(
  130. this.project_id
  131. )
  132. userIds.should.deep.equal(['user-id-1', 'user-id-2'])
  133. })
  134. it('getCommentThreadIds should return all the thread ids from (non-deleted) ranges', async function () {
  135. const threadIds = await DocstoreClient.getCommentThreadIds(this.project_id)
  136. threadIds.should.deep.equal({
  137. [this.docs[0]._id.toString()]: [this.threadId1],
  138. [this.docs[2]._id.toString()]: [this.threadId2],
  139. })
  140. })
  141. })