From 2dfa5acc123cfa85c857383b28e8bf9f72773a1d Mon Sep 17 00:00:00 2001 From: Sara Hentzel Date: Fri, 13 Nov 2026 14:35:51 -0600 Subject: [PATCH 1/2] TT-7873 match existing multiverse range to replace instead of expecting existing to be one verse --- .../transcribe/useTranscribeAsr.test.ts | 31 +++++++++++++++++++ .../transcribe/useTranscribeAsr.ts | 4 ++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/web/src/components/PassageDetail/transcribe/useTranscribeAsr.test.ts b/web/src/components/PassageDetail/transcribe/useTranscribeAsr.test.ts index 53054ad90..a8bc59412 100644 --- a/web/src/components/PassageDetail/transcribe/useTranscribeAsr.test.ts +++ b/web/src/components/PassageDetail/transcribe/useTranscribeAsr.test.ts @@ -220,4 +220,35 @@ describe('useTranscribeAsr', () => { expect(onTextAdd).toHaveBeenNthCalledWith(1, ' hello world', true); expect(onTextAdd).toHaveBeenNthCalledWith(2, ' 12345678901:1.2: hi', true); }); + + // Pre-inserted range marker (Mark Verses → Transcribe) must not be half-stripped + // into "1-5 -5" when ASR returns the same range plus later verses. + it('keeps a verse-range marker intact when that range is already in the pane', async () => { + getAsrSettings.mockResolvedValue({ + target: 'alphabet', + language: { bcp47: 'en', languageName: 'English' }, + asrIso: 'eng', + dialect: undefined, + }); + const onTextAdd = jest.fn(); + const { result } = renderHook(() => + useTranscribeAsr({ + team: undefined, + sharedStr: { mustBeOnline: 'online' } as never, + tPlayer: { recognizeSpeech: 'Recognize {0}' } as never, + showMessage: jest.fn(), + onTextAdd, + getCurrentText: () => '\\v 1-5 ', + }) + ); + await waitFor(() => expect(result.current.asrSettings?.asrIso).toBe('eng')); + + act(() => { + result.current.handleAutoTranscribe('\\v 1-5 one two \\v 6-7 three'); + }); + + expect(onTextAdd).toHaveBeenCalledTimes(1); + expect(onTextAdd).toHaveBeenCalledWith('one two \\v 6-7 three', true); + expect(onTextAdd.mock.calls[0][0]).not.toMatch(/-5/); + }); }); diff --git a/web/src/components/PassageDetail/transcribe/useTranscribeAsr.ts b/web/src/components/PassageDetail/transcribe/useTranscribeAsr.ts index 66091cb14..c2757d1d9 100644 --- a/web/src/components/PassageDetail/transcribe/useTranscribeAsr.ts +++ b/web/src/components/PassageDetail/transcribe/useTranscribeAsr.ts @@ -170,7 +170,9 @@ export function useTranscribeAsr({ .trim(); const curTrans = getCurrentText(); if (curTrans.includes(cleanTrans)) return; - const m = /\\v (\d+)\s?/.exec(cleanTrans); + // Match ranges/suffixes (1-5, 1a) so a pre-inserted \v 1-5 is not + // treated as \v 1 and left as a stray "-5" in the pane. + const m = /\\v ([0-9]+[a-f]?-?[0-9]*[a-f]?)\s?/.exec(cleanTrans); const index = m && curTrans.includes(m[0]) ? m[0].length : 0; const space = /\s$/.test(curTrans) ? '' : ' '; onTextAdd(space + cleanTrans.substring(index), true); From 9c4ac2e30f3403abdc5452f59379581add682c03 Mon Sep 17 00:00:00 2001 From: Sara Hentzel Date: Fri, 13 Nov 2026 14:54:28 -0600 Subject: [PATCH 2/2] move replacing text to testable helper --- .../appendAutoTranscription.test.ts | 44 +++++++++++++++++++ .../transcribe/appendAutoTranscription.ts | 21 +++++++++ .../transcribe/useTranscribeAsr.test.ts | 31 +++++-------- .../transcribe/useTranscribeAsr.ts | 31 ++++--------- 4 files changed, 84 insertions(+), 43 deletions(-) create mode 100644 web/src/components/PassageDetail/transcribe/appendAutoTranscription.test.ts create mode 100644 web/src/components/PassageDetail/transcribe/appendAutoTranscription.ts diff --git a/web/src/components/PassageDetail/transcribe/appendAutoTranscription.test.ts b/web/src/components/PassageDetail/transcribe/appendAutoTranscription.test.ts new file mode 100644 index 000000000..49d385e99 --- /dev/null +++ b/web/src/components/PassageDetail/transcribe/appendAutoTranscription.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from '@jest/globals'; +import { appendAutoTranscription } from './appendAutoTranscription'; + +describe('appendAutoTranscription', () => { + it('returns undefined when the cleaned text is already in the pane', () => { + expect(appendAutoTranscription('hello', 'hello world')).toBeUndefined(); + }); + + it('strips timestamp prefixes without clipping a longer digit run', () => { + expect(appendAutoTranscription('0:01.200: hello 1:02.300: world', '')).toBe( + ' hello world' + ); + expect(appendAutoTranscription('12345678901:1.2: hi', '')).toBe( + ' 12345678901:1.2: hi' + ); + }); + + it('skips a leading single-verse marker already in the pane', () => { + expect(appendAutoTranscription('\\v 3 hello', '\\v 3 ')).toBe('hello'); + }); + + // Pre-inserted range marker (Mark Verses → Transcribe) must not be half-stripped + // into "1-5 -5" when ASR returns the same range plus later verses. + it('keeps a verse-range marker intact when that range is already in the pane', () => { + const added = appendAutoTranscription( + '\\v 1-5 one two \\v 6-7 three', + '\\v 1-5 ' + ); + expect(added).toBe('one two \\v 6-7 three'); + expect(added).not.toMatch(/-5/); + }); + + it('keeps a letter-suffix marker intact when already in the pane', () => { + expect(appendAutoTranscription('\\v 1a hello', '\\v 1a ')).toBe('hello'); + }); + + it('adds a leading space when the pane text does not end with one', () => { + expect(appendAutoTranscription('more', 'hi')).toBe(' more'); + }); + + it('adds no leading space when the pane text already ends with one', () => { + expect(appendAutoTranscription('more', 'hi ')).toBe('more'); + }); +}); diff --git a/web/src/components/PassageDetail/transcribe/appendAutoTranscription.ts b/web/src/components/PassageDetail/transcribe/appendAutoTranscription.ts new file mode 100644 index 000000000..c7cda6f3e --- /dev/null +++ b/web/src/components/PassageDetail/transcribe/appendAutoTranscription.ts @@ -0,0 +1,21 @@ +/** + * Text to append from an ASR result, or undefined when curTrans already has it. + * Strips timestamp prefixes and skips a leading verse marker already in the pane. + */ +export function appendAutoTranscription( + trans: string, + curTrans: string +): string | undefined { + // {1,10} keeps this unanchored replace linear. (? { expect(result.current.asrProgressVisible).toBe(false); }); - it('strips timestamp prefixes without clipping a longer digit run', async () => { + it('forwards cleaned ASR text through onTextAdd', async () => { getAsrSettings.mockResolvedValue({ target: 'alphabet', language: { bcp47: 'en', languageName: 'English' }, @@ -211,44 +211,35 @@ describe('useTranscribeAsr', () => { await waitFor(() => expect(result.current.asrSettings?.asrIso).toBe('eng')); act(() => { - result.current.handleAutoTranscribe('0:01.200: hello 1:02.300: world'); - }); - act(() => { - result.current.handleAutoTranscribe('12345678901:1.2: hi'); + result.current.handleAutoTranscribe('0:01.200: hello'); }); - expect(onTextAdd).toHaveBeenNthCalledWith(1, ' hello world', true); - expect(onTextAdd).toHaveBeenNthCalledWith(2, ' 12345678901:1.2: hi', true); + expect(onTextAdd).toHaveBeenCalledWith(' hello', true); }); - // Pre-inserted range marker (Mark Verses → Transcribe) must not be half-stripped - // into "1-5 -5" when ASR returns the same range plus later verses. - it('keeps a verse-range marker intact when that range is already in the pane', async () => { + it('derives phonetic from the active ASR target', async () => { getAsrSettings.mockResolvedValue({ - target: 'alphabet', + target: 'Phonetic', language: { bcp47: 'en', languageName: 'English' }, asrIso: 'eng', dialect: undefined, }); - const onTextAdd = jest.fn(); const { result } = renderHook(() => useTranscribeAsr({ team: undefined, sharedStr: { mustBeOnline: 'online' } as never, tPlayer: { recognizeSpeech: 'Recognize {0}' } as never, showMessage: jest.fn(), - onTextAdd, - getCurrentText: () => '\\v 1-5 ', + onTextAdd: jest.fn(), + getCurrentText: () => '', }) ); await waitFor(() => expect(result.current.asrSettings?.asrIso).toBe('eng')); + expect(result.current.phonetic).toBe(true); - act(() => { - result.current.handleAutoTranscribe('\\v 1-5 one two \\v 6-7 three'); - }); + act(() => result.current.handleTranscribe()); - expect(onTextAdd).toHaveBeenCalledTimes(1); - expect(onTextAdd).toHaveBeenCalledWith('one two \\v 6-7 three', true); - expect(onTextAdd.mock.calls[0][0]).not.toMatch(/-5/); + expect(result.current.asrOverride?.target).toBe('Phonetic'); + expect(result.current.phonetic).toBe(true); }); }); diff --git a/web/src/components/PassageDetail/transcribe/useTranscribeAsr.ts b/web/src/components/PassageDetail/transcribe/useTranscribeAsr.ts index c2757d1d9..21583fbf1 100644 --- a/web/src/components/PassageDetail/transcribe/useTranscribeAsr.ts +++ b/web/src/components/PassageDetail/transcribe/useTranscribeAsr.ts @@ -17,6 +17,7 @@ import { useGlobal } from '../../../context/useGlobal'; import { transcriberSelector } from '../../../selector'; import { AsrTarget } from '../../../business/asr/AsrTarget'; import { IAsrState, asrStatesEqual } from '../../../business/asr/asrState'; +import { appendAutoTranscription } from './appendAutoTranscription'; export interface UseTranscribeAsrProps { team: OrganizationD | undefined; @@ -94,10 +95,10 @@ export function useTranscribeAsr({ const [asrProgressVisible, setAsrProgressVisible] = useState(false); const [asrLangVisible, setAsrLangVisible] = useState(false); - const [phonetic, setPhonetic] = useState(false); const [asrOverride, setAsrOverride] = useState( undefined ); + const phonetic = (asrOverride ?? asrSettings)?.target === AsrTarget.phonetic; const asrTip = useMemo(() => { return (tPlayer.recognizeSpeech + '\u00A0\u00A0').replace( @@ -111,15 +112,10 @@ export function useTranscribeAsr({ ); }, [tPlayer.recognizeSpeech, asrSettings, getName]); - const startAsr = useCallback( - (asrOverrideState?: IAsrState) => { - const asr = asrOverrideState ?? asrSettings; - setAsrOverride(asrOverrideState); - setPhonetic(asr?.target === AsrTarget.phonetic); - setAsrProgressVisible(true); - }, - [asrSettings] - ); + const startAsr = useCallback((asrOverrideState?: IAsrState) => { + setAsrOverride(asrOverrideState); + setAsrProgressVisible(true); + }, []); const openAsrLanguageSettings = useCallback(() => { setAsrLangVisible(true); @@ -163,19 +159,8 @@ export function useTranscribeAsr({ const handleAutoTranscribe = useCallback( (trans: string) => { - // {1,10} keeps this unanchored replace linear. (?