import { Readable } from 'node:stream'; import type { GitPullRequest } from 'azure-devops-node-api/interfaces/GitInterfaces.js'; import { buildTestJwt } from '~test/jwt-util.ts'; import { partial } from '~test/util.ts'; import { streamToString } from '../../../util/streams.ts'; import { getBranchNameWithoutRefsheadsPrefix, getGitStatusContextCombinedName, getGitStatusContextFromCombinedName, getProjectAndRepo, getRenovatePRFormat, getRepoByName, getStorageExtraCloneOpts, getWorkItemTitle, max4000Chars, } from './util.ts'; describe('modules/platform/azure/util', () => { describe('getGitStatusContextCombinedName', () => { it('should return undefined if null context passed', () => { const contextName = getGitStatusContextCombinedName(null); expect(contextName).toBeUndefined(); }); it('should combine valid genre and name with slash', () => { const contextName = getGitStatusContextCombinedName({ genre: 'my-genre', name: 'status-name', }); expect(contextName).toMatch('my-genre/status-name'); }); it('should combine valid empty genre and name without a slash', () => { const contextName = getGitStatusContextCombinedName({ genre: undefined, name: 'status-name', }); expect(contextName).toMatch('status-name'); }); }); describe('getGitStatusContextFromCombinedName', () => { it('should return undefined if null context passed', () => { const context = getGitStatusContextFromCombinedName(null); expect(context).toBeUndefined(); }); it('should parse valid genre and name with slash', () => { const context = getGitStatusContextFromCombinedName( 'my-genre/status-name', ); expect(context).toEqual({ genre: 'my-genre', name: 'status-name', }); }); it('should parse valid genre and name with multiple slashes', () => { const context = getGitStatusContextFromCombinedName( 'my-genre/sub-genre/status-name', ); expect(context).toEqual({ genre: 'my-genre/sub-genre', name: 'status-name', }); }); it('should parse valid empty genre and name without a slash', () => { const context = getGitStatusContextFromCombinedName('status-name'); expect(context).toEqual({ genre: undefined, name: 'status-name', }); }); }); describe('getBranchNameWithoutRefsheadsPrefix', () => { it('should be renamed', () => { const res = getBranchNameWithoutRefsheadsPrefix('refs/heads/testBB'); expect(res).toBe(`testBB`); }); it('should log error and return undefined', () => { const res = getBranchNameWithoutRefsheadsPrefix(undefined); expect(res).toBeUndefined(); }); it('should return the input', () => { const res = getBranchNameWithoutRefsheadsPrefix('testBB'); expect(res).toBe('testBB'); }); }); describe('getRenovatePRFormat', () => { it('should be formated (closed)', () => { const res = getRenovatePRFormat(partial({ status: 2 })); expect(res).toMatchObject({ state: 'closed', status: 2, }); }); it('should be formated (closed v2)', () => { const res = getRenovatePRFormat(partial({ status: 3 })); expect(res).toMatchObject({ state: 'merged', status: 3, }); }); it('should be formated (not closed)', () => { const res = getRenovatePRFormat(partial({ status: 1 })); expect(res).toMatchObject({ state: 'open', status: 1, }); }); }); describe('streamToString', () => { it('converts Readable stream to string', async () => { const res = await streamToString(Readable.from('foobar')); expect(res).toBe('foobar'); }); it('handles error', async () => { const stream = Readable.from('foobar'); const res = streamToString(stream); stream.destroy(new Error('some unknown error')); await expect(res).rejects.toThrow('some unknown error'); }); }); describe('getStorageExtraCloneOpts', () => { it('should configure basic auth', () => { const res = getStorageExtraCloneOpts({ username: 'user', password: 'pass', }); expect(res).toEqual({ '-c': 'http.extraHeader=AUTHORIZATION: basic dXNlcjpwYXNz', }); }); it('should configure personal access token', () => { const res = getStorageExtraCloneOpts({ token: '123456789012345678901234567890123456789012345678test', }); expect(res).toEqual({ '-c': 'http.extraHeader=AUTHORIZATION: basic OjEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3OHRlc3Q=', }); }); it('should configure non-JWT token as PAT', () => { const res = getStorageExtraCloneOpts({ token: 'token' }); expect(res['-c']).toContain('AUTHORIZATION: basic'); }); it('should use bearer when token is a JWT', () => { const token = buildTestJwt( { typ: 'JWT', alg: 'RS256' }, { aud: '499b84ac', sub: 'test', exp: 9999999999 }, 'fake-sig', ); const res = getStorageExtraCloneOpts({ token }); expect(res['-c']).toContain('AUTHORIZATION: bearer'); expect(res['-c']).toContain(token); }); it('should use basic for a 52-char PAT', () => { const token = '1234567890123456789012345678901234567890123456789012'; const res = getStorageExtraCloneOpts({ token }); expect(res['-c']).toContain('AUTHORIZATION: basic'); expect(res['-c']).not.toContain('bearer'); }); it('should use basic for a short opaque token', () => { const res = getStorageExtraCloneOpts({ token: 'short-token' }); expect(res['-c']).toContain('AUTHORIZATION: basic'); expect(res['-c']).not.toContain('bearer'); }); it('should use basic for an invalid JWT-like token', () => { const res = getStorageExtraCloneOpts({ token: 'not.valid.jwt' }); expect(res['-c']).toContain('AUTHORIZATION: basic'); expect(res['-c']).not.toContain('bearer'); }); }); describe('max4000Chars', () => { it('should be the same', () => { const res = max4000Chars('Hello'); expect(res).toBe('Hello'); }); it('should be truncated', () => { let str = ''; for (let i = 0; i < 5000; i += 1) { str += 'a'; } const res = max4000Chars(str); expect(res).toHaveLength(3999); }); }); describe('getProjectAndRepo', () => { it('should return the object with same strings', () => { const res = getProjectAndRepo('myRepoName'); expect(res).toEqual({ project: 'myRepoName', repo: 'myRepoName', }); }); it('should return the object with project and repo', () => { const res = getProjectAndRepo('prjName/myRepoName'); expect(res).toEqual({ project: 'prjName', repo: 'myRepoName', }); }); it('should return an error', () => { expect(() => getProjectAndRepo('prjName/myRepoName/blalba')).toThrow( Error( `Azure repository can be only structured this way : 'repository' or 'projectName/repository'!`, ), ); }); }); describe('getRepoByName', () => { it('returns null when repos array is empty', () => { expect(getRepoByName('foo/bar', [])).toBeNull(); expect(getRepoByName('foo/bar', undefined)).toBeNull(); expect(getRepoByName('foo/bar', null)).toBeNull(); }); it('returns null when repo is not found', () => { expect( getRepoByName('foo/foo', [{ name: 'bar', project: { name: 'bar' } }]), ).toBeNull(); }); it('finds repo', () => { expect( getRepoByName('foo/bar', [ { id: '1', name: 'baz', project: { name: 'qux' } }, null, undefined, { id: '2', name: 'bar' }, { id: '3', name: 'bar', project: { name: 'foo' } }, { id: '4', name: 'bar', project: { name: 'foo' } }, ]), ).toMatchObject({ id: '3' }); }); it('supports shorthand names', () => { expect( getRepoByName('foo', [ { id: '1', name: 'bar', project: { name: 'bar' } }, { id: '2', name: 'foo', project: { name: 'foo' } }, ]), ).toMatchObject({ id: '2' }); }); it('is case-independent', () => { const repos = [ { id: '1', name: 'FOO', project: { name: 'FOO' } }, { id: '2', name: 'foo', project: { name: 'foo' } }, ]; expect(getRepoByName('FOO/foo', repos)).toMatchObject({ id: '1' }); expect(getRepoByName('foo/FOO', repos)).toMatchObject({ id: '1' }); expect(getRepoByName('foo/foo', repos)).toMatchObject({ id: '1' }); }); it('throws when repo name is invalid', () => { // TODO: better error handling #22198 expect(() => getRepoByName(undefined as never, [])).toThrow( "Cannot read properties of undefined (reading 'split')", ); expect(() => getRepoByName(null as never, [])).toThrow( "Cannot read properties of null (reading 'split')", ); expect(() => getRepoByName('foo/bar/baz', [])).toThrow( 'Azure repository can be only structured this way', ); }); }); it('returns the raw title if not a dependency dashboard', () => { expect(getWorkItemTitle('Some Issue', 'project/repo')).toBe('Some Issue'); }); it('returns the raw title if dependency dashboard and already prefixed', () => { expect( getWorkItemTitle('[repo] Dependency Dashboard', 'project/repo'), ).toBe('[repo] Dependency Dashboard'); }); it('prefixes dependency dashboard title with repo name if not already prefixed', () => { expect(getWorkItemTitle('Dependency Dashboard', 'project/repo')).toBe( '[repo] Dependency Dashboard', ); }); it('handles repository names with slashes', () => { expect(getWorkItemTitle('Dependency Dashboard', 'org/project/repo')).toBe( '[repo] Dependency Dashboard', ); }); it('does not double prefix if repo name is already present', () => { expect( getWorkItemTitle('[repo] Dependency Dashboard', 'org/project/repo'), ).toBe('[repo] Dependency Dashboard'); }); it('handles titles with apostrophes', () => { expect(getWorkItemTitle("Dependency Dashboard's", 'project/repo')).toBe( "[repo] Dependency Dashboard's", ); }); });