80 lines
2.5 KiB
JavaScript
80 lines
2.5 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
truncateDiff,
|
|
fallbackSummary,
|
|
buildResolveBranchName,
|
|
buildResolveBody,
|
|
} from './index.js';
|
|
|
|
test('truncateDiff: 內容在上限內不截斷', () => {
|
|
const { diff, truncated } = truncateDiff('abc', 100);
|
|
assert.equal(diff, 'abc');
|
|
assert.equal(truncated, false);
|
|
});
|
|
|
|
test('truncateDiff: null/空字串回傳空字串且不截斷', () => {
|
|
assert.deepEqual(truncateDiff(null, 100), { diff: '', truncated: false });
|
|
assert.deepEqual(truncateDiff('', 100), { diff: '', truncated: false });
|
|
});
|
|
|
|
test('truncateDiff: 超過上限時截斷並標示', () => {
|
|
const big = 'x'.repeat(50);
|
|
const { diff, truncated } = truncateDiff(big, 10);
|
|
assert.equal(truncated, true);
|
|
assert.ok(diff.startsWith('xxxxxxxxxx'));
|
|
assert.ok(diff.includes('已截斷'));
|
|
});
|
|
|
|
test('fallbackSummary: 取首個 commit 當標題', () => {
|
|
const s = fallbackSummary({
|
|
source: 'feature',
|
|
target: 'develop',
|
|
commitMessages: '- feat: 新增功能\n- fix: 修正',
|
|
diffStat: ' a.js | 2 +-',
|
|
});
|
|
assert.equal(s.title, 'feat: 新增功能');
|
|
assert.ok(s.description.includes('## 變更摘要'));
|
|
assert.ok(s.description.includes('a.js'));
|
|
});
|
|
|
|
test('fallbackSummary: 無 commit 時退回 Merge 標題且描述非空', () => {
|
|
const s = fallbackSummary({
|
|
source: 'feature',
|
|
target: 'develop',
|
|
commitMessages: '',
|
|
diffStat: '',
|
|
});
|
|
assert.equal(s.title, 'Merge feature into develop');
|
|
assert.ok(s.description.includes('(無)'));
|
|
assert.ok(s.description.length > 0);
|
|
});
|
|
|
|
test('buildResolveBranchName: 含前綴並淨化非法字元', () => {
|
|
const name = buildResolveBranchName('develop', 'feature/x y');
|
|
assert.ok(name.startsWith('resolve-conflict/'));
|
|
assert.ok(name.includes('-into-'));
|
|
// 空白等非法字元被替換為 -
|
|
assert.ok(!/\s/.test(name));
|
|
});
|
|
|
|
test('buildResolveBranchName: 過長 target/source 仍遠低於 255', () => {
|
|
const long = 'a'.repeat(500);
|
|
const name = buildResolveBranchName(long, long);
|
|
assert.ok(name.length < 255);
|
|
});
|
|
|
|
test('buildResolveBody: 含衝突檔案清單與人工檢查清單', () => {
|
|
const body = buildResolveBody({
|
|
source: 'feature',
|
|
target: 'develop',
|
|
resolveBranch: 'resolve-conflict/develop-into-feature',
|
|
files: ['a.js', 'b.js'],
|
|
summary: { title: 't', description: '摘要內容' },
|
|
});
|
|
assert.ok(body.includes('- `a.js`'));
|
|
assert.ok(body.includes('- `b.js`'));
|
|
assert.ok(body.includes('合併前人工檢查清單'));
|
|
assert.ok(body.includes('摘要內容'));
|
|
});
|