@urugus/slack-cli 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/.claude/settings.local.json +2 -1
  2. package/README.md +7 -0
  3. package/dist/commands/history.d.ts.map +1 -1
  4. package/dist/commands/history.js +7 -7
  5. package/dist/commands/history.js.map +1 -1
  6. package/dist/commands/send.d.ts.map +1 -1
  7. package/dist/commands/send.js +7 -11
  8. package/dist/commands/send.js.map +1 -1
  9. package/dist/types/commands.d.ts +1 -0
  10. package/dist/types/commands.d.ts.map +1 -1
  11. package/dist/utils/constants.d.ts +1 -0
  12. package/dist/utils/constants.d.ts.map +1 -1
  13. package/dist/utils/constants.js +1 -0
  14. package/dist/utils/constants.js.map +1 -1
  15. package/dist/utils/profile-config.d.ts.map +1 -1
  16. package/dist/utils/profile-config.js +2 -6
  17. package/dist/utils/profile-config.js.map +1 -1
  18. package/dist/utils/slack-api-client.d.ts +1 -1
  19. package/dist/utils/slack-api-client.d.ts.map +1 -1
  20. package/dist/utils/slack-api-client.js +2 -2
  21. package/dist/utils/slack-api-client.js.map +1 -1
  22. package/dist/utils/slack-operations/channel-operations.d.ts +9 -0
  23. package/dist/utils/slack-operations/channel-operations.d.ts.map +1 -1
  24. package/dist/utils/slack-operations/channel-operations.js +77 -50
  25. package/dist/utils/slack-operations/channel-operations.js.map +1 -1
  26. package/dist/utils/slack-operations/message-operations.d.ts +1 -1
  27. package/dist/utils/slack-operations/message-operations.d.ts.map +1 -1
  28. package/dist/utils/slack-operations/message-operations.js +7 -3
  29. package/dist/utils/slack-operations/message-operations.js.map +1 -1
  30. package/dist/utils/token-utils.d.ts +7 -0
  31. package/dist/utils/token-utils.d.ts.map +1 -0
  32. package/dist/utils/token-utils.js +18 -0
  33. package/dist/utils/token-utils.js.map +1 -0
  34. package/dist/utils/validators.d.ts +79 -0
  35. package/dist/utils/validators.d.ts.map +1 -0
  36. package/dist/utils/validators.js +175 -0
  37. package/dist/utils/validators.js.map +1 -0
  38. package/package.json +1 -1
  39. package/src/commands/history.ts +17 -12
  40. package/src/commands/send.ts +10 -11
  41. package/src/types/commands.ts +1 -0
  42. package/src/utils/constants.ts +1 -0
  43. package/src/utils/profile-config.ts +3 -15
  44. package/src/utils/slack-api-client.ts +6 -2
  45. package/src/utils/slack-operations/channel-operations.ts +91 -54
  46. package/src/utils/slack-operations/message-operations.ts +14 -4
  47. package/src/utils/token-utils.ts +17 -0
  48. package/src/utils/validators.ts +212 -0
  49. package/tests/commands/send.test.ts +69 -2
  50. package/tests/utils/option-parsers.test.ts +173 -0
  51. package/tests/utils/profile-config.test.ts +282 -0
  52. package/tests/utils/slack-operations/channel-operations-refactored.test.ts +179 -0
  53. package/tests/utils/token-utils.test.ts +33 -0
  54. package/tests/utils/validators.test.ts +307 -0
  55. package/src/utils/profile-config-refactored.ts +0 -161
@@ -0,0 +1,212 @@
1
+ import { Command } from 'commander';
2
+ import { ERROR_MESSAGES } from './constants';
3
+
4
+ /**
5
+ * Common validation functions for CLI commands
6
+ */
7
+
8
+ export interface ValidationRule<T = unknown> {
9
+ validate: (value: T) => boolean | string;
10
+ errorMessage?: string;
11
+ }
12
+
13
+ export interface ValidationOptions {
14
+ required?: boolean;
15
+ rules?: ValidationRule[];
16
+ }
17
+
18
+ /**
19
+ * Validates that a value exists (not undefined, null, or empty string)
20
+ */
21
+ export function validateRequired(value: unknown, fieldName: string): string | null {
22
+ if (value === undefined || value === null || value === '') {
23
+ return `${fieldName} is required`;
24
+ }
25
+ return null;
26
+ }
27
+
28
+ /**
29
+ * Validates mutually exclusive options
30
+ */
31
+ export function validateMutuallyExclusive(
32
+ options: Record<string, unknown>,
33
+ fields: string[],
34
+ errorMessage?: string
35
+ ): string | null {
36
+ const presentFields = fields.filter((field) => options[field] !== undefined);
37
+ if (presentFields.length > 1) {
38
+ return errorMessage || `Cannot use both ${presentFields.join(' and ')}`;
39
+ }
40
+ if (presentFields.length === 0) {
41
+ return errorMessage || `Must specify one of: ${fields.join(', ')}`;
42
+ }
43
+ return null;
44
+ }
45
+
46
+ /**
47
+ * Validates string format using regex
48
+ */
49
+ export function validateFormat(
50
+ value: string,
51
+ pattern: RegExp,
52
+ errorMessage: string
53
+ ): string | null {
54
+ if (!pattern.test(value)) {
55
+ return errorMessage;
56
+ }
57
+ return null;
58
+ }
59
+
60
+ /**
61
+ * Validates numeric range
62
+ */
63
+ export function validateRange(
64
+ value: number,
65
+ min?: number,
66
+ max?: number,
67
+ fieldName = 'Value'
68
+ ): string | null {
69
+ if (min !== undefined && value < min) {
70
+ return `${fieldName} must be at least ${min}`;
71
+ }
72
+ if (max !== undefined && value > max) {
73
+ return `${fieldName} must be at most ${max}`;
74
+ }
75
+ return null;
76
+ }
77
+
78
+ /**
79
+ * Validates date format
80
+ */
81
+ export function validateDateFormat(dateString: string): string | null {
82
+ const date = new Date(dateString);
83
+ if (isNaN(date.getTime())) {
84
+ return 'Invalid date format';
85
+ }
86
+ return null;
87
+ }
88
+
89
+ /**
90
+ * Common format validators
91
+ */
92
+ export const formatValidators = {
93
+ /**
94
+ * Validates Slack thread timestamp format (1234567890.123456)
95
+ */
96
+ threadTimestamp: (value: string): string | null => {
97
+ const pattern = /^\d{10}\.\d{6}$/;
98
+ return validateFormat(value, pattern, ERROR_MESSAGES.INVALID_THREAD_TIMESTAMP);
99
+ },
100
+
101
+ /**
102
+ * Validates Slack channel ID format (C1234567890, D1234567890, G1234567890)
103
+ */
104
+ channelId: (value: string): string | null => {
105
+ const pattern = /^[CDG][A-Z0-9]{10,}$/;
106
+ return validateFormat(value, pattern, 'Invalid channel ID format');
107
+ },
108
+
109
+ /**
110
+ * Validates output format options
111
+ */
112
+ outputFormat: (value: string): string | null => {
113
+ const validFormats = ['table', 'simple', 'json', 'compact'];
114
+ if (!validFormats.includes(value)) {
115
+ return `Invalid format. Must be one of: ${validFormats.join(', ')}`;
116
+ }
117
+ return null;
118
+ },
119
+ };
120
+
121
+ /**
122
+ * Creates a preAction hook for command validation
123
+ */
124
+ export function createValidationHook(
125
+ validations: Array<(options: Record<string, unknown>, command: Command) => string | null>
126
+ ): (thisCommand: Command) => void {
127
+ return (thisCommand: Command) => {
128
+ const options = thisCommand.opts();
129
+
130
+ for (const validation of validations) {
131
+ const error = validation(options, thisCommand);
132
+ if (error) {
133
+ thisCommand.error(`Error: ${error}`);
134
+ break; // Stop processing after first error
135
+ }
136
+ }
137
+ };
138
+ }
139
+
140
+ /**
141
+ * Common command option validators
142
+ */
143
+ export const optionValidators = {
144
+ /**
145
+ * Validates message/file options for send command
146
+ */
147
+ messageOrFile: (options: Record<string, unknown>): string | null => {
148
+ if (!options.message && !options.file) {
149
+ return ERROR_MESSAGES.NO_MESSAGE_OR_FILE;
150
+ }
151
+ if (options.message && options.file) {
152
+ return ERROR_MESSAGES.BOTH_MESSAGE_AND_FILE;
153
+ }
154
+ return null;
155
+ },
156
+
157
+ /**
158
+ * Validates thread timestamp if provided
159
+ */
160
+ threadTimestamp: (options: Record<string, unknown>): string | null => {
161
+ if (options.thread) {
162
+ return formatValidators.threadTimestamp(options.thread as string);
163
+ }
164
+ return null;
165
+ },
166
+
167
+ /**
168
+ * Validates message count for history command
169
+ */
170
+ messageCount: (options: Record<string, unknown>): string | null => {
171
+ if (options.number) {
172
+ const count = parseInt(options.number as string, 10);
173
+ if (isNaN(count)) {
174
+ return 'Message count must be a number';
175
+ }
176
+ return validateRange(count, 1, 1000, 'Message count');
177
+ }
178
+ return null;
179
+ },
180
+
181
+ /**
182
+ * Validates date format for history command
183
+ */
184
+ sinceDate: (options: Record<string, unknown>): string | null => {
185
+ if (options.since) {
186
+ const error = validateDateFormat(options.since as string);
187
+ if (error) {
188
+ return 'Invalid date format. Use YYYY-MM-DD HH:MM:SS';
189
+ }
190
+ }
191
+ return null;
192
+ },
193
+ };
194
+
195
+ /**
196
+ * Creates a validated option parser
197
+ */
198
+ export function createOptionParser<T>(
199
+ parser: (value: string | undefined, defaultValue: T) => T,
200
+ validator?: (value: T) => string | null
201
+ ): (value: string | undefined, defaultValue: T) => T {
202
+ return (value: string | undefined, defaultValue: T): T => {
203
+ const parsed = parser(value, defaultValue);
204
+ if (validator) {
205
+ const error = validator(parsed);
206
+ if (error) {
207
+ throw new Error(error);
208
+ }
209
+ }
210
+ return parsed;
211
+ };
212
+ }
@@ -47,7 +47,7 @@ describe('send command', () => {
47
47
 
48
48
  await program.parseAsync(['node', 'slack-cli', 'send', '-c', 'general', '-m', 'Hello, World!']);
49
49
 
50
- expect(mockSlackClient.sendMessage).toHaveBeenCalledWith('general', 'Hello, World!');
50
+ expect(mockSlackClient.sendMessage).toHaveBeenCalledWith('general', 'Hello, World!', undefined);
51
51
  expect(mockConsole.logSpy).toHaveBeenCalledWith(expect.stringContaining(SUCCESS_MESSAGES.MESSAGE_SENT('general')));
52
52
  });
53
53
 
@@ -84,7 +84,74 @@ describe('send command', () => {
84
84
  await program.parseAsync(['node', 'slack-cli', 'send', '-c', 'general', '-f', 'message.txt']);
85
85
 
86
86
  expect(fs.readFile).toHaveBeenCalledWith('message.txt', 'utf-8');
87
- expect(mockSlackClient.sendMessage).toHaveBeenCalledWith('general', fileContent);
87
+ expect(mockSlackClient.sendMessage).toHaveBeenCalledWith('general', fileContent, undefined);
88
+ });
89
+ });
90
+
91
+ describe('send reply to thread', () => {
92
+ it('should send a reply to a thread with --thread option', async () => {
93
+ vi.mocked(mockConfigManager.getConfig).mockResolvedValue({
94
+ token: 'test-token',
95
+ updatedAt: new Date().toISOString()
96
+ });
97
+ vi.mocked(mockSlackClient.sendMessage).mockResolvedValue({
98
+ ok: true,
99
+ ts: '1234567890.123456'
100
+ });
101
+
102
+ await program.parseAsync(['node', 'slack-cli', 'send', '-c', 'general', '-m', 'Reply to thread', '--thread', '1719207629.000100']);
103
+
104
+ expect(mockSlackClient.sendMessage).toHaveBeenCalledWith('general', 'Reply to thread', '1719207629.000100');
105
+ expect(mockConsole.logSpy).toHaveBeenCalledWith(expect.stringContaining(SUCCESS_MESSAGES.MESSAGE_SENT('general')));
106
+ });
107
+
108
+ it('should send a reply to a thread with -t option', async () => {
109
+ vi.mocked(mockConfigManager.getConfig).mockResolvedValue({
110
+ token: 'test-token',
111
+ updatedAt: new Date().toISOString()
112
+ });
113
+ vi.mocked(mockSlackClient.sendMessage).mockResolvedValue({
114
+ ok: true,
115
+ ts: '1234567890.123456'
116
+ });
117
+
118
+ await program.parseAsync(['node', 'slack-cli', 'send', '-c', 'general', '-m', 'Reply to thread', '-t', '1719207629.000100']);
119
+
120
+ expect(mockSlackClient.sendMessage).toHaveBeenCalledWith('general', 'Reply to thread', '1719207629.000100');
121
+ expect(mockConsole.logSpy).toHaveBeenCalledWith(expect.stringContaining(SUCCESS_MESSAGES.MESSAGE_SENT('general')));
122
+ });
123
+
124
+ it('should validate thread timestamp format', async () => {
125
+ vi.mocked(mockConfigManager.getConfig).mockResolvedValue({
126
+ token: 'test-token',
127
+ updatedAt: new Date().toISOString()
128
+ });
129
+
130
+ const sendCommand = setupSendCommand();
131
+ sendCommand.exitOverride();
132
+
133
+ await expect(
134
+ sendCommand.parseAsync(['-c', 'general', '-m', 'Reply', '-t', 'invalid-timestamp'], { from: 'user' })
135
+ ).rejects.toThrow(ERROR_MESSAGES.INVALID_THREAD_TIMESTAMP);
136
+ });
137
+
138
+ it('should send a reply to a thread with file content', async () => {
139
+ const fileContent = 'Reply from file\nLine 2';
140
+ vi.mocked(fs.readFile).mockResolvedValue(fileContent);
141
+ vi.mocked(mockConfigManager.getConfig).mockResolvedValue({
142
+ token: 'test-token',
143
+ updatedAt: new Date().toISOString()
144
+ });
145
+ vi.mocked(mockSlackClient.sendMessage).mockResolvedValue({
146
+ ok: true,
147
+ ts: '1234567890.123456'
148
+ });
149
+
150
+ await program.parseAsync(['node', 'slack-cli', 'send', '-c', 'general', '-f', 'reply.txt', '-t', '1719207629.000100']);
151
+
152
+ expect(fs.readFile).toHaveBeenCalledWith('reply.txt', 'utf-8');
153
+ expect(mockSlackClient.sendMessage).toHaveBeenCalledWith('general', fileContent, '1719207629.000100');
154
+ expect(mockConsole.logSpy).toHaveBeenCalledWith(expect.stringContaining(SUCCESS_MESSAGES.MESSAGE_SENT('general')));
88
155
  });
89
156
  });
90
157
 
@@ -0,0 +1,173 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import {
3
+ parseFormat,
4
+ parseLimit,
5
+ parseBoolean,
6
+ parseCount,
7
+ parseProfile,
8
+ parseListOptions,
9
+ OPTION_DEFAULTS,
10
+ } from '../../src/utils/option-parsers';
11
+
12
+ describe('option-parsers', () => {
13
+ describe('parseFormat', () => {
14
+ it('should return default format when undefined', () => {
15
+ expect(parseFormat(undefined)).toBe('table');
16
+ });
17
+
18
+ it('should return custom default format when specified', () => {
19
+ expect(parseFormat(undefined, 'json')).toBe('json');
20
+ });
21
+
22
+ it('should return provided format', () => {
23
+ expect(parseFormat('compact')).toBe('compact');
24
+ });
25
+
26
+ it('should handle empty string', () => {
27
+ expect(parseFormat('')).toBe('table');
28
+ });
29
+ });
30
+
31
+ describe('parseLimit', () => {
32
+ it('should return default limit when undefined', () => {
33
+ expect(parseLimit(undefined, 100)).toBe(100);
34
+ });
35
+
36
+ it('should parse string limit', () => {
37
+ expect(parseLimit('50', 100)).toBe(50);
38
+ });
39
+
40
+ it('should handle invalid number string', () => {
41
+ expect(parseLimit('abc', 100)).toBe(NaN);
42
+ });
43
+
44
+ it('should handle empty string', () => {
45
+ expect(parseLimit('', 100)).toBe(100);
46
+ });
47
+ });
48
+
49
+ describe('parseBoolean', () => {
50
+ it('should return default value when undefined', () => {
51
+ expect(parseBoolean(undefined)).toBe(false);
52
+ expect(parseBoolean(undefined, true)).toBe(true);
53
+ });
54
+
55
+ it('should return provided boolean value', () => {
56
+ expect(parseBoolean(true)).toBe(true);
57
+ expect(parseBoolean(false)).toBe(false);
58
+ });
59
+
60
+ it('should handle explicit false over default true', () => {
61
+ expect(parseBoolean(false, true)).toBe(false);
62
+ });
63
+ });
64
+
65
+ describe('parseCount', () => {
66
+ it('should return default count when undefined', () => {
67
+ expect(parseCount(undefined, 10)).toBe(10);
68
+ });
69
+
70
+ it('should parse string count', () => {
71
+ expect(parseCount('25', 10)).toBe(25);
72
+ });
73
+
74
+ it('should handle invalid number string', () => {
75
+ expect(parseCount('invalid', 10)).toBe(10);
76
+ });
77
+
78
+ it('should enforce minimum value', () => {
79
+ expect(parseCount('5', 10, 10, 100)).toBe(10);
80
+ });
81
+
82
+ it('should enforce maximum value', () => {
83
+ expect(parseCount('150', 10, 10, 100)).toBe(100);
84
+ });
85
+
86
+ it('should allow values within range', () => {
87
+ expect(parseCount('50', 10, 10, 100)).toBe(50);
88
+ });
89
+
90
+ it('should handle empty string', () => {
91
+ expect(parseCount('', 20)).toBe(20);
92
+ });
93
+ });
94
+
95
+ describe('parseProfile', () => {
96
+ it('should return undefined when not provided', () => {
97
+ expect(parseProfile(undefined)).toBeUndefined();
98
+ });
99
+
100
+ it('should return provided profile', () => {
101
+ expect(parseProfile('production')).toBe('production');
102
+ });
103
+
104
+ it('should handle empty string', () => {
105
+ expect(parseProfile('')).toBe('');
106
+ });
107
+ });
108
+
109
+ describe('parseListOptions', () => {
110
+ it('should use all defaults when no options provided', () => {
111
+ const result = parseListOptions({});
112
+ expect(result).toEqual({
113
+ format: 'table',
114
+ limit: 100,
115
+ countOnly: false,
116
+ });
117
+ });
118
+
119
+ it('should override specific options', () => {
120
+ const result = parseListOptions({
121
+ format: 'json',
122
+ limit: '50',
123
+ countOnly: true,
124
+ });
125
+ expect(result).toEqual({
126
+ format: 'json',
127
+ limit: 50,
128
+ countOnly: true,
129
+ });
130
+ });
131
+
132
+ it('should use custom defaults', () => {
133
+ const result = parseListOptions({}, {
134
+ format: 'compact',
135
+ limit: 200,
136
+ countOnly: true,
137
+ });
138
+ expect(result).toEqual({
139
+ format: 'compact',
140
+ limit: 200,
141
+ countOnly: true,
142
+ });
143
+ });
144
+
145
+ it('should override custom defaults with provided options', () => {
146
+ const result = parseListOptions(
147
+ {
148
+ format: 'json',
149
+ },
150
+ {
151
+ format: 'compact',
152
+ limit: 200,
153
+ }
154
+ );
155
+ expect(result).toEqual({
156
+ format: 'json',
157
+ limit: 200,
158
+ countOnly: false,
159
+ });
160
+ });
161
+ });
162
+
163
+ describe('OPTION_DEFAULTS', () => {
164
+ it('should have expected default values', () => {
165
+ expect(OPTION_DEFAULTS).toEqual({
166
+ format: 'table',
167
+ limit: 100,
168
+ countOnly: false,
169
+ includeArchived: false,
170
+ });
171
+ });
172
+ });
173
+ });