|
| 1 | +import {OrganizationFixture} from 'sentry-fixture/organization'; |
| 2 | + |
| 3 | +import {renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary'; |
| 4 | + |
| 5 | +import {PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT} from 'sentry/types/prevent'; |
| 6 | + |
| 7 | +import {usePreventAIGitHubConfig} from './usePreventAIConfig'; |
| 8 | + |
| 9 | +describe('usePreventAIGitHubConfig', () => { |
| 10 | + const org = OrganizationFixture({slug: 'test-org'}); |
| 11 | + const gitOrgName = 'octo-corp'; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + MockApiClient.clearMockResponses(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('fetches config from API correctly', async () => { |
| 18 | + const configResponse = { |
| 19 | + default_org_config: { |
| 20 | + org_defaults: { |
| 21 | + vanilla: {enabled: true, sensitivity: 'medium'}, |
| 22 | + test_generation: {enabled: false, sensitivity: 'low'}, |
| 23 | + bug_prediction: {enabled: false, sensitivity: 'high'}, |
| 24 | + }, |
| 25 | + repo_overrides: { |
| 26 | + 'repo-123': { |
| 27 | + vanilla: {enabled: false, sensitivity: 'high'}, |
| 28 | + test_generation: {enabled: true, sensitivity: 'critical'}, |
| 29 | + bug_prediction: {enabled: false, sensitivity: 'medium'}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + schema_version: PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT, |
| 33 | + }, |
| 34 | + organization: { |
| 35 | + 'octo-corp': { |
| 36 | + org_defaults: { |
| 37 | + vanilla: {enabled: false, sensitivity: 'low'}, |
| 38 | + test_generation: {enabled: true, sensitivity: 'medium'}, |
| 39 | + bug_prediction: {enabled: true, sensitivity: 'high'}, |
| 40 | + }, |
| 41 | + repo_overrides: {}, |
| 42 | + schema_version: PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT, |
| 43 | + }, |
| 44 | + }, |
| 45 | + schema_version: PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT, |
| 46 | + }; |
| 47 | + |
| 48 | + MockApiClient.addMockResponse({ |
| 49 | + url: `/organizations/${org.slug}/prevent/ai/github/config/${gitOrgName}/`, |
| 50 | + body: configResponse, |
| 51 | + }); |
| 52 | + |
| 53 | + const {result} = renderHookWithProviders( |
| 54 | + () => usePreventAIGitHubConfig({gitOrgName}), |
| 55 | + { |
| 56 | + organization: org, |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + await waitFor(() => { |
| 61 | + expect(result.current.isPending).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + expect(result.current.data).toMatchObject(configResponse); |
| 65 | + expect(result.current.isError).toBeFalsy(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('sets isError when API fails', async () => { |
| 69 | + MockApiClient.addMockResponse({ |
| 70 | + url: `/organizations/${org.slug}/prevent/ai/github/config/${gitOrgName}/`, |
| 71 | + statusCode: 500, |
| 72 | + }); |
| 73 | + |
| 74 | + const {result} = renderHookWithProviders( |
| 75 | + () => usePreventAIGitHubConfig({gitOrgName}), |
| 76 | + { |
| 77 | + organization: org, |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + await waitFor(() => { |
| 82 | + expect(result.current.isPending).toBe(false); |
| 83 | + }); |
| 84 | + |
| 85 | + expect(result.current.isError).toBe(true); |
| 86 | + expect(result.current.data).toBeUndefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('uses correct API URL for different org', async () => { |
| 90 | + const diffOrg = OrganizationFixture({slug: 'diff-org'}); |
| 91 | + |
| 92 | + MockApiClient.addMockResponse({ |
| 93 | + url: `/organizations/diff-org/prevent/ai/github/config/${gitOrgName}/`, |
| 94 | + body: {}, |
| 95 | + }); |
| 96 | + |
| 97 | + const {result} = renderHookWithProviders( |
| 98 | + () => usePreventAIGitHubConfig({gitOrgName}), |
| 99 | + { |
| 100 | + organization: diffOrg, |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + await waitFor(() => { |
| 105 | + expect(result.current.isPending).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + expect(result.current.isError).toBeFalsy(); |
| 109 | + }); |
| 110 | +}); |
0 commit comments