Show size and a draft indicator in snippet-library rows

This commit is contained in:
2026-06-05 14:11:00 +03:00
parent da20475d2f
commit ad239d7d49
4 changed files with 138 additions and 50 deletions
+35
View File
@@ -2,10 +2,12 @@ import { describe, expect, test } from 'vitest';
import {
CURRENT_SNIPPET_VERSION,
createSnippet,
formatSnippetSize,
generateSnippetName,
hasUnpublishedChanges,
SAMPLE_SPEC,
sampleSpecText,
snippetSizeBytes,
} from './snippet';
describe('createSnippet', () => {
@@ -72,3 +74,36 @@ describe('hasUnpublishedChanges', () => {
expect(hasUnpublishedChanges({ ...s, draftSpec: s.spec + ' ' })).toBe(true);
});
});
describe('snippetSizeBytes', () => {
test('measures the draft as UTF-8 byte length, not character count', () => {
const s = createSnippet({ spec: 'abc' });
expect(snippetSizeBytes(s)).toBe(3);
// A 3-character string of multibyte glyphs is more than 3 bytes.
expect(snippetSizeBytes({ ...s, draftSpec: '€€€' })).toBe(9);
});
test('reflects the working draft, not the published spec', () => {
const s = createSnippet({ spec: 'ab' });
expect(snippetSizeBytes({ ...s, draftSpec: 'abcdef' })).toBe(6);
});
});
describe('formatSnippetSize', () => {
test('omits the size under ~1 KB (spec §02 clutter rule)', () => {
expect(formatSnippetSize(0)).toBeNull();
expect(formatSnippetSize(512)).toBeNull();
expect(formatSnippetSize(1023)).toBeNull();
});
test('shows whole KB at and above the threshold', () => {
expect(formatSnippetSize(1024)).toBe('1 KB');
expect(formatSnippetSize(2048)).toBe('2 KB');
expect(formatSnippetSize(1536)).toBe('2 KB'); // rounds to nearest KB
});
test('rolls over to MB for large payloads', () => {
expect(formatSnippetSize(1024 * 1024)).toBe('1 MB');
expect(formatSnippetSize(3 * 1024 * 1024)).toBe('3 MB');
});
});