Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/services/user-config.service.spec.ts is written in an unsupported language. File is not indexed.

0001 // url.service.spec.ts
0002 
0003 import { TestBed } from '@angular/core/testing';
0004 import { UrlService } from './url.service';
0005 import { LocalStorageService } from './local-storage.service';
0006 import { ServerConfigService } from './server-config.service';
0007 import { BehaviorSubject } from 'rxjs';
0008 
0009 describe('UrlService', () => {
0010   let service: UrlService;
0011   let userConfigService: LocalStorageService;
0012   let serverConfigService: ServerConfigService;
0013 
0014   beforeEach(() => {
0015     // Create mock services
0016     const mockUserConfigService = {
0017       localServerUrl: {
0018         subject: new BehaviorSubject<string>('http://localhost:5454'),
0019         value: 'http://localhost:5454'
0020       },
0021       localServerUseApi: {
0022         subject: new BehaviorSubject<boolean>(false),
0023         value: false
0024       }
0025     };
0026 
0027     const mockServerConfigService = {
0028       config: {
0029         servedByPyrobird: true,
0030         apiBaseUrl: 'http://localhost:5454'
0031       }
0032     };
0033 
0034     TestBed.configureTestingModule({
0035       providers: [
0036         UrlService,
0037         { provide: LocalStorageService, useValue: mockUserConfigService },
0038         { provide: ServerConfigService, useValue: mockServerConfigService }
0039       ]
0040     });
0041 
0042     service = TestBed.inject(UrlService);
0043     userConfigService = TestBed.inject(LocalStorageService);
0044     serverConfigService = TestBed.inject(ServerConfigService);
0045   });
0046 
0047   it('should be created', () => {
0048     expect(service).toBeTruthy();
0049   });
0050 
0051   describe('resolveDownloadUrl', () => {
0052     it('should return absolute URL as is (Case 1.1)', () => {
0053       const inputUrl = 'https://example.com/file.root';
0054       const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0055       expect(resolvedUrl).toBe(inputUrl);
0056     });
0057 
0058     it('should handle relative URL without protocol when backend is available (Case 1.2)', () => {
0059       // Simulate backend availability
0060       userConfigService.localServerUseApi.subject.next(true);
0061 
0062       const inputUrl = '/path/to/file.root';
0063       const expectedUrl = 'http://localhost:5454/api/v1/download?f=%2Fpath%2Fto%2Ffile.root';
0064 
0065       const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0066       expect(resolvedUrl).toBe(expectedUrl);
0067     });
0068 
0069     it('should handle relative URL without protocol when backend is not available (Case 1.2)', () => {
0070       // Ensure backend is not available
0071       serverConfigService.config.servedByPyrobird = false;
0072       userConfigService.localServerUseApi.subject.next(false);
0073 
0074       // Manually trigger the service to update its config
0075       (service as any).updateServerConfig();
0076 
0077 
0078       const inputUrl = '/path/to/file.root';
0079       const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0080       expect(resolvedUrl).toBe(inputUrl);
0081     });
0082 
0083     it('should handle URLs starting with asset://', () => {
0084       const inputUrl = 'asset://images/logo.png';
0085       const baseUri = document.baseURI.endsWith('/') ? document.baseURI : `${document.baseURI}/`;
0086       const expectedUrl = `${baseUri}assets/images/logo.png`;
0087 
0088       const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0089       expect(resolvedUrl).toBe(expectedUrl);
0090     });
0091 
0092     it('should handle URLs with custom protocol alias epic://', () => {
0093       const inputUrl = 'epic://some/path/file.root';
0094       const expectedUrl = 'https://eic.github.io/epic/artifacts/some/path/file.root';
0095 
0096       const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0097       expect(resolvedUrl).toBe(expectedUrl);
0098     });
0099 
0100     it('should encode the input URL correctly in download endpoint', () => {
0101       // Simulate backend availability
0102       userConfigService.localServerUseApi.subject.next(true);
0103 
0104       const inputUrl = '/path with spaces/file.root';
0105       const expectedUrl = 'http://localhost:5454/api/v1/download?f=%2Fpath%20with%20spaces%2Ffile.root';
0106 
0107       const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0108       expect(resolvedUrl).toBe(expectedUrl);
0109     });
0110   });
0111 
0112   describe('resolveConvertUrl', () => {
0113     it('should construct convert URL when backend is available', () => {
0114       // Simulate backend availability
0115       userConfigService.localServerUseApi.subject.next(true);
0116 
0117       const inputUrl = 'https://example.com/file.root';
0118       const fileType = 'edm4eic';
0119       const entries = 'all';
0120       const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(inputUrl)}`;
0121 
0122       const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0123       expect(resolvedUrl).toBe(expectedUrl);
0124     });
0125 
0126 
0127     it('should handle URLs starting with asset:// in convert URL', () => {
0128       // Simulate backend availability
0129       userConfigService.localServerUseApi.subject.next(true);
0130 
0131       const inputUrl = 'asset://data/sample.dat';
0132       const baseUri = document.baseURI.endsWith('/') ? document.baseURI : `${document.baseURI}/`;
0133       const resolvedAssetUrl = `${baseUri}assets/data/sample.dat`;
0134 
0135       const fileType = 'edm4eic';
0136       const entries = 'all';
0137       const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(resolvedAssetUrl)}`;
0138 
0139       const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0140       expect(resolvedUrl).toBe(expectedUrl);
0141     });
0142 
0143     it('should handle URLs with custom protocol alias epic:// in convert URL', () => {
0144       // Simulate backend availability
0145       userConfigService.localServerUseApi.subject.next(true);
0146 
0147       const inputUrl = 'epic://some/path/file.root';
0148       const resolvedAliasUrl = 'https://eic.github.io/epic/artifacts/some/path/file.root';
0149 
0150       const fileType = 'edm4eic';
0151       const entries = 'all';
0152       const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(resolvedAliasUrl)}`;
0153 
0154       const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0155       expect(resolvedUrl).toBe(expectedUrl);
0156     });
0157   });
0158 });