CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/guesslang.test.ts
Views: 687
1
import { createMocks } from "lib/api/test-framework";
2
import handler from "./guesslang";
3
4
describe("/api/v2/guesslang", () => {
5
test("guess language of some code", async () => {
6
const { req, res } = createMocks({
7
method: "POST",
8
url: "/api/v2/guesslang",
9
body: {
10
code: "for i in range(10):\n print(i**2)",
11
cutoff: 3,
12
},
13
});
14
15
await handler(req, res);
16
expect(res.statusCode).toBe(200);
17
const data = res._getJSONData();
18
expect(data.result.length).toBe(3);
19
expect(data.result[0]).toEqual("py");
20
});
21
22
test("default number of responses is 5", async () => {
23
const { req, res } = createMocks({
24
method: "POST",
25
url: "/api/v2/guesslang",
26
body: { code: "for i in range(10):\n print(i**2)" },
27
});
28
29
await handler(req, res);
30
31
expect(res.statusCode).toBe(200);
32
const data = res._getJSONData();
33
expect(data.result.length).toBe(5);
34
});
35
36
test("error if code not given", async () => {
37
const { req, res } = createMocks({
38
method: "POST",
39
url: "/api/v2/guesslang",
40
body: {},
41
});
42
43
await handler(req, res);
44
expect(res.statusCode).toBe(400);
45
});
46
47
test("error if code is not a string", async () => {
48
const { req, res } = createMocks({
49
method: "POST",
50
url: "/api/v2/guesslang",
51
body: { code: 10 },
52
});
53
54
await handler(req, res);
55
expect(res.statusCode).toBe(400);
56
});
57
58
test("no error if extra param", async () => {
59
const cutoff = 7;
60
const { req, res } = createMocks({
61
method: "POST",
62
url: "/api/v2/guesslang",
63
body: {
64
code: "for i in range(10):\n print(i**2)",
65
foo: "bar",
66
cutoff,
67
},
68
});
69
70
await handler(req, res);
71
expect(res.statusCode).toBe(200);
72
const data = res._getJSONData();
73
expect(data.result.length).toBe(cutoff);
74
});
75
});
76
77