|
| 1 | +import express from 'express'; |
| 2 | +import request from 'supertest'; |
| 3 | + |
| 4 | +const repositoryModelMock = { |
| 5 | + findById: jest.fn(), |
| 6 | + update: jest.fn(), |
| 7 | +}; |
| 8 | + |
| 9 | +const oauthInstallationModelMock = { |
| 10 | + findById: jest.fn(), |
| 11 | +}; |
| 12 | + |
| 13 | +const oauthInstallationServiceMock = { |
| 14 | + ensureValidAccessToken: jest.fn(), |
| 15 | +}; |
| 16 | + |
| 17 | +const analysisModelMock = { |
| 18 | + findByRepository: jest.fn(), |
| 19 | +}; |
| 20 | + |
| 21 | +const analysisJobModelMock = { |
| 22 | + findByAnalysisId: jest.fn(), |
| 23 | +}; |
| 24 | + |
| 25 | +const jobModelMock = { |
| 26 | + findByType: jest.fn(), |
| 27 | +}; |
| 28 | + |
| 29 | +const queueServiceMock = { |
| 30 | + getJobModel: jest.fn(() => jobModelMock), |
| 31 | +}; |
| 32 | + |
| 33 | +const reviewTriggerServiceMock = { |
| 34 | + triggerByRepositoryId: jest.fn(), |
| 35 | +}; |
| 36 | + |
| 37 | +const platformClientMock = { |
| 38 | + listPullRequests: jest.fn(), |
| 39 | +}; |
| 40 | + |
| 41 | +const createPlatformClientMock = jest.fn(() => platformClientMock); |
| 42 | + |
| 43 | +jest.mock('../models/Repository', () => ({ |
| 44 | + getRepositoryModel: () => repositoryModelMock, |
| 45 | +})); |
| 46 | + |
| 47 | +jest.mock('../models/OAuthInstallation', () => ({ |
| 48 | + getOAuthInstallationModel: () => oauthInstallationModelMock, |
| 49 | +})); |
| 50 | + |
| 51 | +jest.mock('../models/Analysis', () => ({ |
| 52 | + getAnalysisModel: () => analysisModelMock, |
| 53 | +})); |
| 54 | + |
| 55 | +jest.mock('../models/AnalysisJob', () => ({ |
| 56 | + getAnalysisJobModel: () => analysisJobModelMock, |
| 57 | +})); |
| 58 | + |
| 59 | +jest.mock('../jobs/QueueService', () => ({ |
| 60 | + getQueueService: () => queueServiceMock, |
| 61 | +})); |
| 62 | + |
| 63 | +jest.mock('../platform/client', () => ({ |
| 64 | + createPlatformClient: createPlatformClientMock, |
| 65 | +})); |
| 66 | + |
| 67 | +jest.mock('../services/OAuthInstallationService', () => ({ |
| 68 | + getOAuthInstallationService: () => oauthInstallationServiceMock, |
| 69 | +})); |
| 70 | + |
| 71 | +jest.mock('../services/ReviewTriggerService', () => ({ |
| 72 | + getReviewTriggerService: () => reviewTriggerServiceMock, |
| 73 | +})); |
| 74 | + |
| 75 | +import repositoryRoutes from './repositoryRoutes'; |
| 76 | + |
| 77 | +describe('repositoryRoutes', () => { |
| 78 | + let app: express.Application; |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + jest.clearAllMocks(); |
| 82 | + app = express(); |
| 83 | + app.use(express.json()); |
| 84 | + app.use('/', repositoryRoutes); |
| 85 | + |
| 86 | + oauthInstallationModelMock.findById.mockReturnValue({ |
| 87 | + id: 3, |
| 88 | + platform: 'gitee', |
| 89 | + is_active: true, |
| 90 | + access_token: 'token', |
| 91 | + auth_type: 'oauth', |
| 92 | + }); |
| 93 | + oauthInstallationServiceMock.ensureValidAccessToken.mockResolvedValue({ |
| 94 | + id: 3, |
| 95 | + platform: 'gitee', |
| 96 | + is_active: true, |
| 97 | + access_token: 'token', |
| 98 | + auth_type: 'oauth', |
| 99 | + }); |
| 100 | + platformClientMock.listPullRequests.mockResolvedValue([]); |
| 101 | + analysisModelMock.findByRepository.mockReturnValue([]); |
| 102 | + analysisJobModelMock.findByAnalysisId.mockReturnValue([]); |
| 103 | + jobModelMock.findByType.mockReturnValue([]); |
| 104 | + }); |
| 105 | + |
| 106 | + it('uses canonical coordinates from full_name for pull request listing', async () => { |
| 107 | + repositoryModelMock.findById.mockReturnValue({ |
| 108 | + id: 9163, |
| 109 | + platform: 'gitee', |
| 110 | + owner: 'mars167', |
| 111 | + name: 'API REIVEW PRO1', |
| 112 | + full_name: 'api-review-test-group/api-reivew-pro1', |
| 113 | + installation_id: 3, |
| 114 | + is_active: true, |
| 115 | + }); |
| 116 | + repositoryModelMock.update.mockReturnValue({ |
| 117 | + id: 9163, |
| 118 | + platform: 'gitee', |
| 119 | + owner: 'api-review-test-group', |
| 120 | + name: 'api-reivew-pro1', |
| 121 | + full_name: 'api-review-test-group/api-reivew-pro1', |
| 122 | + installation_id: 3, |
| 123 | + is_active: true, |
| 124 | + }); |
| 125 | + |
| 126 | + const response = await request(app) |
| 127 | + .get('/9163/pull-requests') |
| 128 | + .query({ state: 'open', page: '1', limit: '20' }); |
| 129 | + |
| 130 | + expect(response.status).toBe(200); |
| 131 | + expect(repositoryModelMock.update).toHaveBeenCalledWith(9163, { |
| 132 | + owner: 'api-review-test-group', |
| 133 | + name: 'api-reivew-pro1', |
| 134 | + full_name: 'api-review-test-group/api-reivew-pro1', |
| 135 | + }); |
| 136 | + expect(platformClientMock.listPullRequests).toHaveBeenCalledWith( |
| 137 | + 'api-review-test-group', |
| 138 | + 'api-reivew-pro1', |
| 139 | + expect.objectContaining({ |
| 140 | + state: 'open', |
| 141 | + page: 1, |
| 142 | + per_page: 20, |
| 143 | + }) |
| 144 | + ); |
| 145 | + expect(response.body.repository).toEqual(expect.objectContaining({ |
| 146 | + owner: 'api-review-test-group', |
| 147 | + name: 'api-reivew-pro1', |
| 148 | + full_name: 'api-review-test-group/api-reivew-pro1', |
| 149 | + })); |
| 150 | + }); |
| 151 | +}); |
0 commit comments