the-biscuit-machine/test/vitest/__tests__/biscuitMachine.test.ts

125 lines
3.7 KiB
TypeScript

import { BiscuitSwitchState } from 'src/components/BiscuitSwitch.vue';
import {
useBiscuitMachine,
OPTIMAL_COOKIES_TARGET_TEMPERATURE,
BELT_SIZE,
STAMPER_POSITION,
EXTRUDER_POSITION,
} from 'src/composables/biscuitMachine';
import { expect, test } from 'vitest';
import { nextTick, ref } from 'vue';
import { toggleTickAsync } from '../utils';
test('start in the expected state', async () => {
const mainSwitchPosition = ref<BiscuitSwitchState>('off');
const inTargetZoneIn = ref(false);
const motorPulseIn = ref(false);
const {
motorOn,
stamperPulseOut,
extruderPulseOut,
targetTemperature,
machineState,
} = useBiscuitMachine(mainSwitchPosition, inTargetZoneIn, motorPulseIn);
expect(machineState.value).toBe('off');
expect(stamperPulseOut.value).toBe(false);
expect(extruderPulseOut.value).toBe(false);
expect(targetTemperature.value).toBe('none');
expect(motorOn.value).toBe(false);
});
test('switches motor on only after target temperature reached', async () => {
const mainSwitchPosition = ref<BiscuitSwitchState>('on');
const inTargetZoneIn = ref(false);
const motorPulseIn = ref(false);
const { motorOn, targetTemperature, machineState } = useBiscuitMachine(
mainSwitchPosition,
inTargetZoneIn,
motorPulseIn
);
expect(machineState.value).toBe('heating');
expect(motorOn.value).toBe(false);
expect(targetTemperature.value).toBe(OPTIMAL_COOKIES_TARGET_TEMPERATURE);
inTargetZoneIn.value = true;
await nextTick();
expect(machineState.value).toBe('on');
expect(motorOn.value).toBe(true);
expect(targetTemperature.value).toBe(OPTIMAL_COOKIES_TARGET_TEMPERATURE);
});
test('pausing the machine stops the motor & keeps the temperature', async () => {
const mainSwitchPosition = ref<BiscuitSwitchState>('on');
const inTargetZoneIn = ref(true);
const motorPulseIn = ref(false);
const { motorOn, targetTemperature, machineState } = useBiscuitMachine(
mainSwitchPosition,
inTargetZoneIn,
motorPulseIn
);
expect(machineState.value).toBe('on');
expect(motorOn.value).toBe(true);
expect(targetTemperature.value).toBe(OPTIMAL_COOKIES_TARGET_TEMPERATURE);
mainSwitchPosition.value = 'paused';
await nextTick();
expect(machineState.value).toBe('paused');
expect(motorOn.value).toBe(false);
expect(targetTemperature.value).toBe(OPTIMAL_COOKIES_TARGET_TEMPERATURE);
});
test('turning off the machine waits for produce to get carted off', async () => {
const mainSwitchPosition = ref<BiscuitSwitchState>('on');
const inTargetZoneIn = ref(true);
const motorPulseIn = ref(false);
const {
motorOn,
targetTemperature,
machineState,
extruderPulseOut,
stamperPulseOut,
} = useBiscuitMachine(mainSwitchPosition, inTargetZoneIn, motorPulseIn);
expect(machineState.value).toBe('on');
expect(motorOn.value).toBe(true);
expect(targetTemperature.value).toBe(OPTIMAL_COOKIES_TARGET_TEMPERATURE);
mainSwitchPosition.value = 'off';
await nextTick();
expect(machineState.value).toBe('turning off');
expect(motorOn.value).toBe(true);
expect(targetTemperature.value).toBe(OPTIMAL_COOKIES_TARGET_TEMPERATURE);
for (let i = 0; i < BELT_SIZE - 1; i++) {
motorPulseIn.value = true;
await nextTick();
expect(machineState.value).toBe('turning off');
expect(motorOn.value).toBe(true);
expect(targetTemperature.value).toBe(OPTIMAL_COOKIES_TARGET_TEMPERATURE);
expect(extruderPulseOut.value).toBe(false);
expect(stamperPulseOut.value).toBe(
i < STAMPER_POSITION - EXTRUDER_POSITION
// i <= 3
);
motorPulseIn.value = false;
await nextTick();
}
await toggleTickAsync(motorPulseIn);
expect(machineState.value).toBe('off');
expect(motorOn.value).toBe(false);
expect(targetTemperature.value).toBe('none');
});