<commit_before>package org.commcare.recovery.measures;

import android.content.Intent;

import org.commcare.CommCareApplication;
import org.commcare.CommCareTestApplication;
import org.commcare.activities.DispatchActivity;
import org.commcare.activities.LoginActivity;
import org.commcare.activities.PromptApkUpdateActivity;
import org.commcare.activities.PromptCCReinstallActivity;
import org.commcare.activities.StandardHomeActivity;
import org.commcare.android.CommCareTestRunner;
import org.commcare.android.util.TestAppInstaller;
import org.commcare.models.database.SqlStorage;
import org.commcare.preferences.HiddenPreferences;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.util.ActivityController;

import java.util.List;

import static junit.framework.Assert.assertTrue;

@Config(application = CommCareTestApplication.class)
@RunWith(CommCareTestRunner.class)
public class RecoveryMeasuresTest {

    private static final String REINSTALL_AND_UPDATE_VALID_FOR_CURRENT_APP_VERSION =  "{\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":1, \"type\":\"app_reinstall_and_update\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.90.0\", \"app_version_min\":0, \"latest_app_version\":9," +
            "\"latest_cc_version\":\"2.91.0\", \"app_version_max\":6} ]}";

    private static final String REINSTALL_AND_UPDATE_NOT_VALID_FOR_CURRENT_APP_VERSION =  "{\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":2, \"type\":\"app_reinstall_and_update\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.90.0\", \"app_version_min\":0, \"latest_app_version\":9," +
            "\"latest_cc_version\":\"2.91.0\", \"app_version_max\":5} ]}";

    private static final String CC_REINSTALL_INVALID_FOR_CURRENT_CC_VERSION =  "{\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":3, \"type\":\"cc_reinstall\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.44.0\", \"app_version_min\":0, \"latest_app_version\":9," +
            "\"latest_cc_version\":\"2.91.0\", \"app_version_max\":6} ]}";

    private static final String CC_REINSTALL_VALID_FOR_ALL_CC_VERSION =  "{\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":4, \"type\":\"cc_reinstall\", \"app_version_min\":0, \"latest_app_version\":9," +
            "\"latest_cc_version\":\"2.91.0\", \"app_version_max\":6} ]}";

    private static final String APP_UPDATE_VALID_FOR_ALL_APP_VERSION =  "{\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":5, \"type\":\"app_update\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.90.0\", \"latest_app_version\":9," +
            "\"latest_cc_version\":\"2.91.0\"} ]}";

    private static final String VALID_FOR_ALL_HENCE_INVALID =  "{\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":6, \"type\":\"app_update\", \"latest_app_version\":9," +
            "\"latest_cc_version\":\"2.91.0\"} ]}";

    private static final String CC_UPDATE_INVALID_DUE_TO_LATEST_VERSION =  "{\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":7, \"type\":\"cc_update\", \"app_version_min\":0, \"latest_app_version\":9," +
            "\"latest_cc_version\":\"2.44.0\", \"app_version_max\":6} ]}";

    private static final String APP_UPDATE_INVALID_DUE_TO_LATEST_VERSION =  "{\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":8, \"type\":\"app_update\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.90.0\", \"latest_app_version\":6," +
            "\"latest_cc_version\":\"2.91.0\"} ]}";

    private static final String CC_UPDATE_VALID =  "{\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":9, \"type\":\"cc_update\", \"app_version_min\":0, \"latest_app_version\":6," +
            "\"latest_cc_version\":\"2.91.0\", \"app_version_max\":6} ]}";


    @Before
    public void setup() {
        // The app version for this app is 95
        TestAppInstaller.installAppAndLogin(
                "jr://resource/commcare-apps/update_tests/base_app/profile.ccpr",
                "test",
                "123");
    }

    @Test
    public void OnlyValidRecoveryMeasures_ShouldGetAdded() {
        requestRecoveryMeasures(REINSTALL_AND_UPDATE_VALID_FOR_CURRENT_APP_VERSION);
        requestRecoveryMeasures(REINSTALL_AND_UPDATE_NOT_VALID_FOR_CURRENT_APP_VERSION);
        requestRecoveryMeasures(CC_REINSTALL_INVALID_FOR_CURRENT_CC_VERSION);
        requestRecoveryMeasures(CC_REINSTALL_VALID_FOR_ALL_CC_VERSION);
        requestRecoveryMeasures(APP_UPDATE_VALID_FOR_ALL_APP_VERSION);
        requestRecoveryMeasures(VALID_FOR_ALL_HENCE_INVALID);
        requestRecoveryMeasures(CC_UPDATE_INVALID_DUE_TO_LATEST_VERSION);
        requestRecoveryMeasures(APP_UPDATE_INVALID_DUE_TO_LATEST_VERSION);
        requestRecoveryMeasures(CC_UPDATE_VALID);

        // Verify that only the valid measures has been registered
        SqlStorage<RecoveryMeasure> recoveryMeasureSqlStorage = CommCareApplication.instance().getAppStorage(RecoveryMeasure.class);
        List<RecoveryMeasure> recoveryMeasures = RecoveryMeasuresHelper.getPendingRecoveryMeasuresInOrder(recoveryMeasureSqlStorage);
        assertTrue(recoveryMeasures.size() == 4);
        assertTrue(recoveryMeasures.get(0).getSequenceNumber() == 1);
        assertTrue(recoveryMeasures.get(1).getSequenceNumber() == 4);
        assertTrue(recoveryMeasures.get(2).getSequenceNumber() == 5);
        assertTrue(recoveryMeasures.get(3).getSequenceNumber() == 9);
    }

    @Test
    public void RecoveryMeasuresBeforeLastSuccesfulSequence_ShouldGetDiscarded() {
        HiddenPreferences.setLatestRecoveryMeasureExecuted(5);
        requestRecoveryMeasures(REINSTALL_AND_UPDATE_VALID_FOR_CURRENT_APP_VERSION);
        requestRecoveryMeasures(CC_REINSTALL_VALID_FOR_ALL_CC_VERSION);
        requestRecoveryMeasures(APP_UPDATE_VALID_FOR_ALL_APP_VERSION);
        requestRecoveryMeasures(CC_UPDATE_VALID);

        // Verify that only the valid measures has been registered
        SqlStorage<RecoveryMeasure> recoveryMeasureSqlStorage = CommCareApplication.instance().getAppStorage(RecoveryMeasure.class);<commit_msg>Corrects test json<commit_after>package org.commcare.recovery.measures;

import android.content.Intent;

import org.commcare.CommCareApplication;
import org.commcare.CommCareTestApplication;
import org.commcare.activities.DispatchActivity;
import org.commcare.activities.LoginActivity;
import org.commcare.activities.PromptApkUpdateActivity;
import org.commcare.activities.PromptCCReinstallActivity;
import org.commcare.activities.StandardHomeActivity;
import org.commcare.android.CommCareTestRunner;
import org.commcare.android.util.TestAppInstaller;
import org.commcare.models.database.SqlStorage;
import org.commcare.preferences.HiddenPreferences;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.util.ActivityController;

import java.util.List;

import static junit.framework.Assert.assertTrue;

@Config(application = CommCareTestApplication.class)
@RunWith(CommCareTestRunner.class)
public class RecoveryMeasuresTest {

    private static final String REINSTALL_AND_UPDATE_VALID_FOR_CURRENT_APP_VERSION =  "{ \"latest_ccz_version\":9, \"latest_apk_version\":\"2.91.0\"," +
            "\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\", \"recovery_measures\": " +
            "[{\"sequence_number\":1, \"type\":\"app_reinstall_and_update\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.90.0\", \"app_version_min\":0," +
            "\"app_version_max\":6} ]}";

    private static final String REINSTALL_AND_UPDATE_INVALID_FOR_CURRENT_APP_VERSION =  "{\"latest_ccz_version\":9, \"latest_apk_version\":\"2.91.0\"," +
            "\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":2, \"type\":\"app_reinstall_and_update\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.90.0\", \"app_version_min\":0, \"app_version_max\":5} ]}";

    private static final String CC_REINSTALL_INVALID_FOR_CURRENT_CC_VERSION =  "{\"latest_ccz_version\":9, \"latest_apk_version\":\"2.91.0\"," +
            "\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":3, \"type\":\"cc_reinstall\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.44.0\", \"app_version_min\":0, \"app_version_max\":6} ]}";

    private static final String CC_REINSTALL_VALID_FOR_ALL_CC_VERSION =  "{\"latest_ccz_version\":9, \"latest_apk_version\":\"2.91.0\"," +
            "\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":4, \"type\":\"cc_reinstall\", \"app_version_min\":0, \"app_version_max\":6} ]}";

    private static final String APP_UPDATE_VALID_FOR_ALL_APP_VERSION =  "{\"latest_ccz_version\":9, \"latest_apk_version\":\"2.91.0\"," +
            "\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":5, \"type\":\"app_update\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.90.0\" }]}";

    private static final String VALID_FOR_ALL_HENCE_INVALID =  "{\"latest_ccz_version\":9, \"latest_apk_version\":\"2.91.0\"," +
            "\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":6, \"type\":\"app_update\" }]}";

    private static final String CC_UPDATE_INVALID_DUE_TO_LATEST_VERSION =  "{\"latest_ccz_version\":9, \"latest_apk_version\":\"2.44.0\"," +
            "\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":7, \"type\":\"cc_update\", \"app_version_min\":0, \"app_version_max\":6} ]}";

    private static final String APP_UPDATE_INVALID_DUE_TO_LATEST_VERSION =  "{\"latest_ccz_version\":6, \"latest_apk_version\":\"2.91.0\"," +
            "\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":8, \"type\":\"app_update\", \"cc_version_min\":\"2.44.0\", " +
            "\"cc_version_max\":\"2.90.0\" }]}";

    private static final String CC_UPDATE_VALID =  "{\"latest_ccz_version\":9, \"latest_apk_version\":\"2.91.0\"," +
            "\"app_id\":\"ac46998a182d2e1d1fd8e75684d23903\",\"recovery_measures\": " +
            "[{\"sequence_number\":9, \"type\":\"cc_update\", \"app_version_min\":0, \"app_version_max\":6} ]}";


    @Before
    public void setup() {
        // The app version for this app is 95
        TestAppInstaller.installAppAndLogin(
                "jr://resource/commcare-apps/update_tests/base_app/profile.ccpr",
                "test",
                "123");
    }

    @Test
    public void OnlyValidRecoveryMeasures_ShouldGetAdded() {
        requestRecoveryMeasures(REINSTALL_AND_UPDATE_VALID_FOR_CURRENT_APP_VERSION);
        requestRecoveryMeasures(REINSTALL_AND_UPDATE_INVALID_FOR_CURRENT_APP_VERSION);
        requestRecoveryMeasures(CC_REINSTALL_INVALID_FOR_CURRENT_CC_VERSION);
        requestRecoveryMeasures(CC_REINSTALL_VALID_FOR_ALL_CC_VERSION);
        requestRecoveryMeasures(APP_UPDATE_VALID_FOR_ALL_APP_VERSION);
        requestRecoveryMeasures(VALID_FOR_ALL_HENCE_INVALID);
        requestRecoveryMeasures(CC_UPDATE_INVALID_DUE_TO_LATEST_VERSION);
        requestRecoveryMeasures(APP_UPDATE_INVALID_DUE_TO_LATEST_VERSION);
        requestRecoveryMeasures(CC_UPDATE_VALID);

        // Verify that only the valid measures has been registered
        SqlStorage<RecoveryMeasure> recoveryMeasureSqlStorage = CommCareApplication.instance().getAppStorage(RecoveryMeasure.class);
        List<RecoveryMeasure> recoveryMeasures = RecoveryMeasuresHelper.getPendingRecoveryMeasuresInOrder(recoveryMeasureSqlStorage);
        assertTrue(recoveryMeasures.size() == 4);
        assertTrue(recoveryMeasures.get(0).getSequenceNumber() == 1);
        assertTrue(recoveryMeasures.get(1).getSequenceNumber() == 4);
        assertTrue(recoveryMeasures.get(2).getSequenceNumber() == 5);
        assertTrue(recoveryMeasures.get(3).getSequenceNumber() == 9);
    }

    @Test
    public void RecoveryMeasuresBeforeLastSuccesfulSequence_ShouldGetDiscarded() {
        HiddenPreferences.setLatestRecoveryMeasureExecuted(5);
        requestRecoveryMeasures(REINSTALL_AND_UPDATE_VALID_FOR_CURRENT_APP_VERSION);
        requestRecoveryMeasures(CC_REINSTALL_VALID_FOR_ALL_CC_VERSION);
        requestRecoveryMeasures(APP_UPDATE_VALID_FOR_ALL_APP_VERSION);
        requestRecoveryMeasures(CC_UPDATE_VALID);

        // Verify that only the valid measures has been registered
        SqlStorage<RecoveryMeasure> recoveryMeasureSqlStorage = CommCareApplication.instance().getAppStorage(RecoveryMeasure.class);
{'fraction_non_alphanumeric': 0.1539647577092511, 'fraction_numerical': 0.04684287812041116, 'mean_word_length': 4.19488939740656, 'pattern_counts': {'":': 152, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5398275', 'n_tokens_mistral': 4818, 'n_tokens_neox': 4458, 'n_words': 482}