|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +vi.mock("~/env.server", () => ({ |
| 3 | + env: {}, |
| 4 | +})); |
| 5 | +vi.mock("~/db.server", () => ({ |
| 6 | + prisma: {}, |
| 7 | + $replica: {}, |
| 8 | +})); |
| 9 | + |
| 10 | +import { createRunsRepository } from "~/services/runsRepositoryFactory.server"; |
| 11 | +import type { ClickHouse } from "@internal/clickhouse"; |
| 12 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 13 | + |
| 14 | +describe("createRunsRepository", () => { |
| 15 | + let mockClickhouse: ClickHouse; |
| 16 | + let mockPrisma: PrismaClient; |
| 17 | + let originalEnv: Record<string, unknown>; |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + const envModule = await import("~/env.server"); |
| 21 | + originalEnv = { ...envModule.env }; |
| 22 | + |
| 23 | + mockClickhouse = {} as ClickHouse; |
| 24 | + mockPrisma = {} as PrismaClient; |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(async () => { |
| 28 | + const envModule = await import("~/env.server"); |
| 29 | + Object.assign(envModule.env, originalEnv); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should default to postgres when RUN_REPLICATION_ENABLED is not set", async () => { |
| 33 | + const envModule = await import("~/env.server"); |
| 34 | + envModule.env.RUN_REPLICATION_ENABLED = "0"; |
| 35 | + envModule.env.RUN_REPLICATION_CLICKHOUSE_URL = "http://localhost:8123"; |
| 36 | + |
| 37 | + const repository = createRunsRepository({ |
| 38 | + clickhouse: mockClickhouse, |
| 39 | + prisma: mockPrisma, |
| 40 | + }); |
| 41 | + |
| 42 | + expect(repository).toBeDefined(); |
| 43 | + expect(repository.listRuns).toBeDefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should default to postgres when RUN_REPLICATION_CLICKHOUSE_URL is not set", async () => { |
| 47 | + const envModule = await import("~/env.server"); |
| 48 | + envModule.env.RUN_REPLICATION_ENABLED = "1"; |
| 49 | + envModule.env.RUN_REPLICATION_CLICKHOUSE_URL = undefined; |
| 50 | + |
| 51 | + const repository = createRunsRepository({ |
| 52 | + clickhouse: mockClickhouse, |
| 53 | + prisma: mockPrisma, |
| 54 | + }); |
| 55 | + |
| 56 | + expect(repository).toBeDefined(); |
| 57 | + expect(repository.listRuns).toBeDefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should default to clickhouse when both conditions are met", async () => { |
| 61 | + const envModule = await import("~/env.server"); |
| 62 | + envModule.env.RUN_REPLICATION_ENABLED = "1"; |
| 63 | + envModule.env.RUN_REPLICATION_CLICKHOUSE_URL = "http://localhost:8123"; |
| 64 | + |
| 65 | + const repository = createRunsRepository({ |
| 66 | + clickhouse: mockClickhouse, |
| 67 | + prisma: mockPrisma, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(repository).toBeDefined(); |
| 71 | + expect(repository.listRuns).toBeDefined(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should create a valid RunsRepository instance", () => { |
| 75 | + const repository = createRunsRepository({ |
| 76 | + clickhouse: mockClickhouse, |
| 77 | + prisma: mockPrisma, |
| 78 | + }); |
| 79 | + |
| 80 | + expect(repository.listRuns).toBeDefined(); |
| 81 | + expect(repository.countRuns).toBeDefined(); |
| 82 | + expect(typeof repository.listRuns).toBe("function"); |
| 83 | + expect(typeof repository.countRuns).toBe("function"); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
0 commit comments