| 1 | import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { |
| 5 | attachmentUploadBlockReason, |
| 6 | formatAttachmentUploadProgress, |
| 7 | } from "./attachmentUploadState"; |
| 8 | |
| 9 | const environmentId = EnvironmentId.make("environment-1"); |
| 10 | |
| 11 | describe("attachmentUploadBlockReason", () => { |
| 12 | it("allows uploaded images from the active environment", () => { |
| 13 | expect( |
| 14 | attachmentUploadBlockReason({ |
| 15 | imageIds: ["image-1"], |
| 16 | environmentId, |
| 17 | uploadsByImageId: { |
| 18 | "image-1": { |
| 19 | status: "ready", |
| 20 | environmentId, |
| 21 | attachmentId: "pending-1", |
| 22 | }, |
| 23 | }, |
| 24 | }), |
| 25 | ).toBeNull(); |
| 26 | }); |
| 27 | |
| 28 | it("blocks images that are missing or uploading", () => { |
| 29 | expect( |
| 30 | attachmentUploadBlockReason({ |
| 31 | imageIds: ["image-1", "image-2"], |
| 32 | environmentId, |
| 33 | uploadsByImageId: { |
| 34 | "image-1": { status: "uploading", environmentId, progress: 0.5 }, |
| 35 | }, |
| 36 | }), |
| 37 | ).toBe("Attachments still uploading"); |
| 38 | }); |
| 39 | |
| 40 | it("asks the user to retry or remove failed uploads", () => { |
| 41 | expect( |
| 42 | attachmentUploadBlockReason({ |
| 43 | imageIds: ["image-1"], |
| 44 | environmentId, |
| 45 | uploadsByImageId: { |
| 46 | "image-1": { status: "failed", environmentId, reason: "Upload failed" }, |
| 47 | }, |
| 48 | }), |
| 49 | ).toBe("Retry or remove the failed attachment"); |
| 50 | }); |
| 51 | |
| 52 | it("does not accept an upload from another environment", () => { |
| 53 | expect( |
| 54 | attachmentUploadBlockReason({ |
| 55 | imageIds: ["image-1"], |
| 56 | environmentId, |
| 57 | uploadsByImageId: { |
| 58 | "image-1": { |
| 59 | status: "ready", |
| 60 | environmentId: EnvironmentId.make("environment-2"), |
| 61 | attachmentId: "pending-1", |
| 62 | }, |
| 63 | }, |
| 64 | }), |
| 65 | ).toBe("Attachment still uploading"); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | describe("formatAttachmentUploadProgress", () => { |
| 70 | it("formats bounded whole percentages", () => { |
| 71 | expect(formatAttachmentUploadProgress(0.429)).toBe("42%"); |
| 72 | expect(formatAttachmentUploadProgress(2)).toBe("100%"); |
| 73 | expect(formatAttachmentUploadProgress(Number.NaN)).toBe("0%"); |
| 74 | }); |
| 75 | }); |