gradio-pr-bot commited on
Commit
02c2127
·
verified ·
1 Parent(s): ce0b5c4

Upload folder using huggingface_hub

Browse files
6.0.0-dev.6/tootils/package.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@self/tootils",
3
+ "version": "0.8.1-dev.0",
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.0-dev.6/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.0-dev.6/tootils/src/render.ts ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ getQueriesForElement,
3
+ prettyDOM,
4
+ fireEvent as dtlFireEvent
5
+ } from "@testing-library/dom";
6
+ import { tick } from "svelte";
7
+ import type { SvelteComponent } 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 } from "@gradio/utils";
18
+ import type { LoadingStatus } from "@gradio/statustracker";
19
+
20
+ const containerCache = new Map();
21
+ const componentCache = new Set();
22
+
23
+ type ComponentType<T extends SvelteComponent, Props> = new (args: {
24
+ target: any;
25
+ props?: Props;
26
+ }) => T;
27
+
28
+ export type RenderResult<
29
+ C extends SvelteComponent,
30
+ Q extends Queries = typeof queries
31
+ > = {
32
+ container: HTMLElement;
33
+ component: C;
34
+ debug: (el?: HTMLElement | DocumentFragment) => void;
35
+ unmount: () => void;
36
+ } & { [P in keyof Q]: BoundFunction<Q[P]> };
37
+
38
+ const loading_status: LoadingStatus = {
39
+ eta: 0,
40
+ queue_position: 1,
41
+ queue_size: 1,
42
+ status: "complete" as LoadingStatus["status"],
43
+ scroll_to_output: false,
44
+ visible: true,
45
+ fn_index: 0,
46
+ show_progress: "full"
47
+ };
48
+
49
+ export interface RenderOptions<Q extends Queries = typeof queries> {
50
+ container?: HTMLElement;
51
+ queries?: Q;
52
+ }
53
+
54
+ export async function render<
55
+ Events extends Record<string, any>,
56
+ Props extends Record<string, any>,
57
+ T extends SvelteComponent<Props, Events>,
58
+ X extends Record<string, any>
59
+ >(
60
+ Component: ComponentType<T, Props> | { default: ComponentType<T, Props> },
61
+ props?: Omit<Props, "gradio" | "loading_status"> & {
62
+ loading_status?: LoadingStatus;
63
+ },
64
+ _container?: HTMLElement
65
+ ): Promise<
66
+ RenderResult<T> & {
67
+ listen: typeof listen;
68
+ wait_for_event: typeof wait_for_event;
69
+ }
70
+ > {
71
+ let container: HTMLElement;
72
+ if (!_container) {
73
+ container = document.body;
74
+ } else {
75
+ container = _container;
76
+ }
77
+
78
+ const target = container.appendChild(document.createElement("div"));
79
+
80
+ const ComponentConstructor: ComponentType<
81
+ T,
82
+ Props & { gradio: typeof Gradio<X> }
83
+ > =
84
+ //@ts-ignore
85
+ Component.default || Component;
86
+
87
+ const id = Math.floor(Math.random() * 1000000);
88
+
89
+ const component = new ComponentConstructor({
90
+ target,
91
+ //@ts-ignore
92
+ props: {
93
+ loading_status,
94
+ ...(props || {}),
95
+ //@ts-ignore
96
+ gradio: new Gradio(
97
+ id,
98
+ target,
99
+ "light",
100
+ "2.0.0",
101
+ "http://localhost:8000",
102
+ false,
103
+ null,
104
+ //@ts-ignore
105
+ (s) => s,
106
+ // @ts-ignore
107
+ { client: {} },
108
+ () => {}
109
+ )
110
+ }
111
+ });
112
+
113
+ containerCache.set(container, { target, component });
114
+ componentCache.add(component);
115
+
116
+ component.$$.on_destroy.push(() => {
117
+ componentCache.delete(component);
118
+ });
119
+
120
+ await tick();
121
+
122
+ type extractGeneric<Type> = Type extends Gradio<infer X> ? X : null;
123
+ type event_name = keyof extractGeneric<Props["gradio"]>;
124
+
125
+ function listen(event: event_name): Spy {
126
+ const mock = spy();
127
+ target.addEventListener("gradio", (e: Event) => {
128
+ if (isCustomEvent(e)) {
129
+ if (e.detail.event === event && e.detail.id === id) {
130
+ mock(e);
131
+ }
132
+ }
133
+ });
134
+
135
+ return mock;
136
+ }
137
+
138
+ async function wait_for_event(event: event_name): Promise<Spy> {
139
+ return new Promise((res) => {
140
+ const mock = spy();
141
+ target.addEventListener("gradio", (e: Event) => {
142
+ if (isCustomEvent(e)) {
143
+ if (e.detail.event === event && e.detail.id === id) {
144
+ mock(e);
145
+ res(mock);
146
+ }
147
+ }
148
+ });
149
+ });
150
+ }
151
+
152
+ return {
153
+ container,
154
+ component,
155
+ //@ts-ignore
156
+ debug: (el = container): void => console.warn(prettyDOM(el)),
157
+ unmount: (): void => {
158
+ if (componentCache.has(component)) component.$destroy();
159
+ },
160
+ ...getQueriesForElement(container),
161
+ listen,
162
+ wait_for_event
163
+ };
164
+ }
165
+
166
+ const cleanupAtContainer = (container: HTMLElement): void => {
167
+ const { target, component } = containerCache.get(container);
168
+
169
+ if (componentCache.has(component)) component.$destroy();
170
+
171
+ if (target.parentNode === document.body) {
172
+ document.body.removeChild(target);
173
+ }
174
+
175
+ containerCache.delete(container);
176
+ };
177
+
178
+ export function cleanup(): void {
179
+ Array.from(containerCache.keys()).forEach(cleanupAtContainer);
180
+ }
181
+
182
+ export const fireEvent = Object.keys(dtlFireEvent).reduce((acc, key) => {
183
+ const _key = key as EventType;
184
+ return {
185
+ ...acc,
186
+ [_key]: async (
187
+ element: Document | Element | Window,
188
+ options: object = {}
189
+ ): Promise<boolean> => {
190
+ const event = dtlFireEvent[_key](element, options);
191
+ await tick();
192
+ return event;
193
+ }
194
+ };
195
+ }, {} as FireObject);
196
+
197
+ export type FireFunction = (
198
+ element: Document | Element | Window,
199
+ event: Event
200
+ ) => Promise<boolean>;
201
+
202
+ export * from "@testing-library/dom";
203
+
204
+ function isCustomEvent(event: Event): event is CustomEvent {
205
+ return "detail" in event;
206
+ }