stigmergy 1.1.6 → 1.2.6

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 (118) hide show
  1. package/LICENSE +18 -18
  2. package/README.md +28 -223
  3. package/STIGMERGY.md +61 -61
  4. package/docs/PROJECT_CONSTITUTION.md +433 -433
  5. package/docs/PROJECT_STRUCTURE_CURRENT.md +80 -80
  6. package/examples/calculator-example.js +72 -72
  7. package/examples/cline_usage_examples.md +364 -364
  8. package/examples/encryption-example.js +67 -67
  9. package/examples/json-parser-example.js +120 -120
  10. package/examples/json-validation-example.js +64 -64
  11. package/examples/rest-client-example.js +52 -52
  12. package/examples/rest_client_example.js +54 -54
  13. package/package.json +15 -7
  14. package/scripts/build.js +74 -74
  15. package/scripts/post-deployment-config.js +296 -296
  16. package/scripts/preinstall-check.js +173 -111
  17. package/scripts/publish.js +58 -268
  18. package/scripts/run-layered-tests.js +247 -0
  19. package/scripts/safe-install.js +139 -139
  20. package/scripts/simple-publish.js +57 -59
  21. package/src/adapters/claude/install_claude_integration.js +292 -0
  22. package/src/adapters/codebuddy/install_codebuddy_integration.js +349 -0
  23. package/src/adapters/codex/install_codex_integration.js +395 -0
  24. package/src/adapters/copilot/install_copilot_integration.js +716 -0
  25. package/src/adapters/gemini/install_gemini_integration.js +304 -0
  26. package/src/adapters/iflow/install_iflow_integration.js +304 -0
  27. package/src/adapters/qoder/install_qoder_integration.js +1090 -0
  28. package/src/adapters/qwen/install_qwen_integration.js +285 -0
  29. package/src/auth.js +173 -173
  30. package/src/auth_command.js +208 -208
  31. package/src/calculator.js +313 -313
  32. package/src/cli/router.js +792 -67
  33. package/src/core/cache_cleaner.js +767 -0
  34. package/src/core/cli_help_analyzer.js +680 -674
  35. package/src/core/cli_parameter_handler.js +132 -127
  36. package/src/core/cli_tools.js +89 -89
  37. package/src/core/coordination/index.js +16 -16
  38. package/src/core/coordination/nodejs/AdapterManager.js +102 -89
  39. package/src/core/coordination/nodejs/CLCommunication.js +132 -124
  40. package/src/core/coordination/nodejs/CLIIntegrationManager.js +272 -236
  41. package/src/core/coordination/nodejs/HealthChecker.js +76 -77
  42. package/src/core/coordination/nodejs/HookDeploymentManager.js +263 -190
  43. package/src/core/coordination/nodejs/StatisticsCollector.js +71 -71
  44. package/src/core/coordination/nodejs/index.js +90 -72
  45. package/src/core/coordination/nodejs/utils/Logger.js +29 -29
  46. package/src/core/enhanced_installer.js +479 -0
  47. package/src/core/enhanced_uninstaller.js +638 -0
  48. package/src/core/error_handler.js +406 -406
  49. package/src/core/installer.js +816 -294
  50. package/src/core/memory_manager.js +83 -83
  51. package/src/core/rest_client.js +160 -160
  52. package/src/core/smart_router.js +249 -146
  53. package/src/core/upgrade_manager.js +420 -0
  54. package/src/data_encryption.js +143 -143
  55. package/src/data_structures.js +440 -440
  56. package/src/deploy.js +55 -55
  57. package/src/index.js +30 -30
  58. package/src/test/cli-availability-checker.js +194 -0
  59. package/src/test/test-environment.js +289 -0
  60. package/src/utils/helpers.js +35 -35
  61. package/src/utils.js +921 -915
  62. package/src/weatherProcessor.js +228 -228
  63. package/test/calculator.test.js +0 -215
  64. package/test/collision-test.js +0 -26
  65. package/test/comprehensive-execution-test.js +0 -428
  66. package/test/conflict-prevention-test.js +0 -95
  67. package/test/cross-cli-detection-test.js +0 -33
  68. package/test/csv-processing-test.js +0 -36
  69. package/test/deploy-hooks-test.js +0 -250
  70. package/test/e2e/claude-cli-test.js +0 -128
  71. package/test/e2e/collaboration-test.js +0 -75
  72. package/test/e2e/comprehensive-test.js +0 -431
  73. package/test/e2e/error-handling-test.js +0 -90
  74. package/test/e2e/individual-tool-test.js +0 -143
  75. package/test/e2e/other-cli-test.js +0 -130
  76. package/test/e2e/qoder-cli-test.js +0 -128
  77. package/test/e2e/run-e2e-tests.js +0 -73
  78. package/test/e2e/test-data.js +0 -88
  79. package/test/e2e/test-utils.js +0 -222
  80. package/test/encryption-simple-test.js +0 -110
  81. package/test/encryption.test.js +0 -129
  82. package/test/enhanced-main-alignment.test.js +0 -298
  83. package/test/error-handling-test.js +0 -341
  84. package/test/fibonacci.test.js +0 -178
  85. package/test/final-deploy-test.js +0 -221
  86. package/test/final-install-test.js +0 -226
  87. package/test/hash-table-demo.js +0 -33
  88. package/test/hash-table-test.js +0 -26
  89. package/test/hash_table_test.js +0 -114
  90. package/test/hook-system-integration-test.js +0 -307
  91. package/test/iflow-integration-test.js +0 -292
  92. package/test/improved-install-test.js +0 -362
  93. package/test/install-command-test.js +0 -370
  94. package/test/json-parser-test.js +0 -161
  95. package/test/json-validation-test.js +0 -164
  96. package/test/natural-language-skills-test.js +0 -320
  97. package/test/nl-integration-test.js +0 -179
  98. package/test/parameter-parsing-test.js +0 -143
  99. package/test/plugin-deployment-test.js +0 -316
  100. package/test/postinstall-test.js +0 -269
  101. package/test/python-plugins-test.js +0 -259
  102. package/test/real-test.js +0 -435
  103. package/test/remaining-adapters-test.js +0 -256
  104. package/test/rest-client-test.js +0 -56
  105. package/test/rest_client.test.js +0 -85
  106. package/test/simple-iflow-hook-test.js +0 -137
  107. package/test/system-compatibility-test.js +0 -467
  108. package/test/tdd-deploy-fix-test.js +0 -324
  109. package/test/tdd-fixes-test.js +0 -211
  110. package/test/third-party-skills-test.js +0 -321
  111. package/test/tool-selection-integration-test.js +0 -158
  112. package/test/unit/calculator-full.test.js +0 -191
  113. package/test/unit/calculator-simple.test.js +0 -96
  114. package/test/unit/calculator.test.js +0 -97
  115. package/test/unit/cli-scanner.test.js +0 -291
  116. package/test/unit/cli_parameter_handler.test.js +0 -116
  117. package/test/unit/cross-cli-executor.test.js +0 -399
  118. package/test/weather-processor.test.js +0 -104
@@ -1,370 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Test for CLI Installation Command Execution
5
- * This test verifies that the installation commands work correctly
6
- * and identifies potential issues with shell execution
7
- */
8
-
9
- const { spawnSync } = require('child_process');
10
- const path = require('path');
11
- const os = require('os');
12
-
13
- class InstallCommandTester {
14
- constructor() {
15
- this.testResults = [];
16
- }
17
-
18
- // Test 1: Verify Qoder CLI installation command format
19
- async testQoderInstallCommandFormat() {
20
- console.log('[TEST 1] Verifying Qoder CLI installation command format...');
21
-
22
- try {
23
- // Check the installation command in the configuration
24
- const installCommand = 'npm install -g @qoder-ai/qodercli';
25
- console.log(` Installation command: ${installCommand}`);
26
-
27
- // Verify it's a valid npm command
28
- const isValidNpmCommand = installCommand.startsWith('npm install -g');
29
- console.log(` Valid npm command: ${isValidNpmCommand}`);
30
-
31
- // Check if package name is valid
32
- const packageName = installCommand.split(' ')[3]; // @qoder-ai/qodercli
33
- const hasValidPackageName = packageName && packageName.includes('@') && packageName.includes('/');
34
- console.log(` Valid package name: ${hasValidPackageName} (${packageName})`);
35
-
36
- this.testResults.push({
37
- name: 'Qoder Install Command Format',
38
- passed: isValidNpmCommand && hasValidPackageName,
39
- details: `Command: ${installCommand}, Package: ${packageName}`
40
- });
41
-
42
- return isValidNpmCommand && hasValidPackageName;
43
- } catch (error) {
44
- console.log(` ✗ Failed to check install command format: ${error.message}`);
45
- this.testResults.push({
46
- name: 'Qoder Install Command Format',
47
- passed: false,
48
- details: `Failed to check: ${error.message}`
49
- });
50
- return false;
51
- }
52
- }
53
-
54
- // Test 2: Verify command execution environment
55
- async testCommandExecutionEnvironment() {
56
- console.log('\n[TEST 2] Verifying command execution environment...');
57
-
58
- try {
59
- // Check if npm is available
60
- const npmCheck = spawnSync('npm', ['--version'], {
61
- encoding: 'utf8',
62
- timeout: 5000
63
- });
64
-
65
- const npmAvailable = npmCheck.status === 0;
66
- console.log(` npm available: ${npmAvailable}`);
67
- if (npmAvailable) {
68
- console.log(` npm version: ${npmCheck.stdout.trim()}`);
69
- }
70
-
71
- // Check if we're on Windows
72
- const isWindows = process.platform === 'win32';
73
- console.log(` Platform: ${process.platform} (Windows: ${isWindows})`);
74
-
75
- // Check NODE_PATH and PATH environment variables
76
- const hasNodePath = !!process.env.NODE_PATH;
77
- const pathLength = process.env.PATH ? process.env.PATH.length : 0;
78
- console.log(` NODE_PATH set: ${hasNodePath}`);
79
- console.log(` PATH length: ${pathLength} characters`);
80
-
81
- this.testResults.push({
82
- name: 'Command Execution Environment',
83
- passed: npmAvailable,
84
- details: `npm: ${npmAvailable}, Platform: ${process.platform}, PATH length: ${pathLength}`
85
- });
86
-
87
- return npmAvailable;
88
- } catch (error) {
89
- console.log(` ✗ Failed to check execution environment: ${error.message}`);
90
- this.testResults.push({
91
- name: 'Command Execution Environment',
92
- passed: false,
93
- details: `Failed to check: ${error.message}`
94
- });
95
- return false;
96
- }
97
- }
98
-
99
- // Test 3: Simulate installation command execution issues
100
- async testInstallationCommandExecution() {
101
- console.log('\n[TEST 3] Simulating installation command execution...');
102
-
103
- try {
104
- // Test a simple npm command first
105
- console.log(' Testing basic npm command...');
106
- const simpleNpmTest = spawnSync('npm', ['help'], {
107
- encoding: 'utf8',
108
- timeout: 10000,
109
- stdio: 'pipe'
110
- });
111
-
112
- console.log(` Simple npm test - Status: ${simpleNpmTest.status}`);
113
- if (simpleNpmTest.stderr && simpleNpmTest.stderr.length > 0) {
114
- console.log(` Simple npm test - Stderr length: ${simpleNpmTest.stderr.length}`);
115
- }
116
-
117
- // Test the actual Qoder installation command (dry run)
118
- console.log(' Testing Qoder installation command (dry run)...');
119
- const installTest = spawnSync('npm', ['install', '@qoder-ai/qodercli', '--dry-run'], {
120
- encoding: 'utf8',
121
- timeout: 15000,
122
- stdio: 'pipe'
123
- });
124
-
125
- console.log(` Qoder install test - Status: ${installTest.status}`);
126
- if (installTest.stdout && installTest.stdout.length > 0) {
127
- const stdoutPreview = installTest.stdout.substring(0, 200);
128
- console.log(` Qoder install test - Stdout preview: ${stdoutPreview}${installTest.stdout.length > 200 ? '...' : ''}`);
129
- }
130
-
131
- if (installTest.stderr && installTest.stderr.length > 0) {
132
- const stderrPreview = installTest.stderr.substring(0, 200);
133
- console.log(` Qoder install test - Stderr preview: ${stderrPreview}${installTest.stderr.length > 200 ? '...' : ''}`);
134
-
135
- // Check for common Windows-related errors
136
- const hasWindowsError = installTest.stderr.includes('spawn') ||
137
- installTest.stderr.includes('ENOENT') ||
138
- installTest.stderr.includes('not found');
139
- if (hasWindowsError) {
140
- console.log(' ⚠ Potential Windows spawn error detected');
141
- }
142
- }
143
-
144
- // Check if the command at least started (status might not be 0 for dry-run)
145
- const commandStarted = installTest.status !== null;
146
-
147
- this.testResults.push({
148
- name: 'Installation Command Execution',
149
- passed: commandStarted,
150
- details: `Status: ${installTest.status}, Started: ${commandStarted}`
151
- });
152
-
153
- return commandStarted;
154
- } catch (error) {
155
- console.log(` ✗ Failed to test installation command execution: ${error.message}`);
156
- this.testResults.push({
157
- name: 'Installation Command Execution',
158
- passed: false,
159
- details: `Failed to test: ${error.message}`
160
- });
161
- return false;
162
- }
163
- }
164
-
165
- // Test 4: Check for Windows-specific shell issues
166
- async testWindowsShellIssues() {
167
- console.log('\n[TEST 4] Checking for Windows-specific shell issues...');
168
-
169
- try {
170
- const isWindows = process.platform === 'win32';
171
- console.log(` Platform: ${process.platform} (Windows: ${isWindows})`);
172
-
173
- if (!isWindows) {
174
- console.log(' ✓ Not on Windows, skipping Windows-specific tests');
175
- this.testResults.push({
176
- name: 'Windows Shell Issues',
177
- passed: true,
178
- details: 'Not on Windows platform'
179
- });
180
- return true;
181
- }
182
-
183
- // Test different shell execution methods
184
- console.log(' Testing cmd.exe execution...');
185
- const cmdTest = spawnSync('cmd.exe', ['/c', 'echo', 'test'], {
186
- encoding: 'utf8',
187
- timeout: 5000
188
- });
189
-
190
- const cmdWorks = cmdTest.status === 0;
191
- console.log(` cmd.exe works: ${cmdWorks}`);
192
-
193
- console.log(' Testing PowerShell execution...');
194
- const psTest = spawnSync('powershell.exe', ['-Command', 'Write-Output "test"'], {
195
- encoding: 'utf8',
196
- timeout: 10000
197
- });
198
-
199
- const psWorks = psTest.status === 0;
200
- console.log(` PowerShell works: ${psWorks}`);
201
-
202
- // Test npm through cmd.exe
203
- console.log(' Testing npm through cmd.exe...');
204
- const npmCmdTest = spawnSync('cmd.exe', ['/c', 'npm', '--version'], {
205
- encoding: 'utf8',
206
- timeout: 10000
207
- });
208
-
209
- const npmThroughCmd = npmCmdTest.status === 0;
210
- console.log(` npm through cmd.exe: ${npmThroughCmd}`);
211
-
212
- this.testResults.push({
213
- name: 'Windows Shell Issues',
214
- passed: cmdWorks && psWorks,
215
- details: `cmd.exe: ${cmdWorks}, PowerShell: ${psWorks}, npm through cmd: ${npmThroughCmd}`
216
- });
217
-
218
- return cmdWorks && psWorks;
219
- } catch (error) {
220
- console.log(` ✗ Failed to check Windows shell issues: ${error.message}`);
221
- this.testResults.push({
222
- name: 'Windows Shell Issues',
223
- passed: false,
224
- details: `Failed to check: ${error.message}`
225
- });
226
- return false;
227
- }
228
- }
229
-
230
- // Test 5: Verify spawnSync vs spawn behavior
231
- async testSpawnBehavior() {
232
- console.log('\n[TEST 5] Verifying spawn behavior differences...');
233
-
234
- try {
235
- // Test spawnSync (currently used in the code)
236
- console.log(' Testing spawnSync behavior...');
237
- const syncResult = spawnSync('npm', ['--version'], {
238
- encoding: 'utf8',
239
- timeout: 5000
240
- });
241
-
242
- console.log(` spawnSync - Status: ${syncResult.status}`);
243
-
244
- // Test spawn (alternative approach)
245
- console.log(' Testing spawn behavior...');
246
- const spawnResult = await new Promise((resolve) => {
247
- const child = require('child_process').spawn('npm', ['--version'], {
248
- encoding: 'utf8',
249
- timeout: 5000
250
- });
251
-
252
- let stdout = '';
253
- let stderr = '';
254
-
255
- child.stdout.on('data', (data) => {
256
- stdout += data.toString();
257
- });
258
-
259
- child.stderr.on('data', (data) => {
260
- stderr += data.toString();
261
- });
262
-
263
- child.on('close', (code) => {
264
- resolve({
265
- status: code,
266
- stdout: stdout,
267
- stderr: stderr
268
- });
269
- });
270
-
271
- child.on('error', (error) => {
272
- resolve({
273
- status: null,
274
- error: error.message
275
- });
276
- });
277
-
278
- // Timeout handling
279
- setTimeout(() => {
280
- resolve({
281
- status: null,
282
- error: 'Timeout'
283
- });
284
- }, 6000);
285
- });
286
-
287
- console.log(` spawn - Status: ${spawnResult.status}`);
288
- if (spawnResult.error) {
289
- console.log(` spawn - Error: ${spawnResult.error}`);
290
- }
291
-
292
- const bothWork = syncResult.status === 0 && spawnResult.status === 0;
293
-
294
- this.testResults.push({
295
- name: 'Spawn Behavior',
296
- passed: bothWork,
297
- details: `spawnSync: ${syncResult.status}, spawn: ${spawnResult.status}`
298
- });
299
-
300
- return bothWork;
301
- } catch (error) {
302
- console.log(` ✗ Failed to test spawn behavior: ${error.message}`);
303
- this.testResults.push({
304
- name: 'Spawn Behavior',
305
- passed: false,
306
- details: `Failed to test: ${error.message}`
307
- });
308
- return false;
309
- }
310
- }
311
-
312
- // Run all tests
313
- async runAllTests() {
314
- console.log('CLI Installation Command Execution Test');
315
- console.log('='.repeat(50));
316
-
317
- await this.testQoderInstallCommandFormat();
318
- await this.testCommandExecutionEnvironment();
319
- await this.testInstallationCommandExecution();
320
- await this.testWindowsShellIssues();
321
- await this.testSpawnBehavior();
322
-
323
- // Summary
324
- console.log('\n' + '='.repeat(50));
325
- console.log('Installation Command Test Summary:');
326
- console.log('='.repeat(50));
327
-
328
- let passedTests = 0;
329
- this.testResults.forEach(result => {
330
- console.log(`${result.name}: ${result.passed ? '✓ PASS' : '✗ FAIL'} - ${result.details}`);
331
- if (result.passed) passedTests++;
332
- });
333
-
334
- console.log(`\nOverall Result: ${passedTests}/${this.testResults.length} tests passed`);
335
-
336
- if (passedTests === this.testResults.length) {
337
- console.log('✓ All installation command tests passed!');
338
- } else if (passedTests > 0) {
339
- console.log('⚠ Some installation command tests failed.');
340
- } else {
341
- console.log('✗ All installation command tests failed.');
342
- }
343
-
344
- return {
345
- totalTests: this.testResults.length,
346
- passedTests: passedTests,
347
- results: this.testResults
348
- };
349
- }
350
- }
351
-
352
- // Run the tests
353
- async function runInstallCommandTests() {
354
- const tester = new InstallCommandTester();
355
- const results = await tester.runAllTests();
356
- return results;
357
- }
358
-
359
- // Export for use in other modules
360
- module.exports = { InstallCommandTester };
361
-
362
- // Run if called directly
363
- if (require.main === module) {
364
- runInstallCommandTests().then(results => {
365
- process.exit(results.passedTests === results.totalTests ? 0 : 1);
366
- }).catch(error => {
367
- console.error('[Test Failed]:', error.message);
368
- process.exit(1);
369
- });
370
- }
@@ -1,161 +0,0 @@
1
- const { parseAndValidateJSON } = require('../src/utils');
2
-
3
- // Test cases for the JSON parser and validator
4
-
5
- console.log('Testing JSON Parser and Validator...\n');
6
-
7
- // Test 1: Valid JSON without schema
8
- try {
9
- const jsonData = '{"name": "John", "age": 30, "city": "New York"}';
10
- const result = parseAndValidateJSON(jsonData);
11
- console.log('Test 1 - Parse valid JSON without schema: PASSED');
12
- console.log('Result:', result);
13
- } catch (error) {
14
- console.log('Test 1 - Parse valid JSON without schema: FAILED');
15
- console.log('Error:', error.message);
16
- }
17
-
18
- console.log('\n---\n');
19
-
20
- // Test 2: Invalid JSON
21
- try {
22
- const invalidJson = '{"name": "John", "age": 30, "city": "New York"'; // Missing closing brace
23
- const result = parseAndValidateJSON(invalidJson);
24
- console.log('Test 2 - Parse invalid JSON: FAILED (should have thrown error)');
25
- } catch (error) {
26
- console.log('Test 2 - Parse invalid JSON: PASSED');
27
- console.log('Error:', error.message);
28
- }
29
-
30
- console.log('\n---\n');
31
-
32
- // Test 3: Valid JSON with schema validation
33
- try {
34
- const jsonData = '{"name": "John", "age": 30, "email": "john@example.com"}';
35
- const schema = {
36
- required: ["name", "age"],
37
- properties: {
38
- name: { type: "string" },
39
- age: { type: "number", minimum: 0, maximum: 120 },
40
- email: { type: "string" }
41
- }
42
- };
43
- const result = parseAndValidateJSON(jsonData, schema);
44
- console.log('Test 3 - Valid JSON with schema: PASSED');
45
- console.log('Result:', result);
46
- } catch (error) {
47
- console.log('Test 3 - Valid JSON with schema: FAILED');
48
- console.log('Error:', error.message);
49
- }
50
-
51
- console.log('\n---\n');
52
-
53
- // Test 4: Valid JSON with schema validation - missing required field
54
- try {
55
- const jsonData = '{"name": "John", "email": "john@example.com"}'; // Missing age
56
- const schema = {
57
- required: ["name", "age"],
58
- properties: {
59
- name: { type: "string" },
60
- age: { type: "number" },
61
- email: { type: "string" }
62
- }
63
- };
64
- const result = parseAndValidateJSON(jsonData, schema);
65
- console.log('Test 4 - Missing required field: FAILED (should have thrown error)');
66
- } catch (error) {
67
- console.log('Test 4 - Missing required field: PASSED');
68
- console.log('Error:', error.message);
69
- }
70
-
71
- console.log('\n---\n');
72
-
73
- // Test 5: Valid JSON with schema validation - wrong data type
74
- try {
75
- const jsonData = '{"name": "John", "age": "thirty", "email": "john@example.com"}'; // Age is string instead of number
76
- const schema = {
77
- required: ["name", "age"],
78
- properties: {
79
- name: { type: "string" },
80
- age: { type: "number" },
81
- email: { type: "string" }
82
- }
83
- };
84
- const result = parseAndValidateJSON(jsonData, schema);
85
- console.log('Test 5 - Wrong data type: FAILED (should have thrown error)');
86
- } catch (error) {
87
- console.log('Test 5 - Wrong data type: PASSED');
88
- console.log('Error:', error.message);
89
- }
90
-
91
- console.log('\n---\n');
92
-
93
- // Test 6: Valid JSON with schema validation - number out of range
94
- try {
95
- const jsonData = '{"name": "John", "age": 150, "email": "john@example.com"}'; // Age > 120
96
- const schema = {
97
- required: ["name", "age"],
98
- properties: {
99
- name: { type: "string" },
100
- age: { type: "number", minimum: 0, maximum: 120 },
101
- email: { type: "string" }
102
- }
103
- };
104
- const result = parseAndValidateJSON(jsonData, schema);
105
- console.log('Test 6 - Number out of range: FAILED (should have thrown error)');
106
- } catch (error) {
107
- console.log('Test 6 - Number out of range: PASSED');
108
- console.log('Error:', error.message);
109
- }
110
-
111
- console.log('\n---\n');
112
-
113
- // Test 7: Valid JSON with array validation
114
- try {
115
- const jsonData = '{"name": "John", "hobbies": ["reading", "swimming", "coding"]}';
116
- const schema = {
117
- required: ["name"],
118
- properties: {
119
- name: { type: "string" },
120
- hobbies: {
121
- type: "array",
122
- items: { type: "string" },
123
- minItems: 1,
124
- maxItems: 5
125
- }
126
- }
127
- };
128
- const result = parseAndValidateJSON(jsonData, schema);
129
- console.log('Test 7 - Array validation: PASSED');
130
- console.log('Result:', result);
131
- } catch (error) {
132
- console.log('Test 7 - Array validation: FAILED');
133
- console.log('Error:', error.message);
134
- }
135
-
136
- console.log('\n---\n');
137
-
138
- // Test 8: Nested object validation
139
- try {
140
- const jsonData = '{"name": "John", "address": {"street": "123 Main St", "city": "New York"}}';
141
- const schema = {
142
- required: ["name"],
143
- properties: {
144
- name: { type: "string" },
145
- address: {
146
- type: "object",
147
- properties: {
148
- street: { type: "string" },
149
- city: { type: "string" }
150
- },
151
- required: ["street", "city"]
152
- }
153
- }
154
- };
155
- const result = parseAndValidateJSON(jsonData, schema);
156
- console.log('Test 8 - Nested object validation: PASSED');
157
- console.log('Result:', result);
158
- } catch (error) {
159
- console.log('Test 8 - Nested object validation: FAILED');
160
- console.log('Error:', error.message);
161
- }
@@ -1,164 +0,0 @@
1
- const { parseAndValidateJSON } = require('../src/utils');
2
-
3
- // Test cases for parseAndValidateJSON function
4
- console.log('Testing parseAndValidateJSON function...\n');
5
-
6
- // Test 1: Valid JSON without schema
7
- try {
8
- const jsonData = '{"name": "John", "age": 30}';
9
- const result = parseAndValidateJSON(jsonData);
10
- console.log('Test 1 passed: Valid JSON parsed successfully');
11
- console.log('Result:', result);
12
- } catch (error) {
13
- console.log('Test 1 failed:', error.message);
14
- }
15
-
16
- console.log();
17
-
18
- // Test 2: Invalid JSON
19
- try {
20
- const invalidJson = '{"name": "John", "age":}';
21
- const result = parseAndValidateJSON(invalidJson);
22
- console.log('Test 2 failed: Should have thrown an error');
23
- } catch (error) {
24
- console.log('Test 2 passed: Correctly rejected invalid JSON');
25
- console.log('Error:', error.message);
26
- }
27
-
28
- console.log();
29
-
30
- // Test 3: Valid JSON with schema validation
31
- try {
32
- const jsonData = '{"name": "John", "age": 30, "active": true}';
33
- const schema = {
34
- required: ['name', 'age'],
35
- properties: {
36
- name: { type: 'string' },
37
- age: { type: 'number' },
38
- active: { type: 'boolean' }
39
- }
40
- };
41
- const result = parseAndValidateJSON(jsonData, schema);
42
- console.log('Test 3 passed: Valid JSON with schema validation');
43
- console.log('Result:', result);
44
- } catch (error) {
45
- console.log('Test 3 failed:', error.message);
46
- }
47
-
48
- console.log();
49
-
50
- // Test 4: Missing required field
51
- try {
52
- const jsonData = '{"name": "John"}';
53
- const schema = {
54
- required: ['name', 'age'],
55
- properties: {
56
- name: { type: 'string' },
57
- age: { type: 'number' }
58
- }
59
- };
60
- const result = parseAndValidateJSON(jsonData, schema);
61
- console.log('Test 4 failed: Should have thrown an error for missing required field');
62
- } catch (error) {
63
- console.log('Test 4 passed: Correctly rejected missing required field');
64
- console.log('Error:', error.message);
65
- }
66
-
67
- console.log();
68
-
69
- // Test 5: Wrong data type
70
- try {
71
- const jsonData = '{"name": "John", "age": "thirty"}';
72
- const schema = {
73
- required: ['name', 'age'],
74
- properties: {
75
- name: { type: 'string' },
76
- age: { type: 'number' }
77
- }
78
- };
79
- const result = parseAndValidateJSON(jsonData, schema);
80
- console.log('Test 5 failed: Should have thrown an error for wrong data type');
81
- } catch (error) {
82
- console.log('Test 5 passed: Correctly rejected wrong data type');
83
- console.log('Error:', error.message);
84
- }
85
-
86
- console.log();
87
-
88
- // Test 6: Enum validation
89
- try {
90
- const jsonData = '{"status": "active"}';
91
- const schema = {
92
- required: ['status'],
93
- properties: {
94
- status: { type: 'string', enum: ['active', 'inactive', 'pending'] }
95
- }
96
- };
97
- const result = parseAndValidateJSON(jsonData, schema);
98
- console.log('Test 6 passed: Valid enum value accepted');
99
- console.log('Result:', result);
100
- } catch (error) {
101
- console.log('Test 6 failed:', error.message);
102
- }
103
-
104
- console.log();
105
-
106
- // Test 7: Invalid enum value
107
- try {
108
- const jsonData = '{"status": "deleted"}';
109
- const schema = {
110
- required: ['status'],
111
- properties: {
112
- status: { type: 'string', enum: ['active', 'inactive', 'pending'] }
113
- }
114
- };
115
- const result = parseAndValidateJSON(jsonData, schema);
116
- console.log('Test 7 failed: Should have thrown an error for invalid enum value');
117
- } catch (error) {
118
- console.log('Test 7 passed: Correctly rejected invalid enum value');
119
- console.log('Error:', error.message);
120
- }
121
-
122
- console.log();
123
-
124
- // Test 8: Array validation
125
- try {
126
- const jsonData = '{"tags": ["javascript", "nodejs"]}';
127
- const schema = {
128
- required: ['tags'],
129
- properties: {
130
- tags: {
131
- type: 'array',
132
- items: { type: 'string' }
133
- }
134
- }
135
- };
136
- const result = parseAndValidateJSON(jsonData, schema);
137
- console.log('Test 8 passed: Valid array accepted');
138
- console.log('Result:', result);
139
- } catch (error) {
140
- console.log('Test 8 failed:', error.message);
141
- }
142
-
143
- console.log();
144
-
145
- // Test 9: Invalid array items
146
- try {
147
- const jsonData = '{"tags": ["javascript", 123]}';
148
- const schema = {
149
- required: ['tags'],
150
- properties: {
151
- tags: {
152
- type: 'array',
153
- items: { type: 'string' }
154
- }
155
- }
156
- };
157
- const result = parseAndValidateJSON(jsonData, schema);
158
- console.log('Test 9 failed: Should have thrown an error for invalid array items');
159
- } catch (error) {
160
- console.log('Test 9 passed: Correctly rejected invalid array items');
161
- console.log('Error:', error.message);
162
- }
163
-
164
- console.log('\nAll tests completed!');