@rdlabo/workers-hono-kit 0.4.0 → 0.4.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/README.md CHANGED
@@ -143,6 +143,7 @@ Requires the `drizzle-orm` and `mysql2` peers. Consolidates duplicated test boil
143
143
  | `provisionUser(pool, firebase, opts)` | Register a token and provision a conventional `users(id, firebase_uid, agree)` row; returns the user id (idempotent). |
144
144
  | `configurableFake(impl, name?)` | Build a test double from a partial implementation; un-stubbed members throw `"${name}.${method} not configured"`. |
145
145
  | `fakeApiList` / `fakePaymentIntent` / `fakeStripeEvent` / `fakeCheckoutSession` / `fakeCustomer` / `fakePrice` / `fakeSubscription` | Stripe object fixtures with sensible defaults, overridable per test. |
146
+ | `fakeKv()` / `fakeQueue()` / `FakeQueue` | In-memory Workers KV / Queues producer doubles (`sent` + `batchCount` on queues for subrequest-bound assertions). |
146
147
 
147
148
  ## Usage
148
149
 
@@ -13,3 +13,5 @@ export type { Database, DisposableDatabase, QueryRunner, TxOf } from '../db/data
13
13
  export { authHeaders, registerFirebaseToken, provisionUser } from './auth.js';
14
14
  export { configurableFake } from './configurable-fake.js';
15
15
  export { fakeApiList, fakePaymentIntent, fakeStripeEvent, fakeCheckoutSession, fakeCustomer, fakePrice, fakeSubscription, } from './stripe-fixtures.js';
16
+ export { fakeKv, fakeQueue } from './workers-bindings.js';
17
+ export type { FakeQueue } from './workers-bindings.js';
@@ -13,3 +13,5 @@ export { authHeaders, registerFirebaseToken, provisionUser } from './auth.js';
13
13
  export { configurableFake } from './configurable-fake.js';
14
14
  // Test fixture factories for Stripe objects.
15
15
  export { fakeApiList, fakePaymentIntent, fakeStripeEvent, fakeCheckoutSession, fakeCustomer, fakePrice, fakeSubscription, } from './stripe-fixtures.js';
16
+ // In-memory Workers binding fakes (KV / Queues producer).
17
+ export { fakeKv, fakeQueue } from './workers-bindings.js';
@@ -0,0 +1,49 @@
1
+ import type { KVNamespace } from '../cache/kv-cache.js';
2
+ import type { QueueLike } from '../queue/send.js';
3
+ /**
4
+ * In-memory {@link QueueLike} test double that records every enqueued message.
5
+ *
6
+ * @remarks
7
+ * `sent` collects all message bodies (from both {@link FakeQueue.send} and
8
+ * {@link FakeQueue.sendBatch}). `batchCount` increments once per `sendBatch` call so tests can
9
+ * assert producers bound subrequests to `ceil(N / chunkSize)` rather than `N`.
10
+ *
11
+ * @typeParam Body - Message body type.
12
+ */
13
+ export interface FakeQueue<Body = unknown> extends QueueLike<Body> {
14
+ /** Every body passed to `send` or `sendBatch`, in enqueue order. */
15
+ readonly sent: Body[];
16
+ /** Number of `sendBatch` calls issued. */
17
+ readonly batchCount: number;
18
+ /**
19
+ * Enqueue a single message (one subrequest in production).
20
+ *
21
+ * @param body - Message payload.
22
+ */
23
+ send(body: Body): Promise<void>;
24
+ }
25
+ /**
26
+ * Create an in-memory {@link FakeQueue} for offline producer tests.
27
+ *
28
+ * @typeParam Body - Message body type.
29
+ * @returns A queue double assignable to `QueueLike` / Workers `Queue` bindings in tests.
30
+ * @example
31
+ * ```ts
32
+ * const queue = fakeQueue<{ userId: number }>();
33
+ * await sendInChunks(queue, [1, 2, 3]);
34
+ * expect(queue.batchCount).toBe(1);
35
+ * expect(queue.sent).toEqual([1, 2, 3]);
36
+ * ```
37
+ */
38
+ export declare function fakeQueue<Body = unknown>(): FakeQueue<Body>;
39
+ /**
40
+ * Create a minimal in-memory {@link KVNamespace} for offline tests (`KVCache`, env fixtures, etc.).
41
+ *
42
+ * @remarks
43
+ * Only `get` / `put` / `delete` are fully implemented (the subset {@link KVCache} uses). `list` and
44
+ * `getWithMetadata` return empty/null stubs so the object is structurally assignable to Workers
45
+ * `KVNamespace` when tests need a binding-shaped fake env.
46
+ *
47
+ * @returns An in-memory KV double.
48
+ */
49
+ export declare function fakeKv(): KVNamespace;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Create an in-memory {@link FakeQueue} for offline producer tests.
3
+ *
4
+ * @typeParam Body - Message body type.
5
+ * @returns A queue double assignable to `QueueLike` / Workers `Queue` bindings in tests.
6
+ * @example
7
+ * ```ts
8
+ * const queue = fakeQueue<{ userId: number }>();
9
+ * await sendInChunks(queue, [1, 2, 3]);
10
+ * expect(queue.batchCount).toBe(1);
11
+ * expect(queue.sent).toEqual([1, 2, 3]);
12
+ * ```
13
+ */
14
+ export function fakeQueue() {
15
+ const sent = [];
16
+ let batchCount = 0;
17
+ return {
18
+ get sent() {
19
+ return sent;
20
+ },
21
+ get batchCount() {
22
+ return batchCount;
23
+ },
24
+ send(body) {
25
+ sent.push(body);
26
+ return Promise.resolve();
27
+ },
28
+ sendBatch(messages) {
29
+ batchCount++;
30
+ for (const m of messages) {
31
+ sent.push(m.body);
32
+ }
33
+ return Promise.resolve();
34
+ },
35
+ };
36
+ }
37
+ /**
38
+ * Create a minimal in-memory {@link KVNamespace} for offline tests (`KVCache`, env fixtures, etc.).
39
+ *
40
+ * @remarks
41
+ * Only `get` / `put` / `delete` are fully implemented (the subset {@link KVCache} uses). `list` and
42
+ * `getWithMetadata` return empty/null stubs so the object is structurally assignable to Workers
43
+ * `KVNamespace` when tests need a binding-shaped fake env.
44
+ *
45
+ * @returns An in-memory KV double.
46
+ */
47
+ export function fakeKv() {
48
+ const store = new Map();
49
+ return {
50
+ get: (key) => Promise.resolve(store.get(key) ?? null),
51
+ put: (key, value) => {
52
+ store.set(key, value);
53
+ return Promise.resolve();
54
+ },
55
+ delete: (key) => {
56
+ store.delete(key);
57
+ return Promise.resolve();
58
+ },
59
+ list: () => Promise.resolve({ keys: [], list_complete: true, cacheStatus: null }),
60
+ getWithMetadata: () => Promise.resolve({ value: null, metadata: null, cacheStatus: null }),
61
+ };
62
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rdlabo/workers-hono-kit",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
File without changes