@untemps/react-vocal 2.0.0-beta.2 → 2.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@untemps/react-vocal",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.3",
4
4
  "author": "Vincent Le Badezet <v.lebadezet@untemps.net>",
5
5
  "repository": "git@github.com:untemps/react-vocal.git",
6
6
  "license": "MIT",
@@ -82,11 +82,22 @@ const Vocal = ({
82
82
  )
83
83
 
84
84
  const _onResult = useCallback(
85
- (event, result) => {
85
+ (event) => {
86
+ const transcript = Array.from(event?.results ?? [], (segment) => {
87
+ let best = { confidence: -Infinity, transcript: '' }
88
+ for (let j = 0; j < segment.length; j++) {
89
+ const alt = segment[j]
90
+ if (alt.confidence === undefined || alt.confidence > best.confidence) {
91
+ best = alt
92
+ }
93
+ }
94
+ return best.transcript ?? ''
95
+ }).join('')
96
+
86
97
  stopTimer()
87
98
  stopRecognition()
88
- triggerCommandRef.current(result)
89
- propsRef.current.onResult?.(result, event)
99
+ triggerCommandRef.current(transcript)
100
+ propsRef.current.onResult?.(transcript, event)
90
101
  },
91
102
  [stopTimer, stopRecognition]
92
103
  )
@@ -427,4 +427,50 @@ describe('Vocal', () => {
427
427
 
428
428
  expect(onErrorV1).not.toHaveBeenCalled()
429
429
  })
430
+
431
+ it('returns the most confident alternative when multiple alternatives are provided', async () => {
432
+ const onResult = vi.fn()
433
+ const recognition = new SpeechRecognitionWrapper()
434
+ const { getByTestId } = render(getInstance({ __rsInstance: recognition, onResult }))
435
+
436
+ await act(async () => {
437
+ fireEvent.click(getByTestId('__vocal-root__'))
438
+ recognition.instance.say([[
439
+ { transcript: 'bar', confidence: 0.4 },
440
+ { transcript: 'foo', confidence: 0.9 },
441
+ { transcript: 'baz', confidence: 0.1 },
442
+ ]])
443
+ await waitFor(() => expect(onResult).toHaveBeenCalledWith('foo', expect.anything()))
444
+ })
445
+ })
446
+
447
+ it('joins all segments when multiple result segments are provided', async () => {
448
+ const onResult = vi.fn()
449
+ const recognition = new SpeechRecognitionWrapper()
450
+ const { getByTestId } = render(getInstance({ __rsInstance: recognition, onResult }))
451
+
452
+ await act(async () => {
453
+ fireEvent.click(getByTestId('__vocal-root__'))
454
+ recognition.instance.say([
455
+ [{ transcript: 'hello ', confidence: 0.9 }],
456
+ [{ transcript: 'world', confidence: 0.8 }],
457
+ ])
458
+ await waitFor(() => expect(onResult).toHaveBeenCalledWith('hello world', expect.anything()))
459
+ })
460
+ })
461
+
462
+ it('picks highest-confidence alternative per segment when multi-segment with multi-alternative', async () => {
463
+ const onResult = vi.fn()
464
+ const recognition = new SpeechRecognitionWrapper()
465
+ const { getByTestId } = render(getInstance({ __rsInstance: recognition, onResult }))
466
+
467
+ await act(async () => {
468
+ fireEvent.click(getByTestId('__vocal-root__'))
469
+ recognition.instance.say([
470
+ [{ transcript: 'good ', confidence: 0.8 }, { transcript: 'bad ', confidence: 0.2 }],
471
+ [{ transcript: 'day', confidence: 0.95 }, { transcript: 'dey', confidence: 0.3 }],
472
+ ])
473
+ await waitFor(() => expect(onResult).toHaveBeenCalledWith('good day', expect.anything()))
474
+ })
475
+ })
430
476
  })
package/vitest.setup.js CHANGED
@@ -54,14 +54,14 @@ global.SpeechRecognition = vi.fn(function () {
54
54
  abort: vi.fn(function () {
55
55
  handlers.end?.()
56
56
  }),
57
- say: vi.fn(function (sentence) {
57
+ say: vi.fn(function (input) {
58
58
  handlers.speechstart?.()
59
59
 
60
60
  const resultEvent = new Event('result')
61
61
  resultEvent.resultIndex = 0
62
- resultEvent.results = [[{ transcript: sentence }]]
62
+ resultEvent.results = Array.isArray(input) ? input : input ? [[{ transcript: input }]] : []
63
63
  handlers.speechend?.()
64
- if (sentence) {
64
+ if (input) {
65
65
  handlers.result?.(resultEvent)
66
66
  } else {
67
67
  handlers.nomatch?.()