The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Prisma from "@alchemy.run/prisma"; |
| 2 | import { PrismaLogStreamError } from "@alchemy.run/prisma/PrismaLogs"; |
| 3 | import { describe, expect, it } from "@alchemy.run/test"; |
| 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 6 | import { ArtifactStore, createArtifactStore } from "alchemy/Artifacts"; |
| 7 | import { AuthProviders, AuthProvidersLive } from "alchemy/Auth/AuthProvider"; |
| 8 | import * as Interaction from "alchemy/Interaction"; |
| 9 | import * as Provider from "alchemy/Provider"; |
| 10 | import { PlatformServices } from "alchemy/Util/PlatformServices"; |
| 11 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Redacted from "effect/Redacted"; |
| 15 | import * as Result from "effect/Result"; |
| 16 | import * as Stream from "effect/Stream"; |
| 17 | import { v4 as uuidv4 } from "uuid"; |
| 18 | import { testStackContext } from "./fixtures/StackContext.ts"; |
| 19 | |
| 20 | const devAlchemyContext = Layer.succeed(AlchemyContext, { |
| 21 | dotAlchemy: ".alchemy-test", |
| 22 | dev: true, |
| 23 | adopt: false, |
| 24 | }); |
| 25 | |
| 26 | // What `Stack.make` supplies to a stack's `providers` layer. |
| 27 | const stackProvidedServices = Layer.mergeAll( |
| 28 | testStackContext, |
| 29 | PlatformServices, |
| 30 | Layer.effect(ArtifactStore, Effect.sync(createArtifactStore)), |
| 31 | ); |
| 32 | |
| 33 | const providePrismaDev = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 34 | effect.pipe( |
| 35 | Effect.provide( |
| 36 | Prisma.providers().pipe( |
| 37 | // The dual registration always carries the (lazy) management-api |
| 38 | // layer; it registers auth at build without resolving credentials. |
| 39 | Layer.provideMerge(AuthProvidersLive), |
| 40 | Layer.provide(stackProvidedServices), |
| 41 | ), |
| 42 | ), |
| 43 | Effect.provide(devAlchemyContext), |
| 44 | Effect.provide(Interaction.layerNonInteractive()), |
| 45 | ); |
| 46 | |
| 47 | const reconcileInput = (id: string, news: unknown, output?: unknown) => |
| 48 | ({ |
| 49 | id, |
| 50 | instanceId: "00000000000000000000000000000000", |
| 51 | news, |
| 52 | olds: undefined, |
| 53 | output, |
| 54 | session: undefined as never, |
| 55 | bindings: [], |
| 56 | }) as never; |
| 57 | |
| 58 | describe("Prisma providers", { tags: ["unit", "provider:prisma", "local"] }, () => { |
| 59 | it.effect( |
| 60 | "registers Prisma auth without resolving credentials", |
| 61 | () => |
| 62 | Effect.gen(function* () { |
| 63 | const authProviders: AuthProviders["Service"] = {}; |
| 64 | |
| 65 | yield* Layer.build( |
| 66 | Prisma.providers().pipe( |
| 67 | Layer.provideMerge(Layer.succeed(AuthProviders, authProviders)), |
| 68 | Layer.provide(stackProvidedServices), |
| 69 | ), |
| 70 | ); |
| 71 | |
| 72 | expect(authProviders.Prisma?.name).toBe("Prisma"); |
| 73 | }).pipe( |
| 74 | Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ CI: true }))), |
| 75 | Effect.provide( |
| 76 | Layer.succeed(AlchemyContext, { dotAlchemy: ".alchemy-test", dev: false, adopt: false }), |
| 77 | ), |
| 78 | Effect.scoped, |
| 79 | ), |
| 80 | { timeout: 30_000 }, |
| 81 | ); |
| 82 | |
| 83 | it.effect( |
| 84 | "registers every Prisma resource provider", |
| 85 | () => |
| 86 | Effect.gen(function* () { |
| 87 | const resourceTypes = [ |
| 88 | Prisma.Project.Type, |
| 89 | Prisma.Database.Type, |
| 90 | Prisma.Connection.Type, |
| 91 | Prisma.Branch.Type, |
| 92 | Prisma.Bucket.Type, |
| 93 | Prisma.BucketAccessKey.Type, |
| 94 | Prisma.Compute.Type, |
| 95 | Prisma.App.Type, |
| 96 | Prisma.Deployment.Type, |
| 97 | Prisma.CustomDomain.Type, |
| 98 | Prisma.EnvironmentVariable.Type, |
| 99 | Prisma.SourceRepository.Type, |
| 100 | ]; |
| 101 | const expectedStables = new Map([ |
| 102 | [Prisma.Project.Type, ["projectId"]], |
| 103 | [Prisma.Database.Type, ["databaseId"]], |
| 104 | [Prisma.Connection.Type, ["connectionId"]], |
| 105 | [Prisma.Branch.Type, ["branchId"]], |
| 106 | [Prisma.Bucket.Type, ["bucketId"]], |
| 107 | [ |
| 108 | Prisma.BucketAccessKey.Type, |
| 109 | [ |
| 110 | "bucketAccessKeyId", |
| 111 | "bucketId", |
| 112 | "accessKeyId", |
| 113 | "secretAccessKey", |
| 114 | "endpoint", |
| 115 | "bucketName", |
| 116 | ], |
| 117 | ], |
| 118 | [Prisma.Compute.Type, ["appId"]], |
| 119 | [Prisma.App.Type, ["appId"]], |
| 120 | [Prisma.Deployment.Type, ["deploymentId"]], |
| 121 | [Prisma.CustomDomain.Type, ["customDomainId"]], |
| 122 | [Prisma.EnvironmentVariable.Type, ["environmentVariableId"]], |
| 123 | [Prisma.SourceRepository.Type, ["sourceRepositoryId"]], |
| 124 | ]); |
| 125 | |
| 126 | const providers = yield* Effect.all( |
| 127 | resourceTypes.map((type) => Provider.findProviderByType(type as any)), |
| 128 | { concurrency: "unbounded" }, |
| 129 | ); |
| 130 | |
| 131 | expect(providers).toHaveLength(resourceTypes.length); |
| 132 | for (const provider of providers) { |
| 133 | expect(typeof provider.reconcile).toBe("function"); |
| 134 | expect(typeof provider.delete).toBe("function"); |
| 135 | // Lookup resolves the concrete local variant in dev. |
| 136 | expect(provider.mode).toBe("local"); |
| 137 | } |
| 138 | for (let i = 0; i < resourceTypes.length; i += 1) { |
| 139 | const provider = providers[i]!; |
| 140 | expect(provider.stables).toEqual(expectedStables.get(resourceTypes[i])); |
| 141 | } |
| 142 | }).pipe(providePrismaDev), |
| 143 | { |
| 144 | tags: [ |
| 145 | "provider:prisma:app", |
| 146 | "provider:prisma:branch", |
| 147 | "provider:prisma:bucket", |
| 148 | "provider:prisma:bucketaccesskey", |
| 149 | "provider:prisma:compute", |
| 150 | "provider:prisma:connection", |
| 151 | "provider:prisma:customdomain", |
| 152 | "provider:prisma:database", |
| 153 | "provider:prisma:deployment", |
| 154 | "provider:prisma:environmentvariable", |
| 155 | "provider:prisma:project", |
| 156 | "provider:prisma:sourcerepository", |
| 157 | ], |
| 158 | }, |
| 159 | ); |
| 160 | |
| 161 | it.effect( |
| 162 | "uses tokenless dev providers from Prisma.providers()", |
| 163 | () => |
| 164 | Effect.gen(function* () { |
| 165 | const projectProvider = yield* Provider.findProviderByType(Prisma.Project.Type as any); |
| 166 | const appProvider = yield* Provider.findProviderByType(Prisma.App.Type as any); |
| 167 | const envProvider = yield* Provider.findProviderByType( |
| 168 | Prisma.EnvironmentVariable.Type as any, |
| 169 | ); |
| 170 | const branchProvider = yield* Provider.findProviderByType(Prisma.Branch.Type as any); |
| 171 | const bucketProvider = yield* Provider.findProviderByType(Prisma.Bucket.Type as any); |
| 172 | const bucketKeyProvider = yield* Provider.findProviderByType( |
| 173 | Prisma.BucketAccessKey.Type as any, |
| 174 | ); |
| 175 | |
| 176 | const project = (yield* projectProvider.reconcile( |
| 177 | reconcileInput("Project", { name: "local-project", createDatabase: false }), |
| 178 | )) as Prisma.Project["Attributes"]; |
| 179 | const app = (yield* appProvider.reconcile( |
| 180 | reconcileInput("App", { project, displayName: "api", regionId: "us-east-1" }), |
| 181 | )) as Prisma.App["Attributes"]; |
| 182 | const env = (yield* envProvider.reconcile( |
| 183 | reconcileInput("Environment", { |
| 184 | project, |
| 185 | branchId: "branch-preview", |
| 186 | class: "preview", |
| 187 | key: "TOKEN", |
| 188 | value: Redacted.make("secret"), |
| 189 | }), |
| 190 | )) as Prisma.EnvironmentVariable["Attributes"]; |
| 191 | const branch = (yield* branchProvider.reconcile( |
| 192 | reconcileInput("Branch", { project, gitName: "main", isDefault: true }), |
| 193 | )) as Prisma.Branch["Attributes"]; |
| 194 | |
| 195 | const bucket = (yield* bucketProvider.reconcile( |
| 196 | reconcileInput("Bucket", { project, name: "uploads" }), |
| 197 | )) as Prisma.Bucket["Attributes"]; |
| 198 | const bucketKey = (yield* bucketKeyProvider.reconcile( |
| 199 | reconcileInput("BucketAccessKey", { bucket, role: "read_write" }), |
| 200 | )) as Prisma.BucketAccessKey["Attributes"]; |
| 201 | |
| 202 | expect(project.projectId).toBe("dev:project:Project"); |
| 203 | expect(app.projectId).toBe(project.projectId); |
| 204 | expect(app.appId).toBe("dev:app:App"); |
| 205 | expect(env.projectId).toBe(project.projectId); |
| 206 | expect(env.branchId).toBe("branch-preview"); |
| 207 | expect(Redacted.value(env.value)).toBe("secret"); |
| 208 | expect(branch.role).toBe("production"); |
| 209 | expect(bucket.bucketId).toBe("dev:bucket:Bucket"); |
| 210 | expect(bucket.name).toBe("uploads"); |
| 211 | expect(bucket.projectId).toBe(project.projectId); |
| 212 | expect(bucketKey.bucketAccessKeyId).toBe("dev:bucket-access-key:BucketAccessKey"); |
| 213 | expect(bucketKey.bucketId).toBe(bucket.bucketId); |
| 214 | expect(Redacted.isRedacted(bucketKey.secretAccessKey)).toBe(true); |
| 215 | }).pipe(providePrismaDev), |
| 216 | { |
| 217 | tags: [ |
| 218 | "provider:prisma:app", |
| 219 | "provider:prisma:branch", |
| 220 | "provider:prisma:bucket", |
| 221 | "provider:prisma:bucketaccesskey", |
| 222 | "provider:prisma:environmentvariable", |
| 223 | "provider:prisma:project", |
| 224 | ], |
| 225 | }, |
| 226 | ); |
| 227 | |
| 228 | it.effect("managementApi rejects an unknown explicit profile", () => |
| 229 | Effect.gen(function* () { |
| 230 | const result = yield* Effect.result( |
| 231 | Effect.sandbox( |
| 232 | Effect.gen(function* () { |
| 233 | yield* Prisma.PrismaClient; |
| 234 | }).pipe( |
| 235 | Effect.provide(Prisma.managementApi()), |
| 236 | Effect.provide( |
| 237 | ConfigProvider.layer( |
| 238 | ConfigProvider.fromUnknown({ ALCHEMY_PROFILE: `non-existent-${uuidv4()}` }), |
| 239 | ), |
| 240 | ), |
| 241 | Effect.provide(NodeServices.layer), |
| 242 | Effect.provide(Interaction.layerNonInteractive()), |
| 243 | ), |
| 244 | ), |
| 245 | ); |
| 246 | expect(Result.isFailure(result)).toBe(true); |
| 247 | if (Result.isFailure(result)) { |
| 248 | expect(String(result.failure)).toContain("does not exist"); |
| 249 | expect(String(result.failure)).toContain("alchemy profile create"); |
| 250 | } |
| 251 | }), |
| 252 | ); |
| 253 | |
| 254 | it.effect( |
| 255 | "tails deployment logs from a providers()-shaped stack context", |
| 256 | () => |
| 257 | Effect.gen(function* () { |
| 258 | const provider = yield* Provider.findProviderByType(Prisma.Deployment.Type as any); |
| 259 | |
| 260 | // The tail stream must resolve everything it needs from the context |
| 261 | // `providers()` produces. Point it at a closed loopback port so the |
| 262 | // WebSocket fails fast: a typed PrismaLogStreamError proves the |
| 263 | // context was complete, while a missing service surfaces as a defect. |
| 264 | const error = yield* Stream.runDrain( |
| 265 | provider.tail!({ output: { deploymentId: "deployment-1" } } as never), |
| 266 | ).pipe(Effect.flip); |
| 267 | |
| 268 | expect(error).toBeInstanceOf(PrismaLogStreamError); |
| 269 | }).pipe( |
| 270 | Effect.provide( |
| 271 | Prisma.providers().pipe( |
| 272 | Layer.provideMerge(AuthProvidersLive), |
| 273 | Layer.provide(stackProvidedServices), |
| 274 | ), |
| 275 | ), |
| 276 | Effect.provide( |
| 277 | Layer.succeed(AlchemyContext, { dotAlchemy: ".alchemy-test", dev: false, adopt: false }), |
| 278 | ), |
| 279 | Effect.provide( |
| 280 | ConfigProvider.layer( |
| 281 | ConfigProvider.fromUnknown({ |
| 282 | CI: true, |
| 283 | PRISMA_SERVICE_TOKEN: "test-token", |
| 284 | PRISMA_API_URL: "http://127.0.0.1:1", |
| 285 | }), |
| 286 | ), |
| 287 | ), |
| 288 | ), |
| 289 | { tags: ["provider:prisma:deployment"], timeout: 30_000 }, |
| 290 | ); |
| 291 | }); |