Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/cli/src/commands/backup/clickhouse.test.ts
T

69 lines
2.3 KiB
TypeScript

import { EventEmitter } from "node:events";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { PassThrough } from "node:stream";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { clickhouseEnabled, maybeSnapshotClickhouse } from "./clickhouse.js";
let workDir: string;
beforeEach(async () => {
workDir = await mkdtemp(join(tmpdir(), "cli-clickhouse-"));
});
afterEach(async () => {
await rm(workDir, { recursive: true, force: true });
});
describe("clickhouseEnabled", () => {
it("returns false when feature flag is missing", () => {
expect(clickhouseEnabled({})).toBe(false);
expect(clickhouseEnabled({ BACKUP_INCLUDE_CLICKHOUSE: "false", CLICKHOUSE_URL: "http://x" })).toBe(false);
});
it("returns false when feature flag set but url missing", () => {
expect(clickhouseEnabled({ BACKUP_INCLUDE_CLICKHOUSE: "true" })).toBe(false);
});
it("returns true when feature flag and url are present", () => {
expect(clickhouseEnabled({ BACKUP_INCLUDE_CLICKHOUSE: "true", CLICKHOUSE_URL: "http://x" })).toBe(true);
});
});
describe("maybeSnapshotClickhouse", () => {
it("returns null when not enabled (no-op path)", async () => {
const result = await maybeSnapshotClickhouse({ outDir: workDir, env: {} });
expect(result).toBeNull();
});
it("invokes clickhouse-client when enabled and reports a result", async () => {
const child = new EventEmitter() as EventEmitter & { stdout: PassThrough; stderr: PassThrough };
child.stdout = new PassThrough();
child.stderr = new PassThrough();
const spawnFn = ((_cmd: string, args: readonly string[]) => {
expect(args).toContain("--query");
queueMicrotask(() => {
child.stdout.end();
child.stderr.end();
child.emit("close", 0);
});
return child as never;
}) as unknown as typeof import("node:child_process").spawn;
const result = await maybeSnapshotClickhouse({
outDir: workDir,
env: {
BACKUP_INCLUDE_CLICKHOUSE: "true",
CLICKHOUSE_URL: "http://localhost:8123",
CLICKHOUSE_DATABASE: "analytics",
},
spawnFn,
});
expect(result).not.toBeNull();
expect(result?.path).toMatch(/snapshot\.native\.gz$/u);
expect(result?.checksum.algorithm).toBe("sha256");
});
});