@bchconnect/test-utils 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +329 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +408 -0
- package/dist/index.d.ts +408 -0
- package/dist/index.js +323 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -3
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { CapabilityMap, Connector, ConnectorContext, EventName, EventPayload, MethodMetadata, MethodName, MethodParams, MethodResult, Pairing, ProtocolDefinition, RequestOptions, Session, SessionClient, SessionStatus, SignMessageRequest, SignMessageResponse, SignTransactionRequest, SignTransactionResponse, WalletIdentity } from "@bchconnect/core";
|
|
2
|
+
//#region src/fake-connector.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* What a scripted method does when the client dispatches it: a handler, or one
|
|
5
|
+
* of the canned outcomes.
|
|
6
|
+
*
|
|
7
|
+
* `"reject"` rejects `REJECTED` by the user, `"malformed"` rejects
|
|
8
|
+
* `INVALID_WALLET_RESPONSE` the way a connector does when validation of the
|
|
9
|
+
* wallet's reply fails, and `"hang"` never settles on its own — the call shows
|
|
10
|
+
* up in {@link FakeConnectorHandle.pending} for the test to settle, and aborts
|
|
11
|
+
* with `ABORTED` when its signal does.
|
|
12
|
+
*
|
|
13
|
+
* @beta
|
|
14
|
+
*/
|
|
15
|
+
type FakeMethodScript<P extends ProtocolDefinition, M extends MethodName<P>> = ((params: MethodParams<P, M>, opts: RequestOptions | undefined) => Promise<MethodResult<P, M>>) | "reject" | "hang" | "malformed";
|
|
16
|
+
/**
|
|
17
|
+
* Scripted methods, by name. A method left out rejects
|
|
18
|
+
* `METHOD_UNSUPPORTED`, the way a connector reports a method the session does
|
|
19
|
+
* not advertise.
|
|
20
|
+
*
|
|
21
|
+
* @beta
|
|
22
|
+
*/
|
|
23
|
+
type FakeMethodScripts<P extends ProtocolDefinition> = { readonly [M in MethodName<P>]?: FakeMethodScript<P, M>; };
|
|
24
|
+
/**
|
|
25
|
+
* Deadline and user-interaction metadata the connector reports, by method
|
|
26
|
+
* name. Mirrors what a real connector would carry for its protocol.
|
|
27
|
+
*
|
|
28
|
+
* @beta
|
|
29
|
+
*/
|
|
30
|
+
type FakeMethodMetadata<P extends ProtocolDefinition> = NonNullable<Connector<P>["methods"]>;
|
|
31
|
+
/**
|
|
32
|
+
* What a {@link createFakeConnector} connector does, from `setup()` to
|
|
33
|
+
* `dispose()`.
|
|
34
|
+
*
|
|
35
|
+
* @beta
|
|
36
|
+
*/
|
|
37
|
+
interface FakeConnectorScript<P extends ProtocolDefinition> {
|
|
38
|
+
/** Protocol id. Must equal `P["id"]`. */
|
|
39
|
+
readonly protocol: P["id"];
|
|
40
|
+
/**
|
|
41
|
+
* The session `connect()` resolves with. A value is returned as written on
|
|
42
|
+
* every call; a function is called once per `connect()`, so
|
|
43
|
+
* `() => fakeSession(…)` gives a fresh id each time.
|
|
44
|
+
*/
|
|
45
|
+
readonly session?: Session<P> | (() => Session<P>);
|
|
46
|
+
/** Pairing material handed to `onPairing` at the start of `connect()`. */
|
|
47
|
+
readonly pairing?: Pairing;
|
|
48
|
+
/** How `connect()` behaves. Resolves with the scripted session by default. */
|
|
49
|
+
readonly connect?: "reject" | "hang" | ((opts: {
|
|
50
|
+
signal?: AbortSignal;
|
|
51
|
+
onPairing?: (pairing: Pairing) => void;
|
|
52
|
+
timeoutMs?: number;
|
|
53
|
+
}) => Promise<Session<P>>);
|
|
54
|
+
/** What `restore()` returns, or `"throw"` to fail it. Defaults to `[]`. */
|
|
55
|
+
readonly restore?: readonly Session<P>[] | (() => Promise<readonly Session<P>[]>) | "throw";
|
|
56
|
+
/** `"throw"` makes `setup()` fail, to exercise connector isolation. */
|
|
57
|
+
readonly setup?: "throw";
|
|
58
|
+
/** `"throw"` makes `disconnect()` fail, after its local cleanup ran. */
|
|
59
|
+
readonly disconnect?: "throw";
|
|
60
|
+
/** `"throw"` makes `dispose()` fail. */
|
|
61
|
+
readonly dispose?: "throw";
|
|
62
|
+
/** Scripted methods. */
|
|
63
|
+
readonly methods?: FakeMethodScripts<P>;
|
|
64
|
+
/** Metadata the connector reports for its methods. Defaults to none. */
|
|
65
|
+
readonly metadata?: FakeMethodMetadata<P>;
|
|
66
|
+
/** Capabilities of every session, or of one session. Defaults to none. */
|
|
67
|
+
readonly capabilities?: readonly P["capability"][] | ((session: Session<P>) => readonly P["capability"][]);
|
|
68
|
+
/**
|
|
69
|
+
* Builds the capability interfaces of one session, over the client bound
|
|
70
|
+
* to it. Without it the connector supplies none.
|
|
71
|
+
*/
|
|
72
|
+
readonly interfaces?: (session: Session<P>, client: SessionClient<P>) => CapabilityMap<P>;
|
|
73
|
+
/**
|
|
74
|
+
* Whether `connect()` writes a marker to its storage namespace and
|
|
75
|
+
* `disconnect()` removes it. On by default; `setup()` never writes.
|
|
76
|
+
*/
|
|
77
|
+
readonly persist?: boolean;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* One recorded `request()` call.
|
|
81
|
+
*
|
|
82
|
+
* @beta
|
|
83
|
+
*/
|
|
84
|
+
interface FakeRequestCall<P extends ProtocolDefinition> {
|
|
85
|
+
/** The session the request was dispatched on. */
|
|
86
|
+
readonly sessionId: string;
|
|
87
|
+
/** Method name. */
|
|
88
|
+
readonly method: MethodName<P>;
|
|
89
|
+
/** Params, as the client passed them. */
|
|
90
|
+
readonly params: unknown;
|
|
91
|
+
/** Options, as the client passed them. */
|
|
92
|
+
readonly opts: RequestOptions | undefined;
|
|
93
|
+
/** The combined signal the client built, when there was one. */
|
|
94
|
+
readonly signal: AbortSignal | undefined;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* One recorded call of any kind, in call order.
|
|
98
|
+
*
|
|
99
|
+
* @beta
|
|
100
|
+
*/
|
|
101
|
+
type FakeLogEntry<P extends ProtocolDefinition> = {
|
|
102
|
+
readonly kind: "setup";
|
|
103
|
+
} | {
|
|
104
|
+
readonly kind: "connect";
|
|
105
|
+
readonly timeoutMs: number | undefined;
|
|
106
|
+
} | {
|
|
107
|
+
readonly kind: "restore";
|
|
108
|
+
} | {
|
|
109
|
+
readonly kind: "disconnect";
|
|
110
|
+
readonly sessionId: string;
|
|
111
|
+
} | {
|
|
112
|
+
readonly kind: "request";
|
|
113
|
+
readonly sessionId: string;
|
|
114
|
+
readonly method: MethodName<P>;
|
|
115
|
+
} | {
|
|
116
|
+
readonly kind: "subscribe";
|
|
117
|
+
readonly sessionId: string;
|
|
118
|
+
readonly event: EventName<P>;
|
|
119
|
+
} | {
|
|
120
|
+
readonly kind: "unsubscribe";
|
|
121
|
+
readonly sessionId: string;
|
|
122
|
+
readonly event: EventName<P>;
|
|
123
|
+
} | {
|
|
124
|
+
readonly kind: "capabilities";
|
|
125
|
+
readonly sessionId: string;
|
|
126
|
+
} | {
|
|
127
|
+
readonly kind: "dispose";
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* A `"hang"` call the test can settle whenever it likes.
|
|
131
|
+
*
|
|
132
|
+
* @beta
|
|
133
|
+
*/
|
|
134
|
+
interface FakePendingRequest<P extends ProtocolDefinition> {
|
|
135
|
+
/** The session the request was dispatched on. */
|
|
136
|
+
readonly sessionId: string;
|
|
137
|
+
/** Method name. */
|
|
138
|
+
readonly method: MethodName<P>;
|
|
139
|
+
/** Params, as the client passed them. */
|
|
140
|
+
readonly params: unknown;
|
|
141
|
+
/** The combined signal the client built, when there was one. */
|
|
142
|
+
readonly signal: AbortSignal | undefined;
|
|
143
|
+
/** Settles the call with a result. A second settlement does nothing. */
|
|
144
|
+
resolve(result: unknown): void;
|
|
145
|
+
/** Settles the call with an error. A second settlement does nothing. */
|
|
146
|
+
reject(error: unknown): void;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* What a fake connector exposes on top of
|
|
150
|
+
* {@link @bchconnect/core#Connector | Connector}.
|
|
151
|
+
*
|
|
152
|
+
* @beta
|
|
153
|
+
*/
|
|
154
|
+
interface FakeConnectorHandle<P extends ProtocolDefinition> {
|
|
155
|
+
/** Every `request()` call, in order. */
|
|
156
|
+
readonly calls: readonly FakeRequestCall<P>[];
|
|
157
|
+
/** Every call of any kind, in order. */
|
|
158
|
+
readonly log: readonly FakeLogEntry<P>[];
|
|
159
|
+
/** Unsettled `"hang"` calls, in dispatch order. */
|
|
160
|
+
readonly pending: readonly FakePendingRequest<P>[];
|
|
161
|
+
/** Emits a session lifecycle event. Throws before `setup()` ran. */
|
|
162
|
+
emit: ConnectorContext["emit"];
|
|
163
|
+
/** Delivers a protocol event to the listeners of one session. */
|
|
164
|
+
emitProtocolEvent<E extends EventName<P>>(session: Session<P>, event: E, payload: EventPayload<P, E>): void;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* A connector whose every outcome is scripted, plus its recordings.
|
|
168
|
+
*
|
|
169
|
+
* @beta
|
|
170
|
+
*/
|
|
171
|
+
type FakeConnector<P extends ProtocolDefinition> = Connector<P> & FakeConnectorHandle<P>;
|
|
172
|
+
/**
|
|
173
|
+
* Builds a connector that does exactly what its script says, and records
|
|
174
|
+
* everything the client asked of it.
|
|
175
|
+
*
|
|
176
|
+
* The protocol is passed explicitly: a partial method map and an optional
|
|
177
|
+
* session give inference nothing reliable to work from.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const connector = createFakeConnector<DemoProtocol>({
|
|
182
|
+
* protocol: "demo",
|
|
183
|
+
* session: demoSession({ id: "s1" }),
|
|
184
|
+
* methods: {
|
|
185
|
+
* get_balance: async () => ({ sats: 1_000n }),
|
|
186
|
+
* sign_message: "hang",
|
|
187
|
+
* },
|
|
188
|
+
* capabilities: ["message-signing"],
|
|
189
|
+
* });
|
|
190
|
+
*
|
|
191
|
+
* const session = await connector.connect({});
|
|
192
|
+
* connector.emitProtocolEvent(session, "wallet_ready", { walletName: "Fake" });
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* @beta
|
|
196
|
+
*/
|
|
197
|
+
export declare function createFakeConnector<P extends ProtocolDefinition>(script: FakeConnectorScript<P>): FakeConnector<P>;
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/session.d.ts
|
|
200
|
+
/**
|
|
201
|
+
* The parts of a {@link @bchconnect/core#Session | Session} a test cares
|
|
202
|
+
* about. Everything else is filled with a plausible default.
|
|
203
|
+
*
|
|
204
|
+
* @beta
|
|
205
|
+
*/
|
|
206
|
+
interface FakeSessionInit<P extends ProtocolDefinition> {
|
|
207
|
+
/** Protocol id. Must match the connector the session belongs to. */
|
|
208
|
+
readonly protocol: P["id"];
|
|
209
|
+
/** The protocol's handshake payload. */
|
|
210
|
+
readonly data: P["session"];
|
|
211
|
+
/** Session id. Defaults to a fresh `crypto.randomUUID()`. */
|
|
212
|
+
readonly id?: string;
|
|
213
|
+
/** Wallet identity. Defaults to `{ source: "protocol" }`, with no name. */
|
|
214
|
+
readonly wallet?: WalletIdentity;
|
|
215
|
+
/** Session status. Defaults to connected and reachable. */
|
|
216
|
+
readonly status?: SessionStatus;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Builds a `Session` for tests.
|
|
220
|
+
*
|
|
221
|
+
* Pass the result to `createFakeConnector` as a value for a session whose id
|
|
222
|
+
* the test pins, or as a function for a fresh id on every `connect()`.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* const session = fakeSession<DemoProtocol>({
|
|
227
|
+
* id: "s1",
|
|
228
|
+
* protocol: "demo",
|
|
229
|
+
* data: {
|
|
230
|
+
* addresses: ["bitcoincash:qpea4gc3r589sdqdcprsu6uyxpugpqnshsa8v7vsxq"],
|
|
231
|
+
* xpubs: [],
|
|
232
|
+
* },
|
|
233
|
+
* });
|
|
234
|
+
* ```
|
|
235
|
+
*
|
|
236
|
+
* @beta
|
|
237
|
+
*/
|
|
238
|
+
export declare function fakeSession<P extends ProtocolDefinition>(init: FakeSessionInit<P>): Session<P>;
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/protocols.d.ts
|
|
241
|
+
/**
|
|
242
|
+
* Handshake payload of {@link DemoProtocol}: addresses, derivation paths and
|
|
243
|
+
* the wallet's own name when it sent one.
|
|
244
|
+
*
|
|
245
|
+
* @beta
|
|
246
|
+
*/
|
|
247
|
+
interface DemoSessionData {
|
|
248
|
+
/** Addresses the wallet exposed, in wallet order. */
|
|
249
|
+
readonly addresses: readonly string[];
|
|
250
|
+
/** Named extended public keys. */
|
|
251
|
+
readonly xpubs: readonly {
|
|
252
|
+
readonly name: string;
|
|
253
|
+
readonly xpub: string;
|
|
254
|
+
}[];
|
|
255
|
+
/** Wallet name, when the protocol carries one. */
|
|
256
|
+
readonly walletName?: string;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* A protocol definition for tests, shaped after a real wallet protocol.
|
|
260
|
+
*
|
|
261
|
+
* Its methods cover every branch of the timeout resolution order: a method
|
|
262
|
+
* with no `timeoutMs` (the client's read default applies), one with its own
|
|
263
|
+
* `timeoutMs`, and two `userInteraction` methods (no default deadline).
|
|
264
|
+
*
|
|
265
|
+
* @beta
|
|
266
|
+
*/
|
|
267
|
+
interface DemoProtocol extends ProtocolDefinition {
|
|
268
|
+
/** Protocol id. */
|
|
269
|
+
id: "demo";
|
|
270
|
+
/** Handshake payload. */
|
|
271
|
+
session: DemoSessionData;
|
|
272
|
+
/** Request-shaped methods. */
|
|
273
|
+
methods: {
|
|
274
|
+
/** A read with no parameters and no per-method deadline. */
|
|
275
|
+
get_addresses: {
|
|
276
|
+
params: undefined;
|
|
277
|
+
result: {
|
|
278
|
+
readonly addresses: readonly string[];
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
/** A read carrying its own deadline. */
|
|
282
|
+
get_balance: {
|
|
283
|
+
params: {
|
|
284
|
+
readonly address: string;
|
|
285
|
+
};
|
|
286
|
+
result: {
|
|
287
|
+
readonly sats: bigint;
|
|
288
|
+
};
|
|
289
|
+
timeoutMs: 5_000;
|
|
290
|
+
};
|
|
291
|
+
/** Signing, gated on the user. */
|
|
292
|
+
sign_transaction: {
|
|
293
|
+
params: {
|
|
294
|
+
readonly transaction: SignTransactionRequest;
|
|
295
|
+
};
|
|
296
|
+
result: SignTransactionResponse;
|
|
297
|
+
userInteraction: true;
|
|
298
|
+
};
|
|
299
|
+
/** Message signing, gated on the user. */
|
|
300
|
+
sign_message: {
|
|
301
|
+
params: SignMessageRequest;
|
|
302
|
+
result: SignMessageResponse;
|
|
303
|
+
userInteraction: true;
|
|
304
|
+
};
|
|
305
|
+
};
|
|
306
|
+
/** Wallet-initiated messages. */
|
|
307
|
+
events: {
|
|
308
|
+
/** The wallet's address set changed. */
|
|
309
|
+
addresses_changed: {
|
|
310
|
+
readonly addresses: readonly string[];
|
|
311
|
+
};
|
|
312
|
+
/** The wallet finished loading and announced itself. */
|
|
313
|
+
wallet_ready: {
|
|
314
|
+
readonly walletName?: string;
|
|
315
|
+
};
|
|
316
|
+
};
|
|
317
|
+
/** Capabilities sessions of this protocol may have. */
|
|
318
|
+
capability: "libauth-signing" | "message-signing";
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* The runtime metadata of {@link DemoProtocol}'s methods, as its connector
|
|
322
|
+
* reports it: `get_balance` carries its own deadline, the signing methods
|
|
323
|
+
* wait on the user, and `get_addresses` is a plain read.
|
|
324
|
+
*
|
|
325
|
+
* @beta
|
|
326
|
+
*/
|
|
327
|
+
export declare const demoMetadata: { readonly [M in keyof DemoProtocol["methods"]]: Pick<DemoProtocol["methods"][M], keyof DemoProtocol["methods"][M] & keyof MethodMetadata> & { readonly [K in Exclude<keyof MethodMetadata, keyof DemoProtocol["methods"][M]>]?: never; }; };
|
|
328
|
+
/**
|
|
329
|
+
* The capability interfaces of a {@link DemoProtocol} session, as its
|
|
330
|
+
* connector builds them: `sign_transaction` wraps the request, and
|
|
331
|
+
* `sign_message` takes it as is.
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* ```ts
|
|
335
|
+
* const connector = createFakeConnector<DemoProtocol>({
|
|
336
|
+
* protocol: "demo",
|
|
337
|
+
* capabilities: ["libauth-signing"],
|
|
338
|
+
* interfaces: demoCapabilities,
|
|
339
|
+
* });
|
|
340
|
+
* ```
|
|
341
|
+
*
|
|
342
|
+
* @beta
|
|
343
|
+
*/
|
|
344
|
+
export declare function demoCapabilities(_session: Session<DemoProtocol>, client: SessionClient<DemoProtocol>): CapabilityMap<DemoProtocol>;
|
|
345
|
+
/**
|
|
346
|
+
* Handshake payload of {@link DemoAltProtocol}.
|
|
347
|
+
*
|
|
348
|
+
* @beta
|
|
349
|
+
*/
|
|
350
|
+
interface DemoAltSessionData {
|
|
351
|
+
/** The wallet's public key, its only identity on the wire. */
|
|
352
|
+
readonly pubkey: Uint8Array;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* A second protocol definition, for multi-protocol tests.
|
|
356
|
+
*
|
|
357
|
+
* It shares the method name `get_balance` with {@link DemoProtocol} under a
|
|
358
|
+
* different result type, and declares no events, so narrowing by
|
|
359
|
+
* `session.protocol` can be exercised.
|
|
360
|
+
*
|
|
361
|
+
* @beta
|
|
362
|
+
*/
|
|
363
|
+
interface DemoAltProtocol extends ProtocolDefinition {
|
|
364
|
+
/** Protocol id. */
|
|
365
|
+
id: "demo-alt";
|
|
366
|
+
/** Handshake payload. */
|
|
367
|
+
session: DemoAltSessionData;
|
|
368
|
+
/** Request-shaped methods. */
|
|
369
|
+
methods: {
|
|
370
|
+
/** Same name as {@link DemoProtocol}'s, different result. */
|
|
371
|
+
get_balance: {
|
|
372
|
+
params: {
|
|
373
|
+
readonly token?: string;
|
|
374
|
+
};
|
|
375
|
+
result: bigint;
|
|
376
|
+
};
|
|
377
|
+
};
|
|
378
|
+
/** This protocol has no wallet-initiated messages. */
|
|
379
|
+
events: Record<never, never>;
|
|
380
|
+
/** Capabilities sessions of this protocol may have. */
|
|
381
|
+
capability: "message-signing";
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Builds a {@link DemoProtocol} session with placeholder data.
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```ts
|
|
388
|
+
* const session = demoSession({ id: "s1" });
|
|
389
|
+
* const other = demoSession({ data: { addresses: [], xpubs: [] } });
|
|
390
|
+
* ```
|
|
391
|
+
*
|
|
392
|
+
* @beta
|
|
393
|
+
*/
|
|
394
|
+
export declare function demoSession(init?: Partial<FakeSessionInit<DemoProtocol>>): Session<DemoProtocol>;
|
|
395
|
+
/**
|
|
396
|
+
* Builds a {@link DemoAltProtocol} session with placeholder data.
|
|
397
|
+
*
|
|
398
|
+
* @example
|
|
399
|
+
* ```ts
|
|
400
|
+
* const session = demoAltSession({ id: "s2" });
|
|
401
|
+
* ```
|
|
402
|
+
*
|
|
403
|
+
* @beta
|
|
404
|
+
*/
|
|
405
|
+
export declare function demoAltSession(init?: Partial<FakeSessionInit<DemoAltProtocol>>): Session<DemoAltProtocol>;
|
|
406
|
+
//#endregion
|
|
407
|
+
export type { DemoAltProtocol, DemoAltSessionData, DemoProtocol, DemoSessionData, FakeConnector, FakeConnectorHandle, FakeConnectorScript, FakeLogEntry, FakeMethodMetadata, FakeMethodScript, FakeMethodScripts, FakePendingRequest, FakeRequestCall, FakeSessionInit };
|
|
408
|
+
//# sourceMappingURL=index.d.ts.map
|