|
| 1 | +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 2 | +import axios from 'axios'; |
| 3 | + |
| 4 | +describe('git-push service', () => { |
| 5 | + const originalLocation = globalThis.location; |
| 6 | + |
| 7 | + beforeAll(() => { |
| 8 | + globalThis.location = { origin: 'https://lovely-git-proxy.com' } as any; |
| 9 | + globalThis.localStorage = { |
| 10 | + getItem: vi.fn().mockResolvedValue(null), |
| 11 | + } as any; |
| 12 | + globalThis.document = { |
| 13 | + cookie: '', |
| 14 | + } as any; |
| 15 | + }); |
| 16 | + |
| 17 | + afterAll(() => { |
| 18 | + globalThis.location = originalLocation; |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + vi.restoreAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('rejectPush', () => { |
| 26 | + it('should return true when successfully rejected a push', async () => { |
| 27 | + const axiosPostSpy = vi.spyOn(axios, 'post').mockResolvedValue({ status: 200 }); |
| 28 | + |
| 29 | + const { rejectPush } = await import('../../../src/ui/services/git-push'); |
| 30 | + const setMessageSpy = vi.fn(); |
| 31 | + |
| 32 | + const result = await rejectPush('test-push-id-123', setMessageSpy, { |
| 33 | + reason: 'tests do not pass', |
| 34 | + }); |
| 35 | + |
| 36 | + expect(result).toBe(true); |
| 37 | + expect(setMessageSpy).toHaveBeenCalledExactlyOnceWith(''); |
| 38 | + expect(axiosPostSpy).toHaveBeenCalledWith( |
| 39 | + 'https://lovely-git-proxy.com/api/v1/push/test-push-id-123/reject', |
| 40 | + { params: { reason: 'tests do not pass' } }, |
| 41 | + expect.any(Object), |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return false when returns 401', async () => { |
| 46 | + const error: any = new Error('Unauthorized'); |
| 47 | + error.response = { status: 401 }; |
| 48 | + const axiosPostSpy = vi.spyOn(axios, 'post').mockRejectedValue(error); |
| 49 | + |
| 50 | + const { rejectPush } = await import('../../../src/ui/services/git-push'); |
| 51 | + const setMessageSpy = vi.fn(); |
| 52 | + |
| 53 | + const result = await rejectPush('test-push-id-456', setMessageSpy, { |
| 54 | + reason: 'tests do not pass', |
| 55 | + }); |
| 56 | + |
| 57 | + expect(result).toBe(false); |
| 58 | + expect(setMessageSpy).toHaveBeenCalledExactlyOnceWith('You are not authorised to reject...'); |
| 59 | + expect(axiosPostSpy).toHaveBeenCalledOnce(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('authorisePush', () => { |
| 64 | + it('should return true when authorised a push', async () => { |
| 65 | + const axiosPostSpy = vi.spyOn(axios, 'post').mockResolvedValue({ status: 200 }); |
| 66 | + |
| 67 | + const { authorisePush } = await import('../../../src/ui/services/git-push'); |
| 68 | + const setMessageSpy = vi.fn(); |
| 69 | + const attestation = [ |
| 70 | + { label: 'Reviewed code', checked: true }, |
| 71 | + { label: 'Verified tests', checked: true }, |
| 72 | + ]; |
| 73 | + |
| 74 | + const result = await authorisePush('test-push-id-789', setMessageSpy, attestation); |
| 75 | + |
| 76 | + expect(result).toBe(true); |
| 77 | + expect(setMessageSpy).toHaveBeenCalledExactlyOnceWith(''); |
| 78 | + expect(axiosPostSpy).toHaveBeenCalledWith( |
| 79 | + 'https://lovely-git-proxy.com/api/v1/push/test-push-id-789/authorise', |
| 80 | + { params: { attestation } }, |
| 81 | + expect.any(Object), |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return false when returned 401', async () => { |
| 86 | + const error: any = new Error('Unauthorized'); |
| 87 | + error.response = { status: 401 }; |
| 88 | + const axiosPostSpy = vi.spyOn(axios, 'post').mockRejectedValue(error); |
| 89 | + |
| 90 | + const { authorisePush } = await import('../../../src/ui/services/git-push'); |
| 91 | + const setMessageSpy = vi.fn(); |
| 92 | + const attestation = [{ label: 'Reviewed code', checked: true }]; |
| 93 | + |
| 94 | + const result = await authorisePush('test-push-id-101', setMessageSpy, attestation); |
| 95 | + |
| 96 | + expect(result).toBe(false); |
| 97 | + expect(setMessageSpy).toHaveBeenCalledExactlyOnceWith('You are not authorised to approve...'); |
| 98 | + expect(axiosPostSpy).toHaveBeenCalledOnce(); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments