Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions apps/web/src/components/LabBench.pipette-burner.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
stubLabFetch,
lastActionInit,
expectCsrfLabAction,
applyPipetteUse,
cloneScene,
} from './labBenchTestHelpers'

describe('LabBench pipette / burner', () => {
Expand Down Expand Up @@ -370,4 +372,44 @@ describe('LabBench pipette / burner', () => {
})
})

it('shows spit cue on dish last-action target, not water, and hides spit from banner', async () => {
const fetchMock = stubLabFetch({
actionHandler: (action, scene) => {
if (action.type === 'use_tool' && action.tool_item_id === 'pipette-1') {
const result = applyPipetteUse(scene, action.target_item_id)
if ('error' in result) return result
if (
action.target_item_id === 'dish-1' &&
result.last_events?.some((event) => event.kind === 'poured')
) {
const next = cloneScene(result)
next.last_events = [
{ kind: 'spit', message: '' },
...(next.last_events ?? []),
]
return next
}
return result
}
return scene
},
})
vi.stubGlobal('fetch', fetchMock)

render(<LabBench />)
await screen.findByRole('button', { name: 'Pipette' })
fireEvent.click(screen.getByRole('button', { name: 'Pipette' }))
fireEvent.click(screen.getByRole('button', { name: 'Beaker' }))
await waitFor(() => {
expect(document.querySelector('[data-pipette-filled="true"]')).not.toBeNull()
})
fireEvent.click(screen.getByRole('button', { name: 'Evaporation dish' }))
await waitFor(() => {
expect(document.querySelector('[data-dish-fill]')).toHaveAttribute('data-spit-cue', 'burst')
})
expect(document.querySelector('[data-water-fill]')).toHaveAttribute('data-spit-cue', 'none')
expect(document.querySelector('[data-event-kind="spit"]')).toBeNull()
expect(screen.getByRole('status')).toHaveTextContent('Emptied the pipette into the vessel.')
})

})
48 changes: 39 additions & 9 deletions apps/web/src/components/LabBench.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ import {
burnerIsOn,
dishAmountMl,
dissolveCueFromEvents,
spitCueFromEvents,
bannerEventsFromLastEvents,
distilledWaterAmountMl,
hclStockAmountMl,
h2so4StockAmountMl,
Expand Down Expand Up @@ -140,6 +142,8 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
const [pointer, setPointer] = useState<{ x: number; y: number } | null>(null)
const [stockCarouselIndex, setStockCarouselIndex] = useState(0)
const [toolCarouselIndex, setToolCarouselIndex] = useState(0)
/** Last action vessel id — spit CSS burst target (dissolve-cue pattern). */
const [lastActionTargetId, setLastActionTargetId] = useState<string | null>(null)

const burnerOn = scene ? burnerIsOn(scene) : false
const needsClockPoll = scene ? sceneNeedsClockPoll(scene) : false
Expand Down Expand Up @@ -275,6 +279,7 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
tool_item_id: toolItemId,
target_item_id: targetItemId,
})
setLastActionTargetId(targetItemId)
setScene(response.scene)
} catch (err) {
setError(err instanceof Error ? err.message : 'Action failed')
Expand All @@ -293,6 +298,7 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
source_item_id: sourceItemId,
target_item_id: targetItemId,
})
setLastActionTargetId(targetItemId)
setScene(response.scene)
} catch (err) {
setError(err instanceof Error ? err.message : 'Action failed')
Expand Down Expand Up @@ -467,6 +473,9 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
const paperHeld = paper?.location === 'held'
const lastEvents = scene ? optionalArray(scene.last_events) : []
const dissolveCue = dissolveCueFromEvents(lastEvents)
const hasSpitCue = spitCueFromEvents(lastEvents)
const spitTargetId = hasSpitCue ? lastActionTargetId : null
const bannerEvents = bannerEventsFromLastEvents(lastEvents)
const hasAqueous = scene ? waterHasAqueous(scene) : false
const inspectItem = scene && inspectItemId ? findItem(scene, inspectItemId) : undefined
const pipetteFilled = scene ? pipetteIsFilled(scene) : false
Expand Down Expand Up @@ -551,11 +560,15 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
aria-label="Filtrate beaker"
disabled={busy}
onClick={onFiltrate}
data-spit-cue={spitTargetId === FILTRATE_ID ? 'burst' : 'none'}
>
{filtrateHeld ? (
<svg viewBox="0 0 120 168" className="h-32 w-20" aria-hidden />
) : (
<FiltrateBeakerSvg amountMl={scene ? filtrateAmountMl(scene) : null} />
<FiltrateBeakerSvg
amountMl={scene ? filtrateAmountMl(scene) : null}
spitCue={spitTargetId === FILTRATE_ID}
/>
)}
<span className="lab-item-label">{filtrate?.label ?? 'Filtrate'}</span>
</button>
Expand All @@ -570,11 +583,15 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
aria-label="Evaporation dish"
disabled={busy}
onClick={onDish}
data-spit-cue={spitTargetId === DISH_ID ? 'burst' : 'none'}
>
{dishHeld ? (
<svg viewBox="0 0 120 56" className="h-14 w-28" aria-hidden />
) : (
<EvaporationDishSvg amountMl={dishAmountMl(scene)} />
<EvaporationDishSvg
amountMl={dishAmountMl(scene)}
spitCue={spitTargetId === DISH_ID}
/>
)}
<span className="lab-item-label">{dish.label}</span>
</button>
Expand All @@ -601,6 +618,7 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
aria-label={water?.label ?? 'Beaker'}
disabled={busy}
onClick={onWater}
data-spit-cue={spitTargetId === WATER_ID ? 'burst' : 'none'}
>
{waterHeld ? (
<svg viewBox="0 0 120 168" className="h-40 w-28" aria-hidden />
Expand All @@ -611,6 +629,7 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
amountMl={waterAmountMl(scene)}
hasAqueous={hasAqueous}
dissolveCue={dissolveCue}
spitCue={spitTargetId === WATER_ID}
/>
)}
<span className="lab-item-label">{water?.label ?? 'Beaker'}</span>
Expand Down Expand Up @@ -785,6 +804,7 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
amountMl={waterAmountMl(scene)}
hasAqueous={hasAqueous}
dissolveCue={dissolveCue}
spitCue={spitTargetId === WATER_ID}
floating
/>
) : heldVesselId === H2O_ID && scene ? (
Expand All @@ -800,9 +820,17 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
floating
/>
) : heldVesselId === DISH_ID && scene ? (
<EvaporationDishSvg amountMl={dishAmountMl(scene)} floating />
<EvaporationDishSvg
amountMl={dishAmountMl(scene)}
spitCue={spitTargetId === DISH_ID}
floating
/>
) : heldVesselId === FILTRATE_ID && scene ? (
<FiltrateBeakerSvg amountMl={filtrateAmountMl(scene)} floating />
<FiltrateBeakerSvg
amountMl={filtrateAmountMl(scene)}
spitCue={spitTargetId === FILTRATE_ID}
floating
/>
) : heldVesselId === PAPER_ID && scene ? (
<FunnelPaperSvg residue={paperHasResidue(scene)} floating />
) : (
Expand All @@ -828,7 +856,7 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
</p>
) : null}

{lastEvents.length > 0 ? (
{bannerEvents.length > 0 ? (
<div className="lab-bench-events mt-3 space-y-2 text-sm" role="status" aria-live="polite">
{dissolveCue ? (
<p
Expand All @@ -841,19 +869,21 @@ export function LabBench({ onModeChange }: { onModeChange?: (mode: string) => vo
{': '}
<span className="text-[var(--ink-soft)]">
{dissolveCue === 'dissolved'
? lastEvents.find((event) => event.kind === 'dissolved')?.message
: lastEvents.find((event) => event.kind === 'did_not_dissolve')?.message}
? bannerEvents.find((event) => event.kind === 'dissolved')?.message
: bannerEvents.find((event) => event.kind === 'did_not_dissolve')?.message}
</span>
</p>
) : (
lastEvents.map((event, index) => {
bannerEvents.map((event, index) => {
const outcome = outcomeLabel(event.kind)
return (
<div key={`${event.kind}-${index}`} className="space-y-1" data-event-kind={event.kind}>
{outcome ? (
<p className="font-medium text-[var(--ink)]">{outcome}</p>
) : null}
<p className="text-[var(--ink-soft)]">{event.message}</p>
{event.message ? (
<p className="text-[var(--ink-soft)]">{event.message}</p>
) : null}
</div>
)
})
Expand Down
80 changes: 78 additions & 2 deletions apps/web/src/components/LabBenchIcons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,55 @@ import {

export type StockSolid = 'nacl' | 'cacl2' | 'sand' | 'naoh' | 'na2so4'

/** Shared spray burst for chemical spit (water / dish / filtrate). */
function SpitSprayOverlay({
originY,
}: {
/** Vertical origin of the droplet cluster in the SVG viewBox. */
originY: number
}) {
return (
<g className="lab-spit-spray" pointerEvents="none">
<circle className="lab-spit-drop lab-spit-drop--1" cx="48" cy={originY} r="2.2" fill="#A4FFEC" />
<circle
className="lab-spit-drop lab-spit-drop--2"
cx="62"
cy={originY - 12}
r="1.8"
fill="#7CF8F7"
/>
<circle
className="lab-spit-drop lab-spit-drop--3"
cx="76"
cy={originY - 4}
r="2.4"
fill="#DDF7FF"
/>
<circle
className="lab-spit-drop lab-spit-drop--4"
cx="40"
cy={originY - 18}
r="1.5"
fill="#85E1FB"
/>
<circle
className="lab-spit-drop lab-spit-drop--5"
cx="88"
cy={originY - 22}
r="1.9"
fill="#A4FFEC"
/>
<circle
className="lab-spit-drop lab-spit-drop--6"
cx="55"
cy={originY - 28}
r="1.4"
fill="#7CF8F7"
/>
</g>
)
}

/** Fill fraction 0..1 from server amount_ml relative to initial water volume. */
export function waterFillRatio(amountMl: number | null | undefined): number {
if (amountMl == null || amountMl <= 0) return 0
Expand All @@ -22,6 +71,7 @@ export function WaterBeakerSvg({
amountMl,
hasAqueous,
dissolveCue,
spitCue,
floating,
}: {
leftoverSolid: StockSolid | null
Expand All @@ -31,6 +81,8 @@ export function WaterBeakerSvg({
hasAqueous?: boolean
/** Latest dissolve-related kind from scene `last_events`. */
dissolveCue?: 'dissolved' | 'did_not_dissolve' | null
/** Silent chemical spit burst from server `last_events`. */
spitCue?: boolean
floating?: boolean
}) {
const fill = waterFillRatio(amountMl)
Expand All @@ -55,6 +107,7 @@ export function WaterBeakerSvg({
data-water-fill={fill.toFixed(2)}
data-water-aqueous={hasAqueous ? 'true' : 'false'}
data-dissolve-cue={dissolveCue ?? 'none'}
data-spit-cue={spitCue ? 'burst' : 'none'}
>
<defs>
<linearGradient id="bench-glass" x1="20" y1="8" x2="100" y2="160" gradientUnits="userSpaceOnUse">
Expand Down Expand Up @@ -105,6 +158,7 @@ export function WaterBeakerSvg({
<circle cx="56" cy="136" r="2.2" />
</g>
) : null}
{spitCue ? <SpitSprayOverlay originY={70} /> : null}
<path d="M86 48c14-4 22 10 16 22" stroke="#DDF7FF" strokeWidth="1.6" strokeLinecap="round" opacity="0.45" />
</svg>
)
Expand Down Expand Up @@ -403,7 +457,16 @@ export function TongsSvg({ floating }: { floating?: boolean }) {
)
}

export function EvaporationDishSvg({ amountMl, floating }: { amountMl?: number | null; floating?: boolean }) {
export function EvaporationDishSvg({
amountMl,
floating,
spitCue,
}: {
amountMl?: number | null
floating?: boolean
/** Silent chemical spit burst from server `last_events`. */
spitCue?: boolean
}) {
const fill = dishFillRatio(amountMl)
const floorY = 42
const height = 16 * fill
Expand All @@ -414,6 +477,7 @@ export function EvaporationDishSvg({ amountMl, floating }: { amountMl?: number |
className={floating ? 'h-10 w-20' : 'h-14 w-28'}
aria-hidden
data-dish-fill={fill.toFixed(2)}
data-spit-cue={spitCue ? 'burst' : 'none'}
>
<path
d="M18 16h84l8 28c1 6-4 10-10 10H20c-6 0-11-4-10-10l8-28z"
Expand All @@ -429,6 +493,7 @@ export function EvaporationDishSvg({ amountMl, floating }: { amountMl?: number |
fillOpacity="0.55"
/>
) : null}
{spitCue ? <SpitSprayOverlay originY={22} /> : null}
</svg>
)
}
Expand Down Expand Up @@ -456,7 +521,16 @@ export function filtrateFillRatio(amountMl: number | null | undefined): number {
return Math.min(1, amountMl / FILTRATE_CAPACITY_ML)
}

export function FiltrateBeakerSvg({ amountMl, floating }: { amountMl?: number | null; floating?: boolean }) {
export function FiltrateBeakerSvg({
amountMl,
floating,
spitCue,
}: {
amountMl?: number | null
floating?: boolean
/** Silent chemical spit burst from server `last_events`. */
spitCue?: boolean
}) {
const fill = filtrateFillRatio(amountMl)
const floorY = 148
const fullHeight = 62
Expand All @@ -469,6 +543,7 @@ export function FiltrateBeakerSvg({ amountMl, floating }: { amountMl?: number |
className={floating ? 'h-24 w-16' : 'h-32 w-20'}
aria-hidden
data-filtrate-fill={fill.toFixed(2)}
data-spit-cue={spitCue ? 'burst' : 'none'}
>
<path d="M28 18h64v10H28z" fill="#86A7DF" opacity="0.85" />
<path
Expand All @@ -485,6 +560,7 @@ export function FiltrateBeakerSvg({ amountMl, floating }: { amountMl?: number |
fillOpacity="0.55"
/>
) : null}
{spitCue ? <SpitSprayOverlay originY={70} /> : null}
</svg>
)
}
Expand Down
16 changes: 16 additions & 0 deletions apps/web/src/components/labBenchScene.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
sceneNeedsDissolvePoll,
sceneNeedsThermalPoll,
solutionVolumeMl,
spitCueFromEvents,
bannerEventsFromLastEvents,
tongsHeldVesselId,
waterAmountMl,
} from './labBenchScene'
Expand Down Expand Up @@ -325,3 +327,17 @@ describe('clock poll gates', () => {
expect(sceneNeedsClockPoll(sandOnly)).toBe(false)
})
})

describe('chemical spit cues', () => {
it('detects spit and suppresses it from the banner list', () => {
const events = [
{ kind: 'poured', message: 'Emptied the pipette into the vessel.' },
{ kind: 'spit', message: '' },
]
expect(spitCueFromEvents(events)).toBe(true)
expect(bannerEventsFromLastEvents(events)).toEqual([
{ kind: 'poured', message: 'Emptied the pipette into the vessel.' },
])
expect(spitCueFromEvents([{ kind: 'poured', message: 'ok' }])).toBe(false)
})
})
Loading
Loading