|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | +import { |
| 3 | + type EntityMarker, |
| 4 | + type InferEntityName, |
| 5 | + type InferEntityType, |
| 6 | + isStandardEntity, |
| 7 | + type SimpleEntity, |
| 8 | + type StandardEntity, |
| 9 | +} from './index' |
| 10 | + |
| 11 | +describe('StandardEntity', () => { |
| 12 | + test('isStandardEntity returns true for valid entity', () => { |
| 13 | + const entity: StandardEntity<'User', { id: string }> = { |
| 14 | + '~entity': { |
| 15 | + name: 'User', |
| 16 | + type: undefined as unknown as { id: string }, |
| 17 | + }, |
| 18 | + } |
| 19 | + |
| 20 | + expect(isStandardEntity(entity)).toBe(true) |
| 21 | + }) |
| 22 | + |
| 23 | + test('isStandardEntity returns false for non-entity', () => { |
| 24 | + expect(isStandardEntity(null)).toBe(false) |
| 25 | + expect(isStandardEntity(undefined)).toBe(false) |
| 26 | + expect(isStandardEntity({})).toBe(false) |
| 27 | + expect(isStandardEntity({ name: 'User' })).toBe(false) |
| 28 | + expect(isStandardEntity({ '~entity': null })).toBe(false) |
| 29 | + expect(isStandardEntity({ '~entity': {} })).toBe(false) |
| 30 | + }) |
| 31 | + |
| 32 | + test('isStandardEntity returns true for entity with fields', () => { |
| 33 | + const entity = { |
| 34 | + '~entity': { |
| 35 | + name: 'Post', |
| 36 | + type: undefined, |
| 37 | + }, |
| 38 | + fields: { |
| 39 | + title: { type: 'string' }, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + expect(isStandardEntity(entity)).toBe(true) |
| 44 | + }) |
| 45 | +}) |
| 46 | + |
| 47 | +describe('Type inference', () => { |
| 48 | + test('InferEntityName extracts name', () => { |
| 49 | + type UserEntity = StandardEntity<'User', { id: string }> |
| 50 | + type Name = InferEntityName<UserEntity> |
| 51 | + |
| 52 | + // Type-level test - if this compiles, it works |
| 53 | + const name: Name = 'User' |
| 54 | + expect(name).toBe('User') |
| 55 | + }) |
| 56 | + |
| 57 | + test('InferEntityType extracts data type', () => { |
| 58 | + type UserData = { id: string; name: string } |
| 59 | + type UserEntity = StandardEntity<'User', UserData> |
| 60 | + type Data = InferEntityType<UserEntity> |
| 61 | + |
| 62 | + // Type-level test - if this compiles, it works |
| 63 | + const data: Data = { id: '1', name: 'Alice' } |
| 64 | + expect(data.id).toBe('1') |
| 65 | + expect(data.name).toBe('Alice') |
| 66 | + }) |
| 67 | +}) |
| 68 | + |
| 69 | +describe('EntityMarker', () => { |
| 70 | + test('can create entity with EntityMarker', () => { |
| 71 | + interface MyEntity<N extends string, D> extends EntityMarker<N, D> { |
| 72 | + readonly customProp: string |
| 73 | + } |
| 74 | + |
| 75 | + const entity: MyEntity<'Custom', { value: number }> = { |
| 76 | + '~entity': { |
| 77 | + name: 'Custom', |
| 78 | + type: undefined as unknown as { value: number }, |
| 79 | + }, |
| 80 | + customProp: 'test', |
| 81 | + } |
| 82 | + |
| 83 | + expect(isStandardEntity(entity)).toBe(true) |
| 84 | + expect(entity['~entity'].name).toBe('Custom') |
| 85 | + expect(entity.customProp).toBe('test') |
| 86 | + }) |
| 87 | +}) |
| 88 | + |
| 89 | +describe('SimpleEntity', () => { |
| 90 | + test('SimpleEntity is a valid StandardEntity', () => { |
| 91 | + const entity: SimpleEntity<'Simple', { foo: string }> = { |
| 92 | + '~entity': { |
| 93 | + name: 'Simple', |
| 94 | + type: undefined as unknown as { foo: string }, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + expect(isStandardEntity(entity)).toBe(true) |
| 99 | + expect(entity['~entity'].name).toBe('Simple') |
| 100 | + }) |
| 101 | +}) |
0 commit comments