|
1 | 1 | import assert from 'node:assert'; |
| 2 | +import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
2 | 4 | import { afterEach, beforeEach, describe, it } from 'node:test'; |
3 | 5 | import { |
| 6 | + clearServerInfoCache, |
4 | 7 | getPage, |
| 8 | + getServerInfo, |
5 | 9 | setPage, |
6 | 10 | startSnapshotServer, |
7 | 11 | stopSnapshotServer, |
@@ -188,4 +192,106 @@ describe('snapshot-server', () => { |
188 | 192 | assert.strictEqual(response.status, 404); |
189 | 193 | }); |
190 | 194 | }); |
| 195 | + |
| 196 | + describe('getServerInfo()', () => { |
| 197 | + let testDir = join(process.cwd(), '.vizzly-test-temp'); |
| 198 | + |
| 199 | + beforeEach(() => { |
| 200 | + // Clear the cached server info before each test |
| 201 | + clearServerInfoCache(); |
| 202 | + |
| 203 | + // Clean up any existing test directory |
| 204 | + if (existsSync(testDir)) { |
| 205 | + rmSync(testDir, { recursive: true }); |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | + afterEach(() => { |
| 210 | + // Clear cache after tests |
| 211 | + clearServerInfoCache(); |
| 212 | + |
| 213 | + // Clean up test directory |
| 214 | + if (existsSync(testDir)) { |
| 215 | + rmSync(testDir, { recursive: true }); |
| 216 | + } |
| 217 | + }); |
| 218 | + |
| 219 | + it('returns null when no server.json exists in isolated directory', () => { |
| 220 | + // Create an isolated test directory without server.json |
| 221 | + mkdirSync(testDir, { recursive: true }); |
| 222 | + |
| 223 | + let originalCwd = process.cwd(); |
| 224 | + try { |
| 225 | + process.chdir(testDir); |
| 226 | + clearServerInfoCache(); |
| 227 | + |
| 228 | + let info = getServerInfo(); |
| 229 | + |
| 230 | + // In an isolated directory without .vizzly/server.json, should return null |
| 231 | + // (unless there's a server.json in a parent directory) |
| 232 | + if (info !== null) { |
| 233 | + assert.ok(typeof info.url === 'string', 'should have url'); |
| 234 | + assert.ok(typeof info.failOnDiff === 'boolean', 'should have failOnDiff'); |
| 235 | + } |
| 236 | + } finally { |
| 237 | + process.chdir(originalCwd); |
| 238 | + } |
| 239 | + }); |
| 240 | + |
| 241 | + it('reads failOnDiff from server.json when present', () => { |
| 242 | + // Create a temporary .vizzly directory with server.json |
| 243 | + let vizzlyDir = join(testDir, '.vizzly'); |
| 244 | + mkdirSync(vizzlyDir, { recursive: true }); |
| 245 | + |
| 246 | + let serverJson = { |
| 247 | + pid: 12345, |
| 248 | + port: 47392, |
| 249 | + startTime: Date.now(), |
| 250 | + failOnDiff: true, |
| 251 | + }; |
| 252 | + writeFileSync(join(vizzlyDir, 'server.json'), JSON.stringify(serverJson)); |
| 253 | + |
| 254 | + // Change to test directory to test discovery |
| 255 | + let originalCwd = process.cwd(); |
| 256 | + try { |
| 257 | + process.chdir(testDir); |
| 258 | + clearServerInfoCache(); |
| 259 | + |
| 260 | + let info = getServerInfo(); |
| 261 | + |
| 262 | + assert.ok(info !== null, 'should find server.json'); |
| 263 | + assert.strictEqual(info.url, 'http://localhost:47392', 'should have correct url'); |
| 264 | + assert.strictEqual(info.failOnDiff, true, 'should read failOnDiff as true'); |
| 265 | + } finally { |
| 266 | + process.chdir(originalCwd); |
| 267 | + } |
| 268 | + }); |
| 269 | + |
| 270 | + it('defaults failOnDiff to false when not specified in server.json', () => { |
| 271 | + let vizzlyDir = join(testDir, '.vizzly'); |
| 272 | + mkdirSync(vizzlyDir, { recursive: true }); |
| 273 | + |
| 274 | + let serverJson = { |
| 275 | + pid: 12345, |
| 276 | + port: 47393, |
| 277 | + startTime: Date.now(), |
| 278 | + // failOnDiff not specified |
| 279 | + }; |
| 280 | + writeFileSync(join(vizzlyDir, 'server.json'), JSON.stringify(serverJson)); |
| 281 | + |
| 282 | + let originalCwd = process.cwd(); |
| 283 | + try { |
| 284 | + process.chdir(testDir); |
| 285 | + clearServerInfoCache(); |
| 286 | + |
| 287 | + let info = getServerInfo(); |
| 288 | + |
| 289 | + assert.ok(info !== null, 'should find server.json'); |
| 290 | + assert.strictEqual(info.url, 'http://localhost:47393', 'should have correct url'); |
| 291 | + assert.strictEqual(info.failOnDiff, false, 'should default failOnDiff to false'); |
| 292 | + } finally { |
| 293 | + process.chdir(originalCwd); |
| 294 | + } |
| 295 | + }); |
| 296 | + }); |
191 | 297 | }); |
0 commit comments