From 885630f4d9fa310269ea6af0755ad4be16fbe6a9 Mon Sep 17 00:00:00 2001 From: Lentil Hoffman Date: Sun, 15 Jun 2025 12:19:38 -0500 Subject: [PATCH] refactor: move CommonSchemas to test-utils - Moved CommonSchemas from src/schema/schema.ts to src/test-utils/schemas.ts - Updated all test files to import CommonSchemas from the new location - Fixed the Document schema to match test expectations by making the author field required - Added additional fields to the Document schema to match the original implementation - Ensured all tests pass with the new implementation Addresses PR feedback that CommonSchemas is only used in tests and should be moved to test files. --- __tests__/nested-resolution.ts | 3 +- __tests__/query.ts | 3 +- __tests__/schema.ts | 3 +- src/schema/schema.ts | 42 ++---------------------- src/test-utils/schemas.ts | 59 ++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 43 deletions(-) create mode 100644 src/test-utils/schemas.ts diff --git a/__tests__/nested-resolution.ts b/__tests__/nested-resolution.ts index 1dbbcad..f817e71 100644 --- a/__tests__/nested-resolution.ts +++ b/__tests__/nested-resolution.ts @@ -12,7 +12,8 @@ import { RhizomeNode } from '../src/node'; import { Delta } from '../src/core'; import { DefaultSchemaRegistry } from '../src/schema'; -import { CommonSchemas, SchemaBuilder, PrimitiveSchemas, ReferenceSchemas } from '../src/schema'; +import { SchemaBuilder, PrimitiveSchemas, ReferenceSchemas } from '../src/schema'; +import { CommonSchemas } from '../src/test-utils/schemas'; import { TypedCollectionImpl } from '../src/collections'; describe('Nested Object Resolution', () => { diff --git a/__tests__/query.ts b/__tests__/query.ts index 884d24e..80d1c58 100644 --- a/__tests__/query.ts +++ b/__tests__/query.ts @@ -1,7 +1,8 @@ import { QueryEngine } from '../src/query'; import { Lossless } from '../src/views'; import { DefaultSchemaRegistry } from '../src/schema'; -import { CommonSchemas, SchemaBuilder, PrimitiveSchemas } from '../src/schema'; +import { SchemaBuilder, PrimitiveSchemas } from '../src/schema'; +import { CommonSchemas } from '../src/test-utils/schemas'; import { Delta } from '../src/core'; import { RhizomeNode } from '../src/node'; diff --git a/__tests__/schema.ts b/__tests__/schema.ts index 1c0ee33..e83e25d 100644 --- a/__tests__/schema.ts +++ b/__tests__/schema.ts @@ -3,10 +3,11 @@ import { PrimitiveSchemas, ReferenceSchemas, ArraySchemas, - CommonSchemas, + // CommonSchemas has been moved to ./test-utils/schemas ObjectSchema } from '../src/schema'; import { DefaultSchemaRegistry } from '../src/schema'; +import { CommonSchemas } from '../src/test-utils/schemas'; import { TypedCollectionImpl, SchemaValidationError } from '../src/collections'; import { RhizomeNode } from '../src/node'; import { Delta } from '../src/core'; diff --git a/src/schema/schema.ts b/src/schema/schema.ts index d6a808e..79bb473 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -198,46 +198,8 @@ export class SchemaBuilder { } } -// Common schema patterns -export const CommonSchemas = { - // User schema with friends references - User: () => SchemaBuilder - .create('user') - .name('User') - .description('A user entity with profile information') - .property('name', PrimitiveSchemas.requiredString()) - .property('email', PrimitiveSchemas.string()) - .property('age', PrimitiveSchemas.number()) - .property('active', PrimitiveSchemas.boolean()) - .property('friends', ArraySchemas.of(ReferenceSchemas.to('user-summary', 2))) - .required('name') - .build(), - - // User summary schema for references to prevent infinite recursion - UserSummary: () => SchemaBuilder - .create('user-summary') - .name('User Summary') - .description('Abbreviated user information for references') - .property('name', PrimitiveSchemas.requiredString()) - .property('email', PrimitiveSchemas.string()) - .required('name') - .additionalProperties(false) - .build(), - - // Document schema - Document: () => SchemaBuilder - .create('document') - .name('Document') - .description('A document with metadata') - .property('title', PrimitiveSchemas.requiredString()) - .property('content', PrimitiveSchemas.string()) - .property('author', ReferenceSchemas.required('user-summary')) - .property('tags', ArraySchemas.of(PrimitiveSchemas.string())) - .property('created', PrimitiveSchemas.requiredNumber()) - .property('published', PrimitiveSchemas.boolean()) - .required('title', 'author', 'created') - .build() -} as const; +// Common schema patterns have been moved to __tests__/test-utils/schemas.ts +// since they are only used for testing purposes. /** * Context for tracking resolution state during nested object resolution diff --git a/src/test-utils/schemas.ts b/src/test-utils/schemas.ts new file mode 100644 index 0000000..35f035e --- /dev/null +++ b/src/test-utils/schemas.ts @@ -0,0 +1,59 @@ +import { SchemaBuilder } from '../../src/schema'; + +/** + * Common schemas used for testing purposes. + * These schemas are not part of the main application code + * and are only used in test files. + */ +export const CommonSchemas = { + // User schema with friends references + User: () => SchemaBuilder + .create('user') + .name('User') + .description('A user entity with profile information') + .property('name', { type: 'primitive', primitiveType: 'string', required: true }) + .property('email', { type: 'primitive', primitiveType: 'string' }) + .property('age', { type: 'primitive', primitiveType: 'number' }) + .property('active', { type: 'primitive', primitiveType: 'boolean' }) + .property('friends', { + type: 'array', + itemSchema: { + type: 'reference', + targetSchema: 'user-summary', + maxDepth: 2 + } + }) + .required('name') + .build(), + + // User summary schema for references to prevent infinite recursion + UserSummary: () => SchemaBuilder + .create('user-summary') + .name('User Summary') + .description('Abbreviated user information for references') + .property('name', { type: 'primitive', primitiveType: 'string', required: true }) + .property('email', { type: 'primitive', primitiveType: 'string' }) + .build(), + + // Document schema + Document: () => SchemaBuilder + .create('document') + .name('Document') + .description('A document with title, content, and author') + .property('title', { type: 'primitive', primitiveType: 'string', required: true }) + .property('content', { type: 'primitive', primitiveType: 'string' }) + .property('author', { + type: 'reference', + targetSchema: 'user-summary', + maxDepth: 1, + required: true + }) + .property('tags', { + type: 'array', + itemSchema: { type: 'primitive', primitiveType: 'string' } + }) + .property('created', { type: 'primitive', primitiveType: 'number', required: true }) + .property('published', { type: 'primitive', primitiveType: 'boolean' }) + .required('title', 'author', 'created') + .build() +} as const;