11import { describe , it , expect , beforeEach , afterEach } from 'vitest'
2- import { generateRedisConnectionExpr } from '../../src/utils/generate-redis-connection-expr'
3-
4- // Helper: evaluate the generated expression in current process context
5- function evalExpr ( expr : string ) : unknown {
6- return eval ( expr )
7- }
2+ import { generateRedisConnectionExpr , getRedisConnectionImport } from '../../src/utils/generate-redis-connection-expr'
3+ import { resolveRedisConnection } from '../../src/runtime/server/utils/resolve-redis-connection'
84
95function clearEnvKey ( key : string ) {
106 Reflect . deleteProperty ( process . env , key )
117}
128
139describe ( 'generateRedisConnectionExpr' , ( ) => {
14- const savedEnv : Record < string , string | undefined > = { }
10+ it ( 'wraps static config in a resolveRedisConnection call' , ( ) => {
11+ const expr = generateRedisConnectionExpr ( '{"host":"10.0.0.1"}' )
12+ expect ( expr ) . toBe ( 'resolveRedisConnection({"host":"10.0.0.1"})' )
13+ } )
14+
15+ it ( 'handles empty config' , ( ) => {
16+ const expr = generateRedisConnectionExpr ( '{}' )
17+ expect ( expr ) . toBe ( 'resolveRedisConnection({})' )
18+ } )
19+ } )
20+
21+ describe ( 'getRedisConnectionImport' , ( ) => {
22+ it ( 'returns an import statement with the given alias' , ( ) => {
23+ expect ( getRedisConnectionImport ( '#resolve-redis' ) )
24+ . toBe ( 'import { resolveRedisConnection } from \'#resolve-redis\'' )
25+ } )
26+ } )
27+
28+ describe ( 'resolveRedisConnection' , ( ) => {
1529 const envKeys = [
1630 'NUXT_REDIS_URL' ,
1731 'NUXT_REDIS_HOST' ,
@@ -22,6 +36,7 @@ describe('generateRedisConnectionExpr', () => {
2236 'NUXT_REDIS_LAZY_CONNECT' ,
2337 'NUXT_REDIS_CONNECT_TIMEOUT' ,
2438 ]
39+ const savedEnv : Record < string , string | undefined > = { }
2540
2641 beforeEach ( ( ) => {
2742 for ( const key of envKeys ) {
@@ -42,9 +57,7 @@ describe('generateRedisConnectionExpr', () => {
4257 } )
4358
4459 it ( 'returns static config when no env vars are set' , ( ) => {
45- const staticRedis = JSON . stringify ( { host : '10.0.0.1' , port : 6380 , password : 'secret' , db : 2 } )
46- const expr = generateRedisConnectionExpr ( staticRedis )
47- const result = evalExpr ( expr ) as Record < string , unknown >
60+ const result = resolveRedisConnection ( { host : '10.0.0.1' , port : 6380 , password : 'secret' , db : 2 } )
4861
4962 expect ( result ) . toEqual ( {
5063 host : '10.0.0.1' ,
@@ -57,21 +70,21 @@ describe('generateRedisConnectionExpr', () => {
5770 } )
5871 } )
5972
60- it ( 'returns NUXT_REDIS_URL when set at runtime ' , ( ) => {
73+ it ( 'includes url in returned object when NUXT_REDIS_URL is set ' , ( ) => {
6174 process . env . NUXT_REDIS_URL = 'redis://prod-host:6379/1'
62- const staticRedis = JSON . stringify ( { host : '127.0.0.1' , port : 6379 } )
63- const expr = generateRedisConnectionExpr ( staticRedis )
64- const result = evalExpr ( expr )
75+ const result = resolveRedisConnection ( { host : '127.0.0.1' , port : 6379 } )
6576
66- expect ( result ) . toBe ( 'redis://prod-host:6379/1' )
77+ expect ( result ) . toMatchObject ( { url : 'redis://prod-host:6379/1' } )
78+ // Other options are still present alongside url
79+ expect ( result ) . toHaveProperty ( 'host' )
80+ expect ( result ) . toHaveProperty ( 'port' )
6781 } )
6882
69- it ( 'returns static url when set in config and no env url' , ( ) => {
70- const staticRedis = JSON . stringify ( { url : 'redis://config-host:6379/0' , host : '127.0.0.1' } )
71- const expr = generateRedisConnectionExpr ( staticRedis )
72- const result = evalExpr ( expr )
83+ it ( 'includes url from static config when no env url is set' , ( ) => {
84+ const result = resolveRedisConnection ( { url : 'redis://config-host:6379/0' , host : '127.0.0.1' } )
7385
74- expect ( result ) . toBe ( 'redis://config-host:6379/0' )
86+ expect ( result ) . toMatchObject ( { url : 'redis://config-host:6379/0' } )
87+ expect ( result ) . toHaveProperty ( 'host' , '127.0.0.1' )
7588 } )
7689
7790 it ( 'env vars override individual static fields' , ( ) => {
@@ -81,9 +94,7 @@ describe('generateRedisConnectionExpr', () => {
8194 process . env . NUXT_REDIS_USERNAME = 'admin'
8295 process . env . NUXT_REDIS_DB = '5'
8396
84- const staticRedis = JSON . stringify ( { host : '10.0.0.1' , port : 6380 , password : 'build-pw' , db : 2 } )
85- const expr = generateRedisConnectionExpr ( staticRedis )
86- const result = evalExpr ( expr ) as Record < string , unknown >
97+ const result = resolveRedisConnection ( { host : '10.0.0.1' , port : 6380 , password : 'build-pw' , db : 2 } )
8798
8899 expect ( result ) . toEqual ( {
89100 host : '192.168.1.100' ,
@@ -98,11 +109,8 @@ describe('generateRedisConnectionExpr', () => {
98109
99110 it ( 'env vars partially override static fields' , ( ) => {
100111 process . env . NUXT_REDIS_HOST = 'new-host'
101- // port, password etc. not set in env
102112
103- const staticRedis = JSON . stringify ( { host : '10.0.0.1' , port : 6380 , password : 'build-pw' , db : 2 } )
104- const expr = generateRedisConnectionExpr ( staticRedis )
105- const result = evalExpr ( expr ) as Record < string , unknown >
113+ const result = resolveRedisConnection ( { host : '10.0.0.1' , port : 6380 , password : 'build-pw' , db : 2 } )
106114
107115 expect ( result ) . toMatchObject ( {
108116 host : 'new-host' ,
@@ -112,22 +120,18 @@ describe('generateRedisConnectionExpr', () => {
112120 } )
113121 } )
114122
115- it ( 'NUXT_REDIS_URL env takes priority over static url in config ' , ( ) => {
123+ it ( 'NUXT_REDIS_URL env takes priority over static url' , ( ) => {
116124 process . env . NUXT_REDIS_URL = 'redis://env-host:6379'
117- const staticRedis = JSON . stringify ( { url : 'redis://config-host:6379' } )
118- const expr = generateRedisConnectionExpr ( staticRedis )
119- const result = evalExpr ( expr )
125+ const result = resolveRedisConnection ( { url : 'redis://config-host:6379' } )
120126
121- expect ( result ) . toBe ( 'redis://env-host:6379' )
127+ expect ( result ) . toMatchObject ( { url : 'redis://env-host:6379' } )
122128 } )
123129
124130 it ( 'handles lazyConnect and connectTimeout from env' , ( ) => {
125131 process . env . NUXT_REDIS_LAZY_CONNECT = 'true'
126132 process . env . NUXT_REDIS_CONNECT_TIMEOUT = '5000'
127133
128- const staticRedis = JSON . stringify ( { } )
129- const expr = generateRedisConnectionExpr ( staticRedis )
130- const result = evalExpr ( expr ) as Record < string , unknown >
134+ const result = resolveRedisConnection ( { } )
131135
132136 expect ( result ) . toMatchObject ( {
133137 lazyConnect : true ,
@@ -136,9 +140,7 @@ describe('generateRedisConnectionExpr', () => {
136140 } )
137141
138142 it ( 'falls back to defaults when both static and env are empty' , ( ) => {
139- const staticRedis = JSON . stringify ( { } )
140- const expr = generateRedisConnectionExpr ( staticRedis )
141- const result = evalExpr ( expr ) as Record < string , unknown >
143+ const result = resolveRedisConnection ( { } )
142144
143145 expect ( result ) . toEqual ( {
144146 host : '127.0.0.1' ,
@@ -151,11 +153,27 @@ describe('generateRedisConnectionExpr', () => {
151153 } )
152154 } )
153155
154- it ( 'generated expression contains process.env references' , ( ) => {
155- const expr = generateRedisConnectionExpr ( '{}' )
156- expect ( expr ) . toContain ( 'process.env.NUXT_REDIS_URL' )
157- expect ( expr ) . toContain ( 'process.env.NUXT_REDIS_HOST' )
158- expect ( expr ) . toContain ( 'process.env.NUXT_REDIS_PORT' )
159- expect ( expr ) . toContain ( 'process.env.NUXT_REDIS_PASSWORD' )
156+ it ( 'does not include url key when no url is provided' , ( ) => {
157+ const result = resolveRedisConnection ( { host : '10.0.0.1' } )
158+
159+ expect ( result ) . not . toHaveProperty ( 'url' )
160+ } )
161+
162+ it ( 'preserves all options alongside url for setConnection' , ( ) => {
163+ process . env . NUXT_REDIS_URL = 'redis://prod:6379'
164+ process . env . NUXT_REDIS_PASSWORD = 'prod-pw'
165+
166+ const result = resolveRedisConnection ( { host : '127.0.0.1' , connectTimeout : 10000 } )
167+
168+ expect ( result ) . toEqual ( {
169+ url : 'redis://prod:6379' ,
170+ host : '127.0.0.1' ,
171+ port : 6379 ,
172+ password : 'prod-pw' ,
173+ username : undefined ,
174+ db : 0 ,
175+ lazyConnect : undefined ,
176+ connectTimeout : 10000 ,
177+ } )
160178 } )
161179} )
0 commit comments