ChatApiHandler.test.mjs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { vi, expect } from 'vitest'
  2. import path from 'node:path'
  3. import sinon from 'sinon'
  4. import { RequestFailedError } from '@overleaf/fetch-utils'
  5. const MODULE_PATH = path.join(
  6. import.meta.dirname,
  7. '../../../../app/src/Features/Chat/ChatApiHandler'
  8. )
  9. describe('ChatApiHandler', function () {
  10. beforeEach(async function (ctx) {
  11. ctx.settings = {
  12. apis: {
  13. chat: {
  14. internal_url: 'http://chat.overleaf.env',
  15. },
  16. },
  17. }
  18. ctx.FetchUtils = {
  19. fetchJson: sinon.stub(),
  20. fetchNothing: sinon.stub().resolves(),
  21. }
  22. vi.doMock('@overleaf/settings', () => ({
  23. default: ctx.settings,
  24. }))
  25. vi.doMock('@overleaf/fetch-utils', () => ctx.FetchUtils)
  26. ctx.ChatApiHandler = (await import(MODULE_PATH)).default
  27. ctx.project_id = '3213213kl12j'
  28. ctx.user_id = '2k3jlkjs9'
  29. ctx.content = 'my message here'
  30. })
  31. describe('sendGlobalMessage', function () {
  32. describe('successfully', function () {
  33. beforeEach(async function (ctx) {
  34. ctx.message = { mock: 'message' }
  35. ctx.FetchUtils.fetchJson.resolves(ctx.message)
  36. ctx.result = await ctx.ChatApiHandler.promises.sendGlobalMessage(
  37. ctx.project_id,
  38. ctx.user_id,
  39. ctx.content
  40. )
  41. })
  42. it('should post the data to the chat api', function (ctx) {
  43. ctx.FetchUtils.fetchJson.should.have.been.calledWith(
  44. sinon.match(
  45. url =>
  46. url.toString() ===
  47. `${ctx.settings.apis.chat.internal_url}/project/${ctx.project_id}/messages`
  48. ),
  49. {
  50. method: 'POST',
  51. json: {
  52. content: ctx.content,
  53. user_id: ctx.user_id,
  54. },
  55. }
  56. )
  57. })
  58. it('should return the message from the post', function (ctx) {
  59. expect(ctx.result).to.deep.equal(ctx.message)
  60. })
  61. })
  62. describe('with a non-success status code', function () {
  63. beforeEach(async function (ctx) {
  64. ctx.error = new RequestFailedError('some-url', {}, { status: 500 })
  65. ctx.FetchUtils.fetchJson.rejects(ctx.error)
  66. })
  67. it('should throw the error', async function (ctx) {
  68. await expect(
  69. ctx.ChatApiHandler.promises.sendGlobalMessage(
  70. ctx.project_id,
  71. ctx.user_id,
  72. ctx.content
  73. )
  74. ).to.be.rejectedWith(ctx.error)
  75. })
  76. })
  77. })
  78. describe('getGlobalMessages', function () {
  79. beforeEach(function (ctx) {
  80. ctx.messages = [{ mock: 'message' }]
  81. ctx.limit = 30
  82. ctx.before = '1234'
  83. })
  84. describe('successfully', function () {
  85. beforeEach(async function (ctx) {
  86. ctx.FetchUtils.fetchJson.resolves(ctx.messages)
  87. ctx.result = await ctx.ChatApiHandler.promises.getGlobalMessages(
  88. ctx.project_id,
  89. ctx.limit,
  90. ctx.before
  91. )
  92. })
  93. it('should make get request for room to chat api', function (ctx) {
  94. ctx.FetchUtils.fetchJson.should.have.been.calledWith(
  95. sinon.match(
  96. url =>
  97. url.toString() ===
  98. `${ctx.settings.apis.chat.internal_url}/project/${ctx.project_id}/messages?limit=${ctx.limit}&before=${ctx.before}`
  99. )
  100. )
  101. })
  102. it('should return the messages from the request', function (ctx) {
  103. expect(ctx.result).to.deep.equal(ctx.messages)
  104. })
  105. })
  106. describe('with failure error code', function () {
  107. beforeEach(function (ctx) {
  108. ctx.error = new RequestFailedError('some-url', {}, { status: 500 })
  109. ctx.FetchUtils.fetchJson.rejects(ctx.error)
  110. })
  111. it('should throw the error', async function (ctx) {
  112. await expect(
  113. ctx.ChatApiHandler.promises.getGlobalMessages(
  114. ctx.project_id,
  115. ctx.limit,
  116. ctx.before
  117. )
  118. ).to.be.rejectedWith(ctx.error)
  119. })
  120. })
  121. })
  122. describe('duplicateCommentThreads', function () {
  123. beforeEach(async function (ctx) {
  124. ctx.FetchUtils.fetchJson.resolves(
  125. (ctx.mapping = {
  126. 'comment-thread-1': 'comment-thread-1-dup',
  127. 'comment-thread-2': 'comment-thread-2-dup',
  128. 'comment-thread-3': 'comment-thread-3-dup',
  129. })
  130. )
  131. ctx.threads = ['comment-thread-1', 'comment-thread-2', 'comment-thread-3']
  132. ctx.result = await ctx.ChatApiHandler.promises.duplicateCommentThreads(
  133. ctx.project_id,
  134. ctx.threads
  135. )
  136. })
  137. it('should make a post request to the chat api', function (ctx) {
  138. expect(ctx.FetchUtils.fetchJson).to.have.been.calledWith(
  139. sinon.match(
  140. url =>
  141. url.toString() ===
  142. `${ctx.settings.apis.chat.internal_url}/project/${ctx.project_id}/duplicate-comment-threads`
  143. ),
  144. {
  145. method: 'POST',
  146. json: {
  147. threads: ctx.threads,
  148. },
  149. }
  150. )
  151. })
  152. it('should return the thread mapping', function (ctx) {
  153. expect(ctx.result).to.deep.equal(ctx.mapping)
  154. })
  155. })
  156. describe('generateThreadData', async function () {
  157. beforeEach(async function (ctx) {
  158. ctx.FetchUtils.fetchJson.resolves(
  159. (ctx.chatResponse = {
  160. 'comment-thread-1': {
  161. messages: [
  162. {
  163. content: 'message 1',
  164. timestamp: '2024-01-01T00:00:00.000Z',
  165. user_id: 'user-1',
  166. },
  167. ],
  168. },
  169. 'comment-thread-2': {
  170. messages: [
  171. {
  172. content: 'message 2',
  173. timestamp: '2024-01-01T00:00:00.000Z',
  174. user_id: 'user-2',
  175. },
  176. ],
  177. },
  178. })
  179. )
  180. // Chat won't return threads that couldn't be found, so response can have
  181. // fewer threads
  182. ctx.threads = ['comment-thread-1', 'comment-thread-2', 'comment-thread-3']
  183. ctx.result = await ctx.ChatApiHandler.promises.generateThreadData(
  184. ctx.project_id,
  185. ctx.threads
  186. )
  187. })
  188. it('should make a post request to the chat api', function (ctx) {
  189. expect(ctx.FetchUtils.fetchJson).to.have.been.calledWith(
  190. sinon.match(
  191. url =>
  192. url.toString() ===
  193. `${ctx.settings.apis.chat.internal_url}/project/${ctx.project_id}/generate-thread-data`
  194. ),
  195. {
  196. method: 'POST',
  197. json: {
  198. threads: ctx.threads,
  199. },
  200. }
  201. )
  202. })
  203. it('should return the thread data', function (ctx) {
  204. expect(ctx.result).to.deep.equal(ctx.chatResponse)
  205. })
  206. })
  207. })