Files
renovate/lib/util/git/set-branch-commit.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

106 lines
3.2 KiB
TypeScript

import { DateTime } from 'luxon';
import { fakeSha, git, logger, partial } from '~test/util.ts';
import * as _repositoryCache from '../cache/repository/index.ts';
import type { BranchCache, RepoCacheData } from '../cache/repository/types.ts';
import { setBranchNewCommit } from './set-branch-commit.ts';
vi.mock('../cache/repository/index.ts');
vi.mock('./index.ts');
const repositoryCache = vi.mocked(_repositoryCache);
describe('util/git/set-branch-commit', () => {
const baseSha = fakeSha('base_SHA');
let repoCache: RepoCacheData = {};
beforeEach(() => {
repoCache = {};
repositoryCache.getCache.mockReturnValue(repoCache);
});
describe('setBranchCommit', () => {
it('sets new branch in cache if it does not exist', () => {
git.getBranchCommit.mockReturnValueOnce(baseSha);
setBranchNewCommit('branch_name', 'base_branch', 'SHA', null);
expect(logger.logger.debug).toHaveBeenCalledWith(
'setBranchCommit(): Branch cache not present',
);
expect(repoCache.branches).toEqual([
{
branchName: 'branch_name',
baseBranch: 'base_branch',
sha: 'SHA',
baseBranchSha: baseSha,
isBehindBase: false,
isConflicted: false,
isModified: false,
pristine: true,
},
]);
});
it('sets new values in branch when old state exists', () => {
repoCache = {
branches: [
partial<BranchCache>({
branchName: 'branch_name',
baseBranch: 'base_branch',
sha: 'SHA',
baseBranchSha: baseSha,
isBehindBase: false,
isModified: true,
pristine: false,
isConflicted: true,
}),
],
};
git.getBranchCommit.mockReturnValueOnce(baseSha);
repositoryCache.getCache.mockReturnValue(repoCache);
setBranchNewCommit('branch_name', 'base_branch', 'SHA', null);
expect(repoCache.branches).toEqual([
{
branchName: 'branch_name',
baseBranch: 'base_branch',
sha: 'SHA',
baseBranchSha: baseSha,
isBehindBase: false,
isModified: false,
isConflicted: false,
pristine: true,
},
]);
});
it('sets commitTimestamp when DateTime is provided', () => {
repoCache = {
branches: [
partial<BranchCache>({
branchName: 'branch_name',
baseBranch: 'base_branch',
sha: 'old_SHA',
baseBranchSha: baseSha,
}),
],
};
const commitDate = DateTime.fromISO('2023-05-20T14:25:30.123Z').toUTC();
git.getBranchCommit.mockReturnValueOnce(baseSha);
repositoryCache.getCache.mockReturnValue(repoCache);
setBranchNewCommit('branch_name', 'base_branch', 'new_SHA', commitDate);
expect(repoCache.branches).toEqual([
{
branchName: 'branch_name',
baseBranch: 'base_branch',
sha: 'new_SHA',
baseBranchSha: baseSha,
isBehindBase: false,
isModified: false,
isConflicted: false,
pristine: true,
commitTimestamp: '2023-05-20T14:25:30.123Z',
},
]);
});
});
});