-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-tests.mjs
More file actions
220 lines (192 loc) · 6.27 KB
/
run-tests.mjs
File metadata and controls
220 lines (192 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env node
import { test } from 'node:test';
import assert from 'node:assert';
import { AffixioClient } from './dist/index.js';
import {
AffixioApiError,
AffixioValidationError,
AffixioTransportError,
} from './dist/errors.js';
// Mock fetch for testing
globalThis.fetch = (async (url, options) => {
// Mock successful verification response
if (url.includes('/v1/verify') && options.method === 'POST') {
const body = JSON.parse(options.body);
// Simulate network error for specific test case
if (body.circuit_id === 'network-error') {
throw new TypeError('fetch failed');
}
// Simulate timeout
if (body.circuit_id === 'timeout') {
const error = new Error('Aborted');
error.name = 'AbortError';
throw error;
}
// Simulate API error
if (body.circuit_id === 'api-error') {
return {
ok: false,
status: 400,
statusText: 'Bad Request',
json: async () => ({
error: 'Invalid circuit',
}),
headers: new Map([['X-Request-ID', 'req_123']]),
};
}
// Default success response
return {
ok: true,
status: 200,
statusText: 'OK',
json: async () => ({
eligible: true,
proof: 'proof_abc123',
circuit_id: body.circuit_id,
latency_ms: 145,
logged: true,
}),
headers: new Map([['X-Request-ID', 'req_123']]),
};
}
throw new Error('Unexpected fetch call');
});
test('AffixioClient: constructor validates apiKey', () => {
assert.throws(
() => new AffixioClient({ apiKey: '' }),
AffixioValidationError
);
assert.throws(
() => new AffixioClient({ apiKey: undefined }),
AffixioValidationError
);
});
test('AffixioClient: verify happy path', async () => {
const client = new AffixioClient({ apiKey: 'test_key' });
const response = await client.verify({
circuit_id: 'agentic-payment-permission',
identifier: 'agent:test-001',
context: { amount_cents: 1000 },
});
assert.strictEqual(response.eligible, true);
assert.strictEqual(response.proof, 'proof_abc123');
assert.strictEqual(response.circuit_id, 'agentic-payment-permission');
assert.strictEqual(response.latency_ms, 145);
});
test('AffixioClient: verifyIssuerAgentAuthorization happy path', async () => {
const client = new AffixioClient({ apiKey: 'test_key' });
const response = await client.verifyIssuerAgentAuthorization({
agentId: 'agent:procurement-001',
accountId: 'acct_123',
consentReference: 'consent_abc',
amount: 14900,
currency: 'USD',
merchantCategory: 'software_subscription',
merchantId: 'merchant_xyz',
});
assert.strictEqual(response.eligible, true);
assert.strictEqual(response.circuit_id, 'agentic-payment-permission');
});
test('AffixioClient: verifyMerchantAgentCheckout happy path', async () => {
const client = new AffixioClient({ apiKey: 'test_key' });
const response = await client.verifyMerchantAgentCheckout({
agentId: 'agent:checkout-001',
customerId: 'cust_456',
basketTotalCents: 5999,
currency: 'USD',
merchantId: 'merchant_xyz',
merchantCategory: 'software_subscription',
basketCategories: ['licenses', 'support'],
});
assert.strictEqual(response.eligible, true);
});
test('AffixioClient: verifyAgentConsent happy path', async () => {
const client = new AffixioClient({ apiKey: 'test_key' });
const response = await client.verifyAgentConsent({
agentId: 'agent:consent-001',
consentReference: 'consent_xyz',
transactionContext: {
amount_cents: 2500,
currency: 'USD',
merchant_category: 'travel_agency',
purpose: 'flight_booking',
},
});
assert.strictEqual(response.eligible, true);
});
test('AffixioClient: verifyAgentPaymentComposite happy path', async () => {
const client = new AffixioClient({ apiKey: 'test_key' });
const response = await client.verifyAgentPaymentComposite({
agentId: 'agent:composite-001',
accountId: 'acct_789',
consentReference: 'consent_def',
transactionContext: {
amount_cents: 50000,
currency: 'USD',
merchant_category: 'subscription_service',
},
});
assert.strictEqual(response.overallEligible, true);
assert.strictEqual(response.perCircuit.length, 3);
assert.strictEqual(response.perCircuit[0].circuitId, 'agentic-payment-permission');
assert.strictEqual(response.perCircuit[0].eligible, true);
assert.match(response.aggregatedProof, /proof_abc123/);
});
test('AffixioClient: verifyAgentPaymentComposite with custom circuits', async () => {
const client = new AffixioClient({ apiKey: 'test_key' });
const response = await client.verifyAgentPaymentComposite({
agentId: 'agent:custom-circuits',
cardId: 'card_456',
consentReference: 'consent_ghi',
transactionContext: {
amount_cents: 75000,
currency: 'GBP',
},
circuits: ['agentic-payment-permission', 'cross-mfa-verification'],
});
assert.strictEqual(response.perCircuit.length, 2);
});
test('AffixioClient: API error handling', async () => {
const client = new AffixioClient({ apiKey: 'test_key' });
try {
await client.verify({
circuit_id: 'api-error',
identifier: 'agent:error-001',
});
assert.fail('Expected AffixioApiError');
} catch (error) {
assert(error instanceof AffixioApiError);
assert.strictEqual(error.status, 400);
assert.strictEqual(error.requestId, 'req_123');
}
});
test('AffixioClient: network error handling', async () => {
const client = new AffixioClient({ apiKey: 'test_key' });
try {
await client.verify({
circuit_id: 'network-error',
identifier: 'agent:network-error',
});
assert.fail('Expected AffixioTransportError');
} catch (error) {
assert(error instanceof AffixioTransportError);
assert(error.message.includes('Network request failed'));
}
});
test('AffixioClient: timeout error handling', async () => {
const client = new AffixioClient({
apiKey: 'test_key',
timeoutMs: 1000,
});
try {
await client.verify({
circuit_id: 'timeout',
identifier: 'agent:timeout',
});
assert.fail('Expected AffixioTransportError');
} catch (error) {
assert(error instanceof AffixioTransportError);
assert(error.message.includes('Request timeout'));
}
});
console.log('\n✓ All tests passed!\n');