Skip to content
← Public packages

@kentcdodds/friction-log

Qualify and file friction papercuts to GitHub (any owner/repo) or wake Patch for Kody packages; daily sweep of kody-hq/kody friction issues.

FollowFork

src/mention-safe.test.ts

61 lines · 2.5 KB · TypeScript
Raw
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'

import {
	backtickHandles,
	backtickPackageNames,
	backtickPhrases,
	dropMentionCredits,
	inlineCode,
	makeMentionSafe,
} from './mention-safe.ts'

describe('mention-safe', () => {
	it('wraps scoped package names and kody specifiers in inline code', () => {
		assert.equal(backtickPackageNames('Uses @scope/pkg.'), 'Uses `@scope/pkg`.')
		assert.equal(backtickPackageNames('import kody:@scope/pkg/sub here'), 'import `kody:@scope/pkg/sub` here')
		assert.equal(backtickPackageNames('(@a-b/c_d)'), '(`@a-b/c_d`)')
	})

	it('leaves code spans, fenced blocks, emails, and bare handles alone', () => {
		assert.equal(backtickPackageNames('already `@scope/pkg`'), 'already `@scope/pkg`')
		const fenced = '```\nnpm i @scope/pkg\n```'
		assert.equal(backtickPackageNames(fenced), fenced)
		assert.equal(backtickPackageNames('mail me@example.com/x'), 'mail me@example.com/x')
		assert.equal(backtickPackageNames('hi @someone'), 'hi @someone')
	})

	it('backticks phrases such as test names, including ones with @ tags', () => {
		assert.equal(
			backtickPhrases('The test checkout @smoke flakes. checkout @smoke again.', ['checkout @smoke']),
			'The test `checkout @smoke` flakes. `checkout @smoke` again.',
		)
		assert.equal(backtickPhrases('see `checkout @smoke`', ['checkout @smoke']), 'see `checkout @smoke`')
	})

	it('drops credit phrases', () => {
		assert.equal(dropMentionCredits('- Faster builds by @someone ([#1](u))'), '- Faster builds ([#1](u))')
		assert.equal(dropMentionCredits('- Fix (thanks @a and @b)'), '- Fix')
		assert.equal(dropMentionCredits('Ship it, cc @a, @b'), 'Ship it')
		assert.equal(dropMentionCredits('built by hand'), 'built by hand')
		assert.equal(dropMentionCredits('- Thanks @a for the report'), '- Thanks @a for the report')
		assert.equal(dropMentionCredits('1. Fixed - by @a'), '1. Fixed')
	})

	it('inlineCode survives backticks', () => {
		assert.equal(inlineCode('a`b'), '``a`b``')
		assert.equal(inlineCode('`x'), '`` `x ``')
	})

	it('makeMentionSafe runs every step', () => {
		assert.equal(
			makeMentionSafe('Flake in t @tag, by @someone, see @scope/pkg', { phrases: ['t @tag'] }),
			'Flake in `t @tag`, see `@scope/pkg`',
		)
	})

	it('backtickHandles wraps remaining handles and teams but not emails or code', () => {
		assert.equal(backtickHandles('hi @someone and @org/team, me@example.com, `@x`'), 'hi `@someone` and `@org/team`, me@example.com, `@x`')
		assert.equal(makeMentionSafe('Fix by @a. Ping @b', { backtickHandles: true }), 'Fix. Ping `@b`')
	})
})