scan-helper.test.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { describe, expect, it, vi } from "vitest";
  2. import type Redis from "ioredis";
  3. import { scanPattern } from "@/lib/redis/scan-helper";
  4. describe("scanPattern", () => {
  5. it("should collect all keys from multiple scan iterations", async () => {
  6. const mockRedis = {
  7. scan: vi
  8. .fn()
  9. .mockResolvedValueOnce(["5", ["key:1", "key:2"]])
  10. .mockResolvedValueOnce(["0", ["key:3"]]),
  11. } as unknown as Redis;
  12. const result = await scanPattern(mockRedis, "key:*");
  13. expect(result).toEqual(["key:1", "key:2", "key:3"]);
  14. expect(mockRedis.scan).toHaveBeenCalledTimes(2);
  15. });
  16. it("should handle empty result", async () => {
  17. const mockRedis = {
  18. scan: vi.fn().mockResolvedValueOnce(["0", []]),
  19. } as unknown as Redis;
  20. const result = await scanPattern(mockRedis, "nonexistent:*");
  21. expect(result).toEqual([]);
  22. });
  23. it("should use custom count parameter", async () => {
  24. const mockRedis = {
  25. scan: vi.fn().mockResolvedValueOnce(["0", ["key:1"]]),
  26. } as unknown as Redis;
  27. await scanPattern(mockRedis, "key:*", 500);
  28. expect(mockRedis.scan).toHaveBeenCalledWith("0", "MATCH", "key:*", "COUNT", 500);
  29. });
  30. });