Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { imageMimeType } from "./image.ts";
4
5describe("imageMimeType", () => {
6 it("recognizes a picture the file picker left untyped", () => {
7 expect(imageMimeType({ name: "IMG_4997.PNG", mimeType: "" })).toBe("image/png");
8 expect(imageMimeType({ name: "shot.jpg", mimeType: "application/octet-stream" })).toBe(
9 "image/jpeg",
10 );
11 });
12
13 it("trusts a declared supported type", () => {
14 expect(imageMimeType({ name: "no-extension", mimeType: "image/webp" })).toBe("image/webp");
15 expect(imageMimeType({ name: "x.png", mimeType: "image/png; charset=binary" })).toBe(
16 "image/png",
17 );
18 });
19
20 it("leaves images the provider cannot accept as files", () => {
21 expect(imageMimeType({ name: "scan.heic", mimeType: "image/heic" })).toBeNull();
22 expect(imageMimeType({ name: "logo.svg", mimeType: "image/svg+xml" })).toBeNull();
23 });
24
25 it("leaves documents and videos alone", () => {
26 expect(imageMimeType({ name: "notes.txt", mimeType: "text/plain" })).toBeNull();
27 expect(imageMimeType({ name: "clip.mp4", mimeType: "video/mp4" })).toBeNull();
28 expect(imageMimeType({ name: "noext", mimeType: "" })).toBeNull();
29 });
30});
31
32describe("legacy attachments", () => {
33 it("recognizes a picture that was sent before pictures were typed by content", () => {
34 expect(imageMimeType({ name: "IMG_4996.PNG", mimeType: "application/octet-stream" })).toBe(
35 "image/png",
36 );
37 expect(imageMimeType({ name: "1000000020.png", mimeType: "" })).toBe("image/png");
38 });
39});
40
41describe("a declared non-image type", () => {
42 it("wins over a misleading picture extension", () => {
43 expect(imageMimeType({ name: "report.png", mimeType: "application/pdf" })).toBeNull();
44 expect(imageMimeType({ name: "archive.jpg", mimeType: "application/zip" })).toBeNull();
45 expect(imageMimeType({ name: "clip.png", mimeType: "video/mp4" })).toBeNull();
46 });
47
48 it("still falls back when the type is absent or generic", () => {
49 expect(imageMimeType({ name: "IMG_4996.PNG", mimeType: "application/octet-stream" })).toBe(
50 "image/png",
51 );
52 expect(imageMimeType({ name: "1000000020.png", mimeType: "" })).toBe("image/png");
53 });
54});