File size: 1,200 Bytes
755a930 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /**
* Utils Tests - shared utility functions
*/
import { describe, it, expect } from "vitest";
import { sanitizeName, docPath, DATA_DIR } from "../src/utils.js";
import { join } from "path";
describe("sanitizeName", () => {
it("passes through clean names", () => {
expect(sanitizeName("default")).toBe("default");
expect(sanitizeName("my-doc_123")).toBe("my-doc_123");
});
it("replaces special characters with underscore", () => {
expect(sanitizeName("my doc!@#")).toBe("my_doc___");
expect(sanitizeName("hello world")).toBe("hello_world");
});
it("replaces path traversal characters", () => {
const result = sanitizeName("../../../etc/passwd");
expect(result).not.toContain("/");
expect(result).not.toContain(".");
});
it("handles empty string", () => {
expect(sanitizeName("")).toBe("");
});
});
describe("docPath", () => {
it("returns path within DATA_DIR with .yjs extension", () => {
const result = docPath("default");
expect(result).toBe(join(DATA_DIR, "default.yjs"));
});
it("sanitizes the name in the path", () => {
const result = docPath("my doc!");
expect(result).toBe(join(DATA_DIR, "my_doc_.yjs"));
});
});
|