-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.js
More file actions
164 lines (134 loc) · 5.06 KB
/
Copy pathtest.js
File metadata and controls
164 lines (134 loc) · 5.06 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
import { createId, verifyId } from "./dist/index.js"
import * as badWordsList from 'badwords-list'
// Test configuration
const TEST_ITERATIONS = 100
const MIN_LENGTH = 3
const MAX_LENGTH = 20
const STEPS = [2, 3]
console.log("🧪 Starting ID System Tests...\n")
// Test 1: Generate + Verify for different lengths
console.log("📋 Test 1: Generate + Verify (Valid IDs)")
console.log("=".repeat(50))
let totalTests = 0
let passedTests = 0
for (let length = MIN_LENGTH; length <= MAX_LENGTH; length++) {
console.log(`\n🔍 Testing ID length: ${length}`)
let lengthPassed = 0
for (const step of STEPS) {
console.log(` - Step: ${step} positions`)
for (let i = 0; i < TEST_ITERATIONS; i++) {
try {
// Generate ID
const id = await createId({ approximateLength: length, step })
// Verify ID
const isValid = await verifyId(id, { step })
totalTests++
if (isValid) {
passedTests++
lengthPassed++
} else {
console.log(`❌ Failed verification for ID: ${id} (length: ${length}, step: ${step})`)
}
} catch (error) {
console.log(`💥 Error during test: ${error.message}`)
totalTests++
}
}
}
const successRate = ((lengthPassed / TEST_ITERATIONS) * 100).toFixed(1)
console.log(` ✅ Passed: ${lengthPassed}/${TEST_ITERATIONS} (${successRate}%)`)
}
console.log(
`\n📊 Test 1 Summary: ${passedTests}/${totalTests} passed (${((passedTests / totalTests) * 100).toFixed(1)}%)`,
)
// Test 2: Wrong tokens (should fail verification)
console.log("\n📋 Test 2: Wrong Tokens (Should Fail)")
console.log("=".repeat(50))
let wrongTokenTests = 0
let correctlyRejected = 0
for (let i = 0; i < TEST_ITERATIONS; i++) {
try {
// Generate a valid ID
const validId = await createId(10)
// Create wrong token by modifying the ID
let wrongId = validId
// Randomly modify 2 characters
const modifications = 2
for (let j = 0; j < modifications; j++) {
const pos = Math.floor(Math.random() * wrongId.length)
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let newChar = chars[Math.floor(Math.random() * chars.length)]
// Ensure we actually change the character
while (newChar === wrongId[pos]) {
newChar = chars[Math.floor(Math.random() * chars.length)]
}
wrongId = wrongId.substring(0, pos) + newChar + wrongId.substring(pos + 1)
}
// Verify wrong ID (should fail)
const isValid = await verifyId(wrongId)
wrongTokenTests++
if (!isValid) {
correctlyRejected++
} else {
console.log(`⁉️ Wrong ID incorrectly validated: ${wrongId}`)
}
} catch (error) {
console.log(`💥 Error during wrong token test: ${error.message}`)
wrongTokenTests++
}
}
const rejectionRate = ((correctlyRejected / wrongTokenTests) * 100).toFixed(1)
console.log(`\n📊 Test 2 Summary: ${correctlyRejected}/${wrongTokenTests} correctly rejected (${rejectionRate}%)`)
// Test 3: Edge cases
console.log("\n📋 Test 3: Edge Cases")
console.log("=".repeat(50))
// Test empty string
try {
const emptyResult = await verifyId("")
console.log(`Empty string verification: ${emptyResult ? "❌ PASSED (should fail)" : "✅ FAILED (correct)"}`)
} catch (error) {
console.log(`Empty string verification: ✅ THREW ERROR (correct)`)
}
// Test very short ID
try {
const shortResult = await verifyId("a")
console.log(`Very short ID verification: ${shortResult ? "❌ PASSED (should fail)" : "✅ FAILED (correct)"}`)
} catch (error) {
console.log(`Very short ID verification: ✅ THREW ERROR (correct)`)
}
// Test invalid characters (if any exist outside our alphabet)
try {
const invalidResult = await verifyId("invalid@#$")
console.log(`Invalid chars verification: ${invalidResult ? "❌ PASSED (should fail)" : "✅ FAILED (correct)"}`)
} catch (error) {
console.log(`Invalid chars verification: ✅ THREW ERROR (correct)`)
}
// Test bad words and popular phrases
const words = badWordsList.array.concat([
"test", "demo", "admin", "user", "password", "login",
"apple", "signin", "signout", "google", "twitter", "facebook",
"react", "java", "javascript", "vercel", "github"
])
let wordPassed = 0
for (const word of words) {
const result = await verifyId(word)
if (result) {
wordPassed++
console.log(`Word "${word}" unexpectedly validated`)
}
}
if (wordPassed === 0) {
console.log("Words verification: ✅ REJECTED (correct)")
} else {
console.log(`Words verification: ❌ ${wordPassed} bad words incorrectly validated`)
}
// Final summary
console.log("\n🎯 Final Summary")
console.log("=".repeat(50))
console.log(`Valid ID Tests: ${passedTests}/${totalTests} passed (${((passedTests / totalTests) * 100).toFixed(1)}%)`)
console.log(`Wrong Token Tests: ${correctlyRejected}/${wrongTokenTests} correctly rejected (${rejectionRate}%)`)
const overallSuccess = passedTests === totalTests && correctlyRejected === wrongTokenTests
console.log(`\n${overallSuccess ? "🎉 ALL TESTS PASSED!" : "⚠️ SOME TESTS FAILED"}`)
if (!overallSuccess) {
process.exit(1)
}