@schmock/core 2.2.3 → 2.3.1

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 (94) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +35 -0
  3. package/dist/abort.d.ts +3 -0
  4. package/dist/abort.d.ts.map +1 -0
  5. package/dist/abort.js +30 -0
  6. package/dist/binary.d.ts +8 -0
  7. package/dist/binary.d.ts.map +1 -0
  8. package/dist/binary.js +9 -0
  9. package/dist/builder.d.ts +31 -10
  10. package/dist/builder.d.ts.map +1 -1
  11. package/dist/builder.js +804 -203
  12. package/dist/constants.d.ts +38 -0
  13. package/dist/constants.d.ts.map +1 -1
  14. package/dist/constants.js +123 -2
  15. package/dist/errors.d.ts +14 -3
  16. package/dist/errors.d.ts.map +1 -1
  17. package/dist/errors.js +41 -6
  18. package/dist/helpers.d.ts +9 -0
  19. package/dist/helpers.d.ts.map +1 -1
  20. package/dist/helpers.js +18 -2
  21. package/dist/http-helpers.d.ts +52 -5
  22. package/dist/http-helpers.d.ts.map +1 -1
  23. package/dist/http-helpers.js +153 -37
  24. package/dist/index.d.ts +381 -60
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +9 -3
  27. package/dist/interceptor.d.ts +11 -1
  28. package/dist/interceptor.d.ts.map +1 -1
  29. package/dist/interceptor.js +354 -156
  30. package/dist/parser.d.ts.map +1 -1
  31. package/dist/parser.js +12 -3
  32. package/dist/plugin-pipeline.d.ts +11 -4
  33. package/dist/plugin-pipeline.d.ts.map +1 -1
  34. package/dist/plugin-pipeline.js +117 -39
  35. package/dist/response-normalizer.d.ts +16 -0
  36. package/dist/response-normalizer.d.ts.map +1 -0
  37. package/dist/response-normalizer.js +316 -0
  38. package/dist/response-parser.d.ts.map +1 -1
  39. package/dist/response-parser.js +85 -18
  40. package/dist/route-matcher.d.ts +3 -0
  41. package/dist/route-matcher.d.ts.map +1 -1
  42. package/dist/route-matcher.js +12 -6
  43. package/dist/types.d.ts +7 -2
  44. package/dist/types.d.ts.map +1 -1
  45. package/package.json +18 -8
  46. package/src/audit-core-builder.test.ts +0 -218
  47. package/src/audit-response-guard.test.ts +0 -38
  48. package/src/builder.test.ts +0 -289
  49. package/src/builder.ts +0 -701
  50. package/src/constants.test.ts +0 -99
  51. package/src/constants.ts +0 -73
  52. package/src/debug.test.ts +0 -241
  53. package/src/delay.test.ts +0 -319
  54. package/src/dist-shape.test.ts +0 -35
  55. package/src/errors.test.ts +0 -223
  56. package/src/errors.ts +0 -130
  57. package/src/factory.test.ts +0 -133
  58. package/src/helpers.test.ts +0 -147
  59. package/src/helpers.ts +0 -58
  60. package/src/http-helpers.ts +0 -113
  61. package/src/index.ts +0 -151
  62. package/src/interceptor.test.ts +0 -291
  63. package/src/interceptor.ts +0 -320
  64. package/src/namespace.test.ts +0 -274
  65. package/src/parser.property.test.ts +0 -594
  66. package/src/parser.test.ts +0 -148
  67. package/src/parser.ts +0 -64
  68. package/src/plugin-pipeline.ts +0 -103
  69. package/src/plugin-system.test.ts +0 -602
  70. package/src/response-parser.ts +0 -75
  71. package/src/response-parsing.test.ts +0 -333
  72. package/src/route-matcher.ts +0 -69
  73. package/src/route-matching.test.ts +0 -394
  74. package/src/server.test.ts +0 -165
  75. package/src/smart-defaults.test.ts +0 -361
  76. package/src/steps/async-support.steps.ts +0 -400
  77. package/src/steps/audit-core-builder.steps.ts +0 -188
  78. package/src/steps/audit-onerror-tuple.steps.ts +0 -99
  79. package/src/steps/basic-usage.steps.ts +0 -245
  80. package/src/steps/developer-experience.steps.ts +0 -204
  81. package/src/steps/error-handling.steps.ts +0 -345
  82. package/src/steps/fluent-api.steps.ts +0 -255
  83. package/src/steps/http-methods.steps.ts +0 -331
  84. package/src/steps/interceptor.steps.ts +0 -260
  85. package/src/steps/lifecycle-events.steps.ts +0 -142
  86. package/src/steps/performance-reliability.steps.ts +0 -423
  87. package/src/steps/plugin-integration.steps.ts +0 -280
  88. package/src/steps/request-history.steps.ts +0 -276
  89. package/src/steps/response-delay.steps.ts +0 -88
  90. package/src/steps/route-key-format.steps.ts +0 -99
  91. package/src/steps/standalone-server.steps.ts +0 -233
  92. package/src/steps/state-concurrency.steps.ts +0 -739
  93. package/src/steps/stateful-workflows.steps.ts +0 -353
  94. package/src/types.ts +0 -36
@@ -1,99 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- HTTP_METHODS,
4
- isHttpMethod,
5
- isRouteNotFound,
6
- ROUTE_NOT_FOUND_CODE,
7
- toHttpMethod,
8
- } from "./constants";
9
-
10
- describe("constants", () => {
11
- it("exports ROUTE_NOT_FOUND_CODE", () => {
12
- expect(ROUTE_NOT_FOUND_CODE).toBe("ROUTE_NOT_FOUND");
13
- });
14
-
15
- it("exports all HTTP methods", () => {
16
- expect(HTTP_METHODS).toEqual([
17
- "GET",
18
- "POST",
19
- "PUT",
20
- "DELETE",
21
- "PATCH",
22
- "HEAD",
23
- "OPTIONS",
24
- ]);
25
- });
26
- });
27
-
28
- describe("isHttpMethod", () => {
29
- it("returns true for valid HTTP methods", () => {
30
- for (const method of HTTP_METHODS) {
31
- expect(isHttpMethod(method)).toBe(true);
32
- }
33
- });
34
-
35
- it("returns false for invalid methods", () => {
36
- expect(isHttpMethod("INVALID")).toBe(false);
37
- expect(isHttpMethod("")).toBe(false);
38
- expect(isHttpMethod("get")).toBe(false);
39
- });
40
- });
41
-
42
- describe("toHttpMethod", () => {
43
- it("converts lowercase to uppercase", () => {
44
- expect(toHttpMethod("get")).toBe("GET");
45
- expect(toHttpMethod("post")).toBe("POST");
46
- expect(toHttpMethod("delete")).toBe("DELETE");
47
- });
48
-
49
- it("returns already uppercase methods", () => {
50
- expect(toHttpMethod("GET")).toBe("GET");
51
- expect(toHttpMethod("PATCH")).toBe("PATCH");
52
- });
53
-
54
- it("throws for invalid methods", () => {
55
- expect(() => toHttpMethod("INVALID")).toThrow(
56
- 'Invalid HTTP method: "INVALID"',
57
- );
58
- expect(() => toHttpMethod("")).toThrow('Invalid HTTP method: ""');
59
- });
60
- });
61
-
62
- describe("isRouteNotFound", () => {
63
- it("returns true for a route-not-found response", () => {
64
- const response = {
65
- status: 404,
66
- body: { error: "Route not found", code: ROUTE_NOT_FOUND_CODE },
67
- headers: {},
68
- };
69
- expect(isRouteNotFound(response)).toBe(true);
70
- });
71
-
72
- it("returns false for a regular 404 response", () => {
73
- const response = {
74
- status: 404,
75
- body: { message: "User not found" },
76
- headers: {},
77
- };
78
- expect(isRouteNotFound(response)).toBe(false);
79
- });
80
-
81
- it("returns false for a non-404 response", () => {
82
- const response = {
83
- status: 200,
84
- body: { code: ROUTE_NOT_FOUND_CODE },
85
- headers: {},
86
- };
87
- expect(isRouteNotFound(response)).toBe(false);
88
- });
89
-
90
- it("returns false when body is null", () => {
91
- const response = { status: 404, body: null, headers: {} };
92
- expect(isRouteNotFound(response)).toBe(false);
93
- });
94
-
95
- it("returns false when body is a string", () => {
96
- const response = { status: 404, body: "not found", headers: {} };
97
- expect(isRouteNotFound(response)).toBe(false);
98
- });
99
- });
package/src/constants.ts DELETED
@@ -1,73 +0,0 @@
1
- import type { HttpMethod } from "./types.js";
2
-
3
- export const ROUTE_NOT_FOUND_CODE = "ROUTE_NOT_FOUND" as const;
4
-
5
- export const HTTP_METHODS: readonly HttpMethod[] = [
6
- "GET",
7
- "POST",
8
- "PUT",
9
- "DELETE",
10
- "PATCH",
11
- "HEAD",
12
- "OPTIONS",
13
- ] as const;
14
-
15
- export function isHttpMethod(method: string): method is HttpMethod {
16
- return (HTTP_METHODS as readonly string[]).includes(method);
17
- }
18
-
19
- export function toHttpMethod(method: string): HttpMethod {
20
- const upper = method.toUpperCase();
21
- if (!isHttpMethod(upper)) {
22
- throw new Error(`Invalid HTTP method: "${method}"`);
23
- }
24
- return upper;
25
- }
26
-
27
- export function normalizePath(path: string): string {
28
- return path.endsWith("/") && path !== "/" ? path.slice(0, -1) : path;
29
- }
30
-
31
- export function toRouteKey(method: HttpMethod, path: string): Schmock.RouteKey {
32
- const key: `${HttpMethod} ${string}` = `${method} ${path}`;
33
- return key;
34
- }
35
-
36
- /**
37
- * Check if a Schmock response is a route-not-found response.
38
- * Used by adapters to decide whether to pass through to the real backend.
39
- */
40
- export function isRouteNotFound(response: {
41
- status: number;
42
- body: unknown;
43
- }): boolean {
44
- const { status, body } = response;
45
- return (
46
- status === 404 &&
47
- body !== null &&
48
- typeof body === "object" &&
49
- "code" in body &&
50
- body.code === ROUTE_NOT_FOUND_CODE
51
- );
52
- }
53
-
54
- /**
55
- * Check if a value is a status tuple: [status, body] or [status, body, headers]
56
- * Guards against misinterpreting numeric arrays like [1, 2, 3] as tuples.
57
- *
58
- * Known ambiguity: a length-2 numeric array whose first element happens to
59
- * be in the HTTP-status range (e.g. [200, 300] as legitimate data) is
60
- * indistinguishable from a status tuple by shape alone. Prefer the explicit
61
- * status() helper or return an object response when the data could collide.
62
- */
63
- export function isStatusTuple(
64
- value: unknown,
65
- ): value is [number, unknown] | [number, unknown, Record<string, string>] {
66
- return (
67
- Array.isArray(value) &&
68
- (value.length === 2 || value.length === 3) &&
69
- typeof value[0] === "number" &&
70
- value[0] >= 100 &&
71
- value[0] <= 599
72
- );
73
- }
package/src/debug.test.ts DELETED
@@ -1,241 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
- import { schmock } from "./index";
3
-
4
- describe("debug functionality", () => {
5
- const originalConsole = console.log;
6
- const originalTime = console.time;
7
- const originalTimeEnd = console.timeEnd;
8
- let consoleLogs: any[] = [];
9
-
10
- beforeEach(() => {
11
- consoleLogs = [];
12
- console.log = vi.fn((...args) => consoleLogs.push(args));
13
- console.time = vi.fn();
14
- console.timeEnd = vi.fn();
15
- });
16
-
17
- afterEach(() => {
18
- console.log = originalConsole;
19
- console.time = originalTime;
20
- console.timeEnd = originalTimeEnd;
21
- });
22
-
23
- describe("debug logging", () => {
24
- it("logs debug messages when debug mode is enabled", async () => {
25
- const mock = schmock({ debug: true });
26
- mock("GET /test", () => ({ data: "test" }));
27
-
28
- await mock.handle("GET", "/test");
29
-
30
- const logMessages = consoleLogs.map((args) => args.join(" "));
31
- expect(
32
- logMessages.some((msg) => msg.includes("Debug mode enabled")),
33
- ).toBe(true);
34
- expect(logMessages.some((msg) => msg.includes("[SCHMOCK:REQUEST]"))).toBe(
35
- true,
36
- );
37
- });
38
-
39
- it("does not log when debug mode is disabled", async () => {
40
- const mock = schmock({ debug: false });
41
- mock("GET /test", () => ({ data: "test" }));
42
-
43
- await mock.handle("GET", "/test");
44
-
45
- expect(consoleLogs).toHaveLength(0);
46
- });
47
-
48
- it("defaults to no logging when debug is not configured", async () => {
49
- const mock = schmock();
50
- mock("GET /test", () => ({ data: "test" }));
51
-
52
- await mock.handle("GET", "/test");
53
-
54
- expect(consoleLogs).toHaveLength(0);
55
- });
56
-
57
- it("logs plugin registration with debug enabled", () => {
58
- const plugin = {
59
- name: "test-plugin",
60
- version: "1.0.0",
61
- enforce: "pre" as const,
62
- beforeRequest: () => {},
63
- generate: () => {},
64
- };
65
-
66
- const mock = schmock({ debug: true });
67
- mock.pipe(plugin);
68
-
69
- const logMessages = consoleLogs.map((args) => args.join(" "));
70
- expect(
71
- logMessages.some((msg) =>
72
- msg.includes("Registered plugin: test-plugin@1.0.0"),
73
- ),
74
- ).toBe(true);
75
- });
76
-
77
- it("logs plugin execution during request handling", async () => {
78
- const plugin = {
79
- name: "logging-test",
80
- version: "1.0.0",
81
- process: (ctx: any, response: any) => ({
82
- context: ctx,
83
- response: response || { data: "generated" },
84
- }),
85
- };
86
-
87
- const mock = schmock({ debug: true });
88
- mock.pipe(plugin);
89
- mock("GET /test", undefined);
90
-
91
- await mock.handle("GET", "/test");
92
-
93
- const logMessages = consoleLogs.map((args) => args.join(" "));
94
- expect(
95
- logMessages.some((msg) =>
96
- msg.includes("Processing plugin: logging-test"),
97
- ),
98
- ).toBe(true);
99
- expect(
100
- logMessages.some((msg) =>
101
- msg.includes("Plugin logging-test generated response"),
102
- ),
103
- ).toBe(true);
104
- });
105
-
106
- it("logs response status and timing", async () => {
107
- const mock = schmock({ debug: true });
108
- mock("GET /test", () => [201, { created: true }]);
109
-
110
- await mock.handle("GET", "/test");
111
-
112
- const logMessages = consoleLogs.map((args) => args.join(" "));
113
- expect(
114
- logMessages.some((msg) => msg.includes("Sending response 201")),
115
- ).toBe(true);
116
-
117
- expect(console.time).toHaveBeenCalledWith(
118
- expect.stringMatching(/\[SCHMOCK\] request-/),
119
- );
120
- expect(console.timeEnd).toHaveBeenCalledWith(
121
- expect.stringMatching(/\[SCHMOCK\] request-/),
122
- );
123
- });
124
-
125
- it("logs route matching information", async () => {
126
- const mock = schmock({ debug: true });
127
- mock("GET /users/:id", ({ params }) => ({ userId: params.id }));
128
-
129
- await mock.handle("GET", "/users/123");
130
-
131
- const logMessages = consoleLogs.map((args) => args.join(" "));
132
- expect(
133
- logMessages.some(
134
- (msg) =>
135
- msg.includes("Matched route:") || msg.includes("GET /users/123"),
136
- ),
137
- ).toBe(true);
138
- });
139
-
140
- it("logs 404 errors with debug enabled", async () => {
141
- const mock = schmock({ debug: true });
142
- mock("GET /exists", () => "OK");
143
-
144
- await mock.handle("GET", "/missing");
145
-
146
- const logMessages = consoleLogs.map((args) => args.join(" "));
147
- expect(
148
- logMessages.some((msg) =>
149
- msg.includes("No route found for GET /missing"),
150
- ),
151
- ).toBe(true);
152
- });
153
-
154
- it("logs error details when exceptions occur", async () => {
155
- const mock = schmock({ debug: true });
156
- mock("GET /error", () => {
157
- throw new Error("Test error");
158
- });
159
-
160
- await mock.handle("GET", "/error");
161
-
162
- const logMessages = consoleLogs.map((args) => args.join(" "));
163
- expect(
164
- logMessages.some((msg) =>
165
- msg.includes("Error processing request: Test error"),
166
- ),
167
- ).toBe(true);
168
- });
169
-
170
- it("includes request details in debug logs", async () => {
171
- const mock = schmock({ debug: true });
172
- mock("POST /data", ({ body }) => body);
173
-
174
- await mock.handle("POST", "/data", {
175
- headers: { "content-type": "application/json" },
176
- body: { test: "data" },
177
- query: { filter: "active" },
178
- });
179
-
180
- const logMessages = consoleLogs.map((args) => args.join(" "));
181
- const requestLog = logMessages.find((msg) => msg.includes("POST /data"));
182
- expect(requestLog).toBeDefined();
183
- });
184
- });
185
-
186
- describe("performance timing", () => {
187
- it("times the overall request processing", async () => {
188
- const mock = schmock({ debug: true });
189
- mock("GET /test", () => "OK");
190
-
191
- await mock.handle("GET", "/test");
192
-
193
- expect(console.time).toHaveBeenCalledWith(
194
- expect.stringMatching(/\[SCHMOCK\] request-/),
195
- );
196
- expect(console.timeEnd).toHaveBeenCalledWith(
197
- expect.stringMatching(/\[SCHMOCK\] request-/),
198
- );
199
- });
200
-
201
- it("times the request processing", async () => {
202
- const mock = schmock({ debug: true });
203
- mock("GET /test", () => "OK");
204
-
205
- await mock.handle("GET", "/test");
206
-
207
- expect(console.time).toHaveBeenCalledWith(
208
- expect.stringMatching(/\[SCHMOCK\] request-/),
209
- );
210
- expect(console.timeEnd).toHaveBeenCalledWith(
211
- expect.stringMatching(/\[SCHMOCK\] request-/),
212
- );
213
- });
214
- });
215
-
216
- describe("debug configuration inheritance", () => {
217
- it("preserves debug setting through chained calls", () => {
218
- // Create mock with debug enabled, then configure namespace
219
- const mock = schmock({ debug: true, namespace: "/api" });
220
- mock("GET /test", () => "OK");
221
-
222
- expect(
223
- consoleLogs.some((args) =>
224
- args.join(" ").includes("Debug mode enabled"),
225
- ),
226
- ).toBe(true);
227
- });
228
-
229
- it("can disable debug mode with subsequent config", async () => {
230
- const mock = schmock({ debug: false });
231
- mock("GET /test", () => "OK");
232
-
233
- // Clear logs from build phase
234
- consoleLogs = [];
235
-
236
- await mock.handle("GET", "/test");
237
-
238
- expect(consoleLogs).toHaveLength(0);
239
- });
240
- });
241
- });