|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const setMock = vi.fn(); |
| 4 | +const getMock = vi.fn(); |
| 5 | +const delMock = vi.fn(); |
| 6 | +const connectMock = vi.fn(); |
| 7 | +const onMock = vi.fn(); |
| 8 | + |
| 9 | +const createClientMock = vi.fn(() => ({ |
| 10 | + connect: connectMock, |
| 11 | + del: delMock, |
| 12 | + get: getMock, |
| 13 | + on: onMock, |
| 14 | + set: setMock |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("redis", () => ({ |
| 18 | + createClient: createClientMock |
| 19 | +})); |
| 20 | + |
| 21 | +const infoMock = vi.fn(); |
| 22 | +const errorMock = vi.fn(); |
| 23 | +const warnMock = vi.fn(); |
| 24 | +const debugMock = vi.fn(); |
| 25 | + |
| 26 | +vi.mock("@hey/helpers/logger", () => ({ |
| 27 | + default: { |
| 28 | + debug: debugMock, |
| 29 | + error: errorMock, |
| 30 | + info: infoMock, |
| 31 | + warn: warnMock |
| 32 | + } |
| 33 | +})); |
| 34 | + |
| 35 | +afterEach(() => { |
| 36 | + vi.clearAllMocks(); |
| 37 | + vi.restoreAllMocks(); |
| 38 | + vi.resetModules(); |
| 39 | + delete process.env.REDIS_URL; |
| 40 | +}); |
| 41 | + |
| 42 | +describe("hoursToSeconds", () => { |
| 43 | + it("converts hours to seconds", async () => { |
| 44 | + const { hoursToSeconds } = await import("./redis"); |
| 45 | + expect(hoursToSeconds(2)).toBe(7200); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("generateSmallExpiry", () => { |
| 50 | + it("returns value between 1 and 2 days", async () => { |
| 51 | + vi.spyOn(Math, "random").mockReturnValue(0.5); |
| 52 | + const { generateSmallExpiry } = await import("./redis"); |
| 53 | + expect(generateSmallExpiry()).toBe(129600); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe("generateExtraLongExpiry", () => { |
| 58 | + it("returns value between 8 and 10 days", async () => { |
| 59 | + vi.spyOn(Math, "random").mockReturnValue(0.5); |
| 60 | + const { generateExtraLongExpiry } = await import("./redis"); |
| 61 | + expect(generateExtraLongExpiry()).toBe(777600); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe("Redis functions without client", () => { |
| 66 | + it("logs fallback when no client is available", async () => { |
| 67 | + const { setRedis, getRedis, delRedis } = await import("./redis"); |
| 68 | + |
| 69 | + await setRedis("k", "v"); |
| 70 | + await getRedis("k"); |
| 71 | + await delRedis("k"); |
| 72 | + |
| 73 | + const calls = infoMock.mock.calls.filter( |
| 74 | + (call) => call[0] === "[Redis] No Redis client, using fallback" |
| 75 | + ); |
| 76 | + expect(calls).toHaveLength(3); |
| 77 | + expect(setMock).not.toHaveBeenCalled(); |
| 78 | + expect(getMock).not.toHaveBeenCalled(); |
| 79 | + expect(delMock).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe("Redis functions with client", () => { |
| 84 | + it("passes parameters to redis client", async () => { |
| 85 | + process.env.REDIS_URL = "redis://localhost"; |
| 86 | + const module = await import("./redis"); |
| 87 | + const { setRedis, getRedis, delRedis } = module; |
| 88 | + |
| 89 | + setMock.mockResolvedValue("OK"); |
| 90 | + const value = { a: 1 }; |
| 91 | + const resultSet = await setRedis("key", value, 60); |
| 92 | + expect(resultSet).toBe("OK"); |
| 93 | + expect(setMock).toHaveBeenCalledWith("key", JSON.stringify(value), { |
| 94 | + EX: 60 |
| 95 | + }); |
| 96 | + |
| 97 | + getMock.mockResolvedValue("VAL"); |
| 98 | + const resultGet = await getRedis("key"); |
| 99 | + expect(resultGet).toBe("VAL"); |
| 100 | + expect(getMock).toHaveBeenCalledWith("key"); |
| 101 | + |
| 102 | + delMock.mockResolvedValue(1); |
| 103 | + const resultDel = await delRedis("key"); |
| 104 | + expect(resultDel).toBe(1); |
| 105 | + expect(delMock).toHaveBeenCalledWith("key"); |
| 106 | + |
| 107 | + const calls = infoMock.mock.calls.filter( |
| 108 | + (call) => call[0] === "[Redis] No Redis client, using fallback" |
| 109 | + ); |
| 110 | + expect(calls).toHaveLength(0); |
| 111 | + }); |
| 112 | +}); |
0 commit comments