create-pull-request/__test__/create-pull-request.unit.test.ts

219 lines
6.1 KiB
TypeScript

import {Commit} from '../lib/git-command-manager'
describe('create-pull-request PR title and body defaulting', () => {
const defaultTitle = 'Changes by create-pull-request action'
const defaultBody =
'Automated changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action'
test('should use commit subject as title for single commit when title is default', () => {
const commit: Commit = {
sha: 'abc123',
tree: 'tree123',
parents: ['parent123'],
signed: false,
subject: 'Update AGP sources to the latest',
body: '_Auto-generated by `dump-sources` Github workflow._',
changes: [],
unparsedChanges: []
}
const branchCommits = [commit]
let title = defaultTitle
let body = defaultBody
// Simulate the logic from create-pull-request.ts
if (branchCommits.length === 1) {
const singleCommit = branchCommits[0]
if (title === defaultTitle) {
title = singleCommit.subject
}
if (body === defaultBody) {
if (singleCommit.body && singleCommit.body.trim().length > 0) {
body = singleCommit.body
}
}
}
expect(title).toEqual('Update AGP sources to the latest')
expect(body).toEqual('_Auto-generated by `dump-sources` Github workflow._')
})
test('should not override custom title and body', () => {
const commit: Commit = {
sha: 'abc123',
tree: 'tree123',
parents: ['parent123'],
signed: false,
subject: 'Update AGP sources to the latest',
body: '_Auto-generated by `dump-sources` Github workflow._',
changes: [],
unparsedChanges: []
}
const branchCommits = [commit]
let title = 'Custom Title'
let body = 'Custom Body'
// Simulate the logic from create-pull-request.ts
if (branchCommits.length === 1) {
const singleCommit = branchCommits[0]
if (title === defaultTitle) {
title = singleCommit.subject
}
if (body === defaultBody) {
if (singleCommit.body && singleCommit.body.trim().length > 0) {
body = singleCommit.body
}
}
}
expect(title).toEqual('Custom Title')
expect(body).toEqual('Custom Body')
})
test('should not default for multiple commits', () => {
const commit1: Commit = {
sha: 'abc123',
tree: 'tree123',
parents: ['parent123'],
signed: false,
subject: 'First commit',
body: 'First commit body',
changes: [],
unparsedChanges: []
}
const commit2: Commit = {
sha: 'def456',
tree: 'tree456',
parents: ['abc123'],
signed: false,
subject: 'Second commit',
body: 'Second commit body',
changes: [],
unparsedChanges: []
}
const branchCommits = [commit1, commit2]
let title = defaultTitle
let body = defaultBody
// Simulate the logic from create-pull-request.ts
if (branchCommits.length === 1) {
const singleCommit = branchCommits[0]
if (title === defaultTitle) {
title = singleCommit.subject
}
if (body === defaultBody) {
if (singleCommit.body && singleCommit.body.trim().length > 0) {
body = singleCommit.body
}
}
}
expect(title).toEqual(defaultTitle)
expect(body).toEqual(defaultBody)
})
test('should handle commit with empty body', () => {
const commit: Commit = {
sha: 'abc123',
tree: 'tree123',
parents: ['parent123'],
signed: false,
subject: 'Update files',
body: '',
changes: [],
unparsedChanges: []
}
const branchCommits = [commit]
let title = defaultTitle
let body = defaultBody
// Simulate the logic from create-pull-request.ts
if (branchCommits.length === 1) {
const singleCommit = branchCommits[0]
if (title === defaultTitle) {
title = singleCommit.subject
}
if (body === defaultBody) {
if (singleCommit.body && singleCommit.body.trim().length > 0) {
body = singleCommit.body
}
}
}
expect(title).toEqual('Update files')
expect(body).toEqual(defaultBody) // Should keep default when commit body is empty
})
test('should handle commit with multi-line body', () => {
const commit: Commit = {
sha: 'abc123',
tree: 'tree123',
parents: ['parent123'],
signed: false,
subject: 'Update documentation',
body: 'This is a detailed commit message.\n\nIt has multiple paragraphs.\n\n- Item 1\n- Item 2',
changes: [],
unparsedChanges: []
}
const branchCommits = [commit]
let title = defaultTitle
let body = defaultBody
// Simulate the logic from create-pull-request.ts
if (branchCommits.length === 1) {
const singleCommit = branchCommits[0]
if (title === defaultTitle) {
title = singleCommit.subject
}
if (body === defaultBody) {
if (singleCommit.body && singleCommit.body.trim().length > 0) {
body = singleCommit.body
}
}
}
expect(title).toEqual('Update documentation')
expect(body).toEqual(
'This is a detailed commit message.\n\nIt has multiple paragraphs.\n\n- Item 1\n- Item 2'
)
})
test('should handle commit with whitespace-only body', () => {
const commit: Commit = {
sha: 'abc123',
tree: 'tree123',
parents: ['parent123'],
signed: false,
subject: 'Update files',
body: ' \n\n ',
changes: [],
unparsedChanges: []
}
const branchCommits = [commit]
let title = defaultTitle
let body = defaultBody
// Simulate the logic from create-pull-request.ts
if (branchCommits.length === 1) {
const singleCommit = branchCommits[0]
if (title === defaultTitle) {
title = singleCommit.subject
}
if (body === defaultBody) {
if (singleCommit.body && singleCommit.body.trim().length > 0) {
body = singleCommit.body
}
}
}
expect(title).toEqual('Update files')
expect(body).toEqual(defaultBody) // Should keep default when commit body is only whitespace
})
})