|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'bun:test'; |
| 2 | +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { |
| 6 | + detectShell, |
| 7 | + getCompletionLine, |
| 8 | + getRcFilePath, |
| 9 | + installCompletions, |
| 10 | +} from './completions.ts'; |
| 11 | + |
| 12 | +describe('completions', () => { |
| 13 | + describe('detectShell', () => { |
| 14 | + let originalShell: string | undefined; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + originalShell = process.env.SHELL; |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + if (originalShell === undefined) { |
| 22 | + delete process.env.SHELL; |
| 23 | + } else { |
| 24 | + process.env.SHELL = originalShell; |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + it('should detect zsh', () => { |
| 29 | + process.env.SHELL = '/bin/zsh'; |
| 30 | + expect(detectShell()).toBe('zsh'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should detect bash', () => { |
| 34 | + process.env.SHELL = '/bin/bash'; |
| 35 | + expect(detectShell()).toBe('bash'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should detect fish', () => { |
| 39 | + process.env.SHELL = '/usr/local/bin/fish'; |
| 40 | + expect(detectShell()).toBe('fish'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return null for unsupported shells', () => { |
| 44 | + process.env.SHELL = '/bin/csh'; |
| 45 | + expect(detectShell()).toBeNull(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return null when SHELL is not set', () => { |
| 49 | + delete process.env.SHELL; |
| 50 | + expect(detectShell()).toBeNull(); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('getRcFilePath', () => { |
| 55 | + it('should return ~/.zshrc for zsh', () => { |
| 56 | + const result = getRcFilePath('zsh'); |
| 57 | + expect(result).toEndWith('.zshrc'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return ~/.bashrc for bash', () => { |
| 61 | + const result = getRcFilePath('bash'); |
| 62 | + expect(result).toEndWith('.bashrc'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return ~/.config/fish/config.fish for fish', () => { |
| 66 | + const result = getRcFilePath('fish'); |
| 67 | + expect(result).toEndWith(join('.config', 'fish', 'config.fish')); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('getCompletionLine', () => { |
| 72 | + it('should return source line for zsh', () => { |
| 73 | + expect(getCompletionLine('zsh')).toBe('source <(mux completions zsh)'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return source line for bash', () => { |
| 77 | + expect(getCompletionLine('bash')).toBe('source <(mux completions bash)'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return source line for fish', () => { |
| 81 | + expect(getCompletionLine('fish')).toBe( |
| 82 | + 'source (mux completions fish | psub)', |
| 83 | + ); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('installCompletions', () => { |
| 88 | + let testDir: string; |
| 89 | + |
| 90 | + beforeEach(async () => { |
| 91 | + testDir = await mkdtemp(join(tmpdir(), 'mux-cli-completions-test-')); |
| 92 | + }); |
| 93 | + |
| 94 | + afterEach(async () => { |
| 95 | + await rm(testDir, { recursive: true, force: true }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should append completion line to an existing rc file', async () => { |
| 99 | + const rcPath = join(testDir, '.zshrc'); |
| 100 | + await writeFile(rcPath, '# existing content\n'); |
| 101 | + |
| 102 | + const result = await installCompletions('zsh', rcPath); |
| 103 | + |
| 104 | + expect(result.installed).toBe(true); |
| 105 | + expect(result.alreadyInstalled).toBe(false); |
| 106 | + |
| 107 | + const content = await readFile(rcPath, 'utf-8'); |
| 108 | + expect(content).toContain('source <(mux completions zsh)'); |
| 109 | + expect(content).toStartWith('# existing content\n'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should create the rc file if it does not exist', async () => { |
| 113 | + const rcPath = join(testDir, '.zshrc'); |
| 114 | + |
| 115 | + const result = await installCompletions('zsh', rcPath); |
| 116 | + |
| 117 | + expect(result.installed).toBe(true); |
| 118 | + const content = await readFile(rcPath, 'utf-8'); |
| 119 | + expect(content).toContain('source <(mux completions zsh)'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should not duplicate if already installed', async () => { |
| 123 | + const rcPath = join(testDir, '.zshrc'); |
| 124 | + await writeFile(rcPath, 'source <(mux completions zsh)\n'); |
| 125 | + |
| 126 | + const result = await installCompletions('zsh', rcPath); |
| 127 | + |
| 128 | + expect(result.installed).toBe(false); |
| 129 | + expect(result.alreadyInstalled).toBe(true); |
| 130 | + |
| 131 | + const content = await readFile(rcPath, 'utf-8'); |
| 132 | + const matches = content.match(/source <\(mux completions zsh\)/g); |
| 133 | + expect(matches).toHaveLength(1); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should detect existing install even with surrounding content', async () => { |
| 137 | + const rcPath = join(testDir, '.bashrc'); |
| 138 | + await writeFile( |
| 139 | + rcPath, |
| 140 | + '# stuff\nsource <(mux completions bash)\n# more stuff\n', |
| 141 | + ); |
| 142 | + |
| 143 | + const result = await installCompletions('bash', rcPath); |
| 144 | + |
| 145 | + expect(result.installed).toBe(false); |
| 146 | + expect(result.alreadyInstalled).toBe(true); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should work for fish shell', async () => { |
| 150 | + const rcPath = join(testDir, 'config.fish'); |
| 151 | + |
| 152 | + const result = await installCompletions('fish', rcPath); |
| 153 | + |
| 154 | + expect(result.installed).toBe(true); |
| 155 | + const content = await readFile(rcPath, 'utf-8'); |
| 156 | + expect(content).toContain('source (mux completions fish | psub)'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should add a trailing newline after the completion line', async () => { |
| 160 | + const rcPath = join(testDir, '.zshrc'); |
| 161 | + await writeFile(rcPath, '# existing\n'); |
| 162 | + |
| 163 | + await installCompletions('zsh', rcPath); |
| 164 | + |
| 165 | + const content = await readFile(rcPath, 'utf-8'); |
| 166 | + expect(content).toEndWith('\n'); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments