|
| 1 | +import fs from 'fs'; |
| 2 | +import { FileFetch } from './file-fetch'; |
| 3 | +import { FlagdCore } from '@openfeature/flagd-core'; |
| 4 | +import { Logger } from '@openfeature/core'; |
| 5 | + |
| 6 | +jest.mock('fs', () => ({ |
| 7 | + ...jest.requireActual('fs'), |
| 8 | + promises: { |
| 9 | + readFile: jest.fn(), |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +const dataFillCallbackMock = jest.fn(); |
| 14 | +const reconnectCallbackMock = jest.fn(); |
| 15 | +const changedCallbackMock = jest.fn(); |
| 16 | +const loggerMock: Logger = { |
| 17 | + debug: jest.fn(), |
| 18 | + info: jest.fn(), |
| 19 | + warn: jest.fn(), |
| 20 | + error: jest.fn(), |
| 21 | +}; |
| 22 | + |
| 23 | +describe('FileFetch', () => { |
| 24 | + let flagdCore: FlagdCore; |
| 25 | + let fileFetch: FileFetch; |
| 26 | + let dataFillCallback: (flags: string) => string[]; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + flagdCore = new FlagdCore(); |
| 30 | + fileFetch = new FileFetch('./flags.json', loggerMock); |
| 31 | + dataFillCallback = (flags: string) => { |
| 32 | + return flagdCore.setConfigurations(flags); |
| 33 | + }; |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + jest.resetAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should connect to the file and setup the watcher', async () => { |
| 41 | + const flags = '{"flags":{"flag":{"state":"ENABLED","variants":{"on":true,"off":false},"defaultVariant":"off"}}}'; |
| 42 | + mockReadFile(flags); |
| 43 | + const watchMock = jest.fn(); |
| 44 | + |
| 45 | + fs.watchFile = watchMock as jest.MockedFunction<typeof fs.watchFile>; |
| 46 | + |
| 47 | + await fileFetch.connect(dataFillCallbackMock, reconnectCallbackMock, changedCallbackMock); |
| 48 | + |
| 49 | + expect(dataFillCallbackMock).toHaveBeenCalledWith(flags); |
| 50 | + expect(watchMock).toHaveBeenCalledWith('./flags.json', expect.any(Function)); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should throw because of invalid json', async () => { |
| 54 | + const flags = 'this is not JSON'; |
| 55 | + mockReadFile(flags); |
| 56 | + const watchSpy = jest.spyOn(fs, 'watchFile'); |
| 57 | + |
| 58 | + await expect(fileFetch.connect(dataFillCallback, reconnectCallbackMock, changedCallbackMock)).rejects.toThrow(); |
| 59 | + expect(watchSpy).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should throw an error if the file is not found', async () => { |
| 63 | + const mockReadFile = fs.promises.readFile as jest.MockedFunction<typeof fs.promises.readFile>; |
| 64 | + mockReadFile.mockRejectedValue({ code: 'ENOENT' }); |
| 65 | + |
| 66 | + await expect(fileFetch.connect(dataFillCallbackMock, reconnectCallbackMock, changedCallbackMock)).rejects.toThrow( |
| 67 | + 'File not found: ./flags.json', |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should throw an error if the file is not accessible', async () => { |
| 72 | + const mockReadFile = fs.promises.readFile as jest.MockedFunction<typeof fs.promises.readFile>; |
| 73 | + mockReadFile.mockRejectedValue({ code: 'EACCES' }); |
| 74 | + |
| 75 | + await expect(fileFetch.connect(dataFillCallbackMock, reconnectCallbackMock, changedCallbackMock)).rejects.toThrow( |
| 76 | + 'File not accessible: ./flags.json', |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should close the watcher on disconnect', async () => { |
| 81 | + const watchSpy = jest.spyOn(fs, 'watchFile'); |
| 82 | + const unwatchSpy = jest.spyOn(fs, 'unwatchFile'); |
| 83 | + |
| 84 | + await fileFetch.connect(dataFillCallbackMock, reconnectCallbackMock, changedCallbackMock); |
| 85 | + await fileFetch.disconnect(); |
| 86 | + |
| 87 | + expect(watchSpy).toHaveBeenCalled(); |
| 88 | + expect(unwatchSpy).toHaveBeenCalledWith('./flags.json'); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('on file change', () => { |
| 92 | + it('should call changedCallback with the changed flags', async () => { |
| 93 | + const flags = '{"flags":{"flag":{"state":"ENABLED","variants":{"on":true,"off":false},"defaultVariant":"off"}}}'; |
| 94 | + const changedFlags = |
| 95 | + '{"flags":{"flag":{"state":"ENABLED","variants":{"on":true,"off":false},"defaultVariant":"on"}}}'; |
| 96 | + const mockReadFile = fs.promises.readFile as jest.MockedFunction<typeof fs.promises.readFile>; |
| 97 | + mockReadFile.mockResolvedValueOnce(flags); |
| 98 | + const watchMock = jest.fn(); |
| 99 | + fs.watchFile = watchMock as jest.MockedFunction<typeof fs.watchFile>; |
| 100 | + |
| 101 | + await fileFetch.connect(dataFillCallback, reconnectCallbackMock, changedCallbackMock); |
| 102 | + mockReadFile.mockResolvedValueOnce(changedFlags); |
| 103 | + // Manually call the callback that is passed to fs.watchFile; |
| 104 | + await watchMock.mock.calls[0][1](); |
| 105 | + |
| 106 | + expect(changedCallbackMock).toHaveBeenCalledWith(['flag']); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should call skip changedCallback because no flag has changed', async () => { |
| 110 | + const flags = '{"flags":{"flag":{"state":"ENABLED","variants":{"on":true,"off":false},"defaultVariant":"off"}}}'; |
| 111 | + const mockReadFile = fs.promises.readFile as jest.MockedFunction<typeof fs.promises.readFile>; |
| 112 | + mockReadFile.mockResolvedValue(flags); |
| 113 | + const watchMock = jest.fn(); |
| 114 | + fs.watchFile = watchMock as jest.MockedFunction<typeof fs.watchFile>; |
| 115 | + |
| 116 | + await fileFetch.connect(dataFillCallback, reconnectCallbackMock, changedCallbackMock); |
| 117 | + // Manually call the callback that is passed to fs.watchFile; |
| 118 | + await watchMock.mock.calls[0][1](); |
| 119 | + |
| 120 | + expect(changedCallbackMock).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should log an error if the file could not be read', async () => { |
| 124 | + const flags = '{"flags":{"flag":{"state":"ENABLED","variants":{"on":true,"off":false},"defaultVariant":"off"}}}'; |
| 125 | + const mockReadFile = fs.promises.readFile as jest.MockedFunction<typeof fs.promises.readFile>; |
| 126 | + mockReadFile.mockResolvedValue(flags); |
| 127 | + const watchMock = jest.fn(); |
| 128 | + fs.watchFile = watchMock as jest.MockedFunction<typeof fs.watchFile>; |
| 129 | + |
| 130 | + await fileFetch.connect(dataFillCallback, reconnectCallbackMock, changedCallbackMock); |
| 131 | + mockReadFile.mockRejectedValueOnce(new Error('Error reading file')); |
| 132 | + // Manually call the callback that is passed to fs.watchFile; |
| 133 | + await watchMock.mock.calls[0][1](); |
| 134 | + |
| 135 | + expect(changedCallbackMock).not.toHaveBeenCalled(); |
| 136 | + expect(loggerMock.error).toHaveBeenCalled(); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +// Helper function to mock fs.promise.readFile |
| 142 | +function mockReadFile(flags: string): void { |
| 143 | + const mockReadFile = fs.promises.readFile as jest.MockedFunction<typeof fs.promises.readFile>; |
| 144 | + mockReadFile.mockResolvedValue(flags); |
| 145 | +} |
0 commit comments