|
| 1 | +/** |
| 2 | + * @fileoverview Unit tests for maintained Node.js versions module. |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, expect, it } from 'vitest' |
| 6 | + |
| 7 | +import maintainedNodeVersionsModule from '@socketsecurity/lib/maintained-node-versions' |
| 8 | + |
| 9 | +// Handle ESM/CJS interop - the module may be double-wrapped |
| 10 | +const maintainedNodeVersions = |
| 11 | + (maintainedNodeVersionsModule as any).default || maintainedNodeVersionsModule |
| 12 | + |
| 13 | +describe('maintained-node-versions', () => { |
| 14 | + describe('default export', () => { |
| 15 | + it('should export an array', () => { |
| 16 | + expect(Array.isArray(maintainedNodeVersions)).toBe(true) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should be frozen', () => { |
| 20 | + expect(Object.isFrozen(maintainedNodeVersions)).toBe(true) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should have exactly 4 versions', () => { |
| 24 | + expect(maintainedNodeVersions).toHaveLength(4) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should contain only strings', () => { |
| 28 | + maintainedNodeVersions.forEach((version) => { |
| 29 | + expect(typeof version).toBe('string') |
| 30 | + }) |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + describe('named properties', () => { |
| 35 | + it('should have current property', () => { |
| 36 | + expect(maintainedNodeVersions).toHaveProperty('current') |
| 37 | + expect(typeof maintainedNodeVersions.current).toBe('string') |
| 38 | + }) |
| 39 | + |
| 40 | + it('should have last property', () => { |
| 41 | + expect(maintainedNodeVersions).toHaveProperty('last') |
| 42 | + expect(typeof maintainedNodeVersions.last).toBe('string') |
| 43 | + }) |
| 44 | + |
| 45 | + it('should have next property', () => { |
| 46 | + expect(maintainedNodeVersions).toHaveProperty('next') |
| 47 | + expect(typeof maintainedNodeVersions.next).toBe('string') |
| 48 | + }) |
| 49 | + |
| 50 | + it('should have previous property', () => { |
| 51 | + expect(maintainedNodeVersions).toHaveProperty('previous') |
| 52 | + expect(typeof maintainedNodeVersions.previous).toBe('string') |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('array contents', () => { |
| 57 | + it('should have versions in order: last, previous, current, next', () => { |
| 58 | + const [first, second, third, fourth] = maintainedNodeVersions |
| 59 | + expect(first).toBe(maintainedNodeVersions.last) |
| 60 | + expect(second).toBe(maintainedNodeVersions.previous) |
| 61 | + expect(third).toBe(maintainedNodeVersions.current) |
| 62 | + expect(fourth).toBe(maintainedNodeVersions.next) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should have valid semver format for all versions', () => { |
| 66 | + const semverPattern = /^\d+\.\d+\.\d+$/ |
| 67 | + maintainedNodeVersions.forEach((version) => { |
| 68 | + expect(version).toMatch(semverPattern) |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should have versions in ascending order', () => { |
| 73 | + const versions = [...maintainedNodeVersions] |
| 74 | + const sortedVersions = versions |
| 75 | + .map((v) => v.split('.').map(Number)) |
| 76 | + .sort((a, b) => { |
| 77 | + for (let i = 0; i < 3; i++) { |
| 78 | + if (a[i] !== b[i]) return a[i] - b[i] |
| 79 | + } |
| 80 | + return 0 |
| 81 | + }) |
| 82 | + .map((v) => v.join('.')) |
| 83 | + |
| 84 | + expect(versions).toEqual(sortedVersions) |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + describe('version properties match array', () => { |
| 89 | + it('should have current in array', () => { |
| 90 | + expect(maintainedNodeVersions).toContain( |
| 91 | + maintainedNodeVersions.current, |
| 92 | + ) |
| 93 | + }) |
| 94 | + |
| 95 | + it('should have last in array', () => { |
| 96 | + expect(maintainedNodeVersions).toContain(maintainedNodeVersions.last) |
| 97 | + }) |
| 98 | + |
| 99 | + it('should have next in array', () => { |
| 100 | + expect(maintainedNodeVersions).toContain(maintainedNodeVersions.next) |
| 101 | + }) |
| 102 | + |
| 103 | + it('should have previous in array', () => { |
| 104 | + expect(maintainedNodeVersions).toContain( |
| 105 | + maintainedNodeVersions.previous, |
| 106 | + ) |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + describe('immutability', () => { |
| 111 | + it('should not allow modification of array elements', () => { |
| 112 | + expect(() => { |
| 113 | + // @ts-expect-error - testing immutability |
| 114 | + maintainedNodeVersions[0] = '99.99.99' |
| 115 | + }).toThrow() |
| 116 | + }) |
| 117 | + |
| 118 | + it('should not allow push', () => { |
| 119 | + expect(() => { |
| 120 | + // @ts-expect-error - testing immutability |
| 121 | + maintainedNodeVersions.push('99.99.99') |
| 122 | + }).toThrow() |
| 123 | + }) |
| 124 | + |
| 125 | + it('should not allow pop', () => { |
| 126 | + expect(() => { |
| 127 | + // @ts-expect-error - testing immutability |
| 128 | + maintainedNodeVersions.pop() |
| 129 | + }).toThrow() |
| 130 | + }) |
| 131 | + |
| 132 | + it('should not allow modification of named properties', () => { |
| 133 | + expect(() => { |
| 134 | + // @ts-expect-error - testing immutability |
| 135 | + maintainedNodeVersions.current = '99.99.99' |
| 136 | + }).toThrow() |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + describe('version relationships', () => { |
| 141 | + it('should have current >= previous', () => { |
| 142 | + const current = maintainedNodeVersions.current |
| 143 | + .split('.') |
| 144 | + .map(Number) |
| 145 | + const previous = maintainedNodeVersions.previous |
| 146 | + .split('.') |
| 147 | + .map(Number) |
| 148 | + |
| 149 | + const currentMajor = current[0] |
| 150 | + const previousMajor = previous[0] |
| 151 | + |
| 152 | + expect(currentMajor).toBeGreaterThanOrEqual(previousMajor) |
| 153 | + }) |
| 154 | + |
| 155 | + it('should have previous >= last', () => { |
| 156 | + const previous = maintainedNodeVersions.previous |
| 157 | + .split('.') |
| 158 | + .map(Number) |
| 159 | + const last = maintainedNodeVersions.last.split('.').map(Number) |
| 160 | + |
| 161 | + const previousMajor = previous[0] |
| 162 | + const lastMajor = last[0] |
| 163 | + |
| 164 | + expect(previousMajor).toBeGreaterThanOrEqual(lastMajor) |
| 165 | + }) |
| 166 | + |
| 167 | + it('should have next >= current', () => { |
| 168 | + const next = maintainedNodeVersions.next.split('.').map(Number) |
| 169 | + const current = maintainedNodeVersions.current |
| 170 | + .split('.') |
| 171 | + .map(Number) |
| 172 | + |
| 173 | + const nextMajor = next[0] |
| 174 | + const currentMajor = current[0] |
| 175 | + |
| 176 | + expect(nextMajor).toBeGreaterThanOrEqual(currentMajor) |
| 177 | + }) |
| 178 | + }) |
| 179 | + |
| 180 | + describe('realistic version numbers', () => { |
| 181 | + it('should have major versions in reasonable range', () => { |
| 182 | + maintainedNodeVersions.forEach((version) => { |
| 183 | + const major = Number.parseInt(version.split('.')[0], 10) |
| 184 | + expect(major).toBeGreaterThanOrEqual(10) |
| 185 | + expect(major).toBeLessThanOrEqual(100) |
| 186 | + }) |
| 187 | + }) |
| 188 | + |
| 189 | + it('should have minor versions in valid range', () => { |
| 190 | + maintainedNodeVersions.forEach((version) => { |
| 191 | + const minor = Number.parseInt(version.split('.')[1], 10) |
| 192 | + expect(minor).toBeGreaterThanOrEqual(0) |
| 193 | + expect(minor).toBeLessThanOrEqual(99) |
| 194 | + }) |
| 195 | + }) |
| 196 | + |
| 197 | + it('should have patch versions in valid range', () => { |
| 198 | + maintainedNodeVersions.forEach((version) => { |
| 199 | + const patch = Number.parseInt(version.split('.')[2], 10) |
| 200 | + expect(patch).toBeGreaterThanOrEqual(0) |
| 201 | + expect(patch).toBeLessThanOrEqual(99) |
| 202 | + }) |
| 203 | + }) |
| 204 | + }) |
| 205 | + |
| 206 | + describe('array operations', () => { |
| 207 | + it('should support forEach iteration', () => { |
| 208 | + const versions: string[] = [] |
| 209 | + maintainedNodeVersions.forEach((v) => versions.push(v)) |
| 210 | + expect(versions).toHaveLength(4) |
| 211 | + }) |
| 212 | + |
| 213 | + it('should support map operation', () => { |
| 214 | + const majors = maintainedNodeVersions.map((v) => |
| 215 | + Number.parseInt(v.split('.')[0], 10), |
| 216 | + ) |
| 217 | + expect(majors).toHaveLength(4) |
| 218 | + majors.forEach((m) => expect(typeof m).toBe('number')) |
| 219 | + }) |
| 220 | + |
| 221 | + it('should support filter operation', () => { |
| 222 | + const filtered = maintainedNodeVersions.filter((v) => |
| 223 | + v.startsWith('2'), |
| 224 | + ) |
| 225 | + expect(Array.isArray(filtered)).toBe(true) |
| 226 | + }) |
| 227 | + |
| 228 | + it('should support find operation', () => { |
| 229 | + const found = maintainedNodeVersions.find( |
| 230 | + (v) => v === maintainedNodeVersions.current, |
| 231 | + ) |
| 232 | + expect(found).toBe(maintainedNodeVersions.current) |
| 233 | + }) |
| 234 | + |
| 235 | + it('should support includes operation', () => { |
| 236 | + expect( |
| 237 | + maintainedNodeVersions.includes(maintainedNodeVersions.current), |
| 238 | + ).toBe(true) |
| 239 | + expect(maintainedNodeVersions.includes('99.99.99')).toBe(false) |
| 240 | + }) |
| 241 | + |
| 242 | + it('should support slice operation', () => { |
| 243 | + const sliced = maintainedNodeVersions.slice(0, 2) |
| 244 | + expect(sliced).toHaveLength(2) |
| 245 | + expect(sliced[0]).toBe(maintainedNodeVersions.last) |
| 246 | + expect(sliced[1]).toBe(maintainedNodeVersions.previous) |
| 247 | + }) |
| 248 | + |
| 249 | + it('should support spread operator', () => { |
| 250 | + const spread = [...maintainedNodeVersions] |
| 251 | + expect(spread).toHaveLength(4) |
| 252 | + expect(spread[0]).toBe(maintainedNodeVersions[0]) |
| 253 | + }) |
| 254 | + |
| 255 | + it('should support destructuring', () => { |
| 256 | + const [first, second, third, fourth] = maintainedNodeVersions |
| 257 | + expect(first).toBe(maintainedNodeVersions.last) |
| 258 | + expect(second).toBe(maintainedNodeVersions.previous) |
| 259 | + expect(third).toBe(maintainedNodeVersions.current) |
| 260 | + expect(fourth).toBe(maintainedNodeVersions.next) |
| 261 | + }) |
| 262 | + }) |
| 263 | + |
| 264 | + describe('edge cases', () => { |
| 265 | + it('should handle string operations on versions', () => { |
| 266 | + maintainedNodeVersions.forEach((version) => { |
| 267 | + expect(version.length).toBeGreaterThan(0) |
| 268 | + expect(version.includes('.')).toBe(true) |
| 269 | + expect(version.split('.').length).toBe(3) |
| 270 | + }) |
| 271 | + }) |
| 272 | + |
| 273 | + it('should not have duplicates', () => { |
| 274 | + const unique = new Set(maintainedNodeVersions) |
| 275 | + expect(unique.size).toBe(maintainedNodeVersions.length) |
| 276 | + }) |
| 277 | + |
| 278 | + it('should not have empty strings', () => { |
| 279 | + maintainedNodeVersions.forEach((version) => { |
| 280 | + expect(version.length).toBeGreaterThan(0) |
| 281 | + expect(version.trim()).toBe(version) |
| 282 | + }) |
| 283 | + }) |
| 284 | + }) |
| 285 | +}) |
0 commit comments