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.
Files changed (241) hide show
  1. package/.claude/settings.local.json +42 -0
  2. package/.github/workflows/ci.yml +31 -0
  3. package/.mcp.json +12 -0
  4. package/.prettierignore +5 -0
  5. package/.prettierrc.cjs +22 -0
  6. package/AGENTS.md +183 -0
  7. package/LICENSE +201 -0
  8. package/README.md +128 -0
  9. package/package.json +74 -0
  10. package/playground/.claude/settings.local.json +5 -0
  11. package/playground/astro.config.mjs +7 -0
  12. package/playground/node_modules/.bin/astro +21 -0
  13. package/playground/node_modules/.bin/rollup +21 -0
  14. package/playground/node_modules/.bin/tsc +21 -0
  15. package/playground/node_modules/.bin/tsserver +21 -0
  16. package/playground/node_modules/.bin/vite +21 -0
  17. package/playground/node_modules/.vite/_svelte_metadata.json +1 -0
  18. package/playground/node_modules/.vite/deps/@astrojs_svelte_client__js.js +80 -0
  19. package/playground/node_modules/.vite/deps/@astrojs_svelte_client__js.js.map +7 -0
  20. package/playground/node_modules/.vite/deps/_metadata.json +184 -0
  21. package/playground/node_modules/.vite/deps/astro___aria-query.js +6776 -0
  22. package/playground/node_modules/.vite/deps/astro___aria-query.js.map +7 -0
  23. package/playground/node_modules/.vite/deps/astro___axobject-query.js +3754 -0
  24. package/playground/node_modules/.vite/deps/astro___axobject-query.js.map +7 -0
  25. package/playground/node_modules/.vite/deps/astro___html-escaper.js +34 -0
  26. package/playground/node_modules/.vite/deps/astro___html-escaper.js.map +7 -0
  27. package/playground/node_modules/.vite/deps/chunk-AJXJMYAF.js +0 -0
  28. package/playground/node_modules/.vite/deps/chunk-AJXJMYAF.js.map +7 -0
  29. package/playground/node_modules/.vite/deps/chunk-ALJIOON6.js +1005 -0
  30. package/playground/node_modules/.vite/deps/chunk-ALJIOON6.js.map +7 -0
  31. package/playground/node_modules/.vite/deps/chunk-BUSYA2B4.js +8 -0
  32. package/playground/node_modules/.vite/deps/chunk-BUSYA2B4.js.map +7 -0
  33. package/playground/node_modules/.vite/deps/chunk-CNYJBM5F.js +21 -0
  34. package/playground/node_modules/.vite/deps/chunk-CNYJBM5F.js.map +7 -0
  35. package/playground/node_modules/.vite/deps/chunk-DBPNBGEI.js +223 -0
  36. package/playground/node_modules/.vite/deps/chunk-DBPNBGEI.js.map +7 -0
  37. package/playground/node_modules/.vite/deps/chunk-G3C2FXJT.js +204 -0
  38. package/playground/node_modules/.vite/deps/chunk-G3C2FXJT.js.map +7 -0
  39. package/playground/node_modules/.vite/deps/chunk-GKDKFWC5.js +27 -0
  40. package/playground/node_modules/.vite/deps/chunk-GKDKFWC5.js.map +7 -0
  41. package/playground/node_modules/.vite/deps/chunk-HNCLEOC5.js +4376 -0
  42. package/playground/node_modules/.vite/deps/chunk-HNCLEOC5.js.map +7 -0
  43. package/playground/node_modules/.vite/deps/chunk-JICYXBFU.js +688 -0
  44. package/playground/node_modules/.vite/deps/chunk-JICYXBFU.js.map +7 -0
  45. package/playground/node_modules/.vite/deps/chunk-KCUTL6DD.js +5099 -0
  46. package/playground/node_modules/.vite/deps/chunk-KCUTL6DD.js.map +7 -0
  47. package/playground/node_modules/.vite/deps/chunk-ZP4UNCSN.js +23 -0
  48. package/playground/node_modules/.vite/deps/chunk-ZP4UNCSN.js.map +7 -0
  49. package/playground/node_modules/.vite/deps/chunk-ZREFNRZZ.js +148 -0
  50. package/playground/node_modules/.vite/deps/chunk-ZREFNRZZ.js.map +7 -0
  51. package/playground/node_modules/.vite/deps/package.json +3 -0
  52. package/playground/node_modules/.vite/deps/smol-toml.js +843 -0
  53. package/playground/node_modules/.vite/deps/smol-toml.js.map +7 -0
  54. package/playground/node_modules/.vite/deps/svelte.js +55 -0
  55. package/playground/node_modules/.vite/deps/svelte.js.map +7 -0
  56. package/playground/node_modules/.vite/deps/svelte___clsx.js +9 -0
  57. package/playground/node_modules/.vite/deps/svelte___clsx.js.map +7 -0
  58. package/playground/node_modules/.vite/deps/svelte_animate.js +57 -0
  59. package/playground/node_modules/.vite/deps/svelte_animate.js.map +7 -0
  60. package/playground/node_modules/.vite/deps/svelte_attachments.js +15 -0
  61. package/playground/node_modules/.vite/deps/svelte_attachments.js.map +7 -0
  62. package/playground/node_modules/.vite/deps/svelte_easing.js +67 -0
  63. package/playground/node_modules/.vite/deps/svelte_easing.js.map +7 -0
  64. package/playground/node_modules/.vite/deps/svelte_events.js +11 -0
  65. package/playground/node_modules/.vite/deps/svelte_events.js.map +7 -0
  66. package/playground/node_modules/.vite/deps/svelte_internal.js +5 -0
  67. package/playground/node_modules/.vite/deps/svelte_internal.js.map +7 -0
  68. package/playground/node_modules/.vite/deps/svelte_internal_client.js +402 -0
  69. package/playground/node_modules/.vite/deps/svelte_internal_client.js.map +7 -0
  70. package/playground/node_modules/.vite/deps/svelte_internal_disclose-version.js +10 -0
  71. package/playground/node_modules/.vite/deps/svelte_internal_disclose-version.js.map +7 -0
  72. package/playground/node_modules/.vite/deps/svelte_internal_flags_async.js +8 -0
  73. package/playground/node_modules/.vite/deps/svelte_internal_flags_async.js.map +7 -0
  74. package/playground/node_modules/.vite/deps/svelte_internal_flags_legacy.js +8 -0
  75. package/playground/node_modules/.vite/deps/svelte_internal_flags_legacy.js.map +7 -0
  76. package/playground/node_modules/.vite/deps/svelte_internal_flags_tracing.js +8 -0
  77. package/playground/node_modules/.vite/deps/svelte_internal_flags_tracing.js.map +7 -0
  78. package/playground/node_modules/.vite/deps/svelte_legacy.js +35 -0
  79. package/playground/node_modules/.vite/deps/svelte_legacy.js.map +7 -0
  80. package/playground/node_modules/.vite/deps/svelte_motion.js +545 -0
  81. package/playground/node_modules/.vite/deps/svelte_motion.js.map +7 -0
  82. package/playground/node_modules/.vite/deps/svelte_reactivity.js +29 -0
  83. package/playground/node_modules/.vite/deps/svelte_reactivity.js.map +7 -0
  84. package/playground/node_modules/.vite/deps/svelte_reactivity_window.js +127 -0
  85. package/playground/node_modules/.vite/deps/svelte_reactivity_window.js.map +7 -0
  86. package/playground/node_modules/.vite/deps/svelte_store.js +103 -0
  87. package/playground/node_modules/.vite/deps/svelte_store.js.map +7 -0
  88. package/playground/node_modules/.vite/deps/svelte_transition.js +208 -0
  89. package/playground/node_modules/.vite/deps/svelte_transition.js.map +7 -0
  90. package/playground/package.json +16 -0
  91. package/playground/pnpm-lock.yaml +3167 -0
  92. package/playground/src/content/authors/jane-doe.json +8 -0
  93. package/playground/src/content/config/build.toml +2 -0
  94. package/playground/src/content/courses/web-fundamentals.json +29 -0
  95. package/playground/src/content/docs/advanced.mdx +6 -0
  96. package/playground/src/content/docs/intro.md +6 -0
  97. package/playground/src/content/guides/getting-started.mdx +6 -0
  98. package/playground/src/content/posts/hello-world.md +7 -0
  99. package/playground/src/content/products/t-shirt.json +16 -0
  100. package/playground/src/content/recipes/pancakes.mdoc +8 -0
  101. package/playground/src/content/settings/site.yml +2 -0
  102. package/playground/src/content.config.ts +198 -0
  103. package/playground/src/env.d.ts +1 -0
  104. package/playground/src/pages/index.astro +11 -0
  105. package/playground/src/pages/nebula.astro +14 -0
  106. package/pnpm-workspace.yaml +2 -0
  107. package/scripts/subset-icons.mjs +178 -0
  108. package/src/astro/index.ts +295 -0
  109. package/src/client/Admin.svelte +283 -0
  110. package/src/client/components/BackendPicker.svelte +291 -0
  111. package/src/client/components/DraftChip.svelte +46 -0
  112. package/src/client/components/MetadataForm.svelte +56 -0
  113. package/src/client/components/ThemeToggle.svelte +18 -0
  114. package/src/client/components/dialogs/DeleteDraftDialog.svelte +51 -0
  115. package/src/client/components/dialogs/FilenameDialog.svelte +129 -0
  116. package/src/client/components/editor/EditorPane.svelte +227 -0
  117. package/src/client/components/editor/EditorTabs.svelte +81 -0
  118. package/src/client/components/editor/EditorToolbar.svelte +131 -0
  119. package/src/client/components/editor/FormatSelector.svelte +66 -0
  120. package/src/client/components/editor/Toolbar.svelte +17 -0
  121. package/src/client/components/fields/ArrayField.svelte +339 -0
  122. package/src/client/components/fields/ArrayItem.svelte +325 -0
  123. package/src/client/components/fields/BooleanField.svelte +114 -0
  124. package/src/client/components/fields/DateField.svelte +82 -0
  125. package/src/client/components/fields/EnumField.svelte +74 -0
  126. package/src/client/components/fields/FieldWrapper.svelte +96 -0
  127. package/src/client/components/fields/NumberField.svelte +99 -0
  128. package/src/client/components/fields/ObjectField.svelte +121 -0
  129. package/src/client/components/fields/SchemaField.svelte +107 -0
  130. package/src/client/components/fields/StringField.svelte +104 -0
  131. package/src/client/components/sidebar/AdminSidebar.svelte +339 -0
  132. package/src/client/components/sidebar/AdminSidebarSort.svelte +123 -0
  133. package/src/client/css/a11y.css +14 -0
  134. package/src/client/css/btn.css +113 -0
  135. package/src/client/css/dialog.css +29 -0
  136. package/src/client/css/field-input.css +39 -0
  137. package/src/client/css/reset.css +59 -0
  138. package/src/client/css/theme.css +77 -0
  139. package/src/client/index.ts +1 -0
  140. package/src/client/js/drafts/merge.svelte.ts +121 -0
  141. package/src/client/js/drafts/ops.svelte.ts +227 -0
  142. package/src/client/js/drafts/storage.ts +108 -0
  143. package/src/client/js/drafts/workers/diff.ts +40 -0
  144. package/src/client/js/editor/editor.svelte.ts +343 -0
  145. package/src/client/js/editor/languages.ts +98 -0
  146. package/src/client/js/editor/link-wrap.ts +45 -0
  147. package/src/client/js/editor/markdown-shortcuts.ts +261 -0
  148. package/src/client/js/handlers/admin.ts +246 -0
  149. package/src/client/js/state/dialogs.svelte.ts +35 -0
  150. package/src/client/js/state/router.svelte.ts +156 -0
  151. package/src/client/js/state/schema.svelte.ts +140 -0
  152. package/src/client/js/state/state.svelte.ts +334 -0
  153. package/src/client/js/state/theme.svelte.ts +173 -0
  154. package/src/client/js/storage/adapter.ts +102 -0
  155. package/src/client/js/storage/client.ts +150 -0
  156. package/src/client/js/storage/db.ts +36 -0
  157. package/src/client/js/storage/fsa.ts +110 -0
  158. package/src/client/js/storage/github.ts +297 -0
  159. package/src/client/js/storage/storage.ts +83 -0
  160. package/src/client/js/storage/workers/frontmatter.ts +320 -0
  161. package/src/client/js/storage/workers/storage.ts +177 -0
  162. package/src/client/js/storage/workers/toml-parser.ts +106 -0
  163. package/src/client/js/storage/workers/yaml-parser.ts +132 -0
  164. package/src/client/js/utils/file-types.ts +192 -0
  165. package/src/client/js/utils/format.ts +16 -0
  166. package/src/client/js/utils/frontmatter.ts +38 -0
  167. package/src/client/js/utils/schema-utils.ts +295 -0
  168. package/src/client/js/utils/slug.ts +18 -0
  169. package/src/client/js/utils/sort.ts +84 -0
  170. package/src/client/js/utils/stable-stringify.ts +27 -0
  171. package/src/client/js/utils/url-utils.ts +38 -0
  172. package/src/types.ts +25 -0
  173. package/src/virtual.d.ts +22 -0
  174. package/svelte.config.js +4 -0
  175. package/tests/astro/build.test.ts +63 -0
  176. package/tests/astro/index.test.ts +689 -0
  177. package/tests/client/components/Admin.test.ts +446 -0
  178. package/tests/client/components/BackendPicker.test.ts +239 -0
  179. package/tests/client/components/DraftChip.test.ts +53 -0
  180. package/tests/client/components/MetadataForm.test.ts +164 -0
  181. package/tests/client/components/dialogs/DeleteDraftDialog.test.ts +91 -0
  182. package/tests/client/components/dialogs/FilenameDialog.test.ts +209 -0
  183. package/tests/client/components/dialogs/dialog-stubs.ts +19 -0
  184. package/tests/client/components/editor/EditorPane.test.ts +100 -0
  185. package/tests/client/components/editor/EditorTabs.test.ts +253 -0
  186. package/tests/client/components/editor/EditorToolbar.test.ts +252 -0
  187. package/tests/client/components/editor/fixtures.ts +31 -0
  188. package/tests/client/components/fields/ArrayField.test.ts +197 -0
  189. package/tests/client/components/fields/BooleanField.test.ts +206 -0
  190. package/tests/client/components/fields/DateField.test.ts +210 -0
  191. package/tests/client/components/fields/EnumField.test.ts +246 -0
  192. package/tests/client/components/fields/NumberField.test.ts +240 -0
  193. package/tests/client/components/fields/ObjectField.test.ts +157 -0
  194. package/tests/client/components/fields/SchemaField.test.ts +190 -0
  195. package/tests/client/components/fields/StringField.test.ts +223 -0
  196. package/tests/client/components/sidebar/AdminSidebar.test.ts +285 -0
  197. package/tests/client/components/sidebar/AdminSidebarSort.test.ts +135 -0
  198. package/tests/client/components/sidebar/sort-mock.ts +23 -0
  199. package/tests/client/js/drafts/fixtures.ts +22 -0
  200. package/tests/client/js/drafts/merge.test.ts +282 -0
  201. package/tests/client/js/drafts/ops.test.ts +658 -0
  202. package/tests/client/js/drafts/storage.test.ts +200 -0
  203. package/tests/client/js/drafts/workers/diff.test.ts +165 -0
  204. package/tests/client/js/editor/editor.test.ts +616 -0
  205. package/tests/client/js/editor/link-wrap.test.ts +225 -0
  206. package/tests/client/js/editor/markdown-shortcuts.test.ts +370 -0
  207. package/tests/client/js/handlers/admin.test.ts +467 -0
  208. package/tests/client/js/state/router.test.ts +619 -0
  209. package/tests/client/js/state/schema.test.ts +266 -0
  210. package/tests/client/js/state/state.test.ts +328 -0
  211. package/tests/client/js/storage/adapter.test.ts +115 -0
  212. package/tests/client/js/storage/client.test.ts +250 -0
  213. package/tests/client/js/storage/db.test.ts +59 -0
  214. package/tests/client/js/storage/fsa.test.ts +284 -0
  215. package/tests/client/js/storage/github.test.ts +349 -0
  216. package/tests/client/js/storage/mock-port.ts +95 -0
  217. package/tests/client/js/storage/storage.test.ts +77 -0
  218. package/tests/client/js/storage/workers/frontmatter.test.ts +479 -0
  219. package/tests/client/js/storage/workers/storage.test.ts +299 -0
  220. package/tests/client/js/storage/workers/toml-parser.test.ts +169 -0
  221. package/tests/client/js/storage/workers/yaml-parser.test.ts +168 -0
  222. package/tests/client/js/utils/file-types.test.ts +268 -0
  223. package/tests/client/js/utils/frontmatter.test.ts +87 -0
  224. package/tests/client/js/utils/schema-utils.test.ts +318 -0
  225. package/tests/client/js/utils/slug.test.ts +58 -0
  226. package/tests/client/js/utils/sort.test.ts +276 -0
  227. package/tests/client/js/utils/stable-stringify.test.ts +68 -0
  228. package/tests/client/js/utils/url-utils.test.ts +70 -0
  229. package/tests/e2e/backend-connection.test.ts +301 -0
  230. package/tests/e2e/draft-lifecycle.test.ts +388 -0
  231. package/tests/e2e/editing.test.ts +355 -0
  232. package/tests/e2e/github-adapter.test.ts +330 -0
  233. package/tests/e2e/helpers/mock-adapter.ts +166 -0
  234. package/tests/e2e/helpers/test-app.ts +155 -0
  235. package/tests/e2e/navigation.test.ts +358 -0
  236. package/tests/e2e/publishing.test.ts +345 -0
  237. package/tests/e2e/unsaved-changes.test.ts +317 -0
  238. package/tests/setup.ts +2 -0
  239. package/tests/stubs/codemirror.ts +197 -0
  240. package/tsconfig.json +19 -0
  241. package/vitest.config.ts +178 -0
@@ -0,0 +1,299 @@
1
+ import { describe, it, expect, vi, afterEach, type MockInstance } from 'vitest';
2
+
3
+ //////////////////////////////
4
+ // Storage worker tests
5
+ //
6
+ // The worker module uses self.addEventListener('connect', ...) to register a
7
+ // SharedWorker entry point. We mock self before importing so the listener is
8
+ // captured, then invoke it with plain event-like objects (not real
9
+ // MessageEvent instances, because MessageEvent.ports is a readonly getter).
10
+ //
11
+ // The worker's `adapter` variable is module-level singleton state. Each test
12
+ // creates its own isolated port and drives its own init + request sequence
13
+ // rather than sharing state via beforeEach. This avoids cross-test pollution
14
+ // where an adapter set by one test's init is still active when the next test
15
+ // starts.
16
+ //
17
+ // Adapters are loaded via dynamic import() inside the 'init' case. We mock
18
+ // those modules via vi.mock so no real FSA or GitHub code runs.
19
+ //////////////////////////////
20
+
21
+ import type { StorageRequest } from '../../../../../src/client/js/storage/adapter';
22
+ import { makeWorkerMockPort as makeMockPort } from '../mock-port';
23
+
24
+ // ── Mock adapter modules ────────────────────────────────────────────────────
25
+
26
+ vi.mock('../../../../../src/client/js/storage/fsa', () => ({
27
+ FsaAdapter: function () {
28
+ return {
29
+ listFiles: vi
30
+ .fn()
31
+ .mockResolvedValue([{ filename: 'a.md', content: 'A' }]),
32
+ readFile: vi.fn().mockResolvedValue('A content'),
33
+ writeFile: vi.fn().mockResolvedValue(undefined),
34
+ writeFiles: vi.fn().mockResolvedValue(undefined),
35
+ deleteFile: vi.fn().mockResolvedValue(undefined),
36
+ };
37
+ },
38
+ }));
39
+
40
+ vi.mock('../../../../../src/client/js/storage/github', () => ({
41
+ GitHubAdapter: function () {
42
+ return {
43
+ validate: vi.fn().mockResolvedValue(undefined),
44
+ listFiles: vi
45
+ .fn()
46
+ .mockResolvedValue([{ filename: 'b.md', content: 'B' }]),
47
+ readFile: vi.fn().mockResolvedValue('B content'),
48
+ writeFile: vi.fn().mockResolvedValue(undefined),
49
+ writeFiles: vi.fn().mockResolvedValue(undefined),
50
+ deleteFile: vi.fn().mockResolvedValue(undefined),
51
+ };
52
+ },
53
+ }));
54
+
55
+ // ── Self mock ──────────────────────────────────────────────────────────────
56
+
57
+ // Capture the connect listener registered by the worker module at import time.
58
+ // The handler type uses a plain object because MessageEvent.ports is a
59
+ // readonly getter — we cannot assign to it on a real MessageEvent.
60
+ let connectHandler: ((event: { ports: MessagePort[] }) => void) | null = null;
61
+
62
+ vi.stubGlobal('self', {
63
+ addEventListener: vi.fn((type: string, handler: unknown) => {
64
+ if (type === 'connect') {
65
+ connectHandler = handler as (event: { ports: MessagePort[] }) => void;
66
+ }
67
+ }),
68
+ postMessage: vi.fn(),
69
+ });
70
+
71
+ // ── Import module under test (after mocks are in place) ─────────────────────
72
+
73
+ await import('../../../../../src/client/js/storage/workers/storage');
74
+
75
+ // ── Helpers ────────────────────────────────────────────────────────────────
76
+
77
+ /**
78
+ * Connects a port to the worker by invoking the captured connect handler
79
+ * with a plain object. MessageEvent.ports is a readonly getter so we cannot
80
+ * use a real MessageEvent here.
81
+ * @param {MessagePort} port - The port to register with the worker
82
+ * @return {void}
83
+ */
84
+ function connectPort(port: MessagePort): void {
85
+ connectHandler!({ ports: [port] });
86
+ }
87
+
88
+ /**
89
+ * Connects a port, sends an FSA init, and waits for the ok response.
90
+ * @param {MockInstance} postSpy - The port's postMessage spy
91
+ * @param {(req: StorageRequest & { _id?: string }) => void} send - The send helper
92
+ * @param {string} id - A unique _id for this init request
93
+ * @return {Promise<void>}
94
+ */
95
+ async function initFSA(
96
+ postSpy: MockInstance,
97
+ send: (req: StorageRequest & { _id?: string }) => void,
98
+ id: string,
99
+ ): Promise<void> {
100
+ send({
101
+ type: 'init',
102
+ backend: { type: 'fsa', handle: {} as FileSystemDirectoryHandle },
103
+ _id: id,
104
+ });
105
+ await vi.waitUntil(() => postSpy.mock.calls.some((c) => c[0]._id === id), {
106
+ timeout: 2000,
107
+ });
108
+ }
109
+
110
+ // ── Tests ─────────────────────────────────────────────────────────────────────
111
+
112
+ afterEach(() => {
113
+ vi.clearAllMocks();
114
+ });
115
+
116
+ describe('storage worker', () => {
117
+ it('registers a connect listener on self', () => {
118
+ expect(connectHandler).toBeTypeOf('function');
119
+ });
120
+
121
+ it('sends port-connected immediately when a port connects', () => {
122
+ const { port, postSpy } = makeMockPort();
123
+ connectPort(port);
124
+ const portConnected = postSpy.mock.calls.find(
125
+ (c) => c[0].type === 'port-connected',
126
+ );
127
+ expect(portConnected).toBeDefined();
128
+ });
129
+
130
+ it('responds to FSA init with ok: true', async () => {
131
+ const { port, postSpy, send } = makeMockPort();
132
+ connectPort(port);
133
+ await initFSA(postSpy, send, 'init-fsa-1');
134
+ const resp = postSpy.mock.calls.find((c) => c[0]._id === 'init-fsa-1')[0];
135
+ expect(resp.ok).toBe(true);
136
+ expect(resp.type).toBe('init');
137
+ // Clean up shared adapter state
138
+ send({ type: 'teardown', _id: 'teardown-fsa-1' });
139
+ await vi.waitUntil(() =>
140
+ postSpy.mock.calls.some((c) => c[0]._id === 'teardown-fsa-1'),
141
+ );
142
+ });
143
+
144
+ it('responds to GitHub init with ok: true', async () => {
145
+ const { port, postSpy, send } = makeMockPort();
146
+ connectPort(port);
147
+ send({
148
+ type: 'init',
149
+ backend: { type: 'github', token: 'tok', repo: 'owner/repo' },
150
+ _id: 'init-gh-1',
151
+ });
152
+ await vi.waitUntil(
153
+ () => postSpy.mock.calls.some((c) => c[0]._id === 'init-gh-1'),
154
+ { timeout: 2000 },
155
+ );
156
+ const resp = postSpy.mock.calls.find((c) => c[0]._id === 'init-gh-1')[0];
157
+ expect(resp.ok).toBe(true);
158
+ // Clean up
159
+ send({ type: 'teardown', _id: 'teardown-gh-1' });
160
+ await vi.waitUntil(() =>
161
+ postSpy.mock.calls.some((c) => c[0]._id === 'teardown-gh-1'),
162
+ );
163
+ });
164
+
165
+ it('responds to listFiles after init', async () => {
166
+ const { port, postSpy, send } = makeMockPort();
167
+ connectPort(port);
168
+ await initFSA(postSpy, send, 'init-list');
169
+
170
+ send({
171
+ type: 'listFiles',
172
+ collection: 'posts',
173
+ extensions: ['.md', '.mdx'],
174
+ _id: 'list-1',
175
+ });
176
+ await vi.waitUntil(
177
+ () => postSpy.mock.calls.some((c) => c[0]._id === 'list-1'),
178
+ { timeout: 2000 },
179
+ );
180
+
181
+ const resp = postSpy.mock.calls.find((c) => c[0]._id === 'list-1')[0];
182
+ expect(resp.type).toBe('listFiles');
183
+ expect(resp.ok).toBe(true);
184
+ expect(resp.files).toEqual([{ filename: 'a.md', content: 'A' }]);
185
+
186
+ send({ type: 'teardown', _id: 'teardown-list' });
187
+ await vi.waitUntil(() =>
188
+ postSpy.mock.calls.some((c) => c[0]._id === 'teardown-list'),
189
+ );
190
+ });
191
+
192
+ it('returns an error response for listFiles before init', async () => {
193
+ // Teardown any adapter left by previous tests
194
+ const {
195
+ port: cleanPort,
196
+ postSpy: cleanSpy,
197
+ send: cleanSend,
198
+ } = makeMockPort();
199
+ connectPort(cleanPort);
200
+ cleanSend({ type: 'teardown', _id: 'pre-teardown' });
201
+ await vi.waitUntil(() =>
202
+ cleanSpy.mock.calls.some((c) => c[0]._id === 'pre-teardown'),
203
+ );
204
+
205
+ const { port, postSpy, send } = makeMockPort();
206
+ connectPort(port);
207
+ send({
208
+ type: 'listFiles',
209
+ collection: 'posts',
210
+ extensions: ['.md', '.mdx'],
211
+ _id: 'list-no-adapter',
212
+ });
213
+ await vi.waitUntil(
214
+ () => postSpy.mock.calls.some((c) => c[0]._id === 'list-no-adapter'),
215
+ { timeout: 2000 },
216
+ );
217
+
218
+ const resp = postSpy.mock.calls.find(
219
+ (c) => c[0]._id === 'list-no-adapter',
220
+ )[0];
221
+ expect(resp.ok).toBe(false);
222
+ expect(resp.error).toContain('No backend');
223
+ });
224
+
225
+ it('responds to deleteFile after init', async () => {
226
+ const { port, postSpy, send } = makeMockPort();
227
+ connectPort(port);
228
+ await initFSA(postSpy, send, 'init-delete');
229
+
230
+ send({
231
+ type: 'deleteFile',
232
+ collection: 'posts',
233
+ filename: 'old.md',
234
+ _id: 'delete-1',
235
+ });
236
+ await vi.waitUntil(
237
+ () => postSpy.mock.calls.some((c) => c[0]._id === 'delete-1'),
238
+ { timeout: 2000 },
239
+ );
240
+
241
+ const resp = postSpy.mock.calls.find((c) => c[0]._id === 'delete-1')[0];
242
+ expect(resp.type).toBe('deleteFile');
243
+ expect(resp.ok).toBe(true);
244
+
245
+ send({ type: 'teardown', _id: 'teardown-delete' });
246
+ await vi.waitUntil(() =>
247
+ postSpy.mock.calls.some((c) => c[0]._id === 'teardown-delete'),
248
+ );
249
+ });
250
+
251
+ it('returns an error response for deleteFile before init', async () => {
252
+ // Teardown any adapter left by previous tests
253
+ const {
254
+ port: cleanPort,
255
+ postSpy: cleanSpy,
256
+ send: cleanSend,
257
+ } = makeMockPort();
258
+ connectPort(cleanPort);
259
+ cleanSend({ type: 'teardown', _id: 'pre-teardown-del' });
260
+ await vi.waitUntil(() =>
261
+ cleanSpy.mock.calls.some((c) => c[0]._id === 'pre-teardown-del'),
262
+ );
263
+
264
+ const { port, postSpy, send } = makeMockPort();
265
+ connectPort(port);
266
+ send({
267
+ type: 'deleteFile',
268
+ collection: 'posts',
269
+ filename: 'old.md',
270
+ _id: 'delete-no-adapter',
271
+ });
272
+ await vi.waitUntil(
273
+ () => postSpy.mock.calls.some((c) => c[0]._id === 'delete-no-adapter'),
274
+ { timeout: 2000 },
275
+ );
276
+
277
+ const resp = postSpy.mock.calls.find(
278
+ (c) => c[0]._id === 'delete-no-adapter',
279
+ )[0];
280
+ expect(resp.ok).toBe(false);
281
+ expect(resp.error).toContain('No backend');
282
+ });
283
+
284
+ it('responds to teardown with ok: true and clears the adapter', async () => {
285
+ const { port, postSpy, send } = makeMockPort();
286
+ connectPort(port);
287
+ await initFSA(postSpy, send, 'init-teardown');
288
+
289
+ send({ type: 'teardown', _id: 'td-1' });
290
+ await vi.waitUntil(
291
+ () => postSpy.mock.calls.some((c) => c[0]._id === 'td-1'),
292
+ { timeout: 2000 },
293
+ );
294
+
295
+ const resp = postSpy.mock.calls.find((c) => c[0]._id === 'td-1')[0];
296
+ expect(resp.type).toBe('teardown');
297
+ expect(resp.ok).toBe(true);
298
+ });
299
+ });
@@ -0,0 +1,169 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+
3
+ //////////////////////////////
4
+ // TOML parser worker tests
5
+ //
6
+ // The worker registers a self.addEventListener('message', ...) listener that
7
+ // handles 'parse', 'parse-batch', and 'stringify' message types. We stub self
8
+ // before importing the module, capture the handler, then invoke it with plain
9
+ // event-like objects rather than real MessageEvent instances.
10
+ //
11
+ // Real MessageEvent validates its payload structure at construction time;
12
+ // plain objects are used instead so tests remain in Node.js without a real
13
+ // Worker context.
14
+ //////////////////////////////
15
+
16
+ // ── Self mock ──────────────────────────────────────────────────────────────
17
+
18
+ // The handler type uses a loose event shape: the worker reads event.data
19
+ // only, so we only need to match that property.
20
+ let messageHandler:
21
+ | ((event: { data: Record<string, unknown> }) => void)
22
+ | null = null;
23
+ const selfPostMessage = vi.fn();
24
+
25
+ vi.stubGlobal('self', {
26
+ addEventListener: vi.fn((type: string, handler: unknown) => {
27
+ if (type === 'message') {
28
+ messageHandler = handler as (event: {
29
+ data: Record<string, unknown>;
30
+ }) => void;
31
+ }
32
+ }),
33
+ postMessage: selfPostMessage,
34
+ });
35
+
36
+ // ── Import module (after stubs) ────────────────────────────────────────────
37
+
38
+ await import('../../../../../src/client/js/storage/workers/toml-parser');
39
+
40
+ // ── Helpers ────────────────────────────────────────────────────────────────
41
+
42
+ /**
43
+ * Dispatches a fake event to the captured handler by calling it directly with a plain object.
44
+ * MessageEvent construction is bypassed because the constructor validates its payload.
45
+ * @param {Record<string, unknown>} data - The message data payload
46
+ * @return {void}
47
+ */
48
+ function dispatch(data: Record<string, unknown>): void {
49
+ messageHandler!({ data });
50
+ }
51
+
52
+ // ── Tests ─────────────────────────────────────────────────────────────────────
53
+
54
+ describe('toml-parser worker', () => {
55
+ beforeEach(() => {
56
+ vi.clearAllMocks();
57
+ });
58
+
59
+ afterEach(() => {
60
+ vi.clearAllMocks();
61
+ });
62
+
63
+ it('registers a message listener on self', () => {
64
+ expect(messageHandler).toBeTypeOf('function');
65
+ });
66
+
67
+ describe('parse message', () => {
68
+ it('parses a valid TOML string and posts a parse-result with the data', () => {
69
+ dispatch({
70
+ type: 'parse',
71
+ id: 'p1',
72
+ content: 'title = "Hello"\ncount = 42\n',
73
+ });
74
+
75
+ expect(selfPostMessage).toHaveBeenCalledWith({
76
+ type: 'parse-result',
77
+ id: 'p1',
78
+ ok: true,
79
+ data: { title: 'Hello', count: 42 },
80
+ });
81
+ });
82
+
83
+ it('posts a parse-result with ok: false for invalid TOML', () => {
84
+ dispatch({
85
+ type: 'parse',
86
+ id: 'p2',
87
+ content: 'this is not = = valid toml',
88
+ });
89
+
90
+ const call = selfPostMessage.mock.calls[0][0];
91
+ expect(call.type).toBe('parse-result');
92
+ expect(call.id).toBe('p2');
93
+ expect(call.ok).toBe(false);
94
+ expect(typeof call.error).toBe('string');
95
+ });
96
+ });
97
+
98
+ describe('parse-batch message', () => {
99
+ it('parses a batch of TOML strings and posts a parse-batch-result with a results map', () => {
100
+ dispatch({
101
+ type: 'parse-batch',
102
+ id: 'b1',
103
+ items: [
104
+ { key: 'alpha', content: 'value = 1\n' },
105
+ { key: 'beta', content: 'value = 2\n' },
106
+ ],
107
+ });
108
+
109
+ expect(selfPostMessage).toHaveBeenCalledWith({
110
+ type: 'parse-batch-result',
111
+ id: 'b1',
112
+ ok: true,
113
+ results: {
114
+ alpha: { value: 1 },
115
+ beta: { value: 2 },
116
+ },
117
+ });
118
+ });
119
+
120
+ it('posts a parse-batch-result with ok: false when a batch item is invalid', () => {
121
+ dispatch({
122
+ type: 'parse-batch',
123
+ id: 'b2',
124
+ items: [{ key: 'bad', content: 'not = = valid' }],
125
+ });
126
+
127
+ const call = selfPostMessage.mock.calls[0][0];
128
+ expect(call.type).toBe('parse-batch-result');
129
+ expect(call.id).toBe('b2');
130
+ expect(call.ok).toBe(false);
131
+ expect(typeof call.error).toBe('string');
132
+ });
133
+ });
134
+
135
+ describe('stringify message', () => {
136
+ it('serializes an object to a TOML string and posts a stringify-result', () => {
137
+ dispatch({
138
+ type: 'stringify',
139
+ id: 's1',
140
+ data: { name: 'test', count: 5 },
141
+ });
142
+
143
+ const call = selfPostMessage.mock.calls[0][0];
144
+ expect(call.type).toBe('stringify-result');
145
+ expect(call.id).toBe('s1');
146
+ expect(call.ok).toBe(true);
147
+ expect(typeof call.content).toBe('string');
148
+ // Verify round-trip: the output must contain the original values
149
+ expect(call.content).toContain('name');
150
+ expect(call.content).toContain('test');
151
+ });
152
+
153
+ it('posts a stringify-result with ok: false when serialization fails', () => {
154
+ // smol-toml cannot serialize values that are not TOML-compatible (e.g. undefined)
155
+ dispatch({
156
+ type: 'stringify',
157
+ id: 's2',
158
+ // A nested object with a circular-ish unsupported value
159
+ data: { bad: undefined as unknown },
160
+ });
161
+
162
+ // smol-toml may succeed (skipping undefined) or fail — either way the
163
+ // response must be a stringify-result with a defined id
164
+ const call = selfPostMessage.mock.calls[0][0];
165
+ expect(call.type).toBe('stringify-result');
166
+ expect(call.id).toBe('s2');
167
+ });
168
+ });
169
+ });
@@ -0,0 +1,168 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+
3
+ //////////////////////////////
4
+ // YAML parser worker tests
5
+ //
6
+ // The worker registers a self.addEventListener('message', ...) listener that
7
+ // handles 'parse', 'parse-batch', and 'stringify' message types. We stub self
8
+ // before importing the module, capture the handler, then invoke it with plain
9
+ // event-like objects rather than real MessageEvent instances.
10
+ //
11
+ // Real MessageEvent validates that items in the `ports` array are actual
12
+ // MessagePort instances — since we cannot construct those in Node.js without
13
+ // a real Worker context, we pass plain objects that satisfy the fields the
14
+ // worker code actually reads (event.data).
15
+ //////////////////////////////
16
+
17
+ // ── Self mock ──────────────────────────────────────────────────────────────
18
+
19
+ // The handler type uses a loose event shape: real MessageEvent validates
20
+ // that ports[] entries are actual MessagePort instances, which we cannot
21
+ // construct in Node.js. We type the captured handler loosely and call it
22
+ // with plain objects that mirror the fields the worker actually reads.
23
+ let messageHandler:
24
+ | ((event: { data: Record<string, unknown> }) => Promise<void>)
25
+ | null = null;
26
+ const selfPostMessage = vi.fn();
27
+
28
+ vi.stubGlobal('self', {
29
+ addEventListener: vi.fn((type: string, handler: unknown) => {
30
+ if (type === 'message') {
31
+ messageHandler = handler as (event: {
32
+ data: Record<string, unknown>;
33
+ }) => Promise<void>;
34
+ }
35
+ }),
36
+ postMessage: selfPostMessage,
37
+ });
38
+
39
+ // ── Import module (after stubs) ────────────────────────────────────────────
40
+
41
+ await import('../../../../../src/client/js/storage/workers/yaml-parser');
42
+
43
+ // ── Helpers ────────────────────────────────────────────────────────────────
44
+
45
+ /**
46
+ * Dispatches a fake event to the captured handler by calling it directly
47
+ * with a plain object. We bypass MessageEvent construction because the
48
+ * constructor validates that ports[] entries are real MessagePort instances.
49
+ * @param {Record<string, unknown>} data - The message data payload
50
+ * @return {Promise<void>}
51
+ */
52
+ async function dispatch(data: Record<string, unknown>): Promise<void> {
53
+ await messageHandler!({ data });
54
+ }
55
+
56
+ // ── Tests ─────────────────────────────────────────────────────────────────────
57
+
58
+ describe('yaml-parser worker', () => {
59
+ beforeEach(() => {
60
+ vi.clearAllMocks();
61
+ });
62
+
63
+ afterEach(() => {
64
+ vi.clearAllMocks();
65
+ });
66
+
67
+ it('registers a message listener on self', () => {
68
+ expect(messageHandler).toBeTypeOf('function');
69
+ });
70
+
71
+ describe('parse message', () => {
72
+ it('parses a valid YAML string and posts a parse-result with ok: true', async () => {
73
+ await dispatch({
74
+ type: 'parse',
75
+ id: 'req-1',
76
+ content: 'title: Hello World\ncount: 42',
77
+ });
78
+
79
+ expect(selfPostMessage).toHaveBeenCalledOnce();
80
+ const msg = selfPostMessage.mock.calls[0][0];
81
+ expect(msg.type).toBe('parse-result');
82
+ expect(msg.id).toBe('req-1');
83
+ expect(msg.ok).toBe(true);
84
+ expect(msg.data).toEqual({ title: 'Hello World', count: 42 });
85
+ });
86
+
87
+ it('posts a parse-result with ok: false for invalid YAML', async () => {
88
+ await dispatch({
89
+ type: 'parse',
90
+ id: 'req-err',
91
+ // Intentionally malformed: tab characters are not allowed in YAML
92
+ content: 'key:\n\t- bad',
93
+ });
94
+
95
+ expect(selfPostMessage).toHaveBeenCalledOnce();
96
+ const msg = selfPostMessage.mock.calls[0][0];
97
+ expect(msg.type).toBe('parse-result');
98
+ expect(msg.id).toBe('req-err');
99
+ expect(msg.ok).toBe(false);
100
+ expect(typeof msg.error).toBe('string');
101
+ });
102
+ });
103
+
104
+ describe('parse-batch message', () => {
105
+ it('parses a batch of YAML strings and posts a parse-batch-result with ok: true', async () => {
106
+ await dispatch({
107
+ type: 'parse-batch',
108
+ id: 'batch-1',
109
+ items: [
110
+ { key: 'file-a', content: 'title: File A' },
111
+ { key: 'file-b', content: 'title: File B\nactive: true' },
112
+ ],
113
+ });
114
+
115
+ expect(selfPostMessage).toHaveBeenCalledOnce();
116
+ const msg = selfPostMessage.mock.calls[0][0];
117
+ expect(msg.type).toBe('parse-batch-result');
118
+ expect(msg.id).toBe('batch-1');
119
+ expect(msg.ok).toBe(true);
120
+ expect(msg.results).toEqual({
121
+ 'file-a': { title: 'File A' },
122
+ 'file-b': { title: 'File B', active: true },
123
+ });
124
+ });
125
+
126
+ it('posts a parse-batch-result with ok: false when any item has invalid YAML', async () => {
127
+ await dispatch({
128
+ type: 'parse-batch',
129
+ id: 'batch-err',
130
+ items: [
131
+ { key: 'good', content: 'title: Good' },
132
+ { key: 'bad', content: 'key:\n\t- bad' },
133
+ ],
134
+ });
135
+
136
+ expect(selfPostMessage).toHaveBeenCalledOnce();
137
+ const msg = selfPostMessage.mock.calls[0][0];
138
+ expect(msg.type).toBe('parse-batch-result');
139
+ expect(msg.id).toBe('batch-err');
140
+ expect(msg.ok).toBe(false);
141
+ expect(typeof msg.error).toBe('string');
142
+ });
143
+ });
144
+
145
+ describe('stringify message', () => {
146
+ it('serializes an object to a YAML string and posts a stringify-result with ok: true', async () => {
147
+ await dispatch({
148
+ type: 'stringify',
149
+ id: 'str-1',
150
+ data: { title: 'My Post', tags: ['a', 'b'], count: 7 },
151
+ });
152
+
153
+ expect(selfPostMessage).toHaveBeenCalledOnce();
154
+ const msg = selfPostMessage.mock.calls[0][0];
155
+ expect(msg.type).toBe('stringify-result');
156
+ expect(msg.id).toBe('str-1');
157
+ expect(msg.ok).toBe(true);
158
+ expect(typeof msg.content).toBe('string');
159
+ // Verify the serialized YAML round-trips back to the original data
160
+ const { load } = await import('js-yaml');
161
+ expect(load(msg.content)).toEqual({
162
+ title: 'My Post',
163
+ tags: ['a', 'b'],
164
+ count: 7,
165
+ });
166
+ });
167
+ });
168
+ });