Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/services/codeplugSnapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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 });
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/codeplugSnapshots.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>();
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();
}
});
Loading