@simtlix/simfinity-js 2.0.2 → 2.1.0

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.
@@ -1,125 +0,0 @@
1
- import {
2
- describe, test, expect,
3
- } from 'vitest';
4
- import { GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID } from 'graphql';
5
- import { createValidatedScalar } from '../src/index.js';
6
-
7
- describe('Validated Scalar Naming Convention', () => {
8
- test('should generate correct type names with base scalar type suffix', () => {
9
- const EmailScalar = createValidatedScalar(
10
- 'Email',
11
- 'A valid email address',
12
- GraphQLString,
13
- (value) => {
14
- if (!value.includes('@')) {
15
- throw new Error('Invalid email format');
16
- }
17
- },
18
- );
19
-
20
- const EpisodeNumberScalar = createValidatedScalar(
21
- 'EpisodeNumber',
22
- 'A valid episode number',
23
- GraphQLInt,
24
- (value) => {
25
- if (value <= 0) {
26
- throw new Error('Episode number must be positive');
27
- }
28
- },
29
- );
30
-
31
- const RatingScalar = createValidatedScalar(
32
- 'Rating',
33
- 'A valid rating between 0 and 10',
34
- GraphQLFloat,
35
- (value) => {
36
- if (value < 0 || value > 10) {
37
- throw new Error('Rating must be between 0 and 10');
38
- }
39
- },
40
- );
41
-
42
- const IsActiveScalar = createValidatedScalar(
43
- 'IsActive',
44
- 'A boolean indicating if something is active',
45
- GraphQLBoolean,
46
- (value) => {
47
- // Boolean validation is usually not needed, but this is for testing
48
- if (typeof value !== 'boolean') {
49
- throw new Error('Must be a boolean value');
50
- }
51
- },
52
- );
53
-
54
- const CustomIdScalar = createValidatedScalar(
55
- 'CustomId',
56
- 'A custom ID with specific format',
57
- GraphQLID,
58
- (value) => {
59
- if (!value.startsWith('CUST_')) {
60
- throw new Error('Custom ID must start with CUST_');
61
- }
62
- },
63
- );
64
-
65
- // Test the naming convention
66
- expect(EmailScalar.name).toBe('Email_String');
67
- expect(EpisodeNumberScalar.name).toBe('EpisodeNumber_Int');
68
- expect(RatingScalar.name).toBe('Rating_Float');
69
- expect(IsActiveScalar.name).toBe('IsActive_Boolean');
70
- expect(CustomIdScalar.name).toBe('CustomId_ID');
71
- });
72
-
73
- test('should maintain baseScalarType property', () => {
74
- const EmailScalar = createValidatedScalar(
75
- 'Email',
76
- 'A valid email address',
77
- GraphQLString,
78
- (value) => {
79
- if (!value.includes('@')) {
80
- throw new Error('Invalid email format');
81
- }
82
- },
83
- );
84
-
85
- expect(EmailScalar.baseScalarType).toBe(GraphQLString);
86
- expect(EmailScalar.name).toBe('Email_String');
87
- });
88
-
89
- test('should work with validation functions', () => {
90
- const EpisodeNumberScalar = createValidatedScalar(
91
- 'EpisodeNumber',
92
- 'A valid episode number',
93
- GraphQLInt,
94
- (value) => {
95
- if (value <= 0) {
96
- throw new Error('Episode number must be positive');
97
- }
98
- },
99
- );
100
-
101
- // Test valid value
102
- expect(() => EpisodeNumberScalar.serialize(5)).not.toThrow();
103
- expect(EpisodeNumberScalar.serialize(5)).toBe(5);
104
-
105
- // Test invalid value
106
- expect(() => EpisodeNumberScalar.serialize(0)).toThrow('Episode number must be positive');
107
- expect(() => EpisodeNumberScalar.serialize(-1)).toThrow('Episode number must be positive');
108
- });
109
-
110
- test('should generate error messages with correct type names', () => {
111
- const EpisodeNumberScalar = createValidatedScalar(
112
- 'EpisodeNumber',
113
- 'A valid episode number',
114
- GraphQLInt,
115
- (value) => {
116
- if (value <= 0) {
117
- throw new Error('Episode number must be positive');
118
- }
119
- },
120
- );
121
-
122
- // The error message should include the full type name
123
- expect(() => EpisodeNumberScalar.serialize(0)).toThrow('Episode number must be positive');
124
- });
125
- });
@@ -1,172 +0,0 @@
1
- import {
2
- describe, test, expect, beforeAll,
3
- } from 'vitest';
4
- import {
5
- GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLID, GraphQLList, GraphQLNonNull,
6
- } from 'graphql';
7
- import { createValidatedScalar } from '../src/index.js';
8
- import * as simfinity from '../src/index.js';
9
-
10
- describe('Custom Validated Scalar Types', () => {
11
- let EmailScalar;
12
- let PositiveIntScalar;
13
- let PhoneScalar;
14
- let UserType;
15
-
16
- beforeAll(() => {
17
- simfinity.preventCreatingCollection(true);
18
- // Create custom validated scalar types
19
- EmailScalar = createValidatedScalar(
20
- 'Email',
21
- 'A valid email address',
22
- GraphQLString,
23
- (value) => {
24
- const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
25
- if (!emailRegex.test(value)) {
26
- throw new Error('Invalid email format');
27
- }
28
- },
29
- );
30
-
31
- PositiveIntScalar = createValidatedScalar(
32
- 'PositiveInt',
33
- 'A positive integer',
34
- GraphQLInt,
35
- (value) => {
36
- if (value <= 0) {
37
- throw new Error('Value must be positive');
38
- }
39
- },
40
- );
41
-
42
- PhoneScalar = createValidatedScalar(
43
- 'Phone',
44
- 'A valid phone number',
45
- GraphQLString,
46
- (value) => {
47
- const phoneRegex = /^\+?[\d\s\-()]+$/;
48
- if (!phoneRegex.test(value)) {
49
- throw new Error('Invalid phone number format');
50
- }
51
- },
52
- );
53
-
54
- // Create a test type with custom scalars
55
- UserType = new GraphQLObjectType({
56
- name: 'User',
57
- fields: () => ({
58
- id: { type: GraphQLID },
59
- name: { type: GraphQLString },
60
- email: { type: EmailScalar },
61
- age: { type: PositiveIntScalar },
62
- phone: { type: PhoneScalar },
63
- emails: { type: new GraphQLList(EmailScalar) },
64
- requiredEmail: { type: new GraphQLNonNull(EmailScalar) },
65
- ages: { type: new GraphQLList(PositiveIntScalar) },
66
- }),
67
- });
68
- });
69
-
70
- describe('createValidatedScalar function', () => {
71
- test('should create a valid scalar type with baseScalarType property', () => {
72
- expect(EmailScalar).toBeDefined();
73
- expect(EmailScalar.name).toBe('Email_String');
74
- expect(EmailScalar.baseScalarType).toBe(GraphQLString);
75
- expect(EmailScalar.serialize).toBeDefined();
76
- expect(EmailScalar.parseValue).toBeDefined();
77
- expect(EmailScalar.parseLiteral).toBeDefined();
78
- });
79
-
80
- test('should validate baseScalarType parameter', () => {
81
- expect(() => {
82
- createValidatedScalar('Test', 'Test', null, () => {});
83
- }).toThrow('baseScalarType is required');
84
-
85
- expect(() => {
86
- createValidatedScalar('Test', 'Test', 'not a scalar', () => {});
87
- }).toThrow('baseScalarType must be a valid GraphQL scalar type');
88
- });
89
-
90
- test('should handle different base scalar types', () => {
91
- expect(PositiveIntScalar.baseScalarType).toBe(GraphQLInt);
92
- expect(PhoneScalar.baseScalarType).toBe(GraphQLString);
93
- });
94
- });
95
-
96
- describe('Custom scalar validation', () => {
97
- test('should validate email format correctly', () => {
98
- expect(() => EmailScalar.serialize('test@example.com')).not.toThrow();
99
- expect(() => EmailScalar.serialize('invalid-email')).toThrow('Invalid email format');
100
- });
101
-
102
- test('should validate positive integers correctly', () => {
103
- expect(() => PositiveIntScalar.serialize(5)).not.toThrow();
104
- expect(() => PositiveIntScalar.serialize(0)).toThrow('Value must be positive');
105
- expect(() => PositiveIntScalar.serialize(-1)).toThrow('Value must be positive');
106
- });
107
-
108
- test('should validate phone numbers correctly', () => {
109
- expect(() => PhoneScalar.serialize('+1-555-123-4567')).not.toThrow();
110
- expect(() => PhoneScalar.serialize('555-123-4567')).not.toThrow();
111
- expect(() => PhoneScalar.serialize('invalid phone')).toThrow('Invalid phone number format');
112
- });
113
- });
114
-
115
- describe('Schema generation with custom scalars', () => {
116
- let UserModel;
117
-
118
- beforeAll(() => {
119
- simfinity.connect(null, UserType, 'user', 'users');
120
- simfinity.createSchema(); // Models are now generated during schema creation
121
- UserModel = simfinity.getModel(UserType);
122
- });
123
-
124
- test('should generate schema with correct types for custom scalars', () => {
125
- const schema = UserModel.schema.obj;
126
-
127
- // Test individual fields
128
- expect(schema.email).toBe(String);
129
- expect(schema.age).toBe(Number);
130
- expect(schema.phone).toBe(String);
131
-
132
- // Test array fields
133
- expect(Array.isArray(schema.emails)).toBe(true);
134
- expect(schema.emails[0]).toBe(String);
135
- expect(Array.isArray(schema.ages)).toBe(true);
136
- expect(schema.ages[0]).toBe(Number);
137
-
138
- // Test required fields
139
- expect(schema.requiredEmail).toBe(String);
140
- });
141
-
142
- test('should preserve unique constraints', () => {
143
- const UserWithUniqueType = new GraphQLObjectType({
144
- name: 'UserWithUnique',
145
- fields: () => ({
146
- id: { type: GraphQLID },
147
- email: {
148
- type: EmailScalar,
149
- extensions: { unique: true },
150
- },
151
- }),
152
- });
153
- simfinity.connect(null, UserWithUniqueType, 'userWithUnique', 'usersWithUnique');
154
- simfinity.createSchema(); // Models are now generated during schema creation
155
- const UserWithUniqueModel = simfinity.getModel(UserWithUniqueType);
156
- const schema = UserWithUniqueModel.schema.obj;
157
-
158
- expect(schema.email).toEqual({ type: String, unique: true });
159
- });
160
- });
161
-
162
- describe('GraphQL schema integration', () => {
163
- test('should create valid GraphQL schema with custom scalars', () => {
164
- const schema = simfinity.createSchema();
165
-
166
- // The schema should be created without errors
167
- expect(schema).toBeDefined();
168
- expect(schema.getQueryType()).toBeDefined();
169
- expect(schema.getMutationType()).toBeDefined();
170
- });
171
- });
172
- });