assets-model.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { describe, expect, it } from "vitest";
  2. import type { EditorAssetInfo } from "../src/ipc.js";
  3. import { audioAssets, formatAssetSize, imageAssets, mergeImported, removeById } from "../src/renderer/assets-model.js";
  4. function image(id: string): EditorAssetInfo {
  5. return { id, kind: "image", mime: "image/png", size: 10, dataUrl: `data:image/png;base64,${id}` };
  6. }
  7. function audio(id: string): EditorAssetInfo {
  8. return { id, kind: "audio", mime: "audio/ogg", size: 20 };
  9. }
  10. describe("assets-model (F2)", () => {
  11. it("splits the list into images and audio", () => {
  12. const list = [image("a"), audio("b"), image("c")];
  13. expect(imageAssets(list).map((a) => a.id)).toEqual(["a", "c"]);
  14. expect(audioAssets(list).map((a) => a.id)).toEqual(["b"]);
  15. });
  16. it("mergeImported adds new ids, replaces existing ones, and sorts by id", () => {
  17. const current = [image("b"), image("a")];
  18. const merged = mergeImported(current, [audio("c"), image("a")]);
  19. expect(merged.map((a) => a.id)).toEqual(["a", "b", "c"]);
  20. expect(merged[0]?.kind).toBe("image");
  21. expect(merged[2]?.kind).toBe("audio");
  22. });
  23. it("removeById drops the deleted entry and formatAssetSize is human-readable", () => {
  24. const list = [image("a"), audio("b")];
  25. expect(removeById(list, "a").map((a) => a.id)).toEqual(["b"]);
  26. expect(removeById(list, "missing").map((a) => a.id)).toEqual(["a", "b"]);
  27. expect(formatAssetSize(512)).toBe("512 B");
  28. expect(formatAssetSize(2048)).toBe("2.0 KB");
  29. expect(formatAssetSize(3 * 1024 * 1024)).toBe("3.0 MB");
  30. });
  31. });