This commit is contained in:
83
packages/schemas/__tests__/config.spec.ts
Normal file
83
packages/schemas/__tests__/config.spec.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { CustomBackgroundSchema } from "@monkeytype/schemas/configs";
|
||||
|
||||
describe("config schema", () => {
|
||||
describe("CustomBackgroundSchema", () => {
|
||||
it.for([
|
||||
{
|
||||
name: "http",
|
||||
input: `http://example.com/path/image.png`,
|
||||
},
|
||||
{
|
||||
name: "https",
|
||||
input: `https://example.com/path/image.png`,
|
||||
},
|
||||
{
|
||||
name: "png",
|
||||
input: `https://example.com/path/image.png`,
|
||||
},
|
||||
{
|
||||
name: "gif",
|
||||
input: `https://example.com/path/image.gif?width=5`,
|
||||
},
|
||||
{
|
||||
name: "jpeg",
|
||||
input: `https://example.com/path/image.jpeg`,
|
||||
},
|
||||
{
|
||||
name: "jpg",
|
||||
input: `https://example.com/path/image.jpg`,
|
||||
},
|
||||
{
|
||||
name: "tiff",
|
||||
input: `https://example.com/path/image.tiff`,
|
||||
expectedError: "Unsupported image format",
|
||||
},
|
||||
{
|
||||
name: "non-url",
|
||||
input: `test`,
|
||||
expectedError: "Needs to be an URI",
|
||||
},
|
||||
{
|
||||
name: "single quotes",
|
||||
input: `https://example.com/404.jpg?q=alert('1')`,
|
||||
expectedError: "May not contain quotes",
|
||||
},
|
||||
{
|
||||
name: "double quotes",
|
||||
input: `https://example.com/404.jpg?q=alert("1")`,
|
||||
expectedError: "May not contain quotes",
|
||||
},
|
||||
{
|
||||
name: "back tick",
|
||||
input: `https://example.com/404.jpg?q=alert(\`1\`)`,
|
||||
expectedError: "May not contain quotes",
|
||||
},
|
||||
{
|
||||
name: "javascript url",
|
||||
input: `javascript:alert('asdf');//https://example.com/img.jpg`,
|
||||
expectedError: "Unsupported protocol",
|
||||
},
|
||||
{
|
||||
name: "data url",
|
||||
input: `data:image/gif;base64,data`,
|
||||
expectedError: "Unsupported protocol",
|
||||
},
|
||||
{
|
||||
name: "long url",
|
||||
input: `https://example.com/path/image.jpeg?q=${new Array(2048)
|
||||
.fill("x")
|
||||
.join()}`,
|
||||
expectedError: "URL is too long",
|
||||
},
|
||||
])(`$name`, ({ input, expectedError }) => {
|
||||
const parsed = CustomBackgroundSchema.safeParse(input);
|
||||
if (expectedError !== undefined) {
|
||||
expect(parsed.success).toEqual(false);
|
||||
expect(parsed.error?.issues[0]?.message).toEqual(expectedError);
|
||||
} else {
|
||||
expect(parsed.success).toEqual(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
7
packages/schemas/__tests__/tsconfig.json
Normal file
7
packages/schemas/__tests__/tsconfig.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "@monkeytype/typescript-config/base.json",
|
||||
"compilerOptions": {
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["./**/*.ts", "./**/*.spec.ts", "./setup-tests.ts"]
|
||||
}
|
||||
56
packages/schemas/__tests__/util.spec.ts
Normal file
56
packages/schemas/__tests__/util.spec.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { nameWithSeparators, slug } from "../src/util";
|
||||
|
||||
describe("Schema Validation Tests", () => {
|
||||
describe("nameWithSeparators", () => {
|
||||
const schema = nameWithSeparators();
|
||||
|
||||
it("accepts valid names", () => {
|
||||
expect(schema.safeParse("valid_name").success).toBe(true);
|
||||
expect(schema.safeParse("valid-name").success).toBe(true);
|
||||
expect(schema.safeParse("valid123").success).toBe(true);
|
||||
expect(schema.safeParse("Valid_Name-Check").success).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects leading/trailing separators", () => {
|
||||
expect(schema.safeParse("_invalid").success).toBe(false);
|
||||
expect(schema.safeParse("invalid-").success).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects consecutive separators", () => {
|
||||
expect(schema.safeParse("inv__alid").success).toBe(false);
|
||||
expect(schema.safeParse("inv--alid").success).toBe(false);
|
||||
expect(schema.safeParse("inv-_alid").success).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects dots", () => {
|
||||
expect(schema.safeParse("invalid.dot").success).toBe(false);
|
||||
expect(schema.safeParse(".invalid").success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("slug", () => {
|
||||
const schema = slug();
|
||||
|
||||
it("accepts valid slugs", () => {
|
||||
expect(schema.safeParse("valid-slug.123_test").success).toBe(true);
|
||||
expect(schema.safeParse("valid.dots").success).toBe(true);
|
||||
expect(schema.safeParse("_leading_underscore_is_fine").success).toBe(
|
||||
true,
|
||||
);
|
||||
expect(schema.safeParse("-leading_hyphen_is_fine").success).toBe(true);
|
||||
expect(schema.safeParse("trailing_is_fine_in_slug_").success).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects leading dots", () => {
|
||||
expect(schema.safeParse(".invalid").success).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects invalid characters", () => {
|
||||
expect(schema.safeParse("invalid,comma").success).toBe(false);
|
||||
expect(schema.safeParse(",invalid").success).toBe(false);
|
||||
expect(schema.safeParse("invalid space").success).toBe(false);
|
||||
expect(schema.safeParse("invalid#hash").success).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user