From 6532929cf20cd44c4b784d4bcfde2a306de8d864 Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 16 Jul 2026 14:29:47 +0200 Subject: [PATCH] fix(web): gate Plex authorized-no-servers messaging on released lock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adversarial review of the initial fix found that authorizedNoServers = isAuthorized && !hasServers ignored isLocked. Because boot.state updates on every poll observation, the "no eligible servers — sign out and retry" subtitle + summary rendered during the normal `finalizing` window (locked, authorized, no servers yet) alongside the "discovering your Plex servers…" status — and during `budget-exhausted` (large first sync still holding the lock, polling stopped, Sign out enabled) it urged the user to sign out mid-sync. That reintroduced the contradictory-state class the PR fixes. Gate the empty-servers presentation on !isLocked so it only appears at the genuine released-lock terminal. Add regression coverage: the pin-flow test now passes through the finalizing cell asserting the empty-servers messaging is absent while locked, plus a budget-exhausted-while-locked case. Both new guards were confirmed to fail on the ungated code (negative control). Refs #345 --- web/src/screens/PlexSourceScreen.test.tsx | 36 +++++++++++++++++++++++ web/src/screens/PlexSourceScreen.tsx | 7 ++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/web/src/screens/PlexSourceScreen.test.tsx b/web/src/screens/PlexSourceScreen.test.tsx index 0790723ee..1e85a0b94 100644 --- a/web/src/screens/PlexSourceScreen.test.tsx +++ b/web/src/screens/PlexSourceScreen.test.tsx @@ -188,6 +188,16 @@ describe('PlexSourceScreen', () => { fireEvent.click(screen.getByRole('button', { name: 'Sign in to Plex' })); await vi.waitFor(() => expect(screen.getByText(/Waiting for you to authorize/i)).toBeTruthy()); + // Authorized but STILL LOCKED = finalizing (discovery in progress). The empty-servers terminal + // messaging must NOT appear here — while locked this is the normal "discovering" window, not + // "no servers were found" (regression guard for the isLocked gate). + stateRef.current = { isAuthorized: true, isLocked: true, servers: [] }; + await vi.advanceTimersByTimeAsync(2000); + await vi.waitFor(() => expect(screen.getByText(/discovering your Plex servers/i)).toBeTruthy()); + expect(screen.queryByText(/Your Plex account is authorized/i)).toBeNull(); // no "sign out and retry" summary + expect(screen.queryByText(/no eligible servers were discovered/i)).toBeNull(); + expect(screen.queryByRole('button', { name: 'Sign in to Plex' })).toBeNull(); + // Lock releases while authorized but discovery found nothing: the distinct terminal state. stateRef.current = { isAuthorized: true, isLocked: false, servers: [] }; await vi.advanceTimersByTimeAsync(2000); @@ -230,4 +240,30 @@ describe('PlexSourceScreen', () => { expect(fetchSpy.mock.calls.some(([input, init]) => String(input).endsWith('/sign-out') && (init?.method ?? '').toUpperCase() === 'POST')).toBe(true) ); }); + + // #345 (review follow-up): a large first sync legitimately still holds the lock past the poll + // budget (budget-exhausted). The empty-servers "sign out and retry" messaging must NOT show — it + // would contradict the "Still working…" status and urge aborting an in-progress sync. + it('budget-exhausted while locked with no servers does not show the "no servers, sign out" messaging', async () => { + const stateRef = { current: { isAuthorized: false, isLocked: false, servers: [] } as PlexState }; + const counter = { state: 0 }; + installFetch(stateRef, counter); + vi.spyOn(window, 'open').mockReturnValue({} as Window); + vi.useFakeTimers(); + + render(); + await vi.waitFor(() => expect(screen.getByRole('button', { name: 'Sign in to Plex' })).toBeTruthy()); + + // Authorized but locked forever (large first sync holds the lock). + stateRef.current = { isAuthorized: true, isLocked: true, servers: [] }; + fireEvent.click(screen.getByRole('button', { name: 'Sign in to Plex' })); + await vi.waitFor(() => expect(screen.getByText(/discovering your Plex servers|Waiting/i)).toBeTruthy()); + + await vi.advanceTimersByTimeAsync(152_000); // consume the whole 150s budget + await vi.waitFor(() => expect(screen.getByText(/Still working/i)).toBeTruthy()); + + // The lock is still held, so no terminal "no servers were found" messaging. + expect(screen.queryByText(/Your Plex account is authorized/i)).toBeNull(); + expect(screen.queryByText(/no eligible servers were discovered/i)).toBeNull(); + }); }); diff --git a/web/src/screens/PlexSourceScreen.tsx b/web/src/screens/PlexSourceScreen.tsx index ea9e4c6ae..81beab971 100644 --- a/web/src/screens/PlexSourceScreen.tsx +++ b/web/src/screens/PlexSourceScreen.tsx @@ -220,7 +220,12 @@ export function PlexSourceScreen() { // authorized/unlocked/empty-servers state recoverable: Sign out is offered, Sign in is not. const hasPlexAccount = isAuthorized || hasServers; const needsFix = hasServers && !isAuthorized; - const authorizedNoServers = isAuthorized && !hasServers; + // Only a RELEASED lock is terminal: while `isLocked`, an authorized+empty observation is the + // normal `finalizing` (discovery in progress) or `budget-exhausted` (large first sync still + // running) window — NOT "no servers were found". Gating on `!isLocked` keeps the "no eligible + // servers, sign out and retry" messaging from contradicting the in-flight discovery status and + // from urging a sign-out that would abort a legitimately-running sync. + const authorizedNoServers = isAuthorized && !hasServers && !isLocked; const connectionSubtitle = !hasPlexAccount ? 'Not signed in.' : needsFix