|
| 1 | +/** |
| 2 | + * Unit tests for branch cleanup utilities. |
| 3 | + * |
| 4 | + * Purpose: |
| 5 | + * Tests the branch lifecycle management for the fix command. |
| 6 | + * |
| 7 | + * Test Coverage: |
| 8 | + * - cleanupStaleBranch function |
| 9 | + * - cleanupFailedPrBranches function |
| 10 | + * - cleanupSuccessfulPrLocalBranch function |
| 11 | + * - cleanupErrorBranches function |
| 12 | + * |
| 13 | + * Related Files: |
| 14 | + * - src/commands/fix/branch-cleanup.mts (implementation) |
| 15 | + */ |
| 16 | + |
| 17 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 18 | + |
| 19 | +// Mock logger. |
| 20 | +const mockLogger = vi.hoisted(() => ({ |
| 21 | + log: vi.fn(), |
| 22 | + error: vi.fn(), |
| 23 | + warn: vi.fn(), |
| 24 | + fail: vi.fn(), |
| 25 | + success: vi.fn(), |
| 26 | +})) |
| 27 | +vi.mock('@socketsecurity/lib/logger', () => ({ |
| 28 | + getDefaultLogger: () => mockLogger, |
| 29 | +})) |
| 30 | + |
| 31 | +// Mock git operations. |
| 32 | +const mockGitDeleteBranch = vi.hoisted(() => vi.fn()) |
| 33 | +const mockGitDeleteRemoteBranch = vi.hoisted(() => vi.fn()) |
| 34 | +vi.mock('../../../../src/utils/git/operations.mjs', () => ({ |
| 35 | + gitDeleteBranch: mockGitDeleteBranch, |
| 36 | + gitDeleteRemoteBranch: mockGitDeleteRemoteBranch, |
| 37 | +})) |
| 38 | + |
| 39 | +import { |
| 40 | + cleanupErrorBranches, |
| 41 | + cleanupFailedPrBranches, |
| 42 | + cleanupStaleBranch, |
| 43 | + cleanupSuccessfulPrLocalBranch, |
| 44 | +} from '../../../../src/commands/fix/branch-cleanup.mts' |
| 45 | + |
| 46 | +describe('branch-cleanup', () => { |
| 47 | + const cwd = '/test/repo' |
| 48 | + const branch = 'socket/fix/GHSA-1234-5678-90ab' |
| 49 | + const ghsaId = 'GHSA-1234-5678-90ab' |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + vi.clearAllMocks() |
| 53 | + }) |
| 54 | + |
| 55 | + describe('cleanupStaleBranch', () => { |
| 56 | + it('deletes remote and local branches when remote deletion succeeds', async () => { |
| 57 | + mockGitDeleteRemoteBranch.mockResolvedValue(true) |
| 58 | + mockGitDeleteBranch.mockResolvedValue(undefined) |
| 59 | + |
| 60 | + const result = await cleanupStaleBranch(branch, ghsaId, cwd) |
| 61 | + |
| 62 | + expect(result).toBe(true) |
| 63 | + expect(mockGitDeleteRemoteBranch).toHaveBeenCalledWith(branch, cwd) |
| 64 | + expect(mockGitDeleteBranch).toHaveBeenCalledWith(branch, cwd) |
| 65 | + expect(mockLogger.warn).toHaveBeenCalledWith( |
| 66 | + expect.stringContaining('Stale branch'), |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + it('returns false when remote deletion fails', async () => { |
| 71 | + mockGitDeleteRemoteBranch.mockResolvedValue(false) |
| 72 | + |
| 73 | + const result = await cleanupStaleBranch(branch, ghsaId, cwd) |
| 74 | + |
| 75 | + expect(result).toBe(false) |
| 76 | + expect(mockGitDeleteRemoteBranch).toHaveBeenCalledWith(branch, cwd) |
| 77 | + expect(mockGitDeleteBranch).not.toHaveBeenCalled() |
| 78 | + expect(mockLogger.error).toHaveBeenCalledWith( |
| 79 | + expect.stringContaining('Failed to delete stale remote branch'), |
| 80 | + ) |
| 81 | + }) |
| 82 | + |
| 83 | + it('logs warning about stale branch', async () => { |
| 84 | + mockGitDeleteRemoteBranch.mockResolvedValue(true) |
| 85 | + |
| 86 | + await cleanupStaleBranch(branch, ghsaId, cwd) |
| 87 | + |
| 88 | + expect(mockLogger.warn).toHaveBeenCalledWith( |
| 89 | + expect.stringContaining(branch), |
| 90 | + ) |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + describe('cleanupFailedPrBranches', () => { |
| 95 | + it('deletes both remote and local branches', async () => { |
| 96 | + mockGitDeleteRemoteBranch.mockResolvedValue(true) |
| 97 | + mockGitDeleteBranch.mockResolvedValue(undefined) |
| 98 | + |
| 99 | + await cleanupFailedPrBranches(branch, cwd) |
| 100 | + |
| 101 | + expect(mockGitDeleteRemoteBranch).toHaveBeenCalledWith(branch, cwd) |
| 102 | + expect(mockGitDeleteBranch).toHaveBeenCalledWith(branch, cwd) |
| 103 | + }) |
| 104 | + |
| 105 | + it('continues to delete local branch even if remote fails', async () => { |
| 106 | + mockGitDeleteRemoteBranch.mockResolvedValue(false) |
| 107 | + mockGitDeleteBranch.mockResolvedValue(undefined) |
| 108 | + |
| 109 | + await cleanupFailedPrBranches(branch, cwd) |
| 110 | + |
| 111 | + expect(mockGitDeleteRemoteBranch).toHaveBeenCalledWith(branch, cwd) |
| 112 | + expect(mockGitDeleteBranch).toHaveBeenCalledWith(branch, cwd) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + describe('cleanupSuccessfulPrLocalBranch', () => { |
| 117 | + it('deletes only local branch, keeping remote for PR', async () => { |
| 118 | + mockGitDeleteBranch.mockResolvedValue(undefined) |
| 119 | + |
| 120 | + await cleanupSuccessfulPrLocalBranch(branch, cwd) |
| 121 | + |
| 122 | + expect(mockGitDeleteBranch).toHaveBeenCalledWith(branch, cwd) |
| 123 | + expect(mockGitDeleteRemoteBranch).not.toHaveBeenCalled() |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe('cleanupErrorBranches', () => { |
| 128 | + it('deletes both remote and local when remote exists', async () => { |
| 129 | + mockGitDeleteRemoteBranch.mockResolvedValue(true) |
| 130 | + mockGitDeleteBranch.mockResolvedValue(undefined) |
| 131 | + |
| 132 | + await cleanupErrorBranches(branch, cwd, true) |
| 133 | + |
| 134 | + expect(mockGitDeleteRemoteBranch).toHaveBeenCalledWith(branch, cwd) |
| 135 | + expect(mockGitDeleteBranch).toHaveBeenCalledWith(branch, cwd) |
| 136 | + }) |
| 137 | + |
| 138 | + it('deletes only local branch when remote does not exist', async () => { |
| 139 | + mockGitDeleteBranch.mockResolvedValue(undefined) |
| 140 | + |
| 141 | + await cleanupErrorBranches(branch, cwd, false) |
| 142 | + |
| 143 | + expect(mockGitDeleteRemoteBranch).not.toHaveBeenCalled() |
| 144 | + expect(mockGitDeleteBranch).toHaveBeenCalledWith(branch, cwd) |
| 145 | + }) |
| 146 | + }) |
| 147 | +}) |
0 commit comments