|
| 1 | +/** |
| 2 | + * @fileoverview Unit tests for encoding and character code constants. |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, expect, it } from 'vitest' |
| 6 | + |
| 7 | +import { |
| 8 | + CHAR_BACKWARD_SLASH, |
| 9 | + CHAR_COLON, |
| 10 | + CHAR_FORWARD_SLASH, |
| 11 | + CHAR_LOWERCASE_A, |
| 12 | + CHAR_LOWERCASE_Z, |
| 13 | + CHAR_UPPERCASE_A, |
| 14 | + CHAR_UPPERCASE_Z, |
| 15 | + UTF8, |
| 16 | +} from '@socketsecurity/lib/constants/encoding' |
| 17 | + |
| 18 | +describe('constants/encoding', () => { |
| 19 | + describe('encoding', () => { |
| 20 | + it('should export UTF8', () => { |
| 21 | + expect(UTF8).toBe('utf8') |
| 22 | + }) |
| 23 | + |
| 24 | + it('should be a string', () => { |
| 25 | + expect(typeof UTF8).toBe('string') |
| 26 | + }) |
| 27 | + |
| 28 | + it('should be lowercase', () => { |
| 29 | + expect(UTF8).toBe(UTF8.toLowerCase()) |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + describe('character codes', () => { |
| 34 | + it('should export CHAR_BACKWARD_SLASH', () => { |
| 35 | + expect(CHAR_BACKWARD_SLASH).toBe(92) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should export CHAR_COLON', () => { |
| 39 | + expect(CHAR_COLON).toBe(58) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should export CHAR_FORWARD_SLASH', () => { |
| 43 | + expect(CHAR_FORWARD_SLASH).toBe(47) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should export CHAR_LOWERCASE_A', () => { |
| 47 | + expect(CHAR_LOWERCASE_A).toBe(97) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should export CHAR_LOWERCASE_Z', () => { |
| 51 | + expect(CHAR_LOWERCASE_Z).toBe(122) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should export CHAR_UPPERCASE_A', () => { |
| 55 | + expect(CHAR_UPPERCASE_A).toBe(65) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should export CHAR_UPPERCASE_Z', () => { |
| 59 | + expect(CHAR_UPPERCASE_Z).toBe(90) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should all be numbers', () => { |
| 63 | + expect(typeof CHAR_BACKWARD_SLASH).toBe('number') |
| 64 | + expect(typeof CHAR_COLON).toBe('number') |
| 65 | + expect(typeof CHAR_FORWARD_SLASH).toBe('number') |
| 66 | + expect(typeof CHAR_LOWERCASE_A).toBe('number') |
| 67 | + expect(typeof CHAR_LOWERCASE_Z).toBe('number') |
| 68 | + expect(typeof CHAR_UPPERCASE_A).toBe('number') |
| 69 | + expect(typeof CHAR_UPPERCASE_Z).toBe('number') |
| 70 | + }) |
| 71 | + |
| 72 | + it('should all be positive integers', () => { |
| 73 | + expect(CHAR_BACKWARD_SLASH).toBeGreaterThan(0) |
| 74 | + expect(CHAR_COLON).toBeGreaterThan(0) |
| 75 | + expect(CHAR_FORWARD_SLASH).toBeGreaterThan(0) |
| 76 | + expect(CHAR_LOWERCASE_A).toBeGreaterThan(0) |
| 77 | + expect(CHAR_LOWERCASE_Z).toBeGreaterThan(0) |
| 78 | + expect(CHAR_UPPERCASE_A).toBeGreaterThan(0) |
| 79 | + expect(CHAR_UPPERCASE_Z).toBeGreaterThan(0) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should match character codes', () => { |
| 83 | + expect('\\'.charCodeAt(0)).toBe(CHAR_BACKWARD_SLASH) |
| 84 | + expect(':'.charCodeAt(0)).toBe(CHAR_COLON) |
| 85 | + expect('/'.charCodeAt(0)).toBe(CHAR_FORWARD_SLASH) |
| 86 | + expect('a'.charCodeAt(0)).toBe(CHAR_LOWERCASE_A) |
| 87 | + expect('z'.charCodeAt(0)).toBe(CHAR_LOWERCASE_Z) |
| 88 | + expect('A'.charCodeAt(0)).toBe(CHAR_UPPERCASE_A) |
| 89 | + expect('Z'.charCodeAt(0)).toBe(CHAR_UPPERCASE_Z) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should have lowercase before uppercase in ASCII', () => { |
| 93 | + expect(CHAR_UPPERCASE_A).toBeLessThan(CHAR_LOWERCASE_A) |
| 94 | + expect(CHAR_UPPERCASE_Z).toBeLessThan(CHAR_LOWERCASE_Z) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should have A before Z in each case', () => { |
| 98 | + expect(CHAR_UPPERCASE_A).toBeLessThan(CHAR_UPPERCASE_Z) |
| 99 | + expect(CHAR_LOWERCASE_A).toBeLessThan(CHAR_LOWERCASE_Z) |
| 100 | + }) |
| 101 | + |
| 102 | + it('should have forward slash before colon before backward slash', () => { |
| 103 | + expect(CHAR_FORWARD_SLASH).toBeLessThan(CHAR_COLON) |
| 104 | + expect(CHAR_COLON).toBeLessThan(CHAR_BACKWARD_SLASH) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe('character ranges', () => { |
| 109 | + it('should define complete uppercase range', () => { |
| 110 | + const rangeSize = CHAR_UPPERCASE_Z - CHAR_UPPERCASE_A + 1 |
| 111 | + expect(rangeSize).toBe(26) |
| 112 | + }) |
| 113 | + |
| 114 | + it('should define complete lowercase range', () => { |
| 115 | + const rangeSize = CHAR_LOWERCASE_Z - CHAR_LOWERCASE_A + 1 |
| 116 | + expect(rangeSize).toBe(26) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should cover all uppercase letters', () => { |
| 120 | + for (let code = CHAR_UPPERCASE_A; code <= CHAR_UPPERCASE_Z; code++) { |
| 121 | + const char = String.fromCharCode(code) |
| 122 | + expect(char).toMatch(/[A-Z]/) |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + it('should cover all lowercase letters', () => { |
| 127 | + for (let code = CHAR_LOWERCASE_A; code <= CHAR_LOWERCASE_Z; code++) { |
| 128 | + const char = String.fromCharCode(code) |
| 129 | + expect(char).toMatch(/[a-z]/) |
| 130 | + } |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + describe('real-world usage', () => { |
| 135 | + it('should support case-insensitive comparisons', () => { |
| 136 | + const aCode = 'A'.charCodeAt(0) |
| 137 | + const isUppercase = |
| 138 | + aCode >= CHAR_UPPERCASE_A && aCode <= CHAR_UPPERCASE_Z |
| 139 | + expect(isUppercase).toBe(true) |
| 140 | + }) |
| 141 | + |
| 142 | + it('should support lowercase detection', () => { |
| 143 | + const zCode = 'z'.charCodeAt(0) |
| 144 | + const isLowercase = |
| 145 | + zCode >= CHAR_LOWERCASE_A && zCode <= CHAR_LOWERCASE_Z |
| 146 | + expect(isLowercase).toBe(true) |
| 147 | + }) |
| 148 | + |
| 149 | + it('should support path character detection', () => { |
| 150 | + const pathStr = '/usr/local/bin' |
| 151 | + expect(pathStr.charCodeAt(0)).toBe(CHAR_FORWARD_SLASH) |
| 152 | + }) |
| 153 | + |
| 154 | + it('should support Windows path detection', () => { |
| 155 | + const winPath = 'C:\\Windows\\System32' |
| 156 | + expect(winPath.charCodeAt(2)).toBe(CHAR_BACKWARD_SLASH) |
| 157 | + expect(winPath.charCodeAt(1)).toBe(CHAR_COLON) |
| 158 | + }) |
| 159 | + |
| 160 | + it('should support encoding specification', () => { |
| 161 | + const buffer = Buffer.from('test', UTF8) |
| 162 | + expect(buffer.toString(UTF8)).toBe('test') |
| 163 | + }) |
| 164 | + |
| 165 | + it('should support case conversion logic', () => { |
| 166 | + const offset = CHAR_LOWERCASE_A - CHAR_UPPERCASE_A |
| 167 | + const aUpper = 'A'.charCodeAt(0) |
| 168 | + const aLower = aUpper + offset |
| 169 | + expect(aLower).toBe(CHAR_LOWERCASE_A) |
| 170 | + }) |
| 171 | + |
| 172 | + it('should detect drive letters', () => { |
| 173 | + const cCode = 'C'.charCodeAt(0) |
| 174 | + const isDriveLetter = |
| 175 | + cCode >= CHAR_UPPERCASE_A && cCode <= CHAR_UPPERCASE_Z |
| 176 | + expect(isDriveLetter).toBe(true) |
| 177 | + }) |
| 178 | + |
| 179 | + it('should detect URL protocols', () => { |
| 180 | + const url = 'http://example.com' |
| 181 | + const colonIndex = url.indexOf(':') |
| 182 | + expect(url.charCodeAt(colonIndex)).toBe(CHAR_COLON) |
| 183 | + expect(url.charCodeAt(colonIndex + 1)).toBe(CHAR_FORWARD_SLASH) |
| 184 | + expect(url.charCodeAt(colonIndex + 2)).toBe(CHAR_FORWARD_SLASH) |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | + describe('edge cases', () => { |
| 189 | + it('should handle character before A', () => { |
| 190 | + const atSignCode = '@'.charCodeAt(0) |
| 191 | + expect(atSignCode).toBe(CHAR_UPPERCASE_A - 1) |
| 192 | + }) |
| 193 | + |
| 194 | + it('should handle character after Z', () => { |
| 195 | + const bracketCode = '['.charCodeAt(0) |
| 196 | + expect(bracketCode).toBe(CHAR_UPPERCASE_Z + 1) |
| 197 | + }) |
| 198 | + |
| 199 | + it('should handle character before a', () => { |
| 200 | + const backtickCode = '`'.charCodeAt(0) |
| 201 | + expect(backtickCode).toBe(CHAR_LOWERCASE_A - 1) |
| 202 | + }) |
| 203 | + |
| 204 | + it('should handle character after z', () => { |
| 205 | + const braceCode = '{'.charCodeAt(0) |
| 206 | + expect(braceCode).toBe(CHAR_LOWERCASE_Z + 1) |
| 207 | + }) |
| 208 | + |
| 209 | + it('should validate slash types are different', () => { |
| 210 | + expect(CHAR_FORWARD_SLASH).not.toBe(CHAR_BACKWARD_SLASH) |
| 211 | + expect('/').not.toBe('\\') |
| 212 | + }) |
| 213 | + |
| 214 | + it('should validate colon position in ASCII table', () => { |
| 215 | + // Colon is after digits (48-57) and before uppercase letters (65-90) |
| 216 | + expect(CHAR_COLON).toBeGreaterThan(57) |
| 217 | + expect(CHAR_COLON).toBeLessThan(CHAR_UPPERCASE_A) |
| 218 | + }) |
| 219 | + }) |
| 220 | +}) |
0 commit comments