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 { UserConfigService } from './user-config.service';
0006 import { ServerConfigService } from './server-config.service';
0007 import { BehaviorSubject } from 'rxjs';
0008 
0009 describe('UrlService', () => {
0010   let service: UrlService;
0011   let userConfigService: UserConfigService;
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: UserConfigService, useValue: mockUserConfigService },
0038         { provide: ServerConfigService, useValue: mockServerConfigService }
0039       ]
0040     });
0041 
0042     service = TestBed.inject(UrlService);
0043     userConfigService = TestBed.inject(UserConfigService);
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     it('should return input URL when backend is not available', () => {
0127       // Ensure backend is not available
0128       // Ensure backend is not available
0129       serverConfigService.config.servedByPyrobird = false;
0130       userConfigService.localServerUseApi.subject.next(false);
0131 
0132       // Manually trigger the service to update its config
0133       (service as any).updateServerConfig();
0134 
0135 
0136       const inputUrl = 'https://example.com/file.root';
0137       const fileType = 'edm4eic';
0138       const entries = 'all';
0139 
0140       const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0141       expect(resolvedUrl).toBe(inputUrl);
0142     });
0143 
0144     it('should handle URLs starting with asset:// in convert URL', () => {
0145       // Simulate backend availability
0146       userConfigService.localServerUseApi.subject.next(true);
0147 
0148       const inputUrl = 'asset://data/sample.dat';
0149       const baseUri = document.baseURI.endsWith('/') ? document.baseURI : `${document.baseURI}/`;
0150       const resolvedAssetUrl = `${baseUri}assets/data/sample.dat`;
0151 
0152       const fileType = 'edm4eic';
0153       const entries = 'all';
0154       const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(resolvedAssetUrl)}`;
0155 
0156       const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0157       expect(resolvedUrl).toBe(expectedUrl);
0158     });
0159 
0160     it('should handle URLs with custom protocol alias epic:// in convert URL', () => {
0161       // Simulate backend availability
0162       userConfigService.localServerUseApi.subject.next(true);
0163 
0164       const inputUrl = 'epic://some/path/file.root';
0165       const resolvedAliasUrl = 'https://eic.github.io/epic/artifacts/some/path/file.root';
0166 
0167       const fileType = 'edm4eic';
0168       const entries = 'all';
0169       const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(resolvedAliasUrl)}`;
0170 
0171       const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0172       expect(resolvedUrl).toBe(expectedUrl);
0173     });
0174   });
0175 });