Files
mg-utility/tests/VehicleSettingsCoordinatorTest.java

103 lines
8.3 KiB
Java

import cloud.molberg.mgutility.VehicleSettingsApplyCoordinator;
import cloud.molberg.mgutility.VehicleSettingsCatalog;
import cloud.molberg.mgutility.VehicleSettingsProfile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class VehicleSettingsCoordinatorTest {
static final class Store implements VehicleSettingsApplyCoordinator.Store {
final Map<String,Integer> values = new HashMap<String,Integer>();
final Map<String,VehicleSettingsCatalog.Policy> policies = new HashMap<String,VehicleSettingsCatalog.Policy>();
final Map<String,String> results = new HashMap<String,String>();
Integer previous; String ack;
public Integer desiredValue(String id) { return values.get(id); }
public VehicleSettingsCatalog.Policy policy(String id) { return policies.containsKey(id) ? policies.get(id) : VehicleSettingsCatalog.Policy.INHERIT; }
public boolean levelFiveAcknowledged(String profile, int version) { return (profile + ":" + version).equals(ack); }
public void savePreviousRegen(int value) { previous = value; }
public Integer previousRegen() { return previous; }
public void recordObserved(String id, int value) { }
public void recordResult(String id, String result, long at) { results.put(id, result); }
}
static final class Car implements VehicleSettingsApplyCoordinator.Car {
final Map<String,Integer> current = new HashMap<String,Integer>();
int writes; boolean failDrive; boolean applyQueued; int ignition = 1; int gear = 1; float speed = 0f;
public int get(VehicleSettingsCatalog.Definition d) { Integer value = current.get(d.id); return value == null ? -1 : value.intValue(); }
public boolean set(VehicleSettingsCatalog.Definition d, int value) { writes++; if (failDrive && "drive_mode".equals(d.id)) return false; if (applyQueued) current.put(d.id, value); return true; }
public boolean available() { return true; }
public int getIgnition() { return ignition; }
public int getGear() { return gear; }
public float getSpeed() { return speed; }
public int getRegenerativeLevel() { return current.containsKey("regenerative_level") ? current.get("regenerative_level") : -1; }
public boolean setRegenerativeLevel(int value) { writes++; if (applyQueued) current.put("regenerative_level", value); return true; }
}
static void check(boolean condition, String message) { if (!condition) throw new AssertionError(message); }
static void restore(Store s, String id, int value) { s.values.put(id, value); s.policies.put(id, VehicleSettingsCatalog.Policy.RESTORE); }
public static void main(String[] args) {
Store fresh = new Store(); Car car = new Car(); car.applyQueued = true;
car.current.put("drive_mode", 3); car.current.put("energy_saving", 0);
VehicleSettingsApplyCoordinator empty = new VehicleSettingsApplyCoordinator(fresh, car, VehicleSettingsProfile.known());
check(empty.applyStartup("s1").isEmpty() && car.writes == 0, "fresh install has no writes");
restore(fresh, "drive_mode", 4); restore(fresh, "energy_saving", 1);
List<VehicleSettingsApplyCoordinator.Result> results = empty.applyStartup("s2");
check(car.writes == 2 && results.size() == 2, "independent settings apply");
check(empty.applyStartup("s2").isEmpty() && car.writes == 2, "session deduplication");
car.current.put("drive_mode", 3);
car.current.put("energy_saving", 0);
List<VehicleSettingsApplyCoordinator.Result> manual = empty.applyStartupNow();
check(manual.size() == 2 && car.writes == 4, "manual startup reapply bypasses session deduplication");
check(car.current.get("drive_mode") == 4 && car.current.get("energy_saving") == 1,
"manual startup reapply restores configured values");
Car queued = new Car(); queued.current.put("drive_mode", 3); queued.applyQueued = false;
VehicleSettingsApplyCoordinator queuedCoordinator = new VehicleSettingsApplyCoordinator(fresh, queued, VehicleSettingsProfile.known());
List<VehicleSettingsApplyCoordinator.Result> queuedResults = queuedCoordinator.applyStartup("queued");
check(queuedResults.get(0).status == VehicleSettingsApplyCoordinator.Status.QUEUED_UNVERIFIED,
"one-way queue is not verified without read-back");
Car partial = new Car(); partial.current.put("drive_mode", 3); partial.current.put("energy_saving", 0);
partial.applyQueued = true; partial.failDrive = true;
VehicleSettingsApplyCoordinator partialCoordinator = new VehicleSettingsApplyCoordinator(fresh, partial, VehicleSettingsProfile.known());
List<VehicleSettingsApplyCoordinator.Result> partialResults = partialCoordinator.applyStartup("partial");
check(partialResults.size() == 2 && partial.writes == 2, "failed setting does not block independent setting");
Store movingStore = new Store();
restore(movingStore, "drive_mode", 4); restore(movingStore, "regenerative_level", 2);
Car moving = new Car(); moving.applyQueued = true; moving.speed = 50f; moving.gear = 4;
moving.current.put("drive_mode", 3); moving.current.put("regenerative_level", 1);
VehicleSettingsApplyCoordinator movingCoordinator = new VehicleSettingsApplyCoordinator(movingStore, moving, VehicleSettingsProfile.known());
List<VehicleSettingsApplyCoordinator.Result> movingResults = movingCoordinator.applyStartup("moving-check");
check(movingResults.size() == 2 && moving.writes == 2, "startup writes while in gear and moving");
for (VehicleSettingsApplyCoordinator.Result result : movingResults)
check(result.status == VehicleSettingsApplyCoordinator.Status.VERIFIED, "moving startup read-back matches");
check(movingCoordinator.applyStartup("moving-check").isEmpty() && moving.writes == 2,
"moving startup still deduplicates the ignition session");
moving.current.put("drive_mode", 3); moving.current.put("regenerative_level", 1);
List<VehicleSettingsApplyCoordinator.Result> movingManual = movingCoordinator.applyStartupNow();
check(movingManual.size() == 2 && moving.writes == 4, "manual startup writes while in gear and moving");
for (VehicleSettingsApplyCoordinator.Result result : movingManual)
check(result.status == VehicleSettingsApplyCoordinator.Status.VERIFIED, "moving manual read-back matches");
moving.gear = -1; moving.speed = Float.NaN;
moving.current.put("drive_mode", 3); moving.current.put("regenerative_level", 1);
check(movingCoordinator.applyStartup("missing-motion").size() == 2 && moving.writes == 6,
"startup does not depend on gear or speed telemetry");
Car unknownState = new Car(); unknownState.ignition = -1;
VehicleSettingsApplyCoordinator unknownStateCoordinator = new VehicleSettingsApplyCoordinator(fresh, unknownState, VehicleSettingsProfile.known());
check(unknownStateCoordinator.applyStartup("unknown-state").size() == 2 && unknownState.writes == 0, "unknown ignition produces no writes");
check(unknownStateCoordinator.applyStartupNow().size() == 2 && unknownState.writes == 0,
"manual startup still requires ignition readiness");
Car experimental = new Car(); experimental.applyQueued = true; experimental.current.put("regenerative_level", 2);
Store experimentalStore = new Store(); experimentalStore.ack = VehicleSettingsProfile.MG4_LUXURY_2022 + ":1";
VehicleSettingsApplyCoordinator levelFive = new VehicleSettingsApplyCoordinator(experimentalStore, experimental, VehicleSettingsProfile.known());
VehicleSettingsApplyCoordinator.Result five = levelFive.applyExperimentalRegenOff(VehicleSettingsProfile.MG4_LUXURY_2022, 1);
check(five.status == VehicleSettingsApplyCoordinator.Status.VERIFIED && experimentalStore.previous == 2, "level 5 acknowledgement/capture/readback");
Car unknownCar = new Car(); Store unknownStore = new Store(); restore(unknownStore, "drive_mode", 4);
VehicleSettingsApplyCoordinator unknown = new VehicleSettingsApplyCoordinator(unknownStore, unknownCar, VehicleSettingsProfile.unknown());
check(unknown.applyStartup("unknown").isEmpty() && unknownCar.writes == 0, "unknown firmware has zero writes");
System.out.println("VehicleSettingsCoordinatorTest: OK");
}
}