@movebridge/testing 0.0.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.
package/LICENSE ADDED
File without changes
@@ -0,0 +1,451 @@
1
+ import { MovementError, Resource, Transaction, TransactionResponse, ContractEvent, WalletState, TransactionPayload } from '@movebridge/core';
2
+ import { Struct } from 'superstruct';
3
+
4
+ /**
5
+ * @movebridge/testing - Type definitions
6
+ */
7
+
8
+ /** Configuration for test harness */
9
+ interface TestHarnessConfig {
10
+ /** Seed for deterministic random data generation */
11
+ seed?: number;
12
+ /** Network to simulate */
13
+ network?: 'mainnet' | 'testnet';
14
+ /** Default latency in milliseconds for mock responses */
15
+ defaultLatency?: number;
16
+ }
17
+ /** Test harness instance */
18
+ interface TestHarness {
19
+ /** Mocked Movement client */
20
+ readonly client: MockMovementClient;
21
+ /** Call tracker for assertions */
22
+ readonly tracker: CallTracker;
23
+ /** Network simulator for latency/errors */
24
+ readonly simulator: NetworkSimulator;
25
+ /** Response faker for generating test data */
26
+ readonly faker: ResponseFaker;
27
+ /** Cleanup all state */
28
+ cleanup(): void;
29
+ /** Reset to initial state (keeps configuration) */
30
+ reset(): void;
31
+ }
32
+ /** Mock configuration for a method */
33
+ interface MockConfig<T> {
34
+ response?: T;
35
+ error?: MovementError;
36
+ once?: boolean;
37
+ }
38
+ /** Mock Movement client interface */
39
+ interface MockMovementClient {
40
+ mockResponse<T>(method: string, response: T): void;
41
+ mockError(method: string, error: MovementError): void;
42
+ mockResponseOnce<T>(method: string, response: T): void;
43
+ clearMocks(): void;
44
+ getAccountBalance(address: string): Promise<string>;
45
+ getAccountResources(address: string): Promise<Resource[]>;
46
+ getTransaction(hash: string): Promise<Transaction>;
47
+ waitForTransaction(hash: string): Promise<TransactionResponse>;
48
+ }
49
+ /** Result of address validation */
50
+ interface AddressValidationResult {
51
+ valid: boolean;
52
+ normalized?: string;
53
+ error?: string;
54
+ }
55
+ /** Address validator interface */
56
+ interface AddressValidator {
57
+ isValidAddress(address: string): boolean;
58
+ validateAddress(address: string): void;
59
+ normalizeAddress(address: string): string;
60
+ getValidationDetails(address: string): AddressValidationResult;
61
+ }
62
+ /** Transaction validator interface */
63
+ interface TransactionValidator {
64
+ validateTransferPayload(payload: {
65
+ to: string;
66
+ amount: string;
67
+ coinType?: string;
68
+ }): boolean;
69
+ validateEntryFunctionPayload(payload: {
70
+ function: string;
71
+ typeArguments: string[];
72
+ arguments: unknown[];
73
+ }): boolean;
74
+ validatePayload(payload: TransactionPayload): boolean;
75
+ }
76
+ /** Validation error details */
77
+ interface ValidationError {
78
+ path: string;
79
+ expected: string;
80
+ received: string;
81
+ message: string;
82
+ }
83
+ /** Schema names for validation */
84
+ type SchemaName = 'Resource' | 'Transaction' | 'TransactionResponse' | 'WalletState' | 'ContractEvent' | string;
85
+ /** Schema validator interface */
86
+ interface SchemaValidator {
87
+ validateSchema(data: unknown, schemaName: SchemaName): boolean;
88
+ getValidationErrors(data: unknown, schemaName: SchemaName): ValidationError[];
89
+ registerSchema(name: string, schema: unknown): void;
90
+ hasSchema(name: string): boolean;
91
+ }
92
+ /** Options for creating a faker */
93
+ interface FakerOptions {
94
+ seed?: number;
95
+ }
96
+ /** Options for generating fake balance */
97
+ interface BalanceOptions {
98
+ min?: string;
99
+ max?: string;
100
+ }
101
+ /** Response faker interface */
102
+ interface ResponseFaker {
103
+ fakeAddress(): string;
104
+ fakeBalance(options?: BalanceOptions): string;
105
+ fakeTransaction(): Transaction;
106
+ fakeTransactionResponse(success?: boolean): TransactionResponse;
107
+ fakeResource(type: string): Resource;
108
+ fakeEvent(type: string): ContractEvent;
109
+ fakeWalletState(connected?: boolean): WalletState;
110
+ fakeTransactionHash(): string;
111
+ }
112
+ /** Network simulator interface */
113
+ interface NetworkSimulator {
114
+ simulateLatency(ms: number): void;
115
+ simulateTimeout(method: string): void;
116
+ simulateNetworkError(): void;
117
+ simulateRateLimit(maxCalls: number): void;
118
+ resetSimulation(): void;
119
+ getLatency(): number;
120
+ isNetworkErrorEnabled(): boolean;
121
+ isMethodTimedOut(method: string): boolean;
122
+ getRateLimitRemaining(): number | null;
123
+ incrementRateLimitCalls(): void;
124
+ }
125
+ /** Record of a method call */
126
+ interface CallRecord {
127
+ method: string;
128
+ arguments: unknown[];
129
+ timestamp: number;
130
+ result: unknown | undefined;
131
+ error: Error | undefined;
132
+ }
133
+ /** Call tracker interface */
134
+ interface CallTracker {
135
+ recordCall(method: string, args: unknown[], result?: unknown, error?: Error): void;
136
+ getCalls(method: string): CallRecord[];
137
+ getCallCount(method: string): number;
138
+ getAllCalls(): CallRecord[];
139
+ assertCalled(method: string): void;
140
+ assertCalledTimes(method: string, times: number): void;
141
+ assertCalledWith(method: string, ...args: unknown[]): void;
142
+ assertNotCalled(method: string): void;
143
+ clearCalls(): void;
144
+ }
145
+ /** Result of snapshot comparison */
146
+ interface SnapshotResult {
147
+ match: boolean;
148
+ diff?: string;
149
+ }
150
+ /** Snapshot utilities interface */
151
+ interface SnapshotUtils {
152
+ createSnapshot(data: unknown, name: string): void;
153
+ matchSnapshot(data: unknown, name: string): SnapshotResult;
154
+ updateSnapshot(data: unknown, name: string): void;
155
+ deleteSnapshot(name: string): void;
156
+ listSnapshots(): string[];
157
+ }
158
+ /** Test account for integration testing */
159
+ interface TestAccount {
160
+ address: string;
161
+ privateKey: string;
162
+ publicKey: string;
163
+ }
164
+ /** Integration test utilities interface */
165
+ interface IntegrationUtils {
166
+ createTestAccount(): Promise<TestAccount>;
167
+ waitForFunding(address: string, timeout?: number): Promise<string>;
168
+ cleanupTestAccount(account: TestAccount): Promise<void>;
169
+ withTestAccount<T>(callback: (account: TestAccount) => Promise<T>): Promise<T>;
170
+ }
171
+
172
+ /**
173
+ * @movebridge/testing - Test harness for MoveBridge SDK
174
+ */
175
+
176
+ /**
177
+ * Creates a test harness for testing MoveBridge SDK integrations
178
+ * @param config - Optional configuration
179
+ * @returns TestHarness instance with mocked components
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * const harness = createTestHarness({ seed: 12345 });
184
+ *
185
+ * // Configure mock responses
186
+ * harness.client.mockResponse('getAccountBalance', '1000000000');
187
+ *
188
+ * // Use the mocked client
189
+ * const balance = await harness.client.getAccountBalance('0x1');
190
+ *
191
+ * // Assert on calls
192
+ * harness.tracker.assertCalled('getAccountBalance');
193
+ *
194
+ * // Cleanup
195
+ * harness.cleanup();
196
+ * ```
197
+ */
198
+ declare function createTestHarness(config?: TestHarnessConfig): TestHarness;
199
+
200
+ /**
201
+ * @movebridge/testing - Address validation utilities
202
+ */
203
+
204
+ /**
205
+ * Checks if a string is a valid Movement/Aptos address
206
+ * @param address - The address to validate
207
+ * @returns true if valid, false otherwise
208
+ */
209
+ declare function isValidAddress(address: string): boolean;
210
+ /**
211
+ * Validates an address and throws if invalid
212
+ * @param address - The address to validate
213
+ * @throws MovementError with code 'INVALID_ADDRESS' if invalid
214
+ */
215
+ declare function validateAddress(address: string): void;
216
+ /**
217
+ * Normalizes an address to lowercase with '0x' prefix
218
+ * @param address - The address to normalize
219
+ * @returns Normalized address
220
+ * @throws MovementError if address is invalid
221
+ */
222
+ declare function normalizeAddress(address: string): string;
223
+ /**
224
+ * Gets detailed validation information for an address
225
+ * @param address - The address to validate
226
+ * @returns Validation result with details
227
+ */
228
+ declare function getAddressValidationDetails(address: string): AddressValidationResult;
229
+
230
+ /**
231
+ * @movebridge/testing - Transaction validation utilities
232
+ */
233
+
234
+ /**
235
+ * Validates a transfer payload
236
+ * @param payload - Transfer payload to validate
237
+ * @returns true if valid
238
+ * @throws MovementError if invalid
239
+ */
240
+ declare function validateTransferPayload(payload: {
241
+ to: string;
242
+ amount: string;
243
+ coinType?: string;
244
+ }): boolean;
245
+ /**
246
+ * Validates an entry function payload
247
+ * @param payload - Entry function payload to validate
248
+ * @returns true if valid
249
+ * @throws MovementError if invalid
250
+ */
251
+ declare function validateEntryFunctionPayload(payload: {
252
+ function: string;
253
+ typeArguments: string[];
254
+ arguments: unknown[];
255
+ }): boolean;
256
+ /**
257
+ * Validates a transaction payload (polymorphic)
258
+ * @param payload - Transaction payload to validate
259
+ * @returns true if valid
260
+ * @throws MovementError if invalid
261
+ */
262
+ declare function validatePayload(payload: TransactionPayload): boolean;
263
+
264
+ /**
265
+ * @movebridge/testing - Schema validation utilities
266
+ */
267
+
268
+ /**
269
+ * Predefined schemas for SDK types
270
+ */
271
+ declare const PREDEFINED_SCHEMAS: {
272
+ readonly Resource: Struct<{
273
+ type: string;
274
+ data: Record<string, unknown>;
275
+ }, {
276
+ type: Struct<string, null>;
277
+ data: Struct<Record<string, unknown>, null>;
278
+ }>;
279
+ readonly Transaction: Struct<{
280
+ payload: {
281
+ function: string;
282
+ typeArguments: string[];
283
+ arguments: unknown[];
284
+ type: "entry_function_payload";
285
+ };
286
+ hash: string;
287
+ sender: string;
288
+ sequenceNumber: string;
289
+ timestamp: string;
290
+ }, {
291
+ hash: Struct<string, null>;
292
+ sender: Struct<string, null>;
293
+ sequenceNumber: Struct<string, null>;
294
+ payload: Struct<{
295
+ function: string;
296
+ typeArguments: string[];
297
+ arguments: unknown[];
298
+ type: "entry_function_payload";
299
+ }, {
300
+ type: Struct<"entry_function_payload", "entry_function_payload">;
301
+ function: Struct<string, null>;
302
+ typeArguments: Struct<string[], Struct<string, null>>;
303
+ arguments: Struct<unknown[], Struct<unknown, null>>;
304
+ }>;
305
+ timestamp: Struct<string, null>;
306
+ }>;
307
+ readonly TransactionResponse: Struct<{
308
+ hash: string;
309
+ success: boolean;
310
+ vmStatus: string;
311
+ gasUsed: string;
312
+ events: {
313
+ type: string;
314
+ data: Record<string, unknown>;
315
+ sequenceNumber: string;
316
+ }[];
317
+ }, {
318
+ hash: Struct<string, null>;
319
+ success: Struct<boolean, null>;
320
+ vmStatus: Struct<string, null>;
321
+ gasUsed: Struct<string, null>;
322
+ events: Struct<{
323
+ type: string;
324
+ data: Record<string, unknown>;
325
+ sequenceNumber: string;
326
+ }[], Struct<{
327
+ type: string;
328
+ data: Record<string, unknown>;
329
+ sequenceNumber: string;
330
+ }, {
331
+ type: Struct<string, null>;
332
+ sequenceNumber: Struct<string, null>;
333
+ data: Struct<Record<string, unknown>, null>;
334
+ }>>;
335
+ }>;
336
+ readonly WalletState: Struct<{
337
+ address: string | null;
338
+ connected: boolean;
339
+ publicKey: string | null;
340
+ }, {
341
+ connected: Struct<boolean, null>;
342
+ address: Struct<string | null, null>;
343
+ publicKey: Struct<string | null, null>;
344
+ }>;
345
+ readonly ContractEvent: Struct<{
346
+ type: string;
347
+ data: Record<string, unknown>;
348
+ sequenceNumber: string;
349
+ }, {
350
+ type: Struct<string, null>;
351
+ sequenceNumber: Struct<string, null>;
352
+ data: Struct<Record<string, unknown>, null>;
353
+ }>;
354
+ };
355
+ /**
356
+ * Validates data against a schema
357
+ * @param data - Data to validate
358
+ * @param schemaName - Name of the schema to validate against
359
+ * @returns true if valid, false otherwise
360
+ */
361
+ declare function validateSchema(data: unknown, schemaName: SchemaName): boolean;
362
+ /**
363
+ * Gets validation errors for data against a schema
364
+ * @param data - Data to validate
365
+ * @param schemaName - Name of the schema to validate against
366
+ * @returns Array of validation errors (empty if valid)
367
+ */
368
+ declare function getValidationErrors(data: unknown, schemaName: SchemaName): ValidationError[];
369
+ /**
370
+ * Registers a custom schema
371
+ * @param name - Name for the schema
372
+ * @param schema - Superstruct schema definition
373
+ */
374
+ declare function registerSchema(name: string, schema: Struct<unknown>): void;
375
+ /**
376
+ * Checks if a schema exists
377
+ * @param name - Schema name to check
378
+ * @returns true if schema exists
379
+ */
380
+ declare function hasSchema(name: string): boolean;
381
+
382
+ /**
383
+ * @movebridge/testing - Response faker for generating test data
384
+ */
385
+
386
+ /**
387
+ * Creates a response faker with optional seeding
388
+ * @param options - Faker options including seed
389
+ * @returns ResponseFaker instance
390
+ */
391
+ declare function createFaker(options?: FakerOptions): ResponseFaker;
392
+
393
+ /**
394
+ * @movebridge/testing - Network simulator for testing latency, errors, and rate limits
395
+ */
396
+
397
+ /**
398
+ * Creates a network simulator instance
399
+ * @returns NetworkSimulator instance
400
+ */
401
+ declare function createNetworkSimulator(): NetworkSimulator;
402
+
403
+ /**
404
+ * @movebridge/testing - Call tracker for recording and asserting method calls
405
+ */
406
+
407
+ /**
408
+ * Creates a call tracker instance
409
+ * @returns CallTracker instance
410
+ */
411
+ declare function createCallTracker(): CallTracker;
412
+
413
+ /**
414
+ * @movebridge/testing - Snapshot testing utilities
415
+ */
416
+
417
+ /**
418
+ * Creates snapshot utilities with in-memory storage
419
+ * For file-based snapshots, use with a test framework's snapshot feature
420
+ * @returns SnapshotUtils instance
421
+ */
422
+ declare function createSnapshotUtils(): SnapshotUtils;
423
+
424
+ /**
425
+ * @movebridge/testing - Integration test utilities
426
+ */
427
+
428
+ /**
429
+ * Creates integration test utilities
430
+ * @param network - Network to use (only 'testnet' allowed)
431
+ * @returns IntegrationUtils instance
432
+ */
433
+ declare function createIntegrationUtils(network: 'testnet'): IntegrationUtils;
434
+
435
+ /**
436
+ * @movebridge/testing - Mock Movement client
437
+ */
438
+
439
+ interface MockClientDependencies {
440
+ tracker: CallTracker;
441
+ simulator: NetworkSimulator;
442
+ faker: ResponseFaker;
443
+ }
444
+ /**
445
+ * Creates a mock Movement client
446
+ * @param deps - Dependencies (tracker, simulator, faker)
447
+ * @returns MockMovementClient instance
448
+ */
449
+ declare function createMockClient(deps: MockClientDependencies): MockMovementClient;
450
+
451
+ export { type AddressValidationResult, type AddressValidator, type BalanceOptions, type CallRecord, type CallTracker, type FakerOptions, type IntegrationUtils, type MockConfig, type MockMovementClient, type NetworkSimulator, PREDEFINED_SCHEMAS, type ResponseFaker, type SchemaName, type SchemaValidator, type SnapshotResult, type SnapshotUtils, type TestAccount, type TestHarness, type TestHarnessConfig, type TransactionValidator, type ValidationError, createCallTracker, createFaker, createIntegrationUtils, createMockClient, createNetworkSimulator, createSnapshotUtils, createTestHarness, getAddressValidationDetails, getValidationErrors, hasSchema, isValidAddress, normalizeAddress, registerSchema, validateAddress, validateEntryFunctionPayload, validatePayload, validateSchema, validateTransferPayload };