-
Notifications
You must be signed in to change notification settings - Fork 0
feat(multi-account): codev account CLI + global-default (Batch 2a + 2e) #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5d0ac66
feat(multi-account): codev account CLI + Vitest + CI (Batch 2a)
grimmerk d77f2f9
feat(multi-account): global-default resume routing (Batch 2e)
grimmerk 9b03b98
fix(multi-account): address CodeRabbit + cubic review (Batch 2a/2e)
grimmerk 3c9b0a4
fix(multi-account): validate configDirEnv in accounts.sh gen
grimmerk f7d4b4c
fix(multi-account): reject non-string/empty configDirEnv in gen
grimmerk 26db4d2
fix(multi-account): claude-whoami reports the dispatcher default
grimmerk a7f34a8
docs(multi-account): shell-snapshot dispatcher caveat
grimmerk f6a1544
fix(multi-account): validate labels in gen + remove-message nit
grimmerk 4f030b3
docs(multi-account): symlink share mechanism verified
grimmerk 8a6dee8
docs(multi-account): collision policy + per-entry vs whole-dir links
grimmerk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| name: hello-world | ||
| name: test | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| pull_request: | ||
| branches: | ||
| - master | ||
| - staging | ||
| - main | ||
| paths: | ||
| - README.md | ||
|
|
||
| branches: [main] | ||
| push: | ||
| branches: [main] | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| my-job: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: my-step | ||
| run: echo "Hello World!" | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 22 | ||
| cache: yarn | ||
| - name: Install dependencies | ||
| run: yarn install --frozen-lockfile | ||
| - name: Run tests | ||
| run: yarn test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import { execFileSync } from 'child_process'; | ||
| import { writeFileSync } from 'fs'; | ||
| import * as os from 'os'; | ||
| import * as path from 'path'; | ||
| import { describe, it, expect } from 'vitest'; | ||
|
|
||
| import { | ||
| generateAccountsSh, | ||
| resolveDefaultLabel, | ||
| type Registry, | ||
| } from './account-manager'; | ||
|
|
||
| const HOME = os.homedir(); | ||
|
|
||
| /** A representative two-account registry (personal default + work extra). */ | ||
| function reg(overrides: Partial<Registry> = {}): Registry { | ||
| return { | ||
| version: 1, | ||
| defaultAccount: 'personal', | ||
| accounts: [ | ||
| { | ||
| label: 'personal', | ||
| dir: path.join(HOME, '.claude'), | ||
| identityFile: path.join(HOME, '.claude.json'), | ||
| configDirEnv: null, | ||
| isDefault: true, | ||
| }, | ||
| { | ||
| label: 'work', | ||
| dir: path.join(HOME, '.claude-work'), | ||
| identityFile: path.join(HOME, '.claude-work', '.claude.json'), | ||
| configDirEnv: path.join(HOME, '.claude-work'), | ||
| isDefault: false, | ||
| }, | ||
| ], | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe('generateAccountsSh', () => { | ||
| it('launches the default account with CLAUDE_CONFIG_DIR unset', () => { | ||
| const sh = generateAccountsSh(reg()); | ||
| expect(sh).toContain( | ||
| 'claude-personal() { env -u CLAUDE_CONFIG_DIR claude "$@"; }', | ||
| ); | ||
| }); | ||
|
|
||
| it('launches an extra account with CLAUDE_CONFIG_DIR ($HOME-relative)', () => { | ||
| const sh = generateAccountsSh(reg()); | ||
| expect(sh).toContain( | ||
| 'claude-work() { env CLAUDE_CONFIG_DIR="$HOME/.claude-work" claude "$@"; }', | ||
| ); | ||
| }); | ||
|
|
||
| it('adds a dispatcher case per account', () => { | ||
| const sh = generateAccountsSh(reg()); | ||
| expect(sh).toContain( | ||
| 'personal) shift; env -u CLAUDE_CONFIG_DIR claude "$@" ;;', | ||
| ); | ||
| expect(sh).toContain( | ||
| 'work) shift; env CLAUDE_CONFIG_DIR="$HOME/.claude-work" claude "$@" ;;', | ||
| ); | ||
| }); | ||
|
|
||
| it('routes the dispatcher fallback (*) to the current defaultAccount', () => { | ||
| // defaultAccount = personal (the anchor) -> unset CLAUDE_CONFIG_DIR | ||
| expect(generateAccountsSh(reg())).toContain( | ||
| '*) env -u CLAUDE_CONFIG_DIR claude "$@" ;;', | ||
| ); | ||
| // defaultAccount = work -> bare `claude` must set work's config dir | ||
| expect(generateAccountsSh(reg({ defaultAccount: 'work' }))).toContain( | ||
| '*) env CLAUDE_CONFIG_DIR="$HOME/.claude-work" claude "$@" ;;', | ||
| ); | ||
| }); | ||
|
|
||
| it('rejects an account dir with shell-unsafe characters', () => { | ||
| const bad = reg({ | ||
| defaultAccount: 'evil', | ||
| accounts: [ | ||
| { | ||
| label: 'evil', | ||
| dir: '/tmp/$(touch pwned)', | ||
| identityFile: '/tmp/x/.claude.json', | ||
| configDirEnv: '/tmp/$(touch pwned)', | ||
| isDefault: false, | ||
| }, | ||
| ], | ||
| }); | ||
| expect(() => generateAccountsSh(bad)).toThrow(/Unsafe shell characters/); | ||
| }); | ||
|
|
||
| it('reports the default account (not ambient env) in claude-whoami', () => { | ||
| // default = personal -> whoami reports personal, auth under the anchor | ||
| const sh = generateAccountsSh(reg()); | ||
| expect(sh).toContain("bare 'claude' here -> personal"); | ||
| expect(sh).toContain('env -u CLAUDE_CONFIG_DIR claude auth status'); | ||
| // default = work -> whoami reports work + auth under work's env | ||
| const shWork = generateAccountsSh(reg({ defaultAccount: 'work' })); | ||
| expect(shWork).toContain("bare 'claude' here -> work"); | ||
| expect(shWork).toContain( | ||
| 'env CLAUDE_CONFIG_DIR="$HOME/.claude-work" claude auth status', | ||
| ); | ||
| }); | ||
|
|
||
| it('emits syntactically valid shell (bash -n)', () => { | ||
| const sh = generateAccountsSh(reg()); | ||
| const file = path.join(os.tmpdir(), `codev-accounts-${process.pid}.sh`); | ||
| writeFileSync(file, sh); | ||
| expect(() => execFileSync('bash', ['-n', file])).not.toThrow(); | ||
| }); | ||
|
|
||
| it('rejects an unsafe configDirEnv even when dir is clean', () => { | ||
| const bad = reg({ | ||
| defaultAccount: 'evil', | ||
| accounts: [ | ||
| { | ||
| label: 'evil', | ||
| dir: '/tmp/clean', | ||
| identityFile: '/tmp/clean/.claude.json', | ||
| configDirEnv: '/tmp/$(touch pwned)', | ||
| isDefault: false, | ||
| }, | ||
| ], | ||
| }); | ||
| expect(() => generateAccountsSh(bad)).toThrow(/Unsafe shell characters/); | ||
| }); | ||
|
|
||
| it('rejects a shell-unsafe label (hand-edited registry)', () => { | ||
| const bad = reg({ | ||
| defaultAccount: 'x', | ||
| accounts: [ | ||
| { | ||
| label: 'x; touch pwned', | ||
| dir: '/tmp/clean', | ||
| identityFile: '/tmp/clean/.claude.json', | ||
| configDirEnv: '/tmp/clean', | ||
| isDefault: false, | ||
| }, | ||
| ], | ||
| }); | ||
| expect(() => generateAccountsSh(bad)).toThrow(/Invalid account label/); | ||
| }); | ||
|
|
||
| it('rejects an empty configDirEnv (hand-edited registry)', () => { | ||
| const bad = reg({ | ||
| defaultAccount: 'x', | ||
| accounts: [ | ||
| { | ||
| label: 'x', | ||
| dir: '/tmp/x', | ||
| identityFile: '/tmp/x/.claude.json', | ||
| configDirEnv: '', | ||
| isDefault: false, | ||
| }, | ||
| ], | ||
| }); | ||
| expect(() => generateAccountsSh(bad)).toThrow(/Invalid account path/); | ||
| }); | ||
| }); | ||
|
|
||
| describe('resolveDefaultLabel', () => { | ||
| it('returns defaultAccount when it names a real account', () => { | ||
| expect(resolveDefaultLabel(reg({ defaultAccount: 'work' }))).toBe('work'); | ||
| }); | ||
|
|
||
| it('falls back to the isDefault anchor when defaultAccount is missing', () => { | ||
| expect(resolveDefaultLabel(reg({ defaultAccount: undefined }))).toBe( | ||
| 'personal', | ||
| ); | ||
| }); | ||
|
|
||
| it('falls back to the isDefault anchor when defaultAccount is unknown', () => { | ||
| expect(resolveDefaultLabel(reg({ defaultAccount: 'ghost' }))).toBe( | ||
| 'personal', | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.