57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { Capsule } from "../src/capsule.js";
|
|
import { CodeInterpreter } from "../src/code-interpreter/index.js";
|
|
|
|
describe("CodeInterpreter", () => {
|
|
it("creates a capsule with the jupyter template by default", async () => {
|
|
const create = vi.spyOn(Capsule, "create").mockResolvedValue(
|
|
new Capsule("cap_1", {
|
|
baseUrl: "https://api.example.com",
|
|
}),
|
|
);
|
|
|
|
const interpreter = await CodeInterpreter.create({
|
|
baseUrl: "https://api.example.com",
|
|
});
|
|
|
|
expect(interpreter.capsule.id).toBe("cap_1");
|
|
expect(create).toHaveBeenCalledWith("jupyter", {
|
|
baseUrl: "https://api.example.com",
|
|
});
|
|
create.mockRestore();
|
|
});
|
|
|
|
it("connects to an existing capsule", () => {
|
|
const interpreter = CodeInterpreter.connect("cap_1", {
|
|
baseUrl: "https://api.example.com",
|
|
});
|
|
|
|
expect(interpreter.capsule.id).toBe("cap_1");
|
|
});
|
|
|
|
it("executes notebook cells through python3", async () => {
|
|
const capsule = new Capsule("cap_1", {
|
|
baseUrl: "https://api.example.com",
|
|
});
|
|
const exec = vi.spyOn(capsule.commands, "exec").mockResolvedValue({
|
|
exit_code: 0,
|
|
stderr: "",
|
|
stdout: "42\n",
|
|
});
|
|
const interpreter = new CodeInterpreter(capsule);
|
|
|
|
await expect(
|
|
interpreter.notebook.execCell("print(6 * 7)"),
|
|
).resolves.toEqual({
|
|
exitCode: 0,
|
|
stderr: "",
|
|
stdout: "42\n",
|
|
});
|
|
expect(exec).toHaveBeenCalledWith("python3", {
|
|
args: ["-c", "print(6 * 7)"],
|
|
timeoutSec: 30,
|
|
});
|
|
});
|
|
});
|