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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createPinia, setActivePinia } from "pinia";
import { useReadOnlyUnlockStore } from "@/stores/readOnlyUnlockStore";
import { connectionIsEffectivelyReadOnly, ensureReadOnlyWriteAccess, formatWriteUnlockRemaining, sqlLooksLikeMutation } from "../readOnlyWriteAccess";
import zhCN from "@/i18n/locales/zh-CN";
const { toast } = vi.hoisted(() => ({ toast: vi.fn() }));
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast }) }));
vi.mock("@/i18n", () => ({ default: { global: { t: (key: string) => (key === "connection.mysqlDataCharsetWriteBlocked" ? zhCN.connection.mysqlDataCharsetWriteBlocked : key) } } }));
const unlockConnectionWrites = vi.fn();
const lockConnectionWrites = vi.fn();
const connectionWriteUnlockState = vi.fn();
vi.mock("@/lib/backend/api", () => ({
unlockConnectionWrites: (...args: unknown[]) => unlockConnectionWrites(...args),
lockConnectionWrites: (...args: unknown[]) => lockConnectionWrites(...args),
connectionWriteUnlockState: (...args: unknown[]) => connectionWriteUnlockState(...args),
}));
async function waitForPending(connectionId: string) {
const store = useReadOnlyUnlockStore();
await vi.waitFor(() => {
expect(store.pending?.connectionId).toBe(connectionId);
});
return store;
}
describe("read-only write unlock", () => {
beforeEach(() => {
setActivePinia(createPinia());
unlockConnectionWrites.mockReset().mockResolvedValue(60_000);
lockConnectionWrites.mockReset().mockResolvedValue(undefined);
connectionWriteUnlockState.mockReset().mockResolvedValue(0);
vi.useRealTimers();
toast.mockClear();
});
it("formats remaining time as m:ss", () => {
expect(formatWriteUnlockRemaining(60_000)).toBe("1:00");
expect(formatWriteUnlockRemaining(5_000)).toBe("0:05");
expect(formatWriteUnlockRemaining(0)).toBe("0:00");
});
it("never unlocks compatibility mode, even with a stale active window", async () => {
const store = useReadOnlyUnlockStore();
const connection = { id: "legacy", db_type: "mysql" as const, read_only: false, url_params: "dataCharset=GBK" };
store.rememberWindow("legacy", 60_000);
expect(connectionIsEffectivelyReadOnly(connection)).toBe(true);
await expect(ensureReadOnlyWriteAccess({ connection, sql: "UPDATE t SET a=1" })).resolves.toBe(false);
await expect(ensureReadOnlyWriteAccess({ connection, treatAsMutation: true })).resolves.toBe(false);
await expect(ensureReadOnlyWriteAccess({ connection, sql: "SELECT 1" })).resolves.toBe(true);
expect(toast).toHaveBeenCalledTimes(2);
expect(toast).toHaveBeenCalledWith("数据字符集兼容模式强制只读,无法执行写入,也不能临时解锁。", 5000);
expect(store.pending).toBeUndefined();
expect(unlockConnectionWrites).not.toHaveBeenCalled();
expect(connection.read_only).toBe(false);
expect(connection.url_params).toBe("dataCharset=GBK");
expect(connectionIsEffectivelyReadOnly({ ...connection, url_params: "" })).toBe(false);
expect(connectionIsEffectivelyReadOnly({ ...connection, db_type: "firebird" })).toBe(false);
});
it("allows compatibility reads silently and notifies once per explicit mutation denial", async () => {
const connection = { id: "legacy", db_type: "mysql" as const, url_params: "dataCharset=GBK" };
await expect(ensureReadOnlyWriteAccess({ connection, sql: "SELECT 1" })).resolves.toBe(true);
expect(toast).not.toHaveBeenCalled();
await expect(ensureReadOnlyWriteAccess({ connection, sql: "SELECT 1", treatAsMutation: true })).resolves.toBe(false);
expect(toast).toHaveBeenCalledOnce();
expect(useReadOnlyUnlockStore().pending).toBeUndefined();
expect(unlockConnectionWrites).not.toHaveBeenCalled();
expect(connectionWriteUnlockState).not.toHaveBeenCalled();
});
it.each(["az", "en", "es", "id", "it", "ja", "ko", "pt-BR", "ru", "th", "tr", "zh-CN", "zh-TW"])("provides the blocked-write notification in %s", async (locale) => {
const messages = await import(`../../../i18n/locales/${locale}.ts`);
expect(messages.default.connection.mysqlDataCharsetWriteBlocked).toBeTruthy();
});
it("classifies SQL and Redis writes without treating reads as mutations", () => {
expect(sqlLooksLikeMutation("SELECT 1", "mysql")).toBe(false);
expect(sqlLooksLikeMutation("INSERT INTO t VALUES (1)", "mysql")).toBe(true);
expect(sqlLooksLikeMutation("UPDATE t SET a = 1", "postgres")).toBe(true);
expect(sqlLooksLikeMutation("DELETE FROM t", "mysql")).toBe(true);
expect(sqlLooksLikeMutation("CREATE TABLE t (id INT)", "mysql")).toBe(true);
expect(sqlLooksLikeMutation("GET k", "redis")).toBe(false);
expect(sqlLooksLikeMutation("SET k v", "redis")).toBe(true);
expect(sqlLooksLikeMutation("# note\nGET k", "redis")).toBe(false);
expect(sqlLooksLikeMutation("-- note\nSET k v", "redis")).toBe(true);
});
it("keeps the persistent read-only flag on while a window is active", async () => {
const connection = { id: "prod", name: "prod-db", read_only: true, db_type: "mysql" as const };
expect(connectionIsEffectivelyReadOnly(connection)).toBe(true);
const pending = ensureReadOnlyWriteAccess({ connection, sql: "INSERT INTO t VALUES (1)", source: "SQL editor" });
const store = await waitForPending("prod");
expect(unlockConnectionWrites).not.toHaveBeenCalled();
await store.confirm(60);
await expect(pending).resolves.toBe(true);
expect(unlockConnectionWrites).toHaveBeenCalledWith("prod", 60);
expect(connection.read_only).toBe(true);
expect(connectionIsEffectivelyReadOnly(connection)).toBe(false);
expect(store.isUnlocked("prod")).toBe(true);
expect(store.isUnlocked("other")).toBe(false);
});
it("cancels without unlocking and forgets the window when locked or expired", async () => {
const connection = { id: "prod", name: "prod-db", read_only: true, db_type: "mysql" as const };
const cancelled = ensureReadOnlyWriteAccess({ connection, sql: "DELETE FROM t", source: "SQL editor" });
const store = await waitForPending("prod");
store.cancel();
await expect(cancelled).resolves.toBe(false);
expect(toast).not.toHaveBeenCalled();
expect(unlockConnectionWrites).not.toHaveBeenCalled();
expect(connectionIsEffectivelyReadOnly(connection)).toBe(true);
const unlocked = ensureReadOnlyWriteAccess({ connection, sql: "DELETE FROM t", source: "SQL editor" });
await waitForPending("prod");
await store.confirm(60);
await expect(unlocked).resolves.toBe(true);
expect(await ensureReadOnlyWriteAccess({ connection, sql: "UPDATE t SET a = 1" })).toBe(true);
expect(unlockConnectionWrites).toHaveBeenCalledTimes(1);
await store.lockNow("prod");
expect(lockConnectionWrites).toHaveBeenCalledWith("prod");
expect(store.isUnlocked("prod")).toBe(false);
expect(connectionIsEffectivelyReadOnly(connection)).toBe(true);
});
it("does not prompt for reads or writable connections", async () => {
const store = useReadOnlyUnlockStore();
await expect(ensureReadOnlyWriteAccess({ connection: { id: "w", read_only: false, db_type: "mysql" }, sql: "INSERT INTO t VALUES (1)" })).resolves.toBe(true);
await expect(ensureReadOnlyWriteAccess({ connection: { id: "r", read_only: true, db_type: "mysql" }, sql: "SELECT 1" })).resolves.toBe(true);
expect(store.pending).toBeUndefined();
expect(connectionWriteUnlockState).not.toHaveBeenCalled();
expect(toast).not.toHaveBeenCalled();
});
it("does not prompt for Elasticsearch searches on a read-only connection", async () => {
const store = useReadOnlyUnlockStore();
const connection = { id: "es", name: "es-prod", read_only: true, db_type: "elasticsearch" as const };
await expect(ensureReadOnlyWriteAccess({ connection, sql: 'POST /demo/_search\n{\n "size": 100\n}' })).resolves.toBe(true);
await expect(ensureReadOnlyWriteAccess({ connection, sql: "GET /demo/_doc/1" })).resolves.toBe(true);
expect(store.pending).toBeUndefined();
const pending = ensureReadOnlyWriteAccess({ connection, sql: "DELETE /demo" });
await waitForPending("es");
store.cancel();
await expect(pending).resolves.toBe(false);
});
it("reuses an existing backend window without prompting again", async () => {
connectionWriteUnlockState.mockResolvedValue(45_000);
const connection = { id: "prod", name: "prod-db", read_only: true, db_type: "mysql" as const };
await expect(ensureReadOnlyWriteAccess({ connection, sql: "INSERT INTO t VALUES (1)" })).resolves.toBe(true);
expect(unlockConnectionWrites).not.toHaveBeenCalled();
expect(useReadOnlyUnlockStore().pending).toBeUndefined();
expect(connectionIsEffectivelyReadOnly(connection)).toBe(false);
});
it.each(["", '"queryPlanner"', '"executionStats"', '"allPlansExecution"'])("does not request write unlock for MongoDB find explain(%s)", async (verbosity) => {
const store = useReadOnlyUnlockStore();
const connection = { id: "mongo", read_only: true, db_type: "mongodb" as const };
const pending = ensureReadOnlyWriteAccess({ connection, sql: `db.demo.find({}).explain(${verbosity})` });
expect(connectionWriteUnlockState).not.toHaveBeenCalled();
expect(store.pending).toBeUndefined();
await expect(pending).resolves.toBe(true);
expect(unlockConnectionWrites).not.toHaveBeenCalled();
});
it("still requests write unlock for MongoDB explain mixed with writes", async () => {
const connection = { id: "mongo", read_only: true, db_type: "mongodb" as const };
const pending = ensureReadOnlyWriteAccess({ connection, sql: "db.demo.find({}).explain(); db.demo.insertOne({active: true})" });
const store = await waitForPending("mongo");
store.cancel();
await expect(pending).resolves.toBe(false);
expect(unlockConnectionWrites).not.toHaveBeenCalled();
});
it("returns to effectively read-only when the local timer expires", async () => {
vi.useFakeTimers();
const store = useReadOnlyUnlockStore();
const connection = { id: "prod", name: "prod-db", read_only: true, db_type: "mysql" as const };
store.rememberWindow("prod", 60_000);
expect(connectionIsEffectivelyReadOnly(connection)).toBe(false);
await vi.advanceTimersByTimeAsync(60_000);
expect(store.isUnlocked("prod")).toBe(false);
expect(connectionIsEffectivelyReadOnly(connection)).toBe(true);
expect(connection.read_only).toBe(true);
});
});