-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.ts
More file actions
200 lines (187 loc) · 5.13 KB
/
test.ts
File metadata and controls
200 lines (187 loc) · 5.13 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
/**
* Sample tests for validateWeb4AppData function
* Each test tries to validate a data object. If the data is invalid,
* the function should throw an error, which we catch and log.
*/
import { validateWeb4AppData } from './validate'
// Utility to print test results more clearly
function logTestResult(testName: string, error?: unknown) {
if (!error) {
console.log(`✅ [PASSED]: ${testName}`)
} else {
console.error(`❌ [FAILED]: ${testName}`)
console.error(` Error: ${(error as Error).message}`)
}
}
/**
* Run all test scenarios, including:
* 1) Completely valid data
* 2) Missing required fields
* 3) Exceeding length constraints
* 4) Optional fields with/without sub-fields
*/
async function runTests() {
// 1) Completely valid data
const validData = {
project: {
name: 'React + Vite Ping Pong',
version: '1.0.0',
description: 'A short description within 4096 characters.',
goals: ['Fun', 'Score Tracking', 'Learn Blockchain'],
},
features: [
{
name: 'Reactive Gameplay',
description: 'Smooth ball and paddle movement.',
},
],
tests: {
testScenarios: [
{
name: 'Initial Launch',
description: 'Check if the game launches properly.',
},
],
},
authors: [
{
role: 'Maintainer',
name: 'Jane',
contact: 'jane@example.com',
},
],
references: {
librariesOrDependencies: [
{
name: 'React',
url: 'https://react.dev',
notes: 'Required for UI.',
},
],
},
platformSupport: [
{
platform: 'Browser',
notes: 'Standard usage.',
},
],
aiUsage: {
description: 'No direct AI usage.',
optionalDetails: 'Could be added later.',
},
cryptoIntegration: {
description: 'Store and compare scores on-chain.',
actions: [
{
name: 'saveTopScore',
description: 'Saves a new top score.',
supportedChains: [
{
chain: 'Ethereum',
notes: 'Tested on mainnet.',
},
],
},
],
},
}
try {
await validateWeb4AppData(validData)
logTestResult('Valid data structure test')
} catch (error) {
logTestResult('Valid data structure test', error)
}
// 2) Missing required fields (example: missing project.name)
const missingProjectName = {
project: {
version: '1.0.0',
description: 'Missing project name.',
goals: ['Goal1', 'Goal2'],
},
features: [],
tests: { testScenarios: [] },
authors: [],
}
try {
await validateWeb4AppData(missingProjectName)
logTestResult('Missing required field: project.name (should fail)')
} catch (error) {
logTestResult('Missing required field: project.name (should fail)', undefined)
}
// 3) Exceeding length constraints
// We'll try to exceed the description limit (4096 chars) in project.description
const tooLongDescription = 'a'.repeat(5000)
const exceedDescriptionData = {
project: {
name: 'Test',
version: '1.0.0',
description: tooLongDescription,
goals: ['Goal1'],
},
features: [],
tests: { testScenarios: [] },
authors: [],
}
try {
await validateWeb4AppData(exceedDescriptionData)
logTestResult('Exceeding description length (should fail)')
} catch (error) {
logTestResult('Exceeding description length (should fail)', undefined)
}
// 4) Optional fields with sub-fields (example: references.librariesOrDependencies missing required sub-fields)
const invalidReferences = {
project: {
name: 'Project with invalid references',
version: '1.0.0',
description: 'Short desc',
goals: ['Goal1'],
},
features: [],
tests: { testScenarios: [] },
authors: [],
references: {
librariesOrDependencies: [
{
// missing 'name' and 'url'
notes: 'We forgot name and url',
},
],
},
}
try {
await validateWeb4AppData(invalidReferences)
logTestResult('References missing library name/url (should fail)')
} catch (error) {
logTestResult('References missing library name/url (should fail)', undefined)
}
// 5) Test partial optional field presence (cryptoIntegration without supportedChains)
const partialCryptoIntegration = {
project: {
name: 'Project partial crypto test',
version: '1.0.0',
description: 'Testing partial optional field presence.',
goals: ['Goal1'],
},
features: [],
tests: {
testScenarios: [{ name: 'Test scenario', description: 'Test scenario desc' }],
},
authors: [
{
role: 'Contributor',
name: 'John',
},
],
cryptoIntegration: {
description: 'We have no actions array — should fail because actions is required',
// 'actions' array is missing
},
}
try {
await validateWeb4AppData(partialCryptoIntegration)
logTestResult("Missing 'actions' in cryptoIntegration (should fail)")
} catch (error) {
logTestResult("Missing 'actions' in cryptoIntegration (should fail)", undefined)
}
}
runTests()