|
| 1 | +import { describe, expect, vi } from "vitest"; |
| 2 | + |
| 3 | +// Wiring overrides only, as in SpanPresenter.readthrough.test.ts: the presenter is given its |
| 4 | +// Postgres client through the constructor, and the run store and event repository singletons are |
| 5 | +// swapped for instances over the test containers. Every read runs against a real database. |
| 6 | +vi.mock("~/db.server", () => ({ |
| 7 | + prisma: {}, |
| 8 | + $replica: {}, |
| 9 | +})); |
| 10 | + |
| 11 | +const wiring = vi.hoisted(() => ({ |
| 12 | + runStore: undefined as unknown, |
| 13 | + eventRepository: undefined as unknown, |
| 14 | +})); |
| 15 | +vi.mock("~/v3/runStore.server", () => ({ |
| 16 | + get runStore() { |
| 17 | + return wiring.runStore; |
| 18 | + }, |
| 19 | +})); |
| 20 | +vi.mock("~/v3/eventRepository/index.server", () => ({ |
| 21 | + getEventRepositoryForStore: async () => wiring.eventRepository, |
| 22 | +})); |
| 23 | + |
| 24 | +import { ClickHouse } from "@internal/clickhouse"; |
| 25 | +import { PostgresRunStore } from "@internal/run-store"; |
| 26 | +import { containerTestWithClickhouse } from "@internal/testcontainers"; |
| 27 | +import { SpanPresenter } from "~/presenters/v3/SpanPresenter.server"; |
| 28 | +import { |
| 29 | + ClickhouseEventRepository, |
| 30 | + convertDateToClickhouseDateTime, |
| 31 | +} from "~/v3/eventRepository/clickhouseEventRepository.server"; |
| 32 | + |
| 33 | +vi.setConfig({ testTimeout: 90_000 }); |
| 34 | + |
| 35 | +function clickhouseDate(value: Date) { |
| 36 | + return value.toISOString().replace("T", " ").replace("Z", ""); |
| 37 | +} |
| 38 | + |
| 39 | +describe("SpanPresenter debug spans", () => { |
| 40 | + containerTestWithClickhouse( |
| 41 | + "opens a debug span only when debug events are shown", |
| 42 | + async ({ prisma, clickhouseContainer }) => { |
| 43 | + const user = await prisma.user.create({ |
| 44 | + data: { email: "span-debug@example.com", authenticationMethod: "MAGIC_LINK" }, |
| 45 | + }); |
| 46 | + const organization = await prisma.organization.create({ |
| 47 | + data: { title: "span-debug", slug: "span-debug" }, |
| 48 | + }); |
| 49 | + await prisma.orgMember.create({ |
| 50 | + data: { organizationId: organization.id, userId: user.id, role: "MEMBER" }, |
| 51 | + }); |
| 52 | + const project = await prisma.project.create({ |
| 53 | + data: { |
| 54 | + name: "span-debug", |
| 55 | + slug: "span-debug", |
| 56 | + organizationId: organization.id, |
| 57 | + externalRef: "proj_span_debug", |
| 58 | + }, |
| 59 | + }); |
| 60 | + const environment = await prisma.runtimeEnvironment.create({ |
| 61 | + data: { |
| 62 | + slug: "prod", |
| 63 | + type: "PRODUCTION", |
| 64 | + projectId: project.id, |
| 65 | + organizationId: organization.id, |
| 66 | + apiKey: "tr_prod_span_debug", |
| 67 | + pkApiKey: "pk_prod_span_debug", |
| 68 | + shortcode: "sc-span-debug", |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const traceId = "d".repeat(32); |
| 73 | + const createdAt = new Date(Date.now() - 60_000); |
| 74 | + await prisma.taskRun.create({ |
| 75 | + data: { |
| 76 | + friendlyId: "run_span_debug", |
| 77 | + taskIdentifier: "my-task", |
| 78 | + status: "COMPLETED_SUCCESSFULLY", |
| 79 | + payload: "{}", |
| 80 | + payloadType: "application/json", |
| 81 | + traceId, |
| 82 | + spanId: "rootspan00000001", |
| 83 | + queue: "task/my-task", |
| 84 | + runtimeEnvironmentId: environment.id, |
| 85 | + projectId: project.id, |
| 86 | + organizationId: organization.id, |
| 87 | + environmentType: "PRODUCTION", |
| 88 | + engine: "V2", |
| 89 | + taskEventStore: "clickhouse_v2", |
| 90 | + createdAt, |
| 91 | + completedAt: new Date(createdAt.getTime() + 10_000), |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + const clickhouse = new ClickHouse({ |
| 96 | + url: clickhouseContainer.getConnectionUrl(), |
| 97 | + name: "test", |
| 98 | + }); |
| 99 | + const repository = new ClickhouseEventRepository({ |
| 100 | + clickhouse, |
| 101 | + version: "v2", |
| 102 | + insertStrategy: "insert", |
| 103 | + }); |
| 104 | + const startTime = new Date(createdAt.getTime() + 1_000); |
| 105 | + const [insertError] = await clickhouse.taskEventsV2.insert([ |
| 106 | + { |
| 107 | + environment_id: environment.id, |
| 108 | + organization_id: organization.id, |
| 109 | + project_id: project.id, |
| 110 | + task_identifier: "my-task", |
| 111 | + run_id: "run_span_debug", |
| 112 | + start_time: `${clickhouseDate(startTime).slice(0, 19)}.${String( |
| 113 | + startTime.getUTCMilliseconds() * 1_000_000 |
| 114 | + ).padStart(9, "0")}`, |
| 115 | + inserted_at: convertDateToClickhouseDateTime(startTime), |
| 116 | + duration: "0", |
| 117 | + trace_id: traceId, |
| 118 | + span_id: "debugspan0000001", |
| 119 | + parent_span_id: "rootspan00000001", |
| 120 | + message: "debug event", |
| 121 | + kind: "DEBUG_EVENT", |
| 122 | + status: "OK", |
| 123 | + attributes: {}, |
| 124 | + metadata: "{}", |
| 125 | + expires_at: clickhouseDate(new Date(Date.now() + 86_400_000)), |
| 126 | + }, |
| 127 | + ]); |
| 128 | + expect(insertError).toBeNull(); |
| 129 | + |
| 130 | + wiring.runStore = new PostgresRunStore({ prisma, readOnlyPrisma: prisma }); |
| 131 | + wiring.eventRepository = repository; |
| 132 | + |
| 133 | + const presenter = new SpanPresenter(prisma, prisma); |
| 134 | + const call = (showDebug?: boolean) => |
| 135 | + presenter.call({ |
| 136 | + userId: user.id, |
| 137 | + projectSlug: project.slug, |
| 138 | + envSlug: environment.slug, |
| 139 | + spanId: "debugspan0000001", |
| 140 | + runFriendlyId: "run_span_debug", |
| 141 | + showDebug, |
| 142 | + }); |
| 143 | + |
| 144 | + try { |
| 145 | + await expect(call()).rejects.toThrow("Span not found"); |
| 146 | + await expect(call(false)).rejects.toThrow("Span not found"); |
| 147 | + expect(await call(true)).toMatchObject({ |
| 148 | + type: "span", |
| 149 | + span: { spanId: "debugspan0000001" }, |
| 150 | + }); |
| 151 | + } finally { |
| 152 | + await Promise.all([ |
| 153 | + (repository as any)._flushScheduler.shutdown(), |
| 154 | + (repository as any)._llmMetricsFlushScheduler.shutdown(), |
| 155 | + (repository as any)._otlpMetricsFlushScheduler.shutdown(), |
| 156 | + ]); |
| 157 | + } |
| 158 | + } |
| 159 | + ); |
| 160 | +}); |
0 commit comments