|
| 1 | +// RED→GREEN for bounding the DISPLAY `connectedRuns` RELATION hydration (findWaitpoint / |
| 2 | +// findManyWaitpoints with `include`/`select: { connectedRuns }`), NOT the id-list helper |
| 3 | +// `findWaitpointConnectedRunIds` (covered separately in connectedRunsBounded.test.ts). |
| 4 | +// |
| 5 | +// The dedicated-schema hydrator `hydrateConnectedRuns` goes through the generic |
| 6 | +// `batchHydrateJoinRelation`, which fetched EVERY WaitpointRunConnection link with no per-parent |
| 7 | +// cap — so a heavily-fanned-in waitpoint hydrated an unbounded connectedRuns list, bypassing the |
| 8 | +// CONNECTED_RUNS_LIMIT the presenter/`findWaitpointConnectedRunIds` already enforce. |
| 9 | +// |
| 10 | +// The fix bounds it per parent via a window function + existence-JOIN to TaskRun, mirroring the |
| 11 | +// bounded helper: a dangling connection row never occupies a LIMIT slot, and the returned relation |
| 12 | +// is capped at CONNECTED_RUNS_LIMIT. |
| 13 | + |
| 14 | +import { heteroRunOpsPostgresTest } from "@internal/testcontainers"; |
| 15 | +import { describe, expect } from "vitest"; |
| 16 | +import { CONNECTED_RUNS_LIMIT, PostgresRunStore } from "./PostgresRunStore.js"; |
| 17 | + |
| 18 | +function seedEnvironmentDedicated(suffix: string) { |
| 19 | + return { |
| 20 | + organization: { id: `org_${suffix}` }, |
| 21 | + project: { id: `proj_${suffix}` }, |
| 22 | + environment: { id: `env_${suffix}` }, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function taskRunData(opts: { |
| 27 | + id: string; |
| 28 | + friendlyId: string; |
| 29 | + organizationId: string; |
| 30 | + projectId: string; |
| 31 | + runtimeEnvironmentId: string; |
| 32 | +}) { |
| 33 | + return { |
| 34 | + id: opts.id, |
| 35 | + engine: "V2" as const, |
| 36 | + status: "PENDING" as const, |
| 37 | + friendlyId: opts.friendlyId, |
| 38 | + runtimeEnvironmentId: opts.runtimeEnvironmentId, |
| 39 | + environmentType: "DEVELOPMENT" as const, |
| 40 | + organizationId: opts.organizationId, |
| 41 | + projectId: opts.projectId, |
| 42 | + taskIdentifier: "my-task", |
| 43 | + payload: "{}", |
| 44 | + payloadType: "application/json", |
| 45 | + traceContext: {}, |
| 46 | + traceId: `trace_${opts.id}`, |
| 47 | + spanId: `span_${opts.id}`, |
| 48 | + queue: "task/my-task", |
| 49 | + isTest: false, |
| 50 | + taskEventStore: "taskEvent", |
| 51 | + depth: 0, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +describe("PostgresRunStore.findWaitpoint — connectedRuns relation is bounded", () => { |
| 56 | + heteroRunOpsPostgresTest( |
| 57 | + "dedicated schema: include connectedRuns caps at CONNECTED_RUNS_LIMIT and skips dangling connections", |
| 58 | + async ({ prisma17 }) => { |
| 59 | + const store = new PostgresRunStore({ |
| 60 | + prisma: prisma17 as never, |
| 61 | + readOnlyPrisma: prisma17 as never, |
| 62 | + schemaVariant: "dedicated", |
| 63 | + }); |
| 64 | + |
| 65 | + const env = seedEnvironmentDedicated("cr_incl_new"); |
| 66 | + const waitpointId = "waitpoint_cr_incl_new"; |
| 67 | + await prisma17.waitpoint.create({ |
| 68 | + data: { |
| 69 | + id: waitpointId, |
| 70 | + friendlyId: "wp_cr_incl_new", |
| 71 | + type: "MANUAL", |
| 72 | + status: "PENDING", |
| 73 | + idempotencyKey: `idem_${waitpointId}`, |
| 74 | + userProvidedIdempotencyKey: false, |
| 75 | + projectId: env.project.id, |
| 76 | + environmentId: env.environment.id, |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + // Dangling connections (taskRunId points at runs that were never created). Legal on the |
| 81 | + // dedicated schema — WaitpointRunConnection.taskRunId is a scalar with no FK. |
| 82 | + const danglingRunIds = Array.from({ length: 20 }, (_, i) => `run_cr_dangling_${i}`); |
| 83 | + for (const runId of danglingRunIds) { |
| 84 | + await store.blockRunWithWaitpointEdges({ |
| 85 | + runId, |
| 86 | + waitpointIds: [waitpointId], |
| 87 | + projectId: env.project.id, |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + // More REAL connected runs than the limit. |
| 92 | + const realRunIds = Array.from({ length: CONNECTED_RUNS_LIMIT + 3 }, (_, i) => `run_cr_real_${i}`); |
| 93 | + for (const [i, id] of realRunIds.entries()) { |
| 94 | + await prisma17.taskRun.create({ |
| 95 | + data: taskRunData({ |
| 96 | + id, |
| 97 | + friendlyId: `run_cr_incl_new_${i}`, |
| 98 | + organizationId: env.organization.id, |
| 99 | + projectId: env.project.id, |
| 100 | + runtimeEnvironmentId: env.environment.id, |
| 101 | + }), |
| 102 | + }); |
| 103 | + await store.blockRunWithWaitpointEdges({ |
| 104 | + runId: id, |
| 105 | + waitpointIds: [waitpointId], |
| 106 | + projectId: env.project.id, |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + const waitpoint = (await store.findWaitpoint({ |
| 111 | + where: { id: waitpointId }, |
| 112 | + include: { connectedRuns: true }, |
| 113 | + })) as unknown as { connectedRuns: { id: string }[] } | null; |
| 114 | + |
| 115 | + expect(waitpoint).not.toBeNull(); |
| 116 | + const connectedRuns = waitpoint!.connectedRuns; |
| 117 | + |
| 118 | + // Bounded. |
| 119 | + expect(connectedRuns.length).toBeLessThanOrEqual(CONNECTED_RUNS_LIMIT); |
| 120 | + // Never a dangling id — every returned run must be a REAL run. |
| 121 | + for (const run of connectedRuns) { |
| 122 | + expect(realRunIds).toContain(run.id); |
| 123 | + expect(danglingRunIds).not.toContain(run.id); |
| 124 | + } |
| 125 | + } |
| 126 | + ); |
| 127 | +}); |
0 commit comments