github-api-helper.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {jest, describe, it, expect, beforeEach, afterEach} from '@jest/globals'
  2. // Mock @actions/core
  3. const mockDebug = jest.fn()
  4. jest.unstable_mockModule('@actions/core', () => ({
  5. debug: mockDebug,
  6. info: jest.fn(),
  7. warning: jest.fn(),
  8. error: jest.fn()
  9. }))
  10. // Mock @actions/github
  11. const mockGetOctokit = jest.fn()
  12. jest.unstable_mockModule('@actions/github', () => ({
  13. getOctokit: mockGetOctokit
  14. }))
  15. // Dynamic imports after mocking
  16. const githubApiHelper = await import('../src/github-api-helper.js')
  17. describe('github-api-helper object format', () => {
  18. let request: jest.Mock<any>
  19. function mockHashAlgorithmApi(hashAlgorithm: string): void {
  20. request = jest.fn(async () => ({
  21. data: {
  22. hash_algorithm: hashAlgorithm
  23. }
  24. }))
  25. mockGetOctokit.mockReturnValue({
  26. request
  27. } as any)
  28. }
  29. beforeEach(() => {
  30. mockDebug.mockClear()
  31. mockGetOctokit.mockClear()
  32. })
  33. afterEach(() => {
  34. jest.clearAllMocks()
  35. })
  36. it('detects SHA-256 from the repository hash algorithm endpoint', async () => {
  37. mockHashAlgorithmApi('sha256')
  38. await expect(
  39. githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo')
  40. ).resolves.toEqual({format: 'sha256', succeeded: true})
  41. expect(mockGetOctokit).toHaveBeenCalledWith(
  42. 'token',
  43. expect.objectContaining({baseUrl: 'https://api.github.com'})
  44. )
  45. expect(request).toHaveBeenCalledWith(
  46. 'GET /repos/{owner}/{repo}/hash-algorithm',
  47. {owner: 'owner', repo: 'repo'}
  48. )
  49. })
  50. it('detects SHA-1 from the repository hash algorithm endpoint', async () => {
  51. mockHashAlgorithmApi('sha1')
  52. await expect(
  53. githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo')
  54. ).resolves.toEqual({format: 'sha1', succeeded: true})
  55. })
  56. it('detects object format from an existing commit without API calls', async () => {
  57. const commitSha =
  58. '9422233ca7ee1b17f1e905d0e141faf0c401556c41cdc6acd71c6bd685da2e92'
  59. await expect(
  60. githubApiHelper.tryGetRepositoryObjectFormat(
  61. 'token',
  62. 'owner',
  63. 'repo',
  64. undefined,
  65. commitSha
  66. )
  67. ).resolves.toEqual({format: 'sha256', succeeded: true})
  68. expect(mockGetOctokit).not.toHaveBeenCalled()
  69. })
  70. it('returns unsuccessful when the hash algorithm endpoint value is not recognized', async () => {
  71. mockHashAlgorithmApi('unknown')
  72. await expect(
  73. githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo')
  74. ).resolves.toEqual({format: '', succeeded: false})
  75. expect(mockDebug).toHaveBeenCalledWith(
  76. 'Unable to determine repository object format from hash-algorithm endpoint'
  77. )
  78. })
  79. it('returns unsuccessful when the hash algorithm API lookup fails', async () => {
  80. request = jest.fn(async () => {
  81. throw new Error('not found')
  82. })
  83. mockGetOctokit.mockReturnValue({
  84. request
  85. } as any)
  86. await expect(
  87. githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo')
  88. ).resolves.toEqual({format: '', succeeded: false})
  89. expect(mockDebug).toHaveBeenCalledWith(
  90. 'Unable to determine repository object format from hash-algorithm endpoint: not found'
  91. )
  92. })
  93. })