Files
bitvid/tests/ui/profileModalController.test.js
T
2025-11-03 19:04:52 -05:00

2 lines
3.4 KiB
JavaScript

import { ProfileModalController } from '../../js/ui/profileModalController.js';
import { userLogger } from '../../js/utils/logger.js';

describe('ProfileModalController - Direct Messages Error Handling', () => {
 let controller;
 let mockUserLoggerWarn;

 beforeEach(() => {
 jest.spyOn(userLogger, 'warn').mockImplementation(() => {});
 mockUserLoggerWarn = jest.spyOn(userLogger, 'warn');
 controller = new ProfileModalController({ /* minimal mocks for services/state */ });
 jest.spyOn(controller, 'setMessagesLoadingState');
 jest.spyOn(controller, 'setMessagesAnnouncement');
 jest.spyOn(controller, 'updateMessagesReloadState');
 controller.directMessagesCache = []; // Empty for error state
 });

 afterEach(() => {
 jest.restoreAllMocks();
 });

 it('handles "no-decryptors" with specific message, no warn log, and enables reload', () => {
 const detail = {
 context: { reason: 'no-decryptors' },
 error: new Error('Mock signer unlock error')
 };
 controller.handleDirectMessagesError(detail);

 expect(controller.setMessagesLoadingState).toHaveBeenCalledWith('error', {
 message: 'Unlock your signer to read direct messages.'
 });
 expect(mockUserLoggerWarn).not.toHaveBeenCalled(); // No log for this reason
 expect(controller.updateMessagesReloadState).toHaveBeenCalled(); // Reload affordance
 });

 it('handles other reasons with generic message, warn log, and enables reload', () => {
 const detail = {
 context: { reason: 'relay-failed' }, // Mock real transport error
 error: new Error('Mock relay error')
 };
 controller.handleDirectMessagesError(detail);

 expect(controller.setMessagesLoadingState).toHaveBeenCalledWith('error', {
 message: 'Unable to sync direct messages right now.'
 });
 expect(mockUserLoggerWarn).toHaveBeenCalledWith(
 '[profileModal] Direct message sync issue detected:',
 detail.error
 ); // Warn for transport errors
 expect(controller.updateMessagesReloadState).toHaveBeenCalled(); // Reload affordance
 });

 it('handles non-empty cache for "no-decryptors" (announcement only, no error state)', () => {
 controller.directMessagesCache = ['mock-message']; // Non-empty
 const detail = { context: { reason: 'no-decryptors' } };
 controller.handleDirectMessagesError(detail);

 // Uses announcement, not error state
 expect(controller.setMessagesAnnouncement).toHaveBeenCalledWith('Unable to sync direct messages right now.');
 expect(mockUserLoggerWarn).not.toHaveBeenCalled();
 });

 it('maintains behavior for active requests (ignores detail, no state change)', () => {
 controller.activeMessagesRequest = Symbol('active');
 const detail = { context: { reason: 'no-decryptors' } };
 controller.handleDirectMessagesError(detail);

 expect(controller.setMessagesLoadingState).not.toHaveBeenCalled();
 expect(controller.setMessagesAnnouncement).not.toHaveBeenCalled();
 expect(mockUserLoggerWarn).toHaveBeenCalled(); // Still logs for diagnostics
 });

});

// Adjust existing tests to cover "no-decryptors" in DM flows (e.g., error simulation in populateProfileMessages)