From 438dbb9332dbd4da4fc111af3108711bb34c24d4 Mon Sep 17 00:00:00 2001 From: d0ukan <81981301+d0ukan@users.noreply.github.com> Date: Sun, 22 Nov 2026 00:01:51 +0300 Subject: [PATCH] Keep automatic backups of contact-only edits --- src/services/codeplugSnapshots.ts | 4 ++-- tests/unit/codeplugSnapshots.test.ts | 29 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/unit/codeplugSnapshots.test.ts diff --git a/src/services/codeplugSnapshots.ts b/src/services/codeplugSnapshots.ts index 110f6da7..e904f045 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 00000000..35a21c66 --- /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(); + } +});