Persistence: share entity write-through helper; normalize quota at db.put

This commit is contained in:
2026-06-16 23:06:37 +03:00
parent 778b4d848a
commit 2ed9db3792
18 changed files with 422 additions and 205 deletions
+42 -2
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { StorageQuotaError } from '../infrastructure/snippet-store';
import { storageErrorNotification } from './storage-errors';
import { StorageQuotaError } from '../infrastructure/db';
import { entityStorageErrorNotification, storageErrorNotification } from './storage-errors';
describe('storageErrorNotification', () => {
describe('a failure the user can fix (storage full)', () => {
@@ -43,3 +43,43 @@ describe('storageErrorNotification', () => {
});
});
});
describe('entityStorageErrorNotification', () => {
it('names the entity in the title, message, and diagnostic', () => {
const save = entityStorageErrorNotification('dataset', 'save', new Error('boom'));
expect(save.kind).toBe('error');
expect(save.title).toBe("Couldn't save the dataset");
expect(save.message).toContain('dataset change');
expect(save.detail).toBe('Dataset save failed: Error: boom'); // capitalized noun in trace
const del = entityStorageErrorNotification('font', 'delete', new Error('boom'));
expect(del.title).toBe("Couldn't delete the font");
expect(del.detail).toContain('Font delete failed');
});
it('detects a quota failure for any entity tier (not just snippets)', () => {
const n = entityStorageErrorNotification('dataset', 'save', new StorageQuotaError());
expect(n.title).toBe('Storage full');
expect(n.message.toLowerCase()).toContain('dataset');
expect(n.message.toLowerCase()).toContain('delete');
expect(n.detail).toBeUndefined(); // nothing to report — the fix is in the message
});
it('frames a load failure with the entity noun, not "snippets"', () => {
const n = entityStorageErrorNotification(
'dataset',
'load',
new DOMException('blocked', 'SecurityError'),
);
expect(n.title).toBe("Couldn't open your datasets");
expect(n.message).toContain('datasets');
expect(n.message).not.toContain('snippet');
expect(n.detail).toContain('load');
expect(n.detail).toContain('SecurityError');
});
it('describes a non-Error throw without crashing', () => {
const n = entityStorageErrorNotification('theme', 'save', 'weird string');
expect(n.detail).toBe('weird string');
});
});