|
| 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 | +}); |
0 commit comments