Path: blob/master/src/packages/jupyter/kernel/test/kernel-execute-code.test.ts
1449 views
/*1There are a lot of ideas for tests in this bitrotted place:23https://github.com/sagemathinc/cocalc/tree/master/src/packages/project/jupyter/test45pnpm test `pwd`/kernel-execute-code.test.ts6*/78import { getPythonKernel, closeKernels } from "./util";910describe("a kernel implicitly spawns when you execute code", () => {11let k;12it("get and do NOT spawn a python kernel", async () => {13k = await getPythonKernel("python-spawn.ipynb");14});1516it("start some code running and see spawning is automatic", async () => {17const code = "import os; os.getpid()";18const output = k.execute_code({ code });19for await (const out of output.iter()) {20if (out.content?.code) {21expect(out.content.code).toBe(code);22}23if (out.content?.data) {24const pid = out.content?.data["text/plain"];25expect(parseInt(pid)).toEqual(k.pid());26break;27}28}29});3031it("cleans up", () => {32k.close();33});34});3536describe("test execute_code_now and chdir", () => {37let k;38it("get a python kernel", async () => {39k = await getPythonKernel("python-chdir.ipynb");40});4142it("also test the execute_code_now method", async () => {43const out = await k.execute_code_now({ code: "2+3" });44const v = out.filter((x) => x.content?.data);45expect(v[0].content.data["text/plain"]).toBe("5");46});4748it("also test the chdir method", async () => {49// before50const out = await k.execute_code_now({ code: "import os; os.curdir" });51const v = out.filter((x) => x.content?.data);52expect(v[0].content.data["text/plain"]).toBe("'.'");5354await k.chdir("/tmp");55const out2 = await k.execute_code_now({56code: "os.path.abspath(os.curdir)",57});58const v2 = out2.filter((x) => x.content?.data);59expect(v2[0].content.data["text/plain"]).toBe("'/tmp'");60});6162it("cleans up", () => {63k.close();64});65});6667afterAll(() => {68closeKernels();69});707172