Files
tilbudgivern/backend/__tests__/repositoryStatusDiscordService.test.js
T

72 lines
2.6 KiB
JavaScript

const {
buildDiscordPayload,
normalizeGitHubUrl,
parsePorcelain,
postDiscordStatus,
summarizeChecks
} = require('../src/services/repositoryStatusDiscordService');
describe('Repository Discord status', () => {
test('normalizes GitHub remotes without leaking transport details', () => {
expect(normalizeGitHubUrl('[email protected]:alexpolo1/tilbudgivern.git'))
.toBe('https://github.com/alexpolo1/tilbudgivern');
expect(normalizeGitHubUrl('https://github.com/alexpolo1/tilbudgivern.git'))
.toBe('https://github.com/alexpolo1/tilbudgivern');
});
test('counts staged, modified and untracked files', () => {
expect(parsePorcelain('M staged.js\n M modified.js\n?? new.js\nMM both.js\n')).toEqual({
changed: 4,
staged: 2,
modified: 2,
untracked: 1
});
});
test('summarizes pull request checks', () => {
expect(summarizeChecks([{ statusCheckRollup: [
{ status: 'COMPLETED', conclusion: 'SUCCESS' },
{ status: 'IN_PROGRESS', conclusion: '' },
{ status: 'COMPLETED', conclusion: 'FAILURE' }
] }])).toEqual({ passed: 1, pending: 1, failed: 1 });
});
test('builds a red, mention-safe Discord card when drift needs action', () => {
const payload = buildDiscordPayload({
repositoryUrl: 'https://github.com/alexpolo1/tilbudgivern',
branch: 'main',
upstream: 'origin/main',
ahead: 0,
behind: 1,
worktree: { changed: 0, staged: 0, modified: 0, untracked: 0 },
latestCommit: { hash: 'abc1234', subject: 'fix: test' },
pullRequests: [],
issues: [],
checks: { passed: 1, pending: 0, failed: 1 },
health: { ok: false, label: 'Utilgængelig' }
}, new Date('2026-08-18T06:30:00.000Z'));
expect(payload.allowed_mentions).toEqual({ parse: [] });
expect(payload.embeds[0]).toMatchObject({ color: 15548997 });
expect(payload.embeds[0].title).toContain('Kræver handling');
expect(JSON.stringify(payload)).not.toContain('webhook');
});
test('posts JSON with wait enabled without embedding the webhook in payload', async () => {
const fetchImpl = jest.fn().mockResolvedValue({
ok: true,
json: async () => ({ id: 'message-1' })
});
const payload = { allowed_mentions: { parse: [] }, embeds: [] };
await expect(postDiscordStatus({
webhookUrl: 'https://discord.example/webhook-id/token',
payload,
fetchImpl
})).resolves.toEqual({ id: 'message-1' });
expect(fetchImpl.mock.calls[0][0].searchParams.get('wait')).toBe('true');
expect(JSON.parse(fetchImpl.mock.calls[0][1].body)).toEqual(payload);
});
});