Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/services/url.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 { ConfigService } from './config.service';
0006 import { ServerConfigService } from './server-config.service';
0007 import { BehaviorSubject } from 'rxjs';
0008 import { HttpClientModule } from "@angular/common/http";
0009 
0010 describe('UrlService', () => {
0011     let service: UrlService;
0012     let userConfigService: any;
0013     let serverConfigService: any;
0014 
0015     beforeEach(() => {
0016         // Create mock services with .value property
0017         userConfigService = {
0018             localServerUrl: {
0019                 subject: new BehaviorSubject<string>('http://localhost:5454'),
0020                 get value() {
0021                     return this.subject.value;
0022                 }
0023             },
0024             localServerUseApi: {
0025                 subject: new BehaviorSubject<boolean>(false),
0026                 get value() {
0027                     return this.subject.value;
0028                 }
0029             },
0030             getConfig: (key: string) => {
0031                 if (key === 'localServerUrl')
0032                     return userConfigService.localServerUrl;
0033                 if (key === 'localServerUseApi')
0034                     return userConfigService.localServerUseApi;
0035                 return undefined;
0036             }
0037         };
0038 
0039         serverConfigService = {
0040             config: {
0041                 servedByPyrobird: false,
0042                 apiAvailable: true,
0043                 apiBaseUrl: 'http://localhost:5454',
0044             }
0045         };
0046 
0047         TestBed.configureTestingModule({
0048             imports: [HttpClientModule],
0049             providers: [
0050                 UrlService,
0051                 { provide: ConfigService, useValue: userConfigService },
0052                 { provide: ServerConfigService, useValue: serverConfigService }
0053             ]
0054         });
0055 
0056         service = TestBed.inject(UrlService);
0057     });
0058 
0059     describe('resolveDownloadUrl', () => {
0060         it('should handle relative URL without protocol when backend is available (Case 1.2)', async () => {
0061             userConfigService.localServerUseApi.subject.next(true);
0062             // Allow microtask queue to flush
0063             await Promise.resolve();
0064 
0065             const inputUrl = '/path/to/file.root';
0066             const expectedUrl = 'http://localhost:5454/api/v1/download?f=%2Fpath%2Fto%2Ffile.root';
0067 
0068             const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0069 
0070             expect(resolvedUrl).toBe(expectedUrl);
0071         });
0072 
0073         it('should encode the input URL correctly in download endpoint', async () => {
0074             userConfigService.localServerUseApi.subject.next(true);
0075             await Promise.resolve();
0076 
0077             const inputUrl = '/path with spaces/file.root';
0078             const expectedUrl = 'http://localhost:5454/api/v1/download?f=%2Fpath%20with%20spaces%2Ffile.root';
0079 
0080             const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0081             expect(resolvedUrl).toBe(expectedUrl);
0082         });
0083     });
0084 
0085     describe('resolveConvertUrl', () => {
0086         it('should construct convert URL when backend is available', async () => {
0087             userConfigService.localServerUseApi.subject.next(true);
0088             await Promise.resolve();
0089 
0090             const inputUrl = 'https://example.com/file.root';
0091             const fileType = 'edm4eic';
0092             const entries = 'all';
0093             const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(inputUrl)}`;
0094 
0095             const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0096             expect(resolvedUrl).toBe(expectedUrl);
0097         });
0098 
0099         it('should handle URLs starting with asset:// in convert URL', async () => {
0100             userConfigService.localServerUseApi.subject.next(true);
0101             await Promise.resolve();
0102 
0103             const inputUrl = 'asset://data/sample.dat';
0104             const baseUri = document.baseURI.endsWith('/') ? document.baseURI : `${document.baseURI}/`;
0105             const resolvedAssetUrl = `${baseUri}assets/data/sample.dat`;
0106 
0107             const fileType = 'edm4eic';
0108             const entries = 'all';
0109             const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(resolvedAssetUrl)}`;
0110 
0111             const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0112             expect(resolvedUrl).toBe(expectedUrl);
0113         });
0114 
0115         it('should handle URLs with custom protocol alias epic:// in convert URL', async () => {
0116             userConfigService.localServerUseApi.subject.next(true);
0117             await Promise.resolve();
0118 
0119             const inputUrl = 'epic://some/path/file.root';
0120             const resolvedAliasUrl = 'https://eic.github.io/epic/artifacts/some/path/file.root';
0121 
0122             const fileType = 'edm4eic';
0123             const entries = 'all';
0124             const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(resolvedAliasUrl)}`;
0125 
0126             const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0127             expect(resolvedUrl).toBe(expectedUrl);
0128         });
0129     });
0130 
0131     describe('backend availability and server address', () => {
0132         it('should use user-configured server address if localServerUseApi is true', async () => {
0133             userConfigService.localServerUseApi.subject.next(true);
0134             userConfigService.localServerUrl.subject.next('http://customserver:1234');
0135             await Promise.resolve();
0136 
0137             const expectedServerAddress = 'http://customserver:1234';
0138             expect((service as any).serverAddress).toBe(expectedServerAddress);
0139         });
0140 
0141         it('should have backend unavailable when neither PyroBird nor user API is configured', async () => {
0142             userConfigService.localServerUseApi.subject.next(false);
0143             serverConfigService.config.servedByPyrobird = false;
0144             await Promise.resolve();
0145 
0146             expect((service as any).isBackendAvailable).toBe(false);
0147             expect((service as any).serverAddress).toBe('');
0148         });
0149     });
0150 });