Files
superlist/apps/web/src/lib/item-quantity.test.ts
Jose Selesan a814b34036 feat(items): voice dictation with Gemma 4 and a reworked list UI
Dictation: a mic button in the add field uses the Web Speech API (es-AR) and
shows the partial transcript in the input. POST /api/voice/split asks Gemma 4
(Gemini API, plain fetch) to split the sentence into one string per item,
written the way the existing parser reads typed text; each string then goes
through the usual add flow. The endpoint never fails because of the model:
missing key, timeout, 429 or invalid JSON fall back to splitting on commas,
semicolons, " y " and " e ". A toast offers "Deshacer" for the whole dictation;
if the request fails, the transcript is added as a single item.

UI: "+" instead of the search icon, quantity tags ("2 L", "×12", "1 kg"), the
list name with a list switcher and the pending/bought count in the header,
"Comprados (N)" folded by default, and a bottom sheet to edit name, quantity
(− / + and unit), note and delete. PATCH items now accepts a name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 19:25:10 -03:00

48 lines
1.9 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { fromEditableQuantity, stepAmount, toEditableQuantity } from './item-quantity'
describe('toEditableQuantity', () => {
it('splits a stored quantity into amount and unit', () => {
expect(toEditableQuantity(null)).toEqual({ amount: '1', unit: '' })
expect(toEditableQuantity('2')).toEqual({ amount: '2', unit: '' })
expect(toEditableQuantity('1,5 l')).toEqual({ amount: '1,5', unit: 'l' })
expect(toEditableQuantity('½ kg')).toEqual({ amount: '½', unit: 'kg' })
expect(toEditableQuantity('2 paquetes')).toEqual({ amount: '2', unit: 'paquete' })
})
it('keeps free text as the amount', () => {
expect(toEditableQuantity('un par')).toEqual({ amount: 'un par', unit: '' })
})
})
describe('stepAmount', () => {
it('steps by one for counts and by bigger steps for measures', () => {
expect(stepAmount('2', '', 1)).toBe('3')
expect(stepAmount('2', '', -1)).toBe('1')
expect(stepAmount('1', '', -1)).toBe('1')
expect(stepAmount('400', 'g', 1)).toBe('500')
expect(stepAmount('½', 'kg', 1)).toBe('1')
expect(stepAmount('1', 'l', -1)).toBe('½')
})
it('starts from one step when the amount is not a number', () => {
expect(stepAmount('un par', '', 1)).toBe('1')
})
})
describe('fromEditableQuantity', () => {
it('stores the formatted quantity, and nothing for a plain one', () => {
expect(fromEditableQuantity({ amount: '1', unit: '' })).toBeNull()
expect(fromEditableQuantity({ amount: '', unit: 'kg' })).toBeNull()
expect(fromEditableQuantity({ amount: '2', unit: '' })).toBe('2')
expect(fromEditableQuantity({ amount: '1', unit: 'kg' })).toBe('1 kg')
expect(fromEditableQuantity({ amount: '0.5', unit: 'kg' })).toBe('½ kg')
expect(fromEditableQuantity({ amount: '2', unit: 'paquete' })).toBe('2 paquetes')
})
it('keeps free text as typed', () => {
expect(fromEditableQuantity({ amount: 'un par', unit: '' })).toBe('un par')
})
})