PinManagerService.test.ts
1 import { HasPinAbility, IFileInfo, IFolderFile, IIpfsServiceSymbol, IKeyValueStoreSymbol, IPinManagerService, IPinManagerServiceSymbol, IProfile, IProfileSymbol, PinStatus } from 'ipmc-interfaces'; 2 import { describe, expect, test } from 'vitest'; 3 import { Application, CoreModule, MemoryKeyValueStore, PinManagerService } from '../../src'; 4 import { MockIpfsService, TestProfile } from '../../testing'; 5 6 describe('PinManagerService', () => { 7 const app = new Application(); 8 app.use(CoreModule); 9 app.register(MockIpfsService, IIpfsServiceSymbol); 10 app.register(MemoryKeyValueStore, IKeyValueStoreSymbol); 11 app.registerConstant<IProfile>(TestProfile, IProfileSymbol); 12 13 test('Can add and remove a pin', async () => { 14 const manager = app.getService<IPinManagerService>(IPinManagerServiceSymbol)!; 15 const item: HasPinAbility = { 16 cid: 'test', 17 pinId: 'this is a test id' 18 }; 19 let pinChanges = -1; 20 manager.pins.subscribe(() => { 21 pinChanges++; 22 }); 23 24 expect(manager.listPins()).toEqual([]); 25 26 await manager.addPin(item); 27 28 expect(pinChanges).toBe(1); 29 expect(manager.listPins().length).toBe(1); 30 expect(manager.isPinned(item)).toBe(PinStatus.Pinned); 31 32 await manager.removePin(item); 33 34 expect(pinChanges).toBe(2); 35 expect(manager.listPins()).toEqual([]); 36 expect(manager.isPinned(item)).toBe(PinStatus.UnPinned); 37 }); 38 39 test('walkPath works', () => { 40 const manager = app.getService<PinManagerService>(IPinManagerServiceSymbol)!; 41 const stuff: IFolderFile[] = [ 42 { 43 name: 'test', 44 items: [ 45 { 46 name: 'test2', 47 cid: 'test2', 48 type: 'file', 49 } 50 ], 51 cid: 'test', 52 type: 'dir', 53 }, 54 ]; 55 const res = manager.walkPath('test/test2', stuff); 56 57 expect(res).not.toBeUndefined(); 58 expect(res!.name).toBe('test2'); 59 }); 60 });