fix(ui): catch promise errors in OPDS url copy (#2814)

This commit is contained in:
James Ward
2026-09-30 12:30:38 -06:00
committed by GitHub
parent 23a9371f48
commit ab6d4af42b
3 changed files with 135 additions and 9 deletions
@@ -1,8 +1,8 @@
import {signal} from '@angular/core'; import {signal} from '@angular/core';
import {TestBed} from '@angular/core/testing'; import {TestBed} from '@angular/core/testing';
import {of, type Observable} from 'rxjs'; import {of, type Observable} from 'rxjs';
import {describe, expect, it, vi} from 'vitest'; import {afterEach, describe, expect, it, vi} from 'vitest';
import {MessageService} from '@openng/optimus-ui/api'; import {MessageService, ToastMessageOptions} from '@openng/optimus-ui/api';
import {TranslocoService} from '@jsverse/transloco'; import {TranslocoService} from '@jsverse/transloco';
import {AppSettings} from '../../../shared/model/app-settings.model'; import {AppSettings} from '../../../shared/model/app-settings.model';
@@ -40,6 +40,7 @@ interface OpdsTestEnv {
userState: ReturnType<typeof signal<User | null>>; userState: ReturnType<typeof signal<User | null>>;
appSettingsState: ReturnType<typeof signal<AppSettings | null>>; appSettingsState: ReturnType<typeof signal<AppSettings | null>>;
getUser: () => Observable<OpdsUserV2[]>; getUser: () => Observable<OpdsUserV2[]>;
messageServiceAdd?: (options: ToastMessageOptions) => void;
} }
function setupOpdsTest(env: OpdsTestEnv): void { function setupOpdsTest(env: OpdsTestEnv): void {
@@ -55,7 +56,7 @@ function setupOpdsTest(env: OpdsTestEnv): void {
}, },
}, },
{provide: OpdsService, useValue: {getUser: env.getUser}}, {provide: OpdsService, useValue: {getUser: env.getUser}},
{provide: MessageService, useValue: {add: vi.fn()}}, {provide: MessageService, useValue: {add: env.messageServiceAdd ?? vi.fn()}},
{provide: TranslocoService, useValue: {translate: vi.fn((key: string) => key)}}, {provide: TranslocoService, useValue: {translate: vi.fn((key: string) => key)}},
], ],
}); });
@@ -63,6 +64,10 @@ function setupOpdsTest(env: OpdsTestEnv): void {
} }
describe('OpdsSettings', () => { describe('OpdsSettings', () => {
afterEach(() => {
vi.unstubAllGlobals();
});
it('clears the loading state when app settings arrive after permission resolution', () => { it('clears the loading state when app settings arrive after permission resolution', () => {
const userState = signal<User | null>(buildUser()); const userState = signal<User | null>(buildUser());
const appSettingsState = signal<AppSettings | null>(null); const appSettingsState = signal<AppSettings | null>(null);
@@ -109,4 +114,100 @@ describe('OpdsSettings', () => {
fixture.destroy(); fixture.destroy();
}); });
it('shows feedback after copying opds endpoint', async () => {
const userState = signal<User | null>(buildUser());
const appSettingsState = signal<AppSettings | null>(null);
const getUser = vi.fn(() => of([] as OpdsUserV2[]));
const messageServiceAdd = vi.fn();
const writeText = vi.fn().mockResolvedValue(null);
vi.stubGlobal('navigator', { clipboard: { writeText } });
setupOpdsTest({userState, appSettingsState, getUser, messageServiceAdd});
const fixture = TestBed.createComponent(OpdsSettings);
const component = fixture.componentInstance;
component.copyEndpoint();
expect(writeText).toHaveBeenCalledWith("http://localhost:6060/api/v1/opds");
await vi.waitFor(() => expect(messageServiceAdd).toHaveBeenCalledWith({
severity: 'success',
summary: 'common.success',
detail: 'settingsOpds.opdsCopied',
}));
});
it('shows error after copying opds endpoint failure', async () => {
const userState = signal<User | null>(buildUser());
const appSettingsState = signal<AppSettings | null>(null);
const getUser = vi.fn(() => of([] as OpdsUserV2[]));
const messageServiceAdd = vi.fn();
const writeText = vi.fn().mockRejectedValue(new Error());
vi.stubGlobal('navigator', { clipboard: { writeText } });
setupOpdsTest({userState, appSettingsState, getUser, messageServiceAdd});
const fixture = TestBed.createComponent(OpdsSettings);
const component = fixture.componentInstance;
component.copyEndpoint();
expect(writeText).toHaveBeenCalledWith("http://localhost:6060/api/v1/opds");
await vi.waitFor(() => expect(messageServiceAdd).toHaveBeenCalledWith({
severity: 'error',
summary: 'common.error',
detail: 'settingsOpds.copyFailed',
}));
});
it('shows feedback after copying komga endpoint', async () => {
const userState = signal<User | null>(buildUser());
const appSettingsState = signal<AppSettings | null>(null);
const getUser = vi.fn(() => of([] as OpdsUserV2[]));
const messageServiceAdd = vi.fn();
const writeText = vi.fn().mockResolvedValue(null);
vi.stubGlobal('navigator', { clipboard: { writeText } });
setupOpdsTest({userState, appSettingsState, getUser, messageServiceAdd});
const fixture = TestBed.createComponent(OpdsSettings);
const component = fixture.componentInstance;
component.copyKomgaEndpoint();
expect(writeText).toHaveBeenCalledWith("http://localhost:6060/komga");
await vi.waitFor(() => expect(messageServiceAdd).toHaveBeenCalledWith({
severity: 'success',
summary: 'common.success',
detail: 'settingsOpds.komgaCopied',
}));
});
it('shows error after copying komga endpoint failure', async () => {
const userState = signal<User | null>(buildUser());
const appSettingsState = signal<AppSettings | null>(null);
const getUser = vi.fn(() => of([] as OpdsUserV2[]));
const messageServiceAdd = vi.fn();
const writeText = vi.fn().mockRejectedValue(new Error());
vi.stubGlobal('navigator', { clipboard: { writeText } });
setupOpdsTest({userState, appSettingsState, getUser, messageServiceAdd});
const fixture = TestBed.createComponent(OpdsSettings);
const component = fixture.componentInstance;
component.copyKomgaEndpoint();
expect(writeText).toHaveBeenCalledWith("http://localhost:6060/komga");
await vi.waitFor(() => expect(messageServiceAdd).toHaveBeenCalledWith({
severity: 'error',
summary: 'common.error',
detail: 'settingsOpds.copyFailed',
}));
});
}); });
@@ -197,9 +197,21 @@ export class OpdsSettings implements OnInit {
} }
copyEndpoint(): void { copyEndpoint(): void {
navigator.clipboard.writeText(this.opdsEndpoint).then(() => { navigator.clipboard.writeText(this.opdsEndpoint)
this.showMessage('success', this.t.translate('common.success'), this.t.translate('settingsOpds.opdsCopied')); .then(() => {
}); this.showMessage(
'success',
this.t.translate('common.success'),
this.t.translate('settingsOpds.opdsCopied')
);
})
.catch(() => {
this.showMessage(
'error',
this.t.translate('common.error'),
this.t.translate('settingsOpds.copyFailed')
);
});
} }
toggleOpdsServer(): void { toggleOpdsServer(): void {
@@ -221,9 +233,21 @@ export class OpdsSettings implements OnInit {
} }
copyKomgaEndpoint(): void { copyKomgaEndpoint(): void {
navigator.clipboard.writeText(this.komgaEndpoint).then(() => { navigator.clipboard.writeText(this.komgaEndpoint)
this.showMessage('success', this.t.translate('common.success'), this.t.translate('settingsOpds.komgaCopied')); .then(() => {
}); this.showMessage(
'success',
this.t.translate('common.success'),
this.t.translate('settingsOpds.komgaCopied')
);
})
.catch(() => {
this.showMessage(
'error',
this.t.translate('common.error'),
this.t.translate('settingsOpds.copyFailed')
);
});
} }
toggleKomgaGroupUnknown(): void { toggleKomgaGroupUnknown(): void {
+1
View File
@@ -1113,6 +1113,7 @@
"deleteSuccess": "User deleted successfully", "deleteSuccess": "User deleted successfully",
"deleteError": "Failed to delete user", "deleteError": "Failed to delete user",
"opdsCopied": "OPDS endpoint copied to clipboard", "opdsCopied": "OPDS endpoint copied to clipboard",
"copyFailed": "Copy Failed",
"komgaCopied": "Komga API endpoint copied to clipboard", "komgaCopied": "Komga API endpoint copied to clipboard",
"sortUpdateSuccess": "Sort order updated successfully", "sortUpdateSuccess": "Sort order updated successfully",
"sortUpdateError": "Failed to update sort order", "sortUpdateError": "Failed to update sort order",