@fuzdev/fuz_app 0.185.0 → 0.186.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/dist/actions/CLAUDE.md +14 -2
- package/dist/actions/perform_action.d.ts.map +1 -1
- package/dist/actions/perform_action.js +36 -5
- package/dist/actions/ws_socket_session.d.ts.map +1 -1
- package/dist/actions/ws_socket_session.js +3 -1
- package/dist/auth/CLAUDE.md +11 -5
- package/dist/auth/account_queries.d.ts +53 -40
- package/dist/auth/account_queries.d.ts.map +1 -1
- package/dist/auth/account_queries.js +57 -50
- package/dist/auth/account_routes.d.ts.map +1 -1
- package/dist/auth/account_routes.js +59 -25
- package/dist/auth/admin_actions.d.ts.map +1 -1
- package/dist/auth/admin_actions.js +26 -9
- package/dist/auth/audit_emitter.js +2 -2
- package/dist/auth/audit_log_queries.d.ts +11 -3
- package/dist/auth/audit_log_queries.d.ts.map +1 -1
- package/dist/auth/audit_log_queries.js +4 -4
- package/dist/auth/audit_log_schema.js +1 -1
- package/dist/auth/auth_ddl.d.ts.map +1 -1
- package/dist/auth/auth_ddl.js +4 -0
- package/dist/auth/cleanup.js +1 -1
- package/dist/auth/migrations.d.ts.map +1 -1
- package/dist/auth/migrations.js +35 -0
- package/dist/auth/role_grant_offer_queries.d.ts +13 -13
- package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_queries.js +12 -13
- package/dist/auth/role_grant_queries.d.ts +16 -9
- package/dist/auth/role_grant_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_queries.js +16 -9
- package/dist/auth/self_service_role_actions.d.ts.map +1 -1
- package/dist/auth/self_service_role_actions.js +15 -0
- package/dist/auth/session_queries.d.ts +24 -0
- package/dist/auth/session_queries.d.ts.map +1 -1
- package/dist/auth/session_queries.js +28 -0
- package/dist/db/CLAUDE.md +3 -3
- package/dist/db/cell_ddl.d.ts +8 -3
- package/dist/db/cell_ddl.d.ts.map +1 -1
- package/dist/db/cell_ddl.js +10 -5
- package/dist/http/body_limit.d.ts +4 -4
- package/dist/http/body_limit.js +4 -4
- package/dist/testing/CLAUDE.md +126 -27
- package/dist/testing/app_server.d.ts +5 -7
- package/dist/testing/app_server.d.ts.map +1 -1
- package/dist/testing/app_server.js +5 -7
- package/dist/testing/auth_apps.d.ts +3 -1
- package/dist/testing/auth_apps.d.ts.map +1 -1
- package/dist/testing/auth_apps.js +22 -5
- package/dist/testing/cross_backend/backend_config.d.ts +8 -0
- package/dist/testing/cross_backend/backend_config.d.ts.map +1 -1
- package/dist/testing/cross_backend/backend_log.d.ts +71 -0
- package/dist/testing/cross_backend/backend_log.d.ts.map +1 -0
- package/dist/testing/cross_backend/backend_log.js +134 -0
- package/dist/testing/cross_backend/backend_swap.d.ts +41 -0
- package/dist/testing/cross_backend/backend_swap.d.ts.map +1 -0
- package/dist/testing/cross_backend/backend_swap.js +431 -0
- package/dist/testing/cross_backend/body_size_smuggling.js +3 -3
- package/dist/testing/cross_backend/bootstrap_backend.js +4 -4
- package/dist/testing/cross_backend/conformance_case.d.ts +11 -6
- package/dist/testing/cross_backend/conformance_case.d.ts.map +1 -1
- package/dist/testing/cross_backend/conformance_case.js +4 -0
- package/dist/testing/cross_backend/conformance_principal.d.ts +2 -2
- package/dist/testing/cross_backend/conformance_principal.d.ts.map +1 -1
- package/dist/testing/cross_backend/conformance_principal.js +27 -10
- package/dist/testing/cross_backend/default_backend_configs.d.ts +38 -5
- package/dist/testing/cross_backend/default_backend_configs.d.ts.map +1 -1
- package/dist/testing/cross_backend/default_backend_configs.js +50 -15
- package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/in_process_setup.js +52 -30
- package/dist/testing/cross_backend/revocation_drivers.d.ts +14 -2
- package/dist/testing/cross_backend/revocation_drivers.d.ts.map +1 -1
- package/dist/testing/cross_backend/revocation_drivers.js +36 -8
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts +7 -0
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts.map +1 -1
- package/dist/testing/cross_backend/rust_spine_stub_backend_config.js +4 -2
- package/dist/testing/cross_backend/setup.d.ts +44 -43
- package/dist/testing/cross_backend/setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/setup.js +64 -62
- package/dist/testing/cross_backend/spawn_backend.d.ts +7 -3
- package/dist/testing/cross_backend/spawn_backend.d.ts.map +1 -1
- package/dist/testing/cross_backend/spawn_backend.js +86 -7
- package/dist/testing/cross_backend/spine_suites.d.ts +1 -1
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +4 -2
- package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
- package/dist/testing/cross_backend/sse_round_trip.js +4 -2
- package/dist/testing/cross_backend/testing_reset_actions.d.ts +7 -6
- package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_reset_actions.js +10 -8
- package/dist/testing/cross_backend/testing_server_bun.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_server_bun.js +7 -31
- package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
- package/dist/testing/cross_backend/ws_round_trip.js +4 -3
- package/dist/testing/data_exposure.d.ts +4 -1
- package/dist/testing/data_exposure.d.ts.map +1 -1
- package/dist/testing/data_exposure.js +93 -25
- package/dist/testing/db_invariants.d.ts +5 -5
- package/dist/testing/db_invariants.js +5 -5
- package/dist/testing/logged_errors.d.ts +61 -0
- package/dist/testing/logged_errors.d.ts.map +1 -0
- package/dist/testing/logged_errors.js +285 -0
- package/dist/testing/middleware_gate_probes.d.ts.map +1 -1
- package/dist/testing/middleware_gate_probes.js +15 -8
- package/dist/testing/rpc_attack_surface.d.ts.map +1 -1
- package/dist/testing/rpc_attack_surface.js +41 -16
- package/dist/testing/stubs.d.ts +5 -1
- package/dist/testing/stubs.d.ts.map +1 -1
- package/dist/testing/stubs.js +6 -3
- package/dist/testing/test_app.d.ts +40 -11
- package/dist/testing/test_app.d.ts.map +1 -1
- package/dist/testing/test_app.js +66 -9
- package/dist/testing/transports/fetch_transport.d.ts +10 -0
- package/dist/testing/transports/fetch_transport.d.ts.map +1 -1
- package/dist/testing/transports/fetch_transport.js +13 -0
- package/package.json +4 -3
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import './assert_dev_env.js';
|
|
2
|
+
/**
|
|
3
|
+
* The per-test logged-error gate: every error a test logs — a
|
|
4
|
+
* `Logger.prototype.error` call or a `console.error` — fails the test once it
|
|
5
|
+
* ends, unless the test declared it. A test that provokes an error on purpose
|
|
6
|
+
* says so with `expect_logged_error(pattern)`, which also asserts the error
|
|
7
|
+
* appeared, so the noise becomes coverage; an error that is a side effect of
|
|
8
|
+
* what a test or describe sets up, and not what it checks, is
|
|
9
|
+
* `allow_logged_error(pattern, reason)`. The designed test-mode banners
|
|
10
|
+
* (`LOGGED_ERROR_BANNERS`) are allowed everywhere.
|
|
11
|
+
*
|
|
12
|
+
* Wire it as a vitest `setupFiles` entry whose top level calls
|
|
13
|
+
* `install_logged_error_gate()`:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* // src/test/logged_error_gate.ts
|
|
17
|
+
* import { install_logged_error_gate } from '@fuzdev/fuz_app/testing/logged_errors.ts';
|
|
18
|
+
*
|
|
19
|
+
* install_logged_error_gate();
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* A call counts whatever the logger's level: a `Logger` at `'off'` prints
|
|
23
|
+
* nothing and is still a logged error, so a test's silent logger hides nothing
|
|
24
|
+
* from the gate. A test that replaces `console.error` with a spy has claimed
|
|
25
|
+
* the calls it swallows; a `Logger` call still reaches the gate first.
|
|
26
|
+
*
|
|
27
|
+
* Errors only — warnings are not gated. An error logged outside any test (a
|
|
28
|
+
* `beforeAll` or `afterAll`, the file's top level) fails the file in its last
|
|
29
|
+
* `afterAll`, allowed by any `allow_logged_error` the file declared at
|
|
30
|
+
* collection. The check runs in the last of the test's `onTestFinished` hooks,
|
|
31
|
+
* after its `afterEach` hooks and after a `create_test_app` app's close, so
|
|
32
|
+
* what a shutdown logs counts. A test that already failed is left as it
|
|
33
|
+
* failed. An error a test's work logs after the test has finished — a
|
|
34
|
+
* background timer, an effect nothing awaited — lands on the next test or the
|
|
35
|
+
* file. A concurrent test runs beside its siblings, so no buffer can say whose
|
|
36
|
+
* error a log is: what it logs goes to the file's, checked in its last
|
|
37
|
+
* `afterAll` against the file's allowances, and both `expect_logged_error` and
|
|
38
|
+
* `allow_logged_error` throw in one — allow its errors on its `describe`, at
|
|
39
|
+
* collection.
|
|
40
|
+
*
|
|
41
|
+
* @module
|
|
42
|
+
*/
|
|
43
|
+
import { inspect } from 'node:util';
|
|
44
|
+
import { afterAll, beforeEach, TestRunner } from 'vitest';
|
|
45
|
+
import { Logger } from '@fuzdev/fuz_util/log.ts';
|
|
46
|
+
import { RATE_LIMITERS_DISABLED_BANNER } from '../rate_limiter.js';
|
|
47
|
+
import { LOGIN_FAIL_FLOOR_DISABLED_BANNER, SIGNUP_FAIL_FLOOR_DISABLED_BANNER } from '../auth/fail_floor.js';
|
|
48
|
+
/**
|
|
49
|
+
* The designed test-mode banners, allowed in every test: each is printed with
|
|
50
|
+
* `console.error`, once per module instance, when a test-only mode is on, so a
|
|
51
|
+
* production log that shows one is the alarm. In a test run they are the
|
|
52
|
+
* mode working.
|
|
53
|
+
*/
|
|
54
|
+
export const LOGGED_ERROR_BANNERS = [
|
|
55
|
+
{
|
|
56
|
+
pattern: RATE_LIMITERS_DISABLED_BANNER,
|
|
57
|
+
reason: "the banner of `rate_limiters: 'disabled_for_testing'`, the test default"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
pattern: LOGIN_FAIL_FLOOR_DISABLED_BANNER,
|
|
61
|
+
reason: 'the banner of a login failure timing floor of 0, the test default'
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
pattern: SIGNUP_FAIL_FLOOR_DISABLED_BANNER,
|
|
65
|
+
reason: 'the banner of a signup failure timing floor of 0, the test default'
|
|
66
|
+
}
|
|
67
|
+
];
|
|
68
|
+
const GATE_STATE_KEY = Symbol.for('@fuzdev/fuz_app/testing/logged_errors');
|
|
69
|
+
/** Marks a function as the gate's wrapper, so it is installed once per object. */
|
|
70
|
+
const WRAPPED_KEY = Symbol.for('@fuzdev/fuz_app/testing/logged_errors/wrapped');
|
|
71
|
+
/** How much of an error's text a failure message shows. */
|
|
72
|
+
const REPORTED_TEXT_MAX_LENGTH = 600;
|
|
73
|
+
const format_logged_value = (value) => {
|
|
74
|
+
if (typeof value === 'string')
|
|
75
|
+
return value;
|
|
76
|
+
if (value instanceof Error) {
|
|
77
|
+
const cause = value.cause === undefined ? '' : ` [cause: ${format_logged_value(value.cause)}]`;
|
|
78
|
+
return `${value.name}: ${value.message}${cause}`;
|
|
79
|
+
}
|
|
80
|
+
return inspect(value, { depth: 4, breakLength: Infinity });
|
|
81
|
+
};
|
|
82
|
+
const format_logged_args = (args) => args.map(format_logged_value).join(' ');
|
|
83
|
+
const pattern_matches = (pattern, text) => typeof pattern === 'string' ? text.includes(pattern) : text.search(pattern) !== -1;
|
|
84
|
+
const record_logged_error = (state, error) => {
|
|
85
|
+
if (state.file === null)
|
|
86
|
+
return;
|
|
87
|
+
(state.test?.errors ?? state.file.outside).push(error);
|
|
88
|
+
};
|
|
89
|
+
const wrap_error_method = (target, state, source, format) => {
|
|
90
|
+
const original = target.error;
|
|
91
|
+
if (original[WRAPPED_KEY])
|
|
92
|
+
return;
|
|
93
|
+
const wrapped = function (...args) {
|
|
94
|
+
if (source === 'logger') {
|
|
95
|
+
record_logged_error(state, { source, text: format(this, args) });
|
|
96
|
+
const outer = state.in_logger;
|
|
97
|
+
state.in_logger = true;
|
|
98
|
+
try {
|
|
99
|
+
return original.apply(this, args);
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
state.in_logger = outer;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (!state.in_logger)
|
|
106
|
+
record_logged_error(state, { source, text: format(this, args) });
|
|
107
|
+
return original.apply(this, args);
|
|
108
|
+
};
|
|
109
|
+
wrapped[WRAPPED_KEY] = true;
|
|
110
|
+
target.error = wrapped;
|
|
111
|
+
};
|
|
112
|
+
/** The gate state, creating it and wrapping `Logger.prototype.error` and `console.error` once. */
|
|
113
|
+
const get_gate_state = () => {
|
|
114
|
+
const globals = globalThis;
|
|
115
|
+
let state = globals[GATE_STATE_KEY];
|
|
116
|
+
if (!state) {
|
|
117
|
+
state = { file: null, test: null, in_logger: false };
|
|
118
|
+
globals[GATE_STATE_KEY] = state;
|
|
119
|
+
}
|
|
120
|
+
// each its own check: under `isolate` a file may load a fresh `Logger`
|
|
121
|
+
wrap_error_method(Logger.prototype, state, 'logger', (self, args) => {
|
|
122
|
+
const label = self.label;
|
|
123
|
+
const text = format_logged_args(args);
|
|
124
|
+
return label ? `[${label}] ${text}` : text;
|
|
125
|
+
});
|
|
126
|
+
wrap_error_method(console, state, 'console', (_self, args) => format_logged_args(args));
|
|
127
|
+
return state;
|
|
128
|
+
};
|
|
129
|
+
/** The gate state if a setup file installed it, else `null`. */
|
|
130
|
+
const read_gate_state = () => {
|
|
131
|
+
const state = globalThis[GATE_STATE_KEY];
|
|
132
|
+
return state?.file ? state : null;
|
|
133
|
+
};
|
|
134
|
+
const validate_allowance = (allowance, label) => {
|
|
135
|
+
if (typeof allowance.reason !== 'string' || allowance.reason.trim() === '') {
|
|
136
|
+
throw new Error(`${label}: an allowed logged error needs its reason (${String(allowance.pattern)})`);
|
|
137
|
+
}
|
|
138
|
+
if (allowance.pattern === '') {
|
|
139
|
+
throw new Error(`${label}: an empty pattern would allow every logged error`);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
/** Whether `suite` is `task` or one of its ancestors. */
|
|
143
|
+
const task_is_within = (task, suite) => {
|
|
144
|
+
for (let t = task; t; t = t.suite) {
|
|
145
|
+
if (t === suite)
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
};
|
|
150
|
+
const format_unexpected = (errors, where) => {
|
|
151
|
+
const lines = errors.map((e) => {
|
|
152
|
+
const text = e.text.length > REPORTED_TEXT_MAX_LENGTH
|
|
153
|
+
? `${e.text.slice(0, REPORTED_TEXT_MAX_LENGTH)}…`
|
|
154
|
+
: e.text;
|
|
155
|
+
return ` - (${e.source}) ${text}`;
|
|
156
|
+
});
|
|
157
|
+
return (`${where} logged ${errors.length === 1 ? 'an error' : 'errors'} it did not declare:\n` +
|
|
158
|
+
`${lines.join('\n')}\n` +
|
|
159
|
+
'Declare one the test provokes with `expect_logged_error(pattern)`, or one it ' +
|
|
160
|
+
'tolerates with `allow_logged_error(pattern, reason)` (`testing/logged_errors.ts`).');
|
|
161
|
+
};
|
|
162
|
+
const check_test = (file, test) => {
|
|
163
|
+
const allowed = (text) => file.global.some((a) => pattern_matches(a.pattern, text)) ||
|
|
164
|
+
test.allowances.some((a) => pattern_matches(a.pattern, text)) ||
|
|
165
|
+
file.allowances.some((a) => (a.suite === null || task_is_within(test.task, a.suite)) && pattern_matches(a.pattern, text)) ||
|
|
166
|
+
test.expected.some((x) => pattern_matches(x.pattern, text));
|
|
167
|
+
const missing = test.expected.filter((x) => !test.errors.some((e) => pattern_matches(x.pattern, e.text)));
|
|
168
|
+
const unexpected = test.errors.filter((e) => !allowed(e.text));
|
|
169
|
+
const problems = [];
|
|
170
|
+
if (missing.length > 0) {
|
|
171
|
+
problems.push(`expected a logged error matching ${missing.map((x) => String(x.pattern)).join(', ')}, ` +
|
|
172
|
+
`but ${test.errors.length === 0 ? 'none was logged' : `the test logged only:\n${test.errors.map((e) => ` - ${e.text.slice(0, REPORTED_TEXT_MAX_LENGTH)}`).join('\n')}`}`);
|
|
173
|
+
}
|
|
174
|
+
if (unexpected.length > 0)
|
|
175
|
+
problems.push(format_unexpected(unexpected, 'the test'));
|
|
176
|
+
if (problems.length > 0)
|
|
177
|
+
throw new Error(problems.join('\n\n'));
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Install the gate for the current test file — call at the top level of a
|
|
181
|
+
* vitest `setupFiles` module, which vitest runs ahead of each test file. Wraps
|
|
182
|
+
* `Logger.prototype.error` and `console.error` once per process (each wrapper
|
|
183
|
+
* calls the original), resets the file's state, and registers the file's
|
|
184
|
+
* hooks: a `beforeEach` that starts the test's buffer and checks it in an
|
|
185
|
+
* `onTestFinished` hook, and an `afterAll` that checks what was logged outside
|
|
186
|
+
* any test.
|
|
187
|
+
*
|
|
188
|
+
* @param options - errors allowed in every test beside `LOGGED_ERROR_BANNERS`
|
|
189
|
+
* @throws Error on an allowance without its reason
|
|
190
|
+
*/
|
|
191
|
+
export const install_logged_error_gate = (options = {}) => {
|
|
192
|
+
const global = [...LOGGED_ERROR_BANNERS, ...(options.allow ?? [])];
|
|
193
|
+
for (const allowance of global)
|
|
194
|
+
validate_allowance(allowance, 'install_logged_error_gate');
|
|
195
|
+
const state = get_gate_state();
|
|
196
|
+
const file = { global, allowances: [], outside: [] };
|
|
197
|
+
state.file = file;
|
|
198
|
+
state.test = null;
|
|
199
|
+
beforeEach((ctx) => {
|
|
200
|
+
// a concurrent test runs beside its siblings, so no buffer can tell whose
|
|
201
|
+
// error a log is: what it logs goes to the file's, checked in `afterAll`
|
|
202
|
+
if (ctx.task.concurrent) {
|
|
203
|
+
state.test = null;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const test = { task: ctx.task, errors: [], allowances: [], expected: [] };
|
|
207
|
+
state.test = test;
|
|
208
|
+
// the first `onTestFinished` hook registered runs last: after the test's
|
|
209
|
+
// own, so what a `create_test_app` close logs is the test's
|
|
210
|
+
ctx.onTestFinished(({ task }) => {
|
|
211
|
+
if (state.test === test)
|
|
212
|
+
state.test = null;
|
|
213
|
+
if (task.result?.state === 'fail')
|
|
214
|
+
return;
|
|
215
|
+
check_test(file, test);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
afterAll(() => {
|
|
219
|
+
state.test = null;
|
|
220
|
+
const unexpected = file.outside.filter((e) => !file.global.some((a) => pattern_matches(a.pattern, e.text)) &&
|
|
221
|
+
!file.allowances.some((a) => pattern_matches(a.pattern, e.text)));
|
|
222
|
+
file.outside.length = 0;
|
|
223
|
+
if (unexpected.length > 0) {
|
|
224
|
+
throw new Error(format_unexpected(unexpected, 'the file, outside any test or in a concurrent test,'));
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Declare that the running test logs an error matching `pattern`, and fail
|
|
230
|
+
* the test when it ends unless one was logged: the error the test provokes is
|
|
231
|
+
* asserted, not just tolerated. Every error the pattern matches is allowed.
|
|
232
|
+
*
|
|
233
|
+
* @param pattern - a substring of the error's text, or a `RegExp` searched for
|
|
234
|
+
* @throws Error outside a test, in a concurrent test, or when no setup file installed the gate
|
|
235
|
+
*/
|
|
236
|
+
export const expect_logged_error = (pattern) => {
|
|
237
|
+
const state = read_gate_state();
|
|
238
|
+
if (!state) {
|
|
239
|
+
throw new Error('expect_logged_error needs the logged-error gate — install it in a setup file (`install_logged_error_gate`)');
|
|
240
|
+
}
|
|
241
|
+
const current = TestRunner.getCurrentTest();
|
|
242
|
+
if (current?.concurrent) {
|
|
243
|
+
throw new Error("expect_logged_error: a concurrent test's errors can't be attributed to it — allow them with `allow_logged_error` on its `describe`");
|
|
244
|
+
}
|
|
245
|
+
if (!state.test || !current) {
|
|
246
|
+
throw new Error('expect_logged_error must be called inside a test');
|
|
247
|
+
}
|
|
248
|
+
if (pattern === '')
|
|
249
|
+
throw new Error('expect_logged_error: an empty pattern matches every error');
|
|
250
|
+
state.test.expected.push({ pattern });
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Allow logged errors matching `pattern`, with the reason they are expected:
|
|
254
|
+
* inside a test, for that test; at collection, for every test of the
|
|
255
|
+
* enclosing `describe` (the whole file at its top level) and for what the
|
|
256
|
+
* file logs outside its tests or in its concurrent tests. It throws in a
|
|
257
|
+
* concurrent test, whose errors share the file's bucket with its siblings',
|
|
258
|
+
* so an allowance made there would hide theirs. Unlike `expect_logged_error`
|
|
259
|
+
* it asserts nothing. A no-op when no setup file installed the gate.
|
|
260
|
+
*
|
|
261
|
+
* @param pattern - a substring of the error's text, or a `RegExp` searched for
|
|
262
|
+
* @param reason - why the error is expected; required
|
|
263
|
+
* @throws Error on an empty reason or pattern, or in a concurrent test
|
|
264
|
+
*/
|
|
265
|
+
export const allow_logged_error = (pattern, reason) => {
|
|
266
|
+
validate_allowance({ pattern, reason }, 'allow_logged_error');
|
|
267
|
+
const state = read_gate_state();
|
|
268
|
+
if (!state)
|
|
269
|
+
return;
|
|
270
|
+
const current = TestRunner.getCurrentTest();
|
|
271
|
+
if (current?.concurrent) {
|
|
272
|
+
// an allowance it added would reach the file's bucket, every sibling's
|
|
273
|
+
// errors in it, so its errors are allowed where the scope is visible
|
|
274
|
+
throw new Error("allow_logged_error: a concurrent test's errors can't be attributed to it — allow them on its `describe`, at collection");
|
|
275
|
+
}
|
|
276
|
+
if (state.test && current) {
|
|
277
|
+
state.test.allowances.push({ pattern, reason });
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
state.file.allowances.push({
|
|
281
|
+
pattern,
|
|
282
|
+
reason,
|
|
283
|
+
suite: TestRunner.getCurrentSuite().suite ?? null
|
|
284
|
+
});
|
|
285
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware_gate_probes.d.ts","sourceRoot":"","sources":["../../src/lib/testing/middleware_gate_probes.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAqC7B,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAuB,KAAK,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAElF,OAAO,E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a,KAAK,CAAC,SAAS,CAAC,KAC3B,OAAO,CAAC,IAAI,CAmBd,CAAC;AAEF,0DAA0D;AAC1D,MAAM,WAAW,yBAAyB;IACzC;;;OAGG;IACH,UAAU,EAAE,SAAS,CAAC;IACtB,iFAAiF;IACjF,cAAc,EAAE,cAAc,CAAC;IAC/B,mFAAmF;IACnF,mBAAmB,EAAE,MAAM,CAAC;IAC5B,mEAAmE;IACnE,aAAa,EAAE,uBAAuB,CAAC;IACvC;;;;OAIG;IACH,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,8BAA8B,GAAI,SAAS,yBAAyB,KAAG,IAwDnF,CAAC"}
|
|
@@ -31,13 +31,14 @@ import './assert_dev_env.js';
|
|
|
31
31
|
*
|
|
32
32
|
* @module
|
|
33
33
|
*/
|
|
34
|
-
import { assert, describe, test } from 'vitest';
|
|
34
|
+
import { assert, beforeAll, describe, test } from 'vitest';
|
|
35
35
|
import { get_route_error_map } from '../http/route_errors.js';
|
|
36
36
|
import { split_route_template } from '../http/route_error_check.js';
|
|
37
37
|
import { REST_FORBIDDEN_HOST, REST_FORBIDDEN_ORIGIN, REST_INVALID_CONTENT_LENGTH, REST_PAYLOAD_TOO_LARGE } from '../http/error_schemas.js';
|
|
38
38
|
import { ErrorCoverageCollector } from './error_coverage.js';
|
|
39
39
|
import { is_standard_integration_route, MIDDLEWARE_GATE_ERROR_CODES } from './integration_helpers.js';
|
|
40
40
|
import { require_rpc_endpoint_path, resolve_rpc_endpoints_for_setup } from './rpc_helpers.js';
|
|
41
|
+
import { check_db_invariants_after_all } from './db_invariants.js';
|
|
41
42
|
import { to_route_content_type } from './schema_generators.js';
|
|
42
43
|
import { raw_http_send_head } from './transports/raw_http.js';
|
|
43
44
|
import { SPINE_BODY_LIMIT_DEFAULT_BYTES } from './cross_backend/spine_surface_constants.js';
|
|
@@ -228,16 +229,22 @@ export const describe_middleware_gate_tests = (options) => {
|
|
|
228
229
|
}
|
|
229
230
|
describe('middleware gates', () => {
|
|
230
231
|
const collector = new ErrorCoverageCollector(options.surface_source.route_error_maps);
|
|
231
|
-
// in-process the probes share one fixture
|
|
232
|
-
//
|
|
232
|
+
// in-process the probes share one fixture, built ahead of them so it
|
|
233
|
+
// outlives each test (an app built in a test is closed when it
|
|
234
|
+
// finishes) and checked once they are done; over a socket they need
|
|
235
|
+
// none, as they read and write no state
|
|
236
|
+
let fixture = null;
|
|
233
237
|
let in_process_transport = null;
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
238
|
+
if (options.base_url === null) {
|
|
239
|
+
beforeAll(async () => {
|
|
240
|
+
fixture = await options.setup_test();
|
|
241
|
+
in_process_transport = fixture.fresh_transport({ origin: null });
|
|
242
|
+
});
|
|
243
|
+
check_db_invariants_after_all(() => fixture);
|
|
244
|
+
}
|
|
237
245
|
for (const probe of probes) {
|
|
238
246
|
test(format_middleware_gate_probe(probe), async () => {
|
|
239
|
-
const
|
|
240
|
-
const res = await send_middleware_gate_probe(probe, options.base_url, transport);
|
|
247
|
+
const res = await send_middleware_gate_probe(probe, options.base_url, in_process_transport);
|
|
241
248
|
await assert_middleware_gate_refusal(probe, res, collector, route_specs);
|
|
242
249
|
});
|
|
243
250
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc_attack_surface.d.ts","sourceRoot":"","sources":["../../src/lib/testing/rpc_attack_surface.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"rpc_attack_surface.d.ts","sourceRoot":"","sources":["../../src/lib/testing/rpc_attack_surface.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAoB7B,OAAO,KAAK,EAGX,cAAc,EACd,MAAM,oBAAoB,CAAC;AAmC5B,uDAAuD;AACvD,MAAM,WAAW,uBAAuB;IACvC,+FAA+F;IAC/F,KAAK,EAAE,MAAM,cAAc,CAAC;IAC5B,yDAAyD;IACzD,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACrB;;;;;;;;;;OAUG;IACH,eAAe,CAAC,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAC/D;AA+bD;;;;;;;;;GASG;AACH,eAAO,MAAM,iCAAiC,GAAI,SAAS,uBAAuB,KAAG,IAOpF,CAAC"}
|
|
@@ -14,6 +14,7 @@ import './assert_dev_env.js';
|
|
|
14
14
|
*/
|
|
15
15
|
import { test, assert, describe } from 'vitest';
|
|
16
16
|
import { Logger } from '@fuzdev/fuz_util/log.ts';
|
|
17
|
+
import { to_error_message } from '@fuzdev/fuz_util/error.ts';
|
|
17
18
|
import { is_keeper_auth, is_public_auth, is_role_auth } from '../http/auth_shape.js';
|
|
18
19
|
import { JSONRPC_ERROR_CODES } from '../http/jsonrpc_errors.js';
|
|
19
20
|
import { create_auth_test_apps, create_test_app_from_specs, create_test_request_context, select_auth_app } from './auth_apps.js';
|
|
@@ -41,16 +42,35 @@ const find_rpc_action = (rpc_endpoint_specs, endpoint_path, method_name) => {
|
|
|
41
42
|
};
|
|
42
43
|
/** The key a recording stub notes its call under: the endpoint path and method. */
|
|
43
44
|
const to_reached_key = (endpoint_path, method_name) => `${endpoint_path} ${method_name}`;
|
|
45
|
+
/**
|
|
46
|
+
* A stub handler's answer: a value generated to pass the action's output
|
|
47
|
+
* schema (`generate_valid_body`; `null` for a null output), so DEV output
|
|
48
|
+
* validation has nothing to report. An output the generator can't reach
|
|
49
|
+
* makes the handler throw, naming the action — the method's "correct auth
|
|
50
|
+
* passes" test fails on the 500, and a logged-error gate on the log line.
|
|
51
|
+
*/
|
|
52
|
+
const create_stub_output = (action) => {
|
|
53
|
+
try {
|
|
54
|
+
const output = generate_valid_body(action.spec.output) ?? null;
|
|
55
|
+
return () => output;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
const message = `stub handler for '${action.spec.method}' has no generated output: ${to_error_message(error)}`;
|
|
59
|
+
return () => {
|
|
60
|
+
throw new Error(message);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
};
|
|
44
64
|
/**
|
|
45
65
|
* The route specs with every RPC endpoint's routes rebuilt over its actions,
|
|
46
66
|
* each handler swapped for a stub that records its call in `reached` and
|
|
47
|
-
* answers
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
67
|
+
* answers a generated schema-valid output (`create_stub_output`), so a test
|
|
68
|
+
* that the handler is reached never depends on what the handler would do
|
|
69
|
+
* over stub deps (a live re-read's 401). Each endpoint's routes take the
|
|
70
|
+
* place of its first route in the list, matched on method and path, so
|
|
71
|
+
* another route at the endpoint's path stays; the rest of the list is kept
|
|
72
|
+
* in order. The endpoints mount with no rate limiters, a silent logger, and
|
|
73
|
+
* the no-op notification sender.
|
|
54
74
|
*
|
|
55
75
|
* @param route_specs - the app's route specs, the endpoints' routes among them
|
|
56
76
|
* @param rpc_endpoint_specs - the endpoints whose handlers are stubbed
|
|
@@ -64,13 +84,16 @@ const stub_rpc_endpoint_handlers = (route_specs, rpc_endpoint_specs, reached) =>
|
|
|
64
84
|
for (const endpoint of rpc_endpoint_specs) {
|
|
65
85
|
const rebuilt = create_rpc_endpoint({
|
|
66
86
|
path: endpoint.path,
|
|
67
|
-
actions: endpoint.actions.map((action) =>
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
87
|
+
actions: endpoint.actions.map((action) => {
|
|
88
|
+
const answer = create_stub_output(action);
|
|
89
|
+
return {
|
|
90
|
+
spec: action.spec,
|
|
91
|
+
handler: () => {
|
|
92
|
+
reached.add(to_reached_key(endpoint.path, action.spec.method));
|
|
93
|
+
return answer();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}),
|
|
74
97
|
log,
|
|
75
98
|
action_ip_rate_limiter: null,
|
|
76
99
|
action_account_rate_limiter: null,
|
|
@@ -204,8 +227,10 @@ const describe_rpc_auth = (options) => {
|
|
|
204
227
|
reached.delete(key);
|
|
205
228
|
const app = select_auth_app(stubbed_apps, method.auth);
|
|
206
229
|
const res = await app.request(endpoint.path, create_rpc_post_init(method.name, params));
|
|
207
|
-
|
|
208
|
-
assert.
|
|
230
|
+
const text = await res.text();
|
|
231
|
+
assert.ok(reached.has(key), `${method.name} should reach its handler, got ${res.status}: ${text}`);
|
|
232
|
+
// a stub with no generated output throws, naming why, in the 500's body
|
|
233
|
+
assert.strictEqual(res.status, 200, `${method.name} should answer 200, got: ${text}`);
|
|
209
234
|
});
|
|
210
235
|
}
|
|
211
236
|
});
|
package/dist/testing/stubs.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export declare const create_throwing_stub: <T = any>(label: string) => T;
|
|
|
38
38
|
* this auto-updates when interfaces change.
|
|
39
39
|
*
|
|
40
40
|
* @param label - descriptive name for debug purposes
|
|
41
|
-
* @param overrides - explicit properties to set (e.g. `{
|
|
41
|
+
* @param overrides - explicit properties to set (e.g. `{sign: ...}` on a keyring)
|
|
42
42
|
*/
|
|
43
43
|
export declare const create_noop_stub: <T = any>(_label: string, overrides?: Record<string, unknown>) => T;
|
|
44
44
|
/** Throwing stub — use for deps that should never be reached. */
|
|
@@ -95,6 +95,10 @@ export declare const create_test_audit_emitter: () => AuditEmitter;
|
|
|
95
95
|
export declare const create_stub_audit_sse: () => AuditLogSse;
|
|
96
96
|
/**
|
|
97
97
|
* Create no-op `AppDeps` for auth surface testing.
|
|
98
|
+
*
|
|
99
|
+
* The `db` is a fresh `create_stub_db()`: a transaction runs its callback,
|
|
100
|
+
* and the pool-level `Db` reads as outside one, so a handler on stub deps
|
|
101
|
+
* runs the path it runs in production.
|
|
98
102
|
*/
|
|
99
103
|
export declare const create_stub_app_deps: () => AppDeps;
|
|
100
104
|
/** Options for `create_stub_app_backend`. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stubs.d.ts","sourceRoot":"","sources":["../../src/lib/testing/stubs.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAa7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAE9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAIrF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAG7D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACtE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,EAAE,EAAyB,MAAM,aAAa,CAAC;AACxD,OAAO,EAGN,KAAK,UAAU,EACf,KAAK,SAAS,EACd,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAa,KAAK,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAInE,OAAO,EAEN,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAwB,KAAK,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAC3F,OAAO,KAAK,EAAE,SAAS,EAAmB,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAA+B,KAAK,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAe7F;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,oBAAoB,GAAI,CAAC,GAAG,GAAG,EAAE,OAAO,MAAM,KAAG,CAqBtD,CAAC;AAET;;;;;;;;;GASG;AACH,eAAO,MAAM,gBAAgB,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,iBAAiB,CAAC,EAAE,KAAK,CAAC,kBAAkB,CAAC,CAAC;IAC9C;;;;;;;;OAQG;IACH,SAAS,CAAC,EAAE,sBAAsB,CAAC;IACnC;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACpB;AAmBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,4BAA4B,GACxC,SAAS,+BAA+B,KACtC,cAmHF,CAAC"}
|
package/dist/testing/stubs.js
CHANGED
|
@@ -69,7 +69,7 @@ export const create_throwing_stub = (label) => new Proxy({}, {
|
|
|
69
69
|
* this auto-updates when interfaces change.
|
|
70
70
|
*
|
|
71
71
|
* @param label - descriptive name for debug purposes
|
|
72
|
-
* @param overrides - explicit properties to set (e.g. `{
|
|
72
|
+
* @param overrides - explicit properties to set (e.g. `{sign: ...}` on a keyring)
|
|
73
73
|
*/
|
|
74
74
|
export const create_noop_stub = (_label, overrides) => new Proxy({ ...(overrides ?? {}) }, {
|
|
75
75
|
get: (target, prop) => {
|
|
@@ -117,7 +117,6 @@ attach_route_handler(shape, () => {
|
|
|
117
117
|
});
|
|
118
118
|
/** Stub middleware that passes through. */
|
|
119
119
|
export const stub_mw = async (_c, next) => next();
|
|
120
|
-
const stub_db = create_noop_stub('stub_db');
|
|
121
120
|
/**
|
|
122
121
|
* Build a no-op `AuditEmitter` for tests that don't assert on audit fan-out.
|
|
123
122
|
*
|
|
@@ -166,6 +165,10 @@ export const create_stub_audit_sse = () => {
|
|
|
166
165
|
};
|
|
167
166
|
/**
|
|
168
167
|
* Create no-op `AppDeps` for auth surface testing.
|
|
168
|
+
*
|
|
169
|
+
* The `db` is a fresh `create_stub_db()`: a transaction runs its callback,
|
|
170
|
+
* and the pool-level `Db` reads as outside one, so a handler on stub deps
|
|
171
|
+
* runs the path it runs in production.
|
|
169
172
|
*/
|
|
170
173
|
export const create_stub_app_deps = () => ({
|
|
171
174
|
// `sign` answers a string, as a keyring does, so a route that signs a
|
|
@@ -174,7 +177,7 @@ export const create_stub_app_deps = () => ({
|
|
|
174
177
|
sign: async (value) => `${value}.stub_signature`
|
|
175
178
|
}),
|
|
176
179
|
password: create_noop_stub('password'),
|
|
177
|
-
db:
|
|
180
|
+
db: create_stub_db(),
|
|
178
181
|
log: new Logger('test', { level: 'off' }),
|
|
179
182
|
audit: create_test_audit_emitter(),
|
|
180
183
|
connection_closer: create_realtime_closer(),
|
|
@@ -5,15 +5,15 @@ import './assert_dev_env.js';
|
|
|
5
5
|
* `create_test_app` combines `create_test_app_server` (`testing/app_server.ts`,
|
|
6
6
|
* the vitest-free backend primitives) with `create_app_server`, and hands back
|
|
7
7
|
* the app, the backend, the surface, and request header builders. Built inside
|
|
8
|
-
* a test, the app's database is held to the
|
|
9
|
-
* test finishes
|
|
8
|
+
* a test (its body or a `beforeEach`), the app's database is held to the
|
|
9
|
+
* auth-schema invariants when the test finishes, and the app is closed then,
|
|
10
|
+
* failing the test on a close that gave up on work still in flight.
|
|
10
11
|
*
|
|
11
12
|
* @module
|
|
12
13
|
*/
|
|
13
14
|
import type { Hono } from 'hono';
|
|
14
15
|
import type { Uuid } from '../primitive_schemas.js';
|
|
15
16
|
import { type AppServer, type AppServerOptions, type BootstrapServerOptions } from '../server/app_server.js';
|
|
16
|
-
import type { ShutdownDrainGivenUp } from '../server/shutdown_drain.js';
|
|
17
17
|
import type { AppServerContext } from '../server/app_server_context.js';
|
|
18
18
|
import type { AppSurface, AppSurfaceSpec } from '../http/surface.js';
|
|
19
19
|
import type { RouteSpec } from '../http/route_spec.js';
|
|
@@ -106,7 +106,9 @@ export interface CreateTestAppAccountArgs {
|
|
|
106
106
|
email?: string;
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
109
|
-
* A fully assembled test app — Hono app + backend + helpers.
|
|
109
|
+
* A fully assembled test app — Hono app + backend + helpers. Never built inside
|
|
110
|
+
* a concurrent test (`create_test_app` throws): build a concurrent suite's app
|
|
111
|
+
* in `beforeAll` and close it in `afterAll`.
|
|
110
112
|
*/
|
|
111
113
|
export interface TestApp {
|
|
112
114
|
app: Hono;
|
|
@@ -115,7 +117,10 @@ export interface TestApp {
|
|
|
115
117
|
* The assembled `AppServer` — reach for what `create_app_server` built
|
|
116
118
|
* beyond the fields lifted onto `TestApp`: `audit_sse` (with
|
|
117
119
|
* `audit_log_sse` in `app_options`), the `ws_endpoints` transports,
|
|
118
|
-
* `bootstrap_status`, `migration_results`.
|
|
120
|
+
* `bootstrap_status`, `migration_results`. Its `close` resolves the
|
|
121
|
+
* `ShutdownDrainGivenUp` report, for a test that gives up on purpose, and
|
|
122
|
+
* marks the app closed by the test, so the close when the test finishes
|
|
123
|
+
* leaves the outcome to it.
|
|
119
124
|
*/
|
|
120
125
|
server: AppServer;
|
|
121
126
|
surface_spec: AppSurfaceSpec;
|
|
@@ -138,9 +143,27 @@ export interface TestApp {
|
|
|
138
143
|
* @throws Error listing every broken invariant and its rows
|
|
139
144
|
*/
|
|
140
145
|
assert_db_invariants: () => Promise<void>;
|
|
141
|
-
/**
|
|
142
|
-
|
|
146
|
+
/**
|
|
147
|
+
* Cleanup resources — `AppServer.close`: stops an opted-in auth cleanup
|
|
148
|
+
* schedule, closes live connections, drains the work in flight within its
|
|
149
|
+
* bound (`TEST_SHUTDOWN_DRAIN_TIMEOUT_MS` unless `app_options` sets one),
|
|
150
|
+
* disposes the limiters the server built and removes the audit listeners
|
|
151
|
+
* and closer members it added, then the backend's `close`. Idempotent. A
|
|
152
|
+
* test that gives up on purpose calls `server.close()`, which resolves the
|
|
153
|
+
* report instead.
|
|
154
|
+
*
|
|
155
|
+
* @throws Error naming the `ShutdownDrainGivenUp` report when the drain
|
|
156
|
+
* gave up on work still in flight
|
|
157
|
+
*/
|
|
158
|
+
cleanup: () => Promise<void>;
|
|
143
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* The bound on a test app's shutdown drain (`shutdown_drain_timeout_ms`), in
|
|
162
|
+
* ms: under vitest's per-test timeout, so a test whose app gives up fails on
|
|
163
|
+
* the give-up rather than timing out, and short, since a test's work in
|
|
164
|
+
* flight is milliseconds. Override through `app_options`.
|
|
165
|
+
*/
|
|
166
|
+
export declare const TEST_SHUTDOWN_DRAIN_TIMEOUT_MS = 1000;
|
|
144
167
|
/**
|
|
145
168
|
* Create a fully assembled test app with a Hono server, middleware, and routes.
|
|
146
169
|
*
|
|
@@ -153,15 +176,21 @@ export interface TestApp {
|
|
|
153
176
|
* server's deps (db, keyring), so reuse across servers is unsafe.
|
|
154
177
|
* The expensive resource (the WASM database) is cached separately in `testing/db.ts`.
|
|
155
178
|
*
|
|
156
|
-
* Built inside a running test, it registers
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
179
|
+
* Built inside a running test (its body or a `beforeEach`), it registers one
|
|
180
|
+
* hook for when the test finishes: the database invariant check (skipped when
|
|
181
|
+
* the test already failed, as `register_db_invariants_check` does), then the
|
|
182
|
+
* close. The close throws when it gave up on work still in flight, unless the
|
|
183
|
+
* test failed or closed the app itself (`cleanup`, or `server.close()`, whose
|
|
184
|
+
* outcome is the test's). So an app built in a test lives only for that test.
|
|
185
|
+
* Built in `beforeAll`, nothing is registered: the suite closes the app, and
|
|
186
|
+
* calls `check_db_invariants_after_all(() => test_app)`, or, when the describe
|
|
160
187
|
* closes the app in its own `afterAll`, `check_db_invariants_after_suite(suite,
|
|
161
188
|
* test_app)` inside that hook ahead of the close.
|
|
162
189
|
*
|
|
163
190
|
* @param options - test app configuration
|
|
164
191
|
* @returns a `TestApp` ready for HTTP testing
|
|
192
|
+
* @throws Error when called inside a concurrent test, whose finish vitest
|
|
193
|
+
* can't tell from a sibling's — build the app in `beforeAll` instead
|
|
165
194
|
* @throws Error on an `exclude_db_invariants` entry naming no invariant or
|
|
166
195
|
* carrying no reason
|
|
167
196
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test_app.d.ts","sourceRoot":"","sources":["../../src/lib/testing/test_app.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAE7B
|
|
1
|
+
{"version":3,"file":"test_app.d.ts","sourceRoot":"","sources":["../../src/lib/testing/test_app.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAE7B;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAEjC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAEpD,OAAO,EAEN,KAAK,SAAS,EACd,KAAK,gBAAgB,EACrB,KAAK,sBAAsB,EAC3B,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAMvD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAEhE,OAAO,EAKN,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAEvE;;GAEG;AACH,MAAM,WAAW,oBAAqB,SAAQ,oBAAoB;IACjE,qEAAqE;IACrE,mBAAmB,EAAE,MAAM,CAAC;IAC5B,yEAAyE;IACzE,kBAAkB,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,KAAK,CAAC,SAAS,CAAC,CAAC;IACpE;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,uBAAuB,CAAC;IACxC;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,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