Files
pleno-vue/tests/unit/use-wash-session-actions.spec.js
T

163 lines
4.7 KiB
JavaScript

import { ref } from "vue";
import { describe, expect, it, vi } from "vitest";
import { useWashSessionActions } from "@/composables/useWashSessionActions.js";
const createActions = (overrides = {}) => {
const request = vi.fn(async () => ({ data: { success: true } }));
const alertFn = vi.fn();
const options = {
request,
alertFn,
nearestDepartment: ref({
id: 1,
lanes: [{ id: 7 }],
}),
vehicleTypeSelect: ref(2),
washLaneId: ref(7),
washInProgress: ref(true),
washStartTime: ref(Date.now() - 30_000),
completedDurationMs: ref(null),
now: ref(Date.now()),
currentStep: ref(4),
steps: {
VEHICLE: 0,
QUESTIONS: 1,
SELECT_LANE: 2,
TASKS: 3,
WASH_IN_PROGRESS: 4,
COMPLETED: 5,
},
radioWashType: ref("Manual"),
activeTasks: ref([]),
completedTasks: ref({}),
saveProgress: vi.fn(),
clearProgress: vi.fn(),
startElapsedTimer: vi.fn(),
stopElapsedTimer: vi.fn(),
updateLaneAllowedServices: vi.fn(),
fetchWashSummary: vi.fn(),
enableMachineRelay: vi.fn(),
isServiceAllowed: vi.fn(() => true),
...overrides,
};
return {
request: options.request,
alertFn,
options,
actions: useWashSessionActions(options),
};
};
describe("useWashSessionActions property gate commands", () => {
it("opens property access gate with explicit lane id and tracks loading state", async () => {
const deferred = {};
deferred.promise = new Promise((resolve) => {
deferred.resolve = resolve;
});
const { actions, request } = createActions({
request: vi.fn(() => deferred.promise),
});
const commandPromise = actions.openPropertyAccessGate(9);
expect(actions.openingPropertyAccessGate.value).toBe(true);
deferred.resolve({ data: { success: true } });
await commandPromise;
expect(actions.openingPropertyAccessGate.value).toBe(false);
expect(request).toHaveBeenCalledWith("/modules/self-serve/lane/command", "post", {
lane_id: 9,
command: "OPEN_PROPERTY_ACCESS_GATE",
customer_number: null,
license_plate: null,
});
});
it("opens property exit gate using active wash lane when lane argument is omitted", async () => {
const { actions, request } = createActions();
await actions.openPropertyExitGate();
expect(request).toHaveBeenCalledWith("/modules/self-serve/lane/command", "post", {
lane_id: 7,
command: "OPEN_PROPERTY_EXIT_GATE",
customer_number: null,
license_plate: null,
});
});
it("warns and skips command when no lane id can be resolved", async () => {
const { actions, request, alertFn } = createActions({
washLaneId: ref(null),
});
const result = await actions.openPropertyAccessGate();
expect(result).toBeNull();
expect(request).not.toHaveBeenCalled();
expect(alertFn).toHaveBeenCalledWith("Ingen vaskebane valgt.");
});
it("keeps flow non-blocking when property gate command fails", async () => {
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const { actions, alertFn } = createActions({
request: vi.fn(async () => {
throw new Error("command failed");
}),
});
const result = await actions.openPropertyAccessGate();
expect(result).toBeNull();
expect(actions.openingPropertyAccessGate.value).toBe(false);
expect(alertFn).toHaveBeenCalledWith("command failed");
consoleErrorSpy.mockRestore();
});
it("does not enter wash-in-progress state when the start command fails", async () => {
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const washInProgress = ref(false);
const washLaneId = ref(null);
const washStartTime = ref(null);
const currentStep = ref(2);
const request = vi.fn(async (_url, _method, body) => {
if (body.command === "START") {
return {
data: {
success: false,
data: {
message: "Edge gateway command timed out",
},
},
};
}
return { data: { success: true } };
});
const { actions, alertFn } = createActions({
request,
washInProgress,
washLaneId,
washStartTime,
currentStep,
});
const result = await actions.onStartWash(7, "ab12345", 12345679, 4);
expect(result).toBe(false);
expect(washInProgress.value).toBe(false);
expect(washLaneId.value).toBeNull();
expect(washStartTime.value).toBeNull();
expect(currentStep.value).toBe(2);
expect(actions.isStartingWash.value).toBe(false);
expect(alertFn).toHaveBeenCalledWith("Edge gateway command timed out");
consoleErrorSpy.mockRestore();
});
});