"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const blue_tape_1 = __importDefault(require("blue-tape"));
const express_session_1 = require("express-session");
const ioredis_1 = require("ioredis");
const node_util_1 = require("node:util");
const redis_1 = require("redis");
const _1 = __importDefault(require("./"));
const redisSrv = __importStar(require("./testdata/server"));
(0, blue_tape_1.default)("setup", async () => {
    await redisSrv.connect();
});
(0, blue_tape_1.default)("defaults", async (t) => {
    let client = (0, redis_1.createClient)({ url: `redis://localhost:${redisSrv.port}` });
    await client.connect();
    let store = new _1.default({ client });
    t.ok(store.client, "stores client");
    t.equal(store.prefix, "sess:", "defaults to sess:");
    t.equal(store.ttl, 86400, "defaults to one day");
    t.equal(store.scanCount, 100, "defaults SCAN count to 100");
    t.equal(store.serializer, JSON, "defaults to JSON serialization");
    t.equal(store.disableTouch, false, "defaults to having `touch` enabled");
    t.equal(store.disableTTL, false, "defaults to having `ttl` enabled");
    await client.disconnect();
});
(0, blue_tape_1.default)("redis", async (t) => {
    let client = (0, redis_1.createClient)({ url: `redis://localhost:${redisSrv.port}` });
    await client.connect();
    let store = new _1.default({ client });
    await lifecycleTest(store, client, t);
    await client.disconnect();
});
(0, blue_tape_1.default)("ioredis", async (t) => {
    let client = new ioredis_1.Redis(`redis://localhost:${redisSrv.port}`);
    let store = new _1.default({ client });
    await lifecycleTest(store, client, t);
    client.disconnect();
});
(0, blue_tape_1.default)("teardown", redisSrv.disconnect);
async function lifecycleTest(store, client, t) {
    const P = (f) => (0, node_util_1.promisify)(f).bind(store);
    let res = await P(store.clear)();
    let sess = { foo: "bar" };
    await P(store.set)("123", sess);
    res = await P(store.get)("123");
    t.same(res, sess, "store.get");
    let ttl = await client.ttl("sess:123");
    t.ok(ttl >= 86399, "check one day ttl");
    ttl = 60;
    let expires = new Date(Date.now() + ttl * 1000).toISOString();
    await P(store.set)("456", { cookie: { expires } });
    ttl = await client.ttl("sess:456");
    t.ok(ttl <= 60, "check expires ttl");
    ttl = 90;
    let expires2 = new Date(Date.now() + ttl * 1000).toISOString();
    await P(store.touch)("456", { cookie: { expires: expires2 } });
    ttl = await client.ttl("sess:456");
    t.ok(ttl > 60, "check expires ttl touch");
    res = await P(store.length)();
    t.equal(res, 2, "stored two keys length");
    res = await P(store.ids)();
    res.sort();
    t.same(res, ["123", "456"], "stored two keys ids");
    res = await P(store.all)();
    res.sort((a, b) => (a.id > b.id ? 1 : -1));
    t.same(res, [
        { id: "123", foo: "bar" },
        { id: "456", cookie: { expires } },
    ], "stored two keys data");
    await P(store.destroy)("456");
    res = await P(store.length)();
    t.equal(res, 1, "one key remains");
    res = await P(store.clear)();
    res = await P(store.length)();
    t.equal(res, 0, "no keys remain");
    let count = 1000;
    await load(store, count);
    res = await P(store.length)();
    t.equal(res, count, "bulk count");
    await P(store.clear)();
    res = await P(store.length)();
    t.equal(res, 0, "bulk clear");
    expires = new Date(Date.now() + ttl * 1000).toISOString(); // expires in the future
    res = await P(store.set)("789", { cookie: { expires } });
    res = await P(store.length)();
    t.equal(res, 1, "one key exists (session 789)");
    expires = new Date(Date.now() - ttl * 1000).toISOString(); // expires in the past
    await P(store.set)("789", { cookie: { expires } });
    res = await P(store.length)();
    t.equal(res, 0, "no key remains and that includes session 789");
}
async function load(store, count) {
    let cookie = new express_session_1.Cookie();
    for (let sid = 0; sid < count; sid++) {
        cookie.expires = new Date(Date.now() + 1000);
        await store.set("s" + sid, { cookie });
    }
}
