The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Path from "effect/Path"; |
| 6 | import * as ts from "tsrs/unstable/ast"; |
| 7 | import { API } from "tsrs/unstable/async"; |
| 8 | import { createFileSystemLayer } from "tsrs/unstable/fs"; |
| 9 | |
| 10 | const forbiddenPatterns = [ |
| 11 | { |
| 12 | name: "raw filesystem/path/os imports", |
| 13 | pattern: /\bfrom\s+["'](?:node:fs|node:fs\/promises|node:path|node:os|pathe)["']/, |
| 14 | }, |
| 15 | { name: "async/await", pattern: /(?:^|[^\w.])(?:async|await)\b/ }, |
| 16 | { name: "Effect.promise", pattern: /\bEffect\.promise\b/ }, |
| 17 | { name: "raw Promise constructor", pattern: /\bnew\s+Promise\b/ }, |
| 18 | { name: "raw fetch", pattern: /\bfetch\s*\(/ }, |
| 19 | { name: "Effect.orDie", pattern: /\bEffect\.orDie\b/ }, |
| 20 | { |
| 21 | name: "legacy create/update lifecycle handlers", |
| 22 | pattern: /\b(?:create|update)\s*:\s*Effect\.fn\b/, |
| 23 | }, |
| 24 | { |
| 25 | name: "explicit output undefined create/update branch", |
| 26 | pattern: /\b(?:output\s*(?:===|!==)\s*undefined|undefined\s*(?:===|!==)\s*output)\b/, |
| 27 | }, |
| 28 | { name: "bare process.cwd()", pattern: /\bprocess\.cwd\(\)/ }, |
| 29 | { name: "explicit any", pattern: /\bany\b/ }, |
| 30 | { name: "double unknown cast", pattern: /\bas\s+unknown\s+as\b/ }, |
| 31 | ] as const; |
| 32 | |
| 33 | const documentedResources = [ |
| 34 | "Branch", |
| 35 | "Bucket", |
| 36 | "BucketAccessKey", |
| 37 | "Compute", |
| 38 | "App", |
| 39 | "Deployment", |
| 40 | "Connection", |
| 41 | "CustomDomain", |
| 42 | "Database", |
| 43 | "EnvironmentVariable", |
| 44 | "Project", |
| 45 | "SourceRepository", |
| 46 | ] as const; |
| 47 | |
| 48 | const resourceConfigInterfaces = { |
| 49 | Compute: ["ComputeBuild", "ComputeDev", "ComputeProps", "ComputeStaticBuild"], |
| 50 | Connection: ["ConnectionEnvOptions"], |
| 51 | Database: ["DatabaseDev"], |
| 52 | } as const; |
| 53 | |
| 54 | // These are interop boundaries, not lifecycle implementation shortcuts. Keep |
| 55 | // exceptions scoped to both a file and a rule so other conventions still apply. |
| 56 | const boundaryPatterns: Record<string, ReadonlyArray<string>> = { |
| 57 | "internal/ArchivePlatform.ts": ["raw filesystem/path/os imports", "async/await"], |
| 58 | "internal/ArtifactFile.ts": ["raw filesystem/path/os imports", "async/await"], |
| 59 | // Resolve user paths against the cwd at invocation time, not module import. |
| 60 | "ComputeArchive.ts": ["bare process.cwd()"], |
| 61 | "ORM/Contract.ts": ["bare process.cwd()"], |
| 62 | "ORM/Migrate.ts": ["bare process.cwd()"], |
| 63 | // Prisma's native authoring function is retyped to preserve its generic API. |
| 64 | "ORM/ContractBuilder.ts": ["double unknown cast"], |
| 65 | // The fluent runtime proxy erases intermediate native query-builder types. |
| 66 | "ORM/OrmClient.ts": ["explicit any"], |
| 67 | // Native Promise transactions and close/rollback finalizers cross into Effect. |
| 68 | "ORM/Postgres.ts": ["async/await", "Effect.promise"], |
| 69 | }; |
| 70 | |
| 71 | const stripStringsAndComments = (source: string) => |
| 72 | source |
| 73 | .replaceAll(/`(?:\\.|[^`])*`/gs, "``") |
| 74 | .replaceAll(/"(?:\\.|[^"])*"/g, '""') |
| 75 | .replaceAll(/'(?:\\.|[^'])*'/g, "''") |
| 76 | .replaceAll(/\/\*[\s\S]*?\*\//g, "") |
| 77 | .replaceAll(/\/\/.*$/gm, ""); |
| 78 | |
| 79 | const prismaSourceProject = Effect.gen(function* () { |
| 80 | const path = yield* Path.Path; |
| 81 | const sourceRoot = path.resolve(import.meta.dirname, "../../../prisma/src"); |
| 82 | const configPath = path.join(sourceRoot, "tsconfig.source-conventions.json"); |
| 83 | const config = JSON.stringify({ |
| 84 | files: documentedResources.map((resource) => path.join(sourceRoot, `${resource}.ts`)), |
| 85 | compilerOptions: { noResolve: true, noLib: true, types: [] }, |
| 86 | }); |
| 87 | const api = yield* Effect.acquireRelease( |
| 88 | Effect.sync(() => new API()), |
| 89 | (api) => Effect.promise(() => api.close()), |
| 90 | ); |
| 91 | const snapshot = yield* Effect.tryPromise(() => |
| 92 | api.createSnapshot({ |
| 93 | openProjects: [configPath], |
| 94 | fileSystem: createFileSystemLayer([[configPath, config]]), |
| 95 | }), |
| 96 | ); |
| 97 | const project = snapshot.getConfiguredProject(configPath); |
| 98 | if (!project) throw new Error(`Missing project ${configPath}`); |
| 99 | return project.program; |
| 100 | }); |
| 101 | |
| 102 | describe("Prisma source conventions", { tags: ["unit", "provider:prisma", "local"] }, () => { |
| 103 | it.effect("keeps provider source in Effect-style lifecycle conventions", () => |
| 104 | Effect.gen(function* () { |
| 105 | const fs = yield* FileSystem.FileSystem; |
| 106 | const path = yield* Path.Path; |
| 107 | const sourceRoot = path.resolve(import.meta.dirname, "../../../prisma/src"); |
| 108 | const files = (yield* fs.readDirectory(sourceRoot, { recursive: true })) |
| 109 | .filter((file) => file.endsWith(".ts")) |
| 110 | .sort(); |
| 111 | |
| 112 | const violations: string[] = []; |
| 113 | for (const file of files) { |
| 114 | const fullPath = path.join(sourceRoot, file); |
| 115 | const source = yield* fs.readFileString(fullPath); |
| 116 | for (const { name, pattern } of forbiddenPatterns) { |
| 117 | if (boundaryPatterns[file]?.includes(name)) continue; |
| 118 | const scannedSource = |
| 119 | name === "raw filesystem/path/os imports" ? source : stripStringsAndComments(source); |
| 120 | const match = pattern.exec(scannedSource); |
| 121 | if (match) { |
| 122 | violations.push(`${file}: ${name}: ${match[0]}`); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | expect(violations).toEqual([]); |
| 128 | }).pipe(Effect.provide(NodeServices.layer)), |
| 129 | ); |
| 130 | |
| 131 | it.effect("keeps Prisma resources ready for generated API docs", () => |
| 132 | Effect.gen(function* () { |
| 133 | const fs = yield* FileSystem.FileSystem; |
| 134 | const path = yield* Path.Path; |
| 135 | const sourceRoot = path.resolve(import.meta.dirname, "../../../prisma/src"); |
| 136 | |
| 137 | for (const resource of documentedResources) { |
| 138 | const source = yield* fs.readFileString(path.join(sourceRoot, `${resource}.ts`)); |
| 139 | expect(source).toContain(`export interface ${resource}Props`); |
| 140 | const constructorPattern = |
| 141 | resource === "Compute" |
| 142 | ? `export const ${resource}: Platform<` |
| 143 | : `export const ${resource} = Resource<`; |
| 144 | expect(source).toContain(constructorPattern); |
| 145 | |
| 146 | const resourceDoc = source.match( |
| 147 | new RegExp( |
| 148 | resource === "Compute" |
| 149 | ? `/\\*\\*[\\s\\S]*?\\*/\\s*export const ${resource}: Platform<` |
| 150 | : `/\\*\\*[\\s\\S]*?\\*/\\s*export const ${resource} = Resource<`, |
| 151 | ), |
| 152 | )?.[0]; |
| 153 | if (resourceDoc === undefined) { |
| 154 | throw new Error(`${resource} is missing resource JSDoc`); |
| 155 | } |
| 156 | // `docs:fix-jsdoc` rewrites `@section` / `@example` into markdown |
| 157 | // headings (`###` / `**Example:**`). Source of truth is the |
| 158 | // rewritten form — see scripts/fix-api-jsdocs.ts. |
| 159 | expect(resourceDoc).toContain("### "); |
| 160 | expect(resourceDoc).toContain("**Example:**"); |
| 161 | } |
| 162 | }).pipe(Effect.provide(NodeServices.layer)), |
| 163 | ); |
| 164 | |
| 165 | it.effect("documents public Prisma resource props and attributes", () => |
| 166 | Effect.gen(function* () { |
| 167 | const path = yield* Path.Path; |
| 168 | const sourceRoot = path.resolve(import.meta.dirname, "../../../prisma/src"); |
| 169 | const program = yield* prismaSourceProject; |
| 170 | const missingDocs: string[] = []; |
| 171 | |
| 172 | for (const resource of documentedResources) { |
| 173 | const fileName = `${resource}.ts`; |
| 174 | const sourceFile = yield* Effect.tryPromise(() => |
| 175 | program.getSourceFile(path.join(sourceRoot, fileName)), |
| 176 | ); |
| 177 | if (!sourceFile) throw new Error(`Missing source file ${fileName}`); |
| 178 | const configInterfaces = [ |
| 179 | `${resource}Props`, |
| 180 | ...(resourceConfigInterfaces[resource as keyof typeof resourceConfigInterfaces] ?? []), |
| 181 | ]; |
| 182 | |
| 183 | for (const interfaceName of configInterfaces) { |
| 184 | const declaration = sourceFile.statements |
| 185 | .filter(ts.isInterfaceDeclaration) |
| 186 | .find((node) => node.name.text === interfaceName); |
| 187 | if (declaration === undefined) continue; |
| 188 | for (const property of declaration.members.filter(ts.isPropertySignatureDeclaration)) { |
| 189 | if (!property.jsDoc?.length) { |
| 190 | missingDocs.push(`${fileName}:${interfaceName}.${property.name.getText()}`); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | const resourceDeclaration = sourceFile.statements |
| 196 | .filter(ts.isInterfaceDeclaration) |
| 197 | .find((node) => node.name.text === resource); |
| 198 | const attrs = resourceDeclaration?.heritageClauses?.find( |
| 199 | (clause) => clause.token === ts.SyntaxKind.ExtendsKeyword, |
| 200 | )?.types[0]?.typeArguments?.[2]; |
| 201 | const attributes = attrs && ts.isTypeLiteralNode(attrs) ? attrs : undefined; |
| 202 | if (attributes === undefined) continue; |
| 203 | for (const property of attributes.members.filter(ts.isPropertySignatureDeclaration)) { |
| 204 | if (!property.jsDoc?.length) { |
| 205 | missingDocs.push(`${fileName}:${resource}.Attributes.${property.name.getText()}`); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | expect(missingDocs).toEqual([]); |
| 211 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 212 | ); |
| 213 | |
| 214 | it.effect("keeps raw artifact bytes out of persisted resource props", () => |
| 215 | Effect.gen(function* () { |
| 216 | const path = yield* Path.Path; |
| 217 | const sourceRoot = path.resolve(import.meta.dirname, "../../../prisma/src"); |
| 218 | const program = yield* prismaSourceProject; |
| 219 | |
| 220 | for (const resource of ["Compute", "Deployment"] as const) { |
| 221 | const sourceFile = yield* Effect.tryPromise(() => |
| 222 | program.getSourceFile(path.join(sourceRoot, `${resource}.ts`)), |
| 223 | ); |
| 224 | if (!sourceFile) throw new Error(`Missing source file ${resource}.ts`); |
| 225 | const props = sourceFile.statements |
| 226 | .filter(ts.isInterfaceDeclaration) |
| 227 | .find((node) => node.name.text === `${resource}Props`); |
| 228 | const properties = props?.members |
| 229 | .filter(ts.isPropertySignatureDeclaration) |
| 230 | .map((node) => node.name.getText()); |
| 231 | expect(properties).not.toContain("artifact"); |
| 232 | expect(properties).toContain("artifactPath"); |
| 233 | } |
| 234 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 235 | ); |
| 236 | |
| 237 | it.effect("models deployment environment values as redaction markers", () => |
| 238 | Effect.gen(function* () { |
| 239 | const fs = yield* FileSystem.FileSystem; |
| 240 | const path = yield* Path.Path; |
| 241 | const source = yield* fs.readFileString( |
| 242 | path.resolve(import.meta.dirname, "../../../prisma/src/Types.ts"), |
| 243 | ); |
| 244 | |
| 245 | expect(source).toContain('export type RedactedDeploymentEnvironmentValue = "[redacted]"'); |
| 246 | expect(source).toContain("envVars?: Record<string, RedactedDeploymentEnvironmentValue>"); |
| 247 | }).pipe(Effect.provide(NodeServices.layer)), |
| 248 | ); |
| 249 | }); |