All files / web/src/components/admin/tts-lab/tests NovelClipTest.tsx

0% Statements 0/148
0% Branches 0/1
0% Functions 0/1
0% Lines 0/148

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                                                                                                                                                                                                                                                                                                         
'use client'

import { useCallback, useState } from 'react'
import { useAudioManagerInstance } from '@/contexts/AudioManagerContext'
import { computeClipHash } from '@/lib/audio/clipHash'
import { TestCard, type TestStatus } from '../TestCard'
import { useTestLog } from '../useTestLog'
import { css } from '../../../../../styled-system/css'

const TONE_PRESETS = [
  'tutorial-instruction',
  'problem-prompt',
  'feedback-correct',
  'feedback-incorrect',
  'encouragement',
]

function LabButton({
  onClick,
  disabled,
  children,
}: {
  onClick: () => void
  disabled?: boolean
  children: React.ReactNode
}) {
  return (
    <button
      onClick={onClick}
      disabled={disabled}
      className={css({
        fontSize: '12px',
        padding: '6px 12px',
        borderRadius: '6px',
        border: '1px solid #30363d',
        backgroundColor: '#21262d',
        color: '#c9d1d9',
        cursor: 'pointer',
        _hover: { backgroundColor: '#30363d' },
        _disabled: { opacity: 0.5, cursor: 'not-allowed' },
      })}
    >
      {children}
    </button>
  )
}

export function NovelClipTest() {
  const manager = useAudioManagerInstance()
  const { entries, log, clear } = useTestLog()
  const [status, setStatus] = useState<TestStatus>('idle')
  const [text, setText] = useState('The soroban has five beads on each rod')
  const [tone, setTone] = useState('tutorial-instruction')
  const [isPlaying, setIsPlaying] = useState(false)

  const handleSpeak = useCallback(async () => {
    if (!text.trim()) {
      log('warn', 'No text to speak')
      return
    }

    setStatus('running')
    setIsPlaying(true)

    // Compute and log clip hash
    const clipId = computeClipHash({ en: text }, tone)
    log('info', `Computed clip ID: ${clipId}`)

    // Check availability
    const avail = manager.getClipAvailability(clipId)
    if (avail.length === 0) {
      log('warn', 'No voice chain configured')
    } else {
      for (const a of avail) {
        const sourceName = a.source.type === 'pregenerated' ? a.source.name : 'browser-tts'
        log('info', `  ${sourceName}: hasClip=${a.hasClip}`)
      }
    }

    const pregenSources = avail.filter((a) => a.source.type === 'pregenerated' && a.hasClip)
    if (pregenSources.length === 0) {
      log('info', 'No pregenerated source has this clip — expecting browser TTS fallback')
    }

    const start = performance.now()
    try {
      await manager.speak({ say: { en: text }, tone })
      const elapsed = (performance.now() - start).toFixed(0)
      log('success', `Speak completed in ${elapsed}ms`)
      setStatus('pass')
    } catch (err) {
      log('error', 'Speak failed', String(err))
      setStatus('fail')
    } finally {
      setIsPlaying(false)
    }
  }, [manager, text, tone, log])

  return (
    <TestCard
      title="Test 5: Novel Clips / Browser TTS"
      description="Hash-based clips with no mp3 fall through voice chain to browser TTS"
      status={status}
      entries={entries}
      onClear={clear}
    >
      <div className={css({ display: 'flex', flexDirection: 'column', gap: '8px', width: '100%' })}>
        <input
          type="text"
          value={text}
          onChange={(e) => setText(e.target.value)}
          className={css({
            fontSize: '12px',
            padding: '6px 8px',
            borderRadius: '6px',
            border: '1px solid #30363d',
            backgroundColor: '#0d1117',
            color: '#c9d1d9',
            width: '100%',
          })}
        />
        <div className={css({ display: 'flex', alignItems: 'center', gap: '8px' })}>
          <select
            value={tone}
            onChange={(e) => setTone(e.target.value)}
            className={css({
              fontSize: '12px',
              padding: '4px 8px',
              borderRadius: '6px',
              border: '1px solid #30363d',
              backgroundColor: '#21262d',
              color: '#c9d1d9',
            })}
          >
            {TONE_PRESETS.map((t) => (
              <option key={t} value={t}>
                {t}
              </option>
            ))}
          </select>
          <LabButton onClick={handleSpeak} disabled={isPlaying}>
            Speak
          </LabButton>
        </div>
      </div>
    </TestCard>
  )
}