|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import type { EventData, PostCreatedEvent } from '@/lib/server/events/types' |
| 3 | + |
| 4 | +vi.mock('../api', () => ({ |
| 5 | + createWorkItem: vi.fn(), |
| 6 | +})) |
| 7 | + |
| 8 | +import { azureDevOpsHook } from '../hook' |
| 9 | +import { createWorkItem } from '../api' |
| 10 | + |
| 11 | +function makePostCreatedEvent(): PostCreatedEvent { |
| 12 | + return { |
| 13 | + id: 'evt-1', |
| 14 | + type: 'post.created', |
| 15 | + timestamp: '2025-01-01T00:00:00Z', |
| 16 | + actor: { type: 'user', userId: 'user_1', email: 'test@test.com' }, |
| 17 | + data: { |
| 18 | + post: { |
| 19 | + id: 'post_1', |
| 20 | + title: 'Bug report', |
| 21 | + content: '<p>Something broke</p>', |
| 22 | + boardId: 'board_1', |
| 23 | + boardSlug: 'bugs', |
| 24 | + voteCount: 0, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + vi.clearAllMocks() |
| 32 | +}) |
| 33 | + |
| 34 | +describe('azureDevOpsHook', () => { |
| 35 | + it('does not treat a missing organization name as an auth failure', async () => { |
| 36 | + const result = await azureDevOpsHook.run( |
| 37 | + makePostCreatedEvent(), |
| 38 | + { channelId: 'Proj:Task' }, |
| 39 | + { accessToken: 'pat', rootUrl: 'https://app.example.com' } |
| 40 | + ) |
| 41 | + |
| 42 | + expect(result).toEqual({ |
| 43 | + success: false, |
| 44 | + error: 'Azure DevOps organization name is missing from integration config', |
| 45 | + shouldRetry: false, |
| 46 | + }) |
| 47 | + expect(createWorkItem).not.toHaveBeenCalled() |
| 48 | + }) |
| 49 | + |
| 50 | + it('creates a work item when organizationName is present', async () => { |
| 51 | + vi.mocked(createWorkItem).mockResolvedValue({ |
| 52 | + id: 42, |
| 53 | + url: 'https://dev.azure.com/acme/Proj/_workitems/edit/42', |
| 54 | + }) |
| 55 | + |
| 56 | + const result = await azureDevOpsHook.run( |
| 57 | + makePostCreatedEvent(), |
| 58 | + { channelId: 'Proj:Task' }, |
| 59 | + { |
| 60 | + accessToken: 'pat', |
| 61 | + organizationName: 'acme', |
| 62 | + rootUrl: 'https://app.example.com', |
| 63 | + } |
| 64 | + ) |
| 65 | + |
| 66 | + expect(result.success).toBe(true) |
| 67 | + expect(result.externalId).toBe('42') |
| 68 | + expect(createWorkItem).toHaveBeenCalledWith('pat', 'acme', 'Proj', 'Task', expect.any(Object)) |
| 69 | + }) |
| 70 | + |
| 71 | + it('maps HTTP 401 to reconnect only when organizationName is present', async () => { |
| 72 | + vi.mocked(createWorkItem).mockRejectedValue( |
| 73 | + Object.assign(new Error('Unauthorized'), { status: 401 }) |
| 74 | + ) |
| 75 | + |
| 76 | + const result = await azureDevOpsHook.run( |
| 77 | + makePostCreatedEvent(), |
| 78 | + { channelId: 'Proj:Task' }, |
| 79 | + { |
| 80 | + accessToken: 'pat', |
| 81 | + organizationName: 'acme', |
| 82 | + rootUrl: 'https://app.example.com', |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + expect(result).toEqual({ |
| 87 | + success: false, |
| 88 | + error: 'Authentication failed. Please reconnect Azure DevOps.', |
| 89 | + shouldRetry: false, |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + it('skips non post.created events', async () => { |
| 94 | + const event = { type: 'comment.created' } as unknown as EventData |
| 95 | + expect(await azureDevOpsHook.run(event, { channelId: 'Proj:Task' }, {})).toEqual({ |
| 96 | + success: true, |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
0 commit comments