import { describe, it, expect } from 'vitest'; import { createEntityCollection, MutableSignal, mutableSignal } from '@/utils/mutable-signal'; type TestEntity = { id: string; name: string; value: number; }; describe('createEntityCollection', () => { it('should create empty collection', () => { const collection = createEntityCollection(); expect(collection.collection.value).toEqual({}); }); it('should add single entity', () => { const collection = createEntityCollection(); const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 }; collection.add(testEntity); expect(collection.collection.value).toHaveProperty('e1'); expect(collection.get('e1').value).toEqual(testEntity); }); it('should add multiple entities', () => { const collection = createEntityCollection(); const entity1: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 }; const entity2: TestEntity = { id: 'e2', name: 'Entity 2', value: 20 }; const entity3: TestEntity = { id: 'e3', name: 'Entity 3', value: 30 }; collection.add(entity1, entity2, entity3); expect(Object.keys(collection.collection.value)).toHaveLength(3); expect(collection.get('e1').value.name).toBe('Entity 1'); expect(collection.get('e2').value.name).toBe('Entity 2'); expect(collection.get('e3').value.name).toBe('Entity 3'); }); it('should remove single entity', () => { const collection = createEntityCollection(); const entity1: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 }; const entity2: TestEntity = { id: 'e2', name: 'Entity 2', value: 20 }; collection.add(entity1, entity2); collection.remove('e1'); expect(Object.keys(collection.collection.value)).toHaveLength(1); expect(collection.collection.value).not.toHaveProperty('e1'); expect(collection.collection.value).toHaveProperty('e2'); }); it('should remove multiple entities', () => { const collection = createEntityCollection(); const entity1: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 }; const entity2: TestEntity = { id: 'e2', name: 'Entity 2', value: 20 }; const entity3: TestEntity = { id: 'e3', name: 'Entity 3', value: 30 }; collection.add(entity1, entity2, entity3); collection.remove('e1', 'e3'); expect(Object.keys(collection.collection.value)).toHaveLength(1); expect(collection.collection.value).toHaveProperty('e2'); }); it('should update entity via accessor', () => { const collection = createEntityCollection(); const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 }; collection.add(testEntity); const accessor = collection.get('e1'); accessor.value = { ...testEntity, value: 100, name: 'Updated' }; expect(collection.get('e1').value.value).toBe(100); expect(collection.get('e1').value.name).toBe('Updated'); }); it('should return undefined for non-existent entity', () => { const collection = createEntityCollection(); expect(collection.get('nonexistent')).toBeUndefined(); }); it('should handle removing non-existent entity', () => { const collection = createEntityCollection(); const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 }; collection.add(testEntity); collection.remove('nonexistent'); expect(Object.keys(collection.collection.value)).toHaveLength(1); }); it('should work with reactive updates', () => { const collection = createEntityCollection(); const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 }; collection.add(testEntity); const accessor = collection.get('e1'); expect(accessor.value.value).toBe(10); accessor.value = { ...testEntity, value: 50 }; expect(accessor.value.value).toBe(50); }); }); describe('MutableSignal', () => { it('should create signal with initial value', () => { const s = mutableSignal({ count: 1 }); expect(s.value.count).toBe(1); }); it('should produce immutable updates', () => { const s = mutableSignal({ count: 1, items: [1, 2, 3] }); s.produce(draft => { draft.count = 2; draft.items.push(4); }); expect(s.value.count).toBe(2); expect(s.value.items).toEqual([1, 2, 3, 4]); }); describe('interruption / produceAsync', () => { it('should update immediately when no interruptions', async () => { const s = mutableSignal({ value: 0 }); await s.produceAsync(draft => { draft.value = 1; }); expect(s.value.value).toBe(1); }); it('should wait for addInterruption before updating', async () => { const s = mutableSignal({ value: 0 }); let animationDone = false; const animation = new Promise(resolve => { setTimeout(() => { animationDone = true; resolve(); }, 10); }); s.addInterruption(animation); const producePromise = s.produceAsync(draft => { draft.value = 42; }); // produceAsync 应该等待 animation 完成 expect(animationDone).toBe(false); expect(s.value.value).toBe(0); await producePromise; expect(animationDone).toBe(true); expect(s.value.value).toBe(42); }); it('should wait for all interruptions in parallel', async () => { const s = mutableSignal({ value: 0 }); const order: string[] = []; const anim1 = new Promise(resolve => { setTimeout(() => { order.push('anim1'); resolve(); }, 20); }); const anim2 = new Promise(resolve => { setTimeout(() => { order.push('anim2'); resolve(); }, 10); }); s.addInterruption(anim1); s.addInterruption(anim2); await s.produceAsync(draft => { draft.value = 1; }); // anim2 先完成(10ms),anim1 后完成(20ms) expect(order).toEqual(['anim2', 'anim1']); expect(s.value.value).toBe(1); }); it('should not throw when an interruption rejects', async () => { const s = mutableSignal({ value: 0 }); const failingAnim = Promise.reject(new Error('animation cancelled')); // 避免未捕获的 rejection 警告 failingAnim.catch(() => {}); s.addInterruption(failingAnim); // allSettled 应该让 produceAsync 继续执行 await s.produceAsync(draft => { draft.value = 99; }); expect(s.value.value).toBe(99); }); it('should clear interruptions after produceAsync resolves', async () => { const s = mutableSignal({ value: 0 }); s.addInterruption(Promise.resolve()); await s.produceAsync(draft => { draft.value = 1; }); expect(s.value.value).toBe(1); // 第二次 produceAsync 不应该再等待 await s.produceAsync(draft => { draft.value = 2; }); expect(s.value.value).toBe(2); }); it('should clear all pending interruptions manually', async () => { const s = mutableSignal({ value: 0 }); let animationDone = false; const longAnim = new Promise(resolve => { setTimeout(() => { animationDone = true; resolve(); }, 100); }); s.addInterruption(longAnim); s.clearInterruptions(); await s.produceAsync(draft => { draft.value = 1; }); expect(s.value.value).toBe(1); // clearInterruptions 后,longAnim 仍在后台运行,但 produceAsync 不会等它 expect(animationDone).toBe(false); }); }); });