Skip to content
Open
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
4 changes: 4 additions & 0 deletions apps/web/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import App from './App'
import { clearCsrfTokenCache } from './lib/api'
import { STOCK_FULL_MASS_G, STOCK_FULL_SCOOPS, HCL_STOCK_CAPACITY_ML, HCL_STOCK_WATER_ML, HCL_STOCK_HCL_MOLES } from './lib/benchAmounts'
import { CHALLENGES } from './lib/challenges'

const userPayload = {
id: 'u1',
Expand Down Expand Up @@ -309,6 +310,9 @@ describe('App', () => {
expect(free).toBeChecked()
expect(screen.getByRole('radio', { name: 'Separate salt from sand' })).not.toBeChecked()
expect(screen.getByRole('radio', { name: 'Create table salt' })).not.toBeChecked()
for (const challenge of CHALLENGES) {
expect(screen.getByRole('radio', { name: challenge.title })).toBeInTheDocument()
}
expect(screen.queryByText(/lab bench below/i)).not.toBeInTheDocument()
expect(screen.queryByText(/pick a solid/i)).not.toBeInTheDocument()
})
Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/components/BeakerInspectPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
CompositionInspectLine,
formatTemperatureC,
formatPh,
formatHclWw,
phFromComposition,
hclInventoryMassFraction,
solventVolumeLitres,
} from '../lib/compositionDisplay'
import { optionalArray } from '../lib/scene'
Expand All @@ -22,6 +24,7 @@ export function BeakerInspectPanel({
const temperatureC = itemTemperatureC(scene, item)
const solventL = solventVolumeLitres(composition)
const ph = phFromComposition(composition)
const hclWw = hclInventoryMassFraction(composition)

return (
<aside
Expand All @@ -37,6 +40,9 @@ export function BeakerInspectPanel({
{ph != null ? (
<p className="text-[var(--ink-soft)]">pH: {formatPh(ph)}</p>
) : null}
{hclWw != null ? (
<p className="text-[var(--ink-soft)]">HCl: {formatHclWw(hclWw)}</p>
) : null}
</div>
<button
type="button"
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/components/LabBench.inspect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ describe('LabBench inspect', () => {
expect(panel).toHaveTextContent('8.04 ml')
expect(panel).toHaveTextContent('H+ (aq)')
expect(panel).toHaveTextContent('pH: -0.98')
expect(panel).toHaveTextContent('HCl: 30.0% w/w')
})

it('idle click inspects paper versus filtrate', async () => {
Expand Down
7 changes: 3 additions & 4 deletions apps/web/src/components/labBenchTestFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import type { LabAction, LabScene } from '../generated/contracts'
import { optionalArray } from '../lib/scene'
import { FREE_MODE } from '../lib/challenges'
import {
CREATE_TABLE_SALT_LAYOUT,
SEPARATE_NACL_SIO2_LAYOUT,
challengeStartScene,
cloneScene,
initialScene,
layoutForMode,
withFilledMainBeaker,
} from './labBenchTestFixtures'
import {
Expand All @@ -34,8 +33,8 @@ import {
/** Mirror of the server's mode-aware reset: back to the start scene of the current mode. */
function startSceneForMode(mode: string): LabScene {
if (mode === FREE_MODE) return initialScene()
if (mode === CREATE_TABLE_SALT_LAYOUT.mode) return challengeStartScene(CREATE_TABLE_SALT_LAYOUT)
if (mode === SEPARATE_NACL_SIO2_LAYOUT.mode) return challengeStartScene(SEPARATE_NACL_SIO2_LAYOUT)
const layout = layoutForMode(mode)
if (layout) return challengeStartScene(layout)
// Unknown mode: fall back to Free (safer than inventing a challenge layout).
return initialScene()
}
Expand Down
81 changes: 81 additions & 0 deletions apps/web/src/components/labBenchTestFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,87 @@ export const CREATE_TABLE_SALT_LAYOUT: ChallengeStartLayout = {
distilledWaterMl: null,
}

export const CREATE_SODIUM_SULFATE_LAYOUT: ChallengeStartLayout = {
mode: 'create-sodium-sulfate',
allowedStockItemIds: ['beaker-h2o', 'beaker-h2so4', 'beaker-naoh', 'beaker-na2so4'],
emptyStockItemIds: ['beaker-na2so4'],
mainBeakerSolids: [],
distilledWaterMl: null,
}

export const PRECIPITATE_GYPSUM_LAYOUT: ChallengeStartLayout = {
mode: 'precipitate-gypsum',
allowedStockItemIds: ['beaker-h2o', 'beaker-cacl2', 'beaker-na2so4', 'beaker-h2so4'],
emptyStockItemIds: [],
mainBeakerSolids: [],
distilledWaterMl: null,
}

export const MAKE_HCL_FROM_GYPSUM_LAYOUT: ChallengeStartLayout = {
mode: 'make-hcl-from-gypsum',
allowedStockItemIds: ['beaker-h2o', 'beaker-cacl2', 'beaker-h2so4'],
emptyStockItemIds: [],
mainBeakerSolids: [],
distilledWaterMl: null,
}

export const HOT_PACK_CACL2_LAYOUT: ChallengeStartLayout = {
mode: 'hot-pack-cacl2',
allowedStockItemIds: ['beaker-h2o', 'beaker-cacl2'],
emptyStockItemIds: [],
mainBeakerSolids: [],
distilledWaterMl: 10,
}

export const COMMON_ION_NACL_LAYOUT: ChallengeStartLayout = {
mode: 'common-ion-nacl',
allowedStockItemIds: ['beaker-h2o', 'beaker-hcl', 'beaker-nacl'],
emptyStockItemIds: [],
mainBeakerSolids: [],
distilledWaterMl: 5.6,
}

export const NEUTRALIZE_TO_PH7_LAYOUT: ChallengeStartLayout = {
mode: 'neutralize-to-ph7',
allowedStockItemIds: ['beaker-h2o', 'beaker-hcl', 'beaker-naoh'],
emptyStockItemIds: [],
mainBeakerSolids: [],
distilledWaterMl: null,
}

export const DILUTE_SULFURIC_SAFE_LAYOUT: ChallengeStartLayout = {
mode: 'dilute-sulfuric-safe',
allowedStockItemIds: ['beaker-h2o', 'beaker-h2so4'],
emptyStockItemIds: [],
mainBeakerSolids: [],
distilledWaterMl: null,
}

export const CONCENTRATE_HCL_AZEOTROPE_LAYOUT: ChallengeStartLayout = {
mode: 'concentrate-hcl-azeotrope',
allowedStockItemIds: ['beaker-h2o', 'beaker-hcl'],
emptyStockItemIds: [],
mainBeakerSolids: [],
distilledWaterMl: null,
}

const CHALLENGE_START_LAYOUTS: ChallengeStartLayout[] = [
SEPARATE_NACL_SIO2_LAYOUT,
CREATE_TABLE_SALT_LAYOUT,
CREATE_SODIUM_SULFATE_LAYOUT,
PRECIPITATE_GYPSUM_LAYOUT,
MAKE_HCL_FROM_GYPSUM_LAYOUT,
HOT_PACK_CACL2_LAYOUT,
COMMON_ION_NACL_LAYOUT,
NEUTRALIZE_TO_PH7_LAYOUT,
DILUTE_SULFURIC_SAFE_LAYOUT,
CONCENTRATE_HCL_AZEOTROPE_LAYOUT,
]

export function layoutForMode(mode: string): ChallengeStartLayout | null {
return CHALLENGE_START_LAYOUTS.find((layout) => layout.mode === mode) ?? null
}

/** @deprecated Prefer `challengeStartScene(SEPARATE_NACL_SIO2_LAYOUT)` or a snapshot. */
export function challengeScene(): LabScene {
return challengeStartScene(SEPARATE_NACL_SIO2_LAYOUT)
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/generated/ItemProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ on?: boolean | null,
* Last vessel a pipette drew from, the vessel tongs currently hold, or the
* dish a spoon scoop came from.
*/
source_item_id?: string | null, };
source_item_id?: string | null,
/**
* Dilution order for concentrated H₂SO₄: `true` acid-into-water, `false`
* water-onto-acid. Omitted when unset. Server-authored; used by challenge wins.
*/
h2so4_dilution_into_water?: boolean | null, };
5 changes: 4 additions & 1 deletion apps/web/src/lib/benchAmounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ export const HCL_STOCK_WATER_ML = 8.043
/** Solution density at 30% w/w HCl (g/ml). */
export const HCL_STOCK_DENSITY_G_PER_ML = 1.149

/** Molar mass of HCl (g/mol) — mirrors `chemlab_core::hcl::HCL_MOLAR_MASS_G_PER_MOL`. */
export const HCL_MOLAR_MASS_G_PER_MOL = 36.46

/** Moles of HCl (= H⁺ = Cl⁻) in the filled Free-mode stock. */
export const HCL_STOCK_HCL_MOLES = 3.447 / 36.46
export const HCL_STOCK_HCL_MOLES = 3.447 / HCL_MOLAR_MASS_G_PER_MOL

/** H₂SO₄ stock beaker capacity (ml of liquid acid). */
export const H2SO4_STOCK_CAPACITY_ML = 10
Expand Down
54 changes: 54 additions & 0 deletions apps/web/src/lib/challenges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,60 @@ export const CHALLENGES: Challenge[] = [
prompt: "We're out of NaCl again, can you create some for us?",
done: 'Thank you again.',
},
{
id: 'create-sodium-sulfate',
title: 'Create sodium sulfate',
prompt: "We're out of sodium sulfate, can you make some from sulfuric acid and sodium hydroxide?",
done: 'Thank you.',
},
{
id: 'precipitate-gypsum',
title: 'Precipitate gypsum',
prompt: 'We need gypsum. Mix calcium chloride with a sulfate and filter off the solid.',
done: 'Thank you.',
},
{
id: 'make-hcl-from-gypsum',
title: 'Make hydrochloric acid',
prompt:
'Make hydrochloric acid by mixing calcium chloride with sulfuric acid, then filter off the gypsum.',
done: 'Thank you.',
},
{
id: 'hot-pack-cacl2',
title: 'Hot pack',
prompt:
'Dissolve the calcium chloride in a little distilled water and check that the beaker warms up.',
done: 'Thank you.',
},
{
id: 'common-ion-nacl',
title: 'Crash salt with acid',
prompt:
'Make a clear near-saturated salt solution, add hydrochloric acid, and heat until extra salt appears.',
done: 'Thank you.',
},
{
id: 'neutralize-to-ph7',
title: 'Neutralise to pH 7',
prompt:
'Neutralise sodium hydroxide with hydrochloric acid and check the inspect pH — a 1 ml pipette cannot land on 7.',
done: 'Thank you.',
},
{
id: 'dilute-sulfuric-safe',
title: 'Dilute sulfuric acid safely',
prompt:
'Dilute the concentrated sulfuric acid the safe way: add the acid into water, not water onto the acid.',
done: 'Thank you.',
},
{
id: 'concentrate-hcl-azeotrope',
title: 'Concentrate hydrochloric acid',
prompt:
'Dilute the hydrochloric acid, heat it in the dish, and stop near the azeotrope — you cannot boil it to pure HCl.',
done: 'Thank you.',
},
]

/** Picker options: Free mode first (the default), then the catalog. */
Expand Down
21 changes: 21 additions & 0 deletions apps/web/src/lib/compositionDisplay.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
formatFormulaNodes,
formatFormulaPlain,
formatPh,
formatHclWw,
formatTemperatureC,
phFromComposition,
hclInventoryMassFraction,
solventVolumeLitres,
StockSubstanceLabel,
stockSubstanceAriaLabel,
Expand Down Expand Up @@ -150,6 +152,25 @@ describe('phFromComposition', () => {
expect(formatPh(ph!)).toBe('-0.98')
})

it('returns HCl mass fraction for the stock composition', () => {
const composition = [
entry({ substance_id: 'water', phase: 'liquid', amount_ml: 8.043 }),
entry({ substance_id: 'h+', phase: 'aqueous', amount_mol: 3.447 / 36.46 }),
entry({ substance_id: 'cl-', phase: 'aqueous', amount_mol: 3.447 / 36.46 }),
]
const w = hclInventoryMassFraction(composition)
expect(w).not.toBeNull()
expect(w!).toBeCloseTo(0.30, 5)
expect(formatHclWw(w!)).toBe('30.0% w/w')
expect(
hclInventoryMassFraction([
entry({ substance_id: 'water', phase: 'liquid', amount_ml: 100 }),
entry({ substance_id: 'na+', phase: 'aqueous', amount_mol: 0.01 }),
entry({ substance_id: 'cl-', phase: 'aqueous', amount_mol: 0.01 }),
]),
).toBeNull()
})

it('returns pH 7 for pure water (school Kw)', () => {
expect(phFromComposition([entry({ substance_id: 'water', phase: 'liquid', amount_ml: 100 })])).toBe(7)
})
Expand Down
9 changes: 7 additions & 2 deletions apps/web/src/lib/compositionDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import type { ReactNode } from 'react'
import type { CompositionEntry } from '../generated/contracts'
import { phFromComposition, solutionVolumeMl } from './solutionChemistry'
import { phFromComposition, solutionVolumeMl, hclInventoryMassFraction } from './solutionChemistry'

export { phFromComposition }
export { phFromComposition, hclInventoryMassFraction }

const PHASE_ABBREV: Record<string, string> = {
solid: 's',
Expand Down Expand Up @@ -158,6 +158,11 @@ function formatFixedAmount(value: number): string {
return value.toFixed(2)
}

/** Format HCl mass fraction for inspect (one decimal percent). */
export function formatHclWw(w: number): string {
return `${(w * 100).toFixed(1)}% w/w`
}

/** Format beaker/scene temperature for inspect labels (always two decimals). */
export function formatTemperatureC(temperatureC: number): string {
return temperatureC.toFixed(2)
Expand Down
17 changes: 17 additions & 0 deletions apps/web/src/lib/solutionChemistry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CompositionEntry } from '../generated/contracts'
import {
HCL_MOLAR_MASS_G_PER_MOL,
HCL_STOCK_CAPACITY_ML,
HCL_STOCK_HCL_MOLES,
HCL_STOCK_WATER_ML,
Expand Down Expand Up @@ -100,3 +101,19 @@ export function phFromComposition(composition: CompositionEntry[]): number | nul
if ((water?.amount_ml ?? 0) > 0) return 7
return null
}

/** HCl mass fraction mirroring `HclInventory.w_hcl()` (null when no HCl inventory). */
export function hclInventoryMassFraction(composition: CompositionEntry[]): number | null {
const waterEntry = composition.find(
(c) => c.substance_id === 'water' && c.phase === 'liquid',
)
const waterMl = Math.max(0, waterEntry?.amount_ml ?? 0)
const nH = aqueousMol(composition, 'h+')
const nOh = aqueousMol(composition, 'oh-')
const nCl = aqueousMol(composition, 'cl-')
const nHcl = Math.min(Math.max(nH - nOh, 0), nCl)
const hclMassG = nHcl * HCL_MOLAR_MASS_G_PER_MOL
const totalMassG = waterMl + hclMassG
if (nHcl <= 1e-12 || totalMassG <= 1e-6) return null
return hclMassG / totalMassG
}
5 changes: 5 additions & 0 deletions crates/chemlab-contracts/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl From<chemlab_core::ItemProperties> for ItemProperties {
holding: props.holding.into_iter().map(Into::into).collect(),
on: props.on,
source_item_id: props.source_item_id,
h2so4_dilution_into_water: props.h2so4_dilution_into_water,
}
}
}
Expand All @@ -61,6 +62,10 @@ impl From<ItemProperties> for chemlab_core::ItemProperties {
holding: props.holding.into_iter().map(Into::into).collect(),
on: props.on,
source_item_id: props.source_item_id,
h2so4_dilution_into_water: props.h2so4_dilution_into_water,
hcl_seen_lean: None,
nacl_seen_clear_brine: None,
nacl_solid_g_at_acid: None,
}
}
}
Expand Down
Loading
Loading