@poetora/cli 0.0.1 → 0.1.2

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 (117) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/LICENSE +93 -0
  3. package/bin/accessibility.js +2 -2
  4. package/bin/cli-builder.d.ts +8 -0
  5. package/bin/cli-builder.js +178 -0
  6. package/bin/cli.d.ts +5 -11
  7. package/bin/cli.js +8 -200
  8. package/bin/commands/base.command.d.ts +13 -0
  9. package/bin/commands/base.command.js +40 -0
  10. package/bin/commands/check.command.d.ts +14 -0
  11. package/bin/commands/check.command.js +21 -0
  12. package/bin/commands/dev.command.d.ts +13 -0
  13. package/bin/commands/dev.command.js +40 -0
  14. package/bin/commands/index.d.ts +6 -0
  15. package/bin/commands/index.js +6 -0
  16. package/bin/commands/init.command.d.ts +16 -0
  17. package/bin/commands/init.command.js +88 -0
  18. package/bin/commands/link.command.d.ts +13 -0
  19. package/bin/commands/link.command.js +19 -0
  20. package/bin/commands/update.command.d.ts +10 -0
  21. package/bin/commands/update.command.js +13 -0
  22. package/bin/constants.js +1 -1
  23. package/bin/errors/cli-error.d.ts +26 -0
  24. package/bin/errors/cli-error.js +53 -0
  25. package/bin/errors/index.d.ts +1 -0
  26. package/bin/errors/index.js +1 -0
  27. package/bin/index.d.ts +1 -1
  28. package/bin/index.js +6 -6
  29. package/bin/mdxAccessibility.js +2 -2
  30. package/bin/services/accessibility-check.service.d.ts +10 -0
  31. package/bin/services/accessibility-check.service.js +144 -0
  32. package/bin/services/index.d.ts +7 -0
  33. package/bin/services/index.js +7 -0
  34. package/bin/services/link.service.d.ts +7 -0
  35. package/bin/services/link.service.js +40 -0
  36. package/bin/services/openapi-check.service.d.ts +7 -0
  37. package/bin/services/openapi-check.service.js +43 -0
  38. package/bin/services/port.service.d.ts +7 -0
  39. package/bin/services/port.service.js +26 -0
  40. package/bin/services/template.service.d.ts +22 -0
  41. package/bin/services/template.service.js +127 -0
  42. package/bin/services/update.service.d.ts +10 -0
  43. package/bin/services/update.service.js +57 -0
  44. package/bin/services/version.service.d.ts +16 -0
  45. package/bin/services/version.service.js +102 -0
  46. package/bin/types/common.d.ts +38 -0
  47. package/bin/types/common.js +21 -0
  48. package/bin/types/index.d.ts +2 -0
  49. package/bin/types/index.js +2 -0
  50. package/bin/types/options.d.ts +23 -0
  51. package/bin/types/options.js +1 -0
  52. package/bin/utils/console-logger.d.ts +16 -0
  53. package/bin/utils/console-logger.js +65 -0
  54. package/bin/utils/index.d.ts +2 -0
  55. package/bin/utils/index.js +2 -0
  56. package/bin/utils/logger.interface.d.ts +15 -0
  57. package/bin/utils/logger.interface.js +1 -0
  58. package/package.json +29 -29
  59. package/src/accessibility.ts +2 -2
  60. package/src/cli-builder.ts +267 -0
  61. package/src/cli.ts +15 -0
  62. package/src/commands/__tests__/base.command.test.ts +145 -0
  63. package/src/commands/__tests__/dev.command.test.ts +241 -0
  64. package/src/commands/__tests__/init.command.test.ts +281 -0
  65. package/{__test__ → src/commands/__tests__}/utils.ts +1 -1
  66. package/src/commands/base.command.ts +97 -0
  67. package/src/commands/check.command.ts +40 -0
  68. package/src/commands/dev.command.ts +63 -0
  69. package/src/commands/index.ts +6 -0
  70. package/src/commands/init.command.ts +125 -0
  71. package/src/commands/link.command.ts +39 -0
  72. package/src/commands/update.command.ts +23 -0
  73. package/src/constants.ts +1 -1
  74. package/src/errors/cli-error.ts +83 -0
  75. package/src/errors/index.ts +1 -0
  76. package/src/index.ts +6 -6
  77. package/src/mdxAccessibility.ts +3 -4
  78. package/src/services/__tests__/port.service.test.ts +83 -0
  79. package/src/services/__tests__/template.service.test.ts +234 -0
  80. package/src/services/__tests__/version.service.test.ts +165 -0
  81. package/src/services/accessibility-check.service.ts +226 -0
  82. package/src/services/index.ts +7 -0
  83. package/src/services/link.service.ts +65 -0
  84. package/src/services/openapi-check.service.ts +68 -0
  85. package/src/services/port.service.ts +47 -0
  86. package/src/services/template.service.ts +203 -0
  87. package/src/services/update.service.ts +76 -0
  88. package/src/services/version.service.ts +161 -0
  89. package/src/types/common.ts +53 -0
  90. package/src/types/index.ts +2 -0
  91. package/src/types/options.ts +42 -0
  92. package/src/utils/console-logger.ts +114 -0
  93. package/src/utils/index.ts +2 -0
  94. package/src/utils/logger.interface.ts +70 -0
  95. package/tsconfig.build.json +2 -1
  96. package/tsconfig.json +1 -1
  97. package/.prettierignore +0 -2
  98. package/__test__/brokenLinks.test.ts +0 -93
  99. package/__test__/checkPort.test.ts +0 -92
  100. package/__test__/openApiCheck.test.ts +0 -127
  101. package/__test__/update.test.ts +0 -108
  102. package/bin/accessibilityCheck.d.ts +0 -2
  103. package/bin/accessibilityCheck.js +0 -70
  104. package/bin/helpers.d.ts +0 -17
  105. package/bin/helpers.js +0 -104
  106. package/bin/init.d.ts +0 -1
  107. package/bin/init.js +0 -73
  108. package/bin/mdxLinter.d.ts +0 -2
  109. package/bin/mdxLinter.js +0 -45
  110. package/bin/update.d.ts +0 -3
  111. package/bin/update.js +0 -32
  112. package/src/accessibilityCheck.tsx +0 -145
  113. package/src/cli.tsx +0 -302
  114. package/src/helpers.tsx +0 -131
  115. package/src/init.tsx +0 -93
  116. package/src/mdxLinter.tsx +0 -88
  117. package/src/update.tsx +0 -37
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Base CLI error class for all custom errors
3
+ */
4
+ export class CliError extends Error {
5
+ constructor(
6
+ message: string,
7
+ public readonly code: string,
8
+ public readonly exitCode: number = 1
9
+ ) {
10
+ super(message);
11
+ this.name = 'CliError';
12
+ Error.captureStackTrace(this, this.constructor);
13
+ }
14
+ }
15
+
16
+ /**
17
+ * Error thrown when environment requirements are not met
18
+ */
19
+ export class InvalidEnvironmentError extends CliError {
20
+ constructor(message: string) {
21
+ super(message, 'INVALID_ENVIRONMENT', 1);
22
+ this.name = 'InvalidEnvironmentError';
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Error thrown when no available port is found
28
+ */
29
+ export class NoAvailablePortError extends CliError {
30
+ constructor(message: string) {
31
+ super(message, 'NO_AVAILABLE_PORT', 1);
32
+ this.name = 'NoAvailablePortError';
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Error thrown when configuration file is not found
38
+ */
39
+ export class ConfigNotFoundError extends CliError {
40
+ constructor(message: string) {
41
+ super(message, 'CONFIG_NOT_FOUND', 1);
42
+ this.name = 'ConfigNotFoundError';
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Error thrown when command validation fails
48
+ */
49
+ export class ValidationError extends CliError {
50
+ constructor(
51
+ message: string,
52
+ public readonly field?: string
53
+ ) {
54
+ super(message, 'VALIDATION_ERROR', 1);
55
+ this.name = 'ValidationError';
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Error thrown when file operations fail
61
+ */
62
+ export class FileSystemError extends CliError {
63
+ constructor(
64
+ message: string,
65
+ public readonly filePath?: string
66
+ ) {
67
+ super(message, 'FILE_SYSTEM_ERROR', 1);
68
+ this.name = 'FileSystemError';
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Error thrown when external service calls fail
74
+ */
75
+ export class ExternalServiceError extends CliError {
76
+ constructor(
77
+ message: string,
78
+ public readonly service?: string
79
+ ) {
80
+ super(message, 'EXTERNAL_SERVICE_ERROR', 1);
81
+ this.name = 'ExternalServiceError';
82
+ }
83
+ }
@@ -0,0 +1 @@
1
+ export * from './cli-error.js';
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
- import { spawn, type ChildProcess } from 'child_process';
3
- import path from 'path';
4
- import { fileURLToPath } from 'url';
2
+ import { type ChildProcess, spawn } from 'node:child_process';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
5
 
6
6
  const __filename = fileURLToPath(import.meta.url);
7
7
  const __dirname = path.dirname(__filename);
@@ -31,12 +31,12 @@ const cleanup = async (): Promise<void> => {
31
31
  resolve();
32
32
  }, 5000);
33
33
 
34
- cli!.once('exit', () => {
34
+ cli?.once('exit', () => {
35
35
  clearTimeout(timeout);
36
36
  resolve();
37
37
  });
38
38
  });
39
- } catch (error) {
39
+ } catch (_error) {
40
40
  // ignore
41
41
  }
42
42
  }
@@ -101,7 +101,7 @@ process.on('exit', () => {
101
101
  if (cli && !cli.killed) {
102
102
  try {
103
103
  cli.kill('SIGKILL');
104
- } catch (error) {
104
+ } catch (_error) {
105
105
  // ignore
106
106
  }
107
107
  }
@@ -1,10 +1,9 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
1
3
  import { categorizeFilePaths, getPoetIgnore } from '@poetora/prebuild';
2
4
  import { coreRemark } from '@poetora/shared';
3
- import fs from 'fs';
4
- import type { Root, Text } from 'mdast';
5
- import type { Node } from 'mdast';
5
+ import type { Node, Root, Text } from 'mdast';
6
6
  import type { MdxJsxFlowElement } from 'mdast-util-mdx-jsx';
7
- import path from 'path';
8
7
  import { visit } from 'unist-util-visit';
9
8
 
10
9
  export interface AccessibilityFixAttribute {
@@ -0,0 +1,83 @@
1
+ import detect from 'detect-port';
2
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
3
+
4
+ import { NoAvailablePortError } from '../../errors/index.js';
5
+ import { CLI_CONSTANTS } from '../../types/index.js';
6
+ import { PortService } from '../port.service.js';
7
+
8
+ vi.mock('detect-port');
9
+
10
+ describe('PortService', () => {
11
+ let service: PortService;
12
+ const mockDetect = vi.mocked(detect);
13
+
14
+ beforeEach(() => {
15
+ service = new PortService();
16
+ vi.clearAllMocks();
17
+ });
18
+
19
+ describe('findAvailablePort', () => {
20
+ it('should return preferred port when available', async () => {
21
+ mockDetect.mockResolvedValue(3000);
22
+
23
+ const port = await service.findAvailablePort(3000);
24
+
25
+ expect(port).toBe(3000);
26
+ expect(mockDetect).toHaveBeenCalledWith(3000);
27
+ });
28
+
29
+ it('should return default port when no preferred port specified', async () => {
30
+ mockDetect.mockResolvedValue(CLI_CONSTANTS.PORT.DEFAULT);
31
+
32
+ const port = await service.findAvailablePort();
33
+
34
+ expect(port).toBe(CLI_CONSTANTS.PORT.DEFAULT);
35
+ });
36
+
37
+ it('should try next port when first is occupied', async () => {
38
+ mockDetect.mockResolvedValueOnce(3001); // 3000 occupied
39
+ mockDetect.mockResolvedValueOnce(3001); // 3001 available
40
+
41
+ const port = await service.findAvailablePort(3000);
42
+
43
+ expect(port).toBe(3001);
44
+ expect(mockDetect).toHaveBeenCalledTimes(2);
45
+ expect(mockDetect).toHaveBeenNthCalledWith(1, 3000);
46
+ expect(mockDetect).toHaveBeenNthCalledWith(2, 3001);
47
+ });
48
+
49
+ it('should try multiple ports until finding available one', async () => {
50
+ // Ports 3000, 3001, 3002 are occupied
51
+ mockDetect.mockResolvedValueOnce(3001);
52
+ mockDetect.mockResolvedValueOnce(3002);
53
+ mockDetect.mockResolvedValueOnce(3003);
54
+ // Port 3003 is available
55
+ mockDetect.mockResolvedValueOnce(3003);
56
+
57
+ const port = await service.findAvailablePort(3000);
58
+
59
+ expect(port).toBe(3003);
60
+ expect(mockDetect).toHaveBeenCalledTimes(4);
61
+ });
62
+
63
+ it('should throw NoAvailablePortError after MAX_ATTEMPTS', async () => {
64
+ // All ports are occupied
65
+ mockDetect.mockImplementation((port) => Promise.resolve((port as number) + 1));
66
+
67
+ await expect(service.findAvailablePort(3000)).rejects.toThrow(NoAvailablePortError);
68
+ expect(mockDetect).toHaveBeenCalledTimes(CLI_CONSTANTS.PORT.MAX_ATTEMPTS);
69
+ });
70
+
71
+ it('should include start port in error message', async () => {
72
+ mockDetect.mockImplementation((port) => Promise.resolve((port as number) + 1));
73
+
74
+ try {
75
+ await service.findAvailablePort(5000);
76
+ expect.fail('Should have thrown error');
77
+ } catch (error) {
78
+ expect(error).toBeInstanceOf(NoAvailablePortError);
79
+ expect((error as NoAvailablePortError).message).toContain('5000');
80
+ }
81
+ });
82
+ });
83
+ });
@@ -0,0 +1,234 @@
1
+ import * as fs from 'node:fs';
2
+ import AdmZip from 'adm-zip';
3
+ import * as fse from 'fs-extra';
4
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
5
+ import { ExternalServiceError, FileSystemError } from '../../errors/index.js';
6
+ import { TemplateService } from '../template.service.js';
7
+
8
+ vi.mock('node:fs', () => ({
9
+ default: {
10
+ promises: {
11
+ readdir: vi.fn(),
12
+ writeFile: vi.fn(),
13
+ readFile: vi.fn(),
14
+ },
15
+ },
16
+ promises: {
17
+ readdir: vi.fn(),
18
+ writeFile: vi.fn(),
19
+ readFile: vi.fn(),
20
+ },
21
+ }));
22
+
23
+ vi.mock('fs-extra');
24
+ vi.mock('adm-zip', () => ({
25
+ default: vi.fn(),
26
+ }));
27
+
28
+ describe('TemplateService', () => {
29
+ let service: TemplateService;
30
+
31
+ beforeEach(() => {
32
+ service = new TemplateService();
33
+ vi.clearAllMocks();
34
+ });
35
+
36
+ afterEach(() => {
37
+ vi.restoreAllMocks();
38
+ });
39
+
40
+ describe('checkDirectory', () => {
41
+ it('should return exists=true and hasContents=true for non-empty directory', async () => {
42
+ vi.mocked(fse.ensureDir).mockResolvedValue(undefined);
43
+ vi.mocked(fs.promises.readdir).mockResolvedValue([
44
+ 'file1.txt',
45
+ 'file2.txt',
46
+ ] as unknown as fs.Dirent[]);
47
+
48
+ const result = await service.checkDirectory('/test/dir');
49
+
50
+ expect(result).toEqual({
51
+ exists: true,
52
+ hasContents: true,
53
+ });
54
+ expect(fse.ensureDir).toHaveBeenCalledWith('/test/dir');
55
+ });
56
+
57
+ it('should return exists=true and hasContents=false for empty directory', async () => {
58
+ vi.mocked(fse.ensureDir).mockResolvedValue(undefined);
59
+ vi.mocked(fs.promises.readdir).mockResolvedValue([] as unknown as fs.Dirent[]);
60
+
61
+ const result = await service.checkDirectory('/test/empty');
62
+
63
+ expect(result).toEqual({
64
+ exists: true,
65
+ hasContents: false,
66
+ });
67
+ });
68
+
69
+ it('should return exists=false for non-existent directory on ENOENT', async () => {
70
+ const error = new Error('ENOENT') as NodeJS.ErrnoException;
71
+ error.code = 'ENOENT';
72
+ vi.mocked(fse.ensureDir).mockRejectedValue(error);
73
+
74
+ const result = await service.checkDirectory('/nonexistent');
75
+
76
+ expect(result).toEqual({
77
+ exists: false,
78
+ hasContents: false,
79
+ });
80
+ });
81
+
82
+ it('should throw FileSystemError for other errors', async () => {
83
+ vi.mocked(fse.ensureDir).mockRejectedValue(new Error('Permission denied'));
84
+
85
+ await expect(service.checkDirectory('/test/dir')).rejects.toThrow(FileSystemError);
86
+ });
87
+ });
88
+
89
+ describe('getAvailableThemes', () => {
90
+ it('should return available themes from docsConfigSchema', () => {
91
+ const themes = service.getAvailableThemes();
92
+
93
+ expect(themes).toBeInstanceOf(Array);
94
+ expect(themes.length).toBeGreaterThan(0);
95
+ expect(themes).toContain('ora');
96
+ });
97
+ });
98
+
99
+ describe('installTemplate', () => {
100
+ const mockFetch = vi.fn();
101
+ const originalFetch = global.fetch;
102
+
103
+ beforeEach(() => {
104
+ global.fetch = mockFetch;
105
+
106
+ // Mock successful download
107
+ mockFetch.mockResolvedValue({
108
+ ok: true,
109
+ arrayBuffer: vi.fn().mockResolvedValue(new ArrayBuffer(8)),
110
+ });
111
+
112
+ // Mock file operations
113
+ vi.mocked(fs.promises.writeFile).mockResolvedValue(undefined);
114
+ vi.mocked(fs.promises.readFile).mockResolvedValue('{}');
115
+ vi.mocked(fse.ensureDir).mockResolvedValue(undefined);
116
+ vi.mocked(fse.copy).mockResolvedValue(undefined);
117
+ vi.mocked(fse.remove).mockResolvedValue(undefined);
118
+
119
+ // Mock AdmZip constructor - must use function for constructor
120
+ vi.mocked(AdmZip).mockImplementation(function (this: AdmZip) {
121
+ return {
122
+ extractAllTo: vi.fn(),
123
+ } as unknown as AdmZip;
124
+ } as unknown as typeof AdmZip);
125
+ });
126
+
127
+ afterEach(() => {
128
+ global.fetch = originalFetch;
129
+ });
130
+
131
+ it('should successfully install template', async () => {
132
+ await service.installTemplate({
133
+ directory: '/test/project',
134
+ projectName: 'My Project',
135
+ theme: 'quartz',
136
+ });
137
+
138
+ expect(mockFetch).toHaveBeenCalledWith(
139
+ 'https://github.com/poetora/starter/archive/refs/heads/main.zip'
140
+ );
141
+ expect(fse.copy).toHaveBeenCalled();
142
+ expect(fs.promises.writeFile).toHaveBeenCalledWith(
143
+ '/test/project/docs.json',
144
+ expect.stringContaining('My Project'),
145
+ 'utf-8'
146
+ );
147
+ });
148
+
149
+ it('should throw ExternalServiceError on download failure', async () => {
150
+ mockFetch.mockResolvedValue({
151
+ ok: false,
152
+ status: 404,
153
+ statusText: 'Not Found',
154
+ });
155
+
156
+ await expect(
157
+ service.installTemplate({
158
+ directory: '/test/project',
159
+ projectName: 'My Project',
160
+ theme: 'quartz',
161
+ })
162
+ ).rejects.toThrow(ExternalServiceError);
163
+ });
164
+
165
+ it('should cleanup temporary files after successful installation', async () => {
166
+ await service.installTemplate({
167
+ directory: '/test/project',
168
+ projectName: 'My Project',
169
+ theme: 'quartz',
170
+ });
171
+
172
+ expect(fse.remove).toHaveBeenCalledWith('poetora-starter.zip');
173
+ expect(fse.remove).toHaveBeenCalledWith('poetora-starter-temp');
174
+ });
175
+
176
+ it('should cleanup temporary files even on error', async () => {
177
+ vi.mocked(fse.copy).mockRejectedValue(new Error('Copy failed'));
178
+
179
+ await expect(
180
+ service.installTemplate({
181
+ directory: '/test/project',
182
+ projectName: 'My Project',
183
+ theme: 'quartz',
184
+ })
185
+ ).rejects.toThrow();
186
+
187
+ expect(fse.remove).toHaveBeenCalled();
188
+ });
189
+
190
+ it('should handle missing docs.json by creating new config', async () => {
191
+ vi.mocked(fs.promises.readFile).mockRejectedValue(new Error('ENOENT'));
192
+
193
+ await service.installTemplate({
194
+ directory: '/test/project',
195
+ projectName: 'My Project',
196
+ theme: 'quartz',
197
+ });
198
+
199
+ expect(fs.promises.writeFile).toHaveBeenCalledWith(
200
+ '/test/project/docs.json',
201
+ expect.stringContaining('"name": "My Project"'),
202
+ 'utf-8'
203
+ );
204
+ });
205
+
206
+ it('should preserve existing config fields when updating', async () => {
207
+ vi.mocked(fs.promises.readFile).mockResolvedValue(
208
+ JSON.stringify({
209
+ name: 'Old Name',
210
+ theme: 'old-theme',
211
+ customField: 'preserved',
212
+ })
213
+ );
214
+
215
+ await service.installTemplate({
216
+ directory: '/test/project',
217
+ projectName: 'New Project',
218
+ theme: 'quartz',
219
+ });
220
+
221
+ const writeCall = vi
222
+ .mocked(fs.promises.writeFile)
223
+ .mock.calls.find((call) => call[0] === '/test/project/docs.json');
224
+
225
+ expect(writeCall).toBeDefined();
226
+ const writtenConfig = JSON.parse(writeCall?.[1] as string);
227
+ expect(writtenConfig).toMatchObject({
228
+ name: 'New Project',
229
+ theme: 'quartz',
230
+ customField: 'preserved',
231
+ });
232
+ });
233
+ });
234
+ });
@@ -0,0 +1,165 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { VersionService } from '../version.service.js';
3
+
4
+ describe('VersionService', () => {
5
+ let service: VersionService;
6
+ let _originalVersion: string;
7
+
8
+ beforeEach(() => {
9
+ service = new VersionService();
10
+ _originalVersion = process.version;
11
+ });
12
+
13
+ afterEach(() => {
14
+ vi.restoreAllMocks();
15
+ });
16
+
17
+ describe('parseNodeVersion', () => {
18
+ it('should parse version with v prefix', () => {
19
+ vi.spyOn(process, 'version', 'get').mockReturnValue('v20.17.0');
20
+
21
+ const version = service.parseNodeVersion();
22
+
23
+ expect(version.major).toBe(20);
24
+ expect(version.minor).toBe(17);
25
+ expect(version.patch).toBe(0);
26
+ expect(version.raw).toBe('20.17.0');
27
+ });
28
+
29
+ it('should parse version without v prefix', () => {
30
+ vi.spyOn(process, 'version', 'get').mockReturnValue('18.0.0');
31
+
32
+ const version = service.parseNodeVersion();
33
+
34
+ expect(version.major).toBe(18);
35
+ expect(version.minor).toBe(0);
36
+ expect(version.patch).toBe(0);
37
+ });
38
+
39
+ it('should parse version with only major.minor', () => {
40
+ vi.spyOn(process, 'version', 'get').mockReturnValue('v20.17');
41
+
42
+ const version = service.parseNodeVersion();
43
+
44
+ expect(version.major).toBe(20);
45
+ expect(version.minor).toBe(17);
46
+ expect(version.patch).toBe(0);
47
+ });
48
+ });
49
+
50
+ describe('checkNodeVersion', () => {
51
+ it('should pass for Node 20.17.0', () => {
52
+ vi.spyOn(process, 'version', 'get').mockReturnValue('v20.17.0');
53
+
54
+ const result = service.checkNodeVersion();
55
+
56
+ expect(result.isValid).toBe(true);
57
+ expect(result.hasWarning).toBe(false);
58
+ });
59
+
60
+ it('should pass for Node 18.0.0 (minimum)', () => {
61
+ vi.spyOn(process, 'version', 'get').mockReturnValue('v18.0.0');
62
+
63
+ const result = service.checkNodeVersion();
64
+
65
+ expect(result.isValid).toBe(true);
66
+ expect(result.hasWarning).toBe(true);
67
+ expect(result.message).toContain('20.17');
68
+ });
69
+
70
+ it('should pass for Node 24.x', () => {
71
+ vi.spyOn(process, 'version', 'get').mockReturnValue('v24.0.0');
72
+
73
+ const result = service.checkNodeVersion();
74
+
75
+ expect(result.isValid).toBe(true);
76
+ });
77
+
78
+ it('should fail for Node 17.x', () => {
79
+ vi.spyOn(process, 'version', 'get').mockReturnValue('v17.9.0');
80
+
81
+ const result = service.checkNodeVersion();
82
+
83
+ expect(result.isValid).toBe(false);
84
+ expect(result.message).toContain('18.0');
85
+ });
86
+
87
+ it('should fail for Node 25.x', () => {
88
+ vi.spyOn(process, 'version', 'get').mockReturnValue('v25.0.0');
89
+
90
+ const result = service.checkNodeVersion();
91
+
92
+ expect(result.isValid).toBe(false);
93
+ expect(result.message).toContain('not supported');
94
+ });
95
+
96
+ it('should warn for Node 19.x (below recommended)', () => {
97
+ vi.spyOn(process, 'version', 'get').mockReturnValue('v19.5.0');
98
+
99
+ const result = service.checkNodeVersion();
100
+
101
+ expect(result.isValid).toBe(true);
102
+ expect(result.hasWarning).toBe(true);
103
+ expect(result.message).toContain('recommended');
104
+ });
105
+
106
+ it('should warn for Node 20.16 (below recommended minor)', () => {
107
+ vi.spyOn(process, 'version', 'get').mockReturnValue('v20.16.0');
108
+
109
+ const result = service.checkNodeVersion();
110
+
111
+ expect(result.isValid).toBe(true);
112
+ expect(result.hasWarning).toBe(true);
113
+ });
114
+ });
115
+
116
+ describe('getCliVersion', () => {
117
+ it('should return test-cli in test mode', () => {
118
+ const originalEnv = process.env.CLI_TEST_MODE;
119
+ process.env.CLI_TEST_MODE = 'true';
120
+
121
+ const version = service.getCliVersion();
122
+
123
+ expect(version).toBe('test-cli');
124
+
125
+ process.env.CLI_TEST_MODE = originalEnv;
126
+ });
127
+
128
+ it('should return version from package.json', () => {
129
+ const version = service.getCliVersion();
130
+
131
+ expect(version).toBeDefined();
132
+ expect(typeof version).toBe('string');
133
+ });
134
+ });
135
+
136
+ describe('getLatestCliVersion', () => {
137
+ it('should return latest version from npm', () => {
138
+ const version = service.getLatestCliVersion('@poetora/cli');
139
+
140
+ expect(version).toBeDefined();
141
+ expect(typeof version).toBe('string');
142
+ expect(version).toMatch(/^\d+\.\d+\.\d+/);
143
+ });
144
+ });
145
+
146
+ describe('isVersionUpToDate', () => {
147
+ it('should return true when versions match', () => {
148
+ const result = service.isVersionUpToDate('1.0.0', '1.0.0');
149
+
150
+ expect(result).toBe(true);
151
+ });
152
+
153
+ it('should return false when versions differ', () => {
154
+ const result = service.isVersionUpToDate('1.0.0', '1.0.1');
155
+
156
+ expect(result).toBe(false);
157
+ });
158
+
159
+ it('should handle whitespace', () => {
160
+ const result = service.isVersionUpToDate('1.0.0 ', ' 1.0.0');
161
+
162
+ expect(result).toBe(true);
163
+ });
164
+ });
165
+ });