Repository: cypress-io/cypress
File: packages/server/test/unit/cloud/studio/StudioLifecycleManager_spec.ts (TypeScript)

Code before:

      const onCallback = watcherOnStub.args[0][1]

      let mockStudioManagerPromise: Promise<StudioManager>
      const updatedStudioManager = {
        status: 'ENABLED',
        destroy: studioManagerDestroyStub,
      } as unknown as StudioManager

      studioLifecycleManager['createStudioManager'] = sinon.stub().callsFake(() => {
        mockStudioManagerPromise = new Promise((resolve) => {
          resolve(updatedStudioManager)
        })

        return mockStudioManagerPromise
      })

      await onCallback()

      expect(studioManagerDestroyStub).to.be.called

      expect(mockStudioManagerPromise).to.be.present
      expect(await mockStudioManagerPromise).to.equal(updatedStudioManager)
    })

    it('initializes the studio manager and sends debug data to the studio server', async () => {
      const debugData = {
        filePreprocessorHandlerText: 'handler text',
      }

      studioManagerSetupStub.callsFake((args) => {
        mockStudioManager.status = 'ENABLED'

        return Promise.resolve()
      })

      studioLifecycleManager.initializeStudioManager({
        cloudDataSource: mockCloudDataSource,
        ctx: mockCtx,
        cfg: mockCfg,
        debugData,
      })

      const studioReadyPromise = new Promise((resolve) => {
        studioLifecycleManager?.registerStudioReadyListener((studioManager) => {
          resolve(studioManager)
        })
      })

      const mockManifest = {
        'server/index.js': 'e1ed3dc8ba9eb8ece23914004b99ad97bba37e80a25d8b47c009e1e4948a6159',

Review comment (bug):
Let's just add the `debugData` to this test: `initializes the studio manager and registers it in the data context and sets up protocol when studio is enabled`.

Code after:

      expect(watcherOnStub).to.be.calledWith('change')

      const onCallback = watcherOnStub.args[0][1]

      let mockStudioManagerPromise: Promise<StudioManager>
      const updatedStudioManager = {
        status: 'ENABLED',
        destroy: studioManagerDestroyStub,
      } as unknown as StudioManager

      studioLifecycleManager['createStudioManager'] = sinon.stub().callsFake(() => {
        mockStudioManagerPromise = new Promise((resolve) => {
          resolve(updatedStudioManager)
        })

        return mockStudioManagerPromise
      })

      await onCallback()

      expect(studioManagerDestroyStub).to.be.called

      expect(mockStudioManagerPromise).to.be.present
      expect(await mockStudioManagerPromise).to.equal(updatedStudioManager)
    })

    it('throws an error when the studio server script is not found in the manifest', async () => {
      studioManagerSetupStub.callsFake((args) => {
        mockStudioManager.status = 'ENABLED'

        return Promise.resolve()
      })

      const reportErrorPromise = new Promise<void>((resolve) => {
        reportStudioErrorStub.callsFake((err) => {
          resolve()

          return undefined
        })
      })

      const mockManifest = {}

      ensureStudioBundleStub.resolves(mockManifest)

      studioLifecycleManager.initializeStudioManager({
        cloudDataSource: mockCloudDataSource,
        ctx: mockCtx,
        cfg: mockCfg,
        debugData: {},