Skip to content
Snippets Groups Projects
Select Git revision
  • 7eef0d3ed0acd1c67224c55b87395df06bde9753
  • main default protected
  • next
  • revert-31645-feat/rename-gradle-wrapper-validation-action
  • renovate/main-redis-5.x
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • fix/36615-branch-reuse-bug
  • 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.34.1
  • 41.34.0
  • 41.33.0
  • 41.32.3
  • 41.32.2
  • 41.32.1
  • 41.32.0
  • 41.31.1
  • 41.31.0
  • 41.30.5
  • 41.30.4
  • 41.30.3
  • 41.30.2
  • 41.30.1
  • 41.30.0
  • 41.29.1
  • 41.29.0
  • 41.28.2
  • 41.28.1
  • 41.28.0
41 results

comment.spec.ts

Blame
  • comment.spec.ts 5.59 KiB
    import { mocked, platform } from '../../../test/util';
    import * as _cache from '../../util/cache/repository';
    import type { RepoCacheData } from '../../util/cache/repository/types';
    import { ensureComment, ensureCommentRemoval } from './comment';
    
    jest.mock('.');
    jest.mock('../../util/cache/repository');
    
    const cache = mocked(_cache);
    
    describe('modules/platform/comment', () => {
      let repoCache: RepoCacheData = {};
    
      beforeEach(() => {
        repoCache = {};
    
        cache.getCache.mockReturnValue(repoCache);
      });
    
      describe('ensureComment', () => {
        it('caches created comment', async () => {
          platform.ensureComment.mockResolvedValueOnce(true);
    
          const res = await ensureComment({
            number: 1,
            topic: 'foo',
            content: 'bar',
          });
    
          expect(res).toBe(true);
          expect(repoCache).toEqual({
            prComments: {
              '1': {
                foo: 'd82c4eb5261cb9c8aa9855edd67d1bd10482f41529858d925094d173fa662aa91ff39bc5b188615273484021dfb16fd8284cf684ccf0fc795be3aa2fc1e6c181',
              },
            },
          });
        });
    
        it('caches comment with no topic', async () => {
          platform.ensureComment.mockResolvedValueOnce(true);
    
          const res = await ensureComment({
            number: 1,
            topic: null,
            content: 'bar',
          });
    
          expect(res).toBe(true);
          expect(repoCache).toEqual({
            prComments: {
              '1': {
                '': 'd82c4eb5261cb9c8aa9855edd67d1bd10482f41529858d925094d173fa662aa91ff39bc5b188615273484021dfb16fd8284cf684ccf0fc795be3aa2fc1e6c181',
              },
            },
          });
        });
    
        it('does not cache failed comment', async () => {
          platform.ensureComment.mockResolvedValueOnce(false);
    
          const res = await ensureComment({
            number: 1,
            topic: 'foo',
            content: 'bar',
          });
    
          expect(res).toBe(false);
          expect(repoCache).toBeEmpty();
        });
    
        it('short-circuits if comment already exists', async () => {
          platform.ensureComment.mockResolvedValue(true);
    
          await ensureComment({ number: 1, topic: 'aaa', content: '111' });
          await ensureComment({ number: 1, topic: 'aaa', content: '111' });
    
          expect(platform.ensureComment).toHaveBeenCalledTimes(1);
        });
    
        it('rewrites content hash', async () => {
          platform.ensureComment.mockResolvedValue(true);
    
          await ensureComment({ number: 1, topic: 'aaa', content: '111' });
          await ensureComment({ number: 1, topic: 'aaa', content: '222' });
    
          expect(platform.ensureComment).toHaveBeenCalledTimes(2);
          expect(repoCache).toEqual({
            prComments: {
              '1': {
                aaa: '5f28f24f5520230fd1e66ea6ac649e9f9637515f516b2ef74fc90622b60f165eafca8f34db8471b85b9b4a2cdf72f75099ae0eb8860c4f339252261778d406eb',
              },
            },
          });
        });
    
        it('caches comments many comments with different topics', async () => {
          platform.ensureComment.mockResolvedValue(true);
    
          await ensureComment({ number: 1, topic: 'aaa', content: '111' });
          await ensureComment({ number: 1, topic: 'aaa', content: '111' });
    
          await ensureComment({ number: 1, topic: 'bbb', content: '111' });
          await ensureComment({ number: 1, topic: 'bbb', content: '111' });
    
          expect(platform.ensureComment).toHaveBeenCalledTimes(2);
          expect(repoCache).toEqual({
            prComments: {
              '1': {
                aaa: 'fb131bc57a477c8c9d068f1ee5622ac304195a77164ccc2d75d82dfe1a727ba8d674ed87f96143b2b416aacefb555e3045c356faa23e6d21de72b85822e39fdd',
                bbb: 'fb131bc57a477c8c9d068f1ee5622ac304195a77164ccc2d75d82dfe1a727ba8d674ed87f96143b2b416aacefb555e3045c356faa23e6d21de72b85822e39fdd',
              },
            },
          });
        });
      });
    
      describe('ensureCommentRemoval', () => {
        beforeEach(() => {
          platform.ensureCommentRemoval.mockResolvedValueOnce();
          platform.ensureComment.mockResolvedValue(true);
        });
    
        it('deletes cached comment by topic', async () => {
          await ensureComment({ number: 1, topic: 'aaa', content: '111' });
          await ensureCommentRemoval({ type: 'by-topic', number: 1, topic: 'aaa' });
          expect(repoCache).toEqual({
            prComments: { '1': {} },
          });
        });
    
        it('deletes cached comment by content', async () => {
          await ensureComment({ number: 1, topic: 'aaa', content: '111' });
          await ensureCommentRemoval({
            type: 'by-content',
            number: 1,
            content: '111',
          });
          expect(repoCache).toEqual({
            prComments: { '1': {} },
          });
        });
    
        it('deletes by content only one comment', async () => {
          await ensureComment({ number: 1, topic: 'aaa', content: '111' });
          await ensureComment({ number: 1, topic: 'bbb', content: '111' });
          await ensureCommentRemoval({
            type: 'by-content',
            number: 1,
            content: '111',
          });
          expect(repoCache).toEqual({
            prComments: {
              '1': {
                bbb: 'fb131bc57a477c8c9d068f1ee5622ac304195a77164ccc2d75d82dfe1a727ba8d674ed87f96143b2b416aacefb555e3045c356faa23e6d21de72b85822e39fdd',
              },
            },
          });
        });
    
        it('deletes only for selected PR', async () => {
          await ensureComment({ number: 1, topic: 'aaa', content: '111' });
          await ensureComment({ number: 2, topic: 'aaa', content: '111' });
          await ensureCommentRemoval({
            type: 'by-content',
            number: 1,
            content: '111',
          });
          expect(repoCache).toEqual({
            prComments: {
              '1': {},
              '2': {
                aaa: 'fb131bc57a477c8c9d068f1ee5622ac304195a77164ccc2d75d82dfe1a727ba8d674ed87f96143b2b416aacefb555e3045c356faa23e6d21de72b85822e39fdd',
              },
            },
          });
        });
      });
    });