forked from crackedstudio/tikka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.spec.ts
More file actions
326 lines (287 loc) · 9.92 KB
/
Copy pathcli.spec.ts
File metadata and controls
326 lines (287 loc) · 9.92 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* cli.spec.ts
*
* Tests for tikka CLI commands:
* - help/usage information
* - config-check (read-only)
* - fee-quote (read-only)
* - read (read-only)
* - list (read-only)
* - info (read-only)
* - create (interactive, requires wallet)
* - buy (interactive, requires wallet)
*/
import { spawn } from 'child_process';
import * as path from 'path';
/**
* Helper to run CLI command and capture output
*/
const runCLI = (
args: string[],
timeout = 5000
): Promise<{ stdout: string; stderr: string; exitCode: number }> => {
return new Promise((resolve, reject) => {
const cliPath = path.join(__dirname, '../bin/tikka.cjs');
const child = spawn('node', [cliPath, ...args], {
cwd: path.join(__dirname, '..'),
});
let stdout = '';
let stderr = '';
child.stdout?.on('data', (data) => {
stdout += data.toString();
});
child.stderr?.on('data', (data) => {
stderr += data.toString();
});
const timer = setTimeout(() => {
child.kill();
reject(new Error(`CLI command timed out after ${timeout}ms`));
}, timeout);
child.on('close', (code) => {
clearTimeout(timer);
resolve({
stdout,
stderr,
exitCode: code || 0,
});
});
child.on('error', (err) => {
clearTimeout(timer);
reject(err);
});
});
};
describe('Tikka CLI', () => {
describe('help commands', () => {
it('should show help when run without arguments', async () => {
const result = await runCLI([]);
expect(result.stdout).toContain('Usage:');
expect(result.stdout).toContain('Tikka SDK Developer CLI');
});
it('should show help with --help flag', async () => {
const result = await runCLI(['--help']);
expect(result.stdout).toContain('Usage:');
expect(result.stdout).toContain('Options:');
expect(result.stdout).toContain('Commands:');
});
it('should show all commands with help-all', async () => {
const result = await runCLI(['help-all']);
expect(result.stdout).toContain('config-check');
expect(result.stdout).toContain('fee-quote');
expect(result.stdout).toContain('read');
expect(result.stdout).toContain('list');
expect(result.stdout).toContain('info');
expect(result.stdout).toContain('create');
expect(result.stdout).toContain('buy');
});
it('should show version with --version', async () => {
const result = await runCLI(['--version']);
expect(result.stdout).toContain('0.1.0');
});
});
describe('config-check command', () => {
it('should check configuration in text format', async () => {
const result = await runCLI(['config-check']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('testnet');
}, 10000);
it('should check configuration in JSON format', async () => {
const result = await runCLI(['config-check', '--json']);
expect(result.exitCode).toBe(0);
const output = JSON.parse(result.stdout);
expect(output).toHaveProperty('version');
expect(output).toHaveProperty('network');
expect(output).toHaveProperty('status');
expect(output.network).toBe('testnet');
}, 10000);
it('should respect --network mainnet flag', async () => {
const result = await runCLI(['config-check', '--network', 'mainnet']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('mainnet');
}, 10000);
it('should output JSON with network option', async () => {
const result = await runCLI([
'config-check',
'--network',
'mainnet',
'--json',
]);
expect(result.exitCode).toBe(0);
const output = JSON.parse(result.stdout);
expect(output.network).toBe('mainnet');
}, 10000);
});
describe('fee-quote command', () => {
it('should get fee quote for contract', async () => {
const result = await runCLI(['fee-quote', 'CONTRACT_ID_123']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('CONTRACT_ID_123');
expect(result.stdout).toContain('stroops');
}, 10000);
it('should output fee quote as JSON', async () => {
const result = await runCLI([
'fee-quote',
'CONTRACT_ID_123',
'--json',
]);
expect(result.exitCode).toBe(0);
const output = JSON.parse(result.stdout);
expect(output.contractId).toBe('CONTRACT_ID_123');
expect(output).toHaveProperty('baseFee');
expect(output).toHaveProperty('estimatedFee');
}, 10000);
it('should accept custom function name', async () => {
const result = await runCLI([
'fee-quote',
'CONTRACT_ID_123',
'--function',
'custom_function',
]);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('custom_function');
}, 10000);
it('should work with mainnet', async () => {
const result = await runCLI([
'fee-quote',
'CONTRACT_ID_123',
'--network',
'mainnet',
]);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('mainnet');
}, 10000);
});
describe('read command', () => {
it('should read contract data', async () => {
const result = await runCLI(['read', 'CONTRACT_ID_456']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('CONTRACT_ID_456');
}, 10000);
it('should output read data as JSON', async () => {
const result = await runCLI(['read', 'CONTRACT_ID_456', '--json']);
expect(result.exitCode).toBe(0);
const output = JSON.parse(result.stdout);
expect(output.contractId).toBe('CONTRACT_ID_456');
expect(output).toHaveProperty('data');
}, 10000);
it('should accept optional key parameter', async () => {
const result = await runCLI([
'read',
'CONTRACT_ID_456',
'--key',
'custom_key',
]);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('custom_key');
}, 10000);
it('should work with mainnet', async () => {
const result = await runCLI([
'read',
'CONTRACT_ID_456',
'--network',
'mainnet',
]);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('mainnet');
}, 10000);
});
describe('list command', () => {
it('should list active raffles', async () => {
const result = await runCLI(['list']);
expect(result.exitCode).toMatch(/0|1/); // May fail if SDK not ready, but should not crash
}, 10000);
it('should output list as JSON', async () => {
const result = await runCLI(['list', '--json']);
// Output should be valid JSON even if empty array or error
try {
JSON.parse(result.stdout);
} catch {
// If parse fails, there should be an error message
expect(result.stdout).toContain('error');
}
}, 10000);
it('should accept limit parameter', async () => {
const result = await runCLI(['list', '--limit', '5']);
expect(result.exitCode).toMatch(/0|1/);
}, 10000);
it('should work with mainnet', async () => {
const result = await runCLI(['list', '--network', 'mainnet']);
expect(result.exitCode).toMatch(/0|1/);
}, 10000);
});
describe('info command', () => {
it('should get contract info', async () => {
const result = await runCLI(['info']);
expect(result.exitCode).toMatch(/0|1/);
}, 10000);
it('should output info as JSON', async () => {
const result = await runCLI(['info', '--json']);
// Should be valid JSON or error
try {
JSON.parse(result.stdout);
} catch {
expect(result.stdout).toContain('error');
}
}, 10000);
it('should work with mainnet', async () => {
const result = await runCLI(['info', '--network', 'mainnet']);
expect(result.exitCode).toMatch(/0|1/);
}, 10000);
it('should not include emojis in JSON output', async () => {
const result = await runCLI(['info', '--json']);
const output = result.stdout;
// If JSON, should not have emoji prefix before JSON
if (output.trim().startsWith('{')) {
expect(output).not.toContain('🔍');
}
}, 10000);
});
describe('error handling', () => {
it('should handle invalid command gracefully', async () => {
const result = await runCLI(['invalid-command']);
expect(result.exitCode).not.toBe(0);
expect(result.stderr).toContain('Invalid command');
});
it('should provide helpful error messages', async () => {
const result = await runCLI(['help-all']);
expect(result.stdout).toContain('EXAMPLES:');
});
});
describe('global options', () => {
it('should support --network testnet', async () => {
const result = await runCLI([
'--network',
'testnet',
'config-check',
]);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('testnet');
}, 10000);
it('should support --network mainnet', async () => {
const result = await runCLI([
'--network',
'mainnet',
'config-check',
]);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('mainnet');
}, 10000);
it('should support -n shorthand for network', async () => {
const result = await runCLI(['-n', 'mainnet', 'config-check']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('mainnet');
}, 10000);
it('should support --json flag', async () => {
const result = await runCLI(['config-check', '--json']);
expect(result.exitCode).toBe(0);
const output = JSON.parse(result.stdout);
expect(output).toBeInstanceOf(Object);
}, 10000);
it('should support -j shorthand for json', async () => {
const result = await runCLI(['config-check', '-j']);
expect(result.exitCode).toBe(0);
const output = JSON.parse(result.stdout);
expect(output).toBeInstanceOf(Object);
}, 10000);
});
});