Merge pull request #979 from 4gray/agent/downloads-runtime-capabilities

refactor(downloads): use runtime availability capability
This commit is contained in:
4gray authored and GitHub committed 2026-05-22 13:04:50 +03:00
commit 33df53f266
3 files changed
+64 -10

No files matched your search

@@ -1,8 +1,18 @@
import { signal, Signal, WritableSignal } from '@angular/core';
import {
EnvironmentInjector,
Injector,
Signal,
WritableSignal,
createEnvironmentInjector,
runInInjectionContext,
signal,
} from '@angular/core';
import {
DownloadItem,
DownloadsService,
} from './downloads.service';
import { RuntimeCapabilitiesService } from './runtime-capabilities.service';
import { SettingsStore } from './settings-store.service';
type TestDownloadsService = {
downloads: WritableSignal<DownloadItem[]>;
@@ -74,6 +84,31 @@ describe('DownloadsService', () => {
return service;
}
it('reports availability through the runtime capability', () => {
const injector = createEnvironmentInjector(
[
DownloadsService,
{ provide: SettingsStore, useValue: {} },
{
provide: RuntimeCapabilitiesService,
useValue: { supportsDownloads: false },
},
],
Injector.NULL as unknown as EnvironmentInjector
);
try {
const service = runInInjectionContext(
injector,
() => new DownloadsService()
);
expect(service.isAvailable()).toBe(false);
} finally {
injector.destroy();
}
});
it('tracks loading and loaded state around a successful download list request', async () => {
const item = createDownload(1);
const pending = createDeferred<DownloadItem[]>();
+3 -1
View File
@@ -1,4 +1,5 @@
import { computed, inject, Injectable, OnDestroy, signal } from '@angular/core';
import { RuntimeCapabilitiesService } from './runtime-capabilities.service';
import { SettingsStore } from './settings-store.service';
export type DownloadStatus =
@@ -31,6 +32,7 @@ export interface DownloadItem {
@Injectable({ providedIn: 'root' })
export class DownloadsService implements OnDestroy {
private readonly runtime = inject(RuntimeCapabilitiesService);
private readonly settingsStore = inject(SettingsStore);
private unsubscribe?: () => void;
private loadDownloadsRequestId = 0;
@@ -48,7 +50,7 @@ export class DownloadsService implements OnDestroy {
readonly hasLoadedDownloads = this._hasLoadedDownloads.asReadonly();
/** Whether the download feature is available (Electron only) */
readonly isAvailable = computed(() => !!window.electron?.downloadsGetList);
readonly isAvailable = computed(() => this.runtime.supportsDownloads);
/** Whether there are any downloads */
readonly hasDownloads = computed(() => this.downloads().length > 0);
@@ -1,4 +1,9 @@
import { TestBed } from '@angular/core/testing';
import {
EnvironmentInjector,
Injector,
createEnvironmentInjector,
runInInjectionContext,
} from '@angular/core';
import { of } from 'rxjs';
import { PlaylistMeta } from '@iptvnator/shared/interfaces';
import { DatabaseService } from './database-electron.service';
@@ -23,6 +28,7 @@ describe('PlaylistDeleteActionService', () => {
let runtime: {
isElectron: boolean;
};
let injector: EnvironmentInjector;
beforeEach(() => {
databaseService = {
@@ -38,18 +44,29 @@ describe('PlaylistDeleteActionService', () => {
isElectron: false,
};
TestBed.configureTestingModule({
providers: [
PlaylistDeleteActionService,
injector = createEnvironmentInjector(
[
{ provide: DatabaseService, useValue: databaseService },
{ provide: PlaylistsService, useValue: playlistsService },
{ provide: RuntimeCapabilitiesService, useValue: runtime },
],
});
Injector.NULL as unknown as EnvironmentInjector
);
});
afterEach(() => {
injector.destroy();
});
function createService(): PlaylistDeleteActionService {
return runInInjectionContext(
injector,
() => new PlaylistDeleteActionService()
);
}
it('deletes browser playlists through PlaylistsService', async () => {
const service = TestBed.inject(PlaylistDeleteActionService);
const service = createService();
await expect(service.deletePlaylist(playlist)).resolves.toBe(true);
@@ -62,7 +79,7 @@ describe('PlaylistDeleteActionService', () => {
it('deletes Electron Xtream playlists through DatabaseService with progress options', async () => {
runtime.isElectron = true;
const onEvent = jest.fn();
const service = TestBed.inject(PlaylistDeleteActionService);
const service = createService();
await expect(
service.deletePlaylist(playlist, { onEvent })
@@ -83,7 +100,7 @@ describe('PlaylistDeleteActionService', () => {
it('deletes Electron non-Xtream playlists without progress options', async () => {
runtime.isElectron = true;
const service = TestBed.inject(PlaylistDeleteActionService);
const service = createService();
await expect(
service.deletePlaylist({