@5ive-tech/sdk 1.1.2

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.
Files changed (119) hide show
  1. package/README.md +279 -0
  2. package/dist/FiveSDK.d.ts +336 -0
  3. package/dist/FiveSDK.js +395 -0
  4. package/dist/accounts/index.d.ts +254 -0
  5. package/dist/accounts/index.js +543 -0
  6. package/dist/assets/vm/dummy.file +0 -0
  7. package/dist/assets/vm/five_vm_wasm.d.ts +762 -0
  8. package/dist/assets/vm/five_vm_wasm.js +3754 -0
  9. package/dist/assets/vm/five_vm_wasm_bg.js +3307 -0
  10. package/dist/assets/vm/five_vm_wasm_bg.wasm +0 -0
  11. package/dist/assets/vm/five_vm_wasm_bg.wasm.d.ts +247 -0
  12. package/dist/assets/vm/package.json +11 -0
  13. package/dist/bin/gen-types.d.ts +2 -0
  14. package/dist/bin/gen-types.js +35 -0
  15. package/dist/compiler/BytecodeCompiler.d.ts +83 -0
  16. package/dist/compiler/BytecodeCompiler.js +379 -0
  17. package/dist/config/ConfigManager.d.ts +13 -0
  18. package/dist/config/ConfigManager.js +27 -0
  19. package/dist/config/ProgramIdResolver.d.ts +62 -0
  20. package/dist/config/ProgramIdResolver.js +104 -0
  21. package/dist/crypto/index.d.ts +211 -0
  22. package/dist/crypto/index.js +451 -0
  23. package/dist/encoding/ParameterEncoder.d.ts +31 -0
  24. package/dist/encoding/ParameterEncoder.js +278 -0
  25. package/dist/index.d.ts +21 -0
  26. package/dist/index.js +28 -0
  27. package/dist/lib/bytecode-encoder.d.ts +62 -0
  28. package/dist/lib/bytecode-encoder.js +281 -0
  29. package/dist/logging/index.d.ts +9 -0
  30. package/dist/logging/index.js +10 -0
  31. package/dist/metadata/index.d.ts +213 -0
  32. package/dist/metadata/index.js +296 -0
  33. package/dist/modules/accounts.d.ts +60 -0
  34. package/dist/modules/accounts.js +275 -0
  35. package/dist/modules/deploy.d.ts +90 -0
  36. package/dist/modules/deploy.js +1118 -0
  37. package/dist/modules/execute.d.ts +90 -0
  38. package/dist/modules/execute.js +649 -0
  39. package/dist/modules/fees.d.ts +14 -0
  40. package/dist/modules/fees.js +112 -0
  41. package/dist/modules/namespaces.d.ts +39 -0
  42. package/dist/modules/namespaces.js +190 -0
  43. package/dist/modules/state-diff.d.ts +35 -0
  44. package/dist/modules/state-diff.js +342 -0
  45. package/dist/modules/vm-state.d.ts +7 -0
  46. package/dist/modules/vm-state.js +44 -0
  47. package/dist/program/AccountResolver.d.ts +67 -0
  48. package/dist/program/AccountResolver.js +134 -0
  49. package/dist/program/BorshSchemaGenerator.d.ts +8 -0
  50. package/dist/program/BorshSchemaGenerator.js +57 -0
  51. package/dist/program/FiveProgram.d.ts +144 -0
  52. package/dist/program/FiveProgram.js +282 -0
  53. package/dist/program/FunctionBuilder.d.ts +114 -0
  54. package/dist/program/FunctionBuilder.js +347 -0
  55. package/dist/program/ProgramAccount.d.ts +38 -0
  56. package/dist/program/ProgramAccount.js +170 -0
  57. package/dist/program/TypeGenerator.d.ts +90 -0
  58. package/dist/program/TypeGenerator.js +195 -0
  59. package/dist/program/index.d.ts +24 -0
  60. package/dist/program/index.js +21 -0
  61. package/dist/project/config.d.ts +5 -0
  62. package/dist/project/config.js +33 -0
  63. package/dist/project/toml.d.ts +6 -0
  64. package/dist/project/toml.js +43 -0
  65. package/dist/project/workspace.d.ts +160 -0
  66. package/dist/project/workspace.js +73 -0
  67. package/dist/testing/AccountMetaGenerator.d.ts +121 -0
  68. package/dist/testing/AccountMetaGenerator.js +261 -0
  69. package/dist/testing/AccountTestFixture.d.ts +211 -0
  70. package/dist/testing/AccountTestFixture.js +530 -0
  71. package/dist/testing/OnChainAccountManager.d.ts +81 -0
  72. package/dist/testing/OnChainAccountManager.js +260 -0
  73. package/dist/testing/StateSerializer.d.ts +65 -0
  74. package/dist/testing/StateSerializer.js +330 -0
  75. package/dist/testing/TestDiscovery.d.ts +79 -0
  76. package/dist/testing/TestDiscovery.js +274 -0
  77. package/dist/testing/TestRunner.d.ts +117 -0
  78. package/dist/testing/TestRunner.js +346 -0
  79. package/dist/testing/index.d.ts +14 -0
  80. package/dist/testing/index.js +13 -0
  81. package/dist/types.d.ts +356 -0
  82. package/dist/types.js +32 -0
  83. package/dist/utils/abi.d.ts +31 -0
  84. package/dist/utils/abi.js +92 -0
  85. package/dist/utils/transaction.d.ts +5 -0
  86. package/dist/utils/transaction.js +48 -0
  87. package/dist/validation/InputValidator.d.ts +142 -0
  88. package/dist/validation/InputValidator.js +332 -0
  89. package/dist/validation/index.d.ts +4 -0
  90. package/dist/validation/index.js +4 -0
  91. package/dist/wasm/compiler/AbiLogic.d.ts +4 -0
  92. package/dist/wasm/compiler/AbiLogic.js +37 -0
  93. package/dist/wasm/compiler/AnalysisLogic.d.ts +6 -0
  94. package/dist/wasm/compiler/AnalysisLogic.js +61 -0
  95. package/dist/wasm/compiler/CompilationLogic.d.ts +10 -0
  96. package/dist/wasm/compiler/CompilationLogic.js +431 -0
  97. package/dist/wasm/compiler/FiveCompiler.d.ts +48 -0
  98. package/dist/wasm/compiler/FiveCompiler.js +183 -0
  99. package/dist/wasm/compiler/InfoLogic.d.ts +6 -0
  100. package/dist/wasm/compiler/InfoLogic.js +24 -0
  101. package/dist/wasm/compiler/OptimizationLogic.d.ts +2 -0
  102. package/dist/wasm/compiler/OptimizationLogic.js +13 -0
  103. package/dist/wasm/compiler/ValidationLogic.d.ts +7 -0
  104. package/dist/wasm/compiler/ValidationLogic.js +26 -0
  105. package/dist/wasm/compiler/index.d.ts +2 -0
  106. package/dist/wasm/compiler/index.js +2 -0
  107. package/dist/wasm/compiler/types.d.ts +8 -0
  108. package/dist/wasm/compiler/types.js +1 -0
  109. package/dist/wasm/compiler/utils.d.ts +8 -0
  110. package/dist/wasm/compiler/utils.js +75 -0
  111. package/dist/wasm/index.d.ts +9 -0
  112. package/dist/wasm/index.js +12 -0
  113. package/dist/wasm/instance.d.ts +1 -0
  114. package/dist/wasm/instance.js +26 -0
  115. package/dist/wasm/loader.d.ts +7 -0
  116. package/dist/wasm/loader.js +112 -0
  117. package/dist/wasm/vm.d.ts +33 -0
  118. package/dist/wasm/vm.js +250 -0
  119. package/package.json +59 -0
@@ -0,0 +1,346 @@
1
+ /**
2
+ * Five SDK Test Runner
3
+ *
4
+ * SDK-based test utilities that replace shell script approaches with programmatic
5
+ * Five SDK usage. Provides comprehensive testing capabilities for Five VM scripts.
6
+ */
7
+ import { readFile, readdir } from 'fs/promises';
8
+ import { join, basename } from 'path';
9
+ import { FiveSDK } from '../FiveSDK.js';
10
+ /**
11
+ * Five SDK-based test runner
12
+ */
13
+ export class FiveTestRunner {
14
+ constructor(options = {}) {
15
+ this.options = {
16
+ timeout: 30000,
17
+ maxComputeUnits: 1000000,
18
+ parallel: 0,
19
+ verbose: false,
20
+ debug: false,
21
+ trace: false,
22
+ pattern: '*',
23
+ failFast: false,
24
+ ...options
25
+ };
26
+ }
27
+ /**
28
+ * Run a single test case
29
+ */
30
+ async runTestCase(testCase) {
31
+ const startTime = Date.now();
32
+ try {
33
+ // Apply test name pattern filter
34
+ if (!this.matchesPattern(testCase.name, this.options.pattern)) {
35
+ return {
36
+ name: testCase.name,
37
+ passed: false,
38
+ duration: 0,
39
+ error: 'Skipped by pattern'
40
+ };
41
+ }
42
+ let bytecode;
43
+ // Get bytecode (compile source or load existing)
44
+ if (testCase.source) {
45
+ const result = await this.compileToBytecode(testCase.source);
46
+ if (!result.success || !result.bytecode) {
47
+ throw new Error(`Compilation failed: ${result.errors?.join(', ')}`);
48
+ }
49
+ bytecode = result.bytecode;
50
+ }
51
+ else if (testCase.bytecode) {
52
+ const data = await readFile(testCase.bytecode);
53
+ bytecode = new Uint8Array(data);
54
+ }
55
+ else {
56
+ throw new Error('Test case must specify either source or bytecode');
57
+ }
58
+ // Validate bytecode
59
+ const validation = await FiveSDK.validateBytecode(bytecode, {
60
+ debug: this.options.debug
61
+ });
62
+ if (!validation.valid) {
63
+ throw new Error(`Invalid bytecode: ${validation.errors?.join(', ')}`);
64
+ }
65
+ // Execute with timeout
66
+ const executionTimeout = testCase.timeout || this.options.timeout;
67
+ const executionPromise = FiveSDK.executeLocally(bytecode, testCase.function || 0, testCase.parameters || [], {
68
+ debug: this.options.debug,
69
+ trace: this.options.trace,
70
+ computeUnitLimit: this.options.maxComputeUnits
71
+ });
72
+ const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error('Test timeout')), executionTimeout));
73
+ const result = await Promise.race([executionPromise, timeoutPromise]);
74
+ const duration = Date.now() - startTime;
75
+ // Validate result
76
+ const passed = this.validateResult(result, testCase.expected);
77
+ return {
78
+ name: testCase.name,
79
+ passed,
80
+ duration,
81
+ computeUnitsUsed: result.computeUnitsUsed,
82
+ result: result.result,
83
+ logs: result.logs,
84
+ trace: result.trace,
85
+ error: passed ? undefined : this.getValidationError(result, testCase.expected)
86
+ };
87
+ }
88
+ catch (error) {
89
+ const duration = Date.now() - startTime;
90
+ // Check if error was expected
91
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error';
92
+ const passed = testCase.expected.success === false &&
93
+ testCase.expected.error !== undefined &&
94
+ errorMessage.includes(testCase.expected.error);
95
+ return {
96
+ name: testCase.name,
97
+ passed,
98
+ duration,
99
+ error: errorMessage
100
+ };
101
+ }
102
+ }
103
+ /**
104
+ * Run a complete test suite
105
+ */
106
+ async runTestSuite(suite) {
107
+ const startTime = Date.now();
108
+ const results = [];
109
+ try {
110
+ // Run setup if provided
111
+ if (suite.setup) {
112
+ await suite.setup();
113
+ }
114
+ // Run tests (serial or parallel)
115
+ if (this.options.parallel > 0) {
116
+ // Parallel execution
117
+ const chunks = this.chunkArray(suite.testCases, this.options.parallel);
118
+ for (const chunk of chunks) {
119
+ const chunkResults = await Promise.all(chunk.map(testCase => this.runTestCase(testCase)));
120
+ results.push(...chunkResults);
121
+ // Fail fast check
122
+ if (this.options.failFast && chunkResults.some(r => !r.passed)) {
123
+ break;
124
+ }
125
+ }
126
+ }
127
+ else {
128
+ // Serial execution
129
+ for (const testCase of suite.testCases) {
130
+ const result = await this.runTestCase(testCase);
131
+ results.push(result);
132
+ if (this.options.verbose) {
133
+ console.log(this.formatTestResult(result));
134
+ }
135
+ // Fail fast check
136
+ if (this.options.failFast && !result.passed) {
137
+ break;
138
+ }
139
+ }
140
+ }
141
+ // Run teardown if provided
142
+ if (suite.teardown) {
143
+ await suite.teardown();
144
+ }
145
+ }
146
+ catch (error) {
147
+ console.error(`Test suite setup/teardown error: ${error}`);
148
+ }
149
+ const duration = Date.now() - startTime;
150
+ const passed = results.filter(r => r.passed).length;
151
+ const failed = results.filter(r => !r.passed && r.error !== 'Skipped by pattern').length;
152
+ const skipped = results.filter(r => r.error === 'Skipped by pattern').length;
153
+ return {
154
+ suite,
155
+ results,
156
+ duration,
157
+ passed,
158
+ failed,
159
+ skipped
160
+ };
161
+ }
162
+ /**
163
+ * Run multiple test suites
164
+ */
165
+ async runTestSuites(suites) {
166
+ const results = [];
167
+ for (const suite of suites) {
168
+ if (this.options.verbose) {
169
+ console.log(`\n📋 Running test suite: ${suite.name}`);
170
+ if (suite.description) {
171
+ console.log(` ${suite.description}`);
172
+ }
173
+ }
174
+ const result = await this.runTestSuite(suite);
175
+ results.push(result);
176
+ if (this.options.verbose) {
177
+ console.log(this.formatSuiteResult(result));
178
+ }
179
+ // Fail fast for suites
180
+ if (this.options.failFast && result.failed > 0) {
181
+ break;
182
+ }
183
+ }
184
+ return results;
185
+ }
186
+ /**
187
+ * Discover and load test suites from directory
188
+ */
189
+ async discoverTestSuites(directory, pattern = '**/*.test.json') {
190
+ const testFiles = await this.findTestFiles(directory, pattern);
191
+ const suites = [];
192
+ for (const file of testFiles) {
193
+ try {
194
+ const content = await readFile(file, 'utf8');
195
+ const data = JSON.parse(content);
196
+ const suite = {
197
+ name: data.name || basename(file, '.test.json'),
198
+ description: data.description,
199
+ testCases: data.tests || data.testCases || []
200
+ };
201
+ suites.push(suite);
202
+ }
203
+ catch (error) {
204
+ console.warn(`Failed to load test suite ${file}: ${error}`);
205
+ }
206
+ }
207
+ return suites;
208
+ }
209
+ /**
210
+ * Compile and execute test - convenience method
211
+ */
212
+ static async compileAndTest(source, functionName, parameters = [], options = {}) {
213
+ try {
214
+ // Compile
215
+ const compilation = await FiveSDK.compile(source, {
216
+ debug: options.debug,
217
+ optimize: false
218
+ });
219
+ if (!compilation.success || !compilation.bytecode) {
220
+ return {
221
+ compilation,
222
+ execution: null,
223
+ success: false,
224
+ error: `Compilation failed: ${compilation.errors?.join(', ')}`
225
+ };
226
+ }
227
+ // Execute
228
+ const execution = await FiveSDK.executeLocally(compilation.bytecode, functionName || 0, parameters, options);
229
+ return {
230
+ compilation,
231
+ execution,
232
+ success: execution.success || false,
233
+ error: execution.error
234
+ };
235
+ }
236
+ catch (error) {
237
+ return {
238
+ compilation: null,
239
+ execution: null,
240
+ success: false,
241
+ error: error instanceof Error ? error.message : 'Unknown error'
242
+ };
243
+ }
244
+ }
245
+ // Private helper methods
246
+ async compileToBytecode(sourcePath) {
247
+ const source = await readFile(sourcePath, 'utf8');
248
+ return FiveSDK.compile(source, { debug: this.options.debug });
249
+ }
250
+ validateResult(result, expected) {
251
+ // Check success/failure
252
+ if (result.success !== expected.success) {
253
+ return false;
254
+ }
255
+ // If expecting success, check result value
256
+ if (expected.success && expected.result !== undefined) {
257
+ if (JSON.stringify(result.result) !== JSON.stringify(expected.result)) {
258
+ return false;
259
+ }
260
+ }
261
+ // Check compute units constraints
262
+ if (expected.maxComputeUnits && result.computeUnitsUsed > expected.maxComputeUnits) {
263
+ return false;
264
+ }
265
+ if (expected.minComputeUnits && result.computeUnitsUsed < expected.minComputeUnits) {
266
+ return false;
267
+ }
268
+ return true;
269
+ }
270
+ getValidationError(result, expected) {
271
+ const errors = [];
272
+ if (result.success !== expected.success) {
273
+ errors.push(`Expected success=${expected.success}, got ${result.success}`);
274
+ }
275
+ if (expected.success && expected.result !== undefined) {
276
+ if (JSON.stringify(result.result) !== JSON.stringify(expected.result)) {
277
+ errors.push(`Expected result ${JSON.stringify(expected.result)}, got ${JSON.stringify(result.result)}`);
278
+ }
279
+ }
280
+ if (expected.maxComputeUnits && result.computeUnitsUsed > expected.maxComputeUnits) {
281
+ errors.push(`Exceeded max compute units: ${result.computeUnitsUsed} > ${expected.maxComputeUnits}`);
282
+ }
283
+ if (expected.minComputeUnits && result.computeUnitsUsed < expected.minComputeUnits) {
284
+ errors.push(`Below min compute units: ${result.computeUnitsUsed} < ${expected.minComputeUnits}`);
285
+ }
286
+ return errors.join('; ');
287
+ }
288
+ matchesPattern(name, pattern) {
289
+ if (pattern === '*')
290
+ return true;
291
+ const regex = new RegExp(pattern.replace(/\*/g, '.*').replace(/\?/g, '.'), 'i');
292
+ return regex.test(name);
293
+ }
294
+ chunkArray(array, chunkSize) {
295
+ const chunks = [];
296
+ for (let i = 0; i < array.length; i += chunkSize) {
297
+ chunks.push(array.slice(i, i + chunkSize));
298
+ }
299
+ return chunks;
300
+ }
301
+ async findTestFiles(directory, pattern) {
302
+ const files = [];
303
+ try {
304
+ const entries = await readdir(directory, { withFileTypes: true });
305
+ for (const entry of entries) {
306
+ const fullPath = join(directory, entry.name);
307
+ if (entry.isDirectory()) {
308
+ const subFiles = await this.findTestFiles(fullPath, pattern);
309
+ files.push(...subFiles);
310
+ }
311
+ else if (entry.isFile() && entry.name.endsWith('.test.json')) {
312
+ files.push(fullPath);
313
+ }
314
+ }
315
+ }
316
+ catch (error) {
317
+ // Directory doesn't exist or not accessible
318
+ }
319
+ return files;
320
+ }
321
+ formatTestResult(result) {
322
+ const icon = result.passed ? '✅' : '❌';
323
+ const duration = `${result.duration}ms`;
324
+ const cu = result.computeUnitsUsed ? `${result.computeUnitsUsed} CU` : '';
325
+ let line = `${icon} ${result.name} (${duration}`;
326
+ if (cu)
327
+ line += `, ${cu}`;
328
+ line += ')';
329
+ if (!result.passed && result.error) {
330
+ line += `\n Error: ${result.error}`;
331
+ }
332
+ return line;
333
+ }
334
+ formatSuiteResult(result) {
335
+ const { passed, failed, skipped } = result;
336
+ const total = passed + failed + skipped;
337
+ const duration = `${result.duration}ms`;
338
+ let line = `📊 Results: ${passed}/${total} passed`;
339
+ if (failed > 0)
340
+ line += `, ${failed} failed`;
341
+ if (skipped > 0)
342
+ line += `, ${skipped} skipped`;
343
+ line += ` (${duration})`;
344
+ return line;
345
+ }
346
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Five SDK Testing Utilities
3
+ *
4
+ * Export all testing utilities for easy access from CLI and other modules.
5
+ */
6
+ export { FiveTestRunner } from './TestRunner.js';
7
+ export type { TestCase, TestSuite, TestResult, TestSuiteResult, TestRunnerOptions } from './TestRunner.js';
8
+ export { TestDiscovery } from './TestDiscovery.js';
9
+ export type { VSourceTest, CompiledTestCase, DiscoveredTest } from './TestDiscovery.js';
10
+ export { AccountTestFixture, FixtureTemplates, AccountTestExecutor } from './AccountTestFixture.js';
11
+ export type { FixtureAccountSpec, CompiledFixture, ConstraintValidationResult, AccountExecutionContext, AccountStateTemplate } from './AccountTestFixture.js';
12
+ export { AccountMetaGenerator, AccountTestUtils } from './AccountMetaGenerator.js';
13
+ export type { GeneratedAccountMeta, AccountConstraints, TestAccountContext } from './AccountMetaGenerator.js';
14
+ export { FiveSDK } from '../FiveSDK.js';
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Five SDK Testing Utilities
3
+ *
4
+ * Export all testing utilities for easy access from CLI and other modules.
5
+ */
6
+ export { FiveTestRunner } from './TestRunner.js';
7
+ // Test discovery utilities
8
+ export { TestDiscovery } from './TestDiscovery.js';
9
+ // Account testing utilities
10
+ export { AccountTestFixture, FixtureTemplates, AccountTestExecutor } from './AccountTestFixture.js';
11
+ export { AccountMetaGenerator, AccountTestUtils } from './AccountMetaGenerator.js';
12
+ // Re-export for convenience
13
+ export { FiveSDK } from '../FiveSDK.js';