primitive-functions 0.3.0-alpha.18 → 0.3.0-alpha.19
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 +32 -3
- package/index.d.ts +467 -69
- package/package.json +10 -2
- package/testing-members.generated.js +502 -0
- package/testing.d.ts +239 -0
- package/testing.js +142 -0
package/testing.d.ts
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test doubles for a server function's `ctx` and `step`, for unit tests that
|
|
3
|
+
* run a handler offline under Node.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { createTestContext, createTestStep } from "primitive-functions/testing";
|
|
7
|
+
*
|
|
8
|
+
* const ctx = createTestContext({
|
|
9
|
+
* runId: "run-1",
|
|
10
|
+
* configVar: async () => "doc-1",
|
|
11
|
+
* });
|
|
12
|
+
* const step = createTestStep();
|
|
13
|
+
*
|
|
14
|
+
* await handler(input, ctx, step);
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Override only the members the test reads. The override types are derived
|
|
18
|
+
* from `FunctionContext` and `FunctionStep`, so an override that no longer
|
|
19
|
+
* matches the declaration is a compile error. A member the test did not
|
|
20
|
+
* override throws `TEST_MEMBER_NOT_PROVIDED`, naming itself, when it is called
|
|
21
|
+
* (or, for a data member such as `ctx.runId`, read).
|
|
22
|
+
*
|
|
23
|
+
* For tests only: a function bundle that imports this module is refused by
|
|
24
|
+
* `config push`.
|
|
25
|
+
*
|
|
26
|
+
* @module
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import type { Db3Handle, Db3ModelNames, Db3TypeKeys, FunctionContext, FunctionStep } from "primitive-functions";
|
|
30
|
+
|
|
31
|
+
type AnyFunction = (...args: any) => any;
|
|
32
|
+
|
|
33
|
+
// Each declared call signature of `T` as an `[args, result]` pair. Matching
|
|
34
|
+
// four signatures yields each overload once, and a single signature once. A
|
|
35
|
+
// generic signature's type parameters take their constraints here, so a
|
|
36
|
+
// signature keyed by a db3 type or a model name is split by key below.
|
|
37
|
+
type Signatures<T> = T extends {
|
|
38
|
+
(...args: infer A1): infer R1;
|
|
39
|
+
(...args: infer A2): infer R2;
|
|
40
|
+
(...args: infer A3): infer R3;
|
|
41
|
+
(...args: infer A4): infer R4;
|
|
42
|
+
}
|
|
43
|
+
? [A1, R1] | [A2, R2] | [A3, R3] | [A4, R4]
|
|
44
|
+
: never;
|
|
45
|
+
|
|
46
|
+
// The intersection of `F<K>` for each `K` of a union, each `F<K>` kept whole
|
|
47
|
+
// (a union of overload overrides stays a union inside it).
|
|
48
|
+
type EachOf<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
49
|
+
|
|
50
|
+
// `FunctionContext` as declared, to instantiate `ctx.db3`'s typed overloads
|
|
51
|
+
// for one db3 type at a time. Not exported (see `export {}` at the end).
|
|
52
|
+
declare const declaredContext: FunctionContext;
|
|
53
|
+
type TypedDb3<K extends Db3TypeKeys> = typeof declaredContext.db3<K>;
|
|
54
|
+
|
|
55
|
+
type Db3TypeOverride<K> = K extends Db3TypeKeys ? { override: TestFunctionOverride<TypedDb3<K>> } : never;
|
|
56
|
+
|
|
57
|
+
type Db3ModelOverride<TModels, TQueries, M> = M extends keyof TModels
|
|
58
|
+
? { override: TestFunctionOverride<Db3Handle<Pick<TModels, M>, TQueries>["model"]> }
|
|
59
|
+
: never;
|
|
60
|
+
|
|
61
|
+
// `model` on a typed db3 handle, one model name at a time: what an override
|
|
62
|
+
// returns for a name must fit that model's row, and an override that takes no
|
|
63
|
+
// name, or any name, answers for every model of the type.
|
|
64
|
+
type Db3HandleModelOverride<TModels, TQueries> = string extends Db3ModelNames<TModels>
|
|
65
|
+
? TestFunctionOverride<Db3Handle<TModels, TQueries>["model"]>
|
|
66
|
+
: EachOf<Db3ModelOverride<TModels, TQueries, Db3ModelNames<TModels>>> extends infer O
|
|
67
|
+
? O extends { override: unknown }
|
|
68
|
+
? O["override"]
|
|
69
|
+
: never
|
|
70
|
+
: never;
|
|
71
|
+
|
|
72
|
+
// An object whose every member is a function or, recursively, another such
|
|
73
|
+
// object: a namespace of `ctx` (`ctx.users`, `ctx.api.groups`).
|
|
74
|
+
type IsNamespace<T> = [T] extends [object]
|
|
75
|
+
? [T] extends [AnyFunction]
|
|
76
|
+
? false
|
|
77
|
+
: [keyof T] extends [never]
|
|
78
|
+
? false
|
|
79
|
+
: false extends { [K in keyof T]-?: T[K] extends AnyFunction ? true : IsNamespace<T[K]> }[keyof T]
|
|
80
|
+
? false
|
|
81
|
+
: true
|
|
82
|
+
: false;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* What a function override may be for a member declared as `T`: a function
|
|
86
|
+
* whose parameters match one of `T`'s declared signatures and whose result is
|
|
87
|
+
* a {@link TestPartialResult} of that signature's result.
|
|
88
|
+
*
|
|
89
|
+
* An overloaded member accepts an implementation of any one overload; annotate
|
|
90
|
+
* its parameters, since no single overload types them.
|
|
91
|
+
*
|
|
92
|
+
* @beta
|
|
93
|
+
*/
|
|
94
|
+
export type TestFunctionOverride<T> =
|
|
95
|
+
Signatures<T> extends infer S
|
|
96
|
+
? S extends [infer A extends any[], infer R]
|
|
97
|
+
? (...args: A) => TestPartialResult<R>
|
|
98
|
+
: never
|
|
99
|
+
: never;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* What a `ctx.db3` override may be: a {@link TestFunctionOverride} of
|
|
103
|
+
* `ctx.db3`. In a tree whose pushed types declare db3 types, its typed call
|
|
104
|
+
* forms are checked one declared type at a time, and a typed handle's `model`
|
|
105
|
+
* one model name at a time: an override that takes no `type` (or any `type`)
|
|
106
|
+
* returns a handle that must fit every declared type, so a field of the wrong
|
|
107
|
+
* type in a row it returns is a compile error. Leave the `type` parameter off
|
|
108
|
+
* a `db3` override, or type it `string`.
|
|
109
|
+
*
|
|
110
|
+
* @beta
|
|
111
|
+
*/
|
|
112
|
+
export type TestDb3Override = string extends Db3TypeKeys
|
|
113
|
+
? TestFunctionOverride<FunctionContext["db3"]>
|
|
114
|
+
: EachOf<Db3TypeOverride<Db3TypeKeys>> extends infer O
|
|
115
|
+
? O extends { override: unknown }
|
|
116
|
+
? O["override"]
|
|
117
|
+
: never
|
|
118
|
+
: never;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A declared result with every field optional, at any depth: through a
|
|
122
|
+
* `Promise`, into objects and arrays, and into the functions the result holds
|
|
123
|
+
* (whose own results are partial in the same way). A field the override does
|
|
124
|
+
* return keeps its declared type.
|
|
125
|
+
*
|
|
126
|
+
* ```ts
|
|
127
|
+
* // ctx.db3 returns a handle; the test returns only what its code calls.
|
|
128
|
+
* createTestContext({ db3: () => ({ model: () => ({ query }) }) });
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* The double does not complete what an override returns: a method the test
|
|
132
|
+
* left out of a partial handle is simply absent.
|
|
133
|
+
*
|
|
134
|
+
* @beta
|
|
135
|
+
*/
|
|
136
|
+
export type TestPartialResult<T> =
|
|
137
|
+
T extends PromiseLike<infer U>
|
|
138
|
+
? Promise<TestPartialResult<U>>
|
|
139
|
+
: T extends AnyFunction
|
|
140
|
+
? TestFunctionOverride<T>
|
|
141
|
+
: T extends Db3Handle<infer TModels, infer TQueries>
|
|
142
|
+
? { [K in keyof T]?: K extends "model" ? Db3HandleModelOverride<TModels, TQueries> : TestPartialResult<T[K]> }
|
|
143
|
+
: T extends object
|
|
144
|
+
? // `& object` keeps a primitive from passing for an object where
|
|
145
|
+
// TypeScript skips its all-optional ("weak") type check: inside
|
|
146
|
+
// the per-type and per-model intersections above.
|
|
147
|
+
{ [K in keyof T]?: TestPartialResult<T[K]> } & object
|
|
148
|
+
: T;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The override for one member declared as `T`: a {@link TestNamespaceOverrides}
|
|
152
|
+
* for a namespace, a {@link TestFunctionOverride} for a function, and a value of
|
|
153
|
+
* the declared type for a data member.
|
|
154
|
+
*
|
|
155
|
+
* @beta
|
|
156
|
+
*/
|
|
157
|
+
export type TestMemberOverride<T> =
|
|
158
|
+
IsNamespace<T> extends true
|
|
159
|
+
? TestNamespaceOverrides<T>
|
|
160
|
+
: [T] extends [AnyFunction]
|
|
161
|
+
? TestFunctionOverride<T>
|
|
162
|
+
: T;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Overrides for a namespace declared as `T`, every member optional. At run
|
|
166
|
+
* time a namespace override must be a plain object (an object literal, or one
|
|
167
|
+
* with a `null` prototype): the double builds its own namespace from the
|
|
168
|
+
* members it holds, so every member the test left out throws by name. A class
|
|
169
|
+
* instance, an array or a function is refused when the double is created; the
|
|
170
|
+
* type cannot tell a class instance from a plain object of the same shape.
|
|
171
|
+
*
|
|
172
|
+
* @beta
|
|
173
|
+
*/
|
|
174
|
+
export type TestNamespaceOverrides<T> = { [K in keyof T]?: TestMemberOverride<T[K]> };
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The overrides {@link createTestContext} accepts: any member of
|
|
178
|
+
* `FunctionContext`, each typed from its declaration, plus `auth` for an
|
|
179
|
+
* object-form handler.
|
|
180
|
+
*
|
|
181
|
+
* @typeParam TAuth - The type of `ctx.auth`, inferred from the `auth` override.
|
|
182
|
+
* @beta
|
|
183
|
+
*/
|
|
184
|
+
export type TestContextOverrides<TAuth = unknown> = Omit<TestNamespaceOverrides<FunctionContext>, "db3"> & {
|
|
185
|
+
/** `ctx.db3`: see {@link TestDb3Override}. */
|
|
186
|
+
db3?: TestDb3Override;
|
|
187
|
+
/** `ctx.auth` for an object-form handler: what its `authorize` resolved. */
|
|
188
|
+
auth?: TAuth;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The overrides {@link createTestStep} accepts: any member of `FunctionStep`,
|
|
193
|
+
* with its declared type. A `do` override must handle both call forms,
|
|
194
|
+
* `do(name, body)` and `do(name, config, body)`, as the declaration does.
|
|
195
|
+
*
|
|
196
|
+
* @beta
|
|
197
|
+
*/
|
|
198
|
+
export type TestStepOverrides = Partial<FunctionStep>;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Build a `FunctionContext` for a unit test.
|
|
202
|
+
*
|
|
203
|
+
* Every member the overrides do not set is still present: a function throws an
|
|
204
|
+
* `Error` named `TEST_MEMBER_NOT_PROVIDED`, whose `member` is the dotted path
|
|
205
|
+
* (`ctx.users.send`), when called; a required data member (`user`, `trigger`,
|
|
206
|
+
* `runtime`, `runId`, `sliceId`, and `auth`) throws the same error when read;
|
|
207
|
+
* `tool` reads `undefined`.
|
|
208
|
+
*
|
|
209
|
+
* Throws a `TypeError` naming the path when an override key is not a member of
|
|
210
|
+
* `FunctionContext`, when a function member is given something other than a
|
|
211
|
+
* function, or when a namespace override is not a plain object.
|
|
212
|
+
*
|
|
213
|
+
* @param overrides - The members the test provides.
|
|
214
|
+
* @returns A fresh context; with an `auth` override, typed as carrying it.
|
|
215
|
+
* @beta
|
|
216
|
+
*/
|
|
217
|
+
export declare function createTestContext<TAuth = never>(
|
|
218
|
+
overrides?: TestContextOverrides<TAuth>
|
|
219
|
+
): [TAuth] extends [never] ? FunctionContext : FunctionContext & { auth: TAuth };
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Build a `FunctionStep` for a unit test.
|
|
223
|
+
*
|
|
224
|
+
* `step.do(name, body)` and `step.do(name, config, body)` run the body once
|
|
225
|
+
* and resolve to its value, or reject with what it threw, as a request-runtime
|
|
226
|
+
* step does. `sleep`, `sleepUntil` and `waitForEvent` throw
|
|
227
|
+
* `TEST_MEMBER_NOT_PROVIDED` unless overridden.
|
|
228
|
+
*
|
|
229
|
+
* Throws a `TypeError` naming the path when an override key is not a member of
|
|
230
|
+
* `FunctionStep` or is not a function.
|
|
231
|
+
*
|
|
232
|
+
* @param overrides - The members the test provides.
|
|
233
|
+
* @returns A fresh step.
|
|
234
|
+
* @beta
|
|
235
|
+
*/
|
|
236
|
+
export declare function createTestStep(overrides?: TestStepOverrides): FunctionStep;
|
|
237
|
+
|
|
238
|
+
// Keeps the declarations above without an `export` keyword private to this module.
|
|
239
|
+
export {};
|
package/testing.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Test doubles for a server function's `ctx` and `step`. See testing.d.ts.
|
|
2
|
+
//
|
|
3
|
+
// Unlike the package's main entry, which the platform replaces at invoke
|
|
4
|
+
// time, this module is real code: it runs under Node with no platform, for
|
|
5
|
+
// unit tests only. Each double is a plain object built from the member tree
|
|
6
|
+
// `pnpm functions:profile` generates from the declaration, so every declared
|
|
7
|
+
// member is present — not a Proxy, which would answer `then`, `toJSON` and
|
|
8
|
+
// every other probe a test runner makes.
|
|
9
|
+
|
|
10
|
+
import { TEST_MEMBERS } from "./testing-members.generated.js";
|
|
11
|
+
|
|
12
|
+
const NOT_PROVIDED = "TEST_MEMBER_NOT_PROVIDED";
|
|
13
|
+
|
|
14
|
+
const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
|
|
15
|
+
|
|
16
|
+
function isPlainObject(value) {
|
|
17
|
+
if (value === null || typeof value !== "object") return false;
|
|
18
|
+
const prototype = Object.getPrototypeOf(value);
|
|
19
|
+
return prototype === Object.prototype || prototype === null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// `ctx.users.send` → `{ users: { send: … } }`: the override that provides it.
|
|
23
|
+
function overrideFor(member) {
|
|
24
|
+
return member
|
|
25
|
+
.split(".")
|
|
26
|
+
.slice(1)
|
|
27
|
+
.reduceRight((inner, key) => `{ ${key}: ${inner} }`, "…");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function notProvided(member, factory) {
|
|
31
|
+
const error = new Error(
|
|
32
|
+
`${member} was not provided to the test double: pass it to ${factory}() as ${overrideFor(member)}`
|
|
33
|
+
);
|
|
34
|
+
error.name = NOT_PROVIDED;
|
|
35
|
+
error.member = member;
|
|
36
|
+
return error;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function refused(member, problem) {
|
|
40
|
+
const error = new TypeError(`${member} ${problem}`);
|
|
41
|
+
error.member = member;
|
|
42
|
+
return error;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Refuse what the double cannot honour, before anything is built.
|
|
46
|
+
function validate(tree, overrides, path, extraKeys) {
|
|
47
|
+
if (!isPlainObject(overrides)) {
|
|
48
|
+
throw refused(path, "must be a plain object of overrides (an object literal), not a class instance, array, function or primitive");
|
|
49
|
+
}
|
|
50
|
+
for (const key of Reflect.ownKeys(overrides)) {
|
|
51
|
+
const member = `${path}.${String(key)}`;
|
|
52
|
+
if (typeof key === "symbol" || !hasOwn(tree, key)) {
|
|
53
|
+
if (typeof key === "string" && extraKeys.includes(key)) continue;
|
|
54
|
+
throw refused(member, "is not a declared member, so it cannot be overridden");
|
|
55
|
+
}
|
|
56
|
+
const kind = tree[key];
|
|
57
|
+
const value = overrides[key];
|
|
58
|
+
if (kind === "fn") {
|
|
59
|
+
if (typeof value !== "function") throw refused(member, "is a function: its override must be a function");
|
|
60
|
+
} else if (typeof kind === "object") {
|
|
61
|
+
validate(kind, value, member, []);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function throwingMember(member, factory) {
|
|
67
|
+
return function notProvidedMember() {
|
|
68
|
+
throw notProvided(member, factory);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A data member the test did not set: throws on read, and a test may still
|
|
73
|
+
// assign it later. Non-enumerable, so spreading or printing the double never
|
|
74
|
+
// reads it.
|
|
75
|
+
function defineThrowingData(target, key, member, factory) {
|
|
76
|
+
Object.defineProperty(target, key, {
|
|
77
|
+
configurable: true,
|
|
78
|
+
enumerable: false,
|
|
79
|
+
get() {
|
|
80
|
+
throw notProvided(member, factory);
|
|
81
|
+
},
|
|
82
|
+
set(value) {
|
|
83
|
+
Object.defineProperty(this, key, { value, writable: true, enumerable: true, configurable: true });
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function build(tree, overrides, path, factory) {
|
|
89
|
+
const built = {};
|
|
90
|
+
for (const [key, kind] of Object.entries(tree)) {
|
|
91
|
+
const member = `${path}.${key}`;
|
|
92
|
+
const given = hasOwn(overrides, key);
|
|
93
|
+
if (typeof kind === "object") {
|
|
94
|
+
// A namespace is always the double's own object, so every member the
|
|
95
|
+
// test left out is present and throws by name.
|
|
96
|
+
built[key] = build(kind, given ? overrides[key] : {}, member, factory);
|
|
97
|
+
} else if (given) {
|
|
98
|
+
built[key] = overrides[key];
|
|
99
|
+
} else if (kind === "fn") {
|
|
100
|
+
built[key] = throwingMember(member, factory);
|
|
101
|
+
} else if (kind === "data") {
|
|
102
|
+
defineThrowingData(built, key, member, factory);
|
|
103
|
+
}
|
|
104
|
+
// "data?" not given: absent, so it reads `undefined`.
|
|
105
|
+
}
|
|
106
|
+
return built;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Build a `FunctionContext` for a unit test. See testing.d.ts.
|
|
111
|
+
*
|
|
112
|
+
* @param {object} [overrides] The members the test provides.
|
|
113
|
+
* @returns {object} A fresh context.
|
|
114
|
+
*/
|
|
115
|
+
export function createTestContext(overrides = {}) {
|
|
116
|
+
validate(TEST_MEMBERS.ctx, overrides, "ctx", ["auth"]);
|
|
117
|
+
const ctx = build(TEST_MEMBERS.ctx, overrides, "ctx", "createTestContext");
|
|
118
|
+
if (hasOwn(overrides, "auth")) ctx.auth = overrides.auth;
|
|
119
|
+
else defineThrowingData(ctx, "auth", "ctx.auth", "createTestContext");
|
|
120
|
+
return ctx;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Build a `FunctionStep` for a unit test. See testing.d.ts.
|
|
125
|
+
*
|
|
126
|
+
* @param {object} [overrides] The members the test provides.
|
|
127
|
+
* @returns {object} A fresh step.
|
|
128
|
+
*/
|
|
129
|
+
export function createTestStep(overrides = {}) {
|
|
130
|
+
validate(TEST_MEMBERS.step, overrides, "step", []);
|
|
131
|
+
const step = build(TEST_MEMBERS.step, overrides, "step", "createTestStep");
|
|
132
|
+
if (!hasOwn(overrides, "do")) {
|
|
133
|
+
// The request runtime's rule: the body is the second argument, or the
|
|
134
|
+
// third after a config; it runs once and its result is the step's.
|
|
135
|
+
step.do = async (name, configOrBody, maybeBody) => {
|
|
136
|
+
const body = typeof configOrBody === "function" ? configOrBody : maybeBody;
|
|
137
|
+
if (typeof body !== "function") throw new TypeError(`step.do("${name}") requires a function body`);
|
|
138
|
+
return body();
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return step;
|
|
142
|
+
}
|