The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { generate } from "@alchemy.run/prisma/ORM/Generate"; |
| 2 | import { expect, layer } from "@alchemy.run/test"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import { ChildProcess } from "effect/process"; |
| 8 | import * as Result from "effect/Result"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Schema from "effect/Schema"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | |
| 13 | const describe = layer(NodeServices.layer, { excludeTestServices: true }); |
| 14 | |
| 15 | describe("Effect contract generation", (it) => { |
| 16 | it.effect( |
| 17 | "emits deterministic PSL bindings, validates rows, and removes disabled outputs", |
| 18 | () => |
| 19 | Effect.gen(function* () { |
| 20 | const fs = yield* FileSystem.FileSystem; |
| 21 | const path = yield* Path.Path; |
| 22 | const fixtures = yield* path.fromFileUrl(new URL("./fixtures", import.meta.url)); |
| 23 | const directory = yield* fs.makeTempDirectoryScoped({ |
| 24 | directory: fixtures, |
| 25 | prefix: "generate-", |
| 26 | }); |
| 27 | const source = yield* fs.readFileString(path.join(fixtures, "psl/contract.psl")); |
| 28 | yield* fs.writeFileString(path.join(directory, "contract.psl"), source); |
| 29 | const config = (client: boolean, schemas: boolean) => |
| 30 | `import { defineConfig } from "@prisma/orm-postgres/config";\nimport { definePrismaConfig } from "prisma/config";\nimport { withEffect } from "@alchemy.run/prisma/ORM/generator";\nexport default definePrismaConfig({ orm: withEffect(defineConfig({ contract: "./contract.psl", output: "./generated" }), { client: ${client}, schemas: ${schemas} }) });\n`; |
| 31 | for (const [name, client, schemas] of [ |
| 32 | ["both", true, true], |
| 33 | ["schemas", false, true], |
| 34 | ["client", true, false], |
| 35 | ] as const) { |
| 36 | yield* fs.writeFileString( |
| 37 | path.join(directory, `${name}.config.ts`), |
| 38 | config(client, schemas), |
| 39 | ); |
| 40 | } |
| 41 | const result = yield* generate(path.join(directory, "both.config.ts")); |
| 42 | const before = yield* Effect.forEach(result.files, (file) => fs.readFileString(file)); |
| 43 | yield* generate(path.join(directory, "both.config.ts")); |
| 44 | expect(yield* Effect.forEach(result.files, (file) => fs.readFileString(file))).toEqual( |
| 45 | before, |
| 46 | ); |
| 47 | expect(result.files).toHaveLength(6); |
| 48 | const schemaUrl = yield* path.toFileUrl(path.join(result.directory, "schemas.ts")); |
| 49 | const { schemas } = yield* Effect.promise(() => import(schemaUrl.href)); |
| 50 | expect( |
| 51 | Schema.is(schemas.public.User)({ |
| 52 | id: 1, |
| 53 | email: "a@example.com", |
| 54 | name: null, |
| 55 | }), |
| 56 | ).toBe(true); |
| 57 | expect( |
| 58 | Schema.is(schemas.public.User)({ |
| 59 | id: "bad", |
| 60 | email: "a@example.com", |
| 61 | name: null, |
| 62 | }), |
| 63 | ).toBe(false); |
| 64 | const standalone = yield* fs.readFileString(path.join(result.directory, "schemas.ts")); |
| 65 | expect(standalone).not.toContain('from "alchemy'); |
| 66 | expect(standalone).not.toContain("@prisma"); |
| 67 | yield* generate(path.join(directory, "schemas.config.ts")); |
| 68 | expect(yield* fs.exists(path.join(result.directory, "client.ts"))).toBe(false); |
| 69 | expect(yield* fs.exists(path.join(result.directory, "runtime.ts"))).toBe(false); |
| 70 | expect(yield* fs.readFileString(path.join(result.directory, "index.ts"))).not.toContain( |
| 71 | "makeDatabase", |
| 72 | ); |
| 73 | yield* generate(path.join(directory, "client.config.ts")); |
| 74 | expect(yield* fs.exists(path.join(result.directory, "schemas.ts"))).toBe(false); |
| 75 | expect(yield* fs.exists(path.join(result.directory, "client.ts"))).toBe(true); |
| 76 | yield* fs.writeFileString(path.join(result.directory, "schemas.ts"), "// handwritten\n"); |
| 77 | const refused = yield* generate(path.join(directory, "both.config.ts")).pipe(Effect.result); |
| 78 | expect(Result.isFailure(refused)).toBe(true); |
| 79 | expect(yield* fs.readFileString(path.join(result.directory, "schemas.ts"))).toBe( |
| 80 | "// handwritten\n", |
| 81 | ); |
| 82 | yield* fs.remove(path.join(result.directory, "schemas.ts")); |
| 83 | yield* fs.writeFileString(path.join(directory, "contract.psl"), "invalid contract"); |
| 84 | const failed = yield* generate(path.join(directory, "both.config.ts")).pipe(Effect.result); |
| 85 | expect(Result.isFailure(failed)).toBe(true); |
| 86 | expect(yield* fs.readFileString(path.join(result.directory, "contract.json"))).toBe( |
| 87 | before[result.files.indexOf(path.join(result.directory, "contract.json"))], |
| 88 | ); |
| 89 | }).pipe(Effect.scoped), |
| 90 | { |
| 91 | tags: ["unit", "provider:prisma", "provider:prisma:orm", "local"], |
| 92 | timeout: 120_000, |
| 93 | }, |
| 94 | ); |
| 95 | |
| 96 | it.effect( |
| 97 | "watch regenerates imported TypeScript changes and recovers after invalid source", |
| 98 | () => |
| 99 | Effect.gen(function* () { |
| 100 | const fs = yield* FileSystem.FileSystem; |
| 101 | const path = yield* Path.Path; |
| 102 | const fixtures = yield* path.fromFileUrl(new URL("./fixtures", import.meta.url)); |
| 103 | const directory = yield* fs.makeTempDirectoryScoped({ |
| 104 | directory: fixtures, |
| 105 | prefix: "watch-", |
| 106 | }); |
| 107 | const config = path.join(directory, "prisma.config.ts"); |
| 108 | yield* fs.writeFileString( |
| 109 | config, |
| 110 | `import { defineConfig } from "@prisma/orm-postgres/config";\nimport { definePrismaConfig } from "prisma/config";\nimport { withEffect } from "@alchemy.run/prisma/ORM/generator";\nexport default definePrismaConfig({ orm: withEffect(defineConfig({ contract: "./contract.ts", output: "./generated" })) });\n`, |
| 111 | ); |
| 112 | yield* fs.writeFileString( |
| 113 | path.join(directory, "contract.ts"), |
| 114 | `import { defineContract, field, model } from "@alchemy.run/prisma/ORM";\nimport { value } from "./fields.ts";\nexport const contract = defineContract({ models: { Item: model("Item", { fields: { id: field.column({ codecId: "pg/int4@1", nativeType: "int4" }).id(), value } }) } });\n`, |
| 115 | ); |
| 116 | const fieldModule = (codec: "text" | "int4") => |
| 117 | `import { field } from "@alchemy.run/prisma/ORM";\nexport const value = field.column({ codecId: "pg/${codec}@1", nativeType: "${codec}" });\n`; |
| 118 | const fields = path.join(directory, "fields.ts"); |
| 119 | yield* fs.writeFileString(fields, fieldModule("text")); |
| 120 | const bin = yield* path.fromFileUrl( |
| 121 | new URL("../../../../alchemy/bin/cli.js", import.meta.url), |
| 122 | ); |
| 123 | const process = yield* ChildProcess.make(globalThis.process.execPath, [ |
| 124 | bin, |
| 125 | "provider", |
| 126 | "prisma", |
| 127 | "generate", |
| 128 | "--config", |
| 129 | config, |
| 130 | "--watch", |
| 131 | "--no-input", |
| 132 | ]); |
| 133 | let output = ""; |
| 134 | yield* process.stdout.pipe( |
| 135 | Stream.decodeText(), |
| 136 | Stream.runForEach((text) => |
| 137 | Effect.sync(() => { |
| 138 | output += text; |
| 139 | }), |
| 140 | ), |
| 141 | Effect.forkScoped, |
| 142 | ); |
| 143 | yield* process.stderr.pipe( |
| 144 | Stream.decodeText(), |
| 145 | Stream.runForEach((text) => |
| 146 | Effect.sync(() => { |
| 147 | output += text; |
| 148 | }), |
| 149 | ), |
| 150 | Effect.forkScoped, |
| 151 | ); |
| 152 | const wait = (condition: () => boolean) => |
| 153 | Effect.sync(condition).pipe( |
| 154 | Effect.repeat({ |
| 155 | schedule: Schedule.spaced("1 second"), |
| 156 | until: Boolean, |
| 157 | times: 10, |
| 158 | }), |
| 159 | Effect.flatMap((ready) => Effect.sync(() => expect(ready, output).toBe(true))), |
| 160 | ); |
| 161 | yield* wait(() => output.includes("Watching")); |
| 162 | const generated = path.join(directory, "generated/schemas.ts"); |
| 163 | const waitForField = (expression: string) => |
| 164 | fs.readFileString(generated).pipe( |
| 165 | Effect.repeat({ |
| 166 | schedule: Schedule.spaced("1 second"), |
| 167 | until: (text) => text.includes(expression), |
| 168 | times: 10, |
| 169 | }), |
| 170 | Effect.tap((text) => Effect.sync(() => expect(text).toContain(expression))), |
| 171 | ); |
| 172 | yield* fs.writeFileString(fields, fieldModule("int4")); |
| 173 | yield* waitForField('"value": Schema.Int'); |
| 174 | yield* fs.writeFileString(fields, "invalid typescript"); |
| 175 | yield* wait(() => output.includes("Prisma.CliError")); |
| 176 | yield* fs.writeFileString(fields, fieldModule("text")); |
| 177 | yield* waitForField('"value": Schema.String'); |
| 178 | }).pipe(Effect.scoped), |
| 179 | { |
| 180 | tags: ["unit", "provider:prisma", "provider:prisma:orm", "local"], |
| 181 | timeout: 120_000, |
| 182 | }, |
| 183 | ); |
| 184 | }); |