Open History to see the last commit
1import test from 'node:test'
2import assert from 'node:assert/strict'
3import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
4import { tmpdir } from 'node:os'
5import path from 'node:path'
6import {
7 createDevConfig,
8 createPrintableConfig,
9 createSpawnConfig,
10 getPnpmCommand,
11 loadEnvFileIntoEnv,
12 pickLanAddress,
13 spawnProcess,
14} from './dev.mjs'
15
16const packageJson = JSON.parse(
17 await readFile(new URL('../package.json', import.meta.url), 'utf8'),
18)
19
20test('pickLanAddress prefers non-internal private IPv4 addresses', () => {
21 const address = pickLanAddress({
22 loopback: [{ family: 'IPv4', address: '127.0.0.1', internal: true }],
23 wifi: [{ family: 'IPv4', address: '192.168.1.23', internal: false }],
24 vpn: [{ family: 'IPv4', address: '100.64.0.8', internal: false }],
25 })
26
27 assert.equal(address, '192.168.1.23')
28})
29
30test('pickLanAddress prefers Wi-Fi over earlier WSL virtual adapters', () => {
31 const address = pickLanAddress({
32 'vEthernet (WSL)': [
33 { family: 'IPv4', address: '172.28.48.1', internal: false },
34 ],
35 'Wi-Fi': [{ family: 'IPv4', address: '192.168.1.68', internal: false }],
36 })
37
38 assert.equal(address, '192.168.1.68')
39})
40
41test('createDevConfig uses provided backend URL before auto-detecting LAN IP', () => {
42 const config = createDevConfig({
43 env: { EXPO_PUBLIC_TANSTACK_AI_BASE_URL: 'http://10.0.2.2:8787' },
44 networkInterfaces: {
45 wifi: [{ family: 'IPv4', address: '192.168.1.23', internal: false }],
46 },
47 platform: 'win32',
48 })
49
50 assert.equal(config.baseUrl, 'http://10.0.2.2:8787')
51 assert.equal(config.pnpmCommand, 'pnpm.cmd')
52})
53
54test('createDevConfig sets Expo backend URL from detected LAN IP', () => {
55 const config = createDevConfig({
56 env: {},
57 networkInterfaces: {
58 'Wi-Fi': [{ family: 'IPv4', address: '192.168.1.23', internal: false }],
59 },
60 platform: 'linux',
61 })
62
63 assert.equal(config.baseUrl, 'http://192.168.1.23:8787')
64 assert.equal(config.lanInterfaceName, 'Wi-Fi')
65 assert.equal(config.serverEnv.PORT, '8787')
66 assert.equal(config.expoEnv.EXPO_PUBLIC_TANSTACK_AI_BASE_URL, config.baseUrl)
67 assert.equal(config.expoEnv.EXPO_NO_DOTENV, '1')
68 assert.equal(config.packagerHostname, undefined)
69 assert.equal(config.metroHostSource, 'expo')
70 assert.equal(config.expoEnv.REACT_NATIVE_PACKAGER_HOSTNAME, undefined)
71})
72
73test('createDevConfig preserves user-provided packager hostname', () => {
74 const config = createDevConfig({
75 env: { REACT_NATIVE_PACKAGER_HOSTNAME: '10.0.0.50' },
76 networkInterfaces: {
77 wifi: [{ family: 'IPv4', address: '192.168.1.23', internal: false }],
78 },
79 })
80
81 assert.equal(config.packagerHostname, '10.0.0.50')
82 assert.equal(config.metroHostSource, 'environment')
83 assert.equal(config.expoEnv.REACT_NATIVE_PACKAGER_HOSTNAME, '10.0.0.50')
84})
85
86test('createDevConfig omits packager hostname when no LAN IP or override exists', () => {
87 const config = createDevConfig({
88 env: {},
89 networkInterfaces: {
90 loopback: [{ family: 'IPv4', address: '127.0.0.1', internal: true }],
91 },
92 })
93
94 assert.equal(config.packagerHostname, undefined)
95 assert.equal(config.metroHostSource, 'expo')
96 assert.equal(config.expoEnv.REACT_NATIVE_PACKAGER_HOSTNAME, undefined)
97})
98
99test('createDevConfig uses PORT for server env and generated Expo backend URL', () => {
100 const config = createDevConfig({
101 env: { PORT: '9999' },
102 networkInterfaces: {
103 wifi: [{ family: 'IPv4', address: '192.168.1.23', internal: false }],
104 },
105 })
106
107 assert.equal(config.baseUrl, 'http://192.168.1.23:9999')
108 assert.equal(config.serverEnv.PORT, '9999')
109 assert.equal(config.expoEnv.EXPO_PUBLIC_TANSTACK_AI_BASE_URL, config.baseUrl)
110})
111
112test('loadEnvFileIntoEnv loads server-only env values without exposing them to Expo', async () => {
113 const tempDir = await mkdtemp(path.join(tmpdir(), 'tanstack-ai-rn-env-'))
114 const envFile = path.join(tempDir, '.env')
115
116 try {
117 await writeFile(
118 envFile,
119 [
120 'OPENAI_API_KEY=sk-test-from-env-file',
121 'OPENAI_MODEL=gpt-5.2',
122 'SERVER_ONLY_FLAG=private',
123 'EXPO_PUBLIC_TANSTACK_AI_BASE_URL=http://10.0.2.2:8787',
124 ].join('\n'),
125 )
126
127 const env = { PATH: 'test-path' }
128 const loaded = loadEnvFileIntoEnv(envFile, env)
129 const config = createDevConfig({
130 env,
131 networkInterfaces: {
132 wifi: [{ family: 'IPv4', address: '192.168.1.23', internal: false }],
133 },
134 })
135
136 assert.equal(loaded, true)
137 assert.equal(config.serverEnv.OPENAI_API_KEY, 'sk-test-from-env-file')
138 assert.equal(config.serverEnv.OPENAI_MODEL, 'gpt-5.2')
139 assert.equal(config.serverEnv.SERVER_ONLY_FLAG, 'private')
140 assert.equal(config.expoEnv.OPENAI_API_KEY, undefined)
141 assert.equal(config.expoEnv.OPENAI_MODEL, undefined)
142 assert.equal(config.expoEnv.SERVER_ONLY_FLAG, undefined)
143 assert.equal(
144 config.expoEnv.EXPO_PUBLIC_TANSTACK_AI_BASE_URL,
145 config.baseUrl,
146 )
147 assert.equal(config.expoEnv.EXPO_NO_DOTENV, '1')
148 assert.equal(config.baseUrl, 'http://10.0.2.2:8787')
149 } finally {
150 await rm(tempDir, { force: true, recursive: true })
151 }
152})
153
154test('createDevConfig strips inherited OpenAI env from Expo process env', () => {
155 const config = createDevConfig({
156 env: {
157 OPENAI_API_KEY: 'sk-inherited',
158 OPENAI_MODEL: 'gpt-5.2',
159 PATH: 'test-path',
160 },
161 networkInterfaces: {
162 wifi: [{ family: 'IPv4', address: '192.168.1.23', internal: false }],
163 },
164 })
165
166 assert.equal(config.serverEnv.OPENAI_API_KEY, 'sk-inherited')
167 assert.equal(config.serverEnv.OPENAI_MODEL, 'gpt-5.2')
168 assert.equal(config.expoEnv.OPENAI_API_KEY, undefined)
169 assert.equal(config.expoEnv.OPENAI_MODEL, undefined)
170 assert.equal(config.expoEnv.PATH, 'test-path')
171 assert.equal(config.expoEnv.EXPO_NO_DOTENV, '1')
172})
173
174test('createPrintableConfig reports secret presence without secret values', () => {
175 const config = createDevConfig({
176 env: {
177 OPENAI_API_KEY: 'sk-secret-value',
178 PATH: 'test-path',
179 },
180 networkInterfaces: {
181 wifi: [{ family: 'IPv4', address: '192.168.1.23', internal: false }],
182 },
183 })
184 const printable = createPrintableConfig(config)
185 const serialized = JSON.stringify(printable)
186
187 assert.equal(printable.serverOpenAIKeyLoaded, true)
188 assert.equal(serialized.includes('sk-secret-value'), false)
189 assert.equal(serialized.includes('OPENAI_API_KEY'), false)
190})
191
192test('getPnpmCommand resolves Windows command shim', () => {
193 assert.equal(getPnpmCommand('win32'), 'pnpm.cmd')
194 assert.equal(getPnpmCommand('linux'), 'pnpm')
195})
196
197test('createSpawnConfig routes Windows command shims through cmd.exe', () => {
198 const config = createSpawnConfig({
199 command: 'pnpm.cmd',
200 args: ['exec', 'expo', 'start', '--lan', '--clear'],
201 cwd: 'C:\\repo\\examples\\ts-react-native-chat',
202 env: { PATH: 'C:\\Windows\\System32' },
203 platform: 'win32',
204 })
205
206 assert.equal(config.command, 'cmd.exe')
207 assert.deepEqual(config.args, [
208 '/d',
209 '/s',
210 '/c',
211 'pnpm.cmd',
212 'exec',
213 'expo',
214 'start',
215 '--lan',
216 '--clear',
217 ])
218 assert.equal(config.options.cwd, 'C:\\repo\\examples\\ts-react-native-chat')
219 assert.deepEqual(config.options.env, { PATH: 'C:\\Windows\\System32' })
220 assert.equal(config.options.stdio, 'inherit')
221 assert.equal(config.options.windowsHide, false)
222})
223
224test('createSpawnConfig preserves direct spawning on non-Windows platforms', () => {
225 const config = createSpawnConfig({
226 command: 'pnpm',
227 args: ['--version'],
228 cwd: '/repo/examples/ts-react-native-chat',
229 env: { PATH: '/usr/bin' },
230 platform: 'linux',
231 })
232
233 assert.equal(config.command, 'pnpm')
234 assert.deepEqual(config.args, ['--version'])
235 assert.equal(config.options.cwd, '/repo/examples/ts-react-native-chat')
236 assert.deepEqual(config.options.env, { PATH: '/usr/bin' })
237 assert.equal(config.options.stdio, 'inherit')
238 assert.equal(config.options.windowsHide, undefined)
239})
240
241test('direct Expo package scripts disable dotenv loading and strip OpenAI env', () => {
242 const directExpoScripts = ['dev:app', 'smoke:expo']
243
244 for (const scriptName of directExpoScripts) {
245 const script = packageJson.scripts[scriptName]
246
247 assert.match(script, /EXPO_NO_DOTENV/u)
248 assert.match(script, /delete env\.OPENAI_API_KEY/u)
249 assert.match(script, /delete env\.OPENAI_MODEL/u)
250 assert.match(script, /pnpm/u)
251 assert.match(script, /exec/u)
252 assert.match(script, /expo/u)
253 }
254})
255
256test(
257 'spawnProcess can run a Windows command shim',
258 { skip: process.platform !== 'win32' },
259 async () => {
260 const child = spawnProcess(
261 getPnpmCommand('win32'),
262 ['--version'],
263 {
264 ...process.env,
265 },
266 {
267 stdio: 'pipe',
268 },
269 )
270
271 let output = ''
272 child.stdout?.on('data', (chunk) => {
273 output += chunk
274 })
275
276 const exitCode = await new Promise((resolve, reject) => {
277 child.on('error', reject)
278 child.on('exit', resolve)
279 })
280
281 assert.equal(exitCode, 0)
282 assert.match(output.trim(), /^\d+\.\d+\.\d+/u)
283 },
284)