-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
219 lines (195 loc) · 6.98 KB
/
index.js
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
import dedent from "codedent";
import assert from "./assert.js";
import "./_utils.js";
const { fetch } = globalThis;
const tick = (ms = 10) => new Promise(($) => setTimeout($, ms));
const clear = (python) => {
for (const [key, value] of Object.entries(python)) {
if (typeof value === "object") python[key] = null;
else python[key] = "";
}
};
const patchFetch = (callback) => {
Object.defineProperty(globalThis, "fetch", {
configurable: true,
get() {
try {
return callback;
} finally {
Object.defineProperty(globalThis, "fetch", {
configurable: true,
value: fetch
});
}
},
});
};
import { parseHTML } from "linkedom";
const { document, window, CustomEvent } = parseHTML("...");
globalThis.indexedDB = { open: () => ({}) };
globalThis.document = document;
globalThis.Element = window.Element;
globalThis.CustomEvent = CustomEvent;
globalThis.dispatchEvent = Object;
globalThis.MutationObserver = window.MutationObserver;
globalThis.Worker = class {};
globalThis.XPathResult = {};
globalThis.XPathEvaluator =
window.XPathEvaluator ||
class XPathEvaluator {
createExpression() {
return { evaluate: () => [] };
}
};
import("../esm/index.js").then(async polyscript => {
// shared 3rd party mocks
const {
python: pyodide,
setTarget,
loadPyodide,
} = await import("./mocked/pyodide.mjs");
const { python: micropython } = await import("./mocked/micropython.mjs");
// shared helpers
const div = document.createElement("div");
const shadowRoot = div.attachShadow({ mode: "open" });
const content = `
import sys
import js
js.document.currentScript.target.textContent = sys.version
`;
const { URL } = globalThis;
globalThis.URL = function (href) {
return { href };
};
globalThis.location = { href: "" };
// all tests
for (const test of [
async function versionedRuntime() {
document.head.innerHTML = `<script type="pyodide" version="0.26.3">${content}</script>`;
await tick();
assert(pyodide.content, dedent(content));
assert(pyodide.target.tagName, "PYODIDE-SCRIPT");
},
async function basicExpectations() {
document.head.innerHTML = `<script type="pyodide">${content}</script>`;
await tick();
assert(pyodide.content, dedent(content));
assert(pyodide.target.tagName, "PYODIDE-SCRIPT");
},
async function foreignRuntime() {
document.head.innerHTML = `<script type="pyodide" version="http://pyodide">${content}</script>`;
await tick();
assert(pyodide.content, dedent(content));
assert(pyodide.target.tagName, "PYODIDE-SCRIPT");
},
async function basicMicroPython() {
document.head.innerHTML = `<script type="micropython">${content}</script>`;
await tick();
assert(micropython.content, dedent(content));
assert(micropython.target.tagName, "MICROPYTHON-SCRIPT");
const script = document.head.firstElementChild;
document.body.appendChild(script);
await tick();
assert(script.nextSibling, micropython.target);
micropython.target = null;
},
async function exernalResourceInShadowRoot() {
patchFetch(() =>
Promise.resolve({ text: () => Promise.resolve("OK") }),
);
shadowRoot.innerHTML = `
<my-plugin></my-plugin>
<script src="./whatever" env="unique" type="pyodide" target="my-plugin"></script>
`.trim();
await tick();
assert(pyodide.content, "OK");
assert(pyodide.target.tagName, "MY-PLUGIN");
},
async function explicitTargetNode() {
setTarget(div);
shadowRoot.innerHTML = `
<my-plugin></my-plugin>
<script type="pyodide"></script>
`.trim();
await tick();
assert(pyodide.target, div);
},
async function explicitTargetAsString() {
setTarget("my-plugin");
shadowRoot.innerHTML = `
<my-plugin></my-plugin>
<script type="pyodide"></script>
`.trim();
await tick();
assert(pyodide.target.tagName, "MY-PLUGIN");
},
async function jsonConfig() {
const packages = {};
patchFetch(() => Promise.resolve({ json: () => ({ packages }) }));
shadowRoot.innerHTML = `<script config="./whatever.json" type="pyodide"></script>`;
await tick();
assert(pyodide.packages, packages);
},
// async function tomlConfig() {
// const jsonPackages = JSON.stringify({
// packages: { a: Math.random() },
// });
// patchFetch(() =>
// Promise.resolve({ text: () => Promise.resolve(jsonPackages) }),
// );
// shadowRoot.innerHTML = `<script config="./whatever.toml" type="pyodide"></script>`;
// // there are more promises in here let's increase the tick delay to avoid flaky tests
// await tick(20);
// assert(
// JSON.stringify({ packages: pyodide.packages }),
// jsonPackages,
// );
// },
async function fetchConfig() {
const jsonPackages = JSON.stringify({
fetch: [
{ files: ["./a.py", "./b.py"] },
{ from: "utils" },
{ from: "/utils", files: ["c.py"] },
],
js_modules: {
main: { "./modules.js": "random_js" }
}
});
patchFetch(() =>
Promise.resolve({
arrayBuffer: () => Promise.resolve([]),
text: () => Promise.resolve(jsonPackages),
}),
);
shadowRoot.innerHTML = `
<script type="pyodide" config="./fetch.toml">
import js
import a, b
js.console.log(a.x)
js.console.log(b.x)
</script>
`;
await tick(10);
},
async function testDefaultRuntime() {
const pyodide = await polyscript.env.pyodide;
const keys = Object.keys(loadPyodide()).join(",");
assert(Object.keys(pyodide).join(","), keys);
const unique = await polyscript.env.unique;
assert(Object.keys(unique).join(","), keys);
},
async function pyEvents() {
shadowRoot.innerHTML = `
<button py-click="test()">test</button>
<button py-env="unique" py-click="test()">test</button>
`;
await tick(20);
},
]) {
await test();
clear(pyodide);
clear(micropython);
}
globalThis.URL = URL;
});