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,13 +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'); + result.current.handleAutoTranscribe('0:01.200: hello'); }); - act(() => { - result.current.handleAutoTranscribe('12345678901:1.2: hi'); + + expect(onTextAdd).toHaveBeenCalledWith(' hello', true); + }); + + it('derives phonetic from the active ASR target', async () => { + getAsrSettings.mockResolvedValue({ + target: 'Phonetic', + language: { bcp47: 'en', languageName: 'English' }, + asrIso: 'eng', + dialect: undefined, }); + const { result } = renderHook(() => + useTranscribeAsr({ + team: undefined, + sharedStr: { mustBeOnline: 'online' } as never, + tPlayer: { recognizeSpeech: 'Recognize {0}' } as never, + showMessage: jest.fn(), + onTextAdd: jest.fn(), + getCurrentText: () => '', + }) + ); + await waitFor(() => expect(result.current.asrSettings?.asrIso).toBe('eng')); + expect(result.current.phonetic).toBe(true); + + act(() => result.current.handleTranscribe()); - expect(onTextAdd).toHaveBeenNthCalledWith(1, ' hello world', true); - expect(onTextAdd).toHaveBeenNthCalledWith(2, ' 12345678901:1.2: hi', true); + 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 66091cb14..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,17 +159,8 @@ export function useTranscribeAsr({ const handleAutoTranscribe = useCallback( (trans: string) => { - // {1,10} keeps this unanchored replace linear. (?