225 lines
6.7 KiB
JavaScript
225 lines
6.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();
|
|
});
|
|
|
|
it("does not duplicate action alerts when allowed-service setup fails before start", 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 updateLaneAllowedServices = vi.fn(async () => {
|
|
throw new Error("Edge gateway command timed out");
|
|
});
|
|
|
|
const { actions, request, alertFn } = createActions({
|
|
updateLaneAllowedServices,
|
|
washInProgress,
|
|
washLaneId,
|
|
washStartTime,
|
|
currentStep,
|
|
});
|
|
|
|
const result = await actions.onStartWash(7, "ab12345", 12345679, 4);
|
|
|
|
expect(result).toBe(false);
|
|
expect(updateLaneAllowedServices).toHaveBeenCalledWith(7);
|
|
expect(request).not.toHaveBeenCalled();
|
|
expect(alertFn).not.toHaveBeenCalled();
|
|
expect(washInProgress.value).toBe(false);
|
|
expect(currentStep.value).toBe(2);
|
|
|
|
consoleErrorSpy.mockRestore();
|
|
});
|
|
|
|
it("keeps the wash started when the post-start machine relay retry 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 enableMachineRelay = vi.fn(async () => {
|
|
throw new Error("Edge gateway command timed out");
|
|
});
|
|
|
|
const { actions, alertFn } = createActions({
|
|
washInProgress,
|
|
washLaneId,
|
|
washStartTime,
|
|
currentStep,
|
|
radioWashType: ref("Machine"),
|
|
enableMachineRelay,
|
|
isServiceAllowed: vi.fn(() => true),
|
|
});
|
|
|
|
const result = await actions.onStartWash(7, "ab12345", 12345679, 4);
|
|
|
|
expect(result).toBe(true);
|
|
expect(washInProgress.value).toBe(true);
|
|
expect(washLaneId.value).toBe(7);
|
|
expect(currentStep.value).toBe(4);
|
|
expect(enableMachineRelay).toHaveBeenCalledWith(7);
|
|
expect(alertFn).not.toHaveBeenCalled();
|
|
|
|
consoleErrorSpy.mockRestore();
|
|
});
|
|
});
|