-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathchange-utils.test.ts
More file actions
224 lines (187 loc) · 7.44 KB
/
change-utils.test.ts
File metadata and controls
224 lines (187 loc) · 7.44 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { promises as fs } from 'fs';
import path from 'path';
import os from 'os';
import { randomUUID } from 'crypto';
import { validateChangeName, createChange } from '../../src/utils/change-utils.js';
describe('validateChangeName', () => {
describe('valid names', () => {
it('should accept simple kebab-case name', () => {
const result = validateChangeName('add-auth');
expect(result).toEqual({ valid: true });
});
it('should accept name with multiple segments', () => {
const result = validateChangeName('add-user-auth');
expect(result).toEqual({ valid: true });
});
it('should accept name with numeric suffix', () => {
const result = validateChangeName('add-feature-2');
expect(result).toEqual({ valid: true });
});
it('should accept single word name', () => {
const result = validateChangeName('refactor');
expect(result).toEqual({ valid: true });
});
it('should accept name with numbers in segments', () => {
const result = validateChangeName('upgrade-to-v2');
expect(result).toEqual({ valid: true });
});
it('should accept name with dot separator', () => {
const result = validateChangeName('v1.2');
expect(result).toEqual({ valid: true });
});
it('should accept name with mixed dot and hyphen separators', () => {
const result = validateChangeName('add-auth.v2');
expect(result).toEqual({ valid: true });
});
it('should accept name with dot between words', () => {
const result = validateChangeName('feature.sub-task');
expect(result).toEqual({ valid: true });
});
});
describe('invalid names - uppercase rejected', () => {
it('should reject name with uppercase letters', () => {
const result = validateChangeName('Add-Auth');
expect(result.valid).toBe(false);
expect(result.error).toContain('lowercase');
});
it('should reject fully uppercase name', () => {
const result = validateChangeName('ADD-AUTH');
expect(result.valid).toBe(false);
expect(result.error).toContain('lowercase');
});
});
describe('invalid names - spaces rejected', () => {
it('should reject name with spaces', () => {
const result = validateChangeName('add auth');
expect(result.valid).toBe(false);
expect(result.error).toContain('spaces');
});
});
describe('invalid names - underscores rejected', () => {
it('should reject name with underscores', () => {
const result = validateChangeName('add_auth');
expect(result.valid).toBe(false);
expect(result.error).toContain('underscores');
});
});
describe('invalid names - special characters rejected', () => {
it('should reject name with exclamation mark', () => {
const result = validateChangeName('add-auth!');
expect(result.valid).toBe(false);
expect(result.error).toBeDefined();
});
it('should reject name with @ symbol', () => {
const result = validateChangeName('add@auth');
expect(result.valid).toBe(false);
expect(result.error).toBeDefined();
});
});
describe('invalid names - leading/trailing hyphens rejected', () => {
it('should reject name with leading hyphen', () => {
const result = validateChangeName('-add-auth');
expect(result.valid).toBe(false);
expect(result.error).toContain('start with a hyphen');
});
it('should reject name with trailing hyphen', () => {
const result = validateChangeName('add-auth-');
expect(result.valid).toBe(false);
expect(result.error).toContain('end with a hyphen');
});
});
describe('invalid names - consecutive hyphens rejected', () => {
it('should reject name with double hyphens', () => {
const result = validateChangeName('add--auth');
expect(result.valid).toBe(false);
expect(result.error).toContain('consecutive hyphens');
});
});
describe('invalid names - consecutive dots rejected', () => {
it('should reject name with double dots', () => {
const result = validateChangeName('add..auth');
expect(result.valid).toBe(false);
expect(result.error).toContain('consecutive dots');
});
});
describe('invalid names - empty name rejected', () => {
it('should reject empty string', () => {
const result = validateChangeName('');
expect(result.valid).toBe(false);
expect(result.error).toContain('empty');
});
});
});
describe('createChange', () => {
let testDir: string;
beforeEach(async () => {
testDir = path.join(os.tmpdir(), `openspec-test-${randomUUID()}`);
await fs.mkdir(testDir, { recursive: true });
});
afterEach(async () => {
await fs.rm(testDir, { recursive: true, force: true });
});
describe('creates directory', () => {
it('should create change directory', async () => {
await createChange(testDir, 'add-auth');
const changeDir = path.join(testDir, 'openspec', 'changes', 'add-auth');
const stats = await fs.stat(changeDir);
expect(stats.isDirectory()).toBe(true);
});
it('should create .openspec.yaml metadata file with default schema', async () => {
await createChange(testDir, 'add-auth');
const metaPath = path.join(testDir, 'openspec', 'changes', 'add-auth', '.openspec.yaml');
const content = await fs.readFile(metaPath, 'utf-8');
expect(content).toContain('schema: spec-driven');
expect(content).toMatch(/created: \d{4}-\d{2}-\d{2}/);
});
it('should create .openspec.yaml with custom schema', async () => {
await createChange(testDir, 'add-auth', { schema: 'spec-driven' });
const metaPath = path.join(testDir, 'openspec', 'changes', 'add-auth', '.openspec.yaml');
const content = await fs.readFile(metaPath, 'utf-8');
expect(content).toContain('schema: spec-driven');
});
});
describe('schema validation', () => {
it('should throw error for unknown schema', async () => {
await expect(createChange(testDir, 'add-auth', { schema: 'unknown-schema' })).rejects.toThrow(
/Unknown schema/
);
});
});
describe('duplicate change throws error', () => {
it('should throw error if change already exists', async () => {
await createChange(testDir, 'add-auth');
await expect(createChange(testDir, 'add-auth')).rejects.toThrow(
/already exists/
);
});
});
describe('invalid name throws validation error', () => {
it('should throw error for uppercase name', async () => {
await expect(createChange(testDir, 'Add-Auth')).rejects.toThrow(
/lowercase/
);
});
it('should throw error for name with spaces', async () => {
await expect(createChange(testDir, 'add auth')).rejects.toThrow(
/spaces/
);
});
it('should throw error for empty name', async () => {
await expect(createChange(testDir, '')).rejects.toThrow(
/empty/
);
});
});
describe('creates parent directories if needed', () => {
it('should create openspec/changes/ directories if they do not exist', async () => {
const newProjectDir = path.join(testDir, 'new-project');
await fs.mkdir(newProjectDir);
// openspec/changes/ does not exist yet
await createChange(newProjectDir, 'add-auth');
const changeDir = path.join(newProjectDir, 'openspec', 'changes', 'add-auth');
const stats = await fs.stat(changeDir);
expect(stats.isDirectory()).toBe(true);
});
});
});