-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPyodideWorker.test.js
More file actions
238 lines (217 loc) · 6.14 KB
/
Copy pathPyodideWorker.test.js
File metadata and controls
238 lines (217 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* global globalThis */
import { waitFor } from "@testing-library/react";
import { TextEncoder } from "util";
class MockPythonArray extends Array {
toJs() {
return this;
}
}
// Mock global functions
global.postMessage = jest.fn();
global.importScripts = jest.fn();
global.TextEncoder = TextEncoder;
global.pygal = {};
global._internal_sense_hat = {};
describe("PyodideWorker", () => {
let worker;
let pyodide;
beforeEach(() => {
jest.resetModules();
pyodide = {
_api: {
pyodide_code: {
find_imports: () => new MockPythonArray(),
},
},
ffi: {
PythonError: Error,
},
FS: {
readdir: jest.fn().mockReturnValue([]),
writeFile: jest.fn(),
},
loadPackage: jest.fn().mockReturnValue({ catch: jest.fn() }),
loadPackagesFromImports: jest.fn(),
micropip: {
install: jest.fn().mockReturnValue({ catch: jest.fn() }),
},
pyimport: jest.fn(),
registerJsModule: jest.fn(),
runPython: jest.fn(),
runPythonAsync: jest.fn(),
setInterruptBuffer: jest.fn(),
setStdin: jest.fn(),
};
global.loadPyodide = jest.fn().mockResolvedValue(pyodide);
require("../../../../../PyodideWorker.js");
worker = globalThis.PyodideWorker();
});
test("it imports the pyodide script", () => {
expect(global.importScripts).toHaveBeenCalledWith(
"https://cdn.jsdelivr.net/pyodide/v0.29.3/full/pyodide.js",
);
});
test("it notifies component when pyodide is loading", () => {
expect(global.postMessage).toHaveBeenCalledWith({
method: "handleLoading",
});
});
test("it loads pyodide", () => {
expect(global.loadPyodide).toHaveBeenCalled();
});
test("it notifies component when pyodide has loaded", () => {
expect(global.postMessage).toHaveBeenCalledWith(
expect.objectContaining({
method: "handleLoaded",
}),
);
});
test("it writes a file", async () => {
await worker.onmessage({
data: {
method: "writeFile",
filename: "main.py",
content: "print('hello')",
},
});
const encoder = new TextEncoder();
expect(pyodide.FS.writeFile).toHaveBeenCalledWith(
"main.py",
encoder.encode("print('hello')"),
);
});
test("it patches the input function", async () => {
expect(pyodide.runPythonAsync).toHaveBeenCalledWith(
expect.stringMatching(/__builtins__.input = __patched_input__/),
);
});
test("it patches urllib and requests modules", async () => {
expect(pyodide.runPythonAsync).toHaveBeenCalledWith(
expect.stringMatching(/pyodide_http.patch_all()/),
);
});
test("it saves original open function", async () => {
expect(pyodide.runPythonAsync).toHaveBeenCalledWith(
expect.stringMatching(/_original_open = builtins.open/),
);
});
test("it tries to load package from file system", async () => {
pyodide._api.pyodide_code.find_imports = () => new MockPythonArray("numpy");
await worker.onmessage({
data: {
method: "runPython",
python: "import numpy",
},
});
await waitFor(() =>
expect(pyodide.FS.readdir).toHaveBeenCalledWith("/home/pyodide"),
);
});
test("it checks if the package is built into python", async () => {
pyodide._api.pyodide_code.find_imports = () => new MockPythonArray("numpy");
await worker.onmessage({
data: {
method: "runPython",
python: "import numpy",
},
});
await waitFor(() => expect(pyodide.pyimport).toHaveBeenCalledWith("numpy"));
});
test("it tries to load the package from pyodide", async () => {
pyodide._api.pyodide_code.find_imports = () => new MockPythonArray("numpy");
await worker.onmessage({
data: {
method: "runPython",
python: "import numpy",
},
});
await waitFor(() =>
expect(pyodide.loadPackage).toHaveBeenCalledWith("numpy"),
);
});
test("it tries to load the package from PyPI", async () => {
pyodide._api.pyodide_code.find_imports = () => new MockPythonArray("numpy");
await worker.onmessage({
data: {
method: "runPython",
python: "import numpy",
},
});
await waitFor(() =>
expect(pyodide.micropip.install).toHaveBeenCalledWith("numpy"),
);
});
test("it registers JS module for pygal", async () => {
pyodide._api.pyodide_code.find_imports = () => new MockPythonArray("pygal");
await worker.onmessage({
data: {
method: "runPython",
python: "import pygal",
},
});
await waitFor(() => {
expect(pyodide.registerJsModule).toHaveBeenCalledWith(
"pygal",
expect.any(Object),
);
});
});
test("it patches the open function", async () => {
await worker.onmessage({
data: {
method: "runPython",
python: "print('hello')",
},
});
await waitFor(() =>
expect(pyodide.runPythonAsync).toHaveBeenCalledWith(
expect.stringMatching(/builtins.open = _custom_open/),
{ filename: "__custom_open__.py" },
),
);
});
test("it runs the python code", async () => {
await worker.onmessage({
data: {
method: "runPython",
python: "print('hello')",
},
});
await waitFor(() => {
expect(pyodide.runPython).toHaveBeenCalledWith("print('hello')");
});
});
test("it clears the pyodide variables after running the code", async () => {
await worker.onmessage({
data: {
method: "runPython",
python: "print('hello')",
},
});
await waitFor(() =>
expect(pyodide.runPythonAsync).toHaveBeenCalledWith(
expect.stringContaining("del globals()[name]"),
),
);
});
test("it handles stopping by notifying component of an error", async () => {
worker.onmessage({
data: {
method: "runPython",
python: "print('hello')",
},
});
await worker.onmessage({
data: {
method: "stopPython",
},
});
await waitFor(() =>
expect(global.postMessage).toHaveBeenCalledWith(
expect.objectContaining({
method: "handleError",
}),
),
);
});
});