import { describe, it, expect, vi, beforeEach } from "vitest";
import { setActivePinia, createPinia } from "pinia";
import { useScanningStore } from "../scanning.js";
vi.mock("../../api/modules/devices.js", () => ({
devicesApi: {
scanningSetup: vi.fn(),
scanningAll: vi.fn(),
setupNewDevice: vi.fn(),
},
}));
import { devicesApi } from "../../api/modules/devices.js";
describe("useScanningStore", () => {
beforeEach(() => {
setActivePinia(createPinia());
vi.clearAllMocks();
});
describe("scan", () => {
it("fetches setup devices in setup mode", async () => {
devicesApi.scanningSetup.mockResolvedValue({
ok: true,
data: { data: { devices: [{ device_name: "New", status: "setup" }] } },
});
const store = useScanningStore();
await store.scan();
expect(devicesApi.scanningSetup).toHaveBeenCalled();
expect(store.devices.length).toBe(1);
expect(store.devices[0].device_name).toBe("New");
expect(store.isLoading).toBe(false);
});
it("fetches all devices in all mode", async () => {
devicesApi.scanningAll.mockResolvedValue({
ok: true,
data: { data: { devices: [{ device_name: "All", status: "normal" }] } },
});
const store = useScanningStore();
store.setMode("all");
await store.scan();
expect(devicesApi.scanningAll).toHaveBeenCalled();
expect(store.devices[0].status).toBe("normal");
});
it("sets error on failure", async () => {
devicesApi.scanningSetup.mockResolvedValue({
ok: false,
error: { message: "Scan failed" },
});
const store = useScanningStore();
await store.scan();
expect(store.error).toEqual({ message: "Scan failed" });
});
});
describe("setMode", () => {
it("changes mode and clears previous results", () => {
const store = useScanningStore();
store.devices = [{ device_name: "Old" }];
store.error = { message: "Old error" };
store.setMode("all");
expect(store.mode).toBe("all");
expect(store.devices).toEqual([]);
expect(store.error).toBeNull();
});
});
describe("setupDevice", () => {
it("calls API with payload", async () => {
devicesApi.setupNewDevice.mockResolvedValue({ ok: true });
const store = useScanningStore();
const result = await store.setupDevice({
device_ip: "192.168.1.50",
alias: "new_relay",
name: "New Relay",
});
expect(devicesApi.setupNewDevice).toHaveBeenCalledWith({
device_ip: "192.168.1.50",
alias: "new_relay",
name: "New Relay",
});
expect(result.ok).toBe(true);
});
});
});