Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/jupyter/stateless-api/execute.test.ts
1710 views
1
import { getPythonKernelName } from "../kernel/kernel-data";
2
import jupyterExecute from "./execute";
3
import Kernel from "./kernel";
4
5
describe("test the jupyterExecute function", () => {
6
let kernel;
7
8
it(`gets a kernel name`, async () => {
9
kernel = await getPythonKernelName();
10
});
11
12
it("computes 2+3", async () => {
13
const outputs = await jupyterExecute({ kernel, input: "a=5; 2+3" });
14
expect(outputs).toEqual([{ data: { "text/plain": "5" } }]);
15
});
16
17
it("checks that its stateless, i.e., a is not defined", async () => {
18
const outputs = await jupyterExecute({ kernel, input: "print(a)" });
19
expect(JSON.stringify(outputs)).toContain("is not defined");
20
});
21
22
it("sets a via history", async () => {
23
const outputs = await jupyterExecute({
24
kernel,
25
input: "print(a**2)",
26
history: ["a=5"],
27
});
28
expect(outputs).toEqual([{ name: "stdout", text: "25\n" }]);
29
});
30
31
it("limits the output size", async () => {
32
const outputs = await jupyterExecute({
33
kernel,
34
input: "print('hi'); import sys; sys.stdout.flush(); print('x'*100)",
35
limits: { max_output_per_cell: 50 },
36
});
37
expect(outputs).toEqual([
38
{ name: "stdout", text: "hi\n" },
39
{
40
name: "stdout",
41
output_type: "stream",
42
text: [
43
"Output truncated since it exceeded the cell output limit of 50 characters",
44
],
45
},
46
]);
47
});
48
});
49
50
afterAll(async () => {
51
Kernel.closeAll();
52
});
53
54
55