diff --git a/tests/unit/readaloud.test.ts b/tests/unit/readaloud.test.ts new file mode 100644 index 0000000..32aaaa1 --- /dev/null +++ b/tests/unit/readaloud.test.ts @@ -0,0 +1,69 @@ + +import { vi, beforeEach, expect, test } from 'vitest' +import { Notification } from 'electron' +import ReadAloud from '../../src/automations/readaloud' +import * as window from '../../src/main/window' + +// mock electron +vi.mock('electron', async() => { + const Notification = vi.fn(); + Notification.prototype.show = vi.fn(); + return { + Notification + } +}) + +// mock windows +vi.mock('../../src/main/window.ts', async () => { + return { + hideWindows: vi.fn(), + restoreWindows: vi.fn(), + releaseFocus: vi.fn(), + openReadAloudPalette: vi.fn(), + isMainWindowFocused: vi.fn(() => false), + } +}) + +vi.mock('../../src/main/utils', async () => { + return { + wait: vi.fn(), + putCachedText: vi.fn(() => 'textId') + } +}) + +// mock automator +vi.mock('../../src/automations/automator.ts', async () => { + let call = -1 + const Automator = vi.fn() + Automator.prototype.getSelectedText = vi.fn(() => { + if (++call === 0) return 'Grabbed text' + else if (call === 1) return '' + else return null + }) + return { default: Automator } +}) + +beforeEach(() => { + vi.clearAllMocks() +}) + +test('Open readaloud window', async () => { + await ReadAloud.read() + expect(window.hideWindows).toHaveBeenCalled() + expect(window.releaseFocus).toHaveBeenCalled() + expect(window.openReadAloudPalette).toHaveBeenCalledWith('textId') +}) + +test('Show no text error notification', async () => { + await ReadAloud.read() + expect(window.hideWindows).toHaveBeenCalled() + expect(window.releaseFocus).toHaveBeenCalled() + expect(Notification).toHaveBeenCalledWith({ title: 'Witsy', body: 'Please highlight the text you want to read aloud' }) +}) + +test('Show no grab error notification', async () => { + await ReadAloud.read() + expect(window.hideWindows).toHaveBeenCalled() + expect(window.releaseFocus).toHaveBeenCalled() + expect(Notification).toHaveBeenCalledWith({ title: 'Witsy', body: 'An error occurred while trying to grab the text. Please check Privacy & Security settings.' }) +}) diff --git a/tests/unit/transcriber.test.ts b/tests/unit/transcriber.test.ts new file mode 100644 index 0000000..b44c6f4 --- /dev/null +++ b/tests/unit/transcriber.test.ts @@ -0,0 +1,43 @@ + +import { vi, beforeEach, expect, test } from 'vitest' +import Transcriber from '../../src/automations/transcriber' +import Automator from '../../src/automations/automator' +import * as window from '../../src/main/window' + +// mock windows +vi.mock('../../src/main/window.ts', async () => { + return { + hideWindows: vi.fn(), + restoreWindows: vi.fn(), + releaseFocus: vi.fn(), + openTranscribePalette: vi.fn(), + closeTranscribePalette: vi.fn(), + isMainWindowFocused: vi.fn(() => false), + } +}) + +// mock automator +vi.mock('../../src/automations/automator.ts', async () => { + const Automator = vi.fn() + Automator.prototype.pasteText = vi.fn() + return { default: Automator } +}) + + +beforeEach(() => { + vi.clearAllMocks() +}) + +test('Open transcriber window', async () => { + await Transcriber.initTranscription() + expect(window.hideWindows).toHaveBeenCalled() + expect(window.releaseFocus).toHaveBeenCalled() + expect(window.openTranscribePalette).toHaveBeenCalledWith() +}) + +test('Insert transcription', async () => { + await Transcriber.insertTranscription('Hello, World!') + expect(window.closeTranscribePalette).toHaveBeenCalled() + expect(window.releaseFocus).toHaveBeenCalled() + expect(Automator.prototype.pasteText).toHaveBeenCalledWith('Hello, World!') +}) diff --git a/vitest.config.mjs b/vitest.config.mjs index a145890..b57c317 100644 --- a/vitest.config.mjs +++ b/vitest.config.mjs @@ -27,7 +27,9 @@ export default defineConfig({ '*.config.ts', 'build/*', 'src/*.ts', + 'src/automations/computer*.ts', 'src/automations/macos*.ts', + 'src/automations/nut*.ts', 'src/automations/robot.ts', 'src/automations/windows.ts', 'src/services/*worker.ts',