|
| 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 } = await import('../src/lib/paths.js') |
| 12 | +const { bindAccount } = await import('../src/commands/bind.js') |
| 13 | +const { unbindAccount } = await import('../src/commands/unbind.js') |
| 14 | + |
| 15 | +const WORK_DIR = path.join(TMP, 'project') |
| 16 | + |
| 17 | +function createFakeProfile(name) { |
| 18 | + const dir = profileDir(name) |
| 19 | + fs.mkdirSync(dir, { recursive: true }) |
| 20 | + fs.writeFileSync(path.join(dir, '.claude.json'), '{}') |
| 21 | +} |
| 22 | + |
| 23 | +function cleanup() { |
| 24 | + if (fs.existsSync(PROFILES_DIR)) fs.rmSync(PROFILES_DIR, { recursive: true, force: true }) |
| 25 | + const cloakFile = path.join(WORK_DIR, '.cloak') |
| 26 | + if (fs.existsSync(cloakFile)) fs.unlinkSync(cloakFile) |
| 27 | +} |
| 28 | + |
| 29 | +function interceptExit(fn) { |
| 30 | + let exitCode = null |
| 31 | + const original = process.exit |
| 32 | + process.exit = (code) => { exitCode = code } |
| 33 | + return async () => { |
| 34 | + try { await fn() } finally { process.exit = original } |
| 35 | + return exitCode |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +describe('bind', () => { |
| 40 | + beforeEach(() => { |
| 41 | + cleanup() |
| 42 | + if (!fs.existsSync(WORK_DIR)) fs.mkdirSync(WORK_DIR, { recursive: true }) |
| 43 | + }) |
| 44 | + |
| 45 | + after(() => { |
| 46 | + fs.rmSync(TMP, { recursive: true, force: true }) |
| 47 | + }) |
| 48 | + |
| 49 | + it('BI-01: bind creates .cloak file with profile name', async () => { |
| 50 | + createFakeProfile('work') |
| 51 | + await bindAccount('work', WORK_DIR) |
| 52 | + const content = fs.readFileSync(path.join(WORK_DIR, '.cloak'), 'utf8') |
| 53 | + assert.equal(content.trim(), 'work') |
| 54 | + }) |
| 55 | + |
| 56 | + it('BI-02: bind fails for non-existent profile', async () => { |
| 57 | + const run = interceptExit(() => bindAccount('ghost', WORK_DIR)) |
| 58 | + const code = await run() |
| 59 | + assert.equal(code, 1) |
| 60 | + assert.ok(!fs.existsSync(path.join(WORK_DIR, '.cloak'))) |
| 61 | + }) |
| 62 | + |
| 63 | + it('BI-03: bind fails for invalid name', async () => { |
| 64 | + const run = interceptExit(() => bindAccount('../bad', WORK_DIR)) |
| 65 | + const code = await run() |
| 66 | + assert.equal(code, 1) |
| 67 | + assert.ok(!fs.existsSync(path.join(WORK_DIR, '.cloak'))) |
| 68 | + }) |
| 69 | + |
| 70 | + it('BI-04: unbind removes .cloak file', async () => { |
| 71 | + fs.writeFileSync(path.join(WORK_DIR, '.cloak'), 'work') |
| 72 | + await unbindAccount(WORK_DIR) |
| 73 | + assert.ok(!fs.existsSync(path.join(WORK_DIR, '.cloak'))) |
| 74 | + }) |
| 75 | + |
| 76 | + it('BI-05: unbind fails when no .cloak file', async () => { |
| 77 | + const run = interceptExit(() => unbindAccount(WORK_DIR)) |
| 78 | + const code = await run() |
| 79 | + assert.equal(code, 1) |
| 80 | + }) |
| 81 | + |
| 82 | + it('BI-06: .cloak file contains only the profile name', async () => { |
| 83 | + createFakeProfile('my-project') |
| 84 | + await bindAccount('my-project', WORK_DIR) |
| 85 | + const content = fs.readFileSync(path.join(WORK_DIR, '.cloak'), 'utf8') |
| 86 | + assert.equal(content, 'my-project\n') |
| 87 | + }) |
| 88 | +}) |
0 commit comments