Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

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

const mocks = vi.hoisted(() => ({ createClient: vi.fn((_options: unknown) => ({})) }));

vi.mock("@clickhouse/client", () => ({ createClient: mocks.createClient }));

// Writers serialise instants as ISO-8601 with an explicit `Z` (dateTime.ts).
// ClickHouse's default `basic` parser rejects the offset, so the write client
// has to ask for best_effort or every insert fails with CANNOT_PARSE_INPUT.
describe("clickhouse write client", () => {
  it("parses inserted timestamps with best_effort", async () => {
    await import("./client.js");

    const writeClientOptions = mocks.createClient.mock.calls[0][0] as {
      clickhouse_settings?: Record<string, unknown>;
    };
    expect(writeClientOptions.clickhouse_settings).toMatchObject({ date_time_input_format: "best_effort" });
  });

  // Results are gzipped on the wire; inserts stay uncompressed so batches don't
  // cost gzip CPU on the ingestion path. The query user may be readonly=1 when
  // created by hand, which rejects enable_http_compression, so it stays off.
  it("compresses query responses but not inserts or user-authored SQL", async () => {
    await import("./client.js");

    const [writeClientOptions, queryClientOptions] = mocks.createClient.mock.calls.map(
      call => call[0] as { compression?: { request?: boolean; response?: boolean } }
    );
    expect(writeClientOptions.compression).toEqual({ response: true, request: false });
    expect(queryClientOptions.compression).toBeUndefined();
  });
});