asset-refs.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { describe, expect, it } from "vitest";
  2. import type { MapDef, SoundDef, SpriteDef } from "@nengine/core";
  3. import { snapshotAssetRefs } from "../src/sync/asset-refs.js";
  4. function map(): MapDef {
  5. return {
  6. id: "farm",
  7. width: 2,
  8. height: 2,
  9. tilesetRef: "tiles",
  10. spawn: { x: 0, y: 0 },
  11. layers: [{ name: "ground", data: [["grass", "water"], ["grass", "grass"]] }],
  12. tiles: { grass: "grass-1", water: "water-1" },
  13. };
  14. }
  15. describe("snapshotAssetRefs (D37 wanted-id derivation)", () => {
  16. it("collects image assets from the map tiles mapping and sprite image layers", () => {
  17. const sprites: SpriteDef[] = [
  18. {
  19. id: "s1",
  20. frames: [
  21. { layers: [{ shape: "rect", x: 0, y: 0, w: 1, h: 1, fill: "#ff0000" }] },
  22. { layers: [{ shape: "image", src: "plant-sprout" }] },
  23. ],
  24. },
  25. ];
  26. const refs = snapshotAssetRefs({ map: map(), sprites });
  27. expect(refs.images.sort()).toEqual(["grass-1", "plant-sprout", "water-1"]);
  28. expect(refs.audio).toEqual([]);
  29. });
  30. it("collects audio assets from sound def src ids and ignores synth-only defs", () => {
  31. const sounds: SoundDef[] = [
  32. { id: "till", src: "drop-1" },
  33. { id: "coin", synth: { wave: "sine", freq: 1, durationMs: 1 } },
  34. ];
  35. const refs = snapshotAssetRefs({ map: map(), sounds });
  36. expect(refs.audio).toEqual(["drop-1"]);
  37. expect(refs.images).toEqual(["grass-1", "water-1"]);
  38. });
  39. it("returns empty refs for a snapshot with no tiles mapping, no image layers, no src sounds", () => {
  40. const plain = { ...map(), tiles: undefined };
  41. const refs = snapshotAssetRefs({ map: plain });
  42. expect(refs.images).toEqual([]);
  43. expect(refs.audio).toEqual([]);
  44. });
  45. });