diff --git a/src/services/codeplugSnapshots.ts b/src/services/codeplugSnapshots.ts index 110f6da..e904f04 100644 --- a/src/services/codeplugSnapshots.ts +++ b/src/services/codeplugSnapshots.ts @@ -119,9 +119,9 @@ function buildSnapshotLabel(options: SaveSnapshotOptions): string { } } -/** Save a codeplug snapshot. Skips empty codeplugs (0 channels, 0 zones). */ +/** Automatic backups can also contain only contacts or other unsaved tables. */ export async function saveSnapshot(data: CodeplugData, options: SaveSnapshotOptions): Promise { - if (data.channels.length === 0 && data.zones.length === 0) return; + if (options.eventType !== 'backup' && data.channels.length === 0 && data.zones.length === 0) return; const radioModel = options.radioModel ?? data.radioInfo?.model; const label = buildSnapshotLabel({ ...options, radioModel }); diff --git a/tests/unit/codeplugSnapshots.test.ts b/tests/unit/codeplugSnapshots.test.ts new file mode 100644 index 0000000..35a21c6 --- /dev/null +++ b/tests/unit/codeplugSnapshots.test.ts @@ -0,0 +1,29 @@ +import { expect, it, vi } from 'vitest'; +import { getSnapshotData, getSnapshots } from '../../src/services/codeplugSnapshots'; +import { backupUnsavedEdits, trackUnsavedEdits } from '../../src/services/unsavedEdits'; +import { useContactsStore } from '../../src/store/contactsStore'; +import { useUnsavedChangesStore } from '../../src/store/unsavedChangesStore'; + +it('backs up unsaved contacts even when the codeplug has no channels or zones', async () => { + const storage = new Map(); + vi.stubGlobal('localStorage', { + getItem: (key: string) => storage.get(key) ?? null, + setItem: (key: string, value: string) => storage.set(key, value), + }); + const stopTracking = trackUnsavedEdits(); + const contact = { id: 1, name: 'Test contact', dmrId: 1234567 }; + try { + useContactsStore.getState().setContacts([contact]); + await backupUnsavedEdits('reading the radio'); + useContactsStore.getState().setContacts([]); + + const [snapshot] = getSnapshots(); + expect(snapshot?.eventType).toBe('backup'); + expect((await getSnapshotData(snapshot.id))?.contacts).toEqual([contact]); + } finally { + stopTracking(); + useContactsStore.getState().setContacts([]); + useUnsavedChangesStore.getState().markClean(); + vi.unstubAllGlobals(); + } +});