Files
secs-gem/tests/test_loader.cpp
T
raphael 711ee1b40f #4 Split EquipmentDataModel into focused stores
The god-class is gone.  Each capability is now its own focused store:
StatusVariableStore, DataVariableStore, EquipmentConstantStore (with EAC
range validation), EventReportSubscriptions, AlarmRegistry, RecipeStore,
Clock, HostCommandRegistry.  Each is independently testable.

EquipmentDataModel becomes a small composite that holds one of each store
as a public member, plus three convenience methods (vid_value, vid_exists,
compose_reports_for) that span SVIDs+DVIDs and inject the right callbacks
into the EventReportSubscriptions.

New under include/secsgem/gem/store/:

  status_variables.hpp   StatusVariable, StatusVariableStore,
                         DataVariable, DataVariableStore
  equipment_constants.hpp EquipmentConstant, EquipmentConstantStore,
                          EquipmentAck. set_value() now validates
                          numeric values against min_str/max_str and
                          returns EAC=4 on out-of-range — closes the
                          COMPLIANCE.md gap about EC range validation.
  event_reports.hpp      CollectionEvent, Report, ReportData,
                         EventReportSubscriptions + DefineReportAck,
                         LinkEventAck, EnableEventAck. The store is
                         pure data; VidLookup / VidExists callbacks
                         are injected at define / emit time so the
                         service doesn't back-reference the SVID
                         store.
  alarms.hpp             Alarm, AlarmAck, AlarmRegistry.
                         Encapsulates the (enabled, active) sets and
                         ALCD byte computation.
  recipes.hpp            ProcessProgramAck, RecipeStore.
  clock.hpp              TimeAck, Clock. set_time_string applies an
                         offset so subsequent reads reflect the host
                         time without mutating system clock.
  host_commands.hpp      HostCmdAck, CommandParameter,
                         HostCommandRegistry with Spec/Result types.

include/secsgem/gem/data_model.hpp shrinks to a 50-line composite:

  struct EquipmentDataModel {
    StatusVariableStore       svids;
    DataVariableStore         dvids;
    EquipmentConstantStore    ecids;
    EventReportSubscriptions  events;
    AlarmRegistry             alarms;
    RecipeStore               recipes;
    Clock                     clock;
    HostCommandRegistry       commands;
    /* + vid_value, vid_exists, compose_reports_for sugar */
  };

src/gem/data_model.cpp is gone — every store is inline header-only.

include/secsgem/gem/messages_helpers.hpp picks up EventReportAck and
TerminalAck (S6F12 / S10F2-F4 ack enums that aren't tied to any one
store).

Call-site updates:

  apps/secs_server.cpp   model->status_variable(id) -> model->svids.get(id),
                         model->equipment_constant(id) -> model->ecids.get(id),
                         model->alarm_set(id) -> model->alarms.set_active(id),
                         model->dispatch_command(...) -> model->commands.dispatch(...),
                         and similar across every handler.  Plus
                         model->current_time_string() -> model->clock....

  src/config/loader.cpp  model.add_status_variable(sv) -> model.svids.add(sv),
                         and similar.  HostCommandRegistry::Spec replaces
                         EquipmentDataModel::CommandSpec.

  apps/secs_client.cpp   std::vector<EquipmentDataModel::CommandParam> ->
                         std::vector<CommandParameter>.

  tests/test_data_model.cpp  Rewritten around the individual stores;
                         each gets its own TEST_CASE block.  Adds three
                         new cases covering EC range validation (in
                         range / out of range / non-numeric skipped).

  tests/test_loader.cpp  m.has_event(100) -> m.events.has_event(100),
                         etc.

Verified:

  - Tests: 69 cases / 370 assertions pass (was 67 / 384; -14 stale
    composite-API assertions + 16 new store-level assertions covering
    EC range validation and the per-store add/get/list/delete paths).
  - Demo: byte-identical behaviour across the full 17-step flow.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 09:51:54 +02:00

73 lines
2.4 KiB
C++

#include <doctest/doctest.h>
#include <fstream>
#include <string>
#include "secsgem/config/loader.hpp"
#include "secsgem/secs2/item.hpp"
using namespace secsgem;
namespace gem = secsgem::gem;
namespace s2 = secsgem::secs2;
#ifndef SECSGEM_DATA_DIR
#error "SECSGEM_DATA_DIR not defined; see CMakeLists.txt"
#endif
TEST_CASE("control_state.yaml parses into the same shape as default_table()") {
auto cfg = config::load_control_state(SECSGEM_DATA_DIR "/control_state.yaml");
auto baseline = gem::ControlTransitionTable::default_table();
CHECK(cfg.initial == gem::ControlState::HostOffline);
// Same row count; same (from, on) pairs map to the same destination.
CHECK(cfg.table.size() == baseline.size());
for (const auto& r : baseline.rows()) {
const auto* loaded = cfg.table.find(r.from, r.on);
REQUIRE(loaded != nullptr);
CHECK(loaded->to == r.to);
CHECK(loaded->then == r.then);
CHECK(loaded->ack_code == r.ack_code);
}
}
TEST_CASE("equipment.yaml populates SVIDs, ECIDs, CEIDs, alarms, recipes, commands") {
gem::EquipmentDataModel m;
auto desc = config::load_equipment(SECSGEM_DATA_DIR "/equipment.yaml", m);
CHECK(desc.model_name == "SECSGEM-SIM");
CHECK(desc.software_rev == "0.1.0");
REQUIRE(desc.emit_on_control_change.has_value());
CHECK(*desc.emit_on_control_change == 100);
CHECK(m.svids.all().size() == 3);
CHECK(m.svids.get(1)->name == "ControlState");
CHECK(m.svids.get(3)->value == s2::Item::boolean(true));
CHECK(m.ecids.all().size() == 2);
CHECK(m.ecids.get(10)->value == s2::Item::u4(uint32_t{1}));
CHECK(m.events.all_events().size() == 3);
CHECK(m.events.has_event(100));
CHECK(m.events.has_event(300));
CHECK(m.alarms.all().size() == 2);
CHECK(m.alarms.get(1)->text == "Chiller Temp High");
CHECK(m.alarms.get(1)->severity_category == 4);
CHECK(m.recipes.list().size() == 2);
CHECK(m.recipes.get("RECIPE-A").value().find("CHAMBER ARGON") != std::string::npos);
CHECK(m.commands.has("START"));
auto start = m.commands.dispatch("START", {});
CHECK(start.ack == gem::HostCmdAck::Accept);
CHECK(start.emit_ceid.value_or(0) == 300);
auto fault = m.commands.dispatch("FAULT", {});
CHECK(fault.set_alarm.value_or(0) == 1);
}
TEST_CASE("loader surfaces YAML errors with file path") {
CHECK_THROWS_AS(config::load_equipment("/tmp/does-not-exist.yaml", *new gem::EquipmentDataModel),
config::ConfigError);
}