From ef0bb20094bfef474bfecfda24a9f9c14d600e6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrew=20Beveridge=20=E2=98=84=EF=B8=8F?= Date: Sun, 8 Mar 2026 03:52:43 -0400 Subject: [PATCH] Fix review session autosave: use edit-count instead of timer (#511) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - The 60s interval timer reset on every edit (React dependency tracking), so active editing could delay saves indefinitely - Replace with edit-count-based autosave: saves every 3 edits since last backup - Uses refs instead of callback dependencies so the save function stays stable ## Changes - `use-review-session-autosave.ts`: Replace `setInterval` with `useEffect` on `historyLength` that checks `editsSinceBackup >= 3` - Store data/initialData/historyLength in refs so `doSave` callback only depends on `apiClient` - Updated tests to match new behavior (11 tests passing) ## Testing - [x] All 608 frontend tests pass - [x] New tests for edit-count-based auto-save behavior @coderabbitai ignore --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.6 --- .../use-review-session-autosave.test.ts | 106 ++++++++++++------ frontend/hooks/use-review-session-autosave.ts | 59 ++++++---- pyproject.toml | 2 +- 3 files changed, 106 insertions(+), 61 deletions(-) diff --git a/frontend/hooks/__tests__/use-review-session-autosave.test.ts b/frontend/hooks/__tests__/use-review-session-autosave.test.ts index 3a651600..b9f4609c 100644 --- a/frontend/hooks/__tests__/use-review-session-autosave.test.ts +++ b/frontend/hooks/__tests__/use-review-session-autosave.test.ts @@ -62,14 +62,6 @@ function makeMockApiClient(overrides: Partial = {}): Lyri } describe('useReviewSessionAutoSave', () => { - beforeEach(() => { - jest.useFakeTimers() - }) - - afterEach(() => { - jest.useRealTimers() - }) - it('does not save when historyLength <= 1 (no edits)', async () => { const apiClient = makeMockApiClient() const { result } = renderHook(() => @@ -92,12 +84,13 @@ describe('useReviewSessionAutoSave', () => { it('saves when historyLength > 1 and trigger is manual', async () => { const apiClient = makeMockApiClient() + // Use historyLength=2 (below auto-save threshold of 3) const { result } = renderHook(() => useReviewSessionAutoSave({ jobId: 'job-1', data: makeCorrectionData(), initialData: makeCorrectionData(), - historyLength: 5, + historyLength: 2, isReadOnly: false, apiClient, }) @@ -110,7 +103,7 @@ describe('useReviewSessionAutoSave', () => { expect(apiClient.saveReviewSession).toHaveBeenCalledTimes(1) expect(apiClient.saveReviewSession).toHaveBeenCalledWith( expect.any(Object), - 4, // historyLength - 1 + 1, // historyLength - 1 'manual', expect.objectContaining({ total_segments: expect.any(Number), @@ -126,7 +119,7 @@ describe('useReviewSessionAutoSave', () => { jobId: 'job-1', data: makeCorrectionData(), initialData: makeCorrectionData(), - historyLength: 3, + historyLength: 2, isReadOnly: false, apiClient, }) @@ -144,54 +137,93 @@ describe('useReviewSessionAutoSave', () => { expect(apiClient.saveReviewSession).toHaveBeenCalledTimes(1) }) - it('does not save in read-only mode (no interval set)', () => { + it('does not auto-save in read-only mode', async () => { const apiClient = makeMockApiClient() + // historyLength=4 would trigger auto-save (4 >= 3 edits threshold) but read-only prevents it renderHook(() => useReviewSessionAutoSave({ jobId: 'job-1', data: makeCorrectionData(), initialData: makeCorrectionData(), - historyLength: 5, + historyLength: 4, isReadOnly: true, apiClient, }) ) - // Advance timer past 60s - act(() => { - jest.advanceTimersByTime(120_000) - }) - expect(apiClient.saveReviewSession).not.toHaveBeenCalled() }) - it('auto-saves on 60-second interval', async () => { + it('auto-saves after 3 edits since last backup', async () => { const apiClient = makeMockApiClient() - renderHook(() => - useReviewSessionAutoSave({ - jobId: 'job-1', - data: makeCorrectionData(), - initialData: makeCorrectionData(), - historyLength: 5, - isReadOnly: false, - apiClient, - }) + const props = { + jobId: 'job-1', + data: makeCorrectionData(), + initialData: makeCorrectionData(), + historyLength: 1, + isReadOnly: false, + apiClient, + } + + const { rerender } = renderHook( + (p) => useReviewSessionAutoSave(p), + { initialProps: props } ) - // Advance to 60s + // 1 edit (historyLength 2) — no save yet await act(async () => { - jest.advanceTimersByTime(60_000) + rerender({ ...props, historyLength: 2 }) }) + expect(apiClient.saveReviewSession).not.toHaveBeenCalled() + // 2 edits (historyLength 3) — auto-save fires (3 - 0 >= 3) + await act(async () => { + rerender({ ...props, historyLength: 3 }) + }) expect(apiClient.saveReviewSession).toHaveBeenCalledTimes(1) expect(apiClient.saveReviewSession).toHaveBeenCalledWith( expect.any(Object), - 4, + 2, 'auto', expect.any(Object) ) }) + it('auto-saves again after another 3 edits', async () => { + const apiClient = makeMockApiClient() + const props = { + jobId: 'job-1', + data: makeCorrectionData(), + initialData: makeCorrectionData(), + historyLength: 1, + isReadOnly: false, + apiClient, + } + + const { rerender } = renderHook( + (p) => useReviewSessionAutoSave(p), + { initialProps: props } + ) + + // First auto-save at historyLength 3 + await act(async () => { + rerender({ ...props, historyLength: 3 }) + }) + expect(apiClient.saveReviewSession).toHaveBeenCalledTimes(1) + + // 3 more edits from lastBackup=3: fires at historyLength 6 + await act(async () => { + rerender({ ...props, historyLength: 4 }) + }) + await act(async () => { + rerender({ ...props, historyLength: 5 }) + }) + await act(async () => { + rerender({ ...props, historyLength: 6 }) + }) + expect(apiClient.saveReviewSession).toHaveBeenCalledTimes(2) + }) + it('handles save failure gracefully without throwing', async () => { const consoleWarn = jest.spyOn(console, 'warn').mockImplementation() const apiClient = makeMockApiClient({ @@ -202,7 +234,7 @@ describe('useReviewSessionAutoSave', () => { jobId: 'job-1', data: makeCorrectionData(), initialData: makeCorrectionData(), - historyLength: 3, + historyLength: 2, isReadOnly: false, apiClient, }) @@ -233,7 +265,7 @@ describe('useReviewSessionAutoSave', () => { jobId: 'job-1', data: currentData, initialData, - historyLength: 3, + historyLength: 2, isReadOnly: false, apiClient, }) @@ -287,12 +319,13 @@ describe('useReviewSessionAutoSave', () => { it('saves with preview trigger', async () => { const apiClient = makeMockApiClient() + // Use historyLength=2 so auto-save doesn't fire first (threshold is 3) const { result } = renderHook(() => useReviewSessionAutoSave({ jobId: 'job-1', data: makeCorrectionData(), initialData: makeCorrectionData(), - historyLength: 10, + historyLength: 2, isReadOnly: false, apiClient, }) @@ -304,7 +337,7 @@ describe('useReviewSessionAutoSave', () => { expect(apiClient.saveReviewSession).toHaveBeenCalledWith( expect.any(Object), - 9, + 1, 'preview', expect.any(Object) ) @@ -314,12 +347,13 @@ describe('useReviewSessionAutoSave', () => { const apiClient = makeMockApiClient({ saveReviewSession: jest.fn().mockResolvedValue({ status: 'error' }), }) + // Use historyLength=2 so auto-save doesn't fire const { result } = renderHook(() => useReviewSessionAutoSave({ jobId: 'job-1', data: makeCorrectionData(), initialData: makeCorrectionData(), - historyLength: 5, + historyLength: 2, isReadOnly: false, apiClient, }) diff --git a/frontend/hooks/use-review-session-autosave.ts b/frontend/hooks/use-review-session-autosave.ts index 44169b5d..74cc28f2 100644 --- a/frontend/hooks/use-review-session-autosave.ts +++ b/frontend/hooks/use-review-session-autosave.ts @@ -48,6 +48,8 @@ function computeSummary(initialData: CorrectionData, currentData: CorrectionData } } +const AUTO_SAVE_EVERY_N_EDITS = 3 + export function useReviewSessionAutoSave({ jobId, data, @@ -58,27 +60,35 @@ export function useReviewSessionAutoSave({ }: UseReviewSessionAutoSaveOptions) { const lastBackupHistoryLength = useRef(0) const isSaving = useRef(false) + // Store latest values in refs so visibilitychange handler always has current data + const dataRef = useRef(data) + const initialDataRef = useRef(initialData) + const historyLengthRef = useRef(historyLength) + dataRef.current = data + initialDataRef.current = initialData + historyLengthRef.current = historyLength - const saveSession = useCallback( + const doSave = useCallback( async (trigger: 'auto' | 'preview' | 'manual') => { + const currentHistoryLength = historyLengthRef.current // No edits yet (only initial state in history) - if (historyLength <= 1) return + if (currentHistoryLength <= 1) return // No new edits since last backup - if (historyLength === lastBackupHistoryLength.current) return + if (currentHistoryLength === lastBackupHistoryLength.current) return // Already saving if (isSaving.current) return isSaving.current = true try { - const summary = computeSummary(initialData, data) + const summary = computeSummary(initialDataRef.current, dataRef.current) const result = await apiClient.saveReviewSession( - data, - historyLength - 1, + dataRef.current, + currentHistoryLength - 1, trigger, summary ) if (result.status === 'saved' || result.status === 'skipped') { - lastBackupHistoryLength.current = historyLength + lastBackupHistoryLength.current = currentHistoryLength } } catch (err) { // Don't break the editing experience on backup failure @@ -87,36 +97,37 @@ export function useReviewSessionAutoSave({ isSaving.current = false } }, - [data, historyLength, initialData, apiClient, jobId] + [apiClient] ) - // 60-second interval auto-save + // Auto-save every N edits useEffect(() => { - if (isReadOnly) return + if (isReadOnly || historyLength <= 1) return + const editsSinceBackup = historyLength - lastBackupHistoryLength.current + if (editsSinceBackup >= AUTO_SAVE_EVERY_N_EDITS) { + doSave('auto') + } + }, [historyLength, isReadOnly, doSave]) - const timer = setInterval(() => { - saveSession('auto') - }, 60_000) - - return () => clearInterval(timer) - }, [saveSession, isReadOnly]) - - // Save on page hide (more reliable than beforeunload for async saves) + // Save on page hide useEffect(() => { if (isReadOnly) return const handler = () => { - if (document.visibilityState === 'hidden' && - historyLength > 1 && - historyLength !== lastBackupHistoryLength.current) { - // Fire-and-forget: we can't await during page hide - saveSession('auto') + if (document.visibilityState === 'hidden') { + doSave('auto') } } document.addEventListener('visibilitychange', handler) return () => document.removeEventListener('visibilitychange', handler) - }, [saveSession, isReadOnly, historyLength]) + }, [isReadOnly, doSave]) + + // Expose saveSession that uses current refs + const saveSession = useCallback( + (trigger: 'auto' | 'preview' | 'manual') => doSave(trigger), + [doSave] + ) return { saveSession } } diff --git a/pyproject.toml b/pyproject.toml index e146700d..9b61d170 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "karaoke-gen" -version = "0.138.1" +version = "0.138.2" description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens." authors = ["Andrew Beveridge "] license = "MIT"