|
| 1 | +import { resolveUIDistDir } from '../web-server.js'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +vi.mock('node:fs'); |
| 7 | + |
| 8 | +const existsSync = vi.mocked(fs.existsSync); |
| 9 | + |
| 10 | +describe('resolveUIDistDir', () => { |
| 11 | + const originalEnv = process.env; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + process.env = { ...originalEnv }; |
| 15 | + delete process.env.AGENT_INSPECTOR_PATH; |
| 16 | + existsSync.mockReturnValue(false); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + process.env = originalEnv; |
| 21 | + vi.restoreAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('returns null when no candidate has index.html', () => { |
| 25 | + expect(resolveUIDistDir()).toBeNull(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('returns AGENT_INSPECTOR_PATH when env var is set and dir has index.html', () => { |
| 29 | + const customPath = '/custom/inspector/dist'; |
| 30 | + process.env.AGENT_INSPECTOR_PATH = customPath; |
| 31 | + |
| 32 | + existsSync.mockImplementation(p => p === path.join(customPath, 'index.html')); |
| 33 | + |
| 34 | + expect(resolveUIDistDir()).toBe(customPath); |
| 35 | + }); |
| 36 | + |
| 37 | + it('skips AGENT_INSPECTOR_PATH when env var is set but dir lacks index.html', () => { |
| 38 | + process.env.AGENT_INSPECTOR_PATH = '/missing/inspector'; |
| 39 | + existsSync.mockReturnValue(false); |
| 40 | + |
| 41 | + expect(resolveUIDistDir()).toBeNull(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('returns the first candidate that has index.html', () => { |
| 45 | + existsSync.mockImplementation(p => { |
| 46 | + return String(p).endsWith(path.join('agent-inspector', 'index.html')); |
| 47 | + }); |
| 48 | + |
| 49 | + const result = resolveUIDistDir(); |
| 50 | + expect(result).not.toBeNull(); |
| 51 | + expect(result!).toMatch(/agent-inspector$/); |
| 52 | + }); |
| 53 | + |
| 54 | + it('prefers AGENT_INSPECTOR_PATH over bundled candidates', () => { |
| 55 | + const customPath = '/custom/path'; |
| 56 | + process.env.AGENT_INSPECTOR_PATH = customPath; |
| 57 | + |
| 58 | + existsSync.mockReturnValue(true); |
| 59 | + |
| 60 | + expect(resolveUIDistDir()).toBe(customPath); |
| 61 | + }); |
| 62 | +}); |
0 commit comments