File size: 3,906 Bytes
0ad74ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect, vi, beforeEach } from "vitest";
import { bootstrap_custom_element } from "./index";

const delay = (ms?: number) =>
	new Promise((resolve) => setTimeout(resolve, ms));

describe("bootstrap_custom_element", () => {
	const create = vi.fn();

	beforeEach(() => {
		document.body.innerHTML = "";
		create.mockClear();
	});

	bootstrap_custom_element(create);

	it("parses a <gradio-lite> element that contains a string literal as a direct child", async () => {
		document.body.innerHTML = `
<gradio-lite>
  import gradio as gr
</gradio-lite>
`;
		await delay(); // Wait for the requestAnimationFrame to run

		expect(create).toHaveBeenCalledWith(
			expect.objectContaining({
				code: expect.stringMatching(/import gradio as gr/),
				requirements: [],
				files: undefined
			})
		);
	});

	it("parses a <gradio-lite> element that contains <gradio-code> and <gradio-requirements> elements", async () => {
		document.body.innerHTML = `
<gradio-lite>
    <gradio-code>
import gradio as gr
    </gradio-code>

    <gradio-requirements>
numpy
scipy
    </gradio-requirements>
</gradio-lite>
`;
		await delay(); // Wait for the requestAnimationFrame to run

		expect(create).toHaveBeenCalledWith(
			expect.objectContaining({
				code: expect.stringMatching(/import gradio as gr/),
				requirements: ["numpy", "scipy"],
				files: undefined
			})
		);
	});

	it("parses a <gradio-lite> element that contains <gradio-file> and <gradio-requirements> elements", async () => {
		document.body.innerHTML = `
<gradio-lite>
    <gradio-file name="app.py" entrypoint>
import gradio as gr

from foo import foo
    </gradio-file>

    <gradio-file name="foo.py">
def foo():
    return "bar"
    </gradio-file>

    <gradio-requirements>
numpy
scipy
    </gradio-requirements>
</gradio-lite>
`;
		await delay(); // Wait for the requestAnimationFrame to run

		expect(create).toHaveBeenCalledWith(
			expect.objectContaining({
				files: {
					"app.py": {
						data: expect.stringMatching(/import gradio as gr/)
					},
					"foo.py": {
						data: expect.stringMatching(/def foo\(\):/)
					}
				},
				entrypoint: "app.py",
				code: undefined,
				requirements: ["numpy", "scipy"]
			})
		);
	});

	it("dedents the code inside <gradio-lite>", async () => {
		document.body.innerHTML = `
<gradio-lite>
		import gradio as gr
</gradio-lite>
`;
		await delay(); // Wait for the requestAnimationFrame to run

		expect(create).toHaveBeenCalledWith(
			expect.objectContaining({
				code: expect.stringMatching(/^import gradio as gr/),
				requirements: [],
				files: undefined
			})
		);
	});

	it("dedents the code inside <gradio-code>", async () => {
		document.body.innerHTML = `
<gradio-lite>
		<gradio-code>
				import gradio as gr
		</gradio-code>
</gradio-lite>
`;
		await delay(); // Wait for the requestAnimationFrame to run

		expect(create).toHaveBeenCalledWith(
			expect.objectContaining({
				code: expect.stringMatching(/^import gradio as gr/),
				requirements: [],
				files: undefined
			})
		);
	});

	it("dedents Python code but normal files inside <gradio-file>", async () => {
		document.body.innerHTML = `
<gradio-lite>
		<gradio-file name="app.py" entrypoint>
				import gradio as gr

				from foo import foo
		</gradio-file>

		<gradio-file name="foo.py">
				def foo():
						return "bar"
		</gradio-file>

		<gradio-file name="data.txt">
				Hello, world!
		</gradio-file>
</gradio-lite>
`;
		await delay(); // Wait for the requestAnimationFrame to run

		expect(create).toHaveBeenCalledWith(
			expect.objectContaining({
				files: {
					"app.py": {
						data: expect.stringMatching(/^import gradio as gr/)
					},
					"foo.py": {
						data: expect.stringMatching(/^def foo\(\):/)
					},
					"data.txt": {
						data: expect.stringMatching(/Hello, world!/)
					}
				},
				entrypoint: "app.py",
				code: undefined,
				requirements: []
			})
		);
	});
});