<commit_before>import actions from '../constants';

export default function (state = {}, action) {
  switch (action.type) {
    case actions.FETCH_SUBMISSION_SUCCESS:
    case actions.SAVE_DRAFT_SUCCESS:
    case actions.FINALISE_SUCCESS:
    case actions.UNSUBMIT_SUCCESS:
    case actions.SAVE_GRADE_SUCCESS:
    case actions.MARK_SUCCESS:
    case actions.UNMARK_SUCCESS:
    case actions.PUBLISH_SUCCESS:
      return {
        ...state,
        ...action.payload.answers.reduce(
          (obj, answer) => ({ ...obj, [answer.questionId]: answer.testCases }),
          {},
        ),
      };
    case actions.AUTOGRADE_SUCCESS:
    case actions.RESET_SUCCESS: {
      const { questionId } = action.payload;
      return Object.keys(state).reduce(
        (obj, key) => {
          if (key !== questionId.toString()) {
            return { ...obj, [key]: state[key] };
          }
          return obj;
        },
        { [questionId]: action.payload.testCases },
      );
    }
    case actions.AUTOGRADE_FAILURE: {
      // Clear the previous test results in the test case results display.
      const { questionId } = action;

      const questionState = {};
      // For each test case in each test type, add back the data without the output
      // and passed values.
      Object.keys(state[questionId]).forEach((testType) => {
        if (testType !== 'stdout' && testType !== 'stderr') {
          questionState[testType] = state[questionId][testType].map(
            (testCase) => ({
              identifier: testCase.identifier,
              expression: testCase.expression,
              expected: testCase.expected,
            }),
          );
        }
      });

      return {
        ...state,
        [questionId]: questionState,
      };
    }<commit_msg>fix(testCases reducer): fix redux state not updated properly upon autograde failure<commit_after>import actions from '../constants';

export default function (state = {}, action) {
  switch (action.type) {
    case actions.FETCH_SUBMISSION_SUCCESS:
    case actions.SAVE_DRAFT_SUCCESS:
    case actions.FINALISE_SUCCESS:
    case actions.UNSUBMIT_SUCCESS:
    case actions.SAVE_GRADE_SUCCESS:
    case actions.MARK_SUCCESS:
    case actions.UNMARK_SUCCESS:
    case actions.PUBLISH_SUCCESS:
      return {
        ...state,
        ...action.payload.answers.reduce(
          (obj, answer) => ({ ...obj, [answer.questionId]: answer.testCases }),
          {},
        ),
      };
    case actions.AUTOGRADE_SUCCESS:
    case actions.RESET_SUCCESS: {
      const { questionId } = action.payload;
      return Object.keys(state).reduce(
        (obj, key) => {
          if (key !== questionId.toString()) {
            return { ...obj, [key]: state[key] };
          }
          return obj;
        },
        { [questionId]: action.payload.testCases },
      );
    }
    case actions.AUTOGRADE_FAILURE: {
      // Clear the previous test results in the test case results display.
      const { questionId } = action;

      const questionState = {};
      // For each test case in each test type, add back the data without the output
      // and passed values.
      Object.keys(state[questionId]).forEach((testType) => {
        if (
          testType !== 'stdout' &&
          testType !== 'stderr' &&
          testType !== 'canReadTests'
        ) {
          questionState[testType] = state[questionId][testType].map(
            (testCase) => ({
              identifier: testCase.identifier,
              expression: testCase.expression,
              expected: testCase.expected,
            }),
          );
        }
      });

      return {
        ...state,
        [questionId]: questionState,
      };
    }
{'fraction_non_alphanumeric': 0.12287793047696038, 'fraction_numerical': 0.0, 'mean_word_length': 2.020341741253051, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3437108', 'n_tokens_mistral': 1038, 'n_tokens_neox': 1028, 'n_words': 236}