forked from tonyoconnell/agent-launch-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-sdk-tutorial.mjs
More file actions
305 lines (260 loc) · 10.5 KB
/
test-sdk-tutorial.mjs
File metadata and controls
305 lines (260 loc) · 10.5 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* SDK Tutorial Test — Coding Agent (SDK) Persona
*
* Tests ALL lifecycle steps of the Agent Launch tutorial using the local SDK build.
* Run: AGENT_LAUNCH_ENV=dev node test-sdk-tutorial.mjs
*/
import 'dotenv/config';
// Import from local SDK build
import {
AgentLaunch,
AgentLaunchClient,
listTokens,
getToken,
calculateBuy,
getApiUrl,
getFrontendUrl,
} from './packages/sdk/dist/index.js';
// ─── Config ──────────────────────────────────────────────────────────────────
const API_KEY = process.env.AGENTVERSE_API_KEY;
if (!API_KEY) {
console.error('FATAL: AGENTVERSE_API_KEY not set in .env');
process.exit(1);
}
const apiUrl = getApiUrl();
const frontendUrl = getFrontendUrl();
console.log(`\n========================================`);
console.log(` SDK Tutorial Test Suite`);
console.log(` Environment: ${process.env.AGENT_LAUNCH_ENV || 'production'}`);
console.log(` API URL: ${apiUrl}`);
console.log(` Frontend: ${frontendUrl}`);
console.log(`========================================\n`);
// ─── Helpers ─────────────────────────────────────────────────────────────────
const results = [];
let bondingToken = null; // Will be populated by step 5
async function runStep(name, fn) {
const start = performance.now();
try {
const data = await fn();
const ms = Math.round(performance.now() - start);
results.push({ step: name, status: 'PASS', ms, data });
console.log(` [PASS] ${name} (${ms}ms)`);
if (data) {
const summary = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
// Print max 500 chars of data
console.log(` ${summary.length > 500 ? summary.slice(0, 500) + '...' : summary}\n`);
}
return data;
} catch (err) {
const ms = Math.round(performance.now() - start);
const msg = err?.message || String(err);
results.push({ step: name, status: 'FAIL', ms, error: msg });
console.log(` [FAIL] ${name} (${ms}ms)`);
console.log(` Error: ${msg}\n`);
return null;
}
}
// ─── Instantiate SDK ─────────────────────────────────────────────────────────
const sdk = new AgentLaunch({ apiKey: API_KEY });
// ─── Step 1: Discovery — Fetch /skill.md with SDK user-agent ─────────────────
await runStep('1. Discovery — fetch /skill.md', async () => {
const url = `${frontendUrl}/skill.md`;
const res = await fetch(url, {
headers: { 'User-Agent': 'agentlaunch-sdk/0.2.13' },
});
if (!res.ok) throw new Error(`HTTP ${res.status} ${res.statusText}`);
const text = await res.text();
const lines = text.split('\n');
return {
url,
status: res.status,
contentLength: text.length,
firstLine: lines[0],
hasApiBase: text.includes('API Base') || text.includes('/api/'),
hasTutorialLink: text.includes('/tutorial'),
};
});
// ─── Step 2: Authenticate — exchange API key for JWT ─────────────────────────
const authResult = await runStep('2. Authenticate — POST /agents/auth', async () => {
// Note: SDK's authenticate() has a bug (calls post() which requires header auth
// even though this endpoint uses body auth). Testing via direct fetch.
const res = await fetch(`${apiUrl}/agents/auth`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ api_key: API_KEY }),
});
if (!res.ok) {
const body = await res.text();
throw new Error(`HTTP ${res.status}: ${body}`);
}
const json = await res.json();
return {
success: json.success,
hasToken: !!json.data?.token,
tokenPreview: json.data?.token ? json.data.token.slice(0, 30) + '...' : null,
expiresIn: json.data?.expires_in,
};
});
// ─── Step 3: Get Wallet — custodial wallet address + balances ────────────────
await runStep('3. Get Wallet — GET /agents/wallet', async () => {
const wallet = await sdk.trading.getWallet(97);
return {
address: wallet.address,
nativeBalance: wallet.nativeBalance,
fetBalance: wallet.fetBalance,
chainId: wallet.chainId,
type: wallet.type,
};
});
// ─── Step 4: List Tokens — list tokens (limit 3) ────────────────────────────
const tokenList = await runStep('4. List Tokens — GET /tokens (limit 3)', async () => {
const response = await sdk.tokens.listTokens({ limit: 3 });
return {
count: response.tokens.length,
total: response.total,
tokens: response.tokens.map(t => ({
name: t.name,
symbol: t.symbol,
address: t.address,
status: t.status,
price: t.price,
progress: t.progress,
listed: t.listed,
})),
};
});
// ─── Step 5: Get Token — get details of a bonding-curve token ────────────────
await runStep('5. Get Token — bonding-curve token detail', async () => {
// Find a bonding-curve token (not fully listed) from the list, or use first available
let targetAddress = null;
if (tokenList?.tokens) {
const bondingCandidate = tokenList.tokens.find(t => t.address && !t.listed);
if (bondingCandidate) {
targetAddress = bondingCandidate.address;
} else {
// Fallback to first token with an address
const withAddr = tokenList.tokens.find(t => t.address);
if (withAddr) targetAddress = withAddr.address;
}
}
if (!targetAddress) {
// Fetch more tokens to find one
const moreTokens = await sdk.tokens.listTokens({ limit: 20 });
const candidate = moreTokens.tokens.find(t => t.address && !t.listed)
|| moreTokens.tokens.find(t => t.address);
if (candidate) targetAddress = candidate.address;
}
if (!targetAddress) throw new Error('No token with a contract address found');
const token = await sdk.tokens.getToken(targetAddress);
bondingToken = token;
return {
name: token.name,
symbol: token.symbol,
address: token.address,
status: token.status,
price: token.price,
marketCap: token.market_cap,
progress: token.progress,
listed: token.listed,
chainId: token.chainId,
};
});
// ─── Step 6: Calculate Buy — preview 10 FET buy ─────────────────────────────
await runStep('6. Calculate Buy — preview 10 FET purchase', async () => {
const address = bondingToken?.address;
if (!address) throw new Error('No bonding token available from step 5');
const result = await calculateBuy(address, '10');
return {
tokenAddress: address,
fetAmount: '10',
tokensReceived: result.tokensReceived,
pricePerToken: result.pricePerToken,
priceImpact: result.priceImpact,
fee: result.fee,
netFetSpent: result.netFetSpent,
};
});
// ─── Step 7: Tokenize — create token record ──────────────────────────────────
const timestamp = Date.now();
let createdTokenId = null;
const tokenizeResult = await runStep('7. Tokenize — POST /agents/tokenize', async () => {
// We need a valid agent address owned by this API key.
// First get agents list.
const agentsRes = await fetch(`${apiUrl}/agents/my-agents`, {
headers: {
'Content-Type': 'application/json',
'X-API-Key': API_KEY,
},
});
if (!agentsRes.ok) {
const body = await agentsRes.text();
throw new Error(`Failed to list agents: HTTP ${agentsRes.status}: ${body}`);
}
const agentsData = await agentsRes.json();
const agents = agentsData?.data?.agents || [];
if (agents.length === 0) {
throw new Error('No agents found under this API key. Cannot tokenize.');
}
const agentAddress = agents[0].address;
const tokenName = `SDK Test ${timestamp}`;
const result = await sdk.tokenize({
agentAddress,
name: tokenName,
symbol: 'SDKT',
description: `Automated SDK tutorial test token created at ${new Date().toISOString()}`,
image: 'auto',
chainId: 97,
});
createdTokenId = result.tokenId;
return {
tokenId: result.tokenId,
handoffLink: result.handoffLink,
name: result.name,
symbol: result.symbol,
status: result.status,
agentUsed: agentAddress,
};
});
// ─── Step 8: Get Created Token — verify existence ────────────────────────────
await runStep('8. Get Created Token — verify token record', async () => {
if (!createdTokenId) throw new Error('No token ID from step 7');
// The token is pending_deployment, so it won't have a contract address yet.
// Query by token ID via the backend API /tokens/id/:id endpoint.
const res = await fetch(`${apiUrl}/tokens/id/${createdTokenId}`, {
headers: {
'Content-Type': 'application/json',
'X-API-Key': API_KEY,
},
});
if (!res.ok) {
const body = await res.text();
throw new Error(`HTTP ${res.status}: ${body}`);
}
const token = await res.json();
return {
id: token.id,
name: token.name,
symbol: token.symbol || token.ticker,
status: token.status,
address: token.address || '(pending deployment)',
chainId: token.chainId,
createdAt: token.created_at || token.createdAt,
matchesStep7: token.id === createdTokenId,
};
});
// ─── Summary ─────────────────────────────────────────────────────────────────
console.log(`\n========================================`);
console.log(` RESULTS SUMMARY`);
console.log(`========================================\n`);
const passed = results.filter(r => r.status === 'PASS').length;
const failed = results.filter(r => r.status === 'FAIL').length;
const totalMs = results.reduce((sum, r) => sum + r.ms, 0);
for (const r of results) {
const icon = r.status === 'PASS' ? 'OK' : 'XX';
const errPart = r.error ? ` — ${r.error.slice(0, 80)}` : '';
console.log(` [${icon}] ${r.step} (${r.ms}ms)${errPart}`);
}
console.log(`\n Total: ${passed}/${results.length} passed, ${failed} failed`);
console.log(` Total time: ${totalMs}ms`);
console.log(`========================================\n`);
process.exit(failed > 0 ? 1 : 0);