MetaController.test.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { expect, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import MockResponse from '../helpers/MockResponse.mjs'
  4. const modulePath = '../../../../app/src/Features/Metadata/MetaController.mjs'
  5. describe('MetaController', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.EditorRealTimeController = {
  8. emitToRoom: sinon.stub(),
  9. }
  10. ctx.MetaHandler = {
  11. promises: {
  12. getAllMetaForProject: sinon.stub(),
  13. getMetaForDoc: sinon.stub(),
  14. },
  15. }
  16. vi.doMock(
  17. '../../../../app/src/Features/Editor/EditorRealTimeController',
  18. () => ({
  19. default: ctx.EditorRealTimeController,
  20. })
  21. )
  22. vi.doMock('../../../../app/src/Features/Metadata/MetaHandler', () => ({
  23. default: ctx.MetaHandler,
  24. }))
  25. vi.doMock(
  26. '../../../../app/src/Features/Analytics/AnalyticsManager',
  27. () => ({ default: {} })
  28. )
  29. vi.doMock(
  30. '../../../../app/src/Features/SplitTests/SplitTestHandler',
  31. () => ({
  32. default: {
  33. promises: {
  34. getAssignment: sinon.stub().resolves({}),
  35. },
  36. },
  37. })
  38. )
  39. ctx.MetadataController = (await import(modulePath)).default
  40. })
  41. describe('getMetadata', function () {
  42. it('should respond with json', async function (ctx) {
  43. const projectMeta = {
  44. 'doc-id': {
  45. labels: ['foo'],
  46. packages: { a: { commands: [] } },
  47. packageNames: ['a'],
  48. },
  49. }
  50. ctx.MetaHandler.promises.getAllMetaForProject = sinon
  51. .stub()
  52. .resolves(projectMeta)
  53. const req = { params: { project_id: 'project-id' } }
  54. const res = new MockResponse(vi)
  55. const next = sinon.stub()
  56. await ctx.MetadataController.getMetadata(req, res, next)
  57. ctx.MetaHandler.promises.getAllMetaForProject.should.have.been.calledWith(
  58. 'project-id'
  59. )
  60. expect(res.json).toHaveBeenCalledTimes(1)
  61. expect(res.json).toHaveBeenCalledWith({
  62. projectId: 'project-id',
  63. projectMeta,
  64. })
  65. next.should.not.have.been.called
  66. })
  67. it('should handle an error', async function (ctx) {
  68. ctx.MetaHandler.promises.getAllMetaForProject = sinon
  69. .stub()
  70. .throws(new Error('woops'))
  71. const req = { params: { project_id: 'project-id' } }
  72. const res = new MockResponse(vi)
  73. const next = sinon.stub()
  74. await ctx.MetadataController.getMetadata(req, res, next)
  75. ctx.MetaHandler.promises.getAllMetaForProject.should.have.been.calledWith(
  76. 'project-id'
  77. )
  78. expect(res.json).not.toHaveBeenCalled()
  79. next.should.have.been.calledWithMatch(error => error instanceof Error)
  80. })
  81. })
  82. describe('broadcastMetadataForDoc', function () {
  83. it('should broadcast on broadcast:true ', async function (ctx) {
  84. ctx.MetaHandler.promises.getMetaForDoc = sinon.stub().resolves({
  85. labels: ['foo'],
  86. packages: { a: { commands: [] } },
  87. packageNames: ['a'],
  88. })
  89. ctx.EditorRealTimeController.emitToRoom = sinon.stub()
  90. const req = {
  91. params: { project_id: 'project-id', doc_id: 'doc-id' },
  92. body: { broadcast: true },
  93. }
  94. const res = new MockResponse(vi)
  95. const next = sinon.stub()
  96. await ctx.MetadataController.broadcastMetadataForDoc(req, res, next)
  97. ctx.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
  98. 'project-id'
  99. )
  100. expect(res.json).not.toHaveBeenCalled()
  101. expect(res.sendStatus).toHaveBeenCalledTimes(1)
  102. expect(res.sendStatus).toHaveBeenCalledWith(200)
  103. next.should.not.have.been.called
  104. ctx.EditorRealTimeController.emitToRoom.should.have.been.calledOnce
  105. const { lastCall } = ctx.EditorRealTimeController.emitToRoom
  106. expect(lastCall.args[0]).to.equal('project-id')
  107. expect(lastCall.args[1]).to.equal('broadcastDocMeta')
  108. expect(lastCall.args[2]).to.have.all.keys(['docId', 'meta'])
  109. })
  110. it('should return json on broadcast:false ', async function (ctx) {
  111. const docMeta = {
  112. labels: ['foo'],
  113. packages: { a: [] },
  114. packageNames: ['a'],
  115. }
  116. ctx.MetaHandler.promises.getMetaForDoc = sinon.stub().resolves(docMeta)
  117. ctx.EditorRealTimeController.emitToRoom = sinon.stub()
  118. const req = {
  119. params: { project_id: 'project-id', doc_id: 'doc-id' },
  120. body: { broadcast: false },
  121. }
  122. const res = new MockResponse(vi)
  123. const next = sinon.stub()
  124. await ctx.MetadataController.broadcastMetadataForDoc(req, res, next)
  125. ctx.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
  126. 'project-id'
  127. )
  128. ctx.EditorRealTimeController.emitToRoom.should.not.have.been.called
  129. expect(res.json).toHaveBeenCalledTimes(1)
  130. expect(res.json).toHaveBeenCalledWith({
  131. docId: 'doc-id',
  132. meta: docMeta,
  133. })
  134. next.should.not.have.been.called
  135. })
  136. it('should handle an error', async function (ctx) {
  137. ctx.MetaHandler.promises.getMetaForDoc = sinon
  138. .stub()
  139. .throws(new Error('woops'))
  140. ctx.EditorRealTimeController.emitToRoom = sinon.stub()
  141. const req = {
  142. params: { project_id: 'project-id', doc_id: 'doc-id' },
  143. body: { broadcast: true },
  144. }
  145. const res = new MockResponse(vi)
  146. const next = sinon.stub()
  147. await ctx.MetadataController.broadcastMetadataForDoc(req, res, next)
  148. ctx.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
  149. 'project-id'
  150. )
  151. expect(res.json).not.toHaveBeenCalled()
  152. next.should.have.been.calledWithMatch(error => error instanceof Error)
  153. })
  154. })
  155. })