Files
lucia-frontend/tests/stores/conformance.test.js

213 lines
6.3 KiB
JavaScript

// The Lucia project.
// Copyright 2026-2026 DSP, inc. All rights reserved.
// Authors:
// imacat.yang@dsp.im (imacat), 2026/03/05
import { describe, it, expect, beforeEach, vi } from "vitest";
import { setActivePinia, createPinia } from "pinia";
vi.mock("@/module/apiError.js", () => ({
default: vi.fn(),
}));
const { mockGet, mockPost, mockPut } = vi.hoisted(() => ({
mockGet: vi.fn(),
mockPost: vi.fn(),
mockPut: vi.fn(),
}));
vi.mock("@/api/client.js", () => ({
default: { get: mockGet, post: mockPost, put: mockPut },
}));
import { useConformanceStore } from "@/stores/conformance";
describe("conformanceStore", () => {
let store;
beforeEach(() => {
setActivePinia(createPinia());
store = useConformanceStore();
vi.clearAllMocks();
});
it("has correct default state", () => {
expect(store.conformanceLogId).toBeNull();
expect(store.conformanceFilterId).toBeNull();
expect(store.allConformanceTask).toEqual([]);
expect(store.selectedRuleType).toBe("Have activity");
});
describe("getConformanceParams", () => {
it("fetches log check params when no filter", async () => {
store.conformanceLogId = 1;
store.conformanceFilterId = null;
const mockData = {
tasks: [{ label: "A" }],
sources: ["A"],
sinks: ["B"],
processing_time: {},
waiting_time: {},
cycle_time: {},
};
mockGet.mockResolvedValue({ data: mockData });
await store.getConformanceParams();
expect(mockGet).toHaveBeenCalledWith("/api/log-checks/params?log_id=1");
expect(store.allConformanceTask).toEqual([{ label: "A" }]);
expect(store.allCfmSeqStart).toEqual(["A"]);
});
it("fetches filter check params when filter set", async () => {
store.conformanceFilterId = 5;
const mockData = {
tasks: [],
sources: [],
sinks: [],
processing_time: {},
waiting_time: {},
cycle_time: {},
};
mockGet.mockResolvedValue({ data: mockData });
await store.getConformanceParams();
expect(mockGet).toHaveBeenCalledWith(
"/api/filter-checks/params?filter_id=5",
);
});
});
describe("addConformanceCheckId", () => {
it("posts to log temp-check and stores id", async () => {
store.conformanceLogId = 1;
store.conformanceFilterId = null;
mockPost.mockResolvedValue({ data: { id: 42 } });
await store.addConformanceCheckId({ rule: "test" });
expect(mockPost).toHaveBeenCalledWith("/api/temp-log-checks?log_id=1", {
rule: "test",
});
expect(store.conformanceLogTempCheckId).toBe(42);
});
it("posts to filter temp-check when filter set", async () => {
store.conformanceFilterId = 3;
mockPost.mockResolvedValue({ data: { id: 99 } });
await store.addConformanceCheckId({ rule: "test" });
expect(mockPost).toHaveBeenCalledWith(
"/api/temp-filter-checks?filter_id=3",
{ rule: "test" },
);
expect(store.conformanceFilterTempCheckId).toBe(99);
});
});
describe("getConformanceReport", () => {
it("fetches temp log check report", async () => {
store.conformanceLogTempCheckId = 10;
const mockData = { file: {}, charts: {} };
mockGet.mockResolvedValue({ data: mockData });
await store.getConformanceReport();
expect(mockGet).toHaveBeenCalledWith("/api/temp-log-checks/10");
expect(store.allConformanceTempReportData).toEqual(mockData);
});
it("stores routeFile when getRouteFile=true", async () => {
store.conformanceLogTempCheckId = 10;
const mockData = { file: { name: "test.csv" } };
mockGet.mockResolvedValue({ data: mockData });
await store.getConformanceReport(true);
expect(store.allRouteFile).toEqual({ name: "test.csv" });
expect(store.allConformanceTempReportData).toBeNull();
});
});
describe("addConformanceCreateCheckId", () => {
it("creates log check and clears temp id", async () => {
store.conformanceLogId = 1;
store.conformanceFilterId = null;
store.conformanceLogTempCheckId = 10;
store.conformanceRuleData = { type: "test" };
mockPost.mockResolvedValue({ data: { id: 100 } });
await store.addConformanceCreateCheckId("myRule");
expect(mockPost).toHaveBeenCalledWith("/api/log-checks?log_id=1", {
name: "myRule",
rule: { type: "test" },
});
expect(store.conformanceLogCreateCheckId).toBe(100);
expect(store.conformanceLogTempCheckId).toBeNull();
});
});
describe("updateConformance", () => {
it("updates existing log check", async () => {
store.conformanceLogCreateCheckId = 50;
store.conformanceRuleData = { type: "updated" };
mockPut.mockResolvedValue({ status: 200 });
await store.updateConformance();
expect(mockPut).toHaveBeenCalledWith("/api/log-checks/50", {
type: "updated",
});
expect(store.isUpdateConformance).toBe(true);
});
});
it("setConformanceLogCreateCheckId sets value", () => {
store.setConformanceLogCreateCheckId("abc");
expect(store.conformanceLogCreateCheckId).toBe("abc");
});
describe("getters", () => {
it("conformanceTask returns labels", () => {
store.allConformanceTask = [{ label: "A" }, { label: "B" }];
expect(store.conformanceTask).toEqual(["A", "B"]);
});
it("cases getter formats date attributes with correct minute token", () => {
store.allCases = [
{
started_at: "2023-06-15T10:30:00Z",
completed_at: "2023-06-15T11:45:00Z",
facets: [],
attributes: [
{
type: "date",
value: "2023-06-15T14:30:00Z",
},
],
},
];
const result = store.cases;
// The date attribute should show minutes (30), not month (06)
expect(result[0].attributes[0].value).toMatch(/:30:/);
});
it("cases getter does not corrupt original state on repeated access", () => {
const originalDate = "2023-06-15T10:30:00Z";
store.allCases = [
{
started_at: originalDate,
completed_at: "2023-06-15T11:45:00Z",
facets: [],
attributes: [],
},
];
const _accessed = store.cases;
// Original state should still contain the ISO date
expect(store.allCases[0].started_at).toBe(originalDate);
});
});
});