@asaidimu/hestia-mock 1.15.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/LICENSE.md ADDED
@@ -0,0 +1,17 @@
1
+ GNU AFFERO GENERAL PUBLIC LICENSE
2
+ Version 3, 19 November 2007
3
+
4
+ Copyright © 2026 Saidimu Lolokile
5
+
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Affero General Public License as published
8
+ by the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Affero General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Affero General Public License
17
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
package/README.md ADDED
@@ -0,0 +1,239 @@
1
+ # @asaidimu/hestia-mock
2
+
3
+ > IndexedDB-backed mock server and transport for the Hestia TypeScript client SDK — full API parity, persistent offline emulation, and realistic error semantics.
4
+
5
+ [![Version](https://img.shields.io/npm/v/@asaidimu/hestia-mock)](https://www.npmjs.com/package/@asaidimu/hestia-mock)
6
+ [![License](https://img.shields.io/npm/l/@asaidimu/hestia-mock)](LICENSE.md)
7
+
8
+ ## Quick Links
9
+
10
+ - [Overview & Features](#overview--features)
11
+ - [Installation & Setup](#installation--setup)
12
+ - [Usage Documentation](#usage-documentation)
13
+ - [Project Architecture](#project-architecture)
14
+ - [Development & Contributing](#development--contributing)
15
+ - [Troubleshooting & FAQ](#troubleshooting--faq)
16
+ - [License](#license)
17
+
18
+ ---
19
+
20
+ ## Overview & Features
21
+
22
+ ### Detailed Description
23
+
24
+ `@asaidimu/hestia-mock` is an in-process server emulator for the [Hestia](https://github.com/asaidimu/hestia) platform. It implements the full Hestia server API using IndexedDB as the storage engine, enabling offline-first development, testing, and prototyping without a running backend.
25
+
26
+ The mock plugs directly into `HestiaClient` via the `Transport` interface — the same abstraction used by `HttpTransport` (browser) and `WailsTransport` (desktop). Every SDK call — auth, collections, documents, blobs, streams — is served entirely in the browser with no network dependency.
27
+
28
+ ### Key Features
29
+
30
+ - **Full SDK Compatibility**: Uses the real `HestiaClient` — not a mock or stub — ensuring production-parity behavior
31
+ - **IndexedDB Persistence**: Sessions, documents, blobs, and settings survive page reloads
32
+ - **Deterministic Seeding**: Pre-populate users, collections, and policies for consistent test/dev environments
33
+ - **Realistic Error Semantics**: Throws `SystemError` instances with production error codes (`SYNC-001-NF`, `AUTH-002-UNAUTH`, etc.)
34
+ - **SSE Stream Emulation**: In-process event bus simulates server-sent events for notifications, audit logs, and workflows
35
+ - **Simulated Latency**: Configurable server delay for realistic UX testing
36
+ - **Query Engine**: Full `@asaidimu/query` support — 14 comparison operators, logical groups, multi-field sort, offset pagination
37
+
38
+ ---
39
+
40
+ ## Installation & Setup
41
+
42
+ ### Prerequisites
43
+
44
+ - Node.js >= 18.0.0 or Bun >= 1.0.0
45
+ - `@asaidimu/hestia` (peer dependency)
46
+
47
+ ### Installation
48
+
49
+ ```bash
50
+ # Bun
51
+ bun add @asaidimu/hestia @asaidimu/hestia-mock
52
+
53
+ # npm
54
+ npm install @asaidimu/hestia @asaidimu/hestia-mock
55
+ ```
56
+
57
+ ### Node/Bun Test Setup
58
+
59
+ For Node.js or Bun environments, install `fake-indexeddb` to polyfill IndexedDB:
60
+
61
+ ```bash
62
+ bun add -d fake-indexeddb
63
+ # or
64
+ npm install -D fake-indexeddb
65
+ ```
66
+
67
+ Then import the polyfill at the top of your test files:
68
+
69
+ ```ts
70
+ import "fake-indexeddb/auto";
71
+ ```
72
+
73
+ ---
74
+
75
+ ## Usage Documentation
76
+
77
+ ### Basic Usage
78
+
79
+ ```ts
80
+ import { createMockHestia } from "@asaidimu/hestia-mock";
81
+
82
+ const mock = await createMockHestia({
83
+ database: "my-app-mock",
84
+ seed: { email: "admin@test.local", password: "password123", admin: true },
85
+ });
86
+
87
+ // Login — session persists across reloads
88
+ await mock.client.auth.login("admin@test.local", "password123");
89
+
90
+ // Typed collections with full CRUD
91
+ const todos = mock.client.collection<{ title: string; done: boolean }>("todos");
92
+ await todos.create({ data: { title: "Ship feature", done: false } });
93
+ const page = await todos.find();
94
+
95
+ // Cleanup
96
+ await mock.destroy();
97
+ ```
98
+
99
+ ### À la carte Wiring
100
+
101
+ ```ts
102
+ import { MockHestiaServer, IndexedDbTransport, createIdbPersistence } from "@asaidimu/hestia-mock";
103
+ import { HestiaClient } from "@asaidimu/hestia";
104
+
105
+ const server = await MockHestiaServer.create({ database: "app" });
106
+ const transport = new IndexedDbTransport(server);
107
+
108
+ const api = new HestiaClient({
109
+ baseUrl: "idb://hestia-mock",
110
+ transport: transport as any,
111
+ persistence: createIdbPersistence(server),
112
+ });
113
+ ```
114
+
115
+ ### Direct Server Access
116
+
117
+ Bypass the SDK and talk to the mock server directly:
118
+
119
+ ```ts
120
+ await server.dispatch("system:core:health:check");
121
+ await server.pushLog({ level: "info", msg: "background job done" });
122
+ await server.fireSchedule(scheduleId); // manual cron tick
123
+ await server.tickSchedules(); // fire all due @every schedules
124
+ ```
125
+
126
+ ### Configuration Options
127
+
128
+ | Option | Type | Default | Description |
129
+ |:---|:---|:---|:---|
130
+ | `database` | `string` | `"hestia-mock"` | IndexedDB database name (isolates state between instances) |
131
+ | `seed` | `SeedConfig` | `DEFAULT_SEED` | Initial admin user and seed data |
132
+ | `reset` | `boolean` | `false` | Wipe the database before seeding |
133
+ | `serverOptions.latency` | `number` | `0` | Simulated server latency in milliseconds |
134
+ | `serverOptions.updateAvailable` | `string \| null` | `null` | Set to a version string to enable the update flow |
135
+
136
+ ---
137
+
138
+ ## Project Architecture
139
+
140
+ ### Core Components
141
+
142
+ - **`MockHestiaServer`**: The in-process server emulator. Handles all message dispatch, manages IndexedDB stores, and emits realtime events via an internal `EventBus`
143
+ - **`IndexedDbTransport`**: Implements the Hestia `Transport` interface. Routes `dispatch()`, raw path methods, and `openStream()` calls to the mock server
144
+ - **`createMockHestia()`**: One-call factory that wires server, transport, persistence, and a real `HestiaClient` together
145
+ - **Query Engine**: `executeQuery()` / `evaluateFilter()` / `sortDocs()` — implements the `@asaidimu/query` DSL against IndexedDB collections
146
+ - **`EventBus`**: Topic-based pub/sub for SSE stream emulation (notifications, audit logs, workflows)
147
+
148
+ ### Persistence Model
149
+
150
+ All data lives in a single IndexedDB database (configurable name). Stores include:
151
+
152
+ | Store | Contents |
153
+ |:---|:---|
154
+ | Collections | All documents across all collections (system + user-defined) |
155
+ | Sessions | Auth sessions (survive reloads) |
156
+ | API Keys | Key metadata and hashes |
157
+ | Notifications | Per-user notification documents |
158
+ | Schedules | Cron schedule definitions |
159
+ | Workflows | Definitions, runs, events, registry |
160
+ | Blobs | Namespaced blob metadata and binary data |
161
+ | Logs | Application logs and audit trail |
162
+ | Settings | Key-value configuration |
163
+ | Capabilities | Feature flags |
164
+ | Seed | Seeding state flags |
165
+
166
+ ### Extension Points
167
+
168
+ - **`MockServerOptions`**: Hook into server behavior (latency, update state, workflow execution)
169
+ - **`EventBus`**: Inject custom events into streams for testing realtime features
170
+ - **`seedDatabase()`**: Custom seeding logic beyond the default admin user
171
+
172
+ ---
173
+
174
+ ## Development & Contributing
175
+
176
+ ### Available Scripts
177
+
178
+ ```bash
179
+ cd client-mock
180
+ bun install
181
+
182
+ bun run test # Run tests with vitest (fake-indexeddb)
183
+ bun run typecheck # TypeScript type checking
184
+ bun run build # Build dist/ with tsdown
185
+ ```
186
+
187
+ ### Testing & Quality Standard
188
+
189
+ ```bash
190
+ bun run test
191
+ ```
192
+
193
+ Tests use the **real SDK** (`@asaidimu/hestia`) against the mock server, covering auth flows, document CRUD with versioning, pagers, streams, blob uploads, audit entries, and persistence across simulated restarts.
194
+
195
+ ### Contributing Guidelines
196
+
197
+ 1. Fork the repository
198
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
199
+ 3. Commit changes (`git commit -m 'feat: add amazing feature'`)
200
+ 4. Push to branch (`git push origin feature/amazing-feature`)
201
+ 5. Open a Pull Request
202
+
203
+ ---
204
+
205
+ ## Troubleshooting & FAQ
206
+
207
+ ### Troubleshooting
208
+
209
+ - **Issue**: `IndexedDB is not defined` in Node.js/Bun
210
+ **Solution**: Install and import the fake-indexeddb polyfill: `import "fake-indexeddb/auto"`
211
+
212
+ - **Issue**: Tests share state between runs
213
+ **Solution**: Use a unique database name per test: `database: \`test-${Date.now()}\``
214
+
215
+ - **Issue**: Blob download URLs are not fetchable
216
+ **Solution**: Use `blobs.namespace(ns).download(key)` instead of `routeUrl()` — the mock does not serve HTTP
217
+
218
+ ### FAQ
219
+
220
+ - **Q**: Does this replace a real Hestia server?
221
+ **A**: No. The mock emulates the API surface for development and testing. It does not implement cron daemons, real password hashing (uses SHA-256), or Cedar-style policy evaluation.
222
+
223
+ - **Q**: Can I use this in Storybook?
224
+ **A**: Yes. Call `createMockHestia()` in a decorator or preview init. The IndexedDB persistence means state survives HMR updates.
225
+
226
+ - **Q**: How do I test error paths?
227
+ **A**: The mock throws real `SystemError` codes. Trigger them by querying non-existent documents (`SYNC-001-NF`), using wrong credentials (`AUTH-003-CRED`), or writing stale versions (`SYNC-003-VC`).
228
+
229
+ ---
230
+
231
+ ## License
232
+
233
+ Distributed under the [AGPL-3.0](LICENSE.md) License. See `LICENSE.md` for more information.
234
+
235
+ ## Acknowledgments
236
+
237
+ - [Hestia](https://github.com/asaidimu/hestia) — the platform this mock emulates
238
+ - [@asaidimu/query](https://www.npmjs.com/package/@asaidimu/query) — the query DSL implemented by the mock engine
239
+ - [fake-indexeddb](https://www.npmjs.com/package/fake-indexeddb) — IndexedDB polyfill for Node.js/Bun testing