A social knowledge tool for researchers built on ATProto
at development 1.9 kB view raw
1import { Result, ok, err } from '../../../../shared/core/Result'; 2import { IMetadataService } from '../../domain/services/IMetadataService'; 3import { UrlMetadata } from '../../domain/value-objects/UrlMetadata'; 4import { URL } from '../../domain/value-objects/URL'; 5 6export class FakeMetadataService implements IMetadataService { 7 private metadataMap: Map<string, UrlMetadata> = new Map(); 8 private shouldFail: boolean = false; 9 private available: boolean = true; 10 11 async fetchMetadata(url: URL): Promise<Result<UrlMetadata>> { 12 if (!this.available) { 13 return err(new Error('Metadata service is not available')); 14 } 15 16 if (this.shouldFail) { 17 return err(new Error('Failed to fetch metadata')); 18 } 19 20 // Check if we have pre-configured metadata for this URL 21 const existingMetadata = this.metadataMap.get(url.value); 22 if (existingMetadata) { 23 return ok(existingMetadata); 24 } 25 26 // Generate fake metadata 27 const metadataResult = UrlMetadata.create({ 28 url: url.value, 29 title: `Fake Title for ${url.value}`, 30 description: `Fake description for ${url.value}`, 31 author: 'Fake Author', 32 siteName: 'Fake Site', 33 retrievedAt: new Date(), 34 }); 35 36 if (metadataResult.isErr()) { 37 return err( 38 new Error(`Failed to create metadata: ${metadataResult.error.message}`), 39 ); 40 } 41 42 return ok(metadataResult.value); 43 } 44 45 async isAvailable(): Promise<boolean> { 46 return this.available; 47 } 48 49 // Test helper methods 50 public setMetadata(url: string, metadata: UrlMetadata): void { 51 this.metadataMap.set(url, metadata); 52 } 53 54 public setShouldFail(shouldFail: boolean): void { 55 this.shouldFail = shouldFail; 56 } 57 58 public setAvailable(available: boolean): void { 59 this.available = available; 60 } 61 62 public clear(): void { 63 this.metadataMap.clear(); 64 this.shouldFail = false; 65 this.available = true; 66 } 67}