test(comments review): 抽出嚴重等級解析斷言 #38
+15
-7
@@ -6,13 +6,6 @@ import path from 'node:path';
|
|||||||
import { saveFindings, parseLocation, postNewCriticalComments, postFindingsReview } from './comments.js';
|
import { saveFindings, parseLocation, postNewCriticalComments, postFindingsReview } from './comments.js';
|
||||||
|
|
|||||||
import { FINDINGS_PATH } from './config.js';
|
import { FINDINGS_PATH } from './config.js';
|
||||||
|
|
||||||
const REVIEW_SEVERITY_PATTERN = /\*\*嚴重等級\*\*:(🔴 嚴重|🟡 警告|🔵 建議)(?:\n|$)/;
|
|
||||||
const REVIEW_SEVERITY_LABELS = ['🔴 嚴重', '🟡 警告', '🔵 建議'];
|
|
||||||
|
|
||||||
function reviewSeverityLabel(comment) {
|
|
||||||
return comment.body.match(REVIEW_SEVERITY_PATTERN)?.[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('saveFindings', () => {
|
describe('saveFindings', () => {
|
||||||
const tempDirs = [];
|
const tempDirs = [];
|
||||||
const makeTempDir = prefix => {
|
const makeTempDir = prefix => {
|
||||||
@@ -194,6 +187,21 @@ describe('postNewCriticalComments', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('postFindingsReview', () => {
|
describe('postFindingsReview', () => {
|
||||||
|
const REVIEW_SEVERITY_LABELS = ['🔴 嚴重', '🟡 警告', '🔵 建議'];
|
||||||
|
const REVIEW_SEVERITY_PATTERN = new RegExp(`\\*\\*嚴重等級\\*\\*:(${REVIEW_SEVERITY_LABELS.join('|')})(?:\\n|$)`);
|
||||||
|
|
||||||
|
admin
commented
嚴重等級:🔵 建議 **嚴重等級**:🔵 建議
**審查員**:Bard
**問題**:此處新增的 `reviewSeverityLabel` 函式,雖其意圖在上下文脈絡中尚稱清晰,但若能為其添上一筆簡潔的 JSDoc 註解,闡明其參數與回傳值的語義,將使這段樂章更臻完善,即便在測試檔案中,亦能提升未來維護者的閱讀體驗,使程式碼的旋律更加和諧。
**建議**:建議為 `reviewSeverityLabel` 函式加上 JSDoc 註解,例如:
```javascript
/**
* 從評論物件中提取嚴重等級標籤。
* @param {object | null | undefined} comment - 評論物件,預期包含 `body` 屬性。
* @returns {string | undefined} 嚴重等級標籤字串(如 '🔴 嚴重'),若無匹配或輸入無效則回傳 undefined。
*/
function reviewSeverityLabel(comment) {
return comment?.body?.match(REVIEW_SEVERITY_PATTERN)?.[1];
}
```
|
|||||||
|
function reviewSeverityLabel(comment) {
|
||||||
|
return comment?.body?.match(REVIEW_SEVERITY_PATTERN)?.[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
it('handles missing review severity bodies gracefully', () => {
|
||||||
|
assert.equal(reviewSeverityLabel(null), undefined);
|
||||||
|
assert.equal(reviewSeverityLabel(undefined), undefined);
|
||||||
|
assert.equal(reviewSeverityLabel({}), undefined);
|
||||||
|
assert.equal(reviewSeverityLabel({ body: null }), undefined);
|
||||||
|
assert.equal(reviewSeverityLabel({ body: undefined }), undefined);
|
||||||
|
});
|
||||||
|
|
||||||
it('extracts review severity labels only when the format is valid', () => {
|
it('extracts review severity labels only when the format is valid', () => {
|
||||||
assert.equal(
|
assert.equal(
|
||||||
reviewSeverityLabel({ body: '**嚴重等級**:🔴 嚴重\n**審查員**:Rex' }),
|
reviewSeverityLabel({ body: '**嚴重等級**:🔴 嚴重\n**審查員**:Rex' }),
|
||||||
|
|||||||
Reference in New Issue
Block a user
嚴重等級:🔴 嚴重
審查員:Maya
問題:新增的
reviewSeverityLabel函式,在處理輸入的comment物件本身為null或undefined,或是comment.body屬性為null或undefined的邊界情況時,可能會拋出執行時期錯誤(TypeError),目前沒有對應的測試案例來驗證此失敗路徑。建議:建議為
reviewSeverityLabel函式新增測試案例,驗證當comment為null或undefined,以及comment.body為null或undefined時,函式能正確地回傳undefined而不拋出錯誤。同時,也請確保函式實作能妥善處理這些邊界輸入。例如,可以在
app/comments.test.js的describe('postFindingsReview', ...)區塊中,為reviewSeverityLabel函式新增以下測試案例:並調整
reviewSeverityLabel函式實作,例如:嚴重等級:🟡 警告
審查員:Leo
問題:在
REVIEW_SEVERITY_PATTERN和REVIEW_SEVERITY_LABELS這兩個常數中,嚴重等級的標籤(例如 '🔴 嚴重')被重複定義了。這會導致未來若要修改或新增標籤時,需要同時更新兩處,容易造成維護上的疏漏與不一致。建議:建議將嚴重等級標籤定義為單一來源,例如只維護
REVIEW_SEVERITY_LABELS陣列,然後動態地從這個陣列產生REVIEW_SEVERITY_PATTERN的正規表達式字串。這樣可以確保兩者永遠同步,降低未來的維護成本。例如: