|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + getOpencodeAuthPath, |
| 5 | + getOpencodeConfigDir, |
| 6 | + getOpencodeDataDir, |
| 7 | + getPiAgentDir, |
| 8 | + getPiAuthPath, |
| 9 | + getPiSettingsPath, |
| 10 | + resolveGlobalConfigPath, |
| 11 | +} from '../xdg-paths.js'; |
| 12 | + |
| 13 | +const HOME = '/home/user'; |
| 14 | + |
| 15 | +describe('xdg-paths', () => { |
| 16 | + const originalEnv = process.env; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + // Clear relevant env vars before each test |
| 20 | + delete process.env.PI_CODING_AGENT_DIR; |
| 21 | + delete process.env.OPENCODE_CONFIG; |
| 22 | + delete process.env.OPENCODE_CONFIG_DIR; |
| 23 | + delete process.env.XDG_CONFIG_HOME; |
| 24 | + delete process.env.XDG_DATA_HOME; |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + process.env = originalEnv; |
| 29 | + }); |
| 30 | + |
| 31 | + /* ─── Pi paths ───────────────────────────────────────────────────────── */ |
| 32 | + |
| 33 | + describe('getPiAgentDir', () => { |
| 34 | + it('defaults to ~/.pi/agent', () => { |
| 35 | + expect(getPiAgentDir(HOME)).toBe('/home/user/.pi/agent'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('honors PI_CODING_AGENT_DIR', () => { |
| 39 | + process.env.PI_CODING_AGENT_DIR = '/custom/pi/dir'; |
| 40 | + expect(getPiAgentDir(HOME)).toBe('/custom/pi/dir'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('tilde-expands PI_CODING_AGENT_DIR', () => { |
| 44 | + process.env.PI_CODING_AGENT_DIR = '~/custom/pi'; |
| 45 | + expect(getPiAgentDir(HOME)).toBe('/home/user/custom/pi'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('tilde-expands Windows-style backslash', () => { |
| 49 | + process.env.PI_CODING_AGENT_DIR = '~\\custom\\pi'; |
| 50 | + // On Unix the backslash is preserved in the remainder; on Windows path.join |
| 51 | + // produces backslashes. We verify tilde is expanded, not path normalization. |
| 52 | + expect(getPiAgentDir(HOME)).toMatch(/^\/home\/user[/\\]custom[/\\]pi$/); |
| 53 | + }); |
| 54 | + |
| 55 | + it('handles bare tilde', () => { |
| 56 | + process.env.PI_CODING_AGENT_DIR = '~'; |
| 57 | + expect(getPiAgentDir(HOME)).toBe('/home/user'); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('getPiAuthPath', () => { |
| 62 | + it('resolves to agentDir/auth.json', () => { |
| 63 | + expect(getPiAuthPath(HOME)).toBe('/home/user/.pi/agent/auth.json'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('follows PI_CODING_AGENT_DIR override', () => { |
| 67 | + process.env.PI_CODING_AGENT_DIR = '/other/pi'; |
| 68 | + expect(getPiAuthPath(HOME)).toBe('/other/pi/auth.json'); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('getPiSettingsPath', () => { |
| 73 | + it('resolves to agentDir/settings.json', () => { |
| 74 | + expect(getPiSettingsPath(HOME)).toBe('/home/user/.pi/agent/settings.json'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('follows PI_CODING_AGENT_DIR override', () => { |
| 78 | + process.env.PI_CODING_AGENT_DIR = '/other/pi'; |
| 79 | + expect(getPiSettingsPath(HOME)).toBe('/other/pi/settings.json'); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + /* ─── OpenCode paths ─────────────────────────────────────────────────── */ |
| 84 | + |
| 85 | + describe('getOpencodeConfigDir', () => { |
| 86 | + it('defaults to ~/.config/opencode', () => { |
| 87 | + expect(getOpencodeConfigDir(HOME)).toBe('/home/user/.config/opencode'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('honors XDG_CONFIG_HOME', () => { |
| 91 | + process.env.XDG_CONFIG_HOME = '/custom/config'; |
| 92 | + expect(getOpencodeConfigDir(HOME)).toBe('/custom/config/opencode'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('prefers OPENCODE_CONFIG_DIR over XDG_CONFIG_HOME', () => { |
| 96 | + process.env.OPENCODE_CONFIG_DIR = '/oc/dir'; |
| 97 | + process.env.XDG_CONFIG_HOME = '/xdg/config'; |
| 98 | + expect(getOpencodeConfigDir(HOME)).toBe('/oc/dir'); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('getOpencodeDataDir', () => { |
| 103 | + it('defaults to ~/.local/share/opencode', () => { |
| 104 | + expect(getOpencodeDataDir(HOME)).toBe('/home/user/.local/share/opencode'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('honors XDG_DATA_HOME', () => { |
| 108 | + process.env.XDG_DATA_HOME = '/custom/data'; |
| 109 | + expect(getOpencodeDataDir(HOME)).toBe('/custom/data/opencode'); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('getOpencodeAuthPath', () => { |
| 114 | + it('resolves to dataDir/auth.json', () => { |
| 115 | + expect(getOpencodeAuthPath(HOME)).toBe('/home/user/.local/share/opencode/auth.json'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('follows XDG_DATA_HOME override', () => { |
| 119 | + process.env.XDG_DATA_HOME = '/other/data'; |
| 120 | + expect(getOpencodeAuthPath(HOME)).toBe('/other/data/opencode/auth.json'); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('resolveGlobalConfigPath', () => { |
| 125 | + it('honors OPENCODE_CONFIG env var', async () => { |
| 126 | + process.env.OPENCODE_CONFIG = '/explicit/opencode.json'; |
| 127 | + const result = await resolveGlobalConfigPath(HOME, async () => false); |
| 128 | + expect(result).toBe('/explicit/opencode.json'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('prefers existing .jsonc over .json', async () => { |
| 132 | + const exists = vi.fn(async (p: string) => p.endsWith('.jsonc')); |
| 133 | + const result = await resolveGlobalConfigPath(HOME, exists); |
| 134 | + expect(result).toBe('/home/user/.config/opencode/opencode.jsonc'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('falls back to .json when .jsonc missing', async () => { |
| 138 | + const exists = vi.fn(async (p: string) => p.endsWith('.json')); |
| 139 | + const result = await resolveGlobalConfigPath(HOME, exists); |
| 140 | + expect(result).toBe('/home/user/.config/opencode/opencode.json'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('returns .json path when neither exists', async () => { |
| 144 | + const exists = vi.fn(async () => false); |
| 145 | + const result = await resolveGlobalConfigPath(HOME, exists); |
| 146 | + expect(result).toBe('/home/user/.config/opencode/opencode.json'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('follows XDG_CONFIG_HOME for config dir', async () => { |
| 150 | + process.env.XDG_CONFIG_HOME = '/xdg/config'; |
| 151 | + const exists = vi.fn(async () => false); |
| 152 | + const result = await resolveGlobalConfigPath(HOME, exists); |
| 153 | + expect(result).toBe('/xdg/config/opencode/opencode.json'); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments