|
| 1 | +import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +const PREV_MOCK = process.env.MOCK_REDIS; |
| 4 | + |
| 5 | +beforeAll(() => { |
| 6 | + process.env.MOCK_REDIS = "1"; |
| 7 | +}); |
| 8 | + |
| 9 | +afterAll(() => { |
| 10 | + if (PREV_MOCK === undefined) { |
| 11 | + delete process.env.MOCK_REDIS; |
| 12 | + } else { |
| 13 | + process.env.MOCK_REDIS = PREV_MOCK; |
| 14 | + } |
| 15 | +}); |
| 16 | + |
| 17 | +describe("MemoryRedisAdapter (MOCK_REDIS=1)", () => { |
| 18 | + test("set/get round-trip and respects expire/ex/lpush/ltrim", async () => { |
| 19 | + // intentionally no top-level timeout override; harness default is generous |
| 20 | + const { createRedis } = await import("../redis"); |
| 21 | + const redis = createRedis(); |
| 22 | + |
| 23 | + // Basic set/get |
| 24 | + await redis.set("hello", "world"); |
| 25 | + expect(await redis.get<string>("hello")).toBe("world"); |
| 26 | + |
| 27 | + // set with ex |
| 28 | + await redis.set("temp", "ttl-value", { ex: 60 }); |
| 29 | + expect(await redis.get<string>("temp")).toBe("ttl-value"); |
| 30 | + |
| 31 | + // set with nx — second call must not overwrite |
| 32 | + await redis.set("once", "first", { nx: true }); |
| 33 | + await redis.set("once", "second", { nx: true }); |
| 34 | + expect(await redis.get<string>("once")).toBe("first"); |
| 35 | + |
| 36 | + // lpush + ltrim |
| 37 | + await redis.lpush("list", "a"); |
| 38 | + await redis.lpush("list", "b"); |
| 39 | + await redis.lpush("list", "c"); |
| 40 | + // Keep only the head element |
| 41 | + await redis.ltrim("list", 0, 0); |
| 42 | + |
| 43 | + // expire returns 1 when key exists |
| 44 | + const expired = await redis.expire("hello", 30); |
| 45 | + expect(Number(expired)).toBe(1); |
| 46 | + |
| 47 | + // get returns null for missing key |
| 48 | + expect(await redis.get("nope")).toBeNull(); |
| 49 | + |
| 50 | + if (redis.quit) await redis.quit(); |
| 51 | + }); |
| 52 | +}); |
0 commit comments