Files
anm2ed/tests/assets.cpp
T
shweetandClaude Opus 5.5 9f43a210c5 Stage 5: content-hash AssetStore; undo records asset keys, not pixels
- resource::AssetStore (core) keeps immutable images and sounds keyed by content
  hash for the life of the document; nothing is purged until it closes.
- Undo state holds spritesheet/sound id -> asset key maps (always tracked), so
  every edit can undo texture changes; the textures-only snapshot flag
  (edit_begin(label, true) / push(message, isTextures)) and the pixel/audio
  equality helpers are gone.
- Document::texture_get/sound_get read through the store; texture_set/sound_set
  add assets; texture_edit gives the pencil a private draft that becomes a new
  asset on the next change().
- Spritesheet dirty state compares path + asset key instead of hashing pixels.
- Preview audio reads sounds through the store.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 15:53:34 -04:00

61 lines
2.0 KiB
C++

#include "common.hpp"
#include "asset_store.hpp"
#include "audio_data.hpp"
#include "image.hpp"
using namespace anm2ed;
using namespace anm2ed::resource;
TEST_CASE("Image png round trip and uid tracking")
{
const std::vector<uint8_t> pixels{255, 0, 0, 255, 0, 255, 0, 128, 0, 0, 255, 0, 10, 20, 30, 40};
Image image(pixels.data(), {2, 2});
REQUIRE(image.is_valid());
auto path = std::filesystem::temp_directory_path() / "anm2ed_test_image.png";
REQUIRE(image.write_png(path));
Image loaded(path);
std::filesystem::remove(path);
CHECK(loaded.size == image.size);
CHECK(loaded.pixels == image.pixels);
CHECK(loaded.uid != image.uid);
auto copy = image;
CHECK(copy.uid == image.uid);
copy.pixel_set({1, 1}, {1.0f, 1.0f, 1.0f, 1.0f});
CHECK(copy.uid != image.uid);
CHECK(copy.pixel_read({1, 1}) == glm::vec4(1.0f));
auto merged = Image::merge_append(image, loaded, true);
CHECK(merged.size == glm::ivec2(4, 2));
CHECK(merged.pixel_read({2, 0}) == image.pixel_read({0, 0}));
}
TEST_CASE("AudioData holds file bytes")
{
const unsigned char bytes[] = {'R', 'I', 'F', 'F'};
AudioData audio(bytes, sizeof(bytes));
CHECK(audio.is_valid());
CHECK(audio.uid != 0);
CHECK_FALSE(AudioData(std::filesystem::path("missing.wav")).is_valid());
}
TEST_CASE("AssetStore keys assets by content")
{
AssetStore store{};
const std::vector<uint8_t> pixels{1, 2, 3, 4};
auto key = store.image_add(Image(pixels.data(), {1, 1}));
CHECK(key != 0);
CHECK(store.image_add(Image(pixels.data(), {1, 1})) == key);
CHECK(store.image_add(Image(pixels.data(), {1, 1}, true)) != key);
REQUIRE(store.image_get(key));
CHECK(store.image_get(key)->pixels == pixels);
CHECK_FALSE(store.image_get(key + 1));
const unsigned char bytes[] = {'O', 'g', 'g', 'S'};
auto audioKey = store.audio_add(AudioData(bytes, sizeof(bytes)));
CHECK(store.audio_add(AudioData(bytes, sizeof(bytes))) == audioKey);
CHECK(store.audio_get(audioKey)->bytes.size() == sizeof(bytes));
}