|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { Context } from 'probot'; |
| 3 | +import { DeepPartial } from 'ts-essentials'; |
| 4 | +import { describe, it, vi } from 'vitest'; |
| 5 | +import { Config } from '../../src/common/types'; |
| 6 | +import { CacheService } from '../../src/services'; |
| 7 | +import { WalkthroughSkipConditions } from '../../src/utils'; |
| 8 | + |
| 9 | +describe('WalkthroughSkipConditions', () => { |
| 10 | + // shouldSkip returns true if any skip condition is met |
| 11 | + it('should return true when any skip condition is met', async () => { |
| 12 | + const context: DeepPartial<Context<'pull_request'>> = { |
| 13 | + payload: { sender: { login: 'lorem' }, pull_request: { head: { ref: 'branchName' }, state: 'closed' } } |
| 14 | + }; |
| 15 | + const config = { |
| 16 | + walkthrough: { |
| 17 | + ignore: { |
| 18 | + 'pull-requests': { |
| 19 | + labels: [], |
| 20 | + titles: [], |
| 21 | + branches: [] |
| 22 | + } |
| 23 | + }, |
| 24 | + enabled: true, |
| 25 | + 'stop-on': { 'pull-requests': { closed: true } }, |
| 26 | + allow: { |
| 27 | + 'pull-requests': { |
| 28 | + always: [], |
| 29 | + never: [], |
| 30 | + 'contributors-only': false, |
| 31 | + 'authors-only': false |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } as Config; |
| 36 | + const cacheService = {} as CacheService; |
| 37 | + const apiKey = 'testApiKey'; |
| 38 | + const walkthrough = new WalkthroughSkipConditions(context as any, config, cacheService, apiKey); |
| 39 | + |
| 40 | + const result = await walkthrough.shouldSkip(); |
| 41 | + |
| 42 | + expect(result).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + // shouldSkip returns false if no skip condition is met |
| 46 | + it('should return false when no skip condition is met', async () => { |
| 47 | + const context = { |
| 48 | + payload: { sender: { login: 'lorem' }, pull_request: { head: { ref: 'branchName' }, state: 'open' } } |
| 49 | + } as Context<'pull_request'>; |
| 50 | + const config = { |
| 51 | + walkthrough: { |
| 52 | + ignore: { |
| 53 | + 'pull-requests': { |
| 54 | + labels: [], |
| 55 | + titles: [], |
| 56 | + branches: [] |
| 57 | + } |
| 58 | + }, |
| 59 | + enabled: true, |
| 60 | + 'stop-on': { 'pull-requests': { closed: false } }, |
| 61 | + allow: { |
| 62 | + 'pull-requests': { |
| 63 | + always: [], |
| 64 | + never: [], |
| 65 | + 'contributors-only': false, |
| 66 | + 'authors-only': false |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } as Config; |
| 71 | + const cacheService = {} as CacheService; |
| 72 | + const apiKey = 'testApiKey'; |
| 73 | + const walkthrough = new WalkthroughSkipConditions(context, config, cacheService, apiKey); |
| 74 | + |
| 75 | + const result = await walkthrough.shouldSkip(); |
| 76 | + |
| 77 | + expect(result).toBe(false); |
| 78 | + }); |
| 79 | + |
| 80 | + // isContributorOnly returns true if sender is not a contributor |
| 81 | + it('should return true when sender is not a contributor', async () => { |
| 82 | + const context = { payload: { sender: { login: 'nonContributor' } } } as Context<'pull_request'>; |
| 83 | + const config = { walkthrough: { allow: { 'pull-requests': { 'contributors-only': true, always: [] } } } } as Config; |
| 84 | + const cacheService = { |
| 85 | + get: vi.fn().mockResolvedValue([{ login: 'contributor' }]) |
| 86 | + } as unknown as CacheService; |
| 87 | + const apiKey = 'testApiKey'; |
| 88 | + const walkthrough = new WalkthroughSkipConditions(context, config, cacheService, apiKey); |
| 89 | + |
| 90 | + const result = await walkthrough['isContributorOnly'](); |
| 91 | + |
| 92 | + expect(result).toBe(true); |
| 93 | + }); |
| 94 | + |
| 95 | + // isAuthorOnly returns true if sender is not the author |
| 96 | + it('should return true when sender is not the author', () => { |
| 97 | + const context = { |
| 98 | + payload: { |
| 99 | + pull_request: { user: { login: 'author' } }, |
| 100 | + sender: { login: 'notAuthor' } |
| 101 | + } |
| 102 | + } as Context<'pull_request'>; |
| 103 | + const config = { walkthrough: { allow: { 'pull-requests': { 'authors-only': true, always: [] } } } } as Config; |
| 104 | + const cacheService = {} as CacheService; |
| 105 | + const apiKey = 'testApiKey'; |
| 106 | + const walkthrough = new WalkthroughSkipConditions(context, config, cacheService, apiKey); |
| 107 | + |
| 108 | + const result = walkthrough['isAuthorOnly'](); |
| 109 | + |
| 110 | + expect(result).toBe(true); |
| 111 | + }); |
| 112 | + |
| 113 | + // isDisabled returns true if walkthrough is disabled in config |
| 114 | + it('should return true when walkthrough is disabled in config', () => { |
| 115 | + const context = {} as Context<'pull_request'>; |
| 116 | + const config = { walkthrough: { enabled: false } } as Config; |
| 117 | + const cacheService = {} as CacheService; |
| 118 | + const apiKey = 'testApiKey'; |
| 119 | + const walkthrough = new WalkthroughSkipConditions(context, config, cacheService, apiKey); |
| 120 | + |
| 121 | + const result = walkthrough['isDisabled'](); |
| 122 | + |
| 123 | + expect(result).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + // isContributorOnly handles missing contributors in cache |
| 127 | + it('should handle missing contributors in cache', async () => { |
| 128 | + const context = { payload: { sender: { login: 'user' } } } as Context<'pull_request'>; |
| 129 | + const config = { walkthrough: { allow: { 'pull-requests': { 'contributors-only': true, always: [] } } } } as Config; |
| 130 | + const cacheService = { |
| 131 | + get: vi.fn().mockResolvedValue(null) |
| 132 | + } as unknown as CacheService; |
| 133 | + const apiKey = 'testApiKey'; |
| 134 | + const walkthrough = new WalkthroughSkipConditions(context, config, cacheService, apiKey); |
| 135 | + |
| 136 | + const result = await walkthrough['isContributorOnly'](); |
| 137 | + |
| 138 | + expect(result).toBe(true); |
| 139 | + }); |
| 140 | + |
| 141 | + // isAuthorOnly handles case sensitivity in author comparison |
| 142 | + it('should handle case sensitivity in author comparison', () => { |
| 143 | + const context = { |
| 144 | + payload: { |
| 145 | + pull_request: { user: { login: 'Author' } }, |
| 146 | + sender: { login: 'author' } |
| 147 | + } |
| 148 | + } as Context<'pull_request'>; |
| 149 | + const config = { walkthrough: { allow: { 'pull-requests': { 'authors-only': true, always: [] } } } } as Config; |
| 150 | + const cacheService = {} as CacheService; |
| 151 | + const apiKey = 'testApiKey'; |
| 152 | + const walkthrough = new WalkthroughSkipConditions(context, config, cacheService, apiKey); |
| 153 | + |
| 154 | + const result = walkthrough['isAuthorOnly'](); |
| 155 | + |
| 156 | + expect(result).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + // isLabelIgnored handles case sensitivity in label comparison |
| 160 | + it('should handle case sensitivity in label comparison', () => { |
| 161 | + const context = { |
| 162 | + payload: { |
| 163 | + pull_request: { |
| 164 | + labels: [{ name: 'bug' }] |
| 165 | + } |
| 166 | + } |
| 167 | + } as Context<'pull_request'>; |
| 168 | + const config = { |
| 169 | + walkthrough: { |
| 170 | + ignore: { |
| 171 | + 'pull-requests': { |
| 172 | + labels: ['BUG'] |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } as Config; |
| 177 | + const cacheService = {} as CacheService; |
| 178 | + const apiKey = 'testApiKey'; |
| 179 | + const walkthrough = new WalkthroughSkipConditions(context, config, cacheService, apiKey); |
| 180 | + |
| 181 | + const result = walkthrough['isLabelIgnored'](); |
| 182 | + |
| 183 | + expect(result).toBe(true); |
| 184 | + }); |
| 185 | + |
| 186 | + // isTitleIgnored handles case sensitivity in title comparison |
| 187 | + it('should handle case sensitivity in title comparison', () => { |
| 188 | + const context = { |
| 189 | + payload: { |
| 190 | + pull_request: { |
| 191 | + title: 'Fix Bug' |
| 192 | + } |
| 193 | + } |
| 194 | + } as Context<'pull_request'>; |
| 195 | + const config = { |
| 196 | + walkthrough: { |
| 197 | + ignore: { |
| 198 | + 'pull-requests': { |
| 199 | + titles: ['fix bug'] |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } as Config; |
| 204 | + const cacheService = {} as CacheService; |
| 205 | + const apiKey = 'testApiKey'; |
| 206 | + const walkthrough = new WalkthroughSkipConditions(context, config, cacheService, apiKey); |
| 207 | + |
| 208 | + const result = walkthrough['isTitleIgnored'](); |
| 209 | + |
| 210 | + expect(result).toBe(true); |
| 211 | + }); |
| 212 | +}); |
0 commit comments