Repository navigation
Expand file tree
/
Copy pathcli.test.js
More file actions
176 lines (155 loc) · 6 KB
/
Copy pathcli.test.js
File metadata and controls
176 lines (155 loc) · 6 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const { execSync } = require('child_process');
/**
* Execute CLI command and return result
* @param {string} args - Command line arguments
* @param {Object} options - Execution options
* @returns {Object} Result with stdout, stderr, and exitCode
*/
function runCli(args = '', options = {}) {
try {
const stdout = execSync(`node cli.js ${args}`, {
encoding: 'utf8',
...options,
});
return { stdout: stdout.trim(), stderr: '', exitCode: 0 };
} catch (error) {
return {
stdout: (error.stdout || '').trim(),
stderr: (error.stderr || '').trim(),
exitCode: error.status || 1,
};
}
}
describe('Timezones CLI', () => {
describe('Help functionality', () => {
test('should show help with --help flag', () => {
const result = runCli('--help');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('USAGE:');
expect(result.stdout).toContain('timezones-cli');
});
test('should show help with -h flag', () => {
const result = runCli('-h');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('USAGE:');
expect(result.stdout).toContain('timezones-cli');
});
});
describe('Date conversion functionality', () => {
test('should convert specific date correctly in Europe/Berlin timezone', () => {
const result = runCli('"2024-01-15 10:30"', {
env: { ...process.env, TZ: 'Europe/Berlin' }
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe(`
PST (-8:00) 2024-01-15 01:30 AM Los Angeles
PDT/MST (-7:00) 2024-01-15 02:30 AM Phoenix, Denver
CST/MDT (-6:00) 2024-01-15 03:30 AM Chicago
CDT/EST (-5:00) 2024-01-15 04:30 AM New York
EDT (-4:00) 2024-01-15 05:30 AM
UTC 2024-01-15 09:30 AM
GMT+1 (+1:00) 2024-01-15 10:30 AM Berlin (LOCAL)`.trim());
});
test('should convert Unix timestamp correctly in Europe/Berlin timezone', () => {
// January 15, 2024, 10:30:00 Europe/Berlin = 1705309800000
const result = runCli('1705309800000', {
env: { ...process.env, TZ: 'Europe/Berlin' }
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe(`
PST (-8:00) 2024-01-15 01:30 AM Los Angeles
PDT/MST (-7:00) 2024-01-15 02:30 AM Phoenix, Denver
CST/MDT (-6:00) 2024-01-15 03:30 AM Chicago
CDT/EST (-5:00) 2024-01-15 04:30 AM New York
EDT (-4:00) 2024-01-15 05:30 AM
UTC 2024-01-15 09:30 AM
GMT+1 (+1:00) 2024-01-15 10:30 AM Berlin (LOCAL)`.trim());
});
test('should show local timezone marker when matching predefined timezone', () => {
const result = runCli('"2024-01-15 10:30"', {
env: { ...process.env, TZ: 'America/New_York' }
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe(`
PST (-8:00) 2024-01-15 07:30 AM Los Angeles
PDT/MST (-7:00) 2024-01-15 08:30 AM Phoenix, Denver
CST/MDT (-6:00) 2024-01-15 09:30 AM Chicago
CDT/EST (-5:00) 2024-01-15 10:30 AM New York (LOCAL)
EDT (-4:00) 2024-01-15 11:30 AM
UTC 2024-01-15 03:30 PM`.trim());
});
test('should add separate row for non-matching local timezone', () => {
const result = runCli('"2024-01-15 10:30"', {
env: { ...process.env, TZ: 'Asia/Tokyo' }
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe(`
PST (-8:00) 2024-01-15 05:30 AM Los Angeles
PDT/MST (-7:00) 2024-01-15 06:30 AM Phoenix, Denver
CST/MDT (-6:00) 2024-01-15 07:30 AM Chicago
CDT/EST (-5:00) 2024-01-15 08:30 AM New York
EDT (-4:00) 2024-01-15 09:30 AM
UTC 2024-01-15 01:30 PM
GMT+9 (+9:00) 2024-01-15 10:30 PM Tokyo (LOCAL)`.trim());
});
test('should handle summer time (DST) correctly', () => {
const result = runCli('"2024-07-15 10:30"', {
env: { ...process.env, TZ: 'America/Los_Angeles' }
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe(`
PST (-8:00) 2024-07-15 09:30 AM
PDT/MST (-7:00) 2024-07-15 10:30 AM Los Angeles, Phoenix (LOCAL)
CST/MDT (-6:00) 2024-07-15 11:30 AM Denver
CDT/EST (-5:00) 2024-07-15 12:30 PM Chicago
EDT (-4:00) 2024-07-15 01:30 PM New York
UTC 2024-07-15 05:30 PM`.trim());
});
});
describe('Current time functionality', () => {
test('should use current time when no arguments provided', () => {
const result = runCli('', {
env: {
...process.env,
TZ: 'Europe/Berlin',
TEST_CURRENT_TIME: '2024-01-15T10:30:00.000Z'
}
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe(`
PST (-8:00) 2024-01-15 02:30 AM Los Angeles
PDT/MST (-7:00) 2024-01-15 03:30 AM Phoenix, Denver
CST/MDT (-6:00) 2024-01-15 04:30 AM Chicago
CDT/EST (-5:00) 2024-01-15 05:30 AM New York
EDT (-4:00) 2024-01-15 06:30 AM
UTC 2024-01-15 10:30 AM
GMT+1 (+1:00) 2024-01-15 11:30 AM Berlin (LOCAL)`.trim());
});
test('should use current time with different timezone', () => {
const result = runCli('', {
env: {
...process.env,
TZ: 'America/New_York',
TEST_CURRENT_TIME: '2024-01-15T15:30:00.000Z'
}
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe(`
PST (-8:00) 2024-01-15 07:30 AM Los Angeles
PDT/MST (-7:00) 2024-01-15 08:30 AM Phoenix, Denver
CST/MDT (-6:00) 2024-01-15 09:30 AM Chicago
CDT/EST (-5:00) 2024-01-15 10:30 AM New York (LOCAL)
EDT (-4:00) 2024-01-15 11:30 AM
UTC 2024-01-15 03:30 PM`.trim());
});
});
describe('Error handling', () => {
test('should show error message and help for invalid date', () => {
const result = runCli('"invalid-date"');
expect(result.exitCode).toBe(1);
expect(result.stderr).toBe('Error: Invalid date format: invalid-date');
expect(result.stdout).toContain('USAGE:');
expect(result.stdout).toContain('timezones-cli');
});
});
});