Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions __tests__/bundle/commandCanonicalization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { FakeGithub } from './fakeGithub'
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest'

import { start } from './fakeGithub'
import { comment, helpersFor, repo, token } from './helpers'
import { runBundle } from './runBundle'

vi.setConfig({ testTimeout: 30_000 })

// the `prow-commands` input's canonicalization in handleIssueComment: a Prow-style alias
// (/remove-help) names its canonical command's module, duplicates collapse to one run, and
// a configured name no module serves fails the run
describe('dist/index.js prow-commands canonicalization', () => {
const labelsRead = `GET ${repo}/labels?per_page=100`
const labelPost = `POST ${repo}/issues/1/labels`
let gh: FakeGithub
const { calls, expectCommandThenConfig } = helpersFor(() => gh)

beforeAll(async () => {
gh = await start()
})
afterEach(() => gh.reset())
afterAll(() => gh.close())

function routeHelp() {
gh.route('GET', `${repo}/labels`, { status: 200, body: [{ name: 'help wanted' }] })
gh.route('POST', `${repo}/issues/1/labels`, { status: 200, body: [] })
}

async function run(body: string, commands: string) {
return runBundle({
eventName: 'issue_comment',
payload: comment(body),
inputs: { ...token, 'prow-commands': commands },
apiUrl: gh.url,
})
}

it('a configured /remove-help alias runs the /help module: /help adds help wanted', async () => {
routeHelp()

const result = await run('/help', '/remove-help')

expect(result.status, result.stdout).toBe(0)
expect(result.errors).toEqual([])
const posts = gh.requestsMatching('POST', /\/issues\/1\/labels$/)
expect(posts).toHaveLength(1)
expect(posts[0].body).toEqual({ labels: ['help wanted'] })
expectCommandThenConfig([labelsRead, labelPost])
})

it('/help, /remove-help and /HELP configured together collapse to a single /help run', async () => {
routeHelp()

const result = await run('/help', '/help /remove-help /HELP')

expect(result.status, result.stdout).toBe(0)
expect(result.errors).toEqual([])
expect(gh.requestsMatching('POST', /\/issues\/1\/labels$/)).toHaveLength(1)
expectCommandThenConfig([labelsRead, labelPost])
})

it('a configured command no module serves fails the run naming it and makes no api call', async () => {
const result = await run('/foo_bar', '/foo_bar')

expect(result.status, result.stdout).toBe(1)
expect(result.errors).toEqual([
'TypeError: error handling issue comment: Error: could not execute /foo_bar. May not be supported - please refer to docs',
])
expect(calls()).toEqual([])
})
})
Loading