131 lines
3.8 KiB
JavaScript
131 lines
3.8 KiB
JavaScript
/**
|
|
* Input Validator Tests
|
|
* Tests for comprehensive input validation
|
|
* Requirements: 5.4, 5.6
|
|
*/
|
|
|
|
const inputValidator = require("../../../src/tui/utils/inputValidator");
|
|
|
|
describe("InputValidator Tests", () => {
|
|
test("should validate operation type correctly", () => {
|
|
const validResult = inputValidator.validateField("operationType", "update");
|
|
expect(validResult.isValid).toBe(true);
|
|
expect(validResult.value).toBe("update");
|
|
|
|
const invalidResult = inputValidator.validateField(
|
|
"operationType",
|
|
"invalid"
|
|
);
|
|
expect(invalidResult.isValid).toBe(false);
|
|
expect(invalidResult.errors).toContain(
|
|
"operationType must be one of: update, rollback"
|
|
);
|
|
});
|
|
|
|
test("should validate scheduled time correctly", () => {
|
|
const futureDate = new Date(Date.now() + 86400000).toISOString();
|
|
const validResult = inputValidator.validateField(
|
|
"scheduledTime",
|
|
futureDate
|
|
);
|
|
expect(validResult.isValid).toBe(true);
|
|
|
|
const pastDate = new Date(Date.now() - 86400000).toISOString();
|
|
const invalidResult = inputValidator.validateField(
|
|
"scheduledTime",
|
|
pastDate
|
|
);
|
|
expect(invalidResult.isValid).toBe(false);
|
|
});
|
|
|
|
test("should validate shop domain correctly", () => {
|
|
const validDomain = "test-store.myshopify.com";
|
|
const validResult = inputValidator.validateField("shopDomain", validDomain);
|
|
expect(validResult.isValid).toBe(true);
|
|
|
|
const invalidDomain = "invalid domain";
|
|
const invalidResult = inputValidator.validateField(
|
|
"shopDomain",
|
|
invalidDomain
|
|
);
|
|
expect(invalidResult.isValid).toBe(false);
|
|
});
|
|
|
|
test("should validate price adjustment correctly", () => {
|
|
const validPercentage = 25.5;
|
|
const validResult = inputValidator.validateField(
|
|
"priceAdjustment",
|
|
validPercentage
|
|
);
|
|
expect(validResult.isValid).toBe(true);
|
|
expect(validResult.value).toBe(25.5);
|
|
|
|
const invalidPercentage = 1500; // Too high
|
|
const invalidResult = inputValidator.validateField(
|
|
"priceAdjustment",
|
|
invalidPercentage
|
|
);
|
|
expect(invalidResult.isValid).toBe(false);
|
|
});
|
|
|
|
test("should validate multiple fields", () => {
|
|
const data = {
|
|
operationType: "update",
|
|
scheduledTime: new Date(Date.now() + 86400000).toISOString(),
|
|
recurrence: "weekly",
|
|
description: "Test schedule",
|
|
};
|
|
|
|
const result = inputValidator.validateFields(data);
|
|
expect(result.isValid).toBe(true);
|
|
expect(result.data.operationType).toBe("update");
|
|
expect(result.data.recurrence).toBe("weekly");
|
|
});
|
|
|
|
test("should handle optional fields correctly", () => {
|
|
const data = {
|
|
operationType: "update",
|
|
scheduledTime: new Date(Date.now() + 86400000).toISOString(),
|
|
recurrence: "once",
|
|
// description is optional and missing
|
|
};
|
|
|
|
const result = inputValidator.validateFields(data);
|
|
expect(result.isValid).toBe(true);
|
|
expect(result.data.description).toBeUndefined();
|
|
});
|
|
|
|
test("should convert string numbers to numbers", () => {
|
|
const result = inputValidator.validateField("priceAdjustment", "25.5");
|
|
expect(result.isValid).toBe(true);
|
|
expect(result.value).toBe(25.5);
|
|
expect(typeof result.value).toBe("number");
|
|
});
|
|
|
|
test("should sanitize input strings", () => {
|
|
const dirtyInput = " test string with \x00 control chars ";
|
|
const sanitized = inputValidator.sanitizeInput(dirtyInput, {
|
|
trim: true,
|
|
removeControlChars: true,
|
|
});
|
|
|
|
expect(sanitized).toBe("test string with control chars");
|
|
});
|
|
|
|
test("should validate string length limits", () => {
|
|
const longDescription = "x".repeat(501);
|
|
const result = inputValidator.validateField("description", longDescription);
|
|
|
|
expect(result.isValid).toBe(false);
|
|
expect(
|
|
result.errors.some((error) => error.includes("500 characters"))
|
|
).toBe(true);
|
|
});
|
|
|
|
test("should validate required fields", () => {
|
|
const result = inputValidator.validateField("operationType", "");
|
|
expect(result.isValid).toBe(false);
|
|
expect(result.errors).toContain("operationType is required");
|
|
});
|
|
});
|