|
1 | 1 | import { describe, it, expect, beforeEach, afterEach } from 'bun:test'; |
2 | 2 | import { resolveCredential } from '../../src/auth/resolver'; |
| 3 | +import { ensureAuth } from '../../src/auth/setup'; |
| 4 | +import { CLIError } from '../../src/errors/base'; |
3 | 5 | import type { Config } from '../../src/config/schema'; |
4 | 6 | import { mkdirSync, rmSync } from 'fs'; |
5 | 7 | import { join } from 'path'; |
@@ -59,9 +61,54 @@ describe('resolveCredential', () => { |
59 | 61 | await expect(resolveCredential(config)).rejects.toThrow('No credentials found'); |
60 | 62 | }); |
61 | 63 |
|
| 64 | + it('no-credentials hint mentions MMX_CONFIG_DIR and working remediation options', async () => { |
| 65 | + const config = makeConfig(); |
| 66 | + try { |
| 67 | + await resolveCredential(config); |
| 68 | + throw new Error('expected resolveCredential to throw'); |
| 69 | + } catch (err) { |
| 70 | + expect(err).toBeInstanceOf(CLIError); |
| 71 | + const hint = (err as CLIError).hint ?? ''; |
| 72 | + expect(hint).toContain('mmx auth login'); |
| 73 | + expect(hint).toContain('--api-key'); |
| 74 | + expect(hint).toContain('MMX_CONFIG_DIR'); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
62 | 78 | it('prefers flag over file api key', async () => { |
63 | 79 | const config = makeConfig({ apiKey: 'sk-flag', fileApiKey: 'sk-file' }); |
64 | 80 | const cred = await resolveCredential(config); |
65 | 81 | expect(cred.token).toBe('sk-flag'); |
66 | 82 | }); |
67 | 83 | }); |
| 84 | + |
| 85 | +describe('ensureAuth (non-interactive, no credentials)', () => { |
| 86 | + const testDir = join(tmpdir(), `mmx-setup-test-${Date.now()}`); |
| 87 | + const originalConfigDir = process.env.MMX_CONFIG_DIR; |
| 88 | + |
| 89 | + beforeEach(() => { |
| 90 | + mkdirSync(join(testDir, '.mmx'), { recursive: true }); |
| 91 | + process.env.MMX_CONFIG_DIR = join(testDir, '.mmx'); |
| 92 | + }); |
| 93 | + |
| 94 | + afterEach(() => { |
| 95 | + if (originalConfigDir) process.env.MMX_CONFIG_DIR = originalConfigDir; |
| 96 | + else delete process.env.MMX_CONFIG_DIR; |
| 97 | + delete process.env.MINIMAX_API_KEY; |
| 98 | + rmSync(testDir, { recursive: true, force: true }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('no-credentials hint mentions MMX_CONFIG_DIR and working remediation options', async () => { |
| 102 | + const config = makeConfig({ nonInteractive: true }); |
| 103 | + try { |
| 104 | + await ensureAuth(config); |
| 105 | + throw new Error('expected ensureAuth to throw'); |
| 106 | + } catch (err) { |
| 107 | + expect(err).toBeInstanceOf(CLIError); |
| 108 | + const hint = (err as CLIError).hint ?? ''; |
| 109 | + expect(hint).toContain('mmx auth login'); |
| 110 | + expect(hint).toContain('--api-key'); |
| 111 | + expect(hint).toContain('MMX_CONFIG_DIR'); |
| 112 | + } |
| 113 | + }); |
| 114 | +}); |
0 commit comments