2026-09-15 14:12:51 +07:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
import {
|
|
|
|
|
SAMPLE_FILE_EXTENSIONS,
|
|
|
|
|
fileKindFromUrl,
|
|
|
|
|
imageContentTypeFromUrl,
|
|
|
|
|
imageFormatFromUrl,
|
|
|
|
|
} from "@/utils/file-kind";
|
|
|
|
|
|
|
|
|
|
describe("fileKindFromUrl", () => {
|
|
|
|
|
it("pdf → pdf, รูปทุกนามสกุล → image (case-insensitive)", () => {
|
|
|
|
|
expect(fileKindFromUrl("/samples/a.pdf")).toBe("pdf");
|
|
|
|
|
expect(fileKindFromUrl("/samples/photo.JPG")).toBe("image");
|
|
|
|
|
expect(fileKindFromUrl("/uploads/scan.Jpeg")).toBe("image");
|
|
|
|
|
expect(fileKindFromUrl("/uploads/scan.png")).toBe("image");
|
|
|
|
|
expect(fileKindFromUrl("/uploads/scan.TIF")).toBe("image");
|
|
|
|
|
expect(fileKindFromUrl("/uploads/scan.tiff")).toBe("image");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("ไม่รู้จัก extension = pdf ตามพฤติกรรมก่อน N3", () => {
|
|
|
|
|
expect(fileKindFromUrl("/samples/file.docx")).toBe("pdf");
|
|
|
|
|
expect(fileKindFromUrl("/samples/noext")).toBe("pdf");
|
|
|
|
|
expect(fileKindFromUrl("/samples/.jpg")).toBe("pdf"); // dotfile ไม่ใช่ extension
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("imageFormatFromUrl", () => {
|
|
|
|
|
it("จับคู่นามสกุล → format (รวม query string / จุดซ้ำในชื่อ)", () => {
|
|
|
|
|
expect(imageFormatFromUrl("/a/b/x.jpg?v=2")).toBe("jpeg");
|
|
|
|
|
expect(imageFormatFromUrl("/a/b/x.jpeg")).toBe("jpeg");
|
|
|
|
|
expect(imageFormatFromUrl("/a/b/scan.v2.png")).toBe("png");
|
|
|
|
|
expect(imageFormatFromUrl("/a/b/x.tif")).toBe("tiff");
|
|
|
|
|
expect(imageFormatFromUrl("/a/b/x.tiff")).toBe("tiff");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("ไม่ใช่รูป → null (รวมชื่อที่คล้ายนามสกุลแต่เป็นโฟลเดอร์)", () => {
|
|
|
|
|
expect(imageFormatFromUrl("/a/b/x.pdf")).toBeNull();
|
|
|
|
|
expect(imageFormatFromUrl("/jpg.png/jpg")).toBeNull();
|
|
|
|
|
expect(imageFormatFromUrl("")).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("imageContentTypeFromUrl", () => {
|
|
|
|
|
it("map format → MIME ที่ถูกต้อง", () => {
|
|
|
|
|
expect(imageContentTypeFromUrl("/a/x.jpg")).toBe("image/jpeg");
|
|
|
|
|
expect(imageContentTypeFromUrl("/a/x.PNG")).toBe("image/png");
|
|
|
|
|
expect(imageContentTypeFromUrl("/a/x.tiff")).toBe("image/tiff");
|
|
|
|
|
expect(imageContentTypeFromUrl("/a/x.pdf")).toBe("application/octet-stream");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-05 12:53:56 +07:00
|
|
|
describe("sentinel staged: ของ N11 (PLAN-STAGED.md D4)", () => {
|
|
|
|
|
const uid = "0d7e6a52-9a71-4d40-bd1c-b2e1a44ab133";
|
|
|
|
|
// ล็อกสมมติฐาน "ext ใน sentinel เข้ากับ extOf() ทุกตัว" กัน helper เปลี่ยนวิธี
|
|
|
|
|
// parse ภายหลัง — sentinel คือ pdf_url จริงใน DB ตั้งแต่ N11
|
|
|
|
|
it("ครบ 4 ext: fileKindFromUrl แยก pdf/image ตาม extension ของ sentinel", () => {
|
|
|
|
|
expect(fileKindFromUrl(`staged:${uid}.pdf`)).toBe("pdf");
|
|
|
|
|
expect(fileKindFromUrl(`staged:${uid}.jpg`)).toBe("image");
|
|
|
|
|
expect(fileKindFromUrl(`staged:${uid}.png`)).toBe("image");
|
|
|
|
|
expect(fileKindFromUrl(`staged:${uid}.tif`)).toBe("image");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("imageFormatFromUrl / imageContentTypeFromUrl ทำงานกับ sentinel (pdf ไม่ผ่านตัวรูป — สถานะเดิมคืน octet-stream)", () => {
|
|
|
|
|
expect(imageFormatFromUrl(`staged:${uid}.pdf`)).toBeNull();
|
|
|
|
|
expect(imageFormatFromUrl(`staged:${uid}.jpg`)).toBe("jpeg");
|
|
|
|
|
expect(imageFormatFromUrl(`staged:${uid}.png`)).toBe("png");
|
|
|
|
|
expect(imageFormatFromUrl(`staged:${uid}.tif`)).toBe("tiff");
|
|
|
|
|
expect(imageContentTypeFromUrl(`staged:${uid}.jpg`)).toBe("image/jpeg");
|
|
|
|
|
expect(imageContentTypeFromUrl(`staged:${uid}.png`)).toBe("image/png");
|
|
|
|
|
expect(imageContentTypeFromUrl(`staged:${uid}.tif`)).toBe("image/tiff");
|
|
|
|
|
expect(imageContentTypeFromUrl(`staged:${uid}.pdf`)).toBe("application/octet-stream");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-15 14:12:51 +07:00
|
|
|
describe("SAMPLE_FILE_EXTENSIONS", () => {
|
|
|
|
|
it("ครบทุกนามสกุลที่ requirement 5.3.4 ระบุ", () => {
|
|
|
|
|
for (const ext of [".pdf", ".jpg", ".jpeg", ".png", ".tif", ".tiff"]) {
|
|
|
|
|
expect(SAMPLE_FILE_EXTENSIONS).toContain(ext);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|