import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { RtmManagerAdapter } from '../../src/managers/rtm-manager-adapter' import { SttError } from '../../src/core/stt-error' describe('RtmManagerAdapter', () => { let manager: RtmManagerAdapter beforeEach(() => { manager = new RtmManagerAdapter() }) afterEach(async () => { if (manager.isJoined) { await manager.destroy() } }) describe('join', () => { it('should join successfully with valid config', async () => { const config = { channel: 'test-channel', userId: 'test-user', userName: 'Test User', } await manager.join(config) expect(manager.isJoined).toBe(true) expect(manager.config).toEqual(config) expect(manager.userId).toBe('test-user') expect(manager.channel).toBe('test-channel') expect(manager.userList).toHaveLength(1) expect(manager.userList[0]).toEqual({ userId: 'test-user', userName: 'Test User', }) }) it('should throw error when config is invalid', async () => { const config = { channel: '', userId: '', userName: '', } await expect(manager.join(config)).rejects.toThrow(SttError) await expect(manager.join(config)).rejects.toThrow( 'Missing required configuration parameters' ) }) it('should throw error when already joined', async () => { const config = { channel: 'test-channel', userId: 'test-user', userName: 'Test User', } await manager.join(config) await expect(manager.join(config)).rejects.toThrow(SttError) await expect(manager.join(config)).rejects.toThrow( 'RTM manager is already joined to a channel' ) }) it('should emit connected event', async () => { const connectedHandler = vi.fn() manager.on('connected', connectedHandler) const config = { channel: 'test-channel', userId: 'test-user', userName: 'Test User', } await manager.join(config) expect(connectedHandler).toHaveBeenCalledTimes(1) expect(connectedHandler).toHaveBeenCalledWith({ channel: 'test-channel', userId: 'test-user', }) }) }) describe('updateSttData', () => { it('should update STT data when joined', async () => { await manager.join({ channel: 'test-channel', userId: 'test-user', userName: 'Test User', }) const updatingHandler = vi.fn() const updatedHandler = vi.fn() manager.on('metadataUpdating', updatingHandler) manager.on('metadataUpdated', updatedHandler) const data = { status: 'start', taskId: 'test-task-id', } await manager.updateSttData(data) expect(updatingHandler).toHaveBeenCalledTimes(1) expect(updatingHandler).toHaveBeenCalledWith({ data }) expect(updatedHandler).toHaveBeenCalledTimes(1) expect(updatedHandler).toHaveBeenCalledWith({ data }) }) it('should throw error when not joined', async () => { const data = { status: 'start', taskId: 'test-task-id', } await expect(manager.updateSttData(data)).rejects.toThrow(SttError) await expect(manager.updateSttData(data)).rejects.toThrow( 'RTM manager must be joined to a channel before updating STT data' ) }) }) describe('acquireLock', () => { it('should acquire lock when joined', async () => { await manager.join({ channel: 'test-channel', userId: 'test-user', userName: 'Test User', }) const acquiringHandler = vi.fn() const acquiredHandler = vi.fn() manager.on('lockAcquiring', acquiringHandler) manager.on('lockAcquired', acquiredHandler) await manager.acquireLock() expect(acquiringHandler).toHaveBeenCalledTimes(1) expect(acquiredHandler).toHaveBeenCalledTimes(1) }) it('should throw error when not joined', async () => { await expect(manager.acquireLock()).rejects.toThrow(SttError) await expect(manager.acquireLock()).rejects.toThrow( 'RTM manager must be joined to a channel before acquiring lock' ) }) }) describe('releaseLock', () => { it('should release lock when joined', async () => { await manager.join({ channel: 'test-channel', userId: 'test-user', userName: 'Test User', }) const releasingHandler = vi.fn() const releasedHandler = vi.fn() manager.on('lockReleasing', releasingHandler) manager.on('lockReleased', releasedHandler) await manager.releaseLock() expect(releasingHandler).toHaveBeenCalledTimes(1) expect(releasedHandler).toHaveBeenCalledTimes(1) }) it('should throw error when not joined', async () => { await expect(manager.releaseLock()).rejects.toThrow(SttError) await expect(manager.releaseLock()).rejects.toThrow( 'RTM manager must be joined to a channel before releasing lock' ) }) }) describe('destroy', () => { it('should destroy manager successfully', async () => { await manager.join({ channel: 'test-channel', userId: 'test-user', userName: 'Test User', }) const destroyingHandler = vi.fn() const destroyedHandler = vi.fn() manager.on('destroying', destroyingHandler) manager.on('destroyed', destroyedHandler) await manager.destroy() expect(manager.isJoined).toBe(false) expect(manager.config).toBeUndefined() expect(manager.userList).toHaveLength(0) expect(destroyingHandler).toHaveBeenCalledTimes(1) expect(destroyedHandler).toHaveBeenCalledTimes(1) }) it('should handle destroy when not joined', async () => { await expect(manager.destroy()).resolves.not.toThrow() }) }) })