|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { OrganizationDataStoresRegistry } from "~/services/dataStores/organizationDataStoresRegistry.server"; |
| 3 | +import { ClickhouseFactory, getQueueMetricsClickhouseClient } from "./clickhouseFactory.server"; |
| 4 | + |
| 5 | +const ORG_WITH_OVERRIDE = "org_with_dedicated_ch"; |
| 6 | +const DEDICATED_CH_URL = "http://dedicated-ch.example:8123"; |
| 7 | + |
| 8 | +function registryWithOverride(overrides: Record<string, string>): OrganizationDataStoresRegistry { |
| 9 | + return { |
| 10 | + isLoaded: true, |
| 11 | + isReady: Promise.resolve(), |
| 12 | + get(organizationId: string, kind: string) { |
| 13 | + const url = overrides[organizationId]; |
| 14 | + if (kind === "CLICKHOUSE" && url) { |
| 15 | + return { kind: "CLICKHOUSE", url }; |
| 16 | + } |
| 17 | + return null; |
| 18 | + }, |
| 19 | + } as unknown as OrganizationDataStoresRegistry; |
| 20 | +} |
| 21 | + |
| 22 | +describe("ClickhouseFactory queue-metrics routing", () => { |
| 23 | + it("reads queue metrics from the shared warehouse even when the org has a dedicated CH override", () => { |
| 24 | + const factory = new ClickhouseFactory( |
| 25 | + registryWithOverride({ [ORG_WITH_OVERRIDE]: DEDICATED_CH_URL }) |
| 26 | + ); |
| 27 | + |
| 28 | + const client = factory.getClickhouseForOrganizationSync(ORG_WITH_OVERRIDE, "queueMetrics"); |
| 29 | + |
| 30 | + expect(client).toBe(getQueueMetricsClickhouseClient()); |
| 31 | + }); |
| 32 | + |
| 33 | + it("routes queue metrics to the shared client identically with or without an org override", () => { |
| 34 | + const withOverride = new ClickhouseFactory( |
| 35 | + registryWithOverride({ [ORG_WITH_OVERRIDE]: DEDICATED_CH_URL }) |
| 36 | + ); |
| 37 | + const withoutOverride = new ClickhouseFactory(registryWithOverride({})); |
| 38 | + |
| 39 | + const overrideClient = withOverride.getClickhouseForOrganizationSync( |
| 40 | + ORG_WITH_OVERRIDE, |
| 41 | + "queueMetrics" |
| 42 | + ); |
| 43 | + const defaultClient = withoutOverride.getClickhouseForOrganizationSync( |
| 44 | + ORG_WITH_OVERRIDE, |
| 45 | + "queueMetrics" |
| 46 | + ); |
| 47 | + |
| 48 | + expect(overrideClient).toBe(defaultClient); |
| 49 | + expect(overrideClient).toBe(getQueueMetricsClickhouseClient()); |
| 50 | + }); |
| 51 | + |
| 52 | + it("still honors the per-org override for non-shared client types (events)", () => { |
| 53 | + const withOverride = new ClickhouseFactory( |
| 54 | + registryWithOverride({ [ORG_WITH_OVERRIDE]: DEDICATED_CH_URL }) |
| 55 | + ); |
| 56 | + const withoutOverride = new ClickhouseFactory(registryWithOverride({})); |
| 57 | + |
| 58 | + const overrideEvents = withOverride.getClickhouseForOrganizationSync( |
| 59 | + ORG_WITH_OVERRIDE, |
| 60 | + "events" |
| 61 | + ); |
| 62 | + const defaultEvents = withoutOverride.getClickhouseForOrganizationSync( |
| 63 | + ORG_WITH_OVERRIDE, |
| 64 | + "events" |
| 65 | + ); |
| 66 | + |
| 67 | + expect(overrideEvents).not.toBe(defaultEvents); |
| 68 | + }); |
| 69 | +}); |
0 commit comments