fix(651): review round 8 — put the selection-id predicate at the boundary, not the site
Round 7 added the int32 check inside `isSearchPickerOption` — the place the defect was found — which left every other door into editor state open. A malformed successful response carrying `1.5` or `2147483648` still entered `draft` through list-backed options and through the selection restored from the detail read, keeping Save enabled and sending a value the API cannot bind, while the identical value arriving via SearchPicker was correctly rejected. The predicate now lives once, in `web/src/api/selectionId.ts`, and sits on every path by which an id from the wire becomes editor state. The class crosses all three screens, not just the one the finding named, so all three are covered: - RerunCollectionsScreen: `toPickerOptions` (3 list branches) + `draftFromRerun` - PlaylistsScreen: `toPickerOptions` (3 list branches) + `draftFromItem` (4 id fields) - FillerPresetsScreen: `draftFromPreset` (5 id fields) + the collection-family browse options - pickers.tsx: `isSearchPickerOption` now delegates rather than carrying its own copy An unbindable id is treated as ABSENT, never coerced — rounding 1.5 to 1 would submit a DIFFERENT record — so it surfaces as "no selection" with Save disabled and a visible reason; an option that cannot be selected safely is dropped rather than rendered. Five regressions assert zero writes are reachable via each previously-unguarded path. Also corrects two of my own test descriptions, per the review: the padded-ETag test is a regression guard rather than a round-7 defect demonstration (Headers strips outer whitespace before the app sees it), and the late-settlement test guards the abort/race COMPOSITION — what it actually fails is an abort-only implementation whose fetch ignores its signal, which is why its stub ignores `init.signal`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ export * from './imageFolders';
|
||||
export * from './languages';
|
||||
export * from './libraries';
|
||||
export * from './libraryBrowse';
|
||||
export * from './selectionId';
|
||||
export * from './logs';
|
||||
export * from './maintenance';
|
||||
export * from './mediaDetail';
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// A *selection id* is any id an editor stores and later submits as an entity reference —
|
||||
// `selectedId` on a rerun collection, `collectionId`/`mediaItemId`/… on a playlist item or filler
|
||||
// preset. The API binds every one of them as a 32-bit integer, so a value outside that domain is
|
||||
// not merely odd: it renders and commits happily and then fails on write.
|
||||
//
|
||||
// This lives in ONE place on purpose. #651 round 7 added the check inside the search picker's
|
||||
// option validator — the site where the defect was found — leaving list-backed options and the
|
||||
// selection restored from a detail read unguarded, so the identical malformed value entered editor
|
||||
// state through a different door (round 8). The predicate belongs at the BOUNDARY the class
|
||||
// crosses: every path by which an id from the wire becomes editor state.
|
||||
export const INT32_MIN = -2_147_483_648;
|
||||
export const INT32_MAX = 2_147_483_647;
|
||||
|
||||
export function isSelectionId(value: unknown): value is number {
|
||||
return typeof value === 'number' && Number.isInteger(value) && value >= INT32_MIN && value <= INT32_MAX;
|
||||
}
|
||||
|
||||
// Normalize an id arriving from the wire into editor state. Anything the API cannot bind is treated
|
||||
// as ABSENT rather than carried: the editor then shows "no selection" with Save disabled — visible
|
||||
// and honest — instead of a value that looks selected and fails on submit. Never silently coerce
|
||||
// (rounding 1.5 to 1 would submit a *different* record).
|
||||
export function selectionIdOrNull(value: unknown): null | number {
|
||||
return isSelectionId(value) ? value : null;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { useEffect, useId, useRef, useState, type KeyboardEvent as ReactKeyboard
|
||||
import { Check, Search, X } from 'lucide-react';
|
||||
import { Spinner } from '../components';
|
||||
import { useIsMountedRef } from '../hooks';
|
||||
import { isSelectionId } from '../api';
|
||||
|
||||
// The shape every search-backed picker resolves to. Deliberately structural rather than the
|
||||
// generated `SchedulingPickerOption` DTO: the scheduling pickers pass that DTO straight through,
|
||||
@@ -44,25 +45,15 @@ const SEARCH_TIMEOUT_MS = 10_000;
|
||||
// and throws on `option.id` during render; an element with a missing or wrong-typed `id`/`name`
|
||||
// yields a broken option or commits an invalid value (#651 round 6). Nothing between the network
|
||||
// and `onSelect` re-checks these, so validate every element before storing it.
|
||||
// The id must be what the API can actually BIND — a 32-bit integer. `Number.isFinite` accepts
|
||||
// `1.5` and values beyond `int32`, which render and commit happily and then fail server-side on a
|
||||
// `selectedId` the model binder cannot parse (#651 round 7).
|
||||
const INT32_MIN = -2_147_483_648;
|
||||
const INT32_MAX = 2_147_483_647;
|
||||
|
||||
// The id must be what the API can actually BIND — see `isSelectionId`. This is one of several
|
||||
// ingresses for the same class; the predicate is shared so they cannot drift apart (#651 round 8).
|
||||
function isSearchPickerOption(value: unknown): value is SearchPickerOption {
|
||||
if (typeof value !== 'object' || value === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const candidate = value as { id?: unknown; name?: unknown };
|
||||
return (
|
||||
typeof candidate.id === 'number' &&
|
||||
Number.isInteger(candidate.id) &&
|
||||
candidate.id >= INT32_MIN &&
|
||||
candidate.id <= INT32_MAX &&
|
||||
typeof candidate.name === 'string'
|
||||
);
|
||||
return isSelectionId(candidate.id) && typeof candidate.name === 'string';
|
||||
}
|
||||
|
||||
export function SearchPicker({
|
||||
|
||||
@@ -14,7 +14,9 @@ import {
|
||||
getShow,
|
||||
messageFromFillerPresetError,
|
||||
searchLibraryPickerOptions,
|
||||
selectionIdOrNull,
|
||||
updateFillerPreset,
|
||||
isSelectionId,
|
||||
LIBRARY_PICKER_MIN_QUERY,
|
||||
type CreateFillerPresetRequest,
|
||||
type FillerPreset,
|
||||
@@ -215,19 +217,22 @@ function defaultDraft(): Draft {
|
||||
function draftFromPreset(preset: FillerPreset & Draft): Draft {
|
||||
return {
|
||||
allowWatermarks: preset.allowWatermarks,
|
||||
collectionId: preset.collectionId,
|
||||
// Every entity-reference id is normalized on the way in: a value the API cannot bind is treated
|
||||
// as ABSENT so it surfaces as "no selection" (Save disabled) rather than as an apparently-valid
|
||||
// selection that fails on submit (#651 round 8).
|
||||
collectionId: selectionIdOrNull(preset.collectionId),
|
||||
collectionType: preset.collectionType,
|
||||
count: preset.count,
|
||||
duration: preset.duration,
|
||||
expression: preset.expression,
|
||||
fillerKind: preset.fillerKind,
|
||||
fillerMode: preset.fillerMode,
|
||||
mediaItemId: preset.mediaItemId,
|
||||
multiCollectionId: preset.multiCollectionId,
|
||||
mediaItemId: selectionIdOrNull(preset.mediaItemId),
|
||||
multiCollectionId: selectionIdOrNull(preset.multiCollectionId),
|
||||
name: preset.name,
|
||||
padToNearestMinute: preset.padToNearestMinute,
|
||||
playlistId: preset.playlistId,
|
||||
smartCollectionId: preset.smartCollectionId,
|
||||
playlistId: selectionIdOrNull(preset.playlistId),
|
||||
smartCollectionId: selectionIdOrNull(preset.smartCollectionId),
|
||||
useChaptersAsMediaItems: preset.useChaptersAsMediaItems
|
||||
};
|
||||
}
|
||||
@@ -670,7 +675,8 @@ function FillerEditor({ mode }: { mode: { id: number; kind: 'edit' } | { kind: '
|
||||
: []),
|
||||
...pickerItems.flatMap((item) => {
|
||||
const id = activeConfig?.itemId(item);
|
||||
return id == null ? [] : [{ label: item.title ?? `#${id}`, value: String(id) }];
|
||||
// Drop, don't offer: an option whose id the API cannot bind is unselectable by construction.
|
||||
return isSelectionId(id) ? [{ label: item.title ?? `#${id}`, value: String(id) }] : [];
|
||||
})
|
||||
];
|
||||
|
||||
|
||||
@@ -34,6 +34,8 @@ import {
|
||||
messageFromPlaylistError,
|
||||
previewPlaylist,
|
||||
searchLibraryPickerOptions,
|
||||
selectionIdOrNull,
|
||||
isSelectionId,
|
||||
updatePlaylist,
|
||||
updatePlaylistGroup,
|
||||
LIBRARY_PICKER_MIN_QUERY,
|
||||
@@ -153,6 +155,13 @@ function orderOptionsWithCurrent(type: CollectionType, current: PlaybackOrder):
|
||||
return options.some((option) => option.value === current) ? options : [...options, orderOption(current)];
|
||||
}
|
||||
|
||||
// See RerunCollectionsScreen: one ingress for list-backed options, dropping ids the API cannot bind.
|
||||
function toPickerOptions(list: Array<{ id: unknown; name?: null | string }>): PickerOption[] {
|
||||
return list.flatMap((entry) =>
|
||||
isSelectionId(entry.id) ? [{ id: entry.id, name: entry.name ?? `#${entry.id}` }] : []
|
||||
);
|
||||
}
|
||||
|
||||
function loadPickerOptions(type: CollectionType, signal?: AbortSignal): Promise<PickerLoadResult> {
|
||||
const config = configFor(type);
|
||||
if (!config) {
|
||||
@@ -162,12 +171,12 @@ function loadPickerOptions(type: CollectionType, signal?: AbortSignal): Promise<
|
||||
switch (config.source) {
|
||||
case 'collection':
|
||||
return getCollections().then((list) => {
|
||||
const items = list.map((entry) => ({ id: entry.id, name: entry.name ?? `#${entry.id}` }));
|
||||
const items = toPickerOptions(list);
|
||||
return { hint: 'none' as const, items };
|
||||
});
|
||||
case 'multi':
|
||||
return loadAllPages(getMultiCollections, undefined, undefined, signal).then(({ complete, items: list }) => {
|
||||
const items = list.map((entry) => ({ id: entry.id, name: entry.name ?? `#${entry.id}` }));
|
||||
const items = toPickerOptions(list);
|
||||
if (!complete && !signal?.aborted) {
|
||||
// #644 follow-up F3: a superseded/aborted load (a type switch mid-load) also returns
|
||||
// `complete: false` — that's expected, not a defect, so don't warn on it.
|
||||
@@ -177,7 +186,7 @@ function loadPickerOptions(type: CollectionType, signal?: AbortSignal): Promise<
|
||||
});
|
||||
case 'smart':
|
||||
return getSmartCollections().then((list) => {
|
||||
const items = list.map((entry) => ({ id: entry.id, name: entry.name ?? `#${entry.id}` }));
|
||||
const items = toPickerOptions(list);
|
||||
return { hint: 'none' as const, items };
|
||||
});
|
||||
default:
|
||||
@@ -227,19 +236,19 @@ function draftFromItem(item: PlaylistItem): DraftItem {
|
||||
|
||||
switch (source) {
|
||||
case 'collection':
|
||||
selectedId = item.collectionId ?? null;
|
||||
selectedId = selectionIdOrNull(item.collectionId);
|
||||
selectedName = item.collectionName ?? '';
|
||||
break;
|
||||
case 'multi':
|
||||
selectedId = item.multiCollectionId ?? null;
|
||||
selectedId = selectionIdOrNull(item.multiCollectionId);
|
||||
selectedName = item.multiCollectionName ?? '';
|
||||
break;
|
||||
case 'smart':
|
||||
selectedId = item.smartCollectionId ?? null;
|
||||
selectedId = selectionIdOrNull(item.smartCollectionId);
|
||||
selectedName = item.smartCollectionName ?? '';
|
||||
break;
|
||||
default:
|
||||
selectedId = item.mediaItemId ?? null;
|
||||
selectedId = selectionIdOrNull(item.mediaItemId);
|
||||
selectedName = item.mediaItemName ?? '';
|
||||
}
|
||||
|
||||
|
||||
@@ -784,7 +784,10 @@ describe('RerunCollectionsScreen', () => {
|
||||
});
|
||||
|
||||
it('a padded ETag is TRIMMED and sent as If-Match, not dropped', async () => {
|
||||
// The other half of "one class": a usable token surrounded by whitespace must still be used.
|
||||
// The other half of "one class": a usable token surrounded by whitespace must still be USED.
|
||||
// NOTE this is a regression guard, not a demonstration of a defect — `Headers` strips outer
|
||||
// HTTP whitespace before the app sees it, so this passes pre-#651-round-7 too. It exists so a
|
||||
// future `usableEtag` that rejects (rather than trims) padding cannot land silently.
|
||||
const fetchMock = mockApi({
|
||||
list: [storedSelection],
|
||||
onRequest: (url, method) => {
|
||||
@@ -908,6 +911,10 @@ describe('RerunCollectionsScreen', () => {
|
||||
}
|
||||
});
|
||||
|
||||
// Guards the ABORT/RACE COMPOSITION, not a round-6 defect: round 6's race guard already covered
|
||||
// the ordinary case. What this fails is an abort-ONLY implementation whose fetch ignores its
|
||||
// signal — which is exactly the mutation it was written for, and why the stub below deliberately
|
||||
// ignores `init.signal`.
|
||||
it('a detail read that settles LATE, after the deadline, cannot revive the editor', async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
@@ -954,4 +961,67 @@ describe('RerunCollectionsScreen', () => {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
// ---- #651 round 8: the int32 boundary, on EVERY ingress ----
|
||||
//
|
||||
// Round 7 put the predicate inside the search picker's option validator — the site the defect was
|
||||
// found at — leaving the two other doors into `draft.selectedId` open. These pin them.
|
||||
|
||||
it.each([
|
||||
{ id: 1.5, label: 'a fractional selectedId' },
|
||||
{ id: 2_147_483_648, label: 'a selectedId above int32' },
|
||||
{ id: -2_147_483_649, label: 'a selectedId below int32' }
|
||||
])('round 8: $label in the DETAIL response cannot enable Save or reach a PUT', async ({ id }) => {
|
||||
const fetchMock = mockApi({
|
||||
list: [storedSelection],
|
||||
onRequest: (url, method) =>
|
||||
url === '/api/v1/rerun-collections/9' && method === 'GET'
|
||||
? detailResponse({ ...storedSelection, collectionType: 'Collection', selectedId: id, selectedName: 'Bogus' })
|
||||
: null
|
||||
});
|
||||
|
||||
render(<RerunCollectionsScreen />);
|
||||
fireEvent.click(await screen.findByText('Stored Rerun'));
|
||||
await screen.findByPlaceholderText('Rerun collection name');
|
||||
|
||||
// Treated as ABSENT — visibly, with the reason — rather than as a selection that fails on write.
|
||||
expect(screen.getByText('A selection is required')).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'Save rerun collection' })).toBeDisabled();
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Save rerun collection' }));
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
expect(
|
||||
fetchMock.mock.calls.filter(([u, i]) => u === '/api/v1/rerun-collections/9' && (i?.method ?? '').toUpperCase() === 'PUT')
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ id: 1.5, label: 'a fractional id' },
|
||||
{ id: 2_147_483_648, label: 'an id above int32' }
|
||||
])('round 8: a LIST-BACKED option with $label is not offered, so it cannot be selected or saved', async ({ id }) => {
|
||||
const fetchMock = mockApi({
|
||||
list: [],
|
||||
onRequest: (url, method) =>
|
||||
url === '/api/v1/collections' && method === 'GET'
|
||||
? jsonResponse([
|
||||
{ collectionType: 'Collection', id, name: 'Bogus Collection', state: 'Normal', useCustomPlaybackOrder: false },
|
||||
{ collectionType: 'Collection', id: 5, name: 'Favorites', state: 'Normal', useCustomPlaybackOrder: false }
|
||||
])
|
||||
: null
|
||||
});
|
||||
|
||||
render(<RerunCollectionsScreen />);
|
||||
fireEvent.click(await screen.findByRole('button', { name: 'New rerun collection' }));
|
||||
|
||||
const picker = (await screen.findAllByRole('combobox'))[1];
|
||||
// The bindable option is offered; the malformed one is dropped rather than rendered.
|
||||
expect(await within(picker).findByText('Favorites')).toBeInTheDocument();
|
||||
expect(within(picker).queryByText('Bogus Collection')).not.toBeInTheDocument();
|
||||
expect(within(picker).queryByRole('option', { name: 'Bogus Collection' })).not.toBeInTheDocument();
|
||||
|
||||
// ...and no POST can carry it.
|
||||
expect(
|
||||
fetchMock.mock.calls.filter(([u, i]) => u === '/api/v1/rerun-collections' && (i?.method ?? '').toUpperCase() === 'POST')
|
||||
).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,7 +15,9 @@ import {
|
||||
loadAllPages,
|
||||
messageFromRerunCollectionError,
|
||||
searchLibraryPickerOptions,
|
||||
selectionIdOrNull,
|
||||
updateRerunCollection,
|
||||
isSelectionId,
|
||||
LIBRARY_PICKER_MIN_QUERY,
|
||||
type CreateRerunCollectionRequest,
|
||||
type LibraryBrowseMediaType,
|
||||
@@ -106,6 +108,15 @@ function orderOptionsWithCurrent(type: RerunCollectionType, current: PlaybackOrd
|
||||
return options.some((option) => option.value === current) ? options : [...options, orderOption(current)];
|
||||
}
|
||||
|
||||
// One ingress for list-backed options. An id the API cannot bind is DROPPED rather than offered:
|
||||
// an unselectable option is better than one that fails on save (#651 round 8 — the boundary, not
|
||||
// the site).
|
||||
function toPickerOptions(list: Array<{ id: unknown; name?: null | string }>): PickerOption[] {
|
||||
return list.flatMap((entry) =>
|
||||
isSelectionId(entry.id) ? [{ id: entry.id, name: entry.name ?? `#${entry.id}` }] : []
|
||||
);
|
||||
}
|
||||
|
||||
function loadPickerOptions(type: RerunCollectionType, signal?: AbortSignal): Promise<PickerLoadResult> {
|
||||
const config = COLLECTION_TYPES.find((entry) => entry.value === type);
|
||||
if (!config) {
|
||||
@@ -115,12 +126,12 @@ function loadPickerOptions(type: RerunCollectionType, signal?: AbortSignal): Pro
|
||||
switch (config.source) {
|
||||
case 'collection':
|
||||
return getCollections().then((list) => {
|
||||
const items = list.map((entry) => ({ id: entry.id, name: entry.name ?? `#${entry.id}` }));
|
||||
const items = toPickerOptions(list);
|
||||
return { hint: 'none' as const, items };
|
||||
});
|
||||
case 'multi':
|
||||
return loadAllPages(getMultiCollections, undefined, undefined, signal).then(({ complete, items: list }) => {
|
||||
const items = list.map((entry) => ({ id: entry.id, name: entry.name ?? `#${entry.id}` }));
|
||||
const items = toPickerOptions(list);
|
||||
if (!complete && !signal?.aborted) {
|
||||
// #644 follow-up F3: a superseded/aborted load (Retry, or a type switch mid-load) also
|
||||
// returns `complete: false` — that's expected, not a defect, so don't warn on it.
|
||||
@@ -130,7 +141,7 @@ function loadPickerOptions(type: RerunCollectionType, signal?: AbortSignal): Pro
|
||||
});
|
||||
case 'smart':
|
||||
return getSmartCollections().then((list) => {
|
||||
const items = list.map((entry) => ({ id: entry.id, name: entry.name ?? `#${entry.id}` }));
|
||||
const items = toPickerOptions(list);
|
||||
return { hint: 'none' as const, items };
|
||||
});
|
||||
default:
|
||||
@@ -238,7 +249,9 @@ function draftFromRerun(rerun: RerunCollection): Draft {
|
||||
firstRunPlaybackOrder: rerun.firstRunPlaybackOrder,
|
||||
name: rerun.name ?? '',
|
||||
rerunPlaybackOrder: rerun.rerunPlaybackOrder,
|
||||
selectedId: rerun.selectedId ?? null,
|
||||
// The other ingress: a malformed id in an otherwise-successful detail response must not become
|
||||
// an apparently-valid selection. Treated as absent, so Save is disabled and says why.
|
||||
selectedId: selectionIdOrNull(rerun.selectedId),
|
||||
selectedName: rerun.selectedName ?? ''
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user