import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { type CacheService, getCacheService } from "@formbricks/cache";
import { prisma } from "@formbricks/database";
import { IntegrationType, type Organization } from "@formbricks/database/prisma";
import { logger } from "@formbricks/logger";
import { sendTelemetryEvents } from "./usage-update";

// Mock dependencies
vi.mock("@formbricks/cache", () => ({
  getCacheService: vi.fn(),
  createCacheKey: {
    custom: vi.fn((_namespace: string, key: string) => key),
  },
}));
vi.mock("@formbricks/database", () => ({
  prisma: {
    organization: {
      findFirst: vi.fn(),
      count: vi.fn(),
    },
    user: { count: vi.fn() },
    team: { count: vi.fn() },
    workspace: { count: vi.fn() },
    survey: { count: vi.fn() },
    response: {
      count: vi.fn(),
      findFirst: vi.fn(),
    },
    display: { count: vi.fn() },
    contact: { count: vi.fn() },
    segment: { count: vi.fn() },
    integration: { findMany: vi.fn() },
    account: { findMany: vi.fn() },
    $queryRaw: vi.fn(),
  },
}));
vi.mock("@formbricks/logger", () => ({
  logger: {
    error: vi.fn(),
    info: vi.fn(),
    warn: vi.fn(),
    debug: vi.fn(),
  },
}));
vi.mock("@/lib/env", () => ({
  env: {
    SMTP_HOST: "smtp.example.com",
    S3_BUCKET_NAME: "my-bucket",
    PROMETHEUS_ENABLED: true,
    RECAPTCHA_SITE_KEY: "site-key",
    RECAPTCHA_SECRET_KEY: "secret-key",
    GITHUB_ID: "github-id",
    SAML_DATABASE_URL: "postgresql://saml.example.com/formbricks",
    ENTERPRISE_LICENSE_KEY: "test-license-key",
  },
}));
vi.mock("@/lib/constants", () => ({
  E2E_TESTING: false,
  IS_DEVELOPMENT: false,
  TELEMETRY_DISABLED: false,
}));
vi.mock("@/lib/hash-string", () => ({
  hashString: vi.fn((s: string) => `hashed-${s}`),
}));
vi.mock("@/modules/ee/license-check/lib/license", () => ({
  getEnterpriseLicense: vi.fn(),
}));
// A sentinel rather than a real ProxyAgent: the test only has to prove the shared dispatcher reaches
// the fetch call, which is what a proxied self-hoster's usage update depends on.
const { proxyDispatcherSentinel } = vi.hoisted(() => ({
  proxyDispatcherSentinel: { kind: "proxy-dispatcher" },
}));
vi.mock("@/lib/proxy-dispatcher", () => ({ proxyDispatcher: proxyDispatcherSentinel }));

// Mock fetch
const fetchMock = vi.fn();
globalThis.fetch = fetchMock;

const mockCacheService = {
  get: vi.fn(),
  set: vi.fn(),
  tryLock: vi.fn(),
  del: vi.fn(),
};

describe("sendTelemetryEvents", () => {
  beforeEach(() => {
    vi.resetAllMocks();
    vi.useFakeTimers();
    // Set a fixed time far in the past to ensure we can always send telemetry
    vi.setSystemTime(new Date("2024-01-01T00:00:00.000Z"));

    // Setup default cache behavior
    vi.mocked(getCacheService).mockResolvedValue({
      ok: true,
      data: mockCacheService as unknown as CacheService,
    });
    mockCacheService.tryLock.mockResolvedValue({ ok: true, data: true }); // Lock acquired
    mockCacheService.del.mockResolvedValue({ ok: true, data: undefined });
    mockCacheService.get.mockResolvedValue({ ok: true, data: null }); // No last sent time
    mockCacheService.set.mockResolvedValue({ ok: true, data: undefined });

    // Setup default prisma behavior
    vi.mocked(prisma.organization.findFirst).mockResolvedValue({
      id: "org-123",
      createdAt: new Date("2023-01-01"),
    } as Awaited<ReturnType<typeof prisma.organization.findFirst>>);

    // Mock raw SQL query for counts (batched query)
    vi.mocked(prisma.$queryRaw).mockResolvedValue([
      {
        organizationCount: BigInt(1),
        userCount: BigInt(5),
        teamCount: BigInt(2),
        workspaceCount: BigInt(3),
        surveyCount: BigInt(10),
        inProgressSurveyCount: BigInt(4),
        completedSurveyCount: BigInt(6),
        responseCountAllTime: BigInt(100),
        responseCountSinceLastUpdate: BigInt(10),
        displayCount: BigInt(50),
        contactCount: BigInt(20),
        segmentCount: BigInt(4),
        newestResponseAt: new Date("2024-01-01T00:00:00.000Z"),
        workflowCount: BigInt(3),
        enabledWorkflowCount: BigInt(2),
        workflowRunCountSinceLastUpdate: BigInt(40),
        workflowRunFailedCountSinceLastUpdate: BigInt(4),
      },
    ] as Awaited<ReturnType<typeof prisma.$queryRaw>>);

    // Mock other queries
    vi.mocked(prisma.integration.findMany).mockResolvedValue([{ type: IntegrationType.notion }] as Awaited<
      ReturnType<typeof prisma.integration.findMany>
    >);
    vi.mocked(prisma.account.findMany, { partial: true, deep: true }).mockResolvedValue([
      { provider: "github" },
    ]);

    fetchMock.mockResolvedValue({ ok: true });
  });

  afterEach(() => {
    vi.useRealTimers();
  });

  test("should send telemetry successfully when conditions are met", async () => {
    await sendTelemetryEvents();

    // Check lock acquisition
    expect(mockCacheService.tryLock).toHaveBeenCalledWith(
      "telemetry_lock",
      "locked",
      60 * 1000 // 1 minute TTL
    );

    // Check data gathering
    expect(prisma.organization.findFirst).toHaveBeenCalled();
    expect(prisma.$queryRaw).toHaveBeenCalled();

    // Check fetch call
    expect(fetchMock).toHaveBeenCalledTimes(1);
    // The usage update must go out through the same proxy dispatcher as the license check.
    expect(fetchMock.mock.calls[0][1]).toMatchObject({ method: "POST", dispatcher: proxyDispatcherSentinel });
    const payload = JSON.parse(fetchMock.mock.calls[0][1].body);
    expect(payload.organizationCount).toBe(1);
    expect(payload.userCount).toBe(5);
    expect(payload.integrations.notion).toBe(true);
    expect(payload.sso.github).toBe(true);
    expect(payload.sso.saml).toBe(true);
    // Workflows adoption rides along; node types fall back to empty when their query yields nothing.
    expect(payload.workflows).toEqual({
      workflowCount: 3,
      enabledWorkflowCount: 2,
      runCountSinceLastUsageUpdate: 40,
      failedRunCountSinceLastUsageUpdate: 4,
      triggerTypes: [],
      actionTypes: [],
    });

    // Check cache update (no TTL parameter)
    expect(mockCacheService.set).toHaveBeenCalledWith("telemetry_last_sent_ts", expect.any(String));

    // Check lock release
    expect(mockCacheService.del).toHaveBeenCalledWith(["telemetry_lock"]);
  });

  // Node-type tests need a fresh module (the in-memory 24h guard is module state) and their own pair
  // of `$queryRaw` results: the batched counts row, then whatever the node-type query yields.
  const countsRow = {
    organizationCount: BigInt(1),
    userCount: BigInt(5),
    teamCount: BigInt(2),
    workspaceCount: BigInt(3),
    surveyCount: BigInt(10),
    inProgressSurveyCount: BigInt(4),
    completedSurveyCount: BigInt(6),
    responseCountAllTime: BigInt(100),
    responseCountSinceLastUpdate: BigInt(10),
    displayCount: BigInt(50),
    contactCount: BigInt(20),
    segmentCount: BigInt(4),
    newestResponseAt: new Date("2024-01-01T00:00:00.000Z"),
    workflowCount: BigInt(3),
    enabledWorkflowCount: BigInt(2),
    workflowRunCountSinceLastUpdate: BigInt(40),
    workflowRunFailedCountSinceLastUpdate: BigInt(4),
  };
  const importFreshSendTelemetryEvents = async () => {
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      E2E_TESTING: false,
      IS_DEVELOPMENT: false,
      TELEMETRY_DISABLED: false,
    }));
    vi.doMock("@/modules/ee/license-check/lib/license", () => ({
      getEnterpriseLicense: vi.fn().mockResolvedValue({ active: false }),
    }));
    const { sendTelemetryEvents: freshSendTelemetryEvents } = await import("./usage-update");

    vi.mocked(getCacheService).mockResolvedValue({
      ok: true,
      data: mockCacheService as unknown as CacheService,
    });
    mockCacheService.tryLock.mockResolvedValue({ ok: true, data: true });
    mockCacheService.del.mockResolvedValue({ ok: true, data: undefined });
    mockCacheService.get.mockResolvedValue({ ok: true, data: null });
    mockCacheService.set.mockResolvedValue({ ok: true, data: undefined });
    // `getInstanceInfo` selects only `id` and `createdAt`; the row is narrowed to the model type it reads.
    vi.mocked(prisma.organization.findFirst).mockResolvedValue({
      id: "org-123",
      createdAt: new Date("2023-01-01"),
    } as Organization);
    vi.mocked(prisma.integration.findMany).mockResolvedValue([]);
    vi.mocked(prisma.account.findMany).mockResolvedValue([]);
    fetchMock.mockResolvedValue({ ok: true });
    return freshSendTelemetryEvents;
  };

  test("reports the distinct workflow trigger and step types in use", async () => {
    const freshSendTelemetryEvents = await importFreshSendTelemetryEvents();
    vi.mocked(prisma.$queryRaw)
      .mockResolvedValueOnce([countsRow] as never)
      .mockResolvedValueOnce([
        { triggerTypes: ["response.completed"], actionTypes: ["send_email", "if_else"] },
      ] as never);

    await freshSendTelemetryEvents();

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const payload = JSON.parse(fetchMock.mock.calls[0][1].body);
    expect(payload.workflows.triggerTypes).toEqual(["response.completed"]);
    expect(payload.workflows.actionTypes).toEqual(["if_else", "send_email"]);
  });

  test("still sends the usage update when the workflow node-type query fails", async () => {
    const freshSendTelemetryEvents = await importFreshSendTelemetryEvents();
    vi.mocked(prisma.$queryRaw)
      .mockResolvedValueOnce([countsRow] as never)
      .mockRejectedValueOnce(new Error("jsonb_array_elements: cannot extract elements from a scalar"));

    await freshSendTelemetryEvents();

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const payload = JSON.parse(fetchMock.mock.calls[0][1].body);
    expect(payload.workflows.workflowCount).toBe(3);
    expect(payload.workflows.triggerTypes).toEqual([]);
    expect(logger.warn).toHaveBeenCalled();
  });

  test("should skip if in-memory check fails", async () => {
    // Run once to set nextTelemetryCheck
    await sendTelemetryEvents();
    vi.clearAllMocks();

    // Run again immediately (should fail in-memory check)
    await sendTelemetryEvents();

    expect(getCacheService).not.toHaveBeenCalled();
    expect(fetchMock).not.toHaveBeenCalled();
  });

  test("should skip if Redis last sent time is recent", async () => {
    // Mock last sent time as recent
    const recentTime = Date.now() - 1000 * 60 * 60; // 1 hour ago
    mockCacheService.get.mockResolvedValue({ ok: true, data: String(recentTime) });

    await sendTelemetryEvents();

    expect(mockCacheService.tryLock).not.toHaveBeenCalled(); // No lock attempt
    expect(fetchMock).not.toHaveBeenCalled();
  });

  test("should skip if lock cannot be acquired", async () => {
    mockCacheService.tryLock.mockResolvedValue({ ok: true, data: false }); // Lock not acquired

    await sendTelemetryEvents();

    expect(fetchMock).not.toHaveBeenCalled();
    expect(mockCacheService.del).not.toHaveBeenCalled(); // Shouldn't try to delete lock we didn't acquire
  });

  test("should handle cache service failure gracefully", async () => {
    vi.mocked(getCacheService).mockResolvedValue({
      ok: false,
      error: new Error("Cache error"),
    } as unknown as Awaited<ReturnType<typeof getCacheService>>);

    await sendTelemetryEvents();

    expect(fetchMock).not.toHaveBeenCalled();
    // Should verify that nextTelemetryCheck was updated, but it's a module variable.
    // We can infer it by running again and checking calls
    vi.clearAllMocks();
    await sendTelemetryEvents();
    expect(getCacheService).not.toHaveBeenCalled(); // Should be blocked by in-memory check
  });

  test("should handle telemetry send failure and apply cooldown", async () => {
    // Reset module to clear nextTelemetryCheck state from previous tests
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      E2E_TESTING: false,
      IS_DEVELOPMENT: false,
      TELEMETRY_DISABLED: false,
    }));
    vi.doMock("@/modules/ee/license-check/lib/license", () => ({
      getEnterpriseLicense: vi.fn().mockResolvedValue({ active: false }),
    }));
    const { sendTelemetryEvents: freshSendTelemetryEvents } = await import("./usage-update");

    // Ensure we can acquire lock by setting last sent time far in the past
    const oldTime = Date.now() - 25 * 60 * 60 * 1000; // 25 hours ago
    mockCacheService.get.mockResolvedValue({ ok: true, data: String(oldTime) });
    mockCacheService.tryLock.mockResolvedValue({ ok: true, data: true }); // Lock acquired

    // Make fetch fail to trigger the catch block
    const networkError = new Error("Network error");
    fetchMock.mockRejectedValue(networkError);

    await freshSendTelemetryEvents();

    // Verify lock was acquired
    expect(mockCacheService.tryLock).toHaveBeenCalledWith("telemetry_lock", "locked", 60 * 1000);

    // The error is caught in the inner catch block and logged at error level, since a licensed
    // instance that cannot deliver its usage update is a compliance problem, not a nicety.
    expect(logger.error).toHaveBeenCalledWith(
      expect.objectContaining({
        error: networkError,
        message: "Network error",
        hashedLicenseKey: "hashed-test-license-key",
      }),
      "Failed to send usage update to the license server - applying 1h cooldown"
    );

    // Lock should be released in finally block
    expect(mockCacheService.del).toHaveBeenCalledWith(["telemetry_lock"]);

    // Cache should not be updated on failure
    expect(mockCacheService.set).not.toHaveBeenCalled();

    // Verify cooldown: run again immediately (should be blocked by in-memory check)
    vi.clearAllMocks();
    mockCacheService.get.mockResolvedValue({ ok: true, data: null });
    mockCacheService.tryLock.mockResolvedValue({ ok: true, data: true });
    await freshSendTelemetryEvents();
    expect(getCacheService).not.toHaveBeenCalled(); // Should be blocked by in-memory check
  });

  test("should skip if no organization exists", async () => {
    // Reset module to clear nextTelemetryCheck state from previous tests
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      E2E_TESTING: false,
      IS_DEVELOPMENT: false,
      TELEMETRY_DISABLED: false,
    }));
    vi.doMock("@/modules/ee/license-check/lib/license", () => ({
      getEnterpriseLicense: vi.fn().mockResolvedValue({ active: false }),
    }));
    const { sendTelemetryEvents: freshSendTelemetryEvents } = await import("./usage-update");

    // Ensure we can acquire lock by setting last sent time far in the past
    const oldTime = Date.now() - 25 * 60 * 60 * 1000; // 25 hours ago

    // Re-setup mocks after resetModules
    vi.mocked(getCacheService).mockResolvedValue({
      ok: true,
      data: mockCacheService as unknown as CacheService,
    });
    mockCacheService.tryLock.mockResolvedValue({ ok: true, data: true }); // Lock acquired
    mockCacheService.del.mockResolvedValue({ ok: true, data: undefined });
    mockCacheService.get.mockResolvedValue({ ok: true, data: String(oldTime) });
    mockCacheService.set.mockResolvedValue({ ok: true, data: undefined });

    vi.mocked(prisma.organization.findFirst).mockResolvedValue(null);

    await freshSendTelemetryEvents();

    // sendTelemetry returns early when no org exists, so nothing is reported
    expect(fetchMock).not.toHaveBeenCalled();

    // Verify lock was acquired (prerequisite for finally block to execute)
    expect(mockCacheService.tryLock).toHaveBeenCalledWith("telemetry_lock", "locked", 60 * 1000);

    // Lock should be released in finally block
    expect(mockCacheService.del).toHaveBeenCalledWith(["telemetry_lock"]);

    // The 24h window must not be consumed when nothing was reported: recording it would delay this
    // instance's *first* usage update by a day (ENG-2107).
    expect(mockCacheService.set).not.toHaveBeenCalled();

    // A 1h cooldown applies instead of the full 24h, so the retry lands once an org exists.
    vi.clearAllMocks();
    vi.mocked(getCacheService).mockResolvedValue({
      ok: true,
      data: mockCacheService as unknown as CacheService,
    });
    vi.setSystemTime(new Date(Date.now() + 61 * 60 * 1000));
    mockCacheService.get.mockResolvedValue({ ok: true, data: String(oldTime) });
    mockCacheService.tryLock.mockResolvedValue({ ok: true, data: true });
    mockCacheService.del.mockResolvedValue({ ok: true, data: undefined });
    mockCacheService.set.mockResolvedValue({ ok: true, data: undefined });
    vi.mocked(prisma.organization.findFirst).mockResolvedValue({
      id: "org-123",
      createdAt: new Date("2023-01-01"),
    } as Awaited<ReturnType<typeof prisma.organization.findFirst>>);
    vi.mocked(prisma.$queryRaw).mockResolvedValue([
      {
        organizationCount: BigInt(1),
        userCount: BigInt(1),
        teamCount: BigInt(1),
        workspaceCount: BigInt(1),
        surveyCount: BigInt(0),
        inProgressSurveyCount: BigInt(0),
        completedSurveyCount: BigInt(0),
        responseCountAllTime: BigInt(0),
        responseCountSinceLastUpdate: BigInt(0),
        displayCount: BigInt(0),
        contactCount: BigInt(0),
        segmentCount: BigInt(0),
        newestResponseAt: null,
        workflowCount: BigInt(3),
        enabledWorkflowCount: BigInt(2),
        workflowRunCountSinceLastUpdate: BigInt(40),
        workflowRunFailedCountSinceLastUpdate: BigInt(4),
      },
    ] as Awaited<ReturnType<typeof prisma.$queryRaw>>);
    vi.mocked(prisma.integration.findMany, { partial: true, deep: true }).mockResolvedValue([]);
    vi.mocked(prisma.account.findMany, { partial: true, deep: true }).mockResolvedValue([]);
    fetchMock.mockResolvedValue({ ok: true });

    await freshSendTelemetryEvents();

    expect(fetchMock).toHaveBeenCalledTimes(1);
    expect(mockCacheService.set).toHaveBeenCalledWith("telemetry_last_sent_ts", expect.any(String));
  });

  test("should not record a send when the usage update endpoint rejects it", async () => {
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      E2E_TESTING: false,
      IS_DEVELOPMENT: false,
      TELEMETRY_DISABLED: false,
    }));
    vi.doMock("@/modules/ee/license-check/lib/license", () => ({
      getEnterpriseLicense: vi.fn().mockResolvedValue({ active: false }),
    }));
    const { sendTelemetryEvents: freshSendTelemetryEvents } = await import("./usage-update");

    fetchMock.mockResolvedValue({ ok: false, status: 503 });

    await freshSendTelemetryEvents();

    expect(fetchMock).toHaveBeenCalledTimes(1);
    // A rejected update is a failure, not a send: the timestamp stays put so the next run retries.
    expect(mockCacheService.set).not.toHaveBeenCalled();
    expect(logger.error).toHaveBeenCalledWith(
      expect.objectContaining({ message: "Usage update endpoint responded with status 503" }),
      "Failed to send usage update to the license server - applying 1h cooldown"
    );
    expect(mockCacheService.del).toHaveBeenCalledWith(["telemetry_lock"]);
  });

  test("should skip telemetry when TELEMETRY_DISABLED is true and no active EE license", async () => {
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      E2E_TESTING: false,
      IS_DEVELOPMENT: false,
      TELEMETRY_DISABLED: true,
    }));
    vi.doMock("@/modules/ee/license-check/lib/license", () => ({
      getEnterpriseLicense: vi.fn().mockResolvedValue({ active: false }),
    }));
    const { sendTelemetryEvents: freshSendTelemetryEvents } = await import("./usage-update");

    await freshSendTelemetryEvents();

    // Should return early without touching cache or sending telemetry
    expect(getCacheService).not.toHaveBeenCalled();
    expect(fetchMock).not.toHaveBeenCalled();
  });

  test("should send telemetry when TELEMETRY_DISABLED is true but EE license is active", async () => {
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      E2E_TESTING: false,
      IS_DEVELOPMENT: false,
      TELEMETRY_DISABLED: true,
    }));
    vi.doMock("@/modules/ee/license-check/lib/license", () => ({
      getEnterpriseLicense: vi.fn().mockResolvedValue({ active: true }),
    }));
    const { sendTelemetryEvents: freshSendTelemetryEvents } = await import("./usage-update");

    // Re-setup mocks after resetModules
    vi.mocked(getCacheService).mockResolvedValue({
      ok: true,
      data: mockCacheService as unknown as CacheService,
    });
    mockCacheService.tryLock.mockResolvedValue({ ok: true, data: true });
    mockCacheService.del.mockResolvedValue({ ok: true, data: undefined });
    mockCacheService.get.mockResolvedValue({ ok: true, data: null });
    mockCacheService.set.mockResolvedValue({ ok: true, data: undefined });

    vi.mocked(prisma.organization.findFirst).mockResolvedValue({
      id: "org-123",
      createdAt: new Date("2023-01-01"),
    } as Awaited<ReturnType<typeof prisma.organization.findFirst>>);
    vi.mocked(prisma.$queryRaw).mockResolvedValue([
      {
        organizationCount: BigInt(1),
        userCount: BigInt(5),
        teamCount: BigInt(2),
        workspaceCount: BigInt(3),
        surveyCount: BigInt(10),
        inProgressSurveyCount: BigInt(4),
        completedSurveyCount: BigInt(6),
        responseCountAllTime: BigInt(100),
        responseCountSinceLastUpdate: BigInt(10),
        displayCount: BigInt(50),
        contactCount: BigInt(20),
        segmentCount: BigInt(4),
        newestResponseAt: new Date("2024-01-01T00:00:00.000Z"),
        workflowCount: BigInt(3),
        enabledWorkflowCount: BigInt(2),
        workflowRunCountSinceLastUpdate: BigInt(40),
        workflowRunFailedCountSinceLastUpdate: BigInt(4),
      },
    ] as Awaited<ReturnType<typeof prisma.$queryRaw>>);
    vi.mocked(prisma.integration.findMany).mockResolvedValue([{ type: IntegrationType.notion }] as Awaited<
      ReturnType<typeof prisma.integration.findMany>
    >);
    vi.mocked(prisma.account.findMany, { partial: true, deep: true }).mockResolvedValue([
      { provider: "github" },
    ]);
    fetchMock.mockResolvedValue({ ok: true });

    await freshSendTelemetryEvents();

    // EE license active — telemetry should bypass TELEMETRY_DISABLED and send
    expect(fetchMock).toHaveBeenCalledTimes(1);
  });

  test("should unconditionally skip when E2E_TESTING is true even with active EE license", async () => {
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      E2E_TESTING: true,
      IS_DEVELOPMENT: false,
      TELEMETRY_DISABLED: false,
    }));
    vi.doMock("@/modules/ee/license-check/lib/license", () => ({
      getEnterpriseLicense: vi.fn().mockResolvedValue({ active: true }),
    }));
    const { sendTelemetryEvents: freshSendTelemetryEvents } = await import("./usage-update");

    await freshSendTelemetryEvents();

    // E2E_TESTING is a hard skip — no EE bypass, no cache, no fetch
    expect(getCacheService).not.toHaveBeenCalled();
    expect(fetchMock).not.toHaveBeenCalled();
  });

  test("should unconditionally skip when IS_DEVELOPMENT is true", async () => {
    vi.resetModules();
    vi.doMock("@/lib/constants", () => ({
      E2E_TESTING: false,
      IS_DEVELOPMENT: true,
      TELEMETRY_DISABLED: false,
    }));
    vi.doMock("@/modules/ee/license-check/lib/license", () => ({
      getEnterpriseLicense: vi.fn().mockResolvedValue({ active: true }),
    }));
    const { sendTelemetryEvents: freshSendTelemetryEvents } = await import("./usage-update");

    await freshSendTelemetryEvents();

    expect(getCacheService).not.toHaveBeenCalled();
    expect(fetchMock).not.toHaveBeenCalled();
  });
});
