|
| 1 | +import { keygen, newHttpClient, randomID } from "../test-utils"; |
| 2 | + |
| 3 | +import { afterAll, describe, expect, test } from "bun:test"; |
| 4 | +import { GetExCommand } from "./getex"; |
| 5 | +import { GetCommand } from "./get"; |
| 6 | +import { SetCommand } from "./set"; |
| 7 | + |
| 8 | +const client = newHttpClient(); |
| 9 | + |
| 10 | +const { newKey, cleanup } = keygen(); |
| 11 | +afterAll(cleanup); |
| 12 | + |
| 13 | +describe("without options", () => { |
| 14 | + test("gets value", async () => { |
| 15 | + const key = newKey(); |
| 16 | + const value = randomID(); |
| 17 | + |
| 18 | + const res = await new SetCommand([key, value]).exec(client); |
| 19 | + expect(res).toEqual("OK"); |
| 20 | + const res2 = await new GetExCommand([key]).exec(client); |
| 21 | + expect(res2).toEqual(value); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe("ex", () => { |
| 26 | + test("gets value and sets expiry in seconds", async () => { |
| 27 | + const key = newKey(); |
| 28 | + const value = randomID(); |
| 29 | + |
| 30 | + const res = await new SetCommand([key, value]).exec(client); |
| 31 | + expect(res).toEqual("OK"); |
| 32 | + const res2 = await new GetExCommand([key, { ex: 1 }]).exec(client); |
| 33 | + expect(res2).toEqual(value); |
| 34 | + await new Promise((res) => setTimeout(res, 2000)); |
| 35 | + |
| 36 | + const res3 = await new GetCommand([key]).exec(client); |
| 37 | + expect(res3).toEqual(null); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("px", () => { |
| 42 | + test("gets value and sets expiry in milliseconds", async () => { |
| 43 | + const key = newKey(); |
| 44 | + const value = randomID(); |
| 45 | + |
| 46 | + const res = await new SetCommand([key, value]).exec(client); |
| 47 | + expect(res).toEqual("OK"); |
| 48 | + const res2 = await new GetExCommand([key, { px: 1000 }]).exec(client); |
| 49 | + expect(res2).toEqual(value); |
| 50 | + await new Promise((res) => setTimeout(res, 2000)); |
| 51 | + |
| 52 | + const res3 = await new GetCommand([key]).exec(client); |
| 53 | + |
| 54 | + expect(res3).toEqual(null); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe("exat", () => { |
| 59 | + test("gets value and sets expiry in Unix time (seconds)", async () => { |
| 60 | + const key = newKey(); |
| 61 | + const value = randomID(); |
| 62 | + |
| 63 | + const res = await new SetCommand([key, value]).exec(client); |
| 64 | + expect(res).toEqual("OK"); |
| 65 | + const res2 = await new GetExCommand([ |
| 66 | + key, |
| 67 | + { |
| 68 | + exat: Math.floor(Date.now() / 1000) + 2, |
| 69 | + }, |
| 70 | + ]).exec(client); |
| 71 | + expect(res2).toEqual(value); |
| 72 | + await new Promise((res) => setTimeout(res, 3000)); |
| 73 | + |
| 74 | + const res3 = await new GetCommand([key]).exec(client); |
| 75 | + |
| 76 | + expect(res3).toEqual(null); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("pxat", () => { |
| 81 | + test("gets value and sets expiry in Unix time (milliseconds)", async () => { |
| 82 | + const key = newKey(); |
| 83 | + const value = randomID(); |
| 84 | + |
| 85 | + const res = await new SetCommand([key, value]).exec(client); |
| 86 | + expect(res).toEqual("OK"); |
| 87 | + const res2 = await new GetExCommand([key, { pxat: Date.now() + 2000 }]).exec(client); |
| 88 | + expect(res2).toEqual(value); |
| 89 | + await new Promise((res) => setTimeout(res, 3000)); |
| 90 | + |
| 91 | + const res3 = await new GetCommand([key]).exec(client); |
| 92 | + |
| 93 | + expect(res3).toEqual(null); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("persist", () => { |
| 98 | + test("gets value and removes expiry", async () => { |
| 99 | + const key = newKey(); |
| 100 | + const value = randomID(); |
| 101 | + |
| 102 | + const res = await new SetCommand([key, value, { ex: 1 }]).exec(client); |
| 103 | + expect(res).toEqual("OK"); |
| 104 | + const res2 = await new GetExCommand([key, { persist: true }]).exec(client); |
| 105 | + expect(res2).toEqual(value); |
| 106 | + await new Promise((res) => setTimeout(res, 2000)); |
| 107 | + |
| 108 | + const res3 = await new GetCommand([key]).exec(client); |
| 109 | + |
| 110 | + expect(res3).toEqual(value); |
| 111 | + }); |
| 112 | +}); |
0 commit comments