|
| 1 | +import { describe, test, expect, beforeAll } from "bun:test"; |
| 2 | +import { RedisService } from "../../src/services/redisService"; |
| 3 | + |
| 4 | +describe("Redis Service – Integration Tests", () => { |
| 5 | + beforeAll(async () => { |
| 6 | + await RedisService.start(); |
| 7 | + await RedisService.op().del("messages"); |
| 8 | + }); |
| 9 | + |
| 10 | + test("Should connect and return a working Redis instance", async () => { |
| 11 | + const pong = await RedisService.op().ping(); |
| 12 | + expect(pong).toBe("PONG"); |
| 13 | + }); |
| 14 | + |
| 15 | + test("Should push a message into Redis list", async () => { |
| 16 | + const result = await RedisService.lpush("messages", "hello"); |
| 17 | + expect(typeof result).toBe("number"); |
| 18 | + expect(result).toBeGreaterThan(0); |
| 19 | + |
| 20 | + const stored = await RedisService.lrange("messages", 0, -1); |
| 21 | + expect(stored).toContain("hello"); |
| 22 | + }); |
| 23 | + |
| 24 | + test("Should push multiple messages and maintain order (LIFO)", async () => { |
| 25 | + await RedisService.op().del("messages"); |
| 26 | + |
| 27 | + await RedisService.lpush("messages", "A"); |
| 28 | + await RedisService.lpush("messages", "B"); |
| 29 | + await RedisService.lpush("messages", "C"); |
| 30 | + |
| 31 | + const stored = await RedisService.lrange("messages", 0, -1); |
| 32 | + expect(stored).toEqual(["C", "B", "A"]); |
| 33 | + }); |
| 34 | + |
| 35 | + test("Should return empty array when key does not exist", async () => { |
| 36 | + await RedisService.op().del("unknown"); |
| 37 | + |
| 38 | + const stored = await RedisService.lrange("unknown", 0, -1); |
| 39 | + expect(stored).toEqual([]); |
| 40 | + }); |
| 41 | + |
| 42 | + test("Should delete the list successfully", async () => { |
| 43 | + await RedisService.lpush("messages", "to_delete"); |
| 44 | + const del = await RedisService.op().del("messages"); |
| 45 | + |
| 46 | + expect(del).toBe(1); |
| 47 | + |
| 48 | + const after = await RedisService.lrange("messages", 0, -1); |
| 49 | + expect(after).toEqual([]); |
| 50 | + }); |
| 51 | + |
| 52 | + test("Should handle empty string pushes", async () => { |
| 53 | + await RedisService.op().del("messages"); |
| 54 | + |
| 55 | + await RedisService.lpush("messages", ""); |
| 56 | + const stored = await RedisService.lrange("messages", 0, -1); |
| 57 | + |
| 58 | + expect(stored).toEqual([""]); |
| 59 | + }); |
| 60 | + |
| 61 | + test("Should throw an error when pushing without Redis started", async () => { |
| 62 | + await RedisService.stop(); |
| 63 | + |
| 64 | + try { |
| 65 | + await RedisService.lpush("messages", "fail"); |
| 66 | + } catch (err) { |
| 67 | + expect((err as Error).message.toLowerCase()).toContain( |
| 68 | + "the client is closed", |
| 69 | + ); |
| 70 | + } finally { |
| 71 | + await RedisService.start(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + test("Should allow multiple concurrent pushes without losing data", async () => { |
| 76 | + await RedisService.op().del("messages"); |
| 77 | + |
| 78 | + const messages = Array.from({ length: 50 }, (_, i) => `msg_${i}`); |
| 79 | + |
| 80 | + await Promise.all(messages.map((m) => RedisService.lpush("messages", m))); |
| 81 | + |
| 82 | + const stored = await RedisService.lrange("messages", 0, -1); |
| 83 | + |
| 84 | + messages.forEach((m) => { |
| 85 | + expect(stored).toContain(m); |
| 86 | + }); |
| 87 | + expect(stored.length).toBe(50); |
| 88 | + }); |
| 89 | +}); |
0 commit comments