@lunora/testing 0.0.0 → 1.0.0-alpha.2
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 +105 -0
- package/README.md +240 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/index.d.mts +265 -0
- package/dist/index.d.ts +265 -0
- package/dist/index.mjs +2 -0
- package/dist/packem_shared/lunoraTest-BUg4Ebv8.mjs +415 -0
- package/package.json +42 -17
package/LICENSE.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, Apache 2.0 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 anolilab and contributors
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or service
|
|
34
|
+
that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software that
|
|
39
|
+
exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee using
|
|
52
|
+
the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to the
|
|
59
|
+
infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN
|
|
79
|
+
IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following will
|
|
93
|
+
apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,18 +1,249 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- START_PACKAGE_OG_IMAGE_PLACEHOLDER -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<a href="https://www.anolilab.com/open-source" align="center">
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
<img src="__assets__/package-og.svg" alt="testing" />
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
</a>
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
- Repository: https://github.com/anolilab/lunora
|
|
9
|
+
<h3 align="center">Testing toolkit for Lunora: an in-memory harness for queries, mutations, and actions</h3>
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
<!-- END_PACKAGE_OG_IMAGE_PLACEHOLDER -->
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
<br />
|
|
14
|
+
|
|
15
|
+
<div align="center">
|
|
16
|
+
|
|
17
|
+
[![typescript-image][typescript-badge]][typescript-url]
|
|
18
|
+
[![FSL-1.1-Apache-2.0 licence][license-badge]][license]
|
|
19
|
+
[![npm version][npm-version-badge]][npm-version]
|
|
20
|
+
[![npm downloads][npm-downloads-badge]][npm-downloads]
|
|
21
|
+
[![PRs Welcome][prs-welcome-badge]][prs-welcome]
|
|
22
|
+
|
|
23
|
+
</div>
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
<div align="center">
|
|
28
|
+
<p>
|
|
29
|
+
<sup>
|
|
30
|
+
Daniel Bannert's open source work is supported by the community on <a href="https://github.com/sponsors/prisis">GitHub Sponsors</a>
|
|
31
|
+
</sup>
|
|
32
|
+
</p>
|
|
33
|
+
</div>
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
Testing toolkit for Lunora: an in-memory harness for queries, mutations, and actions. Today it surfaces the dev mail-catcher helpers — in `lunora dev`, `@lunora/mail` captures every outbound email into the studio's root-shard inbox, and these helpers read that inbox over the admin RPC so a Playwright (or any HTTP) test can drive "request reset → read the email → follow the link" deterministically.
|
|
38
|
+
|
|
39
|
+
Part of the [Lunora](https://github.com/anolilab/lunora) framework — a type-safe, real-time backend on Cloudflare Workers + Durable Objects with a Vite-first DX.
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
npm install @lunora/testing
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
yarn add @lunora/testing
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
pnpm add @lunora/testing
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
### In-memory function harness
|
|
58
|
+
|
|
59
|
+
`lunoraTest(schema)` runs your `query` / `mutation` / `action` functions against
|
|
60
|
+
an in-memory `node:sqlite` backend — no Durable Object, no `wrangler`, no
|
|
61
|
+
network. It mirrors Convex's `convexTest`: `query` / `mutation` / `action` /
|
|
62
|
+
`run` / `withIdentity`, all sharing one database so a write is visible to a
|
|
63
|
+
later read.
|
|
64
|
+
|
|
65
|
+
In your app, `query` / `mutation` / `action` are imported from
|
|
66
|
+
`@/lunora/_generated/server` — the codegen-emitted module. A self-contained test
|
|
67
|
+
can instead derive the builders from a schema with `initLunora`:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { defineSchema, defineTable, initLunora, v } from "@lunora/server";
|
|
71
|
+
import { lunoraTest } from "@lunora/testing";
|
|
72
|
+
import { expect, test } from "vitest";
|
|
73
|
+
|
|
74
|
+
const schema = defineSchema({
|
|
75
|
+
messages: defineTable({
|
|
76
|
+
author: v.string(),
|
|
77
|
+
body: v.string(),
|
|
78
|
+
}),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const { mutation, query } = initLunora.dataModel().create();
|
|
82
|
+
|
|
83
|
+
const send = mutation.input({ author: v.string(), body: v.string() }).mutation(({ args, ctx }) => ctx.db.insert("messages", args));
|
|
84
|
+
|
|
85
|
+
const list = query.query(({ ctx }) => ctx.db.query("messages").collect());
|
|
86
|
+
|
|
87
|
+
test("sends and lists a message", async () => {
|
|
88
|
+
const t = lunoraTest(schema);
|
|
89
|
+
|
|
90
|
+
await t.mutation(send, { author: "ada", body: "hi" });
|
|
91
|
+
|
|
92
|
+
expect(await t.query(list, {})).toHaveLength(1);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("sees the injected identity", async () => {
|
|
96
|
+
const t = lunoraTest(schema).withIdentity({ userId: "u1" });
|
|
97
|
+
|
|
98
|
+
await t.run(async (ctx) => {
|
|
99
|
+
expect(ctx.auth.userId).toBe("u1");
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Each `lunoraTest(...)` opens an in-memory SQLite database; call `t.close()`
|
|
105
|
+
(e.g. in an `afterEach`) to release the native handle when a test finishes.
|
|
106
|
+
|
|
107
|
+
#### Injectable `ctx.fetch`
|
|
108
|
+
|
|
109
|
+
Pass a `fetch` option to replace the throwing stub in action contexts:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
const fakeFetch = vi.fn<typeof globalThis.fetch>().mockResolvedValue(Response.json({ ok: true }));
|
|
113
|
+
const t = lunoraTest(schema, { fetch: fakeFetch });
|
|
114
|
+
// ctx.fetch inside any action now calls fakeFetch
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Without the option, `ctx.fetch` still throws the v1 error on first access.
|
|
118
|
+
|
|
119
|
+
#### Controllable in-memory scheduler
|
|
120
|
+
|
|
121
|
+
`ctx.scheduler` is a fully functional fake. Jobs are enqueued synchronously but
|
|
122
|
+
**only execute** when you advance the virtual clock:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
// The mutation the scheduler will dispatch, registered under "messages:send".
|
|
126
|
+
const sendMessage = mutation.input({ author: v.string(), body: v.string() }).mutation(({ args, ctx }) => ctx.db.insert("messages", args));
|
|
127
|
+
|
|
128
|
+
// A mutation that schedules `sendMessage` 5s out instead of writing directly.
|
|
129
|
+
const enqueue = mutation.mutation(({ ctx }) => ctx.scheduler.runAfter(5_000, "messages:send", { author: "ada", body: "hello" }));
|
|
130
|
+
|
|
131
|
+
test("scheduled mutation writes to db after advance", async () => {
|
|
132
|
+
const t = lunoraTest(schema, {
|
|
133
|
+
functions: { "messages:send": sendMessage }, // path → registered fn
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
await t.mutation(enqueue, {});
|
|
137
|
+
|
|
138
|
+
expect(t.scheduler.list()).toHaveLength(1); // queued, not yet run
|
|
139
|
+
|
|
140
|
+
await t.scheduler.advance(10_000); // tick the clock past scheduledFor
|
|
141
|
+
|
|
142
|
+
expect(await t.query(list, {})).toHaveLength(1); // sendMessage ran
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Harness controls:
|
|
147
|
+
|
|
148
|
+
| Method | Description |
|
|
149
|
+
| -------------------------- | ----------------------------------------------------- |
|
|
150
|
+
| `t.scheduler.list()` | Snapshot of all pending jobs (enqueue order) |
|
|
151
|
+
| `t.scheduler.advance(ms)` | Tick the virtual clock by `ms`, execute due jobs |
|
|
152
|
+
| `t.scheduler.runPending()` | Execute all pending jobs regardless of scheduled time |
|
|
153
|
+
|
|
154
|
+
The virtual clock is **per-harness** — advancing one harness's clock does not
|
|
155
|
+
affect other harnesses, so tests running in parallel are isolated.
|
|
156
|
+
|
|
157
|
+
Provide a `functions` map (`{ "path:name": registeredFn }`) so the scheduler
|
|
158
|
+
can dispatch jobs. Paths not in the map produce a `console.warn` and are
|
|
159
|
+
silently dropped (matching production behaviour for unknown paths).
|
|
160
|
+
|
|
161
|
+
#### Subscription testing
|
|
162
|
+
|
|
163
|
+
`harness.subscribe(query, args)` returns an async iterable that yields the query's
|
|
164
|
+
current result immediately, then re-emits after every `mutation` or `run` call:
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
test("subscription re-emits after mutation", async () => {
|
|
168
|
+
const t = lunoraTest(schema);
|
|
169
|
+
const sub = t.subscribe(list, {});
|
|
170
|
+
|
|
171
|
+
expect((await sub.next()).value).toHaveLength(0); // initial snapshot
|
|
172
|
+
|
|
173
|
+
await t.mutation(send, { author: "ada", body: "hi" });
|
|
174
|
+
|
|
175
|
+
expect((await sub.next()).value).toHaveLength(1); // updated snapshot
|
|
176
|
+
|
|
177
|
+
await sub.return(); // unsubscribe
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Subscriptions are **table-agnostic** — any mutation triggers a re-evaluation.
|
|
182
|
+
Multiple independent subscriptions each maintain their own snapshot stream.
|
|
183
|
+
|
|
184
|
+
> **v1 stubs (still throwing).** `ctx.storage`, `ctx.vectors`, and `ctx.workflows`
|
|
185
|
+
> are clearly-throwing stubs — a handler that touches one fails with a
|
|
186
|
+
> "not available in the in-memory @lunora/testing harness (v1)" error.
|
|
187
|
+
> These are the next planned follow-ups.
|
|
188
|
+
|
|
189
|
+
### Mail-catcher helpers (E2E)
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
import { extractLink, waitForMail } from "@lunora/testing";
|
|
193
|
+
|
|
194
|
+
// Trigger the flow (e.g. POST /api/auth/forgot-password), then:
|
|
195
|
+
const mail = await waitForMail({
|
|
196
|
+
adminToken: process.env.LUNORA_ADMIN_TOKEN!,
|
|
197
|
+
baseUrl: "http://localhost:8787",
|
|
198
|
+
to: "alice@example.test",
|
|
199
|
+
subjectMatch: "Reset your password",
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const resetLink = extractLink(mail, { match: "/reset-password" });
|
|
203
|
+
// → visit `resetLink`, set a new password, assert success.
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
> This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs)**.
|
|
207
|
+
|
|
208
|
+
## Related
|
|
209
|
+
|
|
210
|
+
- [`@lunora/mail`](https://www.npmjs.com/package/@lunora/mail) — captures the outbound email these helpers read.
|
|
211
|
+
- [`@lunora/auth`](https://www.npmjs.com/package/@lunora/auth) — the auth flows (verification, reset, magic links) you test end-to-end.
|
|
212
|
+
- [`@lunora/cli`](https://www.npmjs.com/package/@lunora/cli) — runs the `lunora dev` server the harness drives.
|
|
213
|
+
|
|
214
|
+
## Supported Node.js Versions
|
|
215
|
+
|
|
216
|
+
Libraries in this ecosystem make the best effort to track [Node.js' release schedule](https://github.com/nodejs/release#release-schedule).
|
|
217
|
+
Here's [a post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
|
|
218
|
+
|
|
219
|
+
## Contributing
|
|
220
|
+
|
|
221
|
+
If you would like to help take a look at the [list of issues](https://github.com/anolilab/lunora/issues) and check our [Contributing](https://github.com/anolilab/lunora/blob/alpha/.github/CONTRIBUTING.md) guidelines.
|
|
222
|
+
|
|
223
|
+
> **Note:** please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
|
|
224
|
+
|
|
225
|
+
## Credits
|
|
226
|
+
|
|
227
|
+
- [Daniel Bannert](https://github.com/prisis)
|
|
228
|
+
- [All Contributors](https://github.com/anolilab/lunora/graphs/contributors)
|
|
229
|
+
|
|
230
|
+
## Made with ❤️ at Anolilab
|
|
231
|
+
|
|
232
|
+
This is an open source project and will always remain free to use. If you think it's cool, please star it 🌟. [Anolilab](https://www.anolilab.com/open-source) is a Development and AI Studio. Contact us at [hello@anolilab.com](mailto:hello@anolilab.com) if you need any help with these technologies or just want to say hi!
|
|
15
233
|
|
|
16
234
|
## License
|
|
17
235
|
|
|
18
|
-
FSL-1.1-Apache-2.0
|
|
236
|
+
The Lunora testing package is open-sourced software licensed under the [FSL-1.1-Apache-2.0][license].
|
|
237
|
+
|
|
238
|
+
<!-- badges -->
|
|
239
|
+
|
|
240
|
+
[license-badge]: https://img.shields.io/badge/license-FSL--1.1--Apache--2.0-blue.svg?style=for-the-badge
|
|
241
|
+
[license]: https://github.com/anolilab/lunora/blob/alpha/LICENSE.md
|
|
242
|
+
[npm-version-badge]: https://img.shields.io/npm/v/@lunora/testing?style=for-the-badge
|
|
243
|
+
[npm-version]: https://www.npmjs.com/package/@lunora/testing
|
|
244
|
+
[npm-downloads-badge]: https://img.shields.io/npm/dm/@lunora/testing?style=for-the-badge
|
|
245
|
+
[npm-downloads]: https://www.npmjs.com/package/@lunora/testing
|
|
246
|
+
[prs-welcome-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=for-the-badge
|
|
247
|
+
[prs-welcome]: https://github.com/anolilab/lunora/blob/alpha/.github/CONTRIBUTING.md
|
|
248
|
+
[typescript-badge]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
|
|
249
|
+
[typescript-url]: https://www.typescriptlang.org/
|