Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

2.7 KB
Raw Blame
import { describe, expect, it } from "vitest";

import { computeDisplayTtl, computeTtlCountdownTick, computeTtlCountdownValue, computeTtlForExpiryEdit, DEFAULT_REDIS_AUTO_REFRESH_INTERVAL_SECONDS, normalizeRedisAutoRefreshInterval } from "@/lib/redis/redisAutoRefresh";

describe("computeTtlCountdownTick", () => {
  it("returns decrement while a positive TTL remains", () => {
    expect(computeTtlCountdownTick(10)).toEqual({ type: "decrement" });
    expect(computeTtlCountdownTick(1)).toEqual({ type: "decrement" });
  });

  it("stops decrementing after the TTL has reached zero", () => {
    expect(computeTtlCountdownTick(0)).toEqual({ type: "idle" });
    expect(computeTtlCountdownTick(-1)).toEqual({ type: "idle" });
  });
});

describe("computeTtlCountdownValue", () => {
  it("accounts for time elapsed while countdown timers are paused", () => {
    expect(computeTtlCountdownValue(60, 10_000, 40_000)).toBe(30);
  });

  it("preserves Redis sentinel values and clamps expired TTLs", () => {
    expect(computeTtlCountdownValue(5, 10_000, 20_000)).toBe(0);
    expect(computeTtlCountdownValue(-1, 10_000, 20_000)).toBe(-1);
    expect(computeTtlCountdownValue(-2, 10_000, 20_000)).toBe(-2);
  });
});

describe("normalizeRedisAutoRefreshInterval", () => {
  it("uses the default for invalid values and clamps arbitrary input to safe seconds", () => {
    expect(normalizeRedisAutoRefreshInterval("invalid")).toBe(DEFAULT_REDIS_AUTO_REFRESH_INTERVAL_SECONDS);
    expect(normalizeRedisAutoRefreshInterval(0)).toBe(1);
    expect(normalizeRedisAutoRefreshInterval(1.9)).toBe(1);
    expect(normalizeRedisAutoRefreshInterval(9_999)).toBe(3600);
  });
});

describe("computeDisplayTtl", () => {
  it("returns the live countdown regardless of automatic network refresh", () => {
    expect(computeDisplayTtl(3, 10)).toBe(3);
  });

  it("does not flash back to the stale server TTL at zero", () => {
    expect(computeDisplayTtl(0, 5)).toBe(0);
  });

  it("returns live countdown while counting", () => {
    expect(computeDisplayTtl(5, 10)).toBe(5);
    expect(computeDisplayTtl(1, 10)).toBe(1);
  });

  it("clamps an active countdown below zero instead of showing stale data", () => {
    expect(computeDisplayTtl(-1, 10)).toBe(0);
  });
});

describe("computeTtlForExpiryEdit", () => {
  it("keeps a last-confirmed positive TTL during the in-flight zero-countdown window", () => {
    expect(computeTtlForExpiryEdit(0, 5)).toBe(5);
    expect(computeTtlForExpiryEdit(-1, 5)).toBe(5);
  });

  it("uses the live positive countdown and preserves non-expiring server states", () => {
    expect(computeTtlForExpiryEdit(3, 10)).toBe(3);
    expect(computeTtlForExpiryEdit(0, -1)).toBe(-1);
    expect(computeTtlForExpiryEdit(0, -2)).toBe(-2);
  });
});