MetaController.test.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { expect, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import MockResponse from '../helpers/MockResponse.js'
  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. ctx.MetadataController = (await import(modulePath)).default
  26. })
  27. describe('getMetadata', function () {
  28. it('should respond with json', async function (ctx) {
  29. const projectMeta = {
  30. 'doc-id': {
  31. labels: ['foo'],
  32. packages: { a: { commands: [] } },
  33. packageNames: ['a'],
  34. },
  35. }
  36. ctx.MetaHandler.promises.getAllMetaForProject = sinon
  37. .stub()
  38. .resolves(projectMeta)
  39. const req = { params: { project_id: 'project-id' } }
  40. const res = new MockResponse()
  41. const next = sinon.stub()
  42. await ctx.MetadataController.getMetadata(req, res, next)
  43. ctx.MetaHandler.promises.getAllMetaForProject.should.have.been.calledWith(
  44. 'project-id'
  45. )
  46. res.json.should.have.been.calledOnceWith({
  47. projectId: 'project-id',
  48. projectMeta,
  49. })
  50. next.should.not.have.been.called
  51. })
  52. it('should handle an error', async function (ctx) {
  53. ctx.MetaHandler.promises.getAllMetaForProject = sinon
  54. .stub()
  55. .throws(new Error('woops'))
  56. const req = { params: { project_id: 'project-id' } }
  57. const res = new MockResponse()
  58. const next = sinon.stub()
  59. await ctx.MetadataController.getMetadata(req, res, next)
  60. ctx.MetaHandler.promises.getAllMetaForProject.should.have.been.calledWith(
  61. 'project-id'
  62. )
  63. res.json.should.not.have.been.called
  64. next.should.have.been.calledWithMatch(error => error instanceof Error)
  65. })
  66. })
  67. describe('broadcastMetadataForDoc', function () {
  68. it('should broadcast on broadcast:true ', async function (ctx) {
  69. ctx.MetaHandler.promises.getMetaForDoc = sinon.stub().resolves({
  70. labels: ['foo'],
  71. packages: { a: { commands: [] } },
  72. packageNames: ['a'],
  73. })
  74. ctx.EditorRealTimeController.emitToRoom = sinon.stub()
  75. const req = {
  76. params: { project_id: 'project-id', doc_id: 'doc-id' },
  77. body: { broadcast: true },
  78. }
  79. const res = new MockResponse()
  80. const next = sinon.stub()
  81. await ctx.MetadataController.broadcastMetadataForDoc(req, res, next)
  82. ctx.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
  83. 'project-id'
  84. )
  85. res.json.should.not.have.been.called
  86. res.sendStatus.should.have.been.calledOnceWith(200)
  87. next.should.not.have.been.called
  88. ctx.EditorRealTimeController.emitToRoom.should.have.been.calledOnce
  89. const { lastCall } = ctx.EditorRealTimeController.emitToRoom
  90. expect(lastCall.args[0]).to.equal('project-id')
  91. expect(lastCall.args[1]).to.equal('broadcastDocMeta')
  92. expect(lastCall.args[2]).to.have.all.keys(['docId', 'meta'])
  93. })
  94. it('should return json on broadcast:false ', async function (ctx) {
  95. const docMeta = {
  96. labels: ['foo'],
  97. packages: { a: [] },
  98. packageNames: ['a'],
  99. }
  100. ctx.MetaHandler.promises.getMetaForDoc = sinon.stub().resolves(docMeta)
  101. ctx.EditorRealTimeController.emitToRoom = sinon.stub()
  102. const req = {
  103. params: { project_id: 'project-id', doc_id: 'doc-id' },
  104. body: { broadcast: false },
  105. }
  106. const res = new MockResponse()
  107. const next = sinon.stub()
  108. await ctx.MetadataController.broadcastMetadataForDoc(req, res, next)
  109. ctx.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
  110. 'project-id'
  111. )
  112. ctx.EditorRealTimeController.emitToRoom.should.not.have.been.called
  113. res.json.should.have.been.calledOnceWith({
  114. docId: 'doc-id',
  115. meta: docMeta,
  116. })
  117. next.should.not.have.been.called
  118. })
  119. it('should handle an error', async function (ctx) {
  120. ctx.MetaHandler.promises.getMetaForDoc = sinon
  121. .stub()
  122. .throws(new Error('woops'))
  123. ctx.EditorRealTimeController.emitToRoom = sinon.stub()
  124. const req = {
  125. params: { project_id: 'project-id', doc_id: 'doc-id' },
  126. body: { broadcast: true },
  127. }
  128. const res = new MockResponse()
  129. const next = sinon.stub()
  130. await ctx.MetadataController.broadcastMetadataForDoc(req, res, next)
  131. ctx.MetaHandler.promises.getMetaForDoc.should.have.been.calledWith(
  132. 'project-id'
  133. )
  134. res.json.should.not.have.been.called
  135. next.should.have.been.calledWithMatch(error => error instanceof Error)
  136. })
  137. })
  138. })