feat: add provider-neutral particle field adapter contract
Some checks failed
validate-scripts / validate-scripts (push) Has been cancelled

This commit is contained in:
OpenAI Codex
2026-09-04 11:26:14 +08:00
parent f095447678
commit ce1a148f27
12 changed files with 371 additions and 143 deletions

View File

@@ -22,6 +22,7 @@
#include <mbgl/style/sources/raster_source.hpp>
#include <mbgl/style/sources/vector_source.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/particle/field/field_data_adapter.hpp>
#include <mbgl/renderer/sources/render_geojson_source.hpp>
#include <mbgl/renderer/sources/render_raster_array_source.hpp>
@@ -53,6 +54,7 @@
#include <gmock/gmock.h>
#include <optional>
#include <cstring>
namespace {
@@ -231,6 +233,46 @@ TEST(Source, RasterArrayGpuContentIdentityExcludesRuntimeGeneration) {
EXPECT_FALSE(particle::sameFieldGpuContent(east, changed));
}
TEST(Source, ParticleFieldDataAdapterAcceptsNeutralDescriptor) {
particle::ParticleFieldDescriptor descriptor;
descriptor.fieldID = "weather-service-wind-20260904T0000Z";
descriptor.fieldType = "WIND";
descriptor.displayDomain = "GLOBAL";
descriptor.width = 1;
descriptor.height = 1;
descriptor.byteLength = 2 * sizeof(float);
float values[] = {1.0f, 2.0f};
std::vector<char> bytes(sizeof(values));
std::memcpy(bytes.data(), values, sizeof(values));
const auto result = particle::ParticleFieldDataAdapter::decode(descriptor, bytes, 1);
ASSERT_TRUE(result);
ASSERT_TRUE(result.snapshot);
EXPECT_EQ(result.snapshot->fieldID, descriptor.fieldID);
EXPECT_EQ(result.error, particle::ParticleFieldAdapterError::None);
}
TEST(Source, ParticleFieldDataAdapterReturnsStructuredValidationErrors) {
particle::ParticleFieldDescriptor descriptor;
descriptor.fieldID = "field";
descriptor.fieldType = "WIND";
descriptor.displayDomain = "GLOBAL";
descriptor.width = 2;
descriptor.height = 1;
descriptor.byteLength = 4 * sizeof(float);
std::vector<char> bytes(3 * sizeof(float));
auto result = particle::ParticleFieldDataAdapter::decode(descriptor, bytes, 1);
EXPECT_FALSE(result);
EXPECT_EQ(result.error, particle::ParticleFieldAdapterError::PayloadSizeMismatch);
descriptor.byteLength = bytes.size();
descriptor.encoding = "provider-packed";
result = particle::ParticleFieldDataAdapter::decode(descriptor, bytes, 1);
EXPECT_FALSE(result);
EXPECT_EQ(result.error, particle::ParticleFieldAdapterError::UnsupportedFormat);
}
TEST(Source, RasterArrayGpuReuseConsumesNewRevision) {
const auto oldT1 = particle::OfflineFixtureParticleAdapter::load("uniform-north", 100);
auto wantedNewT0 = oldT1;
@@ -306,9 +348,9 @@ TEST(Source, RasterArrayRuntimeFieldWindowAdvancesWithoutReplacingTheSource) {
const auto nodata = particle::OfflineFixtureParticleAdapter::load("nodata", vortex.generation + 1);
EXPECT_EQ(source.updateRuntimeFieldWindow(vortex), style::RasterArrayRuntimeFieldUpdateResult::Applied);
EXPECT_EQ(source.impl().field->fixtureID, "uniform-north");
EXPECT_EQ(source.impl().field->fieldID, "uniform-north");
EXPECT_EQ(source.impl().field->generation, initialT1Generation);
EXPECT_EQ(source.impl().nextField->fixtureID, "vortex");
EXPECT_EQ(source.impl().nextField->fieldID, "vortex");
EXPECT_EQ(source.impl().nextField->generation, vortex.generation);
EXPECT_EQ(sourceChangedCount, 2u);
@@ -317,18 +359,18 @@ TEST(Source, RasterArrayRuntimeFieldWindowAdvancesWithoutReplacingTheSource) {
invalid.velocity.clear();
EXPECT_EQ(source.updateRuntimeFieldWindow(invalid),
style::RasterArrayRuntimeFieldUpdateResult::RejectedInvalidSnapshot);
EXPECT_EQ(source.impl().field->fixtureID, "uniform-north");
EXPECT_EQ(source.impl().nextField->fixtureID, "vortex");
EXPECT_EQ(source.impl().field->fieldID, "uniform-north");
EXPECT_EQ(source.impl().nextField->fieldID, "vortex");
EXPECT_EQ(source.updateRuntimeFieldWindow(nodata), style::RasterArrayRuntimeFieldUpdateResult::Applied);
EXPECT_EQ(source.impl().field->fixtureID, "vortex");
EXPECT_EQ(source.impl().nextField->fixtureID, "nodata");
EXPECT_EQ(source.impl().field->fieldID, "vortex");
EXPECT_EQ(source.impl().nextField->fieldID, "nodata");
EXPECT_EQ(sourceChangedCount, 3u);
EXPECT_NE(source.impl().field->generation, initialT0Generation);
EXPECT_EQ(source.repeatRuntimeFieldWindow(), style::RasterArrayRuntimeFieldUpdateResult::Noop);
EXPECT_EQ(source.impl().field->fixtureID, "vortex");
EXPECT_EQ(source.impl().nextField->fixtureID, "nodata");
EXPECT_EQ(source.impl().field->fieldID, "vortex");
EXPECT_EQ(source.impl().nextField->fieldID, "nodata");
EXPECT_EQ(sourceChangedCount, 3u);
}