package com.failure.vanta.setup;

import org.json.JSONObject;

/** Run by tools/check.sh with a real org.json ahead of android.jar's stubs. */
public final class TestIdentity {
    static int fails = 0;
    static void ok(boolean c, String what) {
        System.out.println((c ? "ok    " : "FAIL  ") + what);
        if (!c) fails++;
    }
    public static void main(String[] a) throws Exception {
        String[][] p = {{"system_palette", "35E0FF"}, {"font", "com.android.theme.font.ibmplex"}};
        String P = Identity.PREFIX;

        JSONObject empty = new JSONObject(Identity.merge(null, p));
        ok("35E0FF".equals(empty.getString(P + "system_palette")), "null setting -> palette written");
        ok(new JSONObject(Identity.merge("", p)).length() == 2, "empty setting -> exactly our keys");

        String rom = "{\"" + P + "icon_pack\":\"com.x.icons\",\"" + P + "navbar\":\"gestural\",\"" + P + "system_palette\":\"FF0000\"}";
        JSONObject m = new JSONObject(Identity.merge(rom, p));
        ok("com.x.icons".equals(m.getString(P + "icon_pack")), "ROM icon pack survives the merge");
        ok("gestural".equals(m.getString(P + "navbar")), "ROM navbar survives the merge");
        ok("35E0FF".equals(m.getString(P + "system_palette")), "our palette replaces the old one");
        ok(m.length() == 4, "no key lost, none duplicated");

        ok(new JSONObject(Identity.merge("not json", p)).length() == 2, "garbage setting -> fresh object, no crash");

        System.out.println(fails == 0 ? "identity: ok" : "identity: FAILED " + fails);
        System.exit(fails == 0 ? 0 : 1);
    }
}
