-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.test.ts
More file actions
141 lines (113 loc) · 4.56 KB
/
Copy pathcli.test.ts
File metadata and controls
141 lines (113 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Tests for CLI Entry Point
* Tests the main CLI structure and command registration
*/
import { Command } from 'commander';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { ConfigService } from '../../src/storage/config';
jest.mock('chalk', () => {
const mockChalk: Record<string, unknown> & { level: number } = {
level: 3,
red: (s: string) => s,
green: (s: string) => s,
blue: (s: string) => s,
yellow: (s: string) => s,
gray: (s: string) => s,
bold: (s: string) => s,
};
return { __esModule: true, default: mockChalk };
});
describe('CLI Entry Point', () => {
let program: Command;
beforeEach(() => {
// Create a fresh program for each test
program = new Command();
program
.name('deepl')
.description('DeepL CLI - Next-generation translation tool powered by DeepL API')
.version('0.1.0');
// Register commands (simulating the actual CLI structure)
program.command('translate').description('Translate text or files');
program.command('auth').description('Manage DeepL API authentication');
program.command('config').description('Manage configuration');
program.command('cache').description('Manage translation cache');
program.command('glossary').description('Manage translation glossaries');
});
describe('program structure', () => {
it('should create a Commander program', () => {
expect(program).toBeInstanceOf(Command);
});
it('should have correct name and description', () => {
expect(program.name()).toBe('deepl');
expect(program.description()).toContain('DeepL CLI');
});
it('should have version', () => {
expect(program.version()).toBeDefined();
});
});
describe('output.color config', () => {
let testConfigDir: string;
let chalk: { level: number };
beforeEach(() => {
chalk = jest.requireMock<{ default: { level: number } }>('chalk').default;
chalk.level = 3;
testConfigDir = path.join(os.tmpdir(), `.deepl-cli-test-color-${Date.now()}-${Math.random().toString(36).slice(2)}`);
fs.mkdirSync(testConfigDir, { recursive: true });
});
afterEach(() => {
if (fs.existsSync(testConfigDir)) {
fs.rmSync(testConfigDir, { recursive: true, force: true });
}
});
it('should set chalk.level to 0 when output.color is false', () => {
const configPath = path.join(testConfigDir, 'config.json');
fs.writeFileSync(configPath, JSON.stringify({
auth: {},
api: { baseUrl: 'https://api.deepl.com' },
defaults: { targetLangs: [], formality: 'default', preserveFormatting: true },
cache: { enabled: true, maxSize: 1073741824, ttl: 2592000 },
output: { format: 'text', verbose: false, color: false },
watch: { debounceMs: 500, autoCommit: false, pattern: '*.md' },
}, null, 2));
const configService = new ConfigService(configPath);
const colorEnabled = configService.getValue<boolean>('output.color');
expect(colorEnabled).toBe(false);
// Replicate the logic from src/cli/index.ts preAction hook
if (colorEnabled === false) {
chalk.level = 0;
}
expect(chalk.level).toBe(0);
});
it('should not change chalk.level when output.color is true', () => {
const configPath = path.join(testConfigDir, 'config.json');
fs.writeFileSync(configPath, JSON.stringify({
auth: {},
api: { baseUrl: 'https://api.deepl.com' },
defaults: { targetLangs: [], formality: 'default', preserveFormatting: true },
cache: { enabled: true, maxSize: 1073741824, ttl: 2592000 },
output: { format: 'text', verbose: false, color: true },
watch: { debounceMs: 500, autoCommit: false, pattern: '*.md' },
}, null, 2));
const configService = new ConfigService(configPath);
const colorEnabled = configService.getValue<boolean>('output.color');
expect(colorEnabled).toBe(true);
if (colorEnabled === false) {
chalk.level = 0;
}
expect(chalk.level).toBe(3);
});
it('should not change chalk.level when output.color defaults to true', () => {
const configPath = path.join(testConfigDir, 'config.json');
// No config file written -- ConfigService falls back to defaults
const configService = new ConfigService(configPath);
const colorEnabled = configService.getValue<boolean>('output.color');
expect(colorEnabled).toBe(true);
if (colorEnabled === false) {
chalk.level = 0;
}
expect(chalk.level).toBe(3);
});
});
});