gradio-pr-bot commited on
Commit
a6750c3
·
verified ·
1 Parent(s): 69742a9

Upload folder using huggingface_hub

Browse files
6.0.2/tootils/package.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@self/tootils",
3
+ "version": "0.8.1",
4
+ "description": "Internal test utilities",
5
+ "type": "module",
6
+ "main": "src/index.ts",
7
+ "author": "",
8
+ "license": "ISC",
9
+ "private": true,
10
+ "dependencies": {
11
+ "@gradio/statustracker": "workspace:^",
12
+ "@gradio/utils": "workspace:^"
13
+ },
14
+ "peerDependencies": {
15
+ "svelte": "^5.43.4"
16
+ },
17
+ "exports": {
18
+ ".": "./src/index.ts",
19
+ "./render": "./src/render.ts"
20
+ },
21
+ "scripts": {
22
+ "package": "svelte-package --input=. --tsconfig=../../tsconfig.json"
23
+ }
24
+ }
6.0.2/tootils/src/index.ts ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { test as base, type Locator, type Page } from "@playwright/test";
2
+ import { spy } from "tinyspy";
3
+ import { performance } from "node:perf_hooks";
4
+ import url from "url";
5
+ import path from "path";
6
+ import fsPromises from "fs/promises";
7
+
8
+ import type { SvelteComponent } from "svelte";
9
+ import type { SpyFn } from "tinyspy";
10
+
11
+ export function get_text<T extends HTMLElement>(el: T): string {
12
+ return el.innerText.trim();
13
+ }
14
+
15
+ export function wait(n: number): Promise<void> {
16
+ return new Promise((r) => setTimeout(r, n));
17
+ }
18
+
19
+ const ROOT_DIR = path.resolve(
20
+ url.fileURLToPath(import.meta.url),
21
+ "../../../.."
22
+ );
23
+
24
+ const test_normal = base.extend<{ setup: void }>({
25
+ setup: [
26
+ async ({ page }, use, testInfo): Promise<void> => {
27
+ const port = process.env.GRADIO_E2E_TEST_PORT;
28
+ const { file } = testInfo;
29
+ const test_name = path.basename(file, ".spec.ts");
30
+
31
+ await page.goto(`localhost:${port}/${test_name}`);
32
+ if (process.env?.GRADIO_SSR_MODE?.toLowerCase() === "true") {
33
+ await page.waitForSelector("#svelte-announcer");
34
+ }
35
+ await page.waitForLoadState("load");
36
+ await use();
37
+ },
38
+ { auto: true }
39
+ ]
40
+ });
41
+
42
+ export const test = test_normal;
43
+
44
+ export async function wait_for_event(
45
+ component: SvelteComponent,
46
+ event: string
47
+ ): Promise<SpyFn> {
48
+ const mock = spy();
49
+ return new Promise((res) => {
50
+ component.$on(event, () => {
51
+ mock();
52
+ res(mock);
53
+ });
54
+ });
55
+ }
56
+
57
+ export interface ActionReturn<
58
+ Parameter = never,
59
+ Attributes extends Record<string, any> = Record<never, any>
60
+ > {
61
+ update?: [Parameter] extends [never] ? never : (parameter: Parameter) => void;
62
+ destroy?: () => void;
63
+ /**
64
+ * ### DO NOT USE THIS
65
+ * This exists solely for type-checking and has no effect at runtime.
66
+ * Set this through the `Attributes` generic instead.
67
+ */
68
+ $$_attributes?: Attributes;
69
+ }
70
+
71
+ export { expect } from "@playwright/test";
72
+ export * from "./render";
73
+
74
+ export const drag_and_drop_file = async (
75
+ page: Page,
76
+ selector: string | Locator,
77
+ filePath: string,
78
+ fileName: string,
79
+ fileType = "",
80
+ count = 1
81
+ ): Promise<void> => {
82
+ const buffer = (await fsPromises.readFile(filePath)).toString("base64");
83
+
84
+ const dataTransfer = await page.evaluateHandle(
85
+ async ({ bufferData, localFileName, localFileType, count }) => {
86
+ const dt = new DataTransfer();
87
+
88
+ const blobData = await fetch(bufferData).then((res) => res.blob());
89
+
90
+ const file = new File([blobData], localFileName, {
91
+ type: localFileType
92
+ });
93
+
94
+ for (let i = 0; i < count; i++) {
95
+ dt.items.add(file);
96
+ }
97
+ return dt;
98
+ },
99
+ {
100
+ bufferData: `data:application/octet-stream;base64,${buffer}`,
101
+ localFileName: fileName,
102
+ localFileType: fileType,
103
+ count
104
+ }
105
+ );
106
+
107
+ if (typeof selector === "string") {
108
+ await page.dispatchEvent(selector, "drop", { dataTransfer });
109
+ } else {
110
+ await selector.dispatchEvent("drop", { dataTransfer });
111
+ }
112
+ };
113
+
114
+ export async function go_to_testcase(
115
+ page: Page,
116
+ test_case: string
117
+ ): Promise<void> {
118
+ const url = page.url();
119
+ await page.goto(`${url.substring(0, url.length - 1)}_${test_case}_testcase`);
120
+ if (process.env?.GRADIO_SSR_MODE?.toLowerCase() === "true") {
121
+ await page.waitForSelector("#svelte-announcer");
122
+ }
123
+ }
6.0.2/tootils/src/render.ts ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ getQueriesForElement,
3
+ prettyDOM,
4
+ fireEvent as dtlFireEvent
5
+ } from "@testing-library/dom";
6
+ import { tick, mount, unmount } from "svelte";
7
+ import type { SvelteComponent, Component } from "svelte";
8
+
9
+ import type {
10
+ queries,
11
+ Queries,
12
+ BoundFunction,
13
+ EventType,
14
+ FireObject
15
+ } from "@testing-library/dom";
16
+ import { spy, type Spy } from "tinyspy";
17
+ import { GRADIO_ROOT, allowed_shared_props } from "@gradio/utils";
18
+ import type { LoadingStatus } from "@gradio/statustracker";
19
+ import { get } from "svelte/store";
20
+ import { _ } from "svelte-i18n";
21
+
22
+ const containerCache = new Map();
23
+ const componentCache = new Set();
24
+
25
+ type ComponentType<T extends SvelteComponent, Props> = Component<Props>;
26
+
27
+ export type RenderResult<
28
+ C extends SvelteComponent,
29
+ Q extends Queries = typeof queries
30
+ > = {
31
+ container: HTMLElement;
32
+ component: C;
33
+ debug: (el?: HTMLElement | DocumentFragment) => void;
34
+ unmount: () => void;
35
+ } & { [P in keyof Q]: BoundFunction<Q[P]> };
36
+
37
+ const loading_status: LoadingStatus = {
38
+ eta: 0,
39
+ queue_position: 1,
40
+ queue_size: 1,
41
+ status: "complete" as LoadingStatus["status"],
42
+ scroll_to_output: false,
43
+ visible: true,
44
+ fn_index: 0,
45
+ show_progress: "full"
46
+ };
47
+
48
+ export interface RenderOptions<Q extends Queries = typeof queries> {
49
+ container?: HTMLElement;
50
+ queries?: Q;
51
+ }
52
+
53
+ export async function render<
54
+ Events extends Record<string, any>,
55
+ Props extends Record<string, any>,
56
+ T extends SvelteComponent<Props, Events>,
57
+ X extends Record<string, any>
58
+ >(
59
+ Component: ComponentType<T, Props> | { default: ComponentType<T, Props> },
60
+ props?: Omit<Props, "gradio" | "loading_status"> & {
61
+ loading_status?: LoadingStatus;
62
+ },
63
+ _container?: HTMLElement
64
+ ): Promise<
65
+ RenderResult<T> & {
66
+ listen: typeof listen;
67
+ wait_for_event: typeof wait_for_event;
68
+ }
69
+ > {
70
+ let container: HTMLElement;
71
+ if (!_container) {
72
+ container = document.body;
73
+ } else {
74
+ container = _container;
75
+ }
76
+
77
+ const target = container.appendChild(document.createElement("div"));
78
+
79
+ const ComponentConstructor: ComponentType<T, Props> =
80
+ //@ts-ignore
81
+ Component.default || Component;
82
+
83
+ const id = Math.floor(Math.random() * 1000000);
84
+
85
+ const mockRegister = (): void => {};
86
+
87
+ const mockDispatcher = (_id: number, event: string, data: any): void => {
88
+ const e = new CustomEvent("gradio", {
89
+ bubbles: true,
90
+ detail: { data, id: _id, event }
91
+ });
92
+ target.dispatchEvent(e);
93
+ };
94
+
95
+ const i18nFormatter = (s: string | null | undefined): string => s ?? "";
96
+
97
+ const shared_props_obj: Record<string, any> = {
98
+ id,
99
+ target,
100
+ theme_mode: "light" as const,
101
+ version: "2.0.0",
102
+ formatter: i18nFormatter,
103
+ client: {} as any,
104
+ load_component: () => Promise.resolve({ default: {} as any }),
105
+ show_progress: true,
106
+ api_prefix: "",
107
+ server: {} as any,
108
+ show_label: true
109
+ };
110
+
111
+ const component_props_obj: Record<string, any> = {
112
+ i18n: i18nFormatter
113
+ };
114
+
115
+ if (props) {
116
+ for (const key in props) {
117
+ const value = (props as any)[key];
118
+ if (allowed_shared_props.includes(key as any)) {
119
+ shared_props_obj[key] = value;
120
+ } else {
121
+ component_props_obj[key] = value;
122
+ }
123
+ }
124
+ }
125
+
126
+ const componentProps = {
127
+ shared_props: shared_props_obj,
128
+ props: {
129
+ ...component_props_obj
130
+ },
131
+ ...shared_props_obj
132
+ };
133
+
134
+ const component = mount(ComponentConstructor, {
135
+ target,
136
+ props: componentProps,
137
+ context: new Map([
138
+ [GRADIO_ROOT, { register: mockRegister, dispatcher: mockDispatcher }]
139
+ ])
140
+ }) as T;
141
+
142
+ containerCache.set(container, { target, component });
143
+ componentCache.add(component);
144
+
145
+ await tick();
146
+
147
+ type event_name = string;
148
+
149
+ function listen(event: event_name): Spy {
150
+ const mock = spy();
151
+ target.addEventListener("gradio", (e: Event) => {
152
+ if (isCustomEvent(e)) {
153
+ if (e.detail.event === event && e.detail.id === id) {
154
+ mock(e);
155
+ }
156
+ }
157
+ });
158
+
159
+ return mock;
160
+ }
161
+
162
+ async function wait_for_event(event: event_name): Promise<Spy> {
163
+ return new Promise((res) => {
164
+ const mock = spy();
165
+ target.addEventListener("gradio", (e: Event) => {
166
+ if (isCustomEvent(e)) {
167
+ if (e.detail.event === event && e.detail.id === id) {
168
+ mock(e);
169
+ res(mock);
170
+ }
171
+ }
172
+ });
173
+ });
174
+ }
175
+
176
+ return {
177
+ container,
178
+ component,
179
+ //@ts-ignore
180
+ debug: (el = container): void => console.warn(prettyDOM(el)),
181
+ unmount: (): void => {
182
+ if (componentCache.has(component)) {
183
+ unmount(component);
184
+ }
185
+ },
186
+ ...getQueriesForElement(container),
187
+ listen,
188
+ wait_for_event
189
+ };
190
+ }
191
+
192
+ const cleanupAtContainer = (container: HTMLElement): void => {
193
+ const { target, component } = containerCache.get(container);
194
+
195
+ if (componentCache.has(component)) {
196
+ unmount(component);
197
+ }
198
+
199
+ if (target.parentNode === document.body) {
200
+ document.body.removeChild(target);
201
+ }
202
+
203
+ containerCache.delete(container);
204
+ };
205
+
206
+ export function cleanup(): void {
207
+ Array.from(containerCache.keys()).forEach(cleanupAtContainer);
208
+ }
209
+
210
+ export const fireEvent = Object.keys(dtlFireEvent).reduce((acc, key) => {
211
+ const _key = key as EventType;
212
+ return {
213
+ ...acc,
214
+ [_key]: async (
215
+ element: Document | Element | Window,
216
+ options: object = {}
217
+ ): Promise<boolean> => {
218
+ const event = dtlFireEvent[_key](element, options);
219
+ await tick();
220
+ return event;
221
+ }
222
+ };
223
+ }, {} as FireObject);
224
+
225
+ export type FireFunction = (
226
+ element: Document | Element | Window,
227
+ event: Event
228
+ ) => Promise<boolean>;
229
+
230
+ export * from "@testing-library/dom";
231
+
232
+ function isCustomEvent(event: Event): event is CustomEvent {
233
+ return "detail" in event;
234
+ }