<commit_before>import * as actions from "@nteract/actions";
import { createExecuteRequest, createMessage } from "@nteract/messaging";
import * as stateModule from "@nteract/types";
import { mockAppState } from "@nteract/fixtures";
import { ActionsObservable, StateObservable } from "redux-observable";
import { empty, from, Subject } from "rxjs";
import { catchError, share, toArray } from "rxjs/operators";

import {
  createExecuteCellStream,
  executeCellAfterKernelLaunchEpic,
  executeCellEpic,
  executeCellStream,
  lazyLaunchKernelEpic,
  sendExecuteRequestEpic,
  updateDisplayEpic,
  sendInputReplyEpic,
  executeAllCellsEpic
} from "../src/execute";
import { Action } from "rxjs/internal/scheduler/Action";

const Immutable = require("immutable");

describe("executeCell", () => {
  test("returns an executeCell action", () => {
    expect(
      actions.executeCell({ id: "0-0-0-0", contentRef: "fakeContentRef" })
    ).toEqual({
      type: actions.EXECUTE_CELL,
      payload: { id: "0-0-0-0", contentRef: "fakeContentRef" }
    });
  });
});

describe("executeCellStream", () => {
  test("outright rejects a lack of channels.shell and iopub", done => {
    const obs = executeCellStream(
      null,
      "0",
      createMessage("execute_request"),
      "fakeContentRef"
    );
    obs.subscribe(null, err => {
      expect(err.message).toEqual("kernel not connected");
      done();
    });
  });
});

describe("createExecuteCellStream", () => {
  test("errors if the kernel is not connected in create", done => {
    const frontendToShell = new Subject();
    const shellToFrontend = new Subject();
    const mockShell = Subject.create(frontendToShell, shellToFrontend);
    const channels = mockShell;
    const state$ = {
      value: {
        core: stateModule.makeStateRecord({
          kernelRef: "fake",
          entities: stateModule.makeEntitiesRecord({
            kernels: stateModule.makeKernelsRecord({
              byRef: Immutable.Map({
                fake: stateModule.makeRemoteKernelRecord({
                  channels,
                  status: "not connected"
                })
              })
            })
          })
        }),
        app: {
          notificationSystem: { addNotification: jest.fn() }
        }
      }
    };
    const action$ = ActionsObservable.of(
      actions.sendExecuteRequest({<commit_msg>Add tests for execution metadata setting<commit_after>import * as actions from "@nteract/actions";
import { mockAppState } from "@nteract/fixtures";
import { createExecuteRequest, createMessage } from "@nteract/messaging";
import * as stateModule from "@nteract/types";
import { ActionsObservable, StateObservable } from "redux-observable";
import { empty, from, of, Subject } from "rxjs";
import { catchError, share, toArray } from "rxjs/operators";

import {
  createExecuteCellStream,
  executeAllCellsEpic,
  executeCellAfterKernelLaunchEpic,
  executeCellEpic,
  executeCellStream,
  lazyLaunchKernelEpic,
  sendExecuteRequestEpic,
  sendInputReplyEpic,
  updateDisplayEpic
} from "../src/execute";

const Immutable = require("immutable");

describe("executeCell", () => {
  test("returns an executeCell action", () => {
    expect(
      actions.executeCell({ id: "0-0-0-0", contentRef: "fakeContentRef" })
    ).toEqual({
      type: actions.EXECUTE_CELL,
      payload: { id: "0-0-0-0", contentRef: "fakeContentRef" }
    });
  });
});

describe("executeCellStream", () => {
  test("outright rejects a lack of channels.shell and iopub", done => {
    const obs = executeCellStream(
      null,
      "0",
      createMessage("execute_request"),
      "fakeContentRef"
    );
    obs.subscribe(null, err => {
      expect(err.message).toEqual("kernel not connected");
      done();
    });
  });
  test("dispatches actions for updating execution metadata", done => {
    const message = createMessage("execute_request");
    const msg_id = message.header.msg_id;
    const kernelMsgs = [
      {
        parent_header: {
          msg_id
        },
        header: {
          msg_type: "execute_input"
        },
        content: {
          execution_count: 0
        }
      },
      {
        parent_header: {
          msg_id
        },
        header: {
          msg_type: "status"
        },
        content: {
          execution_state: "busy"
        }
      },
      {
        parent_header: {
          msg_id
        },
        header: {
          msg_type: "status"
        },
        content: {
          execution_state: "idle"
        }
      },
      {
        parent_header: {
          msg_id
        },
        header: {
          msg_type: "execute_reply"
        },
        content: {
          execution_count: 0
        }
      }
    ];
    const sent = new Subject();
    const received = new Subject();

    const channels = Subject.create(sent, received);

    sent.subscribe(() => {
      kernelMsgs.map(msg => received.next(msg));
    });

    const obs = executeCellStream(channels, "0", message, "fakeContentRef");

    const emittedActions = [];
    obs.subscribe(action => {
      emittedActions.push(action);
    });

    expect(emittedActions).toContainEqual(
      expect.objectContaining(
        actions.setInCell({
          id: "0",
          contentRef: "fakeContentRef",
          path: ["metadata", "execution", "iopub", "execute_input"],
          value: expect.any(String)
        })
      )
    );

    expect(emittedActions).toContainEqual(
      expect.objectContaining(
        actions.setInCell({
          id: "0",
          contentRef: "fakeContentRef",
          path: ["metadata", "execution", "shell", "execute_reply"],
          value: expect.any(String)
        })
      )
    );

    expect(emittedActions).toContainEqual(
      expect.objectContaining(
        actions.setInCell({
          id: "0",
          contentRef: "fakeContentRef",
          path: ["metadata", "execution", "iopub", "status", "idle"],
          value: expect.any(String)
        })
      )
    );

    expect(emittedActions).toContainEqual(
      expect.objectContaining(
        actions.setInCell({
          id: "0",
          contentRef: "fakeContentRef",
          path: ["metadata", "execution", "iopub", "status", "busy"],
          value: expect.any(String)
        })
      )
    );

    done();
  });
});

describe("createExecuteCellStream", () => {
  test("errors if the kernel is not connected in create", done => {
    const frontendToShell = new Subject();
    const shellToFrontend = new Subject();
    const mockShell = Subject.create(frontendToShell, shellToFrontend);
    const channels = mockShell;
    const state$ = {
      value: {
        core: stateModule.makeStateRecord({
          kernelRef: "fake",
          entities: stateModule.makeEntitiesRecord({
            kernels: stateModule.makeKernelsRecord({
              byRef: Immutable.Map({
                fake: stateModule.makeRemoteKernelRecord({
                  channels,
                  status: "not connected"
                })
              })
            })
          })
        }),
        app: {
          notificationSystem: { addNotification: jest.fn() }
        }
      }
    };
    const action$ = ActionsObservable.of(
      actions.sendExecuteRequest({
{'fraction_non_alphanumeric': 0.12763320941759604, 'fraction_numerical': 0.003442103813851026, 'mean_word_length': 2.3520996769727733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '513536', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2072, 'n_words': 432}