|
| 1 | +import type { IpLocationApiInputSettings } from './getSettings' |
| 2 | +import { join } from 'node:path' |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import { DEFAULT_SETTINGS, LOCATION_FIELDS, MAIN_FIELDS } from '../constants' |
| 5 | +import { getSettings } from './getSettings' |
| 6 | + |
| 7 | +describe('getSettings', () => { |
| 8 | + const originalEnv = process.env |
| 9 | + const originalArgv = process.argv |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + vi.resetModules() |
| 13 | + process.env = { ...originalEnv } |
| 14 | + process.argv = [...originalArgv] |
| 15 | + }) |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + process.env = originalEnv |
| 19 | + process.argv = originalArgv |
| 20 | + }) |
| 21 | + |
| 22 | + it('should return default settings when no input is provided', () => { |
| 23 | + const settings = getSettings() |
| 24 | + expect(settings).toMatchObject({ |
| 25 | + licenseKey: DEFAULT_SETTINGS.licenseKey, |
| 26 | + series: DEFAULT_SETTINGS.series, |
| 27 | + fields: DEFAULT_SETTINGS.fields, |
| 28 | + language: DEFAULT_SETTINGS.language, |
| 29 | + smallMemory: DEFAULT_SETTINGS.smallMemory, |
| 30 | + smallMemoryFileSize: DEFAULT_SETTINGS.smallMemoryFileSize, |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should override default settings with input settings', () => { |
| 35 | + const inputSettings: IpLocationApiInputSettings = { |
| 36 | + licenseKey: 'test-key', |
| 37 | + series: 'GeoIP2', |
| 38 | + fields: ['country', 'city'], |
| 39 | + language: 'es', |
| 40 | + smallMemory: true, |
| 41 | + smallMemoryFileSize: 8192, |
| 42 | + } |
| 43 | + const settings = getSettings(inputSettings) |
| 44 | + expect(settings).toMatchObject({ |
| 45 | + licenseKey: 'test-key', |
| 46 | + series: 'GeoIP2', |
| 47 | + fields: ['country', 'city'], |
| 48 | + language: 'es', |
| 49 | + smallMemory: true, |
| 50 | + smallMemoryFileSize: 8192, |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should use environment variables to override default settings', () => { |
| 55 | + process.env.ILA_LICENSE_KEY = 'env-key' |
| 56 | + process.env.ILA_SERIES = 'GeoIP2' |
| 57 | + process.env.ILA_FIELDS = 'country,city' |
| 58 | + process.env.ILA_LANGUAGE = 'fr' |
| 59 | + process.env.ILA_SMALL_MEMORY = 'true' |
| 60 | + process.env.ILA_SMALL_MEMORY_FILE_SIZE = '16384' |
| 61 | + |
| 62 | + const settings = getSettings() |
| 63 | + expect(settings).toMatchObject({ |
| 64 | + licenseKey: 'env-key', |
| 65 | + series: 'GeoIP2', |
| 66 | + fields: ['country', 'city'], |
| 67 | + language: 'fr', |
| 68 | + smallMemory: true, |
| 69 | + smallMemoryFileSize: 16384, |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should use CLI arguments to override default and environment settings', () => { |
| 74 | + process.env.ILA_LICENSE_KEY = 'env-key' |
| 75 | + process.argv = [ |
| 76 | + ...process.argv, |
| 77 | + 'ILA_LICENSE_KEY=cli-key', |
| 78 | + 'ILA_SERIES=GeoIP2', |
| 79 | + 'ILA_FIELDS=country,region1', |
| 80 | + 'ILA_LANGUAGE=de', |
| 81 | + 'ILA_SMALL_MEMORY=true', |
| 82 | + 'ILA_SMALL_MEMORY_FILE_SIZE=32768', |
| 83 | + ] |
| 84 | + |
| 85 | + const settings = getSettings() |
| 86 | + expect(settings).toMatchObject({ |
| 87 | + licenseKey: 'cli-key', |
| 88 | + series: 'GeoIP2', |
| 89 | + fields: ['country', 'region1'], |
| 90 | + language: 'de', |
| 91 | + smallMemory: true, |
| 92 | + smallMemoryFileSize: 32768, |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should process "all" fields correctly', () => { |
| 97 | + const inputSettings: IpLocationApiInputSettings = { |
| 98 | + fields: 'all', |
| 99 | + } |
| 100 | + const settings = getSettings(inputSettings) |
| 101 | + expect(settings.fields).toEqual([...MAIN_FIELDS, ...LOCATION_FIELDS]) |
| 102 | + |
| 103 | + process.argv = [...process.argv, 'ILA_FIELDS=all'] |
| 104 | + const settings2 = getSettings() |
| 105 | + expect(settings2.fields).toEqual([...MAIN_FIELDS, ...LOCATION_FIELDS]) |
| 106 | + }) |
| 107 | + |
| 108 | + it('should filter out invalid fields', () => { |
| 109 | + const inputSettings: IpLocationApiInputSettings = { |
| 110 | + fields: ['country', 'invalid_field', 'city'] as any, |
| 111 | + } |
| 112 | + const settings = getSettings(inputSettings) |
| 113 | + expect(settings.fields).toEqual(['country', 'city']) |
| 114 | + }) |
| 115 | + |
| 116 | + it('should handle invalid fields input', () => { |
| 117 | + const inputSettings: IpLocationApiInputSettings = { |
| 118 | + fields: 0 as any, |
| 119 | + } |
| 120 | + const settings = getSettings(inputSettings) |
| 121 | + expect(settings.fields).toEqual(DEFAULT_SETTINGS.fields) |
| 122 | + }) |
| 123 | + |
| 124 | + it('should use default fields if all provided fields are invalid', () => { |
| 125 | + const inputSettings: IpLocationApiInputSettings = { |
| 126 | + fields: ['invalid_field1', 'invalid_field2'] as any, |
| 127 | + } |
| 128 | + const settings = getSettings(inputSettings) |
| 129 | + expect(settings.fields).toEqual(DEFAULT_SETTINGS.fields) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should process directory paths correctly', () => { |
| 133 | + const inputSettings: IpLocationApiInputSettings = { |
| 134 | + dataDir: '../custom-data', |
| 135 | + tmpDataDir: '/tmp/custom-tmp', |
| 136 | + } |
| 137 | + const settings = getSettings(inputSettings) |
| 138 | + expect(settings.dataDir).toMatch(/custom-data$/) |
| 139 | + expect(settings.tmpDataDir).toBe('/tmp/custom-tmp') |
| 140 | + }) |
| 141 | + |
| 142 | + it('should calculate correct record sizes and database settings', () => { |
| 143 | + const inputSettings: IpLocationApiInputSettings = { |
| 144 | + fields: ['country', 'city', 'latitude', 'longitude'], |
| 145 | + smallMemory: true, |
| 146 | + smallMemoryFileSize: 4096, |
| 147 | + } |
| 148 | + const settings = getSettings(inputSettings) |
| 149 | + expect(settings.dataType).toBe('City') |
| 150 | + expect(settings.locationFile).toBe(true) |
| 151 | + expect(settings.mainRecordSize).toBeGreaterThan(0) |
| 152 | + expect(settings.locationRecordSize).toBeGreaterThan(0) |
| 153 | + expect(settings.v4.recordSize).toBeGreaterThan(settings.mainRecordSize) |
| 154 | + expect(settings.v6.recordSize).toBeGreaterThan(settings.mainRecordSize) |
| 155 | + expect(settings.v4.fileLineMax).toBeGreaterThan(0) |
| 156 | + expect(settings.v6.fileLineMax).toBeGreaterThan(0) |
| 157 | + |
| 158 | + const settings2 = getSettings({ |
| 159 | + fields: ['country', 'city', 'latitude', 'longitude'], |
| 160 | + smallMemory: true, |
| 161 | + smallMemoryFileSize: 0, |
| 162 | + }) |
| 163 | + expect(settings2.dataType).toBe('City') |
| 164 | + expect(settings2.locationFile).toBe(true) |
| 165 | + expect(settings2.mainRecordSize).toBeGreaterThan(0) |
| 166 | + expect(settings2.locationRecordSize).toBeGreaterThan(0) |
| 167 | + expect(settings2.v4.recordSize).toBeGreaterThan(settings2.mainRecordSize) |
| 168 | + expect(settings2.v6.recordSize).toBeGreaterThan(settings2.mainRecordSize) |
| 169 | + expect(settings2.v4.fileLineMax).toBe(1) |
| 170 | + expect(settings2.v6.fileLineMax).toBe(1) |
| 171 | + }) |
| 172 | + |
| 173 | + it('should generate correct fieldDir', () => { |
| 174 | + const inputSettings: IpLocationApiInputSettings = { |
| 175 | + fields: ['country', 'city'], |
| 176 | + dataDir: '/custom/data/dir', |
| 177 | + } |
| 178 | + const settings = getSettings(inputSettings) |
| 179 | + const expectedFieldDir = join('/custom/data/dir', (16 + 2048).toString(36)) |
| 180 | + expect(settings.fieldDir).toBe(expectedFieldDir) |
| 181 | + }) |
| 182 | + |
| 183 | + it('should handle invalid language input', () => { |
| 184 | + const inputSettings: IpLocationApiInputSettings = { |
| 185 | + language: 'invalid_language' as any, |
| 186 | + } |
| 187 | + const settings = getSettings(inputSettings) |
| 188 | + expect(settings.language).toBe(DEFAULT_SETTINGS.language) |
| 189 | + }) |
| 190 | + |
| 191 | + it('should handle invalid series input', () => { |
| 192 | + const inputSettings: IpLocationApiInputSettings = { |
| 193 | + series: 'InvalidSeries' as any, |
| 194 | + } |
| 195 | + const settings = getSettings(inputSettings) |
| 196 | + expect(settings.series).toBe(DEFAULT_SETTINGS.series) |
| 197 | + }) |
| 198 | +}) |
0 commit comments