|
| 1 | +import type { |
| 2 | + ExtensionPackRef, |
| 3 | + FamilyPackRef, |
| 4 | + TargetPackRef, |
| 5 | +} from '@prisma-next/framework-components/components'; |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | +import { defineContract } from '../src/contract-builder'; |
| 8 | + |
| 9 | +const sqlFamilyPack = { |
| 10 | + kind: 'family', |
| 11 | + id: 'sql', |
| 12 | + familyId: 'sql', |
| 13 | + version: '0.0.1', |
| 14 | +} as const satisfies FamilyPackRef<'sql'>; |
| 15 | + |
| 16 | +const documentFamilyPack = { |
| 17 | + kind: 'family', |
| 18 | + id: 'document', |
| 19 | + familyId: 'document', |
| 20 | + version: '0.0.1', |
| 21 | +} as const satisfies FamilyPackRef<'document'>; |
| 22 | + |
| 23 | +const postgresTargetPack = { |
| 24 | + kind: 'target', |
| 25 | + id: 'postgres', |
| 26 | + familyId: 'sql', |
| 27 | + targetId: 'postgres', |
| 28 | + version: '0.0.1', |
| 29 | +} as const satisfies TargetPackRef<'sql', 'postgres'>; |
| 30 | + |
| 31 | +const pgvectorExtensionPack = { |
| 32 | + kind: 'extension', |
| 33 | + id: 'pgvector', |
| 34 | + familyId: 'sql', |
| 35 | + targetId: 'postgres', |
| 36 | + version: '0.0.1', |
| 37 | +} as const satisfies ExtensionPackRef<'sql', 'postgres'>; |
| 38 | + |
| 39 | +const mysqlExtensionPack = { |
| 40 | + ...pgvectorExtensionPack, |
| 41 | + targetId: 'mysql', |
| 42 | +} as const satisfies ExtensionPackRef<'sql', 'mysql'>; |
| 43 | + |
| 44 | +function unsafeExtensionPackRefForRuntimeTest<FamilyId extends string, TargetId extends string>( |
| 45 | + pack: FamilyPackRef<string> | TargetPackRef<string, string> | ExtensionPackRef<string, string>, |
| 46 | +): ExtensionPackRef<FamilyId, TargetId> { |
| 47 | + // These runtime-guard tests intentionally bypass the static pack-ref contract so they can |
| 48 | + // assert the error paths for invalid inputs that well-typed authoring code cannot produce. |
| 49 | + return pack as unknown as ExtensionPackRef<FamilyId, TargetId>; |
| 50 | +} |
| 51 | + |
| 52 | +describe('defineContract runtime guards', () => { |
| 53 | + it.each([ |
| 54 | + { |
| 55 | + name: 'non-SQL family packs', |
| 56 | + run: () => |
| 57 | + defineContract({ |
| 58 | + family: documentFamilyPack, |
| 59 | + target: postgresTargetPack, |
| 60 | + models: {}, |
| 61 | + }), |
| 62 | + error: 'defineContract only accepts SQL family packs. Received family "document".', |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'non-extension pack refs in extensionPacks', |
| 66 | + run: () => |
| 67 | + defineContract({ |
| 68 | + family: sqlFamilyPack, |
| 69 | + target: postgresTargetPack, |
| 70 | + extensionPacks: { |
| 71 | + invalid: unsafeExtensionPackRefForRuntimeTest(postgresTargetPack), |
| 72 | + }, |
| 73 | + models: {}, |
| 74 | + }), |
| 75 | + error: |
| 76 | + 'defineContract only accepts extension pack refs in extensionPacks. Received kind "target".', |
| 77 | + }, |
| 78 | + { |
| 79 | + name: 'extension packs from another family', |
| 80 | + run: () => |
| 81 | + defineContract({ |
| 82 | + family: sqlFamilyPack, |
| 83 | + target: postgresTargetPack, |
| 84 | + extensionPacks: { |
| 85 | + invalid: unsafeExtensionPackRefForRuntimeTest({ |
| 86 | + ...pgvectorExtensionPack, |
| 87 | + familyId: 'document', |
| 88 | + }), |
| 89 | + }, |
| 90 | + models: {}, |
| 91 | + }), |
| 92 | + error: |
| 93 | + 'extension pack "pgvector" targets family "document" but contract target family is "sql".', |
| 94 | + }, |
| 95 | + { |
| 96 | + name: 'extension packs for another target', |
| 97 | + run: () => |
| 98 | + defineContract({ |
| 99 | + family: sqlFamilyPack, |
| 100 | + target: postgresTargetPack, |
| 101 | + extensionPacks: { |
| 102 | + invalid: mysqlExtensionPack, |
| 103 | + }, |
| 104 | + models: {}, |
| 105 | + }), |
| 106 | + error: 'extension pack "pgvector" targets "mysql" but contract target is "postgres".', |
| 107 | + }, |
| 108 | + ])('rejects $name', ({ run, error }) => { |
| 109 | + expect(run).toThrow(error); |
| 110 | + }); |
| 111 | +}); |
0 commit comments