@prisma-next/emitter 0.3.0-dev.2 → 0.3.0-dev.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,274 @@
1
+ import { contractIR, irHeader, irMeta } from '@prisma-next/contract/ir';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ describe('emitter factories', () => {
5
+ describe('irHeader', () => {
6
+ it('creates header with required fields', () => {
7
+ const header = irHeader({
8
+ target: 'postgres',
9
+ targetFamily: 'sql',
10
+ coreHash: 'sha256:abc123',
11
+ });
12
+ expect(header).toEqual({
13
+ schemaVersion: '1',
14
+ target: 'postgres',
15
+ targetFamily: 'sql',
16
+ coreHash: 'sha256:abc123',
17
+ });
18
+ });
19
+
20
+ it('creates header with profileHash', () => {
21
+ const header = irHeader({
22
+ target: 'postgres',
23
+ targetFamily: 'sql',
24
+ coreHash: 'sha256:abc123',
25
+ profileHash: 'sha256:def456',
26
+ });
27
+ expect(header).toEqual({
28
+ schemaVersion: '1',
29
+ target: 'postgres',
30
+ targetFamily: 'sql',
31
+ coreHash: 'sha256:abc123',
32
+ profileHash: 'sha256:def456',
33
+ });
34
+ });
35
+
36
+ it('creates header for different target families', () => {
37
+ const header = irHeader({
38
+ target: 'mongodb',
39
+ targetFamily: 'document',
40
+ coreHash: 'sha256:xyz789',
41
+ });
42
+ expect(header.targetFamily).toBe('document');
43
+ expect(header.target).toBe('mongodb');
44
+ });
45
+ });
46
+
47
+ describe('irMeta', () => {
48
+ it('creates empty meta when no options provided', () => {
49
+ const meta = irMeta({});
50
+ expect(meta).toEqual({
51
+ capabilities: {},
52
+ extensionPacks: {},
53
+ meta: {},
54
+ sources: {},
55
+ });
56
+ });
57
+
58
+ it('creates meta with capabilities', () => {
59
+ const meta = irMeta({
60
+ capabilities: {
61
+ postgres: {
62
+ returning: true,
63
+ lateral: true,
64
+ },
65
+ },
66
+ });
67
+ expect(meta.capabilities).toEqual({
68
+ postgres: {
69
+ returning: true,
70
+ lateral: true,
71
+ },
72
+ });
73
+ expect(meta.extensionPacks).toEqual({});
74
+ expect(meta.meta).toEqual({});
75
+ expect(meta.sources).toEqual({});
76
+ });
77
+
78
+ it('creates meta with extension packs', () => {
79
+ const meta = irMeta({
80
+ extensionPacks: {
81
+ postgres: {
82
+ id: 'postgres',
83
+ version: '0.0.1',
84
+ },
85
+ },
86
+ });
87
+ expect(meta.extensionPacks).toEqual({
88
+ postgres: {
89
+ id: 'postgres',
90
+ version: '0.0.1',
91
+ },
92
+ });
93
+ });
94
+
95
+ it('creates meta with custom meta', () => {
96
+ const meta = irMeta({
97
+ meta: {
98
+ generated: true,
99
+ timestamp: '2024-01-01T00:00:00Z',
100
+ },
101
+ });
102
+ expect(meta.meta).toEqual({
103
+ generated: true,
104
+ timestamp: '2024-01-01T00:00:00Z',
105
+ });
106
+ });
107
+
108
+ it('creates meta with sources', () => {
109
+ const meta = irMeta({
110
+ sources: {
111
+ userView: {
112
+ kind: 'view',
113
+ sql: 'SELECT * FROM "user"',
114
+ },
115
+ },
116
+ });
117
+ expect(meta.sources).toEqual({
118
+ userView: {
119
+ kind: 'view',
120
+ sql: 'SELECT * FROM "user"',
121
+ },
122
+ });
123
+ });
124
+
125
+ it('creates meta with all fields', () => {
126
+ const meta = irMeta({
127
+ capabilities: {
128
+ postgres: { returning: true },
129
+ },
130
+ extensionPacks: {
131
+ postgres: { id: 'postgres', version: '0.0.1' },
132
+ },
133
+ meta: { generated: true },
134
+ sources: { userView: { kind: 'view' } },
135
+ });
136
+ expect(meta.capabilities).toEqual({
137
+ postgres: { returning: true },
138
+ });
139
+ expect(meta.extensionPacks).toEqual({
140
+ postgres: { id: 'postgres', version: '0.0.1' },
141
+ });
142
+ expect(meta.meta).toEqual({ generated: true });
143
+ expect(meta.sources).toEqual({ userView: { kind: 'view' } });
144
+ });
145
+ });
146
+
147
+ describe('contractIR', () => {
148
+ it('creates complete ContractIR from header, meta, and family sections', () => {
149
+ const header = irHeader({
150
+ target: 'postgres',
151
+ targetFamily: 'sql',
152
+ coreHash: 'sha256:abc123',
153
+ });
154
+ const meta = irMeta({
155
+ capabilities: {
156
+ postgres: { returning: true },
157
+ },
158
+ });
159
+ const storage = { tables: { user: { columns: {} } } };
160
+ const models = { User: { storage: { table: 'user' }, fields: {} } };
161
+ const relations = {};
162
+
163
+ const ir = contractIR({
164
+ header,
165
+ meta,
166
+ storage,
167
+ models,
168
+ relations,
169
+ });
170
+
171
+ expect(ir.schemaVersion).toBe('1');
172
+ expect(ir.target).toBe('postgres');
173
+ expect(ir.targetFamily).toBe('sql');
174
+ // Note: coreHash is not part of ContractIR (it's computed by emitter)
175
+ expect(ir.storage).toEqual(storage);
176
+ expect(ir.models).toEqual(models);
177
+ expect(ir.relations).toEqual(relations);
178
+ expect(ir.capabilities).toEqual({
179
+ postgres: { returning: true },
180
+ });
181
+ expect(ir.extensionPacks).toEqual({});
182
+ expect(ir.meta).toEqual({});
183
+ expect(ir.sources).toEqual({});
184
+ });
185
+
186
+ it('creates ContractIR with profileHash in header', () => {
187
+ const header = irHeader({
188
+ target: 'postgres',
189
+ targetFamily: 'sql',
190
+ coreHash: 'sha256:abc123',
191
+ profileHash: 'sha256:def456',
192
+ });
193
+ const meta = irMeta({});
194
+ const storage = { tables: {} };
195
+ const models = {};
196
+ const relations = {};
197
+
198
+ const ir = contractIR({
199
+ header,
200
+ meta,
201
+ storage,
202
+ models,
203
+ relations,
204
+ });
205
+
206
+ // Note: profileHash is not part of ContractIR (it's computed by emitter)
207
+ // The header contains it, but it's not included in the ContractIR
208
+ expect(header.profileHash).toBe('sha256:def456');
209
+ expect(ir.target).toBe('postgres');
210
+ });
211
+
212
+ it('creates ContractIR with all meta fields', () => {
213
+ const header = irHeader({
214
+ target: 'postgres',
215
+ targetFamily: 'sql',
216
+ coreHash: 'sha256:abc123',
217
+ });
218
+ const meta = irMeta({
219
+ capabilities: {
220
+ postgres: { returning: true },
221
+ },
222
+ extensionPacks: {
223
+ postgres: { id: 'postgres', version: '0.0.1' },
224
+ },
225
+ meta: { generated: true },
226
+ sources: { userView: { kind: 'view' } },
227
+ });
228
+ const storage = { tables: {} };
229
+ const models = {};
230
+ const relations = {};
231
+
232
+ const ir = contractIR({
233
+ header,
234
+ meta,
235
+ storage,
236
+ models,
237
+ relations,
238
+ });
239
+
240
+ expect(ir.capabilities).toEqual({
241
+ postgres: { returning: true },
242
+ });
243
+ expect(ir.extensionPacks).toEqual({
244
+ postgres: { id: 'postgres', version: '0.0.1' },
245
+ });
246
+ expect(ir.meta).toEqual({ generated: true });
247
+ expect(ir.sources).toEqual({ userView: { kind: 'view' } });
248
+ });
249
+
250
+ it('creates ContractIR for document family', () => {
251
+ const header = irHeader({
252
+ target: 'mongodb',
253
+ targetFamily: 'document',
254
+ coreHash: 'sha256:xyz789',
255
+ });
256
+ const meta = irMeta({});
257
+ const storage = { document: { collections: {} } };
258
+ const models = {};
259
+ const relations = {};
260
+
261
+ const ir = contractIR({
262
+ header,
263
+ meta,
264
+ storage,
265
+ models,
266
+ relations,
267
+ });
268
+
269
+ expect(ir.targetFamily).toBe('document');
270
+ expect(ir.target).toBe('mongodb');
271
+ expect(ir.storage).toEqual(storage);
272
+ });
273
+ });
274
+ });
@@ -0,0 +1,59 @@
1
+ import { computeCoreHash, computeProfileHash } from '@prisma-next/core-control-plane/emission';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ describe('hashing', () => {
5
+ it('computes core hash', () => {
6
+ const contract = {
7
+ schemaVersion: '1',
8
+ targetFamily: 'sql',
9
+ target: 'postgres',
10
+ models: {},
11
+ relations: {},
12
+ storage: { tables: {} },
13
+ extensionPacks: {},
14
+ capabilities: {},
15
+ meta: {},
16
+ sources: {},
17
+ };
18
+
19
+ const hash = computeCoreHash(contract);
20
+ expect(hash).toMatch(/^sha256:[a-f0-9]{64}$/);
21
+ });
22
+
23
+ it('computes profile hash', () => {
24
+ const contract = {
25
+ schemaVersion: '1',
26
+ targetFamily: 'sql',
27
+ target: 'postgres',
28
+ models: {},
29
+ relations: {},
30
+ storage: { tables: {} },
31
+ extensionPacks: {},
32
+ capabilities: { postgres: { jsonAgg: true } },
33
+ meta: {},
34
+ sources: {},
35
+ };
36
+
37
+ const hash = computeProfileHash(contract);
38
+ expect(hash).toMatch(/^sha256:[a-f0-9]{64}$/);
39
+ });
40
+
41
+ it('produces stable hashes for identical input', () => {
42
+ const contract = {
43
+ schemaVersion: '1',
44
+ targetFamily: 'sql',
45
+ target: 'postgres',
46
+ models: {},
47
+ relations: {},
48
+ storage: { tables: {} },
49
+ extensionPacks: {},
50
+ capabilities: {},
51
+ meta: {},
52
+ sources: {},
53
+ };
54
+
55
+ const hash1 = computeCoreHash(contract);
56
+ const hash2 = computeCoreHash(contract);
57
+ expect(hash1).toBe(hash2);
58
+ });
59
+ });
package/test/utils.ts ADDED
@@ -0,0 +1,166 @@
1
+ import { type ContractIR, irHeader, irMeta } from '@prisma-next/contract/ir';
2
+
3
+ /**
4
+ * Factory function for creating ContractIR objects in tests.
5
+ * Provides sensible defaults and allows overriding specific fields.
6
+ * Uses the emitter factories internally for consistency.
7
+ *
8
+ * If a field is explicitly set to `undefined` in overrides, it will be omitted
9
+ * from the result (useful for testing validation of missing fields).
10
+ */
11
+ export function createContractIR(
12
+ overrides: Partial<ContractIR> & { coreHash?: string; profileHash?: string } = {},
13
+ ): ContractIR {
14
+ // Check if fields are explicitly undefined (not just missing)
15
+ const hasTarget = 'target' in overrides;
16
+ const hasTargetFamily = 'targetFamily' in overrides;
17
+ const hasCoreHash = 'coreHash' in overrides;
18
+ const hasSchemaVersion = 'schemaVersion' in overrides;
19
+ const hasModels = 'models' in overrides;
20
+ const hasRelations = 'relations' in overrides;
21
+ const hasStorage = 'storage' in overrides;
22
+ const hasCapabilities = 'capabilities' in overrides;
23
+ const hasExtensionPacks = 'extensionPacks' in overrides;
24
+ const hasMeta = 'meta' in overrides;
25
+ const hasSources = 'sources' in overrides;
26
+
27
+ // Build header, omitting fields that are explicitly undefined
28
+ const headerOpts: {
29
+ target?: string;
30
+ targetFamily?: string;
31
+ coreHash?: string;
32
+ profileHash?: string;
33
+ } = {};
34
+
35
+ if (hasTarget && overrides.target !== undefined) {
36
+ headerOpts.target = overrides.target;
37
+ } else if (!hasTarget) {
38
+ headerOpts.target = 'postgres';
39
+ }
40
+
41
+ if (hasTargetFamily && overrides.targetFamily !== undefined) {
42
+ headerOpts.targetFamily = overrides.targetFamily;
43
+ } else if (!hasTargetFamily) {
44
+ headerOpts.targetFamily = 'sql';
45
+ }
46
+
47
+ if (hasCoreHash && overrides.coreHash !== undefined) {
48
+ headerOpts.coreHash = overrides.coreHash;
49
+ } else if (!hasCoreHash) {
50
+ headerOpts.coreHash = 'sha256:test';
51
+ }
52
+
53
+ // profileHash is not part of ContractIR, but we can accept it for header creation
54
+ if (overrides.profileHash !== undefined) {
55
+ headerOpts.profileHash = overrides.profileHash;
56
+ }
57
+
58
+ const header = irHeader(
59
+ headerOpts as {
60
+ target: string;
61
+ targetFamily: string;
62
+ coreHash: string;
63
+ profileHash?: string;
64
+ },
65
+ );
66
+
67
+ // Build meta, handling explicitly undefined fields
68
+ // If a field is explicitly undefined, we'll omit it from the result later
69
+ const metaOpts: {
70
+ capabilities?: Record<string, Record<string, boolean>>;
71
+ extensionPacks?: Record<string, unknown>;
72
+ meta?: Record<string, unknown>;
73
+ sources?: Record<string, unknown>;
74
+ } = {};
75
+
76
+ if (hasCapabilities && overrides.capabilities !== undefined) {
77
+ metaOpts.capabilities = overrides.capabilities;
78
+ } else if (!hasCapabilities) {
79
+ metaOpts.capabilities = {};
80
+ }
81
+
82
+ if (hasExtensionPacks && overrides.extensionPacks !== undefined) {
83
+ metaOpts.extensionPacks = overrides.extensionPacks;
84
+ } else if (!hasExtensionPacks) {
85
+ metaOpts.extensionPacks = {};
86
+ }
87
+
88
+ if (hasMeta && overrides.meta !== undefined) {
89
+ metaOpts.meta = overrides.meta;
90
+ } else if (!hasMeta) {
91
+ metaOpts.meta = {};
92
+ }
93
+
94
+ if (hasSources && overrides.sources !== undefined) {
95
+ metaOpts.sources = overrides.sources;
96
+ } else if (!hasSources) {
97
+ metaOpts.sources = {};
98
+ }
99
+
100
+ const meta = irMeta(Object.keys(metaOpts).length > 0 ? metaOpts : undefined);
101
+
102
+ // Build result by constructing the object directly (ContractIR doesn't include coreHash/profileHash)
103
+ // When fields are explicitly undefined, include them as undefined (tests use type assertions to bypass TS)
104
+ const result = {
105
+ schemaVersion:
106
+ hasSchemaVersion && overrides.schemaVersion !== undefined
107
+ ? overrides.schemaVersion
108
+ : hasSchemaVersion && overrides.schemaVersion === undefined
109
+ ? (undefined as unknown as string)
110
+ : header.schemaVersion,
111
+ target: header.target,
112
+ targetFamily: header.targetFamily,
113
+ // Only include meta fields if they're not explicitly undefined
114
+ capabilities:
115
+ hasCapabilities && overrides.capabilities === undefined
116
+ ? (undefined as unknown as Record<string, Record<string, boolean>>)
117
+ : !hasCapabilities || overrides.capabilities !== undefined
118
+ ? meta.capabilities
119
+ : ({} as Record<string, Record<string, boolean>>),
120
+ extensionPacks:
121
+ hasExtensionPacks && overrides.extensionPacks === undefined
122
+ ? (undefined as unknown as Record<string, unknown>)
123
+ : !hasExtensionPacks || overrides.extensionPacks !== undefined
124
+ ? meta.extensionPacks
125
+ : ({} as Record<string, unknown>),
126
+ meta:
127
+ hasMeta && overrides.meta === undefined
128
+ ? (undefined as unknown as Record<string, unknown>)
129
+ : !hasMeta || overrides.meta !== undefined
130
+ ? meta.meta
131
+ : ({} as Record<string, unknown>),
132
+ sources:
133
+ hasSources && overrides.sources === undefined
134
+ ? (undefined as unknown as Record<string, unknown>)
135
+ : !hasSources || overrides.sources !== undefined
136
+ ? meta.sources
137
+ : ({} as Record<string, unknown>),
138
+ // Only include family sections if they're not explicitly undefined
139
+ storage:
140
+ hasStorage && overrides.storage === undefined
141
+ ? (undefined as unknown as Record<string, unknown>)
142
+ : hasStorage && overrides.storage !== undefined
143
+ ? (overrides.storage as Record<string, unknown>)
144
+ : !hasStorage
145
+ ? ({ tables: {} } as Record<string, unknown>)
146
+ : ({} as Record<string, unknown>),
147
+ models:
148
+ hasModels && overrides.models === undefined
149
+ ? (undefined as unknown as Record<string, unknown>)
150
+ : hasModels && overrides.models !== undefined
151
+ ? (overrides.models as Record<string, unknown>)
152
+ : !hasModels
153
+ ? {}
154
+ : ({} as Record<string, unknown>),
155
+ relations:
156
+ hasRelations && overrides.relations === undefined
157
+ ? (undefined as unknown as Record<string, unknown>)
158
+ : hasRelations && overrides.relations !== undefined
159
+ ? (overrides.relations as Record<string, unknown>)
160
+ : !hasRelations
161
+ ? {}
162
+ : ({} as Record<string, unknown>),
163
+ } as ContractIR;
164
+
165
+ return result;
166
+ }
@@ -1,2 +0,0 @@
1
- export { TargetFamilyHook, TypesImportSpec, ValidationContext } from '@prisma-next/contract/types';
2
- export { EmitOptions, EmitResult, emit } from '@prisma-next/core-control-plane/emission';