nebula-cms 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +42 -0
- package/.github/workflows/ci.yml +31 -0
- package/.mcp.json +12 -0
- package/.prettierignore +5 -0
- package/.prettierrc.cjs +22 -0
- package/AGENTS.md +183 -0
- package/LICENSE +201 -0
- package/README.md +128 -0
- package/package.json +74 -0
- package/playground/.claude/settings.local.json +5 -0
- package/playground/astro.config.mjs +7 -0
- package/playground/node_modules/.bin/astro +21 -0
- package/playground/node_modules/.bin/rollup +21 -0
- package/playground/node_modules/.bin/tsc +21 -0
- package/playground/node_modules/.bin/tsserver +21 -0
- package/playground/node_modules/.bin/vite +21 -0
- package/playground/node_modules/.vite/_svelte_metadata.json +1 -0
- package/playground/node_modules/.vite/deps/@astrojs_svelte_client__js.js +80 -0
- package/playground/node_modules/.vite/deps/@astrojs_svelte_client__js.js.map +7 -0
- package/playground/node_modules/.vite/deps/_metadata.json +184 -0
- package/playground/node_modules/.vite/deps/astro___aria-query.js +6776 -0
- package/playground/node_modules/.vite/deps/astro___aria-query.js.map +7 -0
- package/playground/node_modules/.vite/deps/astro___axobject-query.js +3754 -0
- package/playground/node_modules/.vite/deps/astro___axobject-query.js.map +7 -0
- package/playground/node_modules/.vite/deps/astro___html-escaper.js +34 -0
- package/playground/node_modules/.vite/deps/astro___html-escaper.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-AJXJMYAF.js +0 -0
- package/playground/node_modules/.vite/deps/chunk-AJXJMYAF.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-ALJIOON6.js +1005 -0
- package/playground/node_modules/.vite/deps/chunk-ALJIOON6.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-BUSYA2B4.js +8 -0
- package/playground/node_modules/.vite/deps/chunk-BUSYA2B4.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-CNYJBM5F.js +21 -0
- package/playground/node_modules/.vite/deps/chunk-CNYJBM5F.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-DBPNBGEI.js +223 -0
- package/playground/node_modules/.vite/deps/chunk-DBPNBGEI.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-G3C2FXJT.js +204 -0
- package/playground/node_modules/.vite/deps/chunk-G3C2FXJT.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-GKDKFWC5.js +27 -0
- package/playground/node_modules/.vite/deps/chunk-GKDKFWC5.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-HNCLEOC5.js +4376 -0
- package/playground/node_modules/.vite/deps/chunk-HNCLEOC5.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-JICYXBFU.js +688 -0
- package/playground/node_modules/.vite/deps/chunk-JICYXBFU.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-KCUTL6DD.js +5099 -0
- package/playground/node_modules/.vite/deps/chunk-KCUTL6DD.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-ZP4UNCSN.js +23 -0
- package/playground/node_modules/.vite/deps/chunk-ZP4UNCSN.js.map +7 -0
- package/playground/node_modules/.vite/deps/chunk-ZREFNRZZ.js +148 -0
- package/playground/node_modules/.vite/deps/chunk-ZREFNRZZ.js.map +7 -0
- package/playground/node_modules/.vite/deps/package.json +3 -0
- package/playground/node_modules/.vite/deps/smol-toml.js +843 -0
- package/playground/node_modules/.vite/deps/smol-toml.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte.js +55 -0
- package/playground/node_modules/.vite/deps/svelte.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte___clsx.js +9 -0
- package/playground/node_modules/.vite/deps/svelte___clsx.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_animate.js +57 -0
- package/playground/node_modules/.vite/deps/svelte_animate.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_attachments.js +15 -0
- package/playground/node_modules/.vite/deps/svelte_attachments.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_easing.js +67 -0
- package/playground/node_modules/.vite/deps/svelte_easing.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_events.js +11 -0
- package/playground/node_modules/.vite/deps/svelte_events.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal.js +5 -0
- package/playground/node_modules/.vite/deps/svelte_internal.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_client.js +402 -0
- package/playground/node_modules/.vite/deps/svelte_internal_client.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_disclose-version.js +10 -0
- package/playground/node_modules/.vite/deps/svelte_internal_disclose-version.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_async.js +8 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_async.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_legacy.js +8 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_legacy.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_tracing.js +8 -0
- package/playground/node_modules/.vite/deps/svelte_internal_flags_tracing.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_legacy.js +35 -0
- package/playground/node_modules/.vite/deps/svelte_legacy.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_motion.js +545 -0
- package/playground/node_modules/.vite/deps/svelte_motion.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity.js +29 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity_window.js +127 -0
- package/playground/node_modules/.vite/deps/svelte_reactivity_window.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_store.js +103 -0
- package/playground/node_modules/.vite/deps/svelte_store.js.map +7 -0
- package/playground/node_modules/.vite/deps/svelte_transition.js +208 -0
- package/playground/node_modules/.vite/deps/svelte_transition.js.map +7 -0
- package/playground/package.json +16 -0
- package/playground/pnpm-lock.yaml +3167 -0
- package/playground/src/content/authors/jane-doe.json +8 -0
- package/playground/src/content/config/build.toml +2 -0
- package/playground/src/content/courses/web-fundamentals.json +29 -0
- package/playground/src/content/docs/advanced.mdx +6 -0
- package/playground/src/content/docs/intro.md +6 -0
- package/playground/src/content/guides/getting-started.mdx +6 -0
- package/playground/src/content/posts/hello-world.md +7 -0
- package/playground/src/content/products/t-shirt.json +16 -0
- package/playground/src/content/recipes/pancakes.mdoc +8 -0
- package/playground/src/content/settings/site.yml +2 -0
- package/playground/src/content.config.ts +198 -0
- package/playground/src/env.d.ts +1 -0
- package/playground/src/pages/index.astro +11 -0
- package/playground/src/pages/nebula.astro +14 -0
- package/pnpm-workspace.yaml +2 -0
- package/scripts/subset-icons.mjs +178 -0
- package/src/astro/index.ts +295 -0
- package/src/client/Admin.svelte +283 -0
- package/src/client/components/BackendPicker.svelte +291 -0
- package/src/client/components/DraftChip.svelte +46 -0
- package/src/client/components/MetadataForm.svelte +56 -0
- package/src/client/components/ThemeToggle.svelte +18 -0
- package/src/client/components/dialogs/DeleteDraftDialog.svelte +51 -0
- package/src/client/components/dialogs/FilenameDialog.svelte +129 -0
- package/src/client/components/editor/EditorPane.svelte +227 -0
- package/src/client/components/editor/EditorTabs.svelte +81 -0
- package/src/client/components/editor/EditorToolbar.svelte +131 -0
- package/src/client/components/editor/FormatSelector.svelte +66 -0
- package/src/client/components/editor/Toolbar.svelte +17 -0
- package/src/client/components/fields/ArrayField.svelte +339 -0
- package/src/client/components/fields/ArrayItem.svelte +325 -0
- package/src/client/components/fields/BooleanField.svelte +114 -0
- package/src/client/components/fields/DateField.svelte +82 -0
- package/src/client/components/fields/EnumField.svelte +74 -0
- package/src/client/components/fields/FieldWrapper.svelte +96 -0
- package/src/client/components/fields/NumberField.svelte +99 -0
- package/src/client/components/fields/ObjectField.svelte +121 -0
- package/src/client/components/fields/SchemaField.svelte +107 -0
- package/src/client/components/fields/StringField.svelte +104 -0
- package/src/client/components/sidebar/AdminSidebar.svelte +339 -0
- package/src/client/components/sidebar/AdminSidebarSort.svelte +123 -0
- package/src/client/css/a11y.css +14 -0
- package/src/client/css/btn.css +113 -0
- package/src/client/css/dialog.css +29 -0
- package/src/client/css/field-input.css +39 -0
- package/src/client/css/reset.css +59 -0
- package/src/client/css/theme.css +77 -0
- package/src/client/index.ts +1 -0
- package/src/client/js/drafts/merge.svelte.ts +121 -0
- package/src/client/js/drafts/ops.svelte.ts +227 -0
- package/src/client/js/drafts/storage.ts +108 -0
- package/src/client/js/drafts/workers/diff.ts +40 -0
- package/src/client/js/editor/editor.svelte.ts +343 -0
- package/src/client/js/editor/languages.ts +98 -0
- package/src/client/js/editor/link-wrap.ts +45 -0
- package/src/client/js/editor/markdown-shortcuts.ts +261 -0
- package/src/client/js/handlers/admin.ts +246 -0
- package/src/client/js/state/dialogs.svelte.ts +35 -0
- package/src/client/js/state/router.svelte.ts +156 -0
- package/src/client/js/state/schema.svelte.ts +140 -0
- package/src/client/js/state/state.svelte.ts +334 -0
- package/src/client/js/state/theme.svelte.ts +173 -0
- package/src/client/js/storage/adapter.ts +102 -0
- package/src/client/js/storage/client.ts +150 -0
- package/src/client/js/storage/db.ts +36 -0
- package/src/client/js/storage/fsa.ts +110 -0
- package/src/client/js/storage/github.ts +297 -0
- package/src/client/js/storage/storage.ts +83 -0
- package/src/client/js/storage/workers/frontmatter.ts +320 -0
- package/src/client/js/storage/workers/storage.ts +177 -0
- package/src/client/js/storage/workers/toml-parser.ts +106 -0
- package/src/client/js/storage/workers/yaml-parser.ts +132 -0
- package/src/client/js/utils/file-types.ts +192 -0
- package/src/client/js/utils/format.ts +16 -0
- package/src/client/js/utils/frontmatter.ts +38 -0
- package/src/client/js/utils/schema-utils.ts +295 -0
- package/src/client/js/utils/slug.ts +18 -0
- package/src/client/js/utils/sort.ts +84 -0
- package/src/client/js/utils/stable-stringify.ts +27 -0
- package/src/client/js/utils/url-utils.ts +38 -0
- package/src/types.ts +25 -0
- package/src/virtual.d.ts +22 -0
- package/svelte.config.js +4 -0
- package/tests/astro/build.test.ts +63 -0
- package/tests/astro/index.test.ts +689 -0
- package/tests/client/components/Admin.test.ts +446 -0
- package/tests/client/components/BackendPicker.test.ts +239 -0
- package/tests/client/components/DraftChip.test.ts +53 -0
- package/tests/client/components/MetadataForm.test.ts +164 -0
- package/tests/client/components/dialogs/DeleteDraftDialog.test.ts +91 -0
- package/tests/client/components/dialogs/FilenameDialog.test.ts +209 -0
- package/tests/client/components/dialogs/dialog-stubs.ts +19 -0
- package/tests/client/components/editor/EditorPane.test.ts +100 -0
- package/tests/client/components/editor/EditorTabs.test.ts +253 -0
- package/tests/client/components/editor/EditorToolbar.test.ts +252 -0
- package/tests/client/components/editor/fixtures.ts +31 -0
- package/tests/client/components/fields/ArrayField.test.ts +197 -0
- package/tests/client/components/fields/BooleanField.test.ts +206 -0
- package/tests/client/components/fields/DateField.test.ts +210 -0
- package/tests/client/components/fields/EnumField.test.ts +246 -0
- package/tests/client/components/fields/NumberField.test.ts +240 -0
- package/tests/client/components/fields/ObjectField.test.ts +157 -0
- package/tests/client/components/fields/SchemaField.test.ts +190 -0
- package/tests/client/components/fields/StringField.test.ts +223 -0
- package/tests/client/components/sidebar/AdminSidebar.test.ts +285 -0
- package/tests/client/components/sidebar/AdminSidebarSort.test.ts +135 -0
- package/tests/client/components/sidebar/sort-mock.ts +23 -0
- package/tests/client/js/drafts/fixtures.ts +22 -0
- package/tests/client/js/drafts/merge.test.ts +282 -0
- package/tests/client/js/drafts/ops.test.ts +658 -0
- package/tests/client/js/drafts/storage.test.ts +200 -0
- package/tests/client/js/drafts/workers/diff.test.ts +165 -0
- package/tests/client/js/editor/editor.test.ts +616 -0
- package/tests/client/js/editor/link-wrap.test.ts +225 -0
- package/tests/client/js/editor/markdown-shortcuts.test.ts +370 -0
- package/tests/client/js/handlers/admin.test.ts +467 -0
- package/tests/client/js/state/router.test.ts +619 -0
- package/tests/client/js/state/schema.test.ts +266 -0
- package/tests/client/js/state/state.test.ts +328 -0
- package/tests/client/js/storage/adapter.test.ts +115 -0
- package/tests/client/js/storage/client.test.ts +250 -0
- package/tests/client/js/storage/db.test.ts +59 -0
- package/tests/client/js/storage/fsa.test.ts +284 -0
- package/tests/client/js/storage/github.test.ts +349 -0
- package/tests/client/js/storage/mock-port.ts +95 -0
- package/tests/client/js/storage/storage.test.ts +77 -0
- package/tests/client/js/storage/workers/frontmatter.test.ts +479 -0
- package/tests/client/js/storage/workers/storage.test.ts +299 -0
- package/tests/client/js/storage/workers/toml-parser.test.ts +169 -0
- package/tests/client/js/storage/workers/yaml-parser.test.ts +168 -0
- package/tests/client/js/utils/file-types.test.ts +268 -0
- package/tests/client/js/utils/frontmatter.test.ts +87 -0
- package/tests/client/js/utils/schema-utils.test.ts +318 -0
- package/tests/client/js/utils/slug.test.ts +58 -0
- package/tests/client/js/utils/sort.test.ts +276 -0
- package/tests/client/js/utils/stable-stringify.test.ts +68 -0
- package/tests/client/js/utils/url-utils.test.ts +70 -0
- package/tests/e2e/backend-connection.test.ts +301 -0
- package/tests/e2e/draft-lifecycle.test.ts +388 -0
- package/tests/e2e/editing.test.ts +355 -0
- package/tests/e2e/github-adapter.test.ts +330 -0
- package/tests/e2e/helpers/mock-adapter.ts +166 -0
- package/tests/e2e/helpers/test-app.ts +155 -0
- package/tests/e2e/navigation.test.ts +358 -0
- package/tests/e2e/publishing.test.ts +345 -0
- package/tests/e2e/unsaved-changes.test.ts +317 -0
- package/tests/setup.ts +2 -0
- package/tests/stubs/codemirror.ts +197 -0
- package/tsconfig.json +19 -0
- package/vitest.config.ts +178 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { slugify } from '../../../../src/client/js/utils/slug';
|
|
3
|
+
|
|
4
|
+
describe('slugify', () => {
|
|
5
|
+
it('lowercases the input', () => {
|
|
6
|
+
expect(slugify('Hello World')).toBe('hello-world');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('replaces spaces with hyphens', () => {
|
|
10
|
+
expect(slugify('foo bar baz')).toBe('foo-bar-baz');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('replaces special characters with hyphens', () => {
|
|
14
|
+
expect(slugify('hello! world?')).toBe('hello-world');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('collapses consecutive hyphens into one', () => {
|
|
18
|
+
expect(slugify('foo---bar')).toBe('foo-bar');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('trims leading hyphens', () => {
|
|
22
|
+
expect(slugify('---foo')).toBe('foo');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('trims trailing hyphens', () => {
|
|
26
|
+
expect(slugify('foo---')).toBe('foo');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('handles unicode characters by replacing them with hyphens', () => {
|
|
30
|
+
expect(slugify('café')).toBe('caf');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('handles strings with only special characters', () => {
|
|
34
|
+
expect(slugify('!@#$%')).toBe('');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('returns an empty string for an empty input', () => {
|
|
38
|
+
expect(slugify('')).toBe('');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('leaves an already-slugified string unchanged', () => {
|
|
42
|
+
expect(slugify('already-a-slug')).toBe('already-a-slug');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('handles numeric characters', () => {
|
|
46
|
+
expect(slugify('Post 42')).toBe('post-42');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('handles mixed alphanumeric and special characters', () => {
|
|
50
|
+
expect(slugify('My Post: The (2024) Edition!')).toBe(
|
|
51
|
+
'my-post-the-2024-edition',
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('preserves existing hyphens in the middle of words', () => {
|
|
56
|
+
expect(slugify('well-known')).toBe('well-known');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
toSortDate,
|
|
4
|
+
createComparator,
|
|
5
|
+
readSortMode,
|
|
6
|
+
writeSortMode,
|
|
7
|
+
SORT_MODES,
|
|
8
|
+
SORT_ORDER,
|
|
9
|
+
} from '../../../../src/client/js/utils/sort';
|
|
10
|
+
import type { SidebarItem } from '../../../../src/client/js/utils/sort';
|
|
11
|
+
|
|
12
|
+
//////////////////////////////
|
|
13
|
+
// localStorage mock helpers
|
|
14
|
+
//////////////////////////////
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Minimal in-memory localStorage mock. The unit test environment runs in Node
|
|
18
|
+
* which has no built-in localStorage, so we stub it globally for the tests
|
|
19
|
+
* that exercise readSortMode / writeSortMode.
|
|
20
|
+
*/
|
|
21
|
+
function makeLocalStorageMock(): Storage {
|
|
22
|
+
let store: Record<string, string> = {};
|
|
23
|
+
return {
|
|
24
|
+
getItem: (key: string) => store[key] ?? null,
|
|
25
|
+
setItem: (key: string, value: string) => {
|
|
26
|
+
store[key] = value;
|
|
27
|
+
},
|
|
28
|
+
removeItem: (key: string) => {
|
|
29
|
+
delete store[key];
|
|
30
|
+
},
|
|
31
|
+
clear: () => {
|
|
32
|
+
store = {};
|
|
33
|
+
},
|
|
34
|
+
get length() {
|
|
35
|
+
return Object.keys(store).length;
|
|
36
|
+
},
|
|
37
|
+
key: (index: number) => Object.keys(store)[index] ?? null,
|
|
38
|
+
} as Storage;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//////////////////////////////
|
|
42
|
+
// toSortDate
|
|
43
|
+
//////////////////////////////
|
|
44
|
+
|
|
45
|
+
describe('toSortDate', () => {
|
|
46
|
+
it('returns a Date unchanged when passed a Date instance', () => {
|
|
47
|
+
const d = new Date('2024-01-15');
|
|
48
|
+
expect(toSortDate(d)).toBe(d);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('parses a valid ISO date string into a Date', () => {
|
|
52
|
+
const result = toSortDate('2024-06-01');
|
|
53
|
+
expect(result).toBeInstanceOf(Date);
|
|
54
|
+
expect(result?.getFullYear()).toBe(2024);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('parses a valid ISO datetime string into a Date', () => {
|
|
58
|
+
const result = toSortDate('2024-06-01T12:00:00Z');
|
|
59
|
+
expect(result).toBeInstanceOf(Date);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('returns a Date (possibly Invalid) for a non-date string — callers handle invalid dates', () => {
|
|
63
|
+
// The function returns new Date(str) for any string; invalid date strings
|
|
64
|
+
// yield an Invalid Date object rather than undefined.
|
|
65
|
+
const result = toSortDate('not-a-date');
|
|
66
|
+
expect(result).toBeInstanceOf(Date);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('returns undefined for null', () => {
|
|
70
|
+
expect(toSortDate(null)).toBeUndefined();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('returns undefined for undefined', () => {
|
|
74
|
+
expect(toSortDate(undefined)).toBeUndefined();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('returns undefined for a number', () => {
|
|
78
|
+
expect(toSortDate(42)).toBeUndefined();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('returns undefined for an object', () => {
|
|
82
|
+
expect(toSortDate({ date: '2024-01-01' })).toBeUndefined();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
//////////////////////////////
|
|
87
|
+
// createComparator — alpha mode
|
|
88
|
+
//////////////////////////////
|
|
89
|
+
|
|
90
|
+
describe('createComparator — alpha', () => {
|
|
91
|
+
const cmp = createComparator('alpha');
|
|
92
|
+
|
|
93
|
+
it('sorts items lexicographically by label', () => {
|
|
94
|
+
const a: SidebarItem = { label: 'Banana', href: '/b' };
|
|
95
|
+
const b: SidebarItem = { label: 'Apple', href: '/a' };
|
|
96
|
+
expect(cmp(a, b)).toBeGreaterThan(0);
|
|
97
|
+
expect(cmp(b, a)).toBeLessThan(0);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('returns 0 for identical labels', () => {
|
|
101
|
+
const a: SidebarItem = { label: 'Same', href: '/a' };
|
|
102
|
+
const b: SidebarItem = { label: 'Same', href: '/b' };
|
|
103
|
+
expect(cmp(a, b)).toBe(0);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('is case-insensitive', () => {
|
|
107
|
+
const a: SidebarItem = { label: 'apple', href: '/a' };
|
|
108
|
+
const b: SidebarItem = { label: 'Apple', href: '/b' };
|
|
109
|
+
expect(cmp(a, b)).toBe(0);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
//////////////////////////////
|
|
114
|
+
// createComparator — date-asc mode
|
|
115
|
+
//////////////////////////////
|
|
116
|
+
|
|
117
|
+
describe('createComparator — date-asc', () => {
|
|
118
|
+
const cmp = createComparator('date-asc');
|
|
119
|
+
|
|
120
|
+
it('sorts earlier dates before later dates', () => {
|
|
121
|
+
const a: SidebarItem = {
|
|
122
|
+
label: 'A',
|
|
123
|
+
href: '/a',
|
|
124
|
+
date: new Date('2023-01-01'),
|
|
125
|
+
};
|
|
126
|
+
const b: SidebarItem = {
|
|
127
|
+
label: 'B',
|
|
128
|
+
href: '/b',
|
|
129
|
+
date: new Date('2024-01-01'),
|
|
130
|
+
};
|
|
131
|
+
expect(cmp(a, b)).toBeLessThan(0);
|
|
132
|
+
expect(cmp(b, a)).toBeGreaterThan(0);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('returns 0 for equal dates', () => {
|
|
136
|
+
const a: SidebarItem = {
|
|
137
|
+
label: 'A',
|
|
138
|
+
href: '/a',
|
|
139
|
+
date: new Date('2024-01-01'),
|
|
140
|
+
};
|
|
141
|
+
const b: SidebarItem = {
|
|
142
|
+
label: 'B',
|
|
143
|
+
href: '/b',
|
|
144
|
+
date: new Date('2024-01-01'),
|
|
145
|
+
};
|
|
146
|
+
expect(cmp(a, b)).toBe(0);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('sorts items without a date to the top (before items with a date)', () => {
|
|
150
|
+
const nodateItem: SidebarItem = { label: 'A', href: '/a' };
|
|
151
|
+
const datedItem: SidebarItem = {
|
|
152
|
+
label: 'B',
|
|
153
|
+
href: '/b',
|
|
154
|
+
date: new Date('2020-01-01'),
|
|
155
|
+
};
|
|
156
|
+
expect(cmp(nodateItem, datedItem)).toBeLessThan(0);
|
|
157
|
+
expect(cmp(datedItem, nodateItem)).toBeGreaterThan(0);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('returns 0 when both items have no date', () => {
|
|
161
|
+
const a: SidebarItem = { label: 'A', href: '/a' };
|
|
162
|
+
const b: SidebarItem = { label: 'B', href: '/b' };
|
|
163
|
+
expect(cmp(a, b)).toBe(0);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
//////////////////////////////
|
|
168
|
+
// createComparator — date-desc mode
|
|
169
|
+
//////////////////////////////
|
|
170
|
+
|
|
171
|
+
describe('createComparator — date-desc', () => {
|
|
172
|
+
const cmp = createComparator('date-desc');
|
|
173
|
+
|
|
174
|
+
it('sorts later dates before earlier dates', () => {
|
|
175
|
+
const a: SidebarItem = {
|
|
176
|
+
label: 'A',
|
|
177
|
+
href: '/a',
|
|
178
|
+
date: new Date('2024-01-01'),
|
|
179
|
+
};
|
|
180
|
+
const b: SidebarItem = {
|
|
181
|
+
label: 'B',
|
|
182
|
+
href: '/b',
|
|
183
|
+
date: new Date('2023-01-01'),
|
|
184
|
+
};
|
|
185
|
+
expect(cmp(a, b)).toBeLessThan(0);
|
|
186
|
+
expect(cmp(b, a)).toBeGreaterThan(0);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('sorts items without a date to the top', () => {
|
|
190
|
+
const nodateItem: SidebarItem = { label: 'A', href: '/a' };
|
|
191
|
+
const datedItem: SidebarItem = {
|
|
192
|
+
label: 'B',
|
|
193
|
+
href: '/b',
|
|
194
|
+
date: new Date('2020-01-01'),
|
|
195
|
+
};
|
|
196
|
+
expect(cmp(nodateItem, datedItem)).toBeLessThan(0);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
//////////////////////////////
|
|
201
|
+
// readSortMode / writeSortMode
|
|
202
|
+
//////////////////////////////
|
|
203
|
+
|
|
204
|
+
describe('readSortMode / writeSortMode', () => {
|
|
205
|
+
beforeEach(() => {
|
|
206
|
+
// Stub localStorage globally — Node has no built-in localStorage
|
|
207
|
+
vi.stubGlobal('localStorage', makeLocalStorageMock());
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
afterEach(() => {
|
|
211
|
+
vi.unstubAllGlobals();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('defaults to "alpha" when no value is stored', () => {
|
|
215
|
+
expect(readSortMode('my-collection')).toBe('alpha');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('returns the stored "alpha" mode', () => {
|
|
219
|
+
writeSortMode('my-collection', 'alpha');
|
|
220
|
+
expect(readSortMode('my-collection')).toBe('alpha');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('returns the stored "date-asc" mode', () => {
|
|
224
|
+
writeSortMode('my-collection', 'date-asc');
|
|
225
|
+
expect(readSortMode('my-collection')).toBe('date-asc');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('returns the stored "date-desc" mode', () => {
|
|
229
|
+
writeSortMode('my-collection', 'date-desc');
|
|
230
|
+
expect(readSortMode('my-collection')).toBe('date-desc');
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('defaults to "alpha" when the stored value is not a valid SortMode', () => {
|
|
234
|
+
localStorage.setItem('cms-sort-my-collection', 'invalid-value');
|
|
235
|
+
expect(readSortMode('my-collection')).toBe('alpha');
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('namespaces storage keys by collection name', () => {
|
|
239
|
+
writeSortMode('posts', 'date-desc');
|
|
240
|
+
writeSortMode('pages', 'date-asc');
|
|
241
|
+
expect(readSortMode('posts')).toBe('date-desc');
|
|
242
|
+
expect(readSortMode('pages')).toBe('date-asc');
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
//////////////////////////////
|
|
247
|
+
// SORT_MODES and SORT_ORDER constants
|
|
248
|
+
//////////////////////////////
|
|
249
|
+
|
|
250
|
+
describe('SORT_MODES constant', () => {
|
|
251
|
+
it('contains an entry for every SortMode', () => {
|
|
252
|
+
expect(SORT_MODES).toHaveProperty('alpha');
|
|
253
|
+
expect(SORT_MODES).toHaveProperty('date-asc');
|
|
254
|
+
expect(SORT_MODES).toHaveProperty('date-desc');
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('each entry has icon and label strings', () => {
|
|
258
|
+
for (const entry of Object.values(SORT_MODES)) {
|
|
259
|
+
expect(typeof entry.icon).toBe('string');
|
|
260
|
+
expect(typeof entry.label).toBe('string');
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
describe('SORT_ORDER constant', () => {
|
|
266
|
+
it('is an array with exactly 3 entries', () => {
|
|
267
|
+
expect(Array.isArray(SORT_ORDER)).toBe(true);
|
|
268
|
+
expect(SORT_ORDER).toHaveLength(3);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('contains all three SortMode values', () => {
|
|
272
|
+
expect(SORT_ORDER).toContain('alpha');
|
|
273
|
+
expect(SORT_ORDER).toContain('date-asc');
|
|
274
|
+
expect(SORT_ORDER).toContain('date-desc');
|
|
275
|
+
});
|
|
276
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { stableStringify } from '../../../../src/client/js/utils/stable-stringify';
|
|
3
|
+
|
|
4
|
+
describe('stableStringify', () => {
|
|
5
|
+
it('produces the same output regardless of key insertion order', () => {
|
|
6
|
+
const objA = { z: 1, a: 2, m: 3 };
|
|
7
|
+
const objB = { a: 2, m: 3, z: 1 };
|
|
8
|
+
expect(stableStringify(objA)).toBe(stableStringify(objB));
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('sorts keys alphabetically at the top level', () => {
|
|
12
|
+
const obj = { z: 1, a: 2, m: 3 };
|
|
13
|
+
expect(stableStringify(obj)).toBe('{"a":2,"m":3,"z":1}');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('sorts keys recursively in nested objects', () => {
|
|
17
|
+
const obj = { b: { y: 1, x: 2 }, a: { q: 3, p: 4 } };
|
|
18
|
+
expect(stableStringify(obj)).toBe('{"a":{"p":4,"q":3},"b":{"x":2,"y":1}}');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('preserves array element order (arrays are not sorted)', () => {
|
|
22
|
+
const obj = { items: [3, 1, 2] };
|
|
23
|
+
expect(stableStringify(obj)).toBe('{"items":[3,1,2]}');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('handles null values correctly', () => {
|
|
27
|
+
const obj = { a: null, b: 'text' };
|
|
28
|
+
expect(stableStringify(obj)).toBe('{"a":null,"b":"text"}');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('handles a top-level null', () => {
|
|
32
|
+
expect(stableStringify(null)).toBe('null');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('serializes an empty object', () => {
|
|
36
|
+
expect(stableStringify({})).toBe('{}');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('serializes an empty array', () => {
|
|
40
|
+
expect(stableStringify([])).toBe('[]');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('serializes primitive string values', () => {
|
|
44
|
+
expect(stableStringify('hello')).toBe('"hello"');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('serializes primitive number values', () => {
|
|
48
|
+
expect(stableStringify(42)).toBe('42');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('serializes boolean values', () => {
|
|
52
|
+
expect(stableStringify(true)).toBe('true');
|
|
53
|
+
expect(stableStringify(false)).toBe('false');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('handles arrays containing objects with sorted keys', () => {
|
|
57
|
+
const arr = [
|
|
58
|
+
{ b: 2, a: 1 },
|
|
59
|
+
{ d: 4, c: 3 },
|
|
60
|
+
];
|
|
61
|
+
expect(stableStringify(arr)).toBe('[{"a":1,"b":2},{"c":3,"d":4}]');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('is deterministic across multiple calls', () => {
|
|
65
|
+
const obj = { x: { q: 1, p: 2 }, a: [3, 2, 1] };
|
|
66
|
+
expect(stableStringify(obj)).toBe(stableStringify(obj));
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { isURL } from '../../../../src/client/js/utils/url-utils';
|
|
3
|
+
|
|
4
|
+
describe('isURL', () => {
|
|
5
|
+
// Valid absolute URLs
|
|
6
|
+
it('returns true for an https URL', () => {
|
|
7
|
+
expect(isURL('https://example.com')).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('returns true for an http URL', () => {
|
|
11
|
+
expect(isURL('http://example.com')).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('returns true for an https URL with a path', () => {
|
|
15
|
+
expect(isURL('https://example.com/some/path')).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('returns true for an https URL with query params', () => {
|
|
19
|
+
expect(isURL('https://example.com/search?q=test&page=2')).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('returns true for a mailto: URL', () => {
|
|
23
|
+
expect(isURL('mailto:user@example.com')).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Absolute paths
|
|
27
|
+
it('returns true for an absolute path starting with /', () => {
|
|
28
|
+
expect(isURL('/some/path')).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('returns true for the root path /', () => {
|
|
32
|
+
expect(isURL('/')).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Non-URLs
|
|
36
|
+
it('returns false for plain text', () => {
|
|
37
|
+
expect(isURL('hello world')).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('returns false for a relative path without leading slash', () => {
|
|
41
|
+
expect(isURL('some/relative/path')).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('returns false for a word that looks like a domain but has no scheme', () => {
|
|
45
|
+
expect(isURL('example.com')).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('returns true for a protocol-relative URL — it starts with / so the absolute-path branch accepts it', () => {
|
|
49
|
+
// //example.com/path starts with /, so new URL('//example.com/path', 'https://a.com')
|
|
50
|
+
// resolves successfully. The function accepts absolute paths without requiring a scheme.
|
|
51
|
+
expect(isURL('//example.com/path')).toBe(true);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Edge cases
|
|
55
|
+
it('returns false for an empty string', () => {
|
|
56
|
+
expect(isURL('')).toBe(false);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('returns false for a whitespace-only string', () => {
|
|
60
|
+
expect(isURL(' ')).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('trims whitespace before validation — valid URL with surrounding spaces', () => {
|
|
64
|
+
expect(isURL(' https://example.com ')).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('trims whitespace before validation — plain text with surrounding spaces', () => {
|
|
68
|
+
expect(isURL(' hello ')).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
});
|