Files
renovate/lib/util/git/behind-base-branch-cache.spec.ts
Sebastian Poxhofer 1e03699cd6 test: add linter for as LongCommitSha in test files (#44681)
* test: add linter for `as LongCommitSha` in test files

Adds a custom oxlint rule `renovate/prefer-fake-sha-in-specs` that flags
`as LongCommitSha` casts in spec files and suggests the deterministic
`fakeSha()` helper from `test/util` instead. Enabled at error level for
`**/*.spec.ts` and `test/**`; all 131 pre-existing casts across 22 spec
files replaced.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Update lib/modules/platform/bitbucket-server/index.spec.ts

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-20 10:08:09 +00:00

284 lines
7.1 KiB
TypeScript

import { fakeSha, logger, partial } from '~test/util.ts';
import * as _repositoryCache from '../cache/repository/index.ts';
import type { BranchCache, RepoCacheData } from '../cache/repository/types.ts';
import {
getCachedBehindBaseResult,
setCachedBehindBaseResult,
} from './behind-base-branch-cache.ts';
vi.mock('../cache/repository/index.ts');
const repositoryCache = vi.mocked(_repositoryCache);
describe('util/git/behind-base-branch-cache', () => {
const branchSha = fakeSha('branch_sha');
const baseBranchSha = fakeSha('base_branch_sha');
let repoCache: RepoCacheData = {};
beforeEach(() => {
repoCache = {};
repositoryCache.getCache.mockReturnValue(repoCache);
});
describe('getCachedBehindBaseResult', () => {
it('returns null if cache is not populated', () => {
expect(
getCachedBehindBaseResult(
'branch',
branchSha,
'base_branch',
baseBranchSha,
),
).toBeNull();
});
it('returns null if branch not found', () => {
repoCache = {
branches: [
partial<BranchCache>({
branchName: 'not_branch',
baseBranchSha,
baseBranch: 'base_branch',
sha: branchSha,
isBehindBase: true,
}),
],
};
repositoryCache.getCache.mockReturnValue(repoCache);
expect(
getCachedBehindBaseResult(
'branch',
branchSha,
'base_branch',
baseBranchSha,
),
).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: branchSha,
isBehindBase: true,
}),
],
};
repositoryCache.getCache.mockReturnValue(repoCache);
expect(
getCachedBehindBaseResult(
'branch',
branchSha,
'base_branch',
baseBranchSha,
),
).toBeNull();
});
it('returns null if branch sha is different', () => {
repoCache = {
branches: [
partial<BranchCache>({
branchName: 'branch',
baseBranchSha,
baseBranch: 'base_branch',
sha: 'not_branch_sha',
isBehindBase: true,
}),
],
};
repositoryCache.getCache.mockReturnValue(repoCache);
expect(
getCachedBehindBaseResult(
'branch',
branchSha,
'base_branch',
baseBranchSha,
),
).toBeNull();
});
it('returns null if cached value is undefined', () => {
repoCache = {
branches: [
partial<BranchCache>({
branchName: 'branch',
baseBranchSha,
baseBranch: 'base_branch',
sha: 'not_branch_sha',
}),
],
};
repositoryCache.getCache.mockReturnValue(repoCache);
expect(
getCachedBehindBaseResult(
'branch',
branchSha,
'base_branch',
baseBranchSha,
),
).toBeNull();
});
it('returns null if base branch SHA is null', () => {
repoCache = {
branches: [
partial<BranchCache>({
branchName: 'branch',
baseBranchSha: null,
baseBranch: 'base_branch',
sha: branchSha,
isBehindBase: true,
}),
],
};
repositoryCache.getCache.mockReturnValue(repoCache);
expect(
getCachedBehindBaseResult(
'branch',
branchSha,
'base_branch',
baseBranchSha,
),
).toBeNull();
});
it('returns null if branch SHA is null', () => {
repoCache = {
branches: [
partial<BranchCache>({
branchName: 'branch',
baseBranchSha,
baseBranch: 'base_branch',
sha: null,
isBehindBase: true,
}),
],
};
repositoryCache.getCache.mockReturnValue(repoCache);
expect(
getCachedBehindBaseResult(
'branch',
branchSha,
'base_branch',
baseBranchSha,
),
).toBeNull();
});
it('returns cached value', () => {
repoCache = {
branches: [
partial<BranchCache>({
branchName: 'branch',
baseBranchSha,
baseBranch: 'base_branch',
sha: branchSha,
isBehindBase: true,
}),
],
};
repositoryCache.getCache.mockReturnValue(repoCache);
expect(
getCachedBehindBaseResult(
'branch',
branchSha,
'base_branch',
baseBranchSha,
),
).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,
},
],
});
});
});
});