Path: blob/master/src/packages/jupyter/kernel/test/kernel.test.ts
1450 views
/*1I'm a little hesistant about testing this since we'll need to make sure that a kernel is2installed, e.g., to test on Github actions.3Probably, the way to go would be to install https://www.npmjs.com/package/ijavascript4and just test that a lot, since it would be the minimal dependency.56There are a lot of ideas for tests in this bitrotted place:78https://github.com/sagemathinc/cocalc/tree/master/src/packages/project/jupyter/test9*/1011import { getPythonKernel, closeKernels } from "./util";12import { kernel } from "../kernel";1314describe("test trying to use a kernel that doesn't exist", () => {15it("fails to start", async () => {16const k = kernel({ name: "no-such-kernel", path: "none.ipynb" });17const errors: any[] = [];18k.on("kernel_error", (err) => {19errors.push(err);20});2122await expect(23async () => await k.execute_code_now({ code: "2+3" }),24).rejects.toThrow("No spec available for kernel");2526expect(errors[0]).toContain("No spec available for kernel");27});28});2930describe("create and close python kernel", () => {31let k;32it("get a python kernel", async () => {33k = await getPythonKernel("python-0.ipynb");34});3536it("cleans up", () => {37k.close();38});39});4041describe("spawn and close python kernel", () => {42let k;43it("get a python kernel", async () => {44k = await getPythonKernel("python-1.ipynb");45});4647it("spawns the kernel", async () => {48await k.spawn();49});5051it("cleans up", () => {52k.close();53});54});5556describe("compute 2+3 using a python kernel", () => {57let k;58it("get a python kernel", async () => {59k = await getPythonKernel("python-2.ipynb");60});6162it("spawn the kernel", async () => {63await k.spawn();64});6566it("evaluate 2+3, confirming the result", async () => {67const output = k.execute_code({ code: "2+3" });68const iter = output.iter();69const v: any[] = [];70for await (const x of iter) {71v.push(x);72}73expect(v[0].content).toEqual({ execution_state: "busy" });74expect(v[1].content).toEqual({ code: "2+3", execution_count: 1 });75expect(v[2].content.data).toEqual({ "text/plain": "5" });76});7778it("define a variable in one call, then use it in another", async () => {79const output = k.execute_code({ code: "a=5" });80await output.waitUntilDone();81output.close();82});8384it("uses that variable in another call", async () => {85const output = k.execute_code({ code: "a + a" });86const iter = output.iter();87await output.waitUntilDone();88for await (const x of iter) {89if (x.content?.data) {90expect(x.content?.data).toEqual({ "text/plain": "10" });91break;92}93}94});9596it("cleans up", () => {97k.close();98});99});100101describe("start computation then immediately close the kernel should not crash", () => {102let k;103it("get and spawn a python kernel", async () => {104k = await getPythonKernel("python-4.ipynb");105await k.spawn();106});107108it("start something running, then immediately close and see error event is called", async () => {109const output = k.execute_code({ code: "sleep 10000" });110for await (const _ of output.iter()) {111// it's ack'd as running:112break;113}114});115116it("closes during computation", () => {117k.close();118});119120it("starts and closes another kernel with the same path", async () => {121const k2 = await getPythonKernel("python-4.ipynb", true);122k2.close();123});124});125126afterAll(() => {127closeKernels();128});129130131