logic-sdk 0.1.0 → 0.3.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/src/testing.ts CHANGED
@@ -1,17 +1,19 @@
1
- import { internal, type Caller, type CallerOptions, type Envelope, type Fn, type FnIn, type FnOut, type Registry } from "./index.ts";
1
+ import { internal, type Caller, type DeepPartial, type Envelope, type Fn, type FnIn, type FnOut, type Registry } from "./index.ts";
2
2
 
3
- export interface TestOptions {
3
+ export interface TestOptions<Cf = unknown> {
4
4
  /** Replace services (e.g. `{ mailer: memoryMailer() }`). */
5
5
  services?: object;
6
+ /** Override parts of `config` (e.g. `{ orders: { maxItems: 1 } }`). Merged over the registry's, section by section. */
7
+ config?: DeepPartial<Cf>;
6
8
  /** Freeze time. */
7
9
  now?: Date | string | (() => Date);
8
10
  /** Run listeners inline after each mutation (default), or not at all to test one module in isolation. */
9
11
  listeners?: boolean;
10
12
  }
11
13
 
12
- export interface TestTransaction<S extends object, Mods extends Record<string, object> = Record<string, object>> {
14
+ export interface TestTransaction<S extends object, Mods extends Record<string, object> = Record<string, object>, Cf = unknown> {
13
15
  /** Bind who is calling, exactly like `registry.as(...)`. */
14
- as(scope: S, opts?: CallerOptions): Caller<S>;
16
+ as(scope: S): Caller<S>;
15
17
  /** Replace a function's implementation for this test. Matched by identity, so `run(availability.hold, …)` hits the stub. */
16
18
  stub<F extends Fn>(fn: F, impl: (input: FnIn<F>, ctx: any) => Promise<FnOut<F>>): void;
17
19
  /** Every event emitted (and committed) so far, in order. */
@@ -19,7 +21,7 @@ export interface TestTransaction<S extends object, Mods extends Record<string, o
19
21
  /** The test transaction itself: read rows to assert on them. */
20
22
  readonly db: any;
21
23
  /** The registry bound to this test transaction, for adapters: `toFetchHandler(tx.registry, …)`. */
22
- readonly registry: Registry<S, Mods>;
24
+ readonly registry: Registry<S, Mods, Cf>;
23
25
  }
24
26
 
25
27
  class Rollback extends Error {}
@@ -29,9 +31,9 @@ class Rollback extends Error {}
29
31
  * Every top-level mutation inside becomes a savepoint, so a failing mutation rolls back just its own writes,
30
32
  * like it would in production. Run top-level mutations one at a time inside a test (one connection, nested savepoints).
31
33
  */
32
- export function createTestRegistry<S extends object, Mods extends Record<string, object>>(
33
- registry: Registry<S, Mods>,
34
- options: TestOptions = {},
34
+ export function createTestRegistry<S extends object, Mods extends Record<string, object>, Cf>(
35
+ registry: Registry<S, Mods, Cf>,
36
+ options: TestOptions<Cf> = {},
35
37
  ) {
36
38
  const { options: registryOptions } = internal(registry);
37
39
  const now =
@@ -45,7 +47,7 @@ export function createTestRegistry<S extends object, Mods extends Record<string,
45
47
  })();
46
48
 
47
49
  return {
48
- async transaction<T>(fn: (t: TestTransaction<S, Mods>) => Promise<T>): Promise<T> {
50
+ async transaction<T>(fn: (t: TestTransaction<S, Mods, Cf>) => Promise<T>): Promise<T> {
49
51
  const database = registryOptions.database;
50
52
  let result!: T;
51
53
 
@@ -55,13 +57,14 @@ export function createTestRegistry<S extends object, Mods extends Record<string,
55
57
  const derived = internal(registry).derive({
56
58
  database: outer !== undefined && database?.nest ? database.nest(outer) : database,
57
59
  services: options.services,
60
+ config: options.config as object | undefined,
58
61
  now,
59
62
  stubs,
60
63
  record: (e) => events.push(e),
61
64
  events: options.listeners === false ? { deliver: async () => {} } : undefined,
62
- }) as Registry<S, Mods>;
65
+ }) as Registry<S, Mods, Cf>;
63
66
  result = await fn({
64
- as: (scope, opts) => derived.as(scope, opts),
67
+ as: (scope) => derived.as(scope),
65
68
  stub: (f, impl) => void stubs.set(f, impl as (input: any, ctx: any) => Promise<any>),
66
69
  events,
67
70
  db: outer,