diff --git a/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.html b/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.html index 311c14e51..c6af755be 100644 --- a/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.html +++ b/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.html @@ -121,6 +121,37 @@ +
+
+
+ {{ t('kobo.apiPath') }} +
+ + + +
+
+

+ {{ t('kobo.apiPathDesc') }} +

+
+
+
diff --git a/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.spec.ts b/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.spec.ts index ad869df8d..158c1a009 100644 --- a/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.spec.ts +++ b/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.spec.ts @@ -12,6 +12,7 @@ import {UserService, type User} from '../../../user-management/user.service'; import {KoboService, type KoboSyncSettings} from './kobo.service'; import {KoboSyncSettingsComponent} from './kobo-sync-settings-component'; import {AppSettingsService} from '../../../../../shared/service/app-settings.service'; +import {ConfirmationService, MessageService} from '@openng/optimus-ui/api'; const DEFAULT_KOBO_SYNC_SETTINGS: KoboSyncSettings = { token: '', @@ -243,6 +244,83 @@ describe('KoboSyncSettingsComponent', () => { }); expect(fixture.nativeElement.querySelector('input#koboToken')).not.toBeNull(); + const apiPathInput = fixture.nativeElement.querySelector('input#koboApiPath') as HTMLInputElement | null; + expect(apiPathInput).not.toBeNull(); + expect(apiPathInput?.value).toBe(`${window.location.origin}/api/kobo/token-123`); + + fixture.destroy(); + }); + + it('recomputes the API path when the token is regenerated', () => { + const userState = signal(buildUser({canSyncKobo: true})); + const appSettingsState = signal(null); + const initialSettings: KoboSyncSettings = { + ...DEFAULT_KOBO_SYNC_SETTINGS, + token: 'old-token', + syncEnabled: true, + }; + + TestBed.configureTestingModule({ + imports: [KoboSyncSettingsComponent], + providers: [ + {provide: UserService, useValue: {currentUser: () => userState()}}, + {provide: AppSettingsService, useValue: {appSettings: () => appSettingsState()}}, + { + provide: KoboService, + useValue: { + getUser: () => of(initialSettings), + updateSettings: (s: KoboSyncSettings) => of(s), + createOrUpdateToken: () => of({...initialSettings, token: 'new-token'}), + }, + }, + {provide: SettingsHelperService, useValue: {saveSetting: vi.fn()}}, + {provide: ShelfService, useValue: {reloadShelves: vi.fn()}}, + {provide: TranslocoService, useValue: {translate: vi.fn((key: string) => key)}}, + ], + }); + // Auto-accept the confirmation dialog and stub the toast so no template is needed. + // The rendered [value]="koboApiPath" binding is covered by the hydration test above; + // here we assert the recompute at the component-state level after regeneration. + TestBed.overrideComponent(KoboSyncSettingsComponent, { + set: { + template: '', + providers: [ + {provide: ConfirmationService, useValue: {confirm: (o: {accept?: () => void}) => o.accept?.()}}, + {provide: MessageService, useValue: {add: vi.fn()}}, + ], + }, + }); + + const fixture = TestBed.createComponent(KoboSyncSettingsComponent); + const component = fixture.componentInstance; + + TestBed.flushEffects(); + expect(component.koboApiPath).toBe(`${window.location.origin}/api/kobo/old-token`); + + component.confirmRegenerateToken(); + + expect(component.syncForm.controls.token.value).toBe('new-token'); + expect(component.koboApiPath).toBe(`${window.location.origin}/api/kobo/new-token`); + + fixture.destroy(); + }); + + it('builds the full Kobo API path from the token and is empty without one', () => { + const userState = signal(buildUser({canSyncKobo: true})); + const appSettingsState = signal(null); + + setupKoboTest({userState, appSettingsState, getUser: () => of(DEFAULT_KOBO_SYNC_SETTINGS)}); + + const fixture = TestBed.createComponent(KoboSyncSettingsComponent); + const component = fixture.componentInstance; + + TestBed.flushEffects(); + + expect(component.koboApiPath).toBe(''); + + component.syncForm.controls.token.setValue('token-xyz'); + expect(component.koboApiPath).toBe(`${window.location.origin}/api/kobo/token-xyz`); + fixture.destroy(); }); }); diff --git a/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.ts b/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.ts index 8e35f8165..cce50f34b 100644 --- a/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.ts +++ b/frontend/src/app/features/settings/device-settings/component/kobo-sync-settings/kobo-sync-settings-component.ts @@ -47,6 +47,12 @@ export class KoboSyncSettingsComponent implements OnInit { isAdmin = false; credentialsSaved = false; showToken = false; + readonly koboApiBase = `${window.location.origin}/api/kobo`; + + get koboApiPath(): string { + const token = this.syncForm.controls.token.value; + return token ? `${this.koboApiBase}/${token}` : ''; + } readonly syncForm = this.fb.nonNullable.group({ token: [''], diff --git a/frontend/src/i18n/en/settings-device.json b/frontend/src/i18n/en/settings-device.json index a38b3345f..794461a36 100644 --- a/frontend/src/i18n/en/settings-device.json +++ b/frontend/src/i18n/en/settings-device.json @@ -49,6 +49,8 @@ "autoAddDesc": "When enabled, newly added books will be automatically added to your Kobo shelf for syncing. This eliminates the need to manually add each book to the shelf.", "token": "Kobo Sync Token", "tokenDesc": "Your unique sync token for authenticating with your Kobo device. Keep this secure.", + "apiPath": "Kobo API Path", + "apiPathDesc": "The full Kobo sync URL for your device. Use this as the API endpoint when configuring your Kobo.", "tokenManagement": "Token Management", "regenerateToken": "Regenerate Token", "tokenManagementDesc": "Regenerate your sync token if needed. This will invalidate the current token.",