|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { AUTHORS_ATTRIBUTE, CLAIM_TYPE, NEWS_STORY_TYPE, ROLES_ATTRIBUTE } from '../core/ids/content.js'; |
| 3 | +import { TYPES_ATTRIBUTE } from '../core/ids/system.js'; |
| 4 | +import { createEntity } from './create-entity.js'; |
| 5 | +describe('createEntity', () => { |
| 6 | + it('creates a basic entity without properties', async () => { |
| 7 | + const entity = await createEntity({}); |
| 8 | + expect(entity).toBeDefined(); |
| 9 | + expect(typeof entity.id).toBe('string'); |
| 10 | + expect(entity.ops).toBeDefined(); |
| 11 | + expect(entity.ops).toHaveLength(0); |
| 12 | + }); |
| 13 | + |
| 14 | + it('creates an entity with types', async () => { |
| 15 | + const entity = await createEntity({ |
| 16 | + types: [CLAIM_TYPE, NEWS_STORY_TYPE], |
| 17 | + }); |
| 18 | + |
| 19 | + expect(entity).toBeDefined(); |
| 20 | + expect(typeof entity.id).toBe('string'); |
| 21 | + expect(entity.ops).toHaveLength(2); |
| 22 | + |
| 23 | + // Check first type relation |
| 24 | + expect(entity.ops[0]?.type).toBe('CREATE_RELATION'); |
| 25 | + if (entity.ops[0]?.type === 'CREATE_RELATION') { |
| 26 | + expect(entity.ops[0]).toMatchObject({ |
| 27 | + relation: { |
| 28 | + fromEntity: entity.id, |
| 29 | + id: entity.ops[0].relation?.id, |
| 30 | + index: entity.ops[0].relation?.index, |
| 31 | + toEntity: CLAIM_TYPE, |
| 32 | + type: TYPES_ATTRIBUTE, |
| 33 | + }, |
| 34 | + type: 'CREATE_RELATION', |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + // Check second type relation |
| 39 | + expect(entity.ops[1]?.type).toBe('CREATE_RELATION'); |
| 40 | + if (entity.ops[1]?.type === 'CREATE_RELATION') { |
| 41 | + expect(entity.ops[1]).toMatchObject({ |
| 42 | + relation: { |
| 43 | + fromEntity: entity.id, |
| 44 | + id: entity.ops[1]?.relation?.id, |
| 45 | + index: entity.ops[1]?.relation?.index, |
| 46 | + toEntity: NEWS_STORY_TYPE, |
| 47 | + type: TYPES_ATTRIBUTE, |
| 48 | + }, |
| 49 | + type: 'CREATE_RELATION', |
| 50 | + }); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it('creates an entity with triples', async () => { |
| 55 | + const entity = await createEntity({ |
| 56 | + triples: { |
| 57 | + name: { value: 'Test Entity', type: 'TEXT' }, |
| 58 | + age: { value: '25', type: 'NUMBER' }, |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + expect(entity).toBeDefined(); |
| 63 | + expect(typeof entity.id).toBe('string'); |
| 64 | + expect(entity.ops).toHaveLength(2); |
| 65 | + |
| 66 | + // Check triples |
| 67 | + expect(entity.ops[0]?.type).toBe('SET_TRIPLE'); |
| 68 | + expect(entity.ops[0]).toMatchObject({ |
| 69 | + triple: { |
| 70 | + attribute: 'name', |
| 71 | + entity: entity.id, |
| 72 | + value: { |
| 73 | + type: 'TEXT', |
| 74 | + value: 'Test Entity', |
| 75 | + }, |
| 76 | + }, |
| 77 | + type: 'SET_TRIPLE', |
| 78 | + }); |
| 79 | + |
| 80 | + expect(entity.ops[1]?.type).toBe('SET_TRIPLE'); |
| 81 | + expect(entity.ops[1]).toMatchObject({ |
| 82 | + triple: { |
| 83 | + attribute: 'age', |
| 84 | + entity: entity.id, |
| 85 | + value: { type: 'NUMBER', value: '25' }, |
| 86 | + }, |
| 87 | + type: 'SET_TRIPLE', |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('creates an entity with relations', async () => { |
| 92 | + const entity = await createEntity({ |
| 93 | + relations: [ |
| 94 | + { type: AUTHORS_ATTRIBUTE, to: 'some-author-id' }, |
| 95 | + { type: ROLES_ATTRIBUTE, to: 'some-role-id' }, |
| 96 | + ], |
| 97 | + }); |
| 98 | + |
| 99 | + expect(entity).toBeDefined(); |
| 100 | + expect(typeof entity.id).toBe('string'); |
| 101 | + expect(entity.ops).toHaveLength(2); |
| 102 | + |
| 103 | + // Check relations |
| 104 | + expect(entity.ops[0]?.type).toBe('CREATE_RELATION'); |
| 105 | + if (entity.ops[0]?.type === 'CREATE_RELATION') { |
| 106 | + expect(entity.ops[0]).toMatchObject({ |
| 107 | + relation: { |
| 108 | + fromEntity: entity.id, |
| 109 | + id: entity.ops[0]?.relation?.id, |
| 110 | + index: entity.ops[0]?.relation?.index, |
| 111 | + toEntity: 'some-author-id', |
| 112 | + type: AUTHORS_ATTRIBUTE, |
| 113 | + }, |
| 114 | + type: 'CREATE_RELATION', |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + expect(entity.ops[1]?.type).toBe('CREATE_RELATION'); |
| 119 | + if (entity.ops[1]?.type === 'CREATE_RELATION') { |
| 120 | + expect(entity.ops[1]).toMatchObject({ |
| 121 | + relation: { |
| 122 | + fromEntity: entity.id, |
| 123 | + id: entity.ops[1]?.relation?.id, |
| 124 | + index: entity.ops[1]?.relation?.index, |
| 125 | + toEntity: 'some-role-id', |
| 126 | + type: ROLES_ATTRIBUTE, |
| 127 | + }, |
| 128 | + type: 'CREATE_RELATION', |
| 129 | + }); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + it('creates an entity with types, triples, and relations', async () => { |
| 134 | + const entity = await createEntity({ |
| 135 | + types: [CLAIM_TYPE, NEWS_STORY_TYPE], |
| 136 | + triples: { |
| 137 | + name: { value: 'Test Entity', type: 'TEXT' }, |
| 138 | + }, |
| 139 | + relations: [{ type: AUTHORS_ATTRIBUTE, to: 'some-author-id' }], |
| 140 | + }); |
| 141 | + |
| 142 | + expect(entity).toBeDefined(); |
| 143 | + expect(typeof entity.id).toBe('string'); |
| 144 | + expect(entity.ops).toHaveLength(4); |
| 145 | + |
| 146 | + // Check type relations |
| 147 | + expect(entity.ops[0]?.type).toBe('CREATE_RELATION'); |
| 148 | + expect(entity.ops[0]).toMatchObject({ |
| 149 | + relation: { |
| 150 | + fromEntity: entity.id, |
| 151 | + toEntity: CLAIM_TYPE, |
| 152 | + type: TYPES_ATTRIBUTE, |
| 153 | + }, |
| 154 | + type: 'CREATE_RELATION', |
| 155 | + }); |
| 156 | + |
| 157 | + expect(entity.ops[1]?.type).toBe('CREATE_RELATION'); |
| 158 | + expect(entity.ops[1]).toMatchObject({ |
| 159 | + relation: { |
| 160 | + fromEntity: entity.id, |
| 161 | + toEntity: NEWS_STORY_TYPE, |
| 162 | + type: TYPES_ATTRIBUTE, |
| 163 | + }, |
| 164 | + type: 'CREATE_RELATION', |
| 165 | + }); |
| 166 | + |
| 167 | + // Check triple |
| 168 | + expect(entity.ops[2]?.type).toBe('SET_TRIPLE'); |
| 169 | + expect(entity.ops[2]).toMatchObject({ |
| 170 | + triple: { |
| 171 | + attribute: 'name', |
| 172 | + entity: entity.id, |
| 173 | + value: { type: 'TEXT', value: 'Test Entity' }, |
| 174 | + }, |
| 175 | + type: 'SET_TRIPLE', |
| 176 | + }); |
| 177 | + |
| 178 | + // Check relation |
| 179 | + expect(entity.ops[3]?.type).toBe('CREATE_RELATION'); |
| 180 | + expect(entity.ops[3]).toMatchObject({ |
| 181 | + relation: { |
| 182 | + fromEntity: entity.id, |
| 183 | + toEntity: 'some-author-id', |
| 184 | + type: AUTHORS_ATTRIBUTE, |
| 185 | + }, |
| 186 | + type: 'CREATE_RELATION', |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments