Files
mg-utility/tests/BackupCodecTest.java
T
bonzi dcbd41d37b feat: automatic settings/trip backup that survives uninstall, mic and camera tests
Dev build 0.4.1.24. Also includes previously uncommitted work in the tree.
2026-11-03 18:49:53 +00:00

50 lines
3.0 KiB
Java

package cloud.molberg.mgutility;
import java.util.*;
public final class BackupCodecTest {
static void check(boolean ok, String what) { if (!ok) throw new AssertionError(what); System.out.println("ok - " + what); }
public static void main(String[] args) throws Exception {
Map<String, Map<String, Object>> files = new LinkedHashMap<String, Map<String, Object>>();
Map<String, Object> ui = new LinkedHashMap<String, Object>();
ui.put("theme", 3); ui.put("dark", true); ui.put("ts", 1730000000000L);
ui.put("scale", 1.25f); ui.put("name", "MG \"4\" \u00e6\u00f8\u00e5\nline2");
ui.put("set", new TreeSet<String>(Arrays.asList("b", "a", "c")));
files.put("ui_state", ui);
Map<String, Object> trips = new LinkedHashMap<String, Object>();
trips.put("interval", 5000);
files.put("trip_tracker", trips);
String json = BackupCodec.encode(files, 1234L, "0.3.0.9");
Map<String, Map<String, Object>> back = BackupCodec.decode(json);
check(back.equals(files), "round trip preserves every type exactly");
check(back.get("ui_state").get("theme") instanceof Integer, "int stays int");
check(back.get("ui_state").get("ts") instanceof Long, "long stays long");
check(back.get("ui_state").get("scale") instanceof Float, "float stays float");
check(back.get("ui_state").get("set") instanceof Set, "string set stays set");
check(BackupCodec.countKeys(files) == 7, "key count");
check(BackupCodec.createdAt(json) == 1234L, "created_at");
Map<String, Map<String, Object>> reordered = new LinkedHashMap<String, Map<String, Object>>();
reordered.put("trip_tracker", new LinkedHashMap<String, Object>(trips));
Map<String, Object> ui2 = new LinkedHashMap<String, Object>(); List<String> keys = new ArrayList<String>(ui.keySet());
Collections.reverse(keys); for (String k : keys) ui2.put(k, ui.get(k));
reordered.put("ui_state", ui2);
check(BackupCodec.fingerprint(files).equals(BackupCodec.fingerprint(reordered)), "fingerprint ignores ordering");
ui2.put("theme", 4);
check(!BackupCodec.fingerprint(files).equals(BackupCodec.fingerprint(reordered)), "fingerprint detects a change");
boolean rejected = false;
try { BackupCodec.decode("{\"schema\":1,\"type\":\"other\",\"files\":{}}"); } catch (IllegalArgumentException e) { rejected = true; }
check(rejected, "wrong type rejected");
rejected = false;
try { BackupCodec.decode("{\"schema\":99,\"type\":\"mg-utility-settings-backup\",\"files\":{}}"); } catch (IllegalArgumentException e) { rejected = true; }
check(rejected, "wrong schema rejected");
rejected = false;
try { BackupCodec.decode("not json"); } catch (org.json.JSONException e) { rejected = true; }
check(rejected, "corrupt file rejected");
check(BackupCodec.decode(BackupCodec.encode(new HashMap<String, Map<String, Object>>(), 1L, null)).isEmpty(), "empty backup decodes");
}
}