Skip to content
Snippets Groups Projects
Select Git revision
  • 549c3aebe1d8e5824d190f815004de1712bb41db
  • main default protected
  • next
  • renovate/main-redis-5.x
  • chore/update-static-data
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • gh-readonly-queue/next/pr-35009-046ebf7cb84ab859f7fefceb5fa53a54ce9736f8
  • 41.62.4
  • 41.62.3
  • 41.62.2
  • 41.62.1
  • 41.62.0
  • 41.61.1
  • 41.61.0
  • 41.60.4
  • 41.60.3
  • 41.60.2
  • 41.60.1
  • 41.60.0
  • 41.59.2
  • 41.59.1
  • 41.59.0
  • 41.58.2
  • 41.58.1
  • 41.58.0
  • 41.57.1
  • 41.57.0
41 results

behind-base-branch-cache.spec.ts

Blame
  • behind-base-branch-cache.spec.ts 7.43 KiB
    import { logger, mocked, partial } from '../../../test/util';
    import * as _repositoryCache from '../cache/repository';
    import type { BranchCache, RepoCacheData } from '../cache/repository/types';
    import {
      getCachedBehindBaseResult,
      setCachedBehindBaseResult,
    } from './behind-base-branch-cache';
    import type { LongCommitSha } from './types';
    
    jest.mock('../cache/repository');
    const repositoryCache = mocked(_repositoryCache);
    
    describe('util/git/behind-base-branch-cache', () => {
      let repoCache: RepoCacheData = {};
    
      beforeEach(() => {
        repoCache = {};
        repositoryCache.getCache.mockReturnValue(repoCache);
      });
    
      describe('getCachedBehindBaseResult', () => {
        it('returns null if cache is not populated', () => {
          expect(
            getCachedBehindBaseResult(
              'branch',
              'branch_sha' as LongCommitSha,
              'base_branch',
              'base_branch_sha' as LongCommitSha,
            ),
          ).toBeNull();
        });
    
        it('returns null if branch not found', () => {
          repoCache = {
            branches: [
              partial<BranchCache>({
                branchName: 'not_branch',
                baseBranchSha: 'base_branch_sha',
                baseBranch: 'base_branch',
                sha: 'branch_sha',
                isBehindBase: true,
              }),
            ],
          };
          repositoryCache.getCache.mockReturnValue(repoCache);
          expect(
            getCachedBehindBaseResult(
              'branch',
              'branch_sha' as LongCommitSha,
              'base_branch',
              'base_branch_sha' as LongCommitSha,
            ),
          ).toBeNull();
        });
    
        it('returns null if base branch SHA is different', () => {
          repoCache = {
            branches: [
              partial<BranchCache>({
                branchName: 'branch',
                baseBranchSha: 'not_base_branch_sha',
                baseBranch: 'base_branch',
                sha: 'branch_sha',
                isBehindBase: true,
              }),
            ],
          };
          repositoryCache.getCache.mockReturnValue(repoCache);
          expect(
            getCachedBehindBaseResult(
              'branch',
              'branch_sha' as LongCommitSha,
              'base_branch',
              'base_branch_sha' as LongCommitSha,
            ),
          ).toBeNull();
        });
    
        it('returns null if branch sha is different', () => {
          repoCache = {
            branches: [
              partial<BranchCache>({
                branchName: 'branch',
                baseBranchSha: 'base_branch_sha',
                baseBranch: 'base_branch',
                sha: 'not_branch_sha',
                isBehindBase: true,
              }),
            ],
          };
          repositoryCache.getCache.mockReturnValue(repoCache);
          expect(
            getCachedBehindBaseResult(
              'branch',
              'branch_sha' as LongCommitSha,
              'base_branch',
              'base_branch_sha' as LongCommitSha,
            ),
          ).toBeNull();
        });
    
        it('returns null if cached value is undefined', () => {
          repoCache = {
            branches: [
              partial<BranchCache>({
                branchName: 'branch',
                baseBranchSha: 'base_branch_sha',
                baseBranch: 'base_branch',
                sha: 'not_branch_sha',
              }),
            ],
          };
          repositoryCache.getCache.mockReturnValue(repoCache);
          expect(
            getCachedBehindBaseResult(
              'branch',
              'branch_sha' as LongCommitSha,
              'base_branch',
              'base_branch_sha' as LongCommitSha,
            ),
          ).toBeNull();
        });
    
        it('returns null if base branch SHA is null', () => {
          repoCache = {
            branches: [
              partial<BranchCache>({
                branchName: 'branch',
                baseBranchSha: null,
                baseBranch: 'base_branch',
                sha: 'branch_sha',
                isBehindBase: true,
              }),
            ],
          };
          repositoryCache.getCache.mockReturnValue(repoCache);
          expect(
            getCachedBehindBaseResult(
              'branch',
              'branch_sha' as LongCommitSha,
              'base_branch',
              'base_branch_sha' as LongCommitSha,
            ),
          ).toBeNull();
        });
    
        it('returns null if branch SHA is null', () => {
          repoCache = {
            branches: [
              partial<BranchCache>({
                branchName: 'branch',
                baseBranchSha: 'base_branch_sha',
                baseBranch: 'base_branch',
                sha: null,
                isBehindBase: true,
              }),
            ],
          };
          repositoryCache.getCache.mockReturnValue(repoCache);
          expect(
            getCachedBehindBaseResult(
              'branch',
              'branch_sha' as LongCommitSha,
              'base_branch',
              'base_branch_sha' as LongCommitSha,
            ),
          ).toBeNull();
        });
    
        it('returns cached value', () => {
          repoCache = {
            branches: [
              partial<BranchCache>({
                branchName: 'branch',
                baseBranchSha: 'base_branch_sha',
                baseBranch: 'base_branch',
                sha: 'branch_sha',
                isBehindBase: true,
              }),
            ],
          };
          repositoryCache.getCache.mockReturnValue(repoCache);
          expect(
            getCachedBehindBaseResult(
              'branch',
              'branch_sha' as LongCommitSha,
              'base_branch',
              'base_branch_sha' as LongCommitSha,
            ),
          ).toBeTrue();
        });
      });
    
      describe('setCachedBehindBasedResult', () => {
        it('returns without updating when cache not populated', () => {
          setCachedBehindBaseResult('foo', false);
          expect(repoCache).toEqual({});
          expect(logger.logger.debug).toHaveBeenCalledWith(
            'setCachedBehindBaseResult(): Branch cache not present',
          );
        });
    
        it('returns without updating when branch not found', () => {
          setCachedBehindBaseResult('foo', false);
          expect(repoCache).toEqual({});
          expect(logger.logger.debug).toHaveBeenCalledWith(
            'setCachedBehindBaseResult(): Branch cache not present',
          );
        });
    
        it('updates cached value', () => {
          repoCache = {
            branches: [
              partial<BranchCache>({
                branchName: 'foo',
                sha: '121',
                isBehindBase: true,
              }),
            ],
          };
          repositoryCache.getCache.mockReturnValue(repoCache);
          setCachedBehindBaseResult('foo', false);
          expect(repoCache).toEqual({
            branches: [
              {
                branchName: 'foo',
                sha: '121',
                isBehindBase: false,
              },
            ],
          });
        });
    
        it('handles multiple branches', () => {
          repoCache = {
            branches: [
              partial<BranchCache>({
                branchName: 'foo-1',
                sha: '111',
                isBehindBase: true,
              }),
              partial<BranchCache>({
                branchName: 'foo-2',
                sha: 'aaa',
                isBehindBase: false,
              }),
              partial<BranchCache>({
                branchName: 'foo-3',
                sha: '222',
                isBehindBase: true,
              }),
            ],
          };
          repositoryCache.getCache.mockReturnValue(repoCache);
          setCachedBehindBaseResult('foo-1', false);
          setCachedBehindBaseResult('foo-2', true);
          setCachedBehindBaseResult('foo-3', false);
          expect(repoCache).toEqual({
            branches: [
              {
                branchName: 'foo-1',
                sha: '111',
                isBehindBase: false,
              },
              {
                branchName: 'foo-2',
                sha: 'aaa',
                isBehindBase: true,
              },
              {
                branchName: 'foo-3',
                sha: '222',
                isBehindBase: false,
              },
            ],
          });
        });
      });
    });