mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Move non-tool logic out of tool file.
This commit is contained in:
1 parent
945c4209e8
commit
23c3cead89
10 files changed
+345
-250
No files matched your search
@@ -0,0 +1,168 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import {
|
||||
addConsoleMessage,
|
||||
clearConsoleMessages,
|
||||
getConsoleMessages,
|
||||
getHasConsoleErrors,
|
||||
addErrorStateListener,
|
||||
removeErrorStateListener,
|
||||
ProjectPreviewConsoleError
|
||||
} from './consoleMessages';
|
||||
|
||||
describe('Console Messages System', () => {
|
||||
beforeEach(() => {
|
||||
clearConsoleMessages();
|
||||
});
|
||||
|
||||
describe('addConsoleMessage', () => {
|
||||
it('should add messages to the global store', () => {
|
||||
addConsoleMessage('log', 'Test log message');
|
||||
addConsoleMessage('error', 'Test error message');
|
||||
|
||||
const messages = getConsoleMessages();
|
||||
expect(messages).toHaveLength(2);
|
||||
expect(messages[0]).toEqual({ level: 'log', message: 'Test log message' });
|
||||
expect(messages[1]).toEqual({ level: 'error', message: 'Test error message' });
|
||||
});
|
||||
|
||||
it('should update error state when error message is added', () => {
|
||||
expect(getHasConsoleErrors()).toBe(false);
|
||||
|
||||
addConsoleMessage('log', 'Log message');
|
||||
expect(getHasConsoleErrors()).toBe(false);
|
||||
|
||||
addConsoleMessage('error', 'Error message');
|
||||
expect(getHasConsoleErrors()).toBe(true);
|
||||
});
|
||||
|
||||
it('should only trigger error state once for multiple errors', () => {
|
||||
const listener = vi.fn();
|
||||
addErrorStateListener(listener);
|
||||
|
||||
addConsoleMessage('error', 'First error');
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledWith(true);
|
||||
|
||||
addConsoleMessage('error', 'Second error');
|
||||
expect(listener).toHaveBeenCalledTimes(1); // Should not be called again
|
||||
|
||||
removeErrorStateListener(listener);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getConsoleMessages', () => {
|
||||
it('should return a copy of messages array', () => {
|
||||
addConsoleMessage('info', 'Test message');
|
||||
|
||||
const messages1 = getConsoleMessages();
|
||||
const messages2 = getConsoleMessages();
|
||||
|
||||
expect(messages1).toEqual(messages2);
|
||||
expect(messages1).not.toBe(messages2); // Different array instances
|
||||
});
|
||||
|
||||
it('should return empty array when no messages', () => {
|
||||
const messages = getConsoleMessages();
|
||||
expect(messages).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clearConsoleMessages', () => {
|
||||
it('should clear all messages and reset error state', () => {
|
||||
addConsoleMessage('error', 'Error message');
|
||||
addConsoleMessage('log', 'Log message');
|
||||
|
||||
expect(getConsoleMessages()).toHaveLength(2);
|
||||
expect(getHasConsoleErrors()).toBe(true);
|
||||
|
||||
clearConsoleMessages();
|
||||
|
||||
expect(getConsoleMessages()).toHaveLength(0);
|
||||
expect(getHasConsoleErrors()).toBe(false);
|
||||
});
|
||||
|
||||
it('should notify listeners when error state changes', () => {
|
||||
const listener = vi.fn();
|
||||
addErrorStateListener(listener);
|
||||
|
||||
addConsoleMessage('error', 'Error message');
|
||||
expect(listener).toHaveBeenCalledWith(true);
|
||||
|
||||
clearConsoleMessages();
|
||||
expect(listener).toHaveBeenCalledWith(false);
|
||||
expect(listener).toHaveBeenCalledTimes(2);
|
||||
|
||||
removeErrorStateListener(listener);
|
||||
});
|
||||
|
||||
it('should not notify listeners if no error state change', () => {
|
||||
const listener = vi.fn();
|
||||
addErrorStateListener(listener);
|
||||
|
||||
// No errors added, so clearing should not trigger listener
|
||||
clearConsoleMessages();
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
|
||||
removeErrorStateListener(listener);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error State Listeners', () => {
|
||||
it('should add and remove listeners correctly', () => {
|
||||
const listener1 = vi.fn();
|
||||
const listener2 = vi.fn();
|
||||
|
||||
addErrorStateListener(listener1);
|
||||
addErrorStateListener(listener2);
|
||||
|
||||
addConsoleMessage('error', 'Test error');
|
||||
|
||||
expect(listener1).toHaveBeenCalledWith(true);
|
||||
expect(listener2).toHaveBeenCalledWith(true);
|
||||
|
||||
removeErrorStateListener(listener1);
|
||||
clearConsoleMessages();
|
||||
|
||||
expect(listener1).toHaveBeenCalledTimes(1); // Only called once (not for clear)
|
||||
expect(listener2).toHaveBeenCalledTimes(2); // Called for both add and clear
|
||||
|
||||
removeErrorStateListener(listener2);
|
||||
});
|
||||
|
||||
it('should handle listener errors gracefully', () => {
|
||||
const faultyListener = vi.fn(() => {
|
||||
throw new Error('Listener error');
|
||||
});
|
||||
const workingListener = vi.fn();
|
||||
|
||||
addErrorStateListener(faultyListener);
|
||||
addErrorStateListener(workingListener);
|
||||
|
||||
// Should not throw despite faulty listener
|
||||
expect(() => {
|
||||
addConsoleMessage('error', 'Test error');
|
||||
}).not.toThrow();
|
||||
|
||||
expect(faultyListener).toHaveBeenCalled();
|
||||
expect(workingListener).toHaveBeenCalled();
|
||||
|
||||
removeErrorStateListener(faultyListener);
|
||||
removeErrorStateListener(workingListener);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ProjectPreviewConsoleError', () => {
|
||||
it('should create error with console logs', () => {
|
||||
const logs = [
|
||||
{ level: 'error' as const, message: 'Test error' }
|
||||
];
|
||||
const error = new ProjectPreviewConsoleError('Test message', logs);
|
||||
|
||||
expect(error.message).toBe('Test message');
|
||||
expect(error.name).toBe('ProjectPreviewConsoleError');
|
||||
expect(error.logs).toEqual(logs);
|
||||
expect(error instanceof Error).toBe(true);
|
||||
expect(error instanceof ProjectPreviewConsoleError).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user