GettingAllDocsTests.js 4.2 KB

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