fix(ui): settings forms not reflecting saved values reliably (#648)

This commit is contained in:
Alex
2026-04-22 17:27:41 -04:00
committed by Zach Yale
parent d9d7a18d89
commit 78be516b74
20 changed files with 1176 additions and 384 deletions
@@ -1,14 +1,122 @@
import {describe, it} from 'vitest';
import {signal, type WritableSignal} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
// NOTE(frontend-seam): Real coverage here needs seams around settings hydration and the composed
// metadata preference subpanels so the container shell can be asserted without mounting the full
// nested settings runtime.
describe.skip('MetadataSettingsComponent', () => {
it('needs settings seams to verify metadata option bootstrap and bookdrop-download toggling', () => {
// TODO(seam): Cover loadSettings, initializeSettings, and onMetadataDownloadOnBookdropToggle once app settings and save helpers are exposed through deterministic doubles.
import {getTranslocoModule} from '../../../core/testing/transloco-testing';
import {AppSettingKey, type AppSettings} from '../../../shared/model/app-settings.model';
import {AppSettingsService} from '../../../shared/service/app-settings.service';
import {SettingsHelperService} from '../../../shared/service/settings-helper.service';
import {type MetadataRefreshOptions} from '../../metadata/model/request/metadata-refresh-options.model';
import {MetadataSettingsComponent} from './metadata-settings-component';
describe('MetadataSettingsComponent', () => {
let fixture: ComponentFixture<MetadataSettingsComponent>;
let component: MetadataSettingsComponent;
let appSettingsSignal: WritableSignal<AppSettings | null>;
let settingsHelper: {saveSetting: ReturnType<typeof vi.fn>};
beforeEach(async () => {
appSettingsSignal = signal<AppSettings | null>(null);
settingsHelper = {
saveSetting: vi.fn(),
};
await TestBed.configureTestingModule({
imports: [MetadataSettingsComponent, getTranslocoModule()],
providers: [
{provide: AppSettingsService, useValue: {appSettings: appSettingsSignal}},
{provide: SettingsHelperService, useValue: settingsHelper},
],
})
.overrideComponent(MetadataSettingsComponent, {
set: {
template: '',
imports: [],
},
})
.compileComponents();
fixture = TestBed.createComponent(MetadataSettingsComponent);
component = fixture.componentInstance;
});
it('needs child-panel seams to verify metadata option submissions across nested preferences', () => {
// TODO(seam): Cover onMetadataSubmit after the container shell is isolated from the provider, match-weight, persistence, and review child runtimes.
afterEach(() => {
TestBed.resetTestingModule();
vi.restoreAllMocks();
});
it('hydrates when metadata settings arrive after initial render', async () => {
await render();
expect(component.form.controls.metadataDownloadOnBookdrop.value).toBe(true);
expect(component.currentMetadataOptions).toBeUndefined();
const options = buildMetadataRefreshOptions();
appSettingsSignal.set({
defaultMetadataRefreshOptions: options,
metadataDownloadOnBookdrop: false,
} as AppSettings);
await render();
expect(component.form.controls.metadataDownloadOnBookdrop.value).toBe(false);
expect(component.currentMetadataOptions).toEqual(options);
});
it('persists bookdrop download toggles and metadata option submissions', async () => {
await render();
component.onMetadataDownloadOnBookdropToggle(false);
const options = buildMetadataRefreshOptions();
component.onMetadataSubmit(options);
expect(settingsHelper.saveSetting).toHaveBeenNthCalledWith(
1,
AppSettingKey.METADATA_DOWNLOAD_ON_BOOKDROP,
false
);
expect(settingsHelper.saveSetting).toHaveBeenNthCalledWith(
2,
AppSettingKey.QUICK_BOOK_MATCH,
options
);
});
async function render(): Promise<void> {
fixture.detectChanges();
await new Promise(resolve => setTimeout(resolve, 0));
await fixture.whenStable();
fixture.detectChanges();
}
});
function buildMetadataRefreshOptions(): MetadataRefreshOptions {
const FIELDS = [
'title', 'subtitle', 'description', 'authors', 'publisher', 'publishedDate',
'seriesName', 'seriesNumber', 'seriesTotal', 'isbn13', 'isbn10', 'language',
'pageCount', 'categories', 'cover', 'asin', 'goodreadsId', 'comicvineId',
'hardcoverId', 'hardcoverBookId', 'googleId', 'lubimyczytacId',
'amazonRating', 'amazonReviewCount', 'goodreadsRating', 'goodreadsReviewCount',
'hardcoverRating', 'hardcoverReviewCount', 'lubimyczytacRating',
'ranobedbId', 'ranobedbRating', 'audibleId', 'audibleRating',
'audibleReviewCount', 'moods', 'tags',
] as const;
const fieldOptions = Object.fromEntries(
FIELDS.map(field => [field, {p1: field === 'title' ? 'Amazon' : null, p2: null, p3: null, p4: null}])
) as unknown as MetadataRefreshOptions['fieldOptions'];
const enabledFields = Object.fromEntries(
FIELDS.map(field => [field, true])
) as unknown as MetadataRefreshOptions['enabledFields'];
return {
libraryId: null,
refreshCovers: false,
mergeCategories: true,
reviewBeforeApply: false,
replaceMode: 'REPLACE_MISSING',
fieldOptions,
enabledFields,
};
}