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