|
| 1 | +import { heteroPostgresTest } from "@internal/testcontainers"; |
| 2 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 3 | +import { classifyResidency } from "@trigger.dev/core/v3/isomorphic"; |
| 4 | +import { describe, expect } from "vitest"; |
| 5 | +import { PostgresRunStore } from "./PostgresRunStore.js"; |
| 6 | +import { RoutingRunStore } from "./runOpsStore.js"; |
| 7 | + |
| 8 | +const ORG_ID = "orgroute0000000000000001"; |
| 9 | +const PROJ_ID = "projroute00000000000001"; |
| 10 | +const ENV_ID = "envroute0000000000000001"; |
| 11 | + |
| 12 | +const newId = (i: number) => "k".repeat(20) + String(i).padStart(4, "0") + "01"; |
| 13 | +const cuidId = (i: number) => "c".repeat(21) + String(i).padStart(4, "0"); |
| 14 | + |
| 15 | +async function seedShared(prisma: PrismaClient, suffix: string) { |
| 16 | + await prisma.organization.create({ |
| 17 | + data: { id: ORG_ID, title: `Route ${suffix}`, slug: `route-${suffix}` }, |
| 18 | + }); |
| 19 | + await prisma.project.create({ |
| 20 | + data: { |
| 21 | + id: PROJ_ID, |
| 22 | + name: `Route ${suffix}`, |
| 23 | + slug: `route-${suffix}`, |
| 24 | + externalRef: `proj_route_${suffix}`, |
| 25 | + organizationId: ORG_ID, |
| 26 | + }, |
| 27 | + }); |
| 28 | + await prisma.runtimeEnvironment.create({ |
| 29 | + data: { |
| 30 | + id: ENV_ID, |
| 31 | + type: "PRODUCTION", |
| 32 | + slug: "prod", |
| 33 | + projectId: PROJ_ID, |
| 34 | + organizationId: ORG_ID, |
| 35 | + apiKey: `tr_prod_${suffix}`, |
| 36 | + pkApiKey: `pk_prod_${suffix}`, |
| 37 | + shortcode: `short_${suffix}`, |
| 38 | + }, |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +const BASE = new Date("2026-01-01T00:00:00.000Z").getTime(); |
| 43 | + |
| 44 | +async function seedRun(prisma: PrismaClient, id: string, offsetSec: number) { |
| 45 | + await prisma.taskRun.create({ |
| 46 | + data: { |
| 47 | + id, |
| 48 | + engine: "V2", |
| 49 | + status: "COMPLETED_SUCCESSFULLY", |
| 50 | + friendlyId: `run_${id}`, |
| 51 | + runtimeEnvironmentId: ENV_ID, |
| 52 | + environmentType: "PRODUCTION", |
| 53 | + organizationId: ORG_ID, |
| 54 | + projectId: PROJ_ID, |
| 55 | + taskIdentifier: "route-task", |
| 56 | + payload: "{}", |
| 57 | + payloadType: "application/json", |
| 58 | + traceId: `trace_${id}`, |
| 59 | + spanId: `span_${id}`, |
| 60 | + queue: "task/route", |
| 61 | + isTest: false, |
| 62 | + taskEventStore: "taskEvent", |
| 63 | + depth: 0, |
| 64 | + createdAt: new Date(BASE + offsetSec * 1000), |
| 65 | + }, |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +describe("RoutingRunStore id-set residency routing", () => { |
| 70 | + heteroPostgresTest( |
| 71 | + "routes each id to its owning store and merges in orderBy order", |
| 72 | + { timeout: 120000 }, |
| 73 | + async ({ prisma14, prisma17 }) => { |
| 74 | + for (let i = 0; i < 5; i++) { |
| 75 | + expect(classifyResidency(newId(i))).toBe("NEW"); |
| 76 | + expect(classifyResidency(cuidId(i))).toBe("LEGACY"); |
| 77 | + } |
| 78 | + |
| 79 | + await seedShared(prisma14, "legacy"); |
| 80 | + await seedShared(prisma17, "new"); |
| 81 | + |
| 82 | + for (let i = 0; i < 5; i++) { |
| 83 | + await seedRun(prisma17, newId(i), i * 2 + 1); |
| 84 | + await seedRun(prisma14, cuidId(i), i * 2); |
| 85 | + } |
| 86 | + |
| 87 | + const legacyStore = new PostgresRunStore({ prisma: prisma14, readOnlyPrisma: prisma14 }); |
| 88 | + const newStore = new PostgresRunStore({ prisma: prisma17, readOnlyPrisma: prisma17 }); |
| 89 | + const router = new RoutingRunStore({ new: newStore, legacy: legacyStore }); |
| 90 | + |
| 91 | + const mixedIds = [0, 1, 2, 3, 4].flatMap((i) => [newId(i), cuidId(i)]); |
| 92 | + const globalDesc = [ |
| 93 | + newId(4), |
| 94 | + cuidId(4), |
| 95 | + newId(3), |
| 96 | + cuidId(3), |
| 97 | + newId(2), |
| 98 | + cuidId(2), |
| 99 | + newId(1), |
| 100 | + cuidId(1), |
| 101 | + newId(0), |
| 102 | + cuidId(0), |
| 103 | + ]; |
| 104 | + |
| 105 | + const all = (await router.findRuns({ |
| 106 | + where: { id: { in: mixedIds } }, |
| 107 | + orderBy: { createdAt: "desc" }, |
| 108 | + take: 100, |
| 109 | + })) as Array<{ id: string }>; |
| 110 | + expect(all.map((r) => r.id)).toEqual(globalDesc); |
| 111 | + |
| 112 | + const top4 = (await router.findRuns({ |
| 113 | + where: { id: { in: mixedIds } }, |
| 114 | + orderBy: { createdAt: "desc" }, |
| 115 | + take: 4, |
| 116 | + })) as Array<{ id: string }>; |
| 117 | + expect(top4.map((r) => r.id)).toEqual(globalDesc.slice(0, 4)); |
| 118 | + |
| 119 | + const newOnly = (await router.findRuns({ |
| 120 | + where: { id: { in: [newId(0), newId(2), newId(4)] } }, |
| 121 | + orderBy: { createdAt: "asc" }, |
| 122 | + })) as Array<{ id: string }>; |
| 123 | + expect(newOnly.map((r) => r.id)).toEqual([newId(0), newId(2), newId(4)]); |
| 124 | + |
| 125 | + const legacyOnly = (await router.findRuns({ |
| 126 | + where: { id: { in: [cuidId(1), cuidId(3)] } }, |
| 127 | + orderBy: { createdAt: "asc" }, |
| 128 | + })) as Array<{ id: string }>; |
| 129 | + expect(legacyOnly.map((r) => r.id)).toEqual([cuidId(1), cuidId(3)]); |
| 130 | + } |
| 131 | + ); |
| 132 | +}); |
0 commit comments