@faremeter/test-harness 0.16.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 +165 -0
- package/README.md +41 -0
- package/dist/src/choosers.d.ts +12 -0
- package/dist/src/choosers.d.ts.map +1 -0
- package/dist/src/choosers.js +79 -0
- package/dist/src/harness/config.d.ts +38 -0
- package/dist/src/harness/config.d.ts.map +1 -0
- package/dist/src/harness/config.js +1 -0
- package/dist/src/harness/defaults.d.ts +8 -0
- package/dist/src/harness/defaults.d.ts.map +1 -0
- package/dist/src/harness/defaults.js +20 -0
- package/dist/src/harness/harness.d.ts +70 -0
- package/dist/src/harness/harness.d.ts.map +1 -0
- package/dist/src/harness/harness.js +205 -0
- package/dist/src/harness/harness.test.d.ts +3 -0
- package/dist/src/harness/harness.test.d.ts.map +1 -0
- package/dist/src/harness/harness.test.js +230 -0
- package/dist/src/harness/resource.d.ts +17 -0
- package/dist/src/harness/resource.d.ts.map +1 -0
- package/dist/src/harness/resource.js +8 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +17 -0
- package/dist/src/interceptors/delay.d.ts +5 -0
- package/dist/src/interceptors/delay.d.ts.map +1 -0
- package/dist/src/interceptors/delay.js +30 -0
- package/dist/src/interceptors/failures.d.ts +12 -0
- package/dist/src/interceptors/failures.d.ts.map +1 -0
- package/dist/src/interceptors/failures.js +62 -0
- package/dist/src/interceptors/hooks.d.ts +20 -0
- package/dist/src/interceptors/hooks.d.ts.map +1 -0
- package/dist/src/interceptors/hooks.js +56 -0
- package/dist/src/interceptors/logging.d.ts +20 -0
- package/dist/src/interceptors/logging.d.ts.map +1 -0
- package/dist/src/interceptors/logging.js +57 -0
- package/dist/src/interceptors/matchers.d.ts +15 -0
- package/dist/src/interceptors/matchers.d.ts.map +1 -0
- package/dist/src/interceptors/matchers.js +30 -0
- package/dist/src/interceptors/responses.d.ts +11 -0
- package/dist/src/interceptors/responses.d.ts.map +1 -0
- package/dist/src/interceptors/responses.js +47 -0
- package/dist/src/interceptors/types.d.ts +20 -0
- package/dist/src/interceptors/types.d.ts.map +1 -0
- package/dist/src/interceptors/types.js +19 -0
- package/dist/src/interceptors/utils.d.ts +2 -0
- package/dist/src/interceptors/utils.d.ts.map +1 -0
- package/dist/src/interceptors/utils.js +9 -0
- package/dist/src/scheme/client.d.ts +16 -0
- package/dist/src/scheme/client.d.ts.map +1 -0
- package/dist/src/scheme/client.js +38 -0
- package/dist/src/scheme/constants.d.ts +4 -0
- package/dist/src/scheme/constants.d.ts.map +1 -0
- package/dist/src/scheme/constants.js +3 -0
- package/dist/src/scheme/facilitator.d.ts +16 -0
- package/dist/src/scheme/facilitator.d.ts.map +1 -0
- package/dist/src/scheme/facilitator.js +121 -0
- package/dist/src/scheme/types.d.ts +8 -0
- package/dist/src/scheme/types.d.ts.map +1 -0
- package/dist/src/scheme/types.js +3 -0
- package/dist/src/testing/console.d.ts +9 -0
- package/dist/src/testing/console.d.ts.map +1 -0
- package/dist/src/testing/console.js +15 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @faremeter/test-harness
|
|
2
|
+
|
|
3
|
+
In-process test harness for x402 protocol testing without network calls.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install @faremeter/test-harness
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- In-process testing - No network calls or external services
|
|
14
|
+
- Interceptor pattern - Inject failures, delays, and custom responses
|
|
15
|
+
- Composable - Combine interceptors for complex scenarios
|
|
16
|
+
- Test scheme - Simple payment scheme for protocol validation
|
|
17
|
+
- Framework agnostic - Works with any test runner
|
|
18
|
+
|
|
19
|
+
## API Reference
|
|
20
|
+
|
|
21
|
+
<!-- TSDOC_START -->
|
|
22
|
+
|
|
23
|
+
<!-- TSDOC_END -->
|
|
24
|
+
|
|
25
|
+
## Examples
|
|
26
|
+
|
|
27
|
+
See integration tests in the [faremeter repository](https://github.com/faremeter/faremeter/tree/main/tests):
|
|
28
|
+
|
|
29
|
+
- [Success flow](https://github.com/faremeter/faremeter/blob/main/tests/x402v1/success.test.ts)
|
|
30
|
+
- [Verification failures](https://github.com/faremeter/faremeter/blob/main/tests/x402v1/verification-failures.test.ts)
|
|
31
|
+
- [Settlement failures](https://github.com/faremeter/faremeter/blob/main/tests/x402v1/settlement-failures.test.ts)
|
|
32
|
+
|
|
33
|
+
## Related Packages
|
|
34
|
+
|
|
35
|
+
- [@faremeter/fetch](https://www.npmjs.com/package/@faremeter/fetch) - Client-side fetch wrapper
|
|
36
|
+
- [@faremeter/middleware](https://www.npmjs.com/package/@faremeter/middleware) - Server-side middleware
|
|
37
|
+
- [@faremeter/facilitator](https://www.npmjs.com/package/@faremeter/facilitator) - Payment facilitator service
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
LGPL-3.0-only
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PaymentExecer } from "@faremeter/types/client";
|
|
2
|
+
export declare function chooseFirst(execers: PaymentExecer[]): PaymentExecer;
|
|
3
|
+
export declare function chooseCheapest(execers: PaymentExecer[]): PaymentExecer;
|
|
4
|
+
export declare function chooseMostExpensive(execers: PaymentExecer[]): PaymentExecer;
|
|
5
|
+
export declare function chooseByAsset(asset: string): (execers: PaymentExecer[]) => PaymentExecer;
|
|
6
|
+
export declare function chooseByNetwork(network: string): (execers: PaymentExecer[]) => PaymentExecer;
|
|
7
|
+
export declare function chooseByScheme(scheme: string): (execers: PaymentExecer[]) => PaymentExecer;
|
|
8
|
+
export declare function chooseByIndex(index: number): (execers: PaymentExecer[]) => PaymentExecer;
|
|
9
|
+
export declare function chooseNone(): never;
|
|
10
|
+
export declare function chooseWithInspection(inspector: (execers: PaymentExecer[]) => void, inner: (execers: PaymentExecer[]) => PaymentExecer): (execers: PaymentExecer[]) => PaymentExecer;
|
|
11
|
+
export declare function chooseWithFilter(filter: (execer: PaymentExecer) => boolean, inner: (execers: PaymentExecer[]) => PaymentExecer): (execers: PaymentExecer[]) => PaymentExecer;
|
|
12
|
+
//# sourceMappingURL=choosers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"choosers.d.ts","sourceRoot":"","sources":["../../src/choosers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAE7D,wBAAgB,WAAW,CAAC,OAAO,EAAE,aAAa,EAAE,GAAG,aAAa,CASnE;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,aAAa,EAAE,GAAG,aAAa,CAUtE;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,aAAa,EAAE,GAAG,aAAa,CAU3E;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,GACZ,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,CAU7C;AAED,wBAAgB,eAAe,CAC7B,OAAO,EAAE,MAAM,GACd,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,CAU7C;AAED,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,GACb,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,CAU7C;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,GACZ,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,CAU7C;AAED,wBAAgB,UAAU,IAAI,KAAK,CAElC;AAED,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,IAAI,EAC7C,KAAK,EAAE,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,GACjD,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,CAK7C;AAED,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,OAAO,EAC1C,KAAK,EAAE,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,GACjD,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,CAK7C"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export function chooseFirst(execers) {
|
|
2
|
+
if (execers.length === 0) {
|
|
3
|
+
throw new Error("No payment options available");
|
|
4
|
+
}
|
|
5
|
+
const first = execers[0];
|
|
6
|
+
if (!first) {
|
|
7
|
+
throw new Error("No payment options available");
|
|
8
|
+
}
|
|
9
|
+
return first;
|
|
10
|
+
}
|
|
11
|
+
export function chooseCheapest(execers) {
|
|
12
|
+
if (execers.length === 0) {
|
|
13
|
+
throw new Error("No payment options available");
|
|
14
|
+
}
|
|
15
|
+
return execers.reduce((cheapest, current) => BigInt(current.requirements.maxAmountRequired) <
|
|
16
|
+
BigInt(cheapest.requirements.maxAmountRequired)
|
|
17
|
+
? current
|
|
18
|
+
: cheapest);
|
|
19
|
+
}
|
|
20
|
+
export function chooseMostExpensive(execers) {
|
|
21
|
+
if (execers.length === 0) {
|
|
22
|
+
throw new Error("No payment options available");
|
|
23
|
+
}
|
|
24
|
+
return execers.reduce((expensive, current) => BigInt(current.requirements.maxAmountRequired) >
|
|
25
|
+
BigInt(expensive.requirements.maxAmountRequired)
|
|
26
|
+
? current
|
|
27
|
+
: expensive);
|
|
28
|
+
}
|
|
29
|
+
export function chooseByAsset(asset) {
|
|
30
|
+
return (execers) => {
|
|
31
|
+
const match = execers.find((e) => e.requirements.asset.toLowerCase() === asset.toLowerCase());
|
|
32
|
+
if (!match) {
|
|
33
|
+
throw new Error(`No payment option for asset: ${asset}`);
|
|
34
|
+
}
|
|
35
|
+
return match;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function chooseByNetwork(network) {
|
|
39
|
+
return (execers) => {
|
|
40
|
+
const match = execers.find((e) => e.requirements.network.toLowerCase() === network.toLowerCase());
|
|
41
|
+
if (!match) {
|
|
42
|
+
throw new Error(`No payment option for network: ${network}`);
|
|
43
|
+
}
|
|
44
|
+
return match;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function chooseByScheme(scheme) {
|
|
48
|
+
return (execers) => {
|
|
49
|
+
const match = execers.find((e) => e.requirements.scheme.toLowerCase() === scheme.toLowerCase());
|
|
50
|
+
if (!match) {
|
|
51
|
+
throw new Error(`No payment option for scheme: ${scheme}`);
|
|
52
|
+
}
|
|
53
|
+
return match;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function chooseByIndex(index) {
|
|
57
|
+
return (execers) => {
|
|
58
|
+
const execer = execers[index];
|
|
59
|
+
if (!execer) {
|
|
60
|
+
throw new Error(`Index ${index} out of bounds (${execers.length} options available)`);
|
|
61
|
+
}
|
|
62
|
+
return execer;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export function chooseNone() {
|
|
66
|
+
throw new Error("No suitable payment option");
|
|
67
|
+
}
|
|
68
|
+
export function chooseWithInspection(inspector, inner) {
|
|
69
|
+
return (execers) => {
|
|
70
|
+
inspector(execers);
|
|
71
|
+
return inner(execers);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export function chooseWithFilter(filter, inner) {
|
|
75
|
+
return (execers) => {
|
|
76
|
+
const filtered = execers.filter(filter);
|
|
77
|
+
return inner(filtered);
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { FacilitatorHandler } from "@faremeter/types/facilitator";
|
|
2
|
+
import type { PaymentHandler } from "@faremeter/types/client";
|
|
3
|
+
import type { x402PaymentRequirements } from "@faremeter/types/x402";
|
|
4
|
+
import type { Interceptor } from "../interceptors/types.js";
|
|
5
|
+
export type SettleMode = "settle-only" | "verify-then-settle";
|
|
6
|
+
export type TestHarnessConfig = {
|
|
7
|
+
/**
|
|
8
|
+
* Payment requirements the middleware accepts.
|
|
9
|
+
* Uses Partial because the facilitator will fill in missing fields.
|
|
10
|
+
*/
|
|
11
|
+
accepts: Partial<x402PaymentRequirements>[];
|
|
12
|
+
/**
|
|
13
|
+
* Facilitator handlers to register.
|
|
14
|
+
* Multiple handlers can be provided for different schemes.
|
|
15
|
+
*/
|
|
16
|
+
facilitatorHandlers: FacilitatorHandler[];
|
|
17
|
+
/**
|
|
18
|
+
* Client payment handlers.
|
|
19
|
+
* Multiple handlers can be provided for different schemes.
|
|
20
|
+
*/
|
|
21
|
+
clientHandlers: PaymentHandler[];
|
|
22
|
+
/**
|
|
23
|
+
* Settlement mode for the middleware.
|
|
24
|
+
* @default "settle-only"
|
|
25
|
+
*/
|
|
26
|
+
settleMode?: SettleMode;
|
|
27
|
+
/**
|
|
28
|
+
* Interceptors between test code and middleware.
|
|
29
|
+
* These see all requests from the wrapped fetch to the Hono app.
|
|
30
|
+
*/
|
|
31
|
+
clientInterceptors?: Interceptor[];
|
|
32
|
+
/**
|
|
33
|
+
* Interceptors between middleware and facilitator.
|
|
34
|
+
* These see requests from middleware to facilitator endpoints.
|
|
35
|
+
*/
|
|
36
|
+
middlewareInterceptors?: Interceptor[];
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/harness/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AACrE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEzD,MAAM,MAAM,UAAU,GAAG,aAAa,GAAG,oBAAoB,CAAC;AAE9D,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC,uBAAuB,CAAC,EAAE,CAAC;IAE5C;;;OAGG;IACH,mBAAmB,EAAE,kBAAkB,EAAE,CAAC;IAE1C;;;OAGG;IACH,cAAc,EAAE,cAAc,EAAE,CAAC;IAEjC;;;OAGG;IACH,UAAU,CAAC,EAAE,UAAU,CAAC;IAExB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,WAAW,EAAE,CAAC;IAEnC;;;OAGG;IACH,sBAAsB,CAAC,EAAE,WAAW,EAAE,CAAC;CACxC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { x402PaymentRequirements } from "@faremeter/types/x402";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a payment requirements object with test defaults.
|
|
4
|
+
* Override specific fields by passing a partial:
|
|
5
|
+
* accepts({ maxAmountRequired: "500" })
|
|
6
|
+
*/
|
|
7
|
+
export declare function accepts(overrides?: Partial<x402PaymentRequirements>): x402PaymentRequirements;
|
|
8
|
+
//# sourceMappingURL=defaults.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../../src/harness/defaults.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAErE;;;;GAIG;AACH,wBAAgB,OAAO,CACrB,SAAS,CAAC,EAAE,OAAO,CAAC,uBAAuB,CAAC,GAC3C,uBAAuB,CAazB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { TEST_SCHEME, TEST_NETWORK, TEST_ASSET } from "../scheme/constants.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a payment requirements object with test defaults.
|
|
4
|
+
* Override specific fields by passing a partial:
|
|
5
|
+
* accepts({ maxAmountRequired: "500" })
|
|
6
|
+
*/
|
|
7
|
+
export function accepts(overrides) {
|
|
8
|
+
return {
|
|
9
|
+
scheme: TEST_SCHEME,
|
|
10
|
+
network: TEST_NETWORK,
|
|
11
|
+
maxAmountRequired: "100",
|
|
12
|
+
resource: "http://example.com/test",
|
|
13
|
+
description: "Test resource",
|
|
14
|
+
mimeType: "application/json",
|
|
15
|
+
payTo: "test-receiver",
|
|
16
|
+
maxTimeoutSeconds: 30,
|
|
17
|
+
asset: TEST_ASSET,
|
|
18
|
+
...overrides,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import type { PaymentExecer } from "@faremeter/types/client";
|
|
3
|
+
import type { Interceptor } from "../interceptors/types.js";
|
|
4
|
+
import type { TestHarnessConfig } from "./config.js";
|
|
5
|
+
import type { ResourceHandler } from "./resource.js";
|
|
6
|
+
/**
|
|
7
|
+
* TestHarness provides an in-process test environment for the x402 protocol.
|
|
8
|
+
*
|
|
9
|
+
* It connects client, middleware, and facilitator using function adapters
|
|
10
|
+
* instead of HTTP, allowing full protocol testing without network calls.
|
|
11
|
+
*/
|
|
12
|
+
export declare class TestHarness {
|
|
13
|
+
/**
|
|
14
|
+
* The internal Hono app containing facilitator and middleware routes.
|
|
15
|
+
*/
|
|
16
|
+
readonly app: Hono;
|
|
17
|
+
private readonly config;
|
|
18
|
+
private readonly settleMode;
|
|
19
|
+
private resourceHandler;
|
|
20
|
+
private clientInterceptors;
|
|
21
|
+
private middlewareInterceptors;
|
|
22
|
+
/**
|
|
23
|
+
* Base URL used for internal routing.
|
|
24
|
+
*/
|
|
25
|
+
private readonly baseUrl;
|
|
26
|
+
constructor(config: TestHarnessConfig);
|
|
27
|
+
/**
|
|
28
|
+
* Create the internal Hono app with facilitator and resource routes.
|
|
29
|
+
*/
|
|
30
|
+
private createApp;
|
|
31
|
+
/**
|
|
32
|
+
* Create a fetch function for middleware->facilitator calls.
|
|
33
|
+
* This applies middleware interceptors and routes to the Hono app.
|
|
34
|
+
*/
|
|
35
|
+
private createMiddlewareFetch;
|
|
36
|
+
/**
|
|
37
|
+
* Create a fetch function for client->middleware calls.
|
|
38
|
+
* This applies client interceptors and routes to the Hono app.
|
|
39
|
+
*/
|
|
40
|
+
private createClientFetch;
|
|
41
|
+
/**
|
|
42
|
+
* Set the resource handler that responds after successful payment.
|
|
43
|
+
*/
|
|
44
|
+
setResourceHandler(handler: ResourceHandler): void;
|
|
45
|
+
/**
|
|
46
|
+
* Create a fetch function that handles the full x402 payment flow.
|
|
47
|
+
*
|
|
48
|
+
* @param opts.payerChooser - Function to choose which payment option to use
|
|
49
|
+
*/
|
|
50
|
+
createFetch(opts?: {
|
|
51
|
+
payerChooser?: ((execers: PaymentExecer[]) => PaymentExecer | Promise<PaymentExecer>) | undefined;
|
|
52
|
+
}): typeof fetch;
|
|
53
|
+
/**
|
|
54
|
+
* Add an interceptor to the client chain (between test code and middleware).
|
|
55
|
+
*/
|
|
56
|
+
addClientInterceptor(interceptor: Interceptor): void;
|
|
57
|
+
/**
|
|
58
|
+
* Add an interceptor to the middleware chain (between middleware and facilitator).
|
|
59
|
+
*/
|
|
60
|
+
addMiddlewareInterceptor(interceptor: Interceptor): void;
|
|
61
|
+
/**
|
|
62
|
+
* Clear all interceptors added after construction.
|
|
63
|
+
*/
|
|
64
|
+
clearInterceptors(): void;
|
|
65
|
+
/**
|
|
66
|
+
* Reset harness state (interceptors, resource handler).
|
|
67
|
+
*/
|
|
68
|
+
reset(): void;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=harness.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"harness.d.ts","sourceRoot":"","sources":["../../../src/harness/harness.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAQ5B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAQ7D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGzD,OAAO,KAAK,EAAE,iBAAiB,EAAc,MAAM,UAAU,CAAC;AAC9D,OAAO,KAAK,EAAE,eAAe,EAAmB,MAAM,YAAY,CAAC;AAanE;;;;;GAKG;AACH,qBAAa,WAAW;IACtB;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;IAEnB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoB;IAC3C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IACxC,OAAO,CAAC,eAAe,CAA2C;IAClE,OAAO,CAAC,kBAAkB,CAAqB;IAC/C,OAAO,CAAC,sBAAsB,CAAqB;IAEnD;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAyB;gBAErC,MAAM,EAAE,iBAAiB;IASrC;;OAEG;IACH,OAAO,CAAC,SAAS;IAiGjB;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAe7B;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAmBzB;;OAEG;IACH,kBAAkB,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI;IAIlD;;;;OAIG;IACH,WAAW,CAAC,IAAI,CAAC,EAAE;QACjB,YAAY,CAAC,EACT,CAAC,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,GACtE,SAAS,CAAC;KACf,GAAG,OAAO,KAAK;IAgBhB;;OAEG;IACH,oBAAoB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI;IAIpD;;OAEG;IACH,wBAAwB,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI;IAIxD;;OAEG;IACH,iBAAiB,IAAI,IAAI;IAOzB;;OAEG;IACH,KAAK,IAAI,IAAI;CAId"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { createFacilitatorRoutes } from "@faremeter/facilitator";
|
|
3
|
+
import { wrap } from "@faremeter/fetch";
|
|
4
|
+
import { handleMiddlewareRequest, getPaymentRequiredResponse, } from "@faremeter/middleware/common";
|
|
5
|
+
import { composeInterceptors } from "../interceptors/types.js";
|
|
6
|
+
import { getURLFromRequestInfo } from "../interceptors/utils.js";
|
|
7
|
+
import { defaultResourceHandler } from "./resource.js";
|
|
8
|
+
/**
|
|
9
|
+
* TestHarness provides an in-process test environment for the x402 protocol.
|
|
10
|
+
*
|
|
11
|
+
* It connects client, middleware, and facilitator using function adapters
|
|
12
|
+
* instead of HTTP, allowing full protocol testing without network calls.
|
|
13
|
+
*/
|
|
14
|
+
export class TestHarness {
|
|
15
|
+
/**
|
|
16
|
+
* The internal Hono app containing facilitator and middleware routes.
|
|
17
|
+
*/
|
|
18
|
+
app;
|
|
19
|
+
config;
|
|
20
|
+
settleMode;
|
|
21
|
+
resourceHandler = defaultResourceHandler;
|
|
22
|
+
clientInterceptors = [];
|
|
23
|
+
middlewareInterceptors = [];
|
|
24
|
+
/**
|
|
25
|
+
* Base URL used for internal routing.
|
|
26
|
+
*/
|
|
27
|
+
baseUrl = "http://test-harness";
|
|
28
|
+
constructor(config) {
|
|
29
|
+
this.config = config;
|
|
30
|
+
this.settleMode = config.settleMode ?? "settle-only";
|
|
31
|
+
this.clientInterceptors = [...(config.clientInterceptors ?? [])];
|
|
32
|
+
this.middlewareInterceptors = [...(config.middlewareInterceptors ?? [])];
|
|
33
|
+
this.app = this.createApp();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create the internal Hono app with facilitator and resource routes.
|
|
37
|
+
*/
|
|
38
|
+
createApp() {
|
|
39
|
+
const app = new Hono();
|
|
40
|
+
// Suppress error logging in test harness - errors are expected in tests
|
|
41
|
+
// and should be converted to 500 responses without stack traces
|
|
42
|
+
app.onError((err, c) => {
|
|
43
|
+
return c.json({ error: err.message }, 500);
|
|
44
|
+
});
|
|
45
|
+
// Mount facilitator routes
|
|
46
|
+
const facilitatorRoutes = createFacilitatorRoutes({
|
|
47
|
+
handlers: this.config.facilitatorHandlers,
|
|
48
|
+
});
|
|
49
|
+
app.route("/facilitator", facilitatorRoutes);
|
|
50
|
+
// Resource routes are handled dynamically via the middleware
|
|
51
|
+
// We use a catch-all that applies payment middleware
|
|
52
|
+
app.all("/*", async (c) => {
|
|
53
|
+
// Create a fetch for middleware->facilitator calls that:
|
|
54
|
+
// 1. Applies middleware interceptors
|
|
55
|
+
// 2. Routes to the Hono app
|
|
56
|
+
const middlewareFetch = this.createMiddlewareFetch();
|
|
57
|
+
// Track state for resource handler
|
|
58
|
+
const state = {};
|
|
59
|
+
const result = await handleMiddlewareRequest({
|
|
60
|
+
facilitatorURL: `${this.baseUrl}/facilitator`,
|
|
61
|
+
accepts: this.config.accepts,
|
|
62
|
+
resource: c.req.url,
|
|
63
|
+
fetch: middlewareFetch,
|
|
64
|
+
getHeader: (key) => c.req.header(key),
|
|
65
|
+
getPaymentRequiredResponse,
|
|
66
|
+
sendJSONResponse: (status, body) => {
|
|
67
|
+
c.status(status);
|
|
68
|
+
return c.json(body);
|
|
69
|
+
},
|
|
70
|
+
body: async (context) => {
|
|
71
|
+
const { paymentRequirements, paymentPayload, settle, verify } = context;
|
|
72
|
+
// Store for resource handler
|
|
73
|
+
state.paymentRequirements = paymentRequirements;
|
|
74
|
+
state.paymentPayload = paymentPayload;
|
|
75
|
+
if (this.settleMode === "verify-then-settle") {
|
|
76
|
+
const verifyResult = await verify();
|
|
77
|
+
if (!verifyResult.success) {
|
|
78
|
+
return verifyResult.errorResponse;
|
|
79
|
+
}
|
|
80
|
+
state.verifyResponse = verifyResult.facilitatorResponse;
|
|
81
|
+
const settleResult = await settle();
|
|
82
|
+
if (!settleResult.success) {
|
|
83
|
+
return settleResult.errorResponse;
|
|
84
|
+
}
|
|
85
|
+
state.settleResponse = settleResult.facilitatorResponse;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const settleResult = await settle();
|
|
89
|
+
if (!settleResult.success) {
|
|
90
|
+
return settleResult.errorResponse;
|
|
91
|
+
}
|
|
92
|
+
state.settleResponse = settleResult.facilitatorResponse;
|
|
93
|
+
}
|
|
94
|
+
// Payment successful - call resource handler
|
|
95
|
+
const ctx = {
|
|
96
|
+
resource: c.req.url,
|
|
97
|
+
request: c.req.raw,
|
|
98
|
+
paymentRequirements,
|
|
99
|
+
paymentPayload,
|
|
100
|
+
settleResponse: state.settleResponse,
|
|
101
|
+
verifyResponse: state.verifyResponse,
|
|
102
|
+
};
|
|
103
|
+
const resourceResult = await this.resourceHandler(ctx);
|
|
104
|
+
// Set response headers
|
|
105
|
+
if (resourceResult.headers) {
|
|
106
|
+
for (const [key, value] of Object.entries(resourceResult.headers)) {
|
|
107
|
+
c.header(key, value);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
c.status(resourceResult.status);
|
|
111
|
+
return c.json(resourceResult.body);
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
return result;
|
|
115
|
+
});
|
|
116
|
+
return app;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Create a fetch function for middleware->facilitator calls.
|
|
120
|
+
* This applies middleware interceptors and routes to the Hono app.
|
|
121
|
+
*/
|
|
122
|
+
createMiddlewareFetch() {
|
|
123
|
+
const baseFetch = async (input, init) => {
|
|
124
|
+
const url = getURLFromRequestInfo(input);
|
|
125
|
+
// Route to our Hono app
|
|
126
|
+
const path = url.replace(this.baseUrl, "");
|
|
127
|
+
return this.app.request(path, init);
|
|
128
|
+
};
|
|
129
|
+
if (this.middlewareInterceptors.length === 0) {
|
|
130
|
+
return baseFetch;
|
|
131
|
+
}
|
|
132
|
+
return composeInterceptors(...this.middlewareInterceptors)(baseFetch);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Create a fetch function for client->middleware calls.
|
|
136
|
+
* This applies client interceptors and routes to the Hono app.
|
|
137
|
+
*/
|
|
138
|
+
createClientFetch() {
|
|
139
|
+
const baseFetch = async (input, init) => {
|
|
140
|
+
const url = getURLFromRequestInfo(input);
|
|
141
|
+
// Route to our Hono app - strip any base URL
|
|
142
|
+
let path = url;
|
|
143
|
+
if (path.startsWith("http://") || path.startsWith("https://")) {
|
|
144
|
+
const urlObj = new URL(path);
|
|
145
|
+
path = urlObj.pathname + urlObj.search;
|
|
146
|
+
}
|
|
147
|
+
return this.app.request(path, init);
|
|
148
|
+
};
|
|
149
|
+
if (this.clientInterceptors.length === 0) {
|
|
150
|
+
return baseFetch;
|
|
151
|
+
}
|
|
152
|
+
return composeInterceptors(...this.clientInterceptors)(baseFetch);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Set the resource handler that responds after successful payment.
|
|
156
|
+
*/
|
|
157
|
+
setResourceHandler(handler) {
|
|
158
|
+
this.resourceHandler = handler;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Create a fetch function that handles the full x402 payment flow.
|
|
162
|
+
*
|
|
163
|
+
* @param opts.payerChooser - Function to choose which payment option to use
|
|
164
|
+
*/
|
|
165
|
+
createFetch(opts) {
|
|
166
|
+
const clientFetch = this.createClientFetch();
|
|
167
|
+
const payerChooser = opts?.payerChooser;
|
|
168
|
+
return wrap(clientFetch, {
|
|
169
|
+
handlers: this.config.clientHandlers,
|
|
170
|
+
...(payerChooser
|
|
171
|
+
? {
|
|
172
|
+
payerChooser: async (execers) => payerChooser(execers),
|
|
173
|
+
}
|
|
174
|
+
: {}),
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Add an interceptor to the client chain (between test code and middleware).
|
|
179
|
+
*/
|
|
180
|
+
addClientInterceptor(interceptor) {
|
|
181
|
+
this.clientInterceptors.push(interceptor);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Add an interceptor to the middleware chain (between middleware and facilitator).
|
|
185
|
+
*/
|
|
186
|
+
addMiddlewareInterceptor(interceptor) {
|
|
187
|
+
this.middlewareInterceptors.push(interceptor);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Clear all interceptors added after construction.
|
|
191
|
+
*/
|
|
192
|
+
clearInterceptors() {
|
|
193
|
+
this.clientInterceptors = [...(this.config.clientInterceptors ?? [])];
|
|
194
|
+
this.middlewareInterceptors = [
|
|
195
|
+
...(this.config.middlewareInterceptors ?? []),
|
|
196
|
+
];
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Reset harness state (interceptors, resource handler).
|
|
200
|
+
*/
|
|
201
|
+
reset() {
|
|
202
|
+
this.clearInterceptors();
|
|
203
|
+
this.resourceHandler = defaultResourceHandler;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"harness.test.d.ts","sourceRoot":"","sources":["../../../src/harness/harness.test.ts"],"names":[],"mappings":""}
|