Skip to content

Commit 2d6c396

Browse files
committed
web 에서 seg 버그 수정 및 테스트 코드 추가
1 parent 9bef6ef commit 2d6c396

File tree

4 files changed

+150
-5
lines changed

4 files changed

+150
-5
lines changed

test/web-preview.test.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import { test, describe } from 'node:test';
2+
import assert from 'node:assert';
3+
4+
// Import web-preview core functions
5+
// These are browser-compatible versions that should work the same as the main package
6+
import { renderTemplate } from '../web-preview/src/core.ts';
7+
8+
describe('Web Preview - renderTemplate', () => {
9+
const context = {
10+
branch: 'legacy-contentsvod/CONP-1518',
11+
segs: ['legacy-contentsvod', 'CONP-1518'],
12+
ticket: 'CONP-1518',
13+
msg: 'add new feature',
14+
body: 'add new feature\n\nDetailed description'
15+
};
16+
17+
test('should replace ticket token', () => {
18+
const result = renderTemplate('[${ticket}] ${msg}', context);
19+
assert.strictEqual(result, '[CONP-1518] add new feature');
20+
});
21+
22+
test('should replace branch token', () => {
23+
const result = renderTemplate('${branch}: ${msg}', context);
24+
assert.strictEqual(result, 'legacy-contentsvod/CONP-1518: add new feature');
25+
});
26+
27+
test('should replace seg0 token (NOT segs[0])', () => {
28+
const result = renderTemplate('[${seg0}] ${msg}', context);
29+
assert.strictEqual(result, '[legacy-contentsvod] add new feature');
30+
});
31+
32+
test('should replace seg1 token (NOT segs[1])', () => {
33+
const result = renderTemplate('[${seg1}] ${msg}', context);
34+
assert.strictEqual(result, '[CONP-1518] add new feature');
35+
});
36+
37+
test('should NOT replace segs[0] - incorrect syntax', () => {
38+
const result = renderTemplate('[${segs[0]}] ${msg}', context);
39+
// segs[0] is not a valid token, so it should remain unchanged
40+
assert.strictEqual(result, '[${segs[0]}] add new feature');
41+
});
42+
43+
test('should replace segments token', () => {
44+
const result = renderTemplate('${segments}: ${msg}', context);
45+
assert.strictEqual(result, 'legacy-contentsvod/CONP-1518: add new feature');
46+
});
47+
48+
test('should replace prefix:1 token', () => {
49+
const result = renderTemplate('[${prefix:1}] ${msg}', context);
50+
assert.strictEqual(result, '[legacy-contentsvod] add new feature');
51+
});
52+
53+
test('should replace prefix:2 token', () => {
54+
const result = renderTemplate('[${prefix:2}] ${msg}', context);
55+
assert.strictEqual(result, '[legacy-contentsvod/CONP-1518] add new feature');
56+
});
57+
58+
test('should handle missing segment indices', () => {
59+
const result = renderTemplate('${seg5}', context);
60+
assert.strictEqual(result, '');
61+
});
62+
63+
test('should replace body token', () => {
64+
const result = renderTemplate('${body}', context);
65+
assert.strictEqual(result, 'add new feature\n\nDetailed description');
66+
});
67+
68+
test('should handle complex template with multiple tokens', () => {
69+
const template = '[${ticket}] ${seg0}: ${msg}\n\nBranch: ${branch}\nSegments: ${segments}';
70+
const result = renderTemplate(template, context);
71+
const expected = '[CONP-1518] legacy-contentsvod: add new feature\n\nBranch: legacy-contentsvod/CONP-1518\nSegments: legacy-contentsvod/CONP-1518';
72+
assert.strictEqual(result, expected);
73+
});
74+
75+
test('should handle three-segment branch', () => {
76+
const threeSegContext = {
77+
branch: 'feature/ABC-123/user-auth',
78+
segs: ['feature', 'ABC-123', 'user-auth'],
79+
ticket: 'ABC-123',
80+
msg: 'implement login',
81+
body: 'implement login'
82+
};
83+
84+
assert.strictEqual(renderTemplate('${seg0}', threeSegContext), 'feature');
85+
assert.strictEqual(renderTemplate('${seg1}', threeSegContext), 'ABC-123');
86+
assert.strictEqual(renderTemplate('${seg2}', threeSegContext), 'user-auth');
87+
assert.strictEqual(renderTemplate('${prefix:1}', threeSegContext), 'feature');
88+
assert.strictEqual(renderTemplate('${prefix:2}', threeSegContext), 'feature/ABC-123');
89+
assert.strictEqual(renderTemplate('${prefix:3}', threeSegContext), 'feature/ABC-123/user-auth');
90+
assert.strictEqual(renderTemplate('${segments}', threeSegContext), 'feature/ABC-123/user-auth');
91+
});
92+
93+
test('should handle empty tokens gracefully', () => {
94+
const emptyContext = {
95+
branch: '',
96+
segs: [],
97+
ticket: '',
98+
msg: '',
99+
body: ''
100+
};
101+
const result = renderTemplate('[${ticket}] ${msg}', emptyContext);
102+
assert.strictEqual(result, '[] ');
103+
});
104+
});
105+
106+
describe('Web Preview - Token Compatibility', () => {
107+
test('web-preview should use same token syntax as main package', () => {
108+
const context = {
109+
branch: 'bugfix/PROJ-456-fix-bug',
110+
segs: ['bugfix', 'PROJ-456-fix-bug'],
111+
ticket: 'PROJ-456',
112+
msg: 'fix critical bug',
113+
body: 'fix critical bug'
114+
};
115+
116+
// These should work
117+
const validTokens = [
118+
{ template: '${ticket}', expected: 'PROJ-456' },
119+
{ template: '${seg0}', expected: 'bugfix' },
120+
{ template: '${seg1}', expected: 'PROJ-456-fix-bug' },
121+
{ template: '${segments}', expected: 'bugfix/PROJ-456-fix-bug' },
122+
{ template: '${prefix:1}', expected: 'bugfix' },
123+
{ template: '${branch}', expected: 'bugfix/PROJ-456-fix-bug' },
124+
{ template: '${msg}', expected: 'fix critical bug' }
125+
];
126+
127+
validTokens.forEach(({ template, expected }) => {
128+
const result = renderTemplate(template, context);
129+
assert.strictEqual(result, expected, `Token ${template} should work`);
130+
});
131+
132+
// These should NOT work (invalid syntax)
133+
const invalidTokens = [
134+
'${segs[0]}',
135+
'${segs[1]}',
136+
'${segs}'
137+
];
138+
139+
invalidTokens.forEach((template) => {
140+
const result = renderTemplate(template, context);
141+
// Invalid tokens should remain unchanged in the output
142+
assert.strictEqual(result, template, `Token ${template} should not be replaced`);
143+
});
144+
});
145+
});

web-preview/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ npm run build
3333

3434
- **includePattern**: Glob patterns for branches to include (e.g., `feature/*`, `bugfix/*`)
3535
- **format**: Template for commit messages when ticket is found (e.g., `${ticket}: ${msg}`)
36-
- **fallbackFormat**: Template when no ticket is found (e.g., `${segs[0]}: ${msg}`)
36+
- **fallbackFormat**: Template when no ticket is found (e.g., `${seg0}: ${msg}`)
3737
- **exclude**: Patterns to exclude certain sources
3838

3939
## Template Tokens
4040

4141
- `${ticket}`: Extracted ticket number (e.g., ABC-123)
4242
- `${msg}`: Original commit message
4343
- `${branch}`: Full branch name
44-
- `${segs[0]}`, `${segs[1]}`: Branch segments split by `/`
44+
- `${seg0}`, `${seg1}`, etc.: Individual branch segments split by `/`
4545
- `${segments}`: All segments joined with `/`
4646
- `${prefix:n}`: First n segments joined with `/`
4747

web-preview/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import './App.css';
88
const DEFAULT_CONFIG: CommitFromBranchConfig = {
99
includePattern: ['*'],
1010
format: '${ticket}: ${msg}',
11-
fallbackFormat: '${segs[0]}: ${msg}',
11+
fallbackFormat: '${seg0}: ${msg}',
1212
exclude: []
1313
};
1414

web-preview/src/components/ConfigForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const ConfigForm: React.FC<ConfigFormProps> = ({
8282
className="form-input"
8383
/>
8484
<small className="form-help">
85-
Available tokens: {'${ticket}'}, {'${msg}'}, {'${branch}'}, {'${segs[0]}'}, {'${segs[1]}'}, etc.
85+
Available tokens: {'${ticket}'}, {'${msg}'}, {'${branch}'}, {'${seg0}'}, {'${seg1}'}, {'${segments}'}, {'${prefix:n}'}, etc.
8686
</small>
8787
</div>
8888

@@ -93,7 +93,7 @@ const ConfigForm: React.FC<ConfigFormProps> = ({
9393
type="text"
9494
value={config.fallbackFormat || ''}
9595
onChange={(e) => updateConfig('fallbackFormat', e.target.value)}
96-
placeholder="e.g., ${segs[0]}: ${msg}"
96+
placeholder="e.g., ${seg0}: ${msg}"
9797
className="form-input"
9898
/>
9999
</div>

0 commit comments

Comments
 (0)