import { describe, expect, it } from "vitest";
import { ErrorCode } from "@pocketcoder/shared/constants";
import { AppError } from "../../errors/index.js";
import { validateFilename } from "../filename-validation.js";
describe("filename-validation", () => {
    describe("valid filenames", () => {
        it("should allow normal filenames", () => {
            expect(() => validateFilename("file.txt")).not.toThrow();
            expect(() => validateFilename("my-document.md")).not.toThrow();
            expect(() => validateFilename("index.ts")).not.toThrow();
        });
        it("should allow filenames with dots", () => {
            expect(() => validateFilename(".gitignore")).not.toThrow();
            expect(() => validateFilename("file.test.ts")).not.toThrow();
        });
        it("should allow filenames with hyphens and underscores", () => {
            expect(() => validateFilename("my_file.txt")).not.toThrow();
            expect(() => validateFilename("my-file.txt")).not.toThrow();
        });
        it("should allow filenames with spaces", () => {
            expect(() => validateFilename("my file.txt")).not.toThrow();
        });
    });
    describe("invalid characters", () => {
        it("should reject < character", () => {
            expect(() => validateFilename("file<name")).toThrow(AppError);
        });
        it("should reject > character", () => {
            expect(() => validateFilename("file>name")).toThrow(AppError);
        });
        it("should reject : character", () => {
            expect(() => validateFilename("file:name")).toThrow(AppError);
        });
        it('should reject " character', () => {
            expect(() => validateFilename('file"name')).toThrow(AppError);
        });
        it("should reject | character", () => {
            expect(() => validateFilename("file|name")).toThrow(AppError);
        });
        it("should reject ? character", () => {
            expect(() => validateFilename("file?name")).toThrow(AppError);
        });
        it("should reject * character", () => {
            expect(() => validateFilename("file*name")).toThrow(AppError);
        });
        it("should reject control characters", () => {
            expect(() => validateFilename("file\x00name")).toThrow(AppError);
            expect(() => validateFilename("file\x1fname")).toThrow(AppError);
        });
        it("should throw with INVALID_FILENAME error code", () => {
            try {
                validateFilename("file<name");
            }
            catch (err) {
                expect(err).toBeInstanceOf(AppError);
                expect(err.code).toBe(ErrorCode.INVALID_FILENAME);
            }
        });
    });
    describe("reserved names", () => {
        it("should reject con", () => {
            expect(() => validateFilename("con")).toThrow(AppError);
        });
        it("should reject prn", () => {
            expect(() => validateFilename("prn")).toThrow(AppError);
        });
        it("should reject aux", () => {
            expect(() => validateFilename("aux")).toThrow(AppError);
        });
        it("should reject nul", () => {
            expect(() => validateFilename("nul")).toThrow(AppError);
        });
        it("should reject com1 through com9", () => {
            for (let i = 1; i <= 9; i++) {
                expect(() => validateFilename(`com${i}`)).toThrow(AppError);
            }
        });
        it("should reject lpt1 through lpt9", () => {
            for (let i = 1; i <= 9; i++) {
                expect(() => validateFilename(`lpt${i}`)).toThrow(AppError);
            }
        });
        it("should reject reserved names case-insensitively", () => {
            expect(() => validateFilename("CON")).toThrow(AppError);
            expect(() => validateFilename("Con")).toThrow(AppError);
            expect(() => validateFilename("PRN")).toThrow(AppError);
            expect(() => validateFilename("NUL")).toThrow(AppError);
            expect(() => validateFilename("COM1")).toThrow(AppError);
            expect(() => validateFilename("LPT1")).toThrow(AppError);
        });
        it("should reject reserved names with extensions", () => {
            expect(() => validateFilename("con.txt")).toThrow(AppError);
            expect(() => validateFilename("nul.log")).toThrow(AppError);
            expect(() => validateFilename("COM1.dat")).toThrow(AppError);
        });
    });
    describe("dot and dotdot", () => {
        it('should reject "."', () => {
            expect(() => validateFilename(".")).toThrow(AppError);
        });
        it('should reject ".."', () => {
            expect(() => validateFilename("..")).toThrow(AppError);
        });
    });
    describe("empty filenames", () => {
        it("should reject empty string", () => {
            expect(() => validateFilename("")).toThrow(AppError);
        });
        it("should reject whitespace-only string", () => {
            expect(() => validateFilename("   ")).toThrow(AppError);
        });
    });
});
//# sourceMappingURL=filename-validation.test.js.map