mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-26 05:51:17 -07:00
Fix: use v2 storage API for UniFi Drive widget (#6567)
This commit is contained in:
@@ -25,9 +25,9 @@ export default function Component({ service }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: storage } = storageData;
|
const { pools } = storageData;
|
||||||
|
|
||||||
if (!storage) {
|
if (!Array.isArray(pools) || pools.length === 0) {
|
||||||
return (
|
return (
|
||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block value={t("unifi_drive.no_data")} />
|
<Block value={t("unifi_drive.no_data")} />
|
||||||
@@ -35,12 +35,13 @@ export default function Component({ service }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { totalQuota, usage, status } = storage;
|
const totalBytes = pools.reduce((sum, p) => sum + (p.capacity ?? 0), 0);
|
||||||
const totalBytes = totalQuota ?? 0;
|
const usedBytes = pools.reduce((sum, p) => sum + (p.usage ?? 0), 0);
|
||||||
const usedBytes = (usage?.system || 0) + (usage?.myDrives || 0) + (usage?.sharedDrives || 0);
|
|
||||||
const availableBytes = Math.max(0, totalBytes - usedBytes);
|
const availableBytes = Math.max(0, totalBytes - usedBytes);
|
||||||
|
|
||||||
|
const status = pools.some((p) => p.status === "degraded") ? "degraded" : pools[0]?.status;
|
||||||
let statusValue = status;
|
let statusValue = status;
|
||||||
if (status === "healthy") statusValue = t("unifi_drive.healthy");
|
if (status === "fullyOperational" || status === "noDataProtectionYet") statusValue = t("unifi_drive.healthy");
|
||||||
else if (status === "degraded") statusValue = t("unifi_drive.degraded");
|
else if (status === "degraded") statusValue = t("unifi_drive.degraded");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ describe("widgets/unifi_drive/component", () => {
|
|||||||
expect(screen.getAllByText("widget.api_error", { exact: false }).length).toBeGreaterThan(0);
|
expect(screen.getAllByText("widget.api_error", { exact: false }).length).toBeGreaterThan(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("renders no_data when storage data is missing", () => {
|
it("renders no_data when pools array is empty", () => {
|
||||||
useWidgetAPI.mockReturnValue({ data: { data: null }, error: undefined });
|
useWidgetAPI.mockReturnValue({ data: { pools: [] }, error: undefined });
|
||||||
|
|
||||||
const service = { widget: { type: "unifi_drive" } };
|
const service = { widget: { type: "unifi_drive" } };
|
||||||
renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||||
@@ -48,14 +48,19 @@ describe("widgets/unifi_drive/component", () => {
|
|||||||
expect(screen.getByText("unifi_drive.no_data")).toBeInTheDocument();
|
expect(screen.getByText("unifi_drive.no_data")).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("renders storage statistics when data is loaded", () => {
|
it("renders no_data when pools is missing", () => {
|
||||||
|
useWidgetAPI.mockReturnValue({ data: {}, error: undefined });
|
||||||
|
|
||||||
|
const service = { widget: { type: "unifi_drive" } };
|
||||||
|
renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||||
|
|
||||||
|
expect(screen.getByText("unifi_drive.no_data")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders storage statistics from single pool", () => {
|
||||||
useWidgetAPI.mockReturnValue({
|
useWidgetAPI.mockReturnValue({
|
||||||
data: {
|
data: {
|
||||||
data: {
|
pools: [{ capacity: 1000000000000, usage: 350000000000, status: "fullyOperational" }],
|
||||||
totalQuota: 1000000000000,
|
|
||||||
usage: { system: 100000000000, myDrives: 200000000000, sharedDrives: 50000000000 },
|
|
||||||
status: "healthy",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
error: undefined,
|
error: undefined,
|
||||||
});
|
});
|
||||||
@@ -70,14 +75,34 @@ describe("widgets/unifi_drive/component", () => {
|
|||||||
expectBlockValue(container, "widget.status", "unifi_drive.healthy");
|
expectBlockValue(container, "widget.status", "unifi_drive.healthy");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("renders degraded status", () => {
|
it("aggregates storage across multiple pools", () => {
|
||||||
useWidgetAPI.mockReturnValue({
|
useWidgetAPI.mockReturnValue({
|
||||||
data: {
|
data: {
|
||||||
data: {
|
pools: [
|
||||||
totalQuota: 100,
|
{ capacity: 1000000000000, usage: 300000000000, status: "fullyOperational" },
|
||||||
usage: { system: 10, myDrives: 20, sharedDrives: 5 },
|
{ capacity: 500000000000, usage: 100000000000, status: "noDataProtectionYet" },
|
||||||
status: "degraded",
|
],
|
||||||
},
|
},
|
||||||
|
error: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
const service = { widget: { type: "unifi_drive" } };
|
||||||
|
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||||
|
|
||||||
|
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||||
|
expectBlockValue(container, "resources.total", 1500000000000);
|
||||||
|
expectBlockValue(container, "resources.used", 400000000000);
|
||||||
|
expectBlockValue(container, "resources.free", 1100000000000);
|
||||||
|
expectBlockValue(container, "widget.status", "unifi_drive.healthy");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders degraded status when any pool is degraded", () => {
|
||||||
|
useWidgetAPI.mockReturnValue({
|
||||||
|
data: {
|
||||||
|
pools: [
|
||||||
|
{ capacity: 1000, usage: 400, status: "fullyOperational" },
|
||||||
|
{ capacity: 500, usage: 100, status: "degraded" },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
error: undefined,
|
error: undefined,
|
||||||
});
|
});
|
||||||
@@ -87,6 +112,37 @@ describe("widgets/unifi_drive/component", () => {
|
|||||||
|
|
||||||
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||||
expectBlockValue(container, "widget.status", "unifi_drive.degraded");
|
expectBlockValue(container, "widget.status", "unifi_drive.degraded");
|
||||||
expectBlockValue(container, "resources.free", 65);
|
expectBlockValue(container, "resources.free", 1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders noDataProtectionYet as healthy", () => {
|
||||||
|
useWidgetAPI.mockReturnValue({
|
||||||
|
data: {
|
||||||
|
pools: [{ capacity: 1000, usage: 200, status: "noDataProtectionYet" }],
|
||||||
|
},
|
||||||
|
error: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
const service = { widget: { type: "unifi_drive" } };
|
||||||
|
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||||
|
|
||||||
|
expectBlockValue(container, "widget.status", "unifi_drive.healthy");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles pools with missing capacity or usage", () => {
|
||||||
|
useWidgetAPI.mockReturnValue({
|
||||||
|
data: {
|
||||||
|
pools: [{ status: "fullyOperational" }],
|
||||||
|
},
|
||||||
|
error: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
const service = { widget: { type: "unifi_drive" } };
|
||||||
|
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||||
|
|
||||||
|
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||||
|
expectBlockValue(container, "resources.total", 0);
|
||||||
|
expectBlockValue(container, "resources.used", 0);
|
||||||
|
expectBlockValue(container, "resources.free", 0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ const widget = {
|
|||||||
|
|
||||||
mappings: {
|
mappings: {
|
||||||
storage: {
|
storage: {
|
||||||
endpoint: "v1/systems/storage?type=detail",
|
endpoint: "v2/storage",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user