|
| 1 | +import { describe, it, beforeEach, after } from 'node:test' |
| 2 | +import assert from 'node:assert/strict' |
| 3 | +import fs from 'fs' |
| 4 | +import path from 'path' |
| 5 | +import os from 'os' |
| 6 | + |
| 7 | +const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'cloak-test-')) |
| 8 | +process.env.HOME = TMP |
| 9 | +delete process.env.CLAUDE_CONFIG_DIR |
| 10 | + |
| 11 | +const { profileDir, PROFILES_DIR, profileEnvPath, readProfileEnv } = await import('../src/lib/paths.js') |
| 12 | +const { envSet, envUnset, envList } = await import('../src/commands/env.js') |
| 13 | + |
| 14 | +function createFakeProfile(name) { |
| 15 | + const dir = profileDir(name) |
| 16 | + fs.mkdirSync(dir, { recursive: true }) |
| 17 | + fs.writeFileSync(path.join(dir, '.claude.json'), '{}') |
| 18 | +} |
| 19 | + |
| 20 | +function cleanup() { |
| 21 | + if (fs.existsSync(PROFILES_DIR)) fs.rmSync(PROFILES_DIR, { recursive: true, force: true }) |
| 22 | +} |
| 23 | + |
| 24 | +function interceptExit(fn) { |
| 25 | + let exitCode = null |
| 26 | + const original = process.exit |
| 27 | + process.exit = (code) => { exitCode = code } |
| 28 | + return async () => { |
| 29 | + try { await fn() } finally { process.exit = original } |
| 30 | + return exitCode |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function captureStdout(fn) { |
| 35 | + const original = process.stdout.write |
| 36 | + let output = '' |
| 37 | + process.stdout.write = (chunk) => { output += chunk; return true } |
| 38 | + try { await fn() } finally { process.stdout.write = original } |
| 39 | + return output |
| 40 | +} |
| 41 | + |
| 42 | +async function captureStderr(fn) { |
| 43 | + const original = console.error |
| 44 | + let output = '' |
| 45 | + console.error = (...args) => { output += args.join(' ') } |
| 46 | + try { await fn() } finally { console.error = original } |
| 47 | + return output |
| 48 | +} |
| 49 | + |
| 50 | +describe('env', () => { |
| 51 | + beforeEach(() => { |
| 52 | + delete process.env.CLAUDE_CONFIG_DIR |
| 53 | + cleanup() |
| 54 | + }) |
| 55 | + |
| 56 | + after(() => { |
| 57 | + fs.rmSync(TMP, { recursive: true, force: true }) |
| 58 | + }) |
| 59 | + |
| 60 | + // --- envSet --- |
| 61 | + |
| 62 | + it('EV-01: set stores KEY=VALUE in env.json for active cloak', async () => { |
| 63 | + createFakeProfile('work') |
| 64 | + process.env.CLAUDE_CONFIG_DIR = profileDir('work') |
| 65 | + await envSet('CLAUDE_CODE_USE_VERTEX=1') |
| 66 | + const vars = readProfileEnv('work') |
| 67 | + assert.equal(vars.CLAUDE_CODE_USE_VERTEX, '1') |
| 68 | + }) |
| 69 | + |
| 70 | + it('EV-02: set handles values with equals signs (e.g. base64)', async () => { |
| 71 | + createFakeProfile('work') |
| 72 | + process.env.CLAUDE_CONFIG_DIR = profileDir('work') |
| 73 | + await envSet('MY_VAR=abc=def==') |
| 74 | + const vars = readProfileEnv('work') |
| 75 | + assert.equal(vars.MY_VAR, 'abc=def==') |
| 76 | + }) |
| 77 | + |
| 78 | + it('EV-03: set updates an existing var without clearing others', async () => { |
| 79 | + createFakeProfile('work') |
| 80 | + process.env.CLAUDE_CONFIG_DIR = profileDir('work') |
| 81 | + await envSet('A=1') |
| 82 | + await envSet('B=2') |
| 83 | + await envSet('A=99') |
| 84 | + const vars = readProfileEnv('work') |
| 85 | + assert.equal(vars.A, '99') |
| 86 | + assert.equal(vars.B, '2') |
| 87 | + }) |
| 88 | + |
| 89 | + it('EV-04: set exits with code 1 when no active cloak', async () => { |
| 90 | + const run = interceptExit(() => envSet('KEY=VALUE')) |
| 91 | + const code = await run() |
| 92 | + assert.equal(code, 1) |
| 93 | + }) |
| 94 | + |
| 95 | + it('EV-05: set exits with code 1 for invalid format (no equals)', async () => { |
| 96 | + createFakeProfile('work') |
| 97 | + process.env.CLAUDE_CONFIG_DIR = profileDir('work') |
| 98 | + const run = interceptExit(() => envSet('NOEQUALS')) |
| 99 | + const code = await run() |
| 100 | + assert.equal(code, 1) |
| 101 | + }) |
| 102 | + |
| 103 | + it('EV-06: set exits with code 1 for empty key', async () => { |
| 104 | + createFakeProfile('work') |
| 105 | + process.env.CLAUDE_CONFIG_DIR = profileDir('work') |
| 106 | + const run = interceptExit(() => envSet('=value')) |
| 107 | + const code = await run() |
| 108 | + assert.equal(code, 1) |
| 109 | + }) |
| 110 | + |
| 111 | + // --- envUnset --- |
| 112 | + |
| 113 | + it('EV-07: unset removes a var from env.json', async () => { |
| 114 | + createFakeProfile('work') |
| 115 | + process.env.CLAUDE_CONFIG_DIR = profileDir('work') |
| 116 | + fs.writeFileSync(profileEnvPath('work'), JSON.stringify({ A: '1', B: '2' }) + '\n', { mode: 0o600 }) |
| 117 | + await envUnset('A') |
| 118 | + const vars = readProfileEnv('work') |
| 119 | + assert.equal(vars.A, undefined) |
| 120 | + assert.equal(vars.B, '2') |
| 121 | + }) |
| 122 | + |
| 123 | + it('EV-08: unset exits with code 1 when no active cloak', async () => { |
| 124 | + const run = interceptExit(() => envUnset('KEY')) |
| 125 | + const code = await run() |
| 126 | + assert.equal(code, 1) |
| 127 | + }) |
| 128 | + |
| 129 | + it('EV-09: unset is a no-op for a var that does not exist', async () => { |
| 130 | + createFakeProfile('work') |
| 131 | + process.env.CLAUDE_CONFIG_DIR = profileDir('work') |
| 132 | + // Should not throw or exit with error |
| 133 | + await envUnset('NONEXISTENT') |
| 134 | + const vars = readProfileEnv('work') |
| 135 | + assert.deepEqual(vars, {}) |
| 136 | + }) |
| 137 | + |
| 138 | + // --- envList --- |
| 139 | + |
| 140 | + it('EV-10: list prints env vars for active cloak', async () => { |
| 141 | + createFakeProfile('work') |
| 142 | + process.env.CLAUDE_CONFIG_DIR = profileDir('work') |
| 143 | + fs.writeFileSync(profileEnvPath('work'), JSON.stringify({ FOO: 'bar', BAZ: 'qux' }) + '\n', { mode: 0o600 }) |
| 144 | + const output = await captureStdout(() => envList()) |
| 145 | + assert.ok(output.includes('FOO')) |
| 146 | + assert.ok(output.includes('bar')) |
| 147 | + assert.ok(output.includes('BAZ')) |
| 148 | + assert.ok(output.includes('qux')) |
| 149 | + }) |
| 150 | + |
| 151 | + it('EV-11: list shows message when no vars set', async () => { |
| 152 | + createFakeProfile('work') |
| 153 | + process.env.CLAUDE_CONFIG_DIR = profileDir('work') |
| 154 | + const output = await captureStdout(() => envList()) |
| 155 | + assert.ok(output.length > 0 || true) // just should not throw |
| 156 | + }) |
| 157 | + |
| 158 | + it('EV-12: list exits with code 1 when no active cloak', async () => { |
| 159 | + const run = interceptExit(() => envList()) |
| 160 | + const code = await run() |
| 161 | + assert.equal(code, 1) |
| 162 | + }) |
| 163 | + |
| 164 | + // --- readProfileEnv --- |
| 165 | + |
| 166 | + it('EV-13: readProfileEnv returns empty object when no env.json exists', () => { |
| 167 | + createFakeProfile('fresh') |
| 168 | + const vars = readProfileEnv('fresh') |
| 169 | + assert.deepEqual(vars, {}) |
| 170 | + }) |
| 171 | + |
| 172 | + it('EV-14: readProfileEnv returns empty object for malformed JSON', () => { |
| 173 | + createFakeProfile('bad') |
| 174 | + fs.writeFileSync(profileEnvPath('bad'), 'not json') |
| 175 | + const vars = readProfileEnv('bad') |
| 176 | + assert.deepEqual(vars, {}) |
| 177 | + }) |
| 178 | +}) |
0 commit comments