识别审查评论标记和 PR 里 Closes #N 的规则被写坏(多了反斜杠、标记名少了个 s), 审查评论全被当成普通评论丢掉。现在这两条规则只在 gitea/markers.ts 里定义一份, webhook 和评论气泡都调用它,并补上单测,以后写坏会直接测出来。
66 lines
2.3 KiB
TypeScript
66 lines
2.3 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { hasReviewMarker, parseClosingIssueNumber, stripReviewMarker } from './markers'
|
|
|
|
const REAL_REVIEW = `<!-- spx:review=1 -->
|
|
## 审查意见
|
|
|
|
- **coordinator.ts:1452** 正则多写了反斜杠
|
|
- 建议补单测
|
|
|
|
\`\`\`ts
|
|
const x = 1
|
|
\`\`\``
|
|
|
|
describe('审查标记', () => {
|
|
it('真实审查评论:识别为审查,去标记后正文完整', () => {
|
|
expect(hasReviewMarker(REAL_REVIEW)).toBe(true)
|
|
expect(stripReviewMarker(REAL_REVIEW)).toBe(REAL_REVIEW.slice('<!-- spx:review=1 -->\n'.length))
|
|
})
|
|
|
|
it('普通评论不是审查', () => {
|
|
expect(hasReviewMarker('这个方案可以')).toBe(false)
|
|
expect(hasReviewMarker('提到了 spx:review=1 但不是注释')).toBe(false)
|
|
})
|
|
|
|
it('大小写和注释内空白都放宽', () => {
|
|
expect(hasReviewMarker('<!--SPX:Review=1-->\n正文')).toBe(true)
|
|
expect(hasReviewMarker('<!-- spx:review=1 -->')).toBe(true)
|
|
})
|
|
|
|
it('反复调用结果稳定', () => {
|
|
for (let i = 0; i < 3; i++) {
|
|
expect(hasReviewMarker(REAL_REVIEW)).toBe(true)
|
|
expect(stripReviewMarker(REAL_REVIEW)).toMatch(/^## 审查意见/)
|
|
}
|
|
})
|
|
|
|
it('多个标记全部去掉,只剩标记时为空串', () => {
|
|
expect(stripReviewMarker('<!-- spx:review=1 -->\n a <!-- SPX:review=1 --> b')).toBe('a b')
|
|
expect(stripReviewMarker('<!-- spx:review=1 -->\n')).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('parseClosingIssueNumber', () => {
|
|
it('多行 PR body 最后一行 Closes #462', () => {
|
|
const body = '## 改动\n\n- 修了正则\n- 加了测试\n\nCloses #462'
|
|
expect(parseClosingIssueNumber(body)).toBe(462)
|
|
})
|
|
|
|
it('其它关键字与大小写', () => {
|
|
expect(parseClosingIssueNumber('fixes #12')).toBe(12)
|
|
expect(parseClosingIssueNumber('FiXeS #7')).toBe(7)
|
|
expect(parseClosingIssueNumber('RESOLVE #3')).toBe(3)
|
|
expect(parseClosingIssueNumber('close\t#5')).toBe(5)
|
|
})
|
|
|
|
it('没有关键字返回 undefined', () => {
|
|
expect(parseClosingIssueNumber('关联 #462')).toBeUndefined()
|
|
expect(parseClosingIssueNumber('')).toBeUndefined()
|
|
})
|
|
|
|
it('不误匹配:# 前无空白、关键字粘在别的词后面', () => {
|
|
expect(parseClosingIssueNumber('closes#1')).toBeUndefined()
|
|
expect(parseClosingIssueNumber('prefixcloses #1')).toBeUndefined()
|
|
})
|
|
})
|