The best way to control Claude Code, Codex and more from any computer or phone

main
ts 149 lines 5.6 KB
Raw
1 import { defineContract, enumType, field, member, model } from "@alchemy.run/prisma/ORM";
2 import { emitSchemas, makeSchemas, SchemaError } from "@alchemy.run/prisma/ORM/Schema";
3 import { expect, it } from "@alchemy.run/test";
4 import * as Effect from "effect/Effect";
5 import * as Schema from "effect/Schema";
6 import { contract } from "./fixtures/client/contract.ts";
7
8 it.effect(
9 "derives row schemas without a client and preserves nullable fields",
10 () =>
11 Effect.gen(function* () {
12 const schemas = makeSchemas(contract);
13 const row = yield* Schema.decodeUnknownEffect(schemas.public.User)({
14 id: 1,
15 email: "a@example.com",
16 name: null,
17 });
18 expect(row).toEqual({ id: 1, email: "a@example.com", name: null });
19 expect(Schema.is(schemas.public.User)({ id: "1", email: "a@example.com", name: null })).toBe(
20 false,
21 );
22 expect(Schema.is(schemas.public.User)({ id: 1, email: "a@example.com" })).toBe(false);
23 expect(Schema.is(schemas.public.User)({ id: 1.5, email: "a@example.com", name: null })).toBe(
24 false,
25 );
26 }),
27 { tags: ["unit", "provider:prisma", "provider:prisma:orm", "local"] },
28 );
29
30 it.effect(
31 "maps lists and namespaces without including navigation relations",
32 () =>
33 Effect.sync(() => {
34 const contract = defineContract({ namespaces: ["auth"] }, ({ field, model }) => ({
35 models: {
36 Account: model("Account", {
37 namespace: "auth",
38 fields: {
39 id: field.int().id(),
40 tags: field.text().many(),
41 name: field.text().optional(),
42 },
43 }),
44 },
45 }));
46 const schemas = makeSchemas(contract);
47 expect(Schema.is(schemas.auth.Account)({ id: 1, tags: ["a"], name: null })).toBe(true);
48 expect(Schema.is(schemas.auth.Account)({ id: 1, tags: [1], name: null })).toBe(false);
49 expect(emitSchemas(contract)).not.toContain("Postgres");
50 expect(emitSchemas(contract)).not.toContain("@prisma");
51 }),
52 { tags: ["unit", "provider:prisma", "provider:prisma:orm", "local"] },
53 );
54
55 it.effect(
56 "preserves enums, bigint, decimals, bytes, temporal strings, and JSON",
57 () =>
58 Effect.sync(() => {
59 const Status = enumType(
60 "Status",
61 { codecId: "pg/text@1", nativeType: "text" },
62 member("Active", "active"),
63 member("Inactive", "inactive"),
64 );
65 const contract = defineContract({
66 enums: { Status },
67 models: {
68 Value: model("Value", {
69 fields: {
70 id: field.column({ codecId: "pg/int8@1", nativeType: "int8" }).id(),
71 decimal: field.column({ codecId: "pg/numeric@1", nativeType: "numeric" }),
72 bytes: field.column({ codecId: "pg/bytea@1", nativeType: "bytea" }),
73 uuid: field.column({ codecId: "pg/uuid@1", nativeType: "uuid" }),
74 date: field.column({ codecId: "pg/date-string@1", nativeType: "date" }),
75 json: field.column({ codecId: "pg/jsonb@1", nativeType: "jsonb" }),
76 status: field.namedType(Status),
77 },
78 }),
79 },
80 });
81 const schema = makeSchemas(contract).public.Value;
82 const row = {
83 id: 9007199254740993n,
84 decimal: "12345678901234567890.123456",
85 bytes: new Uint8Array([1]),
86 date: "2026-09-18",
87 uuid: "00000000-0000-0000-0000-000000000001",
88 json: { nested: [null, 1, true] },
89 status: "active",
90 };
91 expect(Schema.is(schema)(row)).toBe(true);
92 expect(Schema.is(schema)({ ...row, id: 1 })).toBe(false);
93 expect(Schema.is(schema)({ ...row, decimal: 1.5 })).toBe(false);
94 expect(Schema.is(schema)({ ...row, decimal: "invalid" })).toBe(false);
95 expect(Schema.is(schema)({ ...row, json: { invalid: undefined } })).toBe(false);
96 expect(Schema.is(schema)({ ...row, status: "missing" })).toBe(false);
97 expect(emitSchemas(contract)).toContain('Schema.Literals(["active","inactive"])');
98 }),
99 { tags: ["unit", "provider:prisma", "provider:prisma:orm", "local"] },
100 );
101
102 it.effect(
103 "emits deterministic standalone expressions for the same contract",
104 () =>
105 Effect.sync(() => {
106 const first = emitSchemas(contract);
107 expect(first).toBe(emitSchemas(contract));
108 expect(first).toContain('"name": Schema.NullOr(Schema.String)');
109 expect(first).not.toContain('"posts":');
110 expect(first).not.toContain('"author":');
111 }),
112 { tags: ["unit", "provider:prisma", "provider:prisma:orm", "local"] },
113 );
114
115 it.effect(
116 "rejects unsupported codecs instead of silently accepting unknown rows",
117 () =>
118 Effect.sync(() => {
119 const custom = {
120 ...contract,
121 domain: {
122 namespaces: {
123 public: {
124 models: {
125 Custom: {
126 fields: {
127 id: {
128 nullable: false,
129 type: { kind: "scalar" as const, codecId: "custom/id@1" },
130 },
131 },
132 relations: {},
133 storage: {},
134 },
135 },
136 },
137 },
138 },
139 };
140 expect(() => makeSchemas(custom)).toThrow(SchemaError);
141 expect(() => emitSchemas(custom)).toThrow('options.codecs["custom/id@1"]');
142 const options = {
143 codecs: { "custom/id@1": { schema: Schema.String, expression: "Schema.String" } },
144 };
145 expect(Schema.is(makeSchemas(custom, options).public.Custom)({ id: "custom" })).toBe(true);
146 expect(emitSchemas(custom, options)).toContain('"id": Schema.String');
147 }),
148 { tags: ["unit", "provider:prisma", "provider:prisma:orm", "local"] },
149 );