@fuzdev/fuz_app 0.163.0 → 0.165.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.
Files changed (199) hide show
  1. package/README.md +2 -1
  2. package/dist/actions/action_event_data.d.ts +1 -1
  3. package/dist/actions/action_rpc.d.ts.map +1 -1
  4. package/dist/actions/action_rpc.js +4 -0
  5. package/dist/actions/action_spec.d.ts +3 -3
  6. package/dist/actions/action_spec.d.ts.map +1 -1
  7. package/dist/auth/CLAUDE.md +1 -1
  8. package/dist/auth/account_route_schema.d.ts +3 -27
  9. package/dist/auth/account_route_schema.d.ts.map +1 -1
  10. package/dist/auth/account_route_schema.js +6 -20
  11. package/dist/auth/admin_action_specs.d.ts +56 -56
  12. package/dist/auth/audit_log_route_schema.d.ts +1 -1
  13. package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
  14. package/dist/auth/audit_log_route_schema.js +4 -5
  15. package/dist/auth/audit_log_schema.d.ts +18 -18
  16. package/dist/auth/bootstrap_route_schema.d.ts +1 -30
  17. package/dist/auth/bootstrap_route_schema.d.ts.map +1 -1
  18. package/dist/auth/bootstrap_route_schema.js +3 -8
  19. package/dist/auth/daemon_token.d.ts +4 -4
  20. package/dist/auth/daemon_token.d.ts.map +1 -1
  21. package/dist/auth/daemon_token.js +3 -3
  22. package/dist/auth/daemon_token_middleware.d.ts +1 -1
  23. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  24. package/dist/auth/daemon_token_middleware.js +5 -5
  25. package/dist/auth/middleware.js +4 -4
  26. package/dist/auth/role_grant_offer_action_specs.d.ts +2 -2
  27. package/dist/auth/signup_route_schema.d.ts.map +1 -1
  28. package/dist/auth/signup_route_schema.js +3 -7
  29. package/dist/db/CLAUDE.md +2 -1
  30. package/dist/db/file_fact_url.d.ts +11 -6
  31. package/dist/db/file_fact_url.d.ts.map +1 -1
  32. package/dist/db/file_fact_url.js +5 -5
  33. package/dist/db/schema_ready.d.ts +0 -5
  34. package/dist/db/schema_ready.d.ts.map +1 -1
  35. package/dist/db/schema_ready.js +0 -5
  36. package/dist/http/CLAUDE.md +105 -71
  37. package/dist/http/auth_shape.d.ts +48 -29
  38. package/dist/http/auth_shape.d.ts.map +1 -1
  39. package/dist/http/auth_shape.js +12 -85
  40. package/dist/http/body_limit.d.ts +12 -0
  41. package/dist/http/body_limit.d.ts.map +1 -1
  42. package/dist/http/body_limit.js +15 -0
  43. package/dist/http/body_limit_schema.d.ts +20 -0
  44. package/dist/http/body_limit_schema.d.ts.map +1 -0
  45. package/dist/http/body_limit_schema.js +20 -0
  46. package/dist/http/common_routes.d.ts +1 -1
  47. package/dist/http/common_routes.d.ts.map +1 -1
  48. package/dist/http/common_routes.js +11 -14
  49. package/dist/http/db_routes.d.ts.map +1 -1
  50. package/dist/http/db_routes.js +59 -69
  51. package/dist/http/dev_route_error_check.d.ts +70 -0
  52. package/dist/http/dev_route_error_check.d.ts.map +1 -0
  53. package/dist/http/dev_route_error_check.js +119 -0
  54. package/dist/http/error_schemas.d.ts +365 -189
  55. package/dist/http/error_schemas.d.ts.map +1 -1
  56. package/dist/http/error_schemas.js +256 -149
  57. package/dist/http/health_schema.d.ts +10 -6
  58. package/dist/http/health_schema.d.ts.map +1 -1
  59. package/dist/http/health_schema.js +3 -3
  60. package/dist/http/middleware_spec.d.ts +8 -3
  61. package/dist/http/middleware_spec.d.ts.map +1 -1
  62. package/dist/http/origin.d.ts +12 -43
  63. package/dist/http/origin.d.ts.map +1 -1
  64. package/dist/http/origin.js +10 -6
  65. package/dist/http/route_error_check.d.ts +179 -0
  66. package/dist/http/route_error_check.d.ts.map +1 -0
  67. package/dist/http/route_error_check.js +283 -0
  68. package/dist/http/route_errors.d.ts +80 -0
  69. package/dist/http/route_errors.d.ts.map +1 -0
  70. package/dist/http/route_errors.js +83 -0
  71. package/dist/http/route_spec.d.ts +33 -17
  72. package/dist/http/route_spec.d.ts.map +1 -1
  73. package/dist/http/route_spec.js +48 -62
  74. package/dist/http/schema_helpers.d.ts +15 -34
  75. package/dist/http/schema_helpers.d.ts.map +1 -1
  76. package/dist/http/schema_helpers.js +11 -47
  77. package/dist/http/surface.d.ts +19 -12
  78. package/dist/http/surface.d.ts.map +1 -1
  79. package/dist/http/surface.js +25 -38
  80. package/dist/http/surface_query.d.ts +11 -2
  81. package/dist/http/surface_query.d.ts.map +1 -1
  82. package/dist/http/surface_query.js +7 -1
  83. package/dist/primitive_schemas_fuz.d.ts +17 -3
  84. package/dist/primitive_schemas_fuz.d.ts.map +1 -1
  85. package/dist/primitive_schemas_fuz.js +13 -3
  86. package/dist/rate_limiter.d.ts +13 -0
  87. package/dist/rate_limiter.d.ts.map +1 -1
  88. package/dist/rate_limiter.js +20 -2
  89. package/dist/schema_issues.d.ts +29 -11
  90. package/dist/schema_issues.d.ts.map +1 -1
  91. package/dist/schema_issues.js +11 -10
  92. package/dist/schema_meta.d.ts +8 -1
  93. package/dist/schema_meta.d.ts.map +1 -1
  94. package/dist/schema_meta.js +2 -1
  95. package/dist/server/app_server.d.ts +3 -1
  96. package/dist/server/app_server.d.ts.map +1 -1
  97. package/dist/server/app_server.js +24 -10
  98. package/dist/server/serve_fact_route.d.ts.map +1 -1
  99. package/dist/server/serve_fact_route.js +8 -31
  100. package/dist/server/surface_route.d.ts.map +1 -1
  101. package/dist/server/surface_route.js +5 -6
  102. package/dist/server/x_accel.d.ts +12 -7
  103. package/dist/server/x_accel.d.ts.map +1 -1
  104. package/dist/server/x_accel.js +7 -9
  105. package/dist/testing/CLAUDE.md +105 -61
  106. package/dist/testing/admin_integration.js +2 -2
  107. package/dist/testing/adversarial_404.d.ts +4 -18
  108. package/dist/testing/adversarial_404.d.ts.map +1 -1
  109. package/dist/testing/adversarial_404.js +20 -52
  110. package/dist/testing/adversarial_headers.d.ts +3 -4
  111. package/dist/testing/adversarial_headers.d.ts.map +1 -1
  112. package/dist/testing/adversarial_headers.js +8 -6
  113. package/dist/testing/adversarial_input.d.ts.map +1 -1
  114. package/dist/testing/adversarial_input.js +20 -19
  115. package/dist/testing/assertions.d.ts +46 -14
  116. package/dist/testing/assertions.d.ts.map +1 -1
  117. package/dist/testing/assertions.js +73 -16
  118. package/dist/testing/attack_surface.d.ts +17 -35
  119. package/dist/testing/attack_surface.d.ts.map +1 -1
  120. package/dist/testing/attack_surface.js +36 -95
  121. package/dist/testing/auth_apps.d.ts.map +1 -1
  122. package/dist/testing/auth_apps.js +2 -1
  123. package/dist/testing/cross_backend/action_manifest.d.ts +126 -56
  124. package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
  125. package/dist/testing/cross_backend/action_manifest.js +11 -11
  126. package/dist/testing/cross_backend/action_manifest_parity.d.ts +1 -1
  127. package/dist/testing/cross_backend/action_manifest_parity.d.ts.map +1 -1
  128. package/dist/testing/cross_backend/cell_crud.d.ts.map +1 -1
  129. package/dist/testing/cross_backend/cell_crud.js +2 -1
  130. package/dist/testing/cross_backend/conformance_table.js +1 -1
  131. package/dist/testing/cross_backend/fact_serving.js +3 -3
  132. package/dist/testing/cross_backend/in_process_setup.d.ts +7 -3
  133. package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
  134. package/dist/testing/cross_backend/in_process_setup.js +11 -4
  135. package/dist/testing/cross_backend/origin_conformance.d.ts +278 -102
  136. package/dist/testing/cross_backend/origin_conformance.d.ts.map +1 -1
  137. package/dist/testing/cross_backend/origin_conformance.js +21 -21
  138. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
  139. package/dist/testing/cross_backend/rust_spine_consumer_surface.js +7 -1
  140. package/dist/testing/cross_backend/setup.d.ts +13 -0
  141. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  142. package/dist/testing/cross_backend/setup.js +24 -7
  143. package/dist/testing/cross_backend/spine_suites.d.ts +3 -2
  144. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  145. package/dist/testing/cross_backend/spine_suites.js +5 -2
  146. package/dist/testing/cross_backend/standard.d.ts +31 -0
  147. package/dist/testing/cross_backend/standard.d.ts.map +1 -1
  148. package/dist/testing/cross_backend/standard.js +9 -3
  149. package/dist/testing/cross_backend/testing_origin_check.d.ts +147 -71
  150. package/dist/testing/cross_backend/testing_origin_check.d.ts.map +1 -1
  151. package/dist/testing/cross_backend/testing_origin_check.js +16 -13
  152. package/dist/testing/cross_backend/testing_reset_actions.d.ts +70 -27
  153. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  154. package/dist/testing/data_exposure.d.ts +7 -0
  155. package/dist/testing/data_exposure.d.ts.map +1 -1
  156. package/dist/testing/data_exposure.js +8 -7
  157. package/dist/testing/error_coverage.d.ts +53 -61
  158. package/dist/testing/error_coverage.d.ts.map +1 -1
  159. package/dist/testing/error_coverage.js +86 -119
  160. package/dist/testing/integration.d.ts +28 -1
  161. package/dist/testing/integration.d.ts.map +1 -1
  162. package/dist/testing/integration.js +54 -29
  163. package/dist/testing/integration_helpers.d.ts +37 -9
  164. package/dist/testing/integration_helpers.d.ts.map +1 -1
  165. package/dist/testing/integration_helpers.js +75 -40
  166. package/dist/testing/middleware.d.ts +3 -5
  167. package/dist/testing/middleware.d.ts.map +1 -1
  168. package/dist/testing/middleware.js +5 -4
  169. package/dist/testing/middleware_gate_probes.d.ts +90 -0
  170. package/dist/testing/middleware_gate_probes.d.ts.map +1 -0
  171. package/dist/testing/middleware_gate_probes.js +179 -0
  172. package/dist/testing/rate_limiting.d.ts +1 -1
  173. package/dist/testing/rate_limiting.d.ts.map +1 -1
  174. package/dist/testing/rate_limiting.js +5 -4
  175. package/dist/testing/round_trip.d.ts +7 -0
  176. package/dist/testing/round_trip.d.ts.map +1 -1
  177. package/dist/testing/round_trip.js +8 -5
  178. package/dist/testing/schema_generators.d.ts +40 -11
  179. package/dist/testing/schema_generators.d.ts.map +1 -1
  180. package/dist/testing/schema_generators.js +71 -17
  181. package/dist/testing/stubs.d.ts +10 -1
  182. package/dist/testing/stubs.d.ts.map +1 -1
  183. package/dist/testing/stubs.js +15 -5
  184. package/dist/testing/surface_invariants.d.ts +36 -115
  185. package/dist/testing/surface_invariants.d.ts.map +1 -1
  186. package/dist/testing/surface_invariants.js +106 -269
  187. package/dist/testing/transports/raw_http.d.ts +10 -2
  188. package/dist/testing/transports/raw_http.d.ts.map +1 -1
  189. package/dist/testing/transports/raw_http.js +10 -4
  190. package/dist/ui/CLAUDE.md +4 -2
  191. package/dist/ui/auth_state.svelte.d.ts.map +1 -1
  192. package/dist/ui/auth_state.svelte.js +2 -8
  193. package/dist/ui/backend_probe.svelte.js +3 -3
  194. package/dist/ui/table_state.svelte.d.ts.map +1 -1
  195. package/dist/ui/table_state.svelte.js +1 -8
  196. package/dist/ui/ui_fetch.d.ts +12 -2
  197. package/dist/ui/ui_fetch.d.ts.map +1 -1
  198. package/dist/ui/ui_fetch.js +22 -4
  199. package/package.json +1 -1
@@ -2,99 +2,52 @@ import './assert_dev_env.js';
2
2
  /**
3
3
  * Error reachability coverage tracking.
4
4
  *
5
- * Tracks which declared error statuses (and specific error codes) are
6
- * actually exercised in tests. `ErrorCoverageCollector` records status
7
- * codes (optionally with body `error` codes) observed during test runs,
8
- * then `assert_error_coverage` compares against declared error schemas
9
- * to find uncovered error paths — reporting per-code when the declared
10
- * schema is a literal or enum, per-status otherwise.
5
+ * Tracks which declared error codes are actually exercised in tests.
6
+ * `ErrorCoverageCollector` records the error responses observed during test
7
+ * runs, each by its body's `error` code where it is known, then
8
+ * `assert_error_coverage` compares against each route's error map
9
+ * (`resolve_route_error_map`) to find uncovered error paths — one per declared
10
+ * code at each status, read from the map's definitions. An observation with a
11
+ * code covers that one path; one recorded with a `null` code covers every code
12
+ * declared at its status.
11
13
  *
12
14
  * @module
13
15
  */
14
- import { z } from 'zod';
15
16
  import { assert } from 'vitest';
16
- import { is_schema } from '@fuzdev/fuz_schema/schema.ts';
17
- import { get_schema_entries } from '@fuzdev/fuz_schema/introspect.ts';
18
- import { merge_error_schemas } from '../http/schema_helpers.js';
17
+ import { format_route_key } from '../http/surface_query.js';
18
+ import { get_route_error_map } from '../http/route_errors.js';
19
19
  import { find_route_spec, assert_response_matches_spec } from './integration_helpers.js';
20
- /**
21
- * Extract declared error code values from an error response schema.
22
- *
23
- * Recognizes schemas shaped like `z.object({error: z.literal(...)})` or
24
- * `z.object({error: z.enum([...])})` (incl. `looseObject`/`strictObject`),
25
- * and the `@fuzdev/fuz_schema` forms `object({error: literal(...)})` or
26
- * `object({error: one_of([...])})` (any object kind, under any wrappers —
27
- * `get_schema_entries`; the `error` entry itself read as given, as the Zod
28
- * field is). Returns the set of declared code values, or `null` if the schema
29
- * doesn't expose a literal/enum `error` field (e.g., bare `ApiError` with
30
- * `z.string()`).
31
- *
32
- * Used by coverage reporting to split a single declared status into per-code
33
- * rows when the route's error schema names specific codes.
34
- */
35
- export const extract_declared_error_codes = (schema) => {
36
- if (is_schema(schema)) {
37
- const error_field = get_schema_entries(schema)?.error;
38
- if (!error_field)
39
- return null;
40
- if (error_field.kind === 'literal')
41
- return [String(error_field.def)];
42
- if (error_field.kind === 'one_of') {
43
- return error_field.def.map(String);
44
- }
45
- return null;
46
- }
47
- if (!(schema instanceof z.ZodObject))
48
- return null;
49
- const error_field = schema.shape.error;
50
- if (!error_field)
51
- return null;
52
- if (error_field instanceof z.ZodLiteral) {
53
- return [...error_field.values].map(String);
54
- }
55
- if (error_field instanceof z.ZodEnum) {
56
- return error_field.options.map(String);
57
- }
58
- return null;
59
- };
60
20
  /**
61
21
  * Shared walk over declared error paths.
62
22
  *
63
23
  * Single source of truth for the route → status → code traversal used by
64
24
  * both `uncovered()` and `assert_error_coverage`. Yields one entry per
65
- * declared coverage path with a `covered` flag, applying the "any-code"
66
- * rule (status-only observation covers all declared codes).
25
+ * declared (status, code) of each route's error map, read from its
26
+ * definitions, with a `covered` flag: covered when that code was observed
27
+ * at that status, or when the status was observed with a `null` code (the
28
+ * "any-code" rule, for a response that carries none). Every definition
29
+ * carries a code, so a declared status always has at least one.
67
30
  */
68
31
  const walk_coverage = (collector, route_specs, options) => {
69
32
  const ignore_routes = new Set(options?.ignore_routes);
70
33
  const ignore_statuses = new Set(options?.ignore_statuses);
71
34
  const entries = [];
72
35
  for (const spec of route_specs) {
73
- const route_key = `${spec.method} ${spec.path}`;
36
+ const route_key = format_route_key(spec);
74
37
  if (ignore_routes.has(route_key))
75
38
  continue;
76
- const merged = merge_error_schemas(spec);
77
- if (!merged)
78
- continue;
79
- for (const status_str of Object.keys(merged)) {
80
- const status = Number(status_str);
39
+ // a code with two shapes at one status is one path
40
+ const seen = new Set();
41
+ for (const { status, code } of get_route_error_map(collector.route_error_maps, spec).defs) {
81
42
  if (ignore_statuses.has(status))
82
43
  continue;
83
- const error_schema = merged[status];
84
- if (!error_schema)
85
- continue;
86
44
  const status_key = `${spec.method} ${spec.path}:${status}`;
87
- const status_observed = collector.observed.has(status_key);
88
- const codes = extract_declared_error_codes(error_schema);
89
- if (codes && codes.length > 0) {
90
- for (const code of codes) {
91
- const covered = status_observed || collector.observed.has(`${status_key}:${code}`);
92
- entries.push({ method: spec.method, path: spec.path, status, code, covered });
93
- }
94
- }
95
- else {
96
- entries.push({ method: spec.method, path: spec.path, status, covered: status_observed });
97
- }
45
+ const code_key = `${status_key}:${code}`;
46
+ if (seen.has(code_key))
47
+ continue;
48
+ seen.add(code_key);
49
+ const covered = collector.observed.has(status_key) || collector.observed.has(code_key);
50
+ entries.push({ method: spec.method, path: spec.path, status, code, covered });
98
51
  }
99
52
  }
100
53
  return entries;
@@ -103,69 +56,84 @@ const walk_coverage = (collector, route_specs, options) => {
103
56
  * Tracks which route × status (and route × status × code) combinations have
104
57
  * been exercised in tests.
105
58
  *
106
- * Use `record()` to log an observed status (optionally with the body's `error`
107
- * code), or `assert_and_record()` to combine response validation with tracking
108
- * (auto-extracts `body.error` from the response when present).
59
+ * Use `record()` to log an observed status with the body's `error` code (`null`
60
+ * for none), or `assert_and_record()` to combine response validation with
61
+ * tracking (reads `body.error` from the response).
109
62
  * After all tests, call `uncovered()` to find declared error paths never
110
63
  * exercised.
111
64
  *
112
- * An observation recorded without a code still satisfies "any-code" coverage
113
- * requirements for the same status — i.e., if a caller records just the status,
114
- * all declared codes for that status are considered covered. Per-code tracking
115
- * is additive: callers who know the body's `error` value should pass it to get
116
- * precise per-code gap reporting on routes with literal/enum error schemas.
65
+ * An observation with a code covers exactly that (route, status, code): a
66
+ * `forbidden_origin` 403 leaves a declared `credential_type_required` 403
67
+ * uncovered. An observation recorded with a `null` code — a response that
68
+ * carries none — satisfies the "any-code" rule for its status: every code
69
+ * declared there counts as covered.
117
70
  */
118
71
  export class ErrorCoverageCollector {
119
72
  /**
120
- * Observed keys: `"METHOD /spec-path:STATUS"` or `"METHOD /spec-path:STATUS:CODE"`.
121
- *
122
- * Both shapes coexist — the code-less key marks the status as covered at any
123
- * code; a code-bearing key adds per-code precision.
73
+ * Each route's error map, keyed `METHOD path` (`AppSurfaceSpec.route_error_maps`),
74
+ * which the declared paths and the response checks read; without one, a
75
+ * route's own map without middleware.
76
+ */
77
+ route_error_maps;
78
+ /**
79
+ * @param route_error_maps - each route's error map, keyed `METHOD path` (`AppSurfaceSpec.route_error_maps`)
80
+ */
81
+ constructor(route_error_maps) {
82
+ this.route_error_maps = route_error_maps ?? null;
83
+ }
84
+ /**
85
+ * Observed keys: `"METHOD /spec-path:STATUS:CODE"` for an observation with a
86
+ * code, `"METHOD /spec-path:STATUS"` for one without, which covers every
87
+ * code declared at the status.
124
88
  */
125
89
  observed = new Set();
126
90
  /**
127
- * Record an observed error status (optionally with the body's `error` code) for a route.
91
+ * Record an observed error status and its body's `error` code for a route.
128
92
  *
129
93
  * Resolves the concrete request path back to the spec template path
130
- * (e.g., `/api/accounts/abc` → `/api/accounts/:id`). When `code` is provided,
131
- * it is stored alongside the status for per-code coverage tracking.
94
+ * (e.g., `/api/accounts/abc` → `/api/accounts/:id`). A `code` covers that
95
+ * code alone; `null`, for a response that truly carries no code, covers
96
+ * every code declared at the status — so any-code coverage is asked for,
97
+ * never had by leaving the code out.
132
98
  *
133
99
  * @param path - request path (may be concrete)
134
- * @param code - observed body `error` code (pass when the route's error
135
- * schema declares specific codes via `z.literal` or `z.enum`)
136
- * @mutates `this.observed` - adds the resolved `"METHOD /spec-path:STATUS"`
137
- * key (and the `:CODE` variant when `code` is provided).
100
+ * @param code - the body's `error` code, or `null` when the response has none
101
+ * @mutates `this.observed` - adds `"METHOD /spec-path:STATUS:CODE"` for a
102
+ * code, `"METHOD /spec-path:STATUS"` for `null`.
138
103
  */
139
104
  record(route_specs, method, path, status, code) {
140
105
  const spec = find_route_spec(route_specs, method, path);
141
106
  const spec_path = spec ? spec.path : path;
142
107
  const base_key = `${method} ${spec_path}:${status}`;
143
- this.observed.add(base_key);
144
- if (code !== undefined) {
145
- this.observed.add(`${base_key}:${code}`);
146
- }
108
+ // a known code covers that code alone, so a refusal at a shared status
109
+ // is no evidence for the others there
110
+ this.observed.add(code === null ? base_key : `${base_key}:${code}`);
147
111
  }
148
112
  /**
149
113
  * Validate a response against its route spec and record the status.
150
114
  *
151
- * Wraps `assert_response_matches_spec` and records the status code. For
152
- * error responses, auto-extracts `body.error` from the JSON body (via a
153
- * cloned response, so the original stream stays usable) and records it
154
- * for per-code coverage. Pass an explicit `code` to override the
155
- * auto-extracted value or when the body was already consumed.
115
+ * Wraps `assert_response_matches_spec` and records the status with the
116
+ * body's `error` code, read from a clone so the original stream stays
117
+ * usable; a body with no string `error` records `null`. Pass `code` when
118
+ * the body was already consumed.
156
119
  *
157
- * @param code - observed body `error` code (override; if omitted and the
158
- * response body is a JSON object with a string `error` field, that value
159
- * is auto-extracted)
120
+ * @param code - the body's `error` code, or `null` when it has none; read
121
+ * from the body when omitted
160
122
  * @mutates `this.observed` - via `record` after `assert_response_matches_spec`
161
123
  * succeeds.
162
124
  * @throws Error if the response body fails the route spec's declared
163
- * schemas (propagated from `assert_response_matches_spec`).
125
+ * schemas (propagated from `assert_response_matches_spec`), or if the
126
+ * body was consumed and no `code` was given.
164
127
  */
165
128
  async assert_and_record(route_specs, method, path, response, code) {
166
- await assert_response_matches_spec(route_specs, method, path, response);
129
+ if (code === undefined && response.bodyUsed) {
130
+ throw new Error(`assert_and_record: the body of ${method} ${path} was consumed; pass its code ` +
131
+ '(or `null` for none)');
132
+ }
133
+ await assert_response_matches_spec(route_specs, method, path, response, this.route_error_maps);
167
134
  let resolved_code = code;
168
- if (resolved_code === undefined && !response.ok && !response.bodyUsed) {
135
+ if (resolved_code === undefined) {
136
+ resolved_code = null;
169
137
  try {
170
138
  const body = await response.clone().json();
171
139
  if (body && typeof body.error === 'string') {
@@ -181,16 +149,16 @@ export class ErrorCoverageCollector {
181
149
  /**
182
150
  * Find declared error paths that were never observed.
183
151
  *
184
- * Computes the declared set from `merge_error_schemas` for each route spec.
185
- * For statuses whose error schema names specific codes (via `z.literal` or
186
- * `z.enum`), reports per-code rows; otherwise reports one row per status.
187
- * A status-only observation (no code) satisfies all declared codes for that
188
- * status — the "any-code" rule.
152
+ * Computes the declared set from each route's error map
153
+ * (`resolve_route_error_map`, `this.route_error_maps`): one row per declared code
154
+ * at each status. An observation with a code covers that row alone; a
155
+ * status-only observation (a `null` code) covers every row at its status — the
156
+ * "any-code" rule.
189
157
  */
190
158
  uncovered(route_specs, options) {
191
159
  return walk_coverage(this, route_specs, options)
192
160
  .filter((entry) => !entry.covered)
193
- .map(({ method, path, status, code }) => ({ method, path, status, ...(code && { code }) }));
161
+ .map(({ method, path, status, code }) => ({ method, path, status, code }));
194
162
  }
195
163
  }
196
164
  /**
@@ -205,16 +173,15 @@ export const DEFAULT_INTEGRATION_ERROR_COVERAGE = 0.2;
205
173
  * Uses `status (code)` — spaces around the code make `:` unambiguous as
206
174
  * the route_key / status separator.
207
175
  */
208
- const format_uncovered = (entry) => `${entry.method} ${entry.path} → ${entry.status}${entry.code ? ` (${entry.code})` : ''}`;
176
+ const format_uncovered = (entry) => `${entry.method} ${entry.path} → ${entry.status} (${entry.code})`;
209
177
  /**
210
178
  * Assert error coverage meets a minimum threshold.
211
179
  *
212
- * Computes the ratio of exercised error paths to total declared error paths.
213
- * For routes whose status error schema names specific codes (`z.literal` or
214
- * `z.enum`), each declared code counts as one coverage path; for schemas
215
- * without declared codes (`ApiError`/`z.string()`), the status counts as one
216
- * path. A status-only observation covers all declared codes for that status
217
- * (the "any-code" rule).
180
+ * Computes the ratio of exercised error paths to total declared error paths,
181
+ * each declared code at each status of a route's error map
182
+ * (`collector.route_error_maps`) one path. An observation with a code covers
183
+ * that path alone; a status-only observation (a `null` code) covers every declared code at
184
+ * its status (the "any-code" rule).
218
185
  *
219
186
  * When `min_coverage` is 0 (default), logs coverage info without failing.
220
187
  * When > 0, fails if coverage is below the threshold.
@@ -1,6 +1,7 @@
1
1
  import './assert_dev_env.js';
2
2
  import type { SessionOptions } from '../auth/session_cookie.js';
3
3
  import { type RpcEndpointsSuiteOption } from './rpc_helpers.js';
4
+ import { type UncoveredEntry } from './error_coverage.js';
4
5
  import type { AppSurfaceSpec } from '../http/surface.js';
5
6
  import { type BackendCapabilities } from './cross_backend/capabilities.js';
6
7
  import type { SetupTest } from './cross_backend/setup.js';
@@ -56,6 +57,31 @@ export interface StandardIntegrationTestOptions {
56
57
  * denial-path drivers.
57
58
  */
58
59
  error_coverage_min?: number;
60
+ /**
61
+ * Base URL the backend is reachable at, or `null` in-process. The
62
+ * middleware-gate probes write their heads to the socket when there is one
63
+ * (`fetch` replaces a caller's `Host` and `Content-Length`), and go through
64
+ * the fixture's transport when there isn't.
65
+ */
66
+ base_url: string | null;
67
+ /**
68
+ * The backend's request-body cap on every counted route but the RPC
69
+ * endpoint, in bytes — the body-limit probe declares one byte more.
70
+ * Default `SPINE_BODY_LIMIT_DEFAULT_BYTES`, the spines' default.
71
+ */
72
+ max_body_bytes?: number;
73
+ /**
74
+ * The backend's request-body cap on the RPC endpoint, in bytes, when it
75
+ * isn't `max_body_bytes`.
76
+ */
77
+ rpc_max_body_bytes?: number;
78
+ /**
79
+ * Called once the suite's tests have run, with the declared error paths of
80
+ * the counted routes it never observed, before the coverage floor is
81
+ * asserted — for a wiring that holds its coverage to an exact set, which
82
+ * catches one covered code swapped for another where a ratio can't.
83
+ */
84
+ check_uncovered_errors?: (uncovered: Array<UncoveredEntry>) => void;
59
85
  }
60
86
  /**
61
87
  * Standard integration test suite for fuz_app auth routes.
@@ -64,7 +90,8 @@ export interface StandardIntegrationTestOptions {
64
90
  * revocation, password change (incl. API token revocation), origin
65
91
  * verification, bearer auth (incl. browser context discard on mutations),
66
92
  * token revocation, cross-account isolation, expired credential rejection,
67
- * signup invite edge cases, and response body validation.
93
+ * signup invite edge cases, response body validation, and the middleware
94
+ * gates each counted route's error map declares (`middleware_gate_probes.ts`).
68
95
  *
69
96
  * Each test group asserts that required routes exist, failing with a descriptive
70
97
  * message if the consumer's route specs are misconfigured.
@@ -1 +1 @@
1
- {"version":3,"file":"integration.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/integration.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAuB7B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAMhE,OAAO,EAKN,KAAK,uBAAuB,EAC5B,MAAM,kBAAkB,CAAC;AAkB1B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EAAW,KAAK,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAKpF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAK1D;;GAEG;AACH,MAAM,WAAW,8BAA8B;IAC9C;;;;OAIG;IACH,UAAU,EAAE,SAAS,CAAC;IACtB;;;;;;;;;OASG;IACH,cAAc,EAAE,cAAc,CAAC;IAC/B,kEAAkE;IAClE,YAAY,EAAE,mBAAmB,CAAC;IAClC;;;;OAIG;IACH,eAAe,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;IACxC;;;;;;;;;;;;OAYG;IACH,aAAa,EAAE,uBAAuB,CAAC;IACvC;;;;;;;OAOG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,eAAO,MAAM,mCAAmC,GAC/C,SAAS,8BAA8B,KACrC,IA28CF,CAAC"}
1
+ {"version":3,"file":"integration.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/testing/integration.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,CAAC;AAuB7B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAMhE,OAAO,EAKN,KAAK,uBAAuB,EAC5B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAIN,KAAK,cAAc,EACnB,MAAM,qBAAqB,CAAC;AAa7B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EAAW,KAAK,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAKpF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAY1D;;GAEG;AACH,MAAM,WAAW,8BAA8B;IAC9C;;;;OAIG;IACH,UAAU,EAAE,SAAS,CAAC;IACtB;;;;;;;;;OASG;IACH,cAAc,EAAE,cAAc,CAAC;IAC/B,kEAAkE;IAClE,YAAY,EAAE,mBAAmB,CAAC;IAClC;;;;OAIG;IACH,eAAe,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;IACxC;;;;;;;;;;;;OAYG;IACH,aAAa,EAAE,uBAAuB,CAAC;IACvC;;;;;;;OAOG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;;OAKG;IACH,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;;OAKG;IACH,sBAAsB,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,CAAC,cAAc,CAAC,KAAK,IAAI,CAAC;CACpE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,eAAO,MAAM,mCAAmC,GAC/C,SAAS,8BAA8B,KACrC,IA0/CF,CAAC"}
@@ -30,6 +30,7 @@ import { assert_iso8601_seconds, assert_iso8601_seconds_nullable } from './cross
30
30
  import { DEFAULT_TEST_PASSWORD } from './test_credentials.js';
31
31
  import { create_test_request_headers, TEST_ORIGIN } from './test_origin.js';
32
32
  import { create_test_raw_api_token } from './entities.js';
33
+ import { assert_middleware_gate_refusal, format_middleware_gate_probe, plan_middleware_gate_probes, send_middleware_gate_probe } from './middleware_gate_probes.js';
33
34
  /**
34
35
  * Standard integration test suite for fuz_app auth routes.
35
36
  *
@@ -37,7 +38,8 @@ import { create_test_raw_api_token } from './entities.js';
37
38
  * revocation, password change (incl. API token revocation), origin
38
39
  * verification, bearer auth (incl. browser context discard on mutations),
39
40
  * token revocation, cross-account isolation, expired credential rejection,
40
- * signup invite edge cases, and response body validation.
41
+ * signup invite edge cases, response body validation, and the middleware
42
+ * gates each counted route's error map declares (`middleware_gate_probes.ts`).
41
43
  *
42
44
  * Each test group asserts that required routes exist, failing with a descriptive
43
45
  * message if the consumer's route specs are misconfigured.
@@ -74,7 +76,7 @@ export const describe_standard_integration_tests = (options) => {
74
76
  describe('standard_integration', () => {
75
77
  const { cookie_name } = options.session_options;
76
78
  // Error coverage tracking across test groups
77
- const error_collector = new ErrorCoverageCollector();
79
+ const error_collector = new ErrorCoverageCollector(options.surface_source.route_error_maps);
78
80
  // the auth routes this suite drives: login / logout / password, signup
79
81
  // (when wired), and the shared RPC endpoint — admin-role routes belong to
80
82
  // the admin suite
@@ -97,16 +99,10 @@ export const describe_standard_integration_tests = (options) => {
97
99
  // routes would dilute the ratio; admin-role routes are scoped to
98
100
  // the admin suite instead.
99
101
  const auth_routes = route_specs.filter(is_suite_auth_route);
100
- assert_error_coverage(error_collector, auth_routes.length > 0 ? auth_routes : route_specs, {
101
- min_coverage: options.error_coverage_min ?? DEFAULT_INTEGRATION_ERROR_COVERAGE,
102
- // Authorization denials (403) on these scoped auth routes — the
103
- // credential-channel gate on /logout + /password, the invite gate on
104
- // /signup — are exercised by the conformance + attack-surface suites,
105
- // not this lifecycle suite. Drop 403 from this collector's denominator
106
- // (same spirit as the [401, 403, 429] ignore in the attack-surface
107
- // tightness defaults); otherwise #10 adding /logout's 403 to the spine
108
- // surface tips the ratio under threshold here though the gate is tested.
109
- ignore_statuses: [403]
102
+ const scoped_routes = auth_routes.length > 0 ? auth_routes : route_specs;
103
+ options.check_uncovered_errors?.(error_collector.uncovered(scoped_routes));
104
+ assert_error_coverage(error_collector, scoped_routes, {
105
+ min_coverage: options.error_coverage_min ?? DEFAULT_INTEGRATION_ERROR_COVERAGE
110
106
  });
111
107
  });
112
108
  // --- 1. Login/logout lifecycle ---
@@ -538,7 +534,7 @@ export const describe_standard_integration_tests = (options) => {
538
534
  })
539
535
  });
540
536
  assert.strictEqual(res.status, 401);
541
- error_collector.record(route_specs, 'POST', password_route.path, 401);
537
+ await error_collector.assert_and_record(route_specs, 'POST', password_route.path, res);
542
538
  // Session should still be valid (password didn't change)
543
539
  const verify_res = await rpc_call_for_spec({
544
540
  app: { request: fixture.transport },
@@ -715,6 +711,7 @@ export const describe_standard_integration_tests = (options) => {
715
711
  headers: { cookie: `${cookie_name}=${non_admin.session_cookie}` }
716
712
  });
717
713
  assert.strictEqual(res.status, 403);
714
+ await error_collector.assert_and_record(route_specs, admin_route.method, admin_route.path, res);
718
715
  const body = await res.json();
719
716
  assert.strictEqual(body.error, 'insufficient_permissions');
720
717
  });
@@ -868,7 +865,7 @@ export const describe_standard_integration_tests = (options) => {
868
865
  })
869
866
  });
870
867
  assert.strictEqual(res.status, 401);
871
- await assert_response_matches_spec(route_specs, 'POST', login_route.path, res);
868
+ await error_collector.assert_and_record(route_specs, 'POST', login_route.path, res);
872
869
  });
873
870
  test('POST /logout 200 response matches output schema', async () => {
874
871
  const fixture = await options.setup_test();
@@ -880,7 +877,7 @@ export const describe_standard_integration_tests = (options) => {
880
877
  body: JSON.stringify({})
881
878
  });
882
879
  assert.strictEqual(res.status, 200);
883
- await assert_response_matches_spec(route_specs, 'POST', logout_route.path, res);
880
+ await assert_response_matches_spec(route_specs, 'POST', logout_route.path, res, options.surface_source.route_error_maps);
884
881
  });
885
882
  test('POST /logout 401 response matches declared error schema', async () => {
886
883
  const fixture = await options.setup_test();
@@ -894,7 +891,7 @@ export const describe_standard_integration_tests = (options) => {
894
891
  body: JSON.stringify({})
895
892
  });
896
893
  assert.strictEqual(res.status, 401);
897
- await assert_response_matches_spec(route_specs, 'POST', logout_route.path, res);
894
+ await error_collector.assert_and_record(route_specs, 'POST', logout_route.path, res);
898
895
  });
899
896
  });
900
897
  // Rate-limit behavior is covered end-to-end by
@@ -906,11 +903,12 @@ export const describe_standard_integration_tests = (options) => {
906
903
  describe('error coverage breadth', () => {
907
904
  test('exercises 401 on multiple auth-required routes for error coverage', async () => {
908
905
  const fixture = await options.setup_test();
909
- // Hit several auth-required RPC methods without credentials to
910
- // broaden error coverage beyond just /login. RPC 401s are tracked
911
- // against the shared endpoint path. The dispatcher runs auth before
912
- // params validation, so any well-formed param body works — we just
913
- // need each call to be type-correct wrt its spec.
906
+ // Hit several auth-required RPC methods without credentials. A
907
+ // refusal on the RPC endpoint is a JSON-RPC envelope, not a declared
908
+ // REST error, so these assert the 401 without recording it; the
909
+ // logout probe below is the REST one that counts. The dispatcher runs
910
+ // auth before params validation, so any well-formed param body works
911
+ // — we just need each call to be type-correct wrt its spec.
914
912
  //
915
913
  // Fresh transport so the per-test session cookie in
916
914
  // `fixture.transport`'s jar doesn't leak into these unauthed
@@ -924,7 +922,6 @@ export const describe_standard_integration_tests = (options) => {
924
922
  headers: { host: 'localhost' }
925
923
  });
926
924
  assert.strictEqual(session_list.status, 401);
927
- error_collector.record(route_specs, 'POST', rpc_path, 401);
928
925
  const session_revoke_all = await rpc_call_for_spec({
929
926
  app: { request: unauthed },
930
927
  path: rpc_path,
@@ -933,7 +930,6 @@ export const describe_standard_integration_tests = (options) => {
933
930
  headers: { host: 'localhost' }
934
931
  });
935
932
  assert.strictEqual(session_revoke_all.status, 401);
936
- error_collector.record(route_specs, 'POST', rpc_path, 401);
937
933
  const token_list = await rpc_call_for_spec({
938
934
  app: { request: unauthed },
939
935
  path: rpc_path,
@@ -942,7 +938,6 @@ export const describe_standard_integration_tests = (options) => {
942
938
  headers: { host: 'localhost' }
943
939
  });
944
940
  assert.strictEqual(token_list.status, 401);
945
- error_collector.record(route_specs, 'POST', rpc_path, 401);
946
941
  const token_create = await rpc_call_for_spec({
947
942
  app: { request: unauthed },
948
943
  path: rpc_path,
@@ -955,7 +950,6 @@ export const describe_standard_integration_tests = (options) => {
955
950
  headers: { host: 'localhost' }
956
951
  });
957
952
  assert.strictEqual(token_create.status, 401);
958
- error_collector.record(route_specs, 'POST', rpc_path, 401);
959
953
  const verify = await rpc_call_for_spec({
960
954
  app: { request: unauthed },
961
955
  path: rpc_path,
@@ -964,7 +958,6 @@ export const describe_standard_integration_tests = (options) => {
964
958
  headers: { host: 'localhost' }
965
959
  });
966
960
  assert.strictEqual(verify.status, 401);
967
- error_collector.record(route_specs, 'POST', rpc_path, 401);
968
961
  // Also exercise POST /logout without auth (still REST)
969
962
  const logout_route = find_auth_route(route_specs, '/logout', 'POST');
970
963
  if (logout_route) {
@@ -974,7 +967,7 @@ export const describe_standard_integration_tests = (options) => {
974
967
  body: JSON.stringify({})
975
968
  });
976
969
  assert.strictEqual(res.status, 401, 'POST /logout without auth should return 401');
977
- error_collector.record(route_specs, 'POST', logout_route.path, 401);
970
+ await error_collector.assert_and_record(route_specs, 'POST', logout_route.path, res);
978
971
  }
979
972
  });
980
973
  });
@@ -1023,7 +1016,7 @@ export const describe_standard_integration_tests = (options) => {
1023
1016
  }
1024
1017
  });
1025
1018
  assert.strictEqual(res.status, 401, 'Bearer with Origin should be discarded → unauthenticated');
1026
- error_collector.record(route_specs, 'POST', logout_route.path, 401);
1019
+ await error_collector.assert_and_record(route_specs, 'POST', logout_route.path, res);
1027
1020
  });
1028
1021
  test('bearer token with Referer header discarded on POST password', async () => {
1029
1022
  const fixture = await options.setup_test();
@@ -1037,7 +1030,7 @@ export const describe_standard_integration_tests = (options) => {
1037
1030
  headers: { ...bearer_headers, referer: `${TEST_ORIGIN}/admin` }
1038
1031
  });
1039
1032
  assert.strictEqual(res.status, 401, 'Bearer with Referer should be discarded → unauthenticated');
1040
- error_collector.record(route_specs, 'POST', password_route.path, 401);
1033
+ await error_collector.assert_and_record(route_specs, 'POST', password_route.path, res);
1041
1034
  });
1042
1035
  });
1043
1036
  // --- 12b. Content type: every JSON POST route refuses an untyped body ---
@@ -1057,6 +1050,35 @@ export const describe_standard_integration_tests = (options) => {
1057
1050
  }
1058
1051
  });
1059
1052
  });
1053
+ // --- 12c. Middleware gates: each counted route's declared gates refuse ---
1054
+ describe('middleware gates', () => {
1055
+ // One probe per gate each counted route's error map declares — the
1056
+ // origin check, the Rust spine's host gate, the body limit — each a
1057
+ // bodiless head with no credential that the gate refuses before auth,
1058
+ // rate limiting or body parsing. A gate the map doesn't declare on a
1059
+ // route (a carve-out, a backend without the layer) gets no probe.
1060
+ const probes = plan_middleware_gate_probes({
1061
+ route_specs: route_specs.filter(is_suite_auth_route),
1062
+ route_error_maps: options.surface_source.route_error_maps,
1063
+ base_url: options.base_url,
1064
+ max_body_bytes: options.max_body_bytes,
1065
+ rpc_path,
1066
+ rpc_max_body_bytes: options.rpc_max_body_bytes
1067
+ });
1068
+ // in-process the probes share one fixture's transport, made by the first
1069
+ // to run; over a socket they need none, as they read and write no state
1070
+ let in_process_transport = null;
1071
+ const get_transport = () => (in_process_transport ??= options
1072
+ .setup_test()
1073
+ .then((fixture) => fixture.fresh_transport({ origin: null })));
1074
+ for (const probe of probes) {
1075
+ test(format_middleware_gate_probe(probe), async () => {
1076
+ const transport = options.base_url === null ? await get_transport() : null;
1077
+ const res = await send_middleware_gate_probe(probe, options.base_url, transport);
1078
+ await assert_middleware_gate_refusal(probe, res, error_collector, route_specs);
1079
+ });
1080
+ }
1081
+ });
1060
1082
  // --- 13. Password change revokes API tokens ---
1061
1083
  describe('password change revokes API tokens', () => {
1062
1084
  test('API tokens are invalidated after password change', async () => {
@@ -1158,6 +1180,7 @@ export const describe_standard_integration_tests = (options) => {
1158
1180
  })
1159
1181
  });
1160
1182
  assert.strictEqual(signup_res.status, 403, 'Signup with non-matching email should be rejected');
1183
+ await error_collector.assert_and_record(route_specs, 'POST', signup_route.path, signup_res);
1161
1184
  const body = await signup_res.json();
1162
1185
  assert.strictEqual(body.error, 'no_matching_invite');
1163
1186
  });
@@ -1208,6 +1231,7 @@ export const describe_standard_integration_tests = (options) => {
1208
1231
  })
1209
1232
  });
1210
1233
  assert.strictEqual(no_match_res.status, 403, 'Expected 403 for non-matching invite');
1234
+ await error_collector.assert_and_record(route_specs, 'POST', signup_route.path, no_match_res);
1211
1235
  const no_match_body = await no_match_res.json();
1212
1236
  // Create invite for a different email via RPC
1213
1237
  const conflict_email = 'conflict-test@example.com';
@@ -1232,6 +1256,7 @@ export const describe_standard_integration_tests = (options) => {
1232
1256
  })
1233
1257
  });
1234
1258
  assert.strictEqual(conflict_res.status, 409, 'Expected 409 for username conflict');
1259
+ await error_collector.assert_and_record(route_specs, 'POST', signup_route.path, conflict_res);
1235
1260
  const conflict_body = await conflict_res.json();
1236
1261
  // Assert both failure responses have identical Object.keys()
1237
1262
  const no_match_keys = Object.keys(no_match_body).sort();