|
| 1 | +import { describe, expect, test, vi, beforeEach } from 'vitest' |
| 2 | +import { getVersionFromString, getVersionString, compareVersions } from './upgradeToast'; |
| 3 | + |
| 4 | +// --- Pure utility function tests --- |
| 5 | + |
| 6 | +describe('getVersionFromString', () => { |
| 7 | + test('parses a valid semver string', () => { |
| 8 | + expect(getVersionFromString('v1.2.3')).toEqual({ major: 1, minor: 2, patch: 3 }); |
| 9 | + }); |
| 10 | + |
| 11 | + test('returns null for invalid version strings', () => { |
| 12 | + expect(getVersionFromString('not-a-version')).toBeNull(); |
| 13 | + expect(getVersionFromString('1.2.3')).toBeNull(); // missing v prefix |
| 14 | + expect(getVersionFromString('v1.2')).toBeNull(); // missing patch |
| 15 | + expect(getVersionFromString('v1.2.3-beta')).toBeNull(); // pre-release suffix |
| 16 | + }); |
| 17 | + |
| 18 | + test('parses zero versions', () => { |
| 19 | + expect(getVersionFromString('v0.0.0')).toEqual({ major: 0, minor: 0, patch: 0 }); |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +describe('getVersionString', () => { |
| 24 | + test('formats a version object as a string', () => { |
| 25 | + expect(getVersionString({ major: 1, minor: 2, patch: 3 })).toBe('v1.2.3'); |
| 26 | + }); |
| 27 | + |
| 28 | + test('formats zero version', () => { |
| 29 | + expect(getVersionString({ major: 0, minor: 0, patch: 0 })).toBe('v0.0.0'); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +describe('compareVersions', () => { |
| 34 | + test('returns 0 for equal versions', () => { |
| 35 | + const v = { major: 1, minor: 2, patch: 3 }; |
| 36 | + expect(compareVersions(v, v)).toBe(0); |
| 37 | + }); |
| 38 | + |
| 39 | + test('compares by major version first', () => { |
| 40 | + const a = { major: 2, minor: 0, patch: 0 }; |
| 41 | + const b = { major: 1, minor: 9, patch: 9 }; |
| 42 | + expect(compareVersions(a, b)).toBeGreaterThan(0); |
| 43 | + expect(compareVersions(b, a)).toBeLessThan(0); |
| 44 | + }); |
| 45 | + |
| 46 | + test('compares by minor version when major is equal', () => { |
| 47 | + const a = { major: 1, minor: 3, patch: 0 }; |
| 48 | + const b = { major: 1, minor: 2, patch: 9 }; |
| 49 | + expect(compareVersions(a, b)).toBeGreaterThan(0); |
| 50 | + expect(compareVersions(b, a)).toBeLessThan(0); |
| 51 | + }); |
| 52 | + |
| 53 | + test('compares by patch version when major and minor are equal', () => { |
| 54 | + const a = { major: 1, minor: 2, patch: 4 }; |
| 55 | + const b = { major: 1, minor: 2, patch: 3 }; |
| 56 | + expect(compareVersions(a, b)).toBeGreaterThan(0); |
| 57 | + expect(compareVersions(b, a)).toBeLessThan(0); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +// --- UpgradeToast isOwner gating test --- |
| 62 | + |
| 63 | +// We mock the external dependencies to isolate the isOwner behavior. |
| 64 | +// The key assertion: when isOwner=false, fetch should NOT be called. |
| 65 | + |
| 66 | +const mockToast = vi.fn(); |
| 67 | +vi.mock('@/components/hooks/use-toast', () => ({ |
| 68 | + useToast: () => ({ toast: mockToast }), |
| 69 | +})); |
| 70 | + |
| 71 | +vi.mock('usehooks-ts', () => ({ |
| 72 | + useLocalStorage: () => [new Date(0).toUTCString(), vi.fn()], |
| 73 | +})); |
| 74 | + |
| 75 | +vi.mock('@tanstack/react-query', () => ({ |
| 76 | + useQuery: () => ({ data: 'v1.0.0' }), |
| 77 | +})); |
| 78 | + |
| 79 | +vi.mock('@/app/api/(client)/client', () => ({ |
| 80 | + getVersion: vi.fn(), |
| 81 | +})); |
| 82 | + |
| 83 | +describe('UpgradeToast isOwner gating', () => { |
| 84 | + beforeEach(() => { |
| 85 | + vi.restoreAllMocks(); |
| 86 | + // Reset the global fetch mock before each test |
| 87 | + global.fetch = vi.fn(); |
| 88 | + }); |
| 89 | + |
| 90 | + test('does not fetch or show toast when isOwner is false', async () => { |
| 91 | + // Dynamic import after mocks are set up |
| 92 | + const { UpgradeToast } = await import('./upgradeToast'); |
| 93 | + const { render } = await import('@testing-library/react'); |
| 94 | + |
| 95 | + render(<UpgradeToast isOwner={false} />); |
| 96 | + |
| 97 | + // fetch should not have been called because isOwner is false |
| 98 | + expect(global.fetch).not.toHaveBeenCalled(); |
| 99 | + expect(mockToast).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + test('fetches GitHub tags when isOwner is true', async () => { |
| 103 | + const mockResponse = { |
| 104 | + json: () => Promise.resolve([{ name: 'v2.0.0' }]), |
| 105 | + }; |
| 106 | + global.fetch = vi.fn().mockResolvedValue(mockResponse); |
| 107 | + |
| 108 | + const { UpgradeToast } = await import('./upgradeToast'); |
| 109 | + const { render, waitFor } = await import('@testing-library/react'); |
| 110 | + |
| 111 | + render(<UpgradeToast isOwner={true} />); |
| 112 | + |
| 113 | + await waitFor(() => { |
| 114 | + expect(global.fetch).toHaveBeenCalledWith( |
| 115 | + 'https://api.github.com/repos/sourcebot-dev/sourcebot/tags' |
| 116 | + ); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments