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, fakeAsync, tick } 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 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 };
0031
0032 serverConfigService = {
0033 config: {
0034 servedByPyrobird: false,
0035 apiAvailable: true,
0036 apiBaseUrl: 'http://localhost:5454',
0037 }
0038 };
0039
0040 TestBed.configureTestingModule({
0041 imports: [HttpClientModule], // Add this line
0042 providers: [
0043 UrlService,
0044 { provide: UserConfigService, useValue: userConfigService },
0045 { provide: ServerConfigService, useValue: serverConfigService }
0046 ]
0047 });
0048
0049 service = TestBed.inject(UrlService);
0050 });
0051
0052 describe('resolveDownloadUrl', () => {
0053 it('should handle relative URL without protocol when backend is available (Case 1.2)', fakeAsync(() => {
0054 userConfigService.localServerUseApi.subject.next(true);
0055 tick(); // Allow subscriptions to process
0056
0057 const inputUrl = '/path/to/file.root';
0058 const expectedUrl = 'http://localhost:5454/api/v1/download?f=%2Fpath%2Fto%2Ffile.root';
0059
0060 const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0061
0062 expect(resolvedUrl).toBe(expectedUrl);
0063 }));
0064
0065 it('should encode the input URL correctly in download endpoint', fakeAsync(() => {
0066 userConfigService.localServerUseApi.subject.next(true);
0067 tick();
0068
0069 const inputUrl = '/path with spaces/file.root';
0070 const expectedUrl = 'http://localhost:5454/api/v1/download?f=%2Fpath%20with%20spaces%2Ffile.root';
0071
0072 const resolvedUrl = service.resolveDownloadUrl(inputUrl);
0073 expect(resolvedUrl).toBe(expectedUrl);
0074 }));
0075 });
0076
0077 describe('resolveConvertUrl', () => {
0078 it('should construct convert URL when backend is available', fakeAsync(() => {
0079 userConfigService.localServerUseApi.subject.next(true);
0080 tick();
0081
0082 const inputUrl = 'https://example.com/file.root';
0083 const fileType = 'edm4eic';
0084 const entries = 'all';
0085 const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(inputUrl)}`;
0086
0087 const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0088 expect(resolvedUrl).toBe(expectedUrl);
0089 }));
0090
0091 it('should handle URLs starting with asset:// in convert URL', fakeAsync(() => {
0092 userConfigService.localServerUseApi.subject.next(true);
0093 tick();
0094
0095 const inputUrl = 'asset://data/sample.dat';
0096 const baseUri = document.baseURI.endsWith('/') ? document.baseURI : `${document.baseURI}/`;
0097 const resolvedAssetUrl = `${baseUri}assets/data/sample.dat`;
0098
0099 const fileType = 'edm4eic';
0100 const entries = 'all';
0101 const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(resolvedAssetUrl)}`;
0102
0103 const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0104 expect(resolvedUrl).toBe(expectedUrl);
0105 }));
0106
0107 it('should handle URLs with custom protocol alias epic:// in convert URL', fakeAsync(() => {
0108 userConfigService.localServerUseApi.subject.next(true);
0109 tick();
0110
0111 const inputUrl = 'epic://some/path/file.root';
0112 const resolvedAliasUrl = 'https://eic.github.io/epic/artifacts/some/path/file.root';
0113
0114 const fileType = 'edm4eic';
0115 const entries = 'all';
0116 const expectedUrl = `http://localhost:5454/api/v1/convert/${fileType}/${entries}?f=${encodeURIComponent(resolvedAliasUrl)}`;
0117
0118 const resolvedUrl = service.resolveConvertUrl(inputUrl, fileType, entries);
0119 expect(resolvedUrl).toBe(expectedUrl);
0120 }));
0121 });
0122
0123 describe('backend availability and server address', () => {
0124 it('should use user-configured server address if localServerUseApi is true', fakeAsync(() => {
0125 userConfigService.localServerUseApi.subject.next(true);
0126 userConfigService.localServerUrl.subject.next('http://customserver:1234');
0127 tick();
0128
0129 const expectedServerAddress = 'http://customserver:1234';
0130 expect((service as any).serverAddress).toBe(expectedServerAddress);
0131 }));
0132
0133 it('should have backend unavailable when neither PyroBird nor user API is configured', fakeAsync(() => {
0134 userConfigService.localServerUseApi.subject.next(false);
0135 serverConfigService.config.servedByPyrobird = false;
0136 tick();
0137
0138 expect((service as any).isBackendAvailable).toBe(false);
0139 expect((service as any).serverAddress).toBe('');
0140 }));
0141 });
0142 });