mock-mcp 0.3.1 → 0.5.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 (40) hide show
  1. package/README.md +212 -124
  2. package/dist/adapter/index.cjs +875 -0
  3. package/dist/adapter/index.d.cts +142 -0
  4. package/dist/adapter/index.d.ts +142 -0
  5. package/dist/adapter/index.js +835 -0
  6. package/dist/client/connect.cjs +991 -0
  7. package/dist/client/connect.d.cts +218 -0
  8. package/dist/client/connect.d.ts +211 -7
  9. package/dist/client/connect.js +941 -20
  10. package/dist/client/index.cjs +992 -0
  11. package/dist/client/index.d.cts +3 -0
  12. package/dist/client/index.d.ts +3 -2
  13. package/dist/client/index.js +951 -2
  14. package/dist/daemon/index.cjs +717 -0
  15. package/dist/daemon/index.d.cts +62 -0
  16. package/dist/daemon/index.d.ts +62 -0
  17. package/dist/daemon/index.js +678 -0
  18. package/dist/index.cjs +2708 -0
  19. package/dist/index.d.cts +602 -0
  20. package/dist/index.d.ts +602 -11
  21. package/dist/index.js +2651 -53
  22. package/dist/shared/index.cjs +506 -0
  23. package/dist/shared/index.d.cts +241 -0
  24. package/dist/shared/index.d.ts +241 -0
  25. package/dist/shared/index.js +423 -0
  26. package/dist/types-bEGXLBF0.d.cts +190 -0
  27. package/dist/types-bEGXLBF0.d.ts +190 -0
  28. package/package.json +45 -4
  29. package/dist/client/batch-mock-collector.d.ts +0 -111
  30. package/dist/client/batch-mock-collector.js +0 -308
  31. package/dist/client/util.d.ts +0 -1
  32. package/dist/client/util.js +0 -3
  33. package/dist/connect.cjs +0 -400
  34. package/dist/connect.d.cts +0 -82
  35. package/dist/server/index.d.ts +0 -1
  36. package/dist/server/index.js +0 -1
  37. package/dist/server/test-mock-mcp-server.d.ts +0 -73
  38. package/dist/server/test-mock-mcp-server.js +0 -419
  39. package/dist/types.d.ts +0 -45
  40. package/dist/types.js +0 -2
@@ -0,0 +1,218 @@
1
+ import { R as ResolvedMock, D as DaemonRegistry } from '../types-bEGXLBF0.cjs';
2
+ import 'node:fs';
3
+
4
+ /**
5
+ * BatchMockCollector - Collects HTTP requests and sends them to the daemon for mock generation.
6
+ *
7
+ * Features:
8
+ * - Connects to daemon via IPC (Unix Domain Socket / Named Pipe)
9
+ * - Automatically discovers and starts daemon if needed
10
+ * - Uses runId for multi-run isolation
11
+ * - Sends HELLO handshake on connection
12
+ * - Batches concurrent requests for efficient processing
13
+ */
14
+
15
+ type Logger = Pick<Console, "log" | "warn" | "error"> & {
16
+ debug?: (...args: unknown[]) => void;
17
+ };
18
+ interface BatchMockCollectorOptions {
19
+ /**
20
+ * Timeout for individual mock requests in milliseconds.
21
+ * @default 60000
22
+ */
23
+ timeout?: number;
24
+ /**
25
+ * Delay (in milliseconds) before flushing the current batch.
26
+ * Setting to 0 flushes on the next macrotask.
27
+ * @default 0
28
+ */
29
+ batchDebounceMs?: number;
30
+ /**
31
+ * Maximum number of requests that may be included in a single batch.
32
+ * @default 50
33
+ */
34
+ maxBatchSize?: number;
35
+ /**
36
+ * Optional custom logger. Defaults to console.
37
+ */
38
+ logger?: Logger;
39
+ /**
40
+ * Interval for WebSocket heartbeats in milliseconds. Set to 0 to disable.
41
+ * @default 15000
42
+ */
43
+ heartbeatIntervalMs?: number;
44
+ /**
45
+ * Automatically attempt to reconnect when the WebSocket closes unexpectedly.
46
+ * @default true
47
+ */
48
+ enableReconnect?: boolean;
49
+ /**
50
+ * Optional project root override. By default, auto-detected from process.cwd().
51
+ * Use this when you want to explicitly specify the project root directory.
52
+ */
53
+ projectRoot?: string;
54
+ /**
55
+ * Path to the current test file. The project root will be auto-detected
56
+ * by searching upward for .git or package.json from this file's directory.
57
+ *
58
+ * This is useful in test frameworks like Jest/Vitest:
59
+ * @example
60
+ * ```typescript
61
+ * // Jest
62
+ * const mockMcpClient = await connect({
63
+ * filePath: expect.getState().testPath,
64
+ * });
65
+ *
66
+ * // Vitest
67
+ * const mockMcpClient = await connect({
68
+ * filePath: import.meta.url,
69
+ * });
70
+ * ```
71
+ *
72
+ * Note: If both `filePath` and `projectRoot` are provided, `projectRoot` takes precedence.
73
+ */
74
+ filePath?: string;
75
+ /**
76
+ * Optional test metadata to include in the HELLO message.
77
+ */
78
+ testMeta?: {
79
+ testFile?: string;
80
+ testName?: string;
81
+ };
82
+ }
83
+ interface RequestMockOptions {
84
+ body?: unknown;
85
+ headers?: Record<string, string>;
86
+ metadata?: Record<string, unknown>;
87
+ }
88
+ /**
89
+ * Collects HTTP requests and forwards them to the daemon for AI-assisted mock generation.
90
+ *
91
+ * New architecture:
92
+ * - Connects to daemon via IPC (not TCP port)
93
+ * - Sends HELLO handshake with runId
94
+ * - Uses the new protocol (BATCH_MOCK_REQUEST → BATCH_MOCK_RESULT)
95
+ */
96
+ declare class BatchMockCollector {
97
+ private ws?;
98
+ private registry?;
99
+ private readonly runId;
100
+ private readonly pendingRequests;
101
+ private readonly queuedRequestIds;
102
+ private readonly timeout;
103
+ private readonly batchDebounceMs;
104
+ private readonly maxBatchSize;
105
+ private readonly logger;
106
+ private readonly heartbeatIntervalMs;
107
+ private readonly enableReconnect;
108
+ private readonly projectRoot?;
109
+ private readonly testMeta?;
110
+ private batchTimer;
111
+ private heartbeatTimer;
112
+ private reconnectTimer;
113
+ private requestIdCounter;
114
+ private closed;
115
+ private authed;
116
+ private readyResolve?;
117
+ private readyReject?;
118
+ private readyPromise;
119
+ constructor(options?: BatchMockCollectorOptions);
120
+ /**
121
+ * Resolve projectRoot from options.
122
+ * Priority: projectRoot (if valid) > filePath > projectRoot (fallback) > undefined (auto-detect)
123
+ *
124
+ * A projectRoot is "valid" if it contains .git or package.json. This prevents
125
+ * accidentally using a wrong directory (e.g., user's home directory) when the
126
+ * caller mistakenly passes process.cwd() as projectRoot.
127
+ */
128
+ private resolveProjectRootFromOptions;
129
+ /**
130
+ * Check if a directory contains .git or package.json
131
+ */
132
+ private hasGitOrPackageJson;
133
+ /**
134
+ * Ensures the underlying connection is ready for use.
135
+ */
136
+ waitUntilReady(): Promise<void>;
137
+ /**
138
+ * Request mock data for a specific endpoint/method pair.
139
+ */
140
+ requestMock<T = unknown>(endpoint: string, method: string, options?: RequestMockOptions): Promise<ResolvedMock<T>>;
141
+ /**
142
+ * Wait for all currently pending requests to settle.
143
+ */
144
+ waitForPendingRequests(): Promise<void>;
145
+ /**
146
+ * Close the connection and fail all pending requests.
147
+ */
148
+ close(code?: number): Promise<void>;
149
+ private initConnection;
150
+ private createWebSocket;
151
+ private setupWebSocket;
152
+ private sendHello;
153
+ private handleMessage;
154
+ private isHelloAck;
155
+ private isBatchMockResult;
156
+ private resolveRequest;
157
+ private rejectRequest;
158
+ private failAllPending;
159
+ private enqueueRequest;
160
+ private flushQueue;
161
+ private sendBatch;
162
+ private startHeartbeat;
163
+ private stopHeartbeat;
164
+ private scheduleReconnect;
165
+ private resetReadyPromise;
166
+ private buildResolvedMock;
167
+ /**
168
+ * Get the run ID for this collector instance.
169
+ */
170
+ getRunId(): string;
171
+ /**
172
+ * Get the daemon registry information (after connection).
173
+ */
174
+ getRegistry(): DaemonRegistry | undefined;
175
+ }
176
+
177
+ /**
178
+ * Options for connecting to the mock-mcp daemon.
179
+ */
180
+ type ConnectOptions = BatchMockCollectorOptions | undefined;
181
+ /**
182
+ * Interface for the mock client returned by connect().
183
+ */
184
+ interface MockClient {
185
+ waitUntilReady(): Promise<void>;
186
+ requestMock<T = unknown>(endpoint: string, method: string, options?: RequestMockOptions): Promise<ResolvedMock<T>>;
187
+ waitForPendingRequests(): Promise<void>;
188
+ close(code?: number): Promise<void>;
189
+ /** Get the unique run ID for this connection */
190
+ getRunId(): string;
191
+ }
192
+ /**
193
+ * Connect to the mock-mcp daemon for AI-assisted mock generation.
194
+ *
195
+ * This function:
196
+ * 1. Automatically discovers or starts the daemon for your project
197
+ * 2. Establishes a WebSocket connection via IPC
198
+ * 3. Returns a MockClient for requesting mocks
199
+ *
200
+ * The daemon uses Unix Domain Sockets (macOS/Linux) or Named Pipes (Windows)
201
+ * instead of TCP ports, eliminating port conflicts when multiple MCP clients
202
+ * are running simultaneously.
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * import { connect } from 'mock-mcp';
207
+ *
208
+ * const client = await connect();
209
+ *
210
+ * const mock = await client.requestMock<User>('/api/users/1', 'GET');
211
+ * console.log(mock.data); // AI-generated mock data
212
+ *
213
+ * await client.close();
214
+ * ```
215
+ */
216
+ declare const connect: (options?: ConnectOptions) => Promise<MockClient>;
217
+
218
+ export { BatchMockCollector as B, type ConnectOptions, type MockClient, type RequestMockOptions as R, type BatchMockCollectorOptions as a, connect };
@@ -1,14 +1,218 @@
1
- import type { BatchMockCollectorOptions, RequestMockOptions } from "./batch-mock-collector.js";
2
- import type { ResolvedMock } from "../types.js";
3
- export type ConnectOptions = number | BatchMockCollectorOptions | undefined;
4
- export interface MockClient {
1
+ import { R as ResolvedMock, D as DaemonRegistry } from '../types-bEGXLBF0.js';
2
+ import 'node:fs';
3
+
4
+ /**
5
+ * BatchMockCollector - Collects HTTP requests and sends them to the daemon for mock generation.
6
+ *
7
+ * Features:
8
+ * - Connects to daemon via IPC (Unix Domain Socket / Named Pipe)
9
+ * - Automatically discovers and starts daemon if needed
10
+ * - Uses runId for multi-run isolation
11
+ * - Sends HELLO handshake on connection
12
+ * - Batches concurrent requests for efficient processing
13
+ */
14
+
15
+ type Logger = Pick<Console, "log" | "warn" | "error"> & {
16
+ debug?: (...args: unknown[]) => void;
17
+ };
18
+ interface BatchMockCollectorOptions {
19
+ /**
20
+ * Timeout for individual mock requests in milliseconds.
21
+ * @default 60000
22
+ */
23
+ timeout?: number;
24
+ /**
25
+ * Delay (in milliseconds) before flushing the current batch.
26
+ * Setting to 0 flushes on the next macrotask.
27
+ * @default 0
28
+ */
29
+ batchDebounceMs?: number;
30
+ /**
31
+ * Maximum number of requests that may be included in a single batch.
32
+ * @default 50
33
+ */
34
+ maxBatchSize?: number;
35
+ /**
36
+ * Optional custom logger. Defaults to console.
37
+ */
38
+ logger?: Logger;
39
+ /**
40
+ * Interval for WebSocket heartbeats in milliseconds. Set to 0 to disable.
41
+ * @default 15000
42
+ */
43
+ heartbeatIntervalMs?: number;
44
+ /**
45
+ * Automatically attempt to reconnect when the WebSocket closes unexpectedly.
46
+ * @default true
47
+ */
48
+ enableReconnect?: boolean;
49
+ /**
50
+ * Optional project root override. By default, auto-detected from process.cwd().
51
+ * Use this when you want to explicitly specify the project root directory.
52
+ */
53
+ projectRoot?: string;
54
+ /**
55
+ * Path to the current test file. The project root will be auto-detected
56
+ * by searching upward for .git or package.json from this file's directory.
57
+ *
58
+ * This is useful in test frameworks like Jest/Vitest:
59
+ * @example
60
+ * ```typescript
61
+ * // Jest
62
+ * const mockMcpClient = await connect({
63
+ * filePath: expect.getState().testPath,
64
+ * });
65
+ *
66
+ * // Vitest
67
+ * const mockMcpClient = await connect({
68
+ * filePath: import.meta.url,
69
+ * });
70
+ * ```
71
+ *
72
+ * Note: If both `filePath` and `projectRoot` are provided, `projectRoot` takes precedence.
73
+ */
74
+ filePath?: string;
75
+ /**
76
+ * Optional test metadata to include in the HELLO message.
77
+ */
78
+ testMeta?: {
79
+ testFile?: string;
80
+ testName?: string;
81
+ };
82
+ }
83
+ interface RequestMockOptions {
84
+ body?: unknown;
85
+ headers?: Record<string, string>;
86
+ metadata?: Record<string, unknown>;
87
+ }
88
+ /**
89
+ * Collects HTTP requests and forwards them to the daemon for AI-assisted mock generation.
90
+ *
91
+ * New architecture:
92
+ * - Connects to daemon via IPC (not TCP port)
93
+ * - Sends HELLO handshake with runId
94
+ * - Uses the new protocol (BATCH_MOCK_REQUEST → BATCH_MOCK_RESULT)
95
+ */
96
+ declare class BatchMockCollector {
97
+ private ws?;
98
+ private registry?;
99
+ private readonly runId;
100
+ private readonly pendingRequests;
101
+ private readonly queuedRequestIds;
102
+ private readonly timeout;
103
+ private readonly batchDebounceMs;
104
+ private readonly maxBatchSize;
105
+ private readonly logger;
106
+ private readonly heartbeatIntervalMs;
107
+ private readonly enableReconnect;
108
+ private readonly projectRoot?;
109
+ private readonly testMeta?;
110
+ private batchTimer;
111
+ private heartbeatTimer;
112
+ private reconnectTimer;
113
+ private requestIdCounter;
114
+ private closed;
115
+ private authed;
116
+ private readyResolve?;
117
+ private readyReject?;
118
+ private readyPromise;
119
+ constructor(options?: BatchMockCollectorOptions);
120
+ /**
121
+ * Resolve projectRoot from options.
122
+ * Priority: projectRoot (if valid) > filePath > projectRoot (fallback) > undefined (auto-detect)
123
+ *
124
+ * A projectRoot is "valid" if it contains .git or package.json. This prevents
125
+ * accidentally using a wrong directory (e.g., user's home directory) when the
126
+ * caller mistakenly passes process.cwd() as projectRoot.
127
+ */
128
+ private resolveProjectRootFromOptions;
129
+ /**
130
+ * Check if a directory contains .git or package.json
131
+ */
132
+ private hasGitOrPackageJson;
133
+ /**
134
+ * Ensures the underlying connection is ready for use.
135
+ */
136
+ waitUntilReady(): Promise<void>;
137
+ /**
138
+ * Request mock data for a specific endpoint/method pair.
139
+ */
140
+ requestMock<T = unknown>(endpoint: string, method: string, options?: RequestMockOptions): Promise<ResolvedMock<T>>;
141
+ /**
142
+ * Wait for all currently pending requests to settle.
143
+ */
144
+ waitForPendingRequests(): Promise<void>;
145
+ /**
146
+ * Close the connection and fail all pending requests.
147
+ */
148
+ close(code?: number): Promise<void>;
149
+ private initConnection;
150
+ private createWebSocket;
151
+ private setupWebSocket;
152
+ private sendHello;
153
+ private handleMessage;
154
+ private isHelloAck;
155
+ private isBatchMockResult;
156
+ private resolveRequest;
157
+ private rejectRequest;
158
+ private failAllPending;
159
+ private enqueueRequest;
160
+ private flushQueue;
161
+ private sendBatch;
162
+ private startHeartbeat;
163
+ private stopHeartbeat;
164
+ private scheduleReconnect;
165
+ private resetReadyPromise;
166
+ private buildResolvedMock;
167
+ /**
168
+ * Get the run ID for this collector instance.
169
+ */
170
+ getRunId(): string;
171
+ /**
172
+ * Get the daemon registry information (after connection).
173
+ */
174
+ getRegistry(): DaemonRegistry | undefined;
175
+ }
176
+
177
+ /**
178
+ * Options for connecting to the mock-mcp daemon.
179
+ */
180
+ type ConnectOptions = BatchMockCollectorOptions | undefined;
181
+ /**
182
+ * Interface for the mock client returned by connect().
183
+ */
184
+ interface MockClient {
5
185
  waitUntilReady(): Promise<void>;
6
186
  requestMock<T = unknown>(endpoint: string, method: string, options?: RequestMockOptions): Promise<ResolvedMock<T>>;
7
187
  waitForPendingRequests(): Promise<void>;
8
188
  close(code?: number): Promise<void>;
189
+ /** Get the unique run ID for this connection */
190
+ getRunId(): string;
9
191
  }
10
192
  /**
11
- * Convenience helper that creates a {@link BatchMockCollector} and waits for the
12
- * underlying WebSocket connection to become ready before resolving.
193
+ * Connect to the mock-mcp daemon for AI-assisted mock generation.
194
+ *
195
+ * This function:
196
+ * 1. Automatically discovers or starts the daemon for your project
197
+ * 2. Establishes a WebSocket connection via IPC
198
+ * 3. Returns a MockClient for requesting mocks
199
+ *
200
+ * The daemon uses Unix Domain Sockets (macOS/Linux) or Named Pipes (Windows)
201
+ * instead of TCP ports, eliminating port conflicts when multiple MCP clients
202
+ * are running simultaneously.
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * import { connect } from 'mock-mcp';
207
+ *
208
+ * const client = await connect();
209
+ *
210
+ * const mock = await client.requestMock<User>('/api/users/1', 'GET');
211
+ * console.log(mock.data); // AI-generated mock data
212
+ *
213
+ * await client.close();
214
+ * ```
13
215
  */
14
- export declare const connect: (options?: ConnectOptions) => Promise<MockClient>;
216
+ declare const connect: (options?: ConnectOptions) => Promise<MockClient>;
217
+
218
+ export { BatchMockCollector as B, type ConnectOptions, type MockClient, type RequestMockOptions as R, type BatchMockCollectorOptions as a, connect };