mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 15:42:01 +03:00
Add tests and error handling for writeImageFile
Added try/catch error handling to writeImageFile in src/storage.ts and created comprehensive test coverage in src/storage.test.ts. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
37cfd2144b
commit
b6da4c246e
2 changed files with 31 additions and 2 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { logger } from "./logger";
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { isTauri, loadSettings, saveSettings, defaultSettingsData, loadAppData, saveAppData, flushAppData, setDataDir, runBackups, backupDeletedSheet, openBackupsFolder, chooseFolder, dataDirHasDocuments, migrateDataDir, fetchRates, fetchHistoricalRates } from './storage';
|
||||
import { isTauri, loadSettings, saveSettings, defaultSettingsData, loadAppData, saveAppData, flushAppData, setDataDir, runBackups, backupDeletedSheet, openBackupsFolder, chooseFolder, dataDirHasDocuments, migrateDataDir, fetchRates, fetchHistoricalRates, writeImageFile } from './storage';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
|
||||
vi.mock('@tauri-apps/api/core', () => ({
|
||||
|
|
@ -382,6 +382,30 @@ describe('storage', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('writeImageFile', () => {
|
||||
it('returns false when not in Tauri', async () => {
|
||||
const result = await writeImageFile('base64data');
|
||||
expect(invoke).not.toHaveBeenCalled();
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true when in Tauri and invoke succeeds', async () => {
|
||||
vi.stubGlobal('window', { __TAURI_INTERNALS__: {} });
|
||||
vi.mocked(invoke).mockResolvedValue(true);
|
||||
const result = await writeImageFile('base64data');
|
||||
expect(invoke).toHaveBeenCalledWith('write_image_file', { dataBase64: 'base64data' });
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false and logs warning if invoke fails', async () => {
|
||||
vi.stubGlobal('window', { __TAURI_INTERNALS__: {} });
|
||||
vi.mocked(invoke).mockRejectedValue(new Error('invoke error'));
|
||||
const result = await writeImageFile('base64data');
|
||||
expect(logger.warn).toHaveBeenCalledWith('writeImageFile failed', expect.any(Error));
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchHistoricalRates', () => {
|
||||
it('uses invoke when in Tauri', async () => {
|
||||
vi.stubGlobal('window', { __TAURI_INTERNALS__: {} });
|
||||
|
|
|
|||
|
|
@ -256,7 +256,12 @@ export async function fetchHistoricalRates(date: string): Promise<Record<string,
|
|||
|
||||
export async function writeImageFile(dataBase64: string): Promise<boolean> {
|
||||
if (!isTauri()) return false;
|
||||
return invoke("write_image_file", { dataBase64 });
|
||||
try {
|
||||
return await invoke("write_image_file", { dataBase64 });
|
||||
} catch (e) {
|
||||
logger.warn("writeImageFile failed", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- market data
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue