feat(integrations): add UniFi OS schemas and pure mappers

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
zacharyandClaude Opus 5 committed 2026-10-08 00:21:50 +02:00
1 parent 13d27fb308
commit 9339e6308b
2 files changed
+372

No files matched your search

@@ -0,0 +1,142 @@
/**
* @vitest-environment node
*/
import { describe, expect, test } from "vitest";
import type { UnifiOsClient, UnifiOsDevice } from "../unifi-os-types";
import {
countClients,
findGateway,
matchCloudSite,
normalizeMacAddress,
summarizeDevices,
unifiOsClientSchema,
} from "../unifi-os-types";
const client = (type: string, accessType = "DEFAULT"): UnifiOsClient => ({
id: `client-${type}-${accessType}-${Math.random()}`,
name: "client",
type,
access: { type: accessType },
});
const device = (overrides: Partial<UnifiOsDevice> = {}): UnifiOsDevice => ({
id: "device-1",
name: "device",
model: "USW Lite 8",
macAddress: "aa:bb:cc:11:22:33",
state: "ONLINE",
firmwareUpdatable: false,
features: ["switching"],
...overrides,
});
describe("normalizeMacAddress", () => {
test("lowercases and strips separators so both APIs compare equal", () => {
expect(normalizeMacAddress("AA:BB:CC:11:22:33")).toBe("aabbcc112233");
expect(normalizeMacAddress("aa-bb-cc-11-22-33")).toBe("aabbcc112233");
expect(normalizeMacAddress("AABBCC112233")).toBe("aabbcc112233");
});
});
describe("countClients", () => {
test("counts each connection type separately", () => {
const counts = countClients([
client("WIRED"),
client("WIRED"),
client("WIRELESS"),
client("VPN"),
client("TELEPORT"),
]);
expect(counts).toEqual({ total: 5, wired: 2, wireless: 1, vpn: 2, guests: 0 });
});
test("folds TELEPORT into vpn because both are remote access", () => {
expect(countClients([client("TELEPORT")]).vpn).toBe(1);
});
test("counts guests across every connection type", () => {
const counts = countClients([client("WIRELESS", "GUEST"), client("WIRED", "GUEST"), client("WIRED")]);
expect(counts.guests).toBe(2);
expect(counts.wired).toBe(2);
});
test("treats an unknown connection type as counted but unclassified", () => {
const counts = countClients([client("SOMETHING_NEW")]);
expect(counts.total).toBe(1);
expect(counts.wired + counts.wireless + counts.vpn).toBe(0);
});
test("tolerates a client without an access block", () => {
const counts = countClients([{ id: "x", name: "x", type: "WIRED" }]);
expect(counts).toEqual({ total: 1, wired: 1, wireless: 0, vpn: 0, guests: 0 });
});
});
describe("summarizeDevices", () => {
test("splits online from everything else and counts pending firmware", () => {
const counts = summarizeDevices([
device({ id: "a", state: "ONLINE", firmwareUpdatable: true }),
device({ id: "b", state: "OFFLINE" }),
device({ id: "c", state: "UPDATING", firmwareUpdatable: true }),
]);
expect(counts).toEqual({ total: 3, online: 1, offline: 2, firmwareUpdatable: 2 });
});
test("returns zeroes for an empty site rather than throwing", () => {
expect(summarizeDevices([])).toEqual({ total: 0, online: 0, offline: 0, firmwareUpdatable: 0 });
});
});
describe("findGateway", () => {
test("prefers the device advertising the gateway feature", () => {
const gateway = device({ id: "gw", features: ["gateway"], model: "UXG Pro" });
expect(findGateway([device(), gateway])?.id).toBe("gw");
});
test("falls back to a gateway-looking model when features are empty", () => {
const gateway = device({ id: "gw", features: [], model: "Gateway Pro" });
expect(findGateway([device(), gateway])?.id).toBe("gw");
});
test("returns null when no gateway is present instead of picking any device", () => {
expect(findGateway([device()])).toBeNull();
});
});
describe("matchCloudSite", () => {
const siteA = { siteId: "a", meta: { gatewayMac: "AA:BB:CC:11:22:33" } };
const siteB = { siteId: "b", meta: { gatewayMac: "dd:ee:ff:44:55:66" } };
test("matches on the gateway MAC regardless of formatting", () => {
expect(matchCloudSite("aa-bb-cc-11-22-33", [siteB, siteA])?.siteId).toBe("a");
});
test("returns null rather than another account site when nothing matches", () => {
expect(matchCloudSite("11:22:33:44:55:66", [siteA, siteB])).toBeNull();
});
test("returns null when the local gateway is unknown", () => {
expect(matchCloudSite(null, [siteA, siteB])).toBeNull();
});
});
describe("unifiOsClientSchema", () => {
test("keeps fields the published OpenAPI does not document", () => {
const parsed = unifiOsClientSchema.parse({
id: "1",
name: "n",
type: "WIRED",
access: { type: "DEFAULT" },
macAddress: "aa:bb:cc:11:22:33",
uplinkDeviceId: "u1",
});
expect(parsed).toMatchObject({ macAddress: "aa:bb:cc:11:22:33", uplinkDeviceId: "u1" });
});
});