Added zod for validation
This commit is contained in:
96
lib/api.test.ts
Normal file
96
lib/api.test.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { expect, test, describe } from "bun:test";
|
||||
import { VehicleSchema, VehicleGroupSchema, FirmwareUpdateSchema, DeploymentSchema } from "./api";
|
||||
|
||||
describe("Schema Validation", () => {
|
||||
describe("VehicleSchema", () => {
|
||||
test("should validate valid vehicle object", () => {
|
||||
const validVehicle = {
|
||||
vin: "TEST12345",
|
||||
status: "Online",
|
||||
currentVersion: "1.0.0",
|
||||
lastHeartbeat: "2023-01-01T12:00:00Z",
|
||||
groupId: 1,
|
||||
group: {
|
||||
id: 1,
|
||||
name: "Test Group"
|
||||
}
|
||||
};
|
||||
const result = VehicleSchema.safeParse(validVehicle);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
test("should fail on missing required fields", () => {
|
||||
const invalidVehicle = {
|
||||
vin: "TEST12345",
|
||||
// status missing
|
||||
currentVersion: "1.0.0"
|
||||
};
|
||||
const result = VehicleSchema.safeParse(invalidVehicle);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
test("should allow null/optional fields", () => {
|
||||
const vehicleWithoutGroup = {
|
||||
vin: "TEST_NO_GROUP",
|
||||
status: "Offline",
|
||||
currentVersion: "2.0",
|
||||
lastHeartbeat: "2023-01-01T10:00:00Z",
|
||||
groupId: null,
|
||||
group: null
|
||||
};
|
||||
const result = VehicleSchema.safeParse(vehicleWithoutGroup);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("VehicleGroupSchema", () => {
|
||||
test("should validate valid group", () => {
|
||||
const validGroup = {
|
||||
id: 10,
|
||||
name: "Production",
|
||||
description: "Main fleet",
|
||||
vehicles: []
|
||||
};
|
||||
const result = VehicleGroupSchema.safeParse(validGroup);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
test("should fail with invalid types", () => {
|
||||
const invalidGroup = {
|
||||
id: "should be number",
|
||||
name: "Test"
|
||||
};
|
||||
const result = VehicleGroupSchema.safeParse(invalidGroup);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("FirmwareUpdateSchema", () => {
|
||||
test("should validate update object", () => {
|
||||
const update = {
|
||||
id: 5,
|
||||
version: "1.2.3",
|
||||
uploadedAt: "2023-10-10",
|
||||
description: "Security Patch"
|
||||
};
|
||||
expect(FirmwareUpdateSchema.safeParse(update).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DeploymentSchema", () => {
|
||||
test("should validate deployment", () => {
|
||||
const deployment = {
|
||||
id: 1,
|
||||
updateId: 5,
|
||||
status: "Pending",
|
||||
createdAt: "2023-10-11",
|
||||
update: {
|
||||
id: 5,
|
||||
version: "1.2.3",
|
||||
uploadedAt: "2023-10-10"
|
||||
}
|
||||
};
|
||||
expect(DeploymentSchema.safeParse(deployment).success).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user