fix(pwa): guard electron app state calls

This commit is contained in:
4gray committed 2026-05-15 17:33:06 +02:00
1 parent 9af1576e84
commit 0878905da0
2 files changed
+56

No files matched your search

@@ -0,0 +1,48 @@
import { DatabaseService } from './database-electron.service';
describe('DatabaseService', () => {
let service: DatabaseService;
let originalElectron: typeof window.electron;
const setElectron = (electron: typeof window.electron | undefined) => {
(window as unknown as { electron?: typeof window.electron }).electron =
electron;
};
beforeEach(() => {
service = new DatabaseService();
originalElectron = window.electron;
});
afterEach(() => {
setElectron(originalElectron);
jest.restoreAllMocks();
});
it('skips Electron app-state reads when the preload API is unavailable', async () => {
setElectron(undefined);
await expect(service.getAppState('key')).resolves.toBeNull();
});
it('skips Electron app-state writes when the preload API is unavailable', async () => {
setElectron(undefined);
await expect(service.setAppState('key', 'value')).resolves.toBe(false);
});
it('delegates Electron app-state calls when the preload API is available', async () => {
setElectron({
dbGetAppState: jest.fn().mockResolvedValue('stored'),
dbSetAppState: jest.fn().mockResolvedValue(undefined),
} as unknown as typeof window.electron);
await expect(service.getAppState('key')).resolves.toBe('stored');
await expect(service.setAppState('key', 'value')).resolves.toBe(true);
expect(window.electron.dbGetAppState).toHaveBeenCalledWith('key');
expect(window.electron.dbSetAppState).toHaveBeenCalledWith(
'key',
'value'
);
});
});
@@ -506,6 +506,10 @@ export class DatabaseService {
}
async getAppState(key: string): Promise<string | null> {
if (typeof window.electron?.dbGetAppState !== 'function') {
return null;
}
try {
return await window.electron.dbGetAppState(key);
} catch (error) {
@@ -515,6 +519,10 @@ export class DatabaseService {
}
async setAppState(key: string, value: string): Promise<boolean> {
if (typeof window.electron?.dbSetAppState !== 'function') {
return false;
}
try {
await window.electron.dbSetAppState(key, value);
return true;