stigmergy 1.0.95 → 1.0.97

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.
@@ -0,0 +1,161 @@
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
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Test for REST API Client
3
+ */
4
+
5
+ const RestClient = require('../src/core/rest_client');
6
+
7
+ async function testRestClient() {
8
+ console.log('Testing REST Client...\n');
9
+
10
+ // Test 1: Basic GET request
11
+ try {
12
+ console.log('Test 1: Basic GET request to JSONPlaceholder');
13
+ const client = new RestClient('https://jsonplaceholder.typicode.com');
14
+
15
+ const response = await client.get('/posts/1');
16
+ console.log('Status:', response.status);
17
+ console.log('Data:', JSON.stringify(response.data, null, 2));
18
+ console.log('Success!\n');
19
+ } catch (error) {
20
+ console.error('Test 1 failed:', error.message, '\n');
21
+ }
22
+
23
+ // Test 2: POST request
24
+ try {
25
+ console.log('Test 2: POST request to JSONPlaceholder');
26
+ const client = new RestClient('https://jsonplaceholder.typicode.com');
27
+
28
+ const postData = {
29
+ title: 'Test Post',
30
+ body: 'This is a test post',
31
+ userId: 1
32
+ };
33
+
34
+ const response = await client.post('/posts', postData);
35
+ console.log('Status:', response.status);
36
+ console.log('Data:', JSON.stringify(response.data, null, 2));
37
+ console.log('Success!\n');
38
+ } catch (error) {
39
+ console.error('Test 2 failed:', error.message, '\n');
40
+ }
41
+
42
+ // Test 3: With custom headers
43
+ try {
44
+ console.log('Test 3: GET request with custom headers');
45
+ const client = new RestClient('https://jsonplaceholder.typicode.com');
46
+ client.setDefaultHeaders({
47
+ 'Custom-Header': 'test-value',
48
+ 'X-Test-Header': 'another-test'
49
+ });
50
+
51
+ const response = await client.get('/posts/2');
52
+ console.log('Status:', response.status);
53
+ console.log('Headers:', response.headers);
54
+ console.log('Success!\n');
55
+ } catch (error) {
56
+ console.error('Test 3 failed:', error.message, '\n');
57
+ }
58
+
59
+ // Test 4: With query parameters
60
+ try {
61
+ console.log('Test 4: GET request with query parameters');
62
+ const client = new RestClient('https://jsonplaceholder.typicode.com');
63
+
64
+ const response = await client.get('/posts', {
65
+ params: {
66
+ userId: 1,
67
+ _limit: 3
68
+ }
69
+ });
70
+ console.log('Status:', response.status);
71
+ console.log('Data count:', response.data.length);
72
+ console.log('Success!\n');
73
+ } catch (error) {
74
+ console.error('Test 4 failed:', error.message, '\n');
75
+ }
76
+
77
+ console.log('REST Client tests completed.');
78
+ }
79
+
80
+ // Run tests if this file is executed directly
81
+ if (require.main === module) {
82
+ testRestClient();
83
+ }
84
+
85
+ module.exports = testRestClient;