|
| 1 | +import { expectSuccessfulTypedBackground, saveFixtures } from '../../helpers'; |
| 2 | +import { parseOpportunityFeedbackWorker as worker } from '../../../src/workers/opportunity/parseOpportunityFeedback'; |
| 3 | +import { DataSource } from 'typeorm'; |
| 4 | +import createOrGetConnection from '../../../src/db'; |
| 5 | +import { OpportunityMatch } from '../../../src/entity/OpportunityMatch'; |
| 6 | +import { User } from '../../../src/entity'; |
| 7 | +import { usersFixture } from '../../fixture'; |
| 8 | +import { OpportunityMatchStatus } from '../../../src/entity/opportunities/types'; |
| 9 | +import { |
| 10 | + FeedbackCategory, |
| 11 | + FeedbackPlatform, |
| 12 | + FeedbackSentiment, |
| 13 | + FeedbackUrgency, |
| 14 | + OpportunityState, |
| 15 | + OpportunityType, |
| 16 | +} from '@dailydotdev/schema'; |
| 17 | + |
| 18 | +const mockParseFeedback = jest.fn(); |
| 19 | + |
| 20 | +jest.mock('../../../src/integrations/bragi', () => ({ |
| 21 | + getBragiClient: () => ({ |
| 22 | + garmr: { |
| 23 | + execute: (fn: () => Promise<unknown>) => fn(), |
| 24 | + }, |
| 25 | + instance: { |
| 26 | + parseFeedback: (...args: unknown[]) => mockParseFeedback(...args), |
| 27 | + }, |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +let con: DataSource; |
| 32 | +const testOpportunityId = '550e8400-e29b-41d4-a716-446655440099'; |
| 33 | + |
| 34 | +beforeAll(async () => { |
| 35 | + con = await createOrGetConnection(); |
| 36 | +}); |
| 37 | + |
| 38 | +beforeEach(async () => { |
| 39 | + jest.resetAllMocks(); |
| 40 | + await saveFixtures(con, User, usersFixture); |
| 41 | + |
| 42 | + // Create a minimal opportunity record |
| 43 | + await con.query( |
| 44 | + `INSERT INTO opportunity (id, type, state, title, tldr, content, meta) |
| 45 | + VALUES ($1, $2, $3, $4, $5, $6, $7) |
| 46 | + ON CONFLICT (id) DO NOTHING`, |
| 47 | + [ |
| 48 | + testOpportunityId, |
| 49 | + OpportunityType.JOB, |
| 50 | + OpportunityState.LIVE, |
| 51 | + 'Test Opportunity', |
| 52 | + 'Test TLDR', |
| 53 | + JSON.stringify({}), |
| 54 | + JSON.stringify({}), |
| 55 | + ], |
| 56 | + ); |
| 57 | +}); |
| 58 | + |
| 59 | +afterEach(async () => { |
| 60 | + await con |
| 61 | + .getRepository(OpportunityMatch) |
| 62 | + .delete({ opportunityId: testOpportunityId }); |
| 63 | +}); |
| 64 | + |
| 65 | +afterAll(async () => { |
| 66 | + await con.query('DELETE FROM opportunity WHERE id = $1', [testOpportunityId]); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('parseOpportunityFeedback worker', () => { |
| 70 | + it('should skip when no match is found', async () => { |
| 71 | + await expectSuccessfulTypedBackground<'api.v1.opportunity-feedback-submitted'>( |
| 72 | + worker, |
| 73 | + { |
| 74 | + opportunityId: testOpportunityId, |
| 75 | + userId: 'nonexistent-user', |
| 76 | + }, |
| 77 | + ); |
| 78 | + |
| 79 | + expect(mockParseFeedback).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should skip when match has no feedback', async () => { |
| 83 | + await con.getRepository(OpportunityMatch).save({ |
| 84 | + opportunityId: testOpportunityId, |
| 85 | + userId: '1', |
| 86 | + status: OpportunityMatchStatus.Pending, |
| 87 | + description: { reasoning: 'Test match' }, |
| 88 | + feedback: [], |
| 89 | + }); |
| 90 | + |
| 91 | + await expectSuccessfulTypedBackground<'api.v1.opportunity-feedback-submitted'>( |
| 92 | + worker, |
| 93 | + { |
| 94 | + opportunityId: testOpportunityId, |
| 95 | + userId: '1', |
| 96 | + }, |
| 97 | + ); |
| 98 | + |
| 99 | + expect(mockParseFeedback).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should parse feedback and store classification in database', async () => { |
| 103 | + await con.getRepository(OpportunityMatch).save({ |
| 104 | + opportunityId: testOpportunityId, |
| 105 | + userId: '2', |
| 106 | + status: OpportunityMatchStatus.Pending, |
| 107 | + description: { reasoning: 'Test match' }, |
| 108 | + feedback: [{ question: 'How was it?', answer: 'Great experience!' }], |
| 109 | + }); |
| 110 | + |
| 111 | + mockParseFeedback.mockResolvedValue({ |
| 112 | + classification: { |
| 113 | + platform: FeedbackPlatform.RECRUITER, |
| 114 | + category: FeedbackCategory.FEATURE_REQUEST, |
| 115 | + sentiment: FeedbackSentiment.POSITIVE, |
| 116 | + urgency: FeedbackUrgency.LOW, |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + await expectSuccessfulTypedBackground<'api.v1.opportunity-feedback-submitted'>( |
| 121 | + worker, |
| 122 | + { |
| 123 | + opportunityId: testOpportunityId, |
| 124 | + userId: '2', |
| 125 | + }, |
| 126 | + ); |
| 127 | + |
| 128 | + expect(mockParseFeedback).toHaveBeenCalledWith({ |
| 129 | + feedback: 'Great experience!', |
| 130 | + }); |
| 131 | + |
| 132 | + // Verify the classification was stored correctly in the database |
| 133 | + const updatedMatch = await con.getRepository(OpportunityMatch).findOne({ |
| 134 | + where: { |
| 135 | + opportunityId: testOpportunityId, |
| 136 | + userId: '2', |
| 137 | + }, |
| 138 | + }); |
| 139 | + |
| 140 | + expect(updatedMatch?.feedback?.[0]?.classification).toEqual({ |
| 141 | + platform: FeedbackPlatform.RECRUITER, |
| 142 | + category: FeedbackCategory.FEATURE_REQUEST, |
| 143 | + sentiment: FeedbackSentiment.POSITIVE, |
| 144 | + urgency: FeedbackUrgency.LOW, |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments