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。

1.5 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { EXECUTION_CONFIRM_CODE_LENGTH, createExecutionConfirmCode, matchesExecutionConfirmCode } from "@/lib/query/executionConfirmCode";

describe("executionConfirmCode", () => {
  it("generates a code of the documented length from an unambiguous alphabet", () => {
    for (let run = 0; run < 50; run += 1) {
      const code = createExecutionConfirmCode();
      expect(code).toHaveLength(EXECUTION_CONFIRM_CODE_LENGTH);
      // No 0/O/1/I look-alikes: the operator has to retype it from the screen.
      expect(code).toMatch(/^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]+$/);
    }
  });

  it("is deterministic for a given random source", () => {
    const sequence = () => {
      const values = [0, 0.5, 0.999, 0.25, 0.75, 0.1];
      let index = 0;
      return () => values[index++ % values.length]!;
    };
    expect(createExecutionConfirmCode(sequence())).toBe(createExecutionConfirmCode(sequence()));
    expect(createExecutionConfirmCode(() => 0)).toBe("AAAAAA");
  });

  it("tolerates case and surrounding spaces when matching", () => {
    expect(matchesExecutionConfirmCode("ab12cd", "AB12CD")).toBe(true);
    expect(matchesExecutionConfirmCode("  AB12CD ", "ab12cd")).toBe(true);
    expect(matchesExecutionConfirmCode("", "AB12CD")).toBe(false);
    expect(matchesExecutionConfirmCode("AB12CE", "AB12CD")).toBe(false);
  });

  it("never matches an empty code", () => {
    expect(matchesExecutionConfirmCode("", "")).toBe(false);
    expect(matchesExecutionConfirmCode("anything", "")).toBe(false);
  });
});