index.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  2. import * as util from '../util'
  3. import * as fileSystemStore from './fileSystemStore'
  4. import { getFunctionsArtifactStore } from './index'
  5. vi.mock('../util', () => ({
  6. assertSelfHosted: vi.fn(),
  7. }))
  8. vi.mock('./fileSystemStore', () => ({
  9. FileSystemFunctionsArtifactStore: vi.fn(),
  10. }))
  11. describe('api/self-hosted/functions/index', () => {
  12. let originalEdgeFunctionsFolder: string | undefined
  13. beforeEach(() => {
  14. originalEdgeFunctionsFolder = process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER
  15. vi.resetAllMocks()
  16. })
  17. afterEach(() => {
  18. if (originalEdgeFunctionsFolder !== undefined) {
  19. process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER = originalEdgeFunctionsFolder
  20. } else {
  21. delete process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER
  22. }
  23. })
  24. describe('getFunctionsArtifactStore', () => {
  25. it('should call assertSelfHosted', () => {
  26. process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER = '/tmp/functions'
  27. getFunctionsArtifactStore()
  28. expect(util.assertSelfHosted).toHaveBeenCalled()
  29. })
  30. it('should throw error if EDGE_FUNCTIONS_MANAGEMENT_FOLDER is not set', () => {
  31. delete process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER
  32. expect(() => getFunctionsArtifactStore()).toThrow(
  33. 'EDGE_FUNCTIONS_MANAGEMENT_FOLDER is required'
  34. )
  35. })
  36. it('should create FileSystemFunctionsArtifactStore with correct path', () => {
  37. process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER = '/var/lib/functions'
  38. getFunctionsArtifactStore()
  39. expect(fileSystemStore.FileSystemFunctionsArtifactStore).toHaveBeenCalledWith(
  40. '/var/lib/functions'
  41. )
  42. })
  43. it('should return FileSystemFunctionsArtifactStore instance', () => {
  44. const mockInstance = {
  45. folderPath: '/tmp/test',
  46. getFunctions: vi.fn(),
  47. getFunctionBySlug: vi.fn(),
  48. getFileEntriesBySlug: vi.fn(),
  49. }
  50. vi.mocked(fileSystemStore.FileSystemFunctionsArtifactStore).mockImplementation(function () {
  51. return mockInstance as any
  52. })
  53. process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER = '/tmp/test'
  54. const result = getFunctionsArtifactStore()
  55. expect(result).toBe(mockInstance)
  56. })
  57. })
  58. })