gradio-pr-bot commited on
Commit
7e82c39
·
verified ·
1 Parent(s): 4e9aec6

Upload folder using huggingface_hub

Browse files
6.4.0/tootils/package.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@self/tootils",
3
+ "version": "0.8.3",
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.4.0/tootils/src/index.ts ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 (
33
+ process.env?.GRADIO_SSR_MODE?.toLowerCase() === "true" &&
34
+ !(
35
+ test_name.includes("multipage") ||
36
+ test_name.includes("chatinterface_deep_link")
37
+ )
38
+ ) {
39
+ await page.waitForSelector("#svelte-announcer");
40
+ }
41
+ await page.waitForLoadState("load");
42
+ await use();
43
+ },
44
+ { auto: true }
45
+ ]
46
+ });
47
+
48
+ export const test = test_normal;
49
+
50
+ export async function wait_for_event(
51
+ component: SvelteComponent,
52
+ event: string
53
+ ): Promise<SpyFn> {
54
+ const mock = spy();
55
+ return new Promise((res) => {
56
+ component.$on(event, () => {
57
+ mock();
58
+ res(mock);
59
+ });
60
+ });
61
+ }
62
+
63
+ export interface ActionReturn<
64
+ Parameter = never,
65
+ Attributes extends Record<string, any> = Record<never, any>
66
+ > {
67
+ update?: [Parameter] extends [never] ? never : (parameter: Parameter) => void;
68
+ destroy?: () => void;
69
+ /**
70
+ * ### DO NOT USE THIS
71
+ * This exists solely for type-checking and has no effect at runtime.
72
+ * Set this through the `Attributes` generic instead.
73
+ */
74
+ $$_attributes?: Attributes;
75
+ }
76
+
77
+ export { expect } from "@playwright/test";
78
+ export * from "./render";
79
+
80
+ export const drag_and_drop_file = async (
81
+ page: Page,
82
+ selector: string | Locator,
83
+ filePath: string,
84
+ fileName: string,
85
+ fileType = "",
86
+ count = 1
87
+ ): Promise<void> => {
88
+ const buffer = (await fsPromises.readFile(filePath)).toString("base64");
89
+
90
+ const dataTransfer = await page.evaluateHandle(
91
+ async ({ bufferData, localFileName, localFileType, count }) => {
92
+ const dt = new DataTransfer();
93
+
94
+ const blobData = await fetch(bufferData).then((res) => res.blob());
95
+
96
+ const file = new File([blobData], localFileName, {
97
+ type: localFileType
98
+ });
99
+
100
+ for (let i = 0; i < count; i++) {
101
+ dt.items.add(file);
102
+ }
103
+ return dt;
104
+ },
105
+ {
106
+ bufferData: `data:application/octet-stream;base64,${buffer}`,
107
+ localFileName: fileName,
108
+ localFileType: fileType,
109
+ count
110
+ }
111
+ );
112
+
113
+ if (typeof selector === "string") {
114
+ await page.dispatchEvent(selector, "drop", { dataTransfer });
115
+ } else {
116
+ await selector.dispatchEvent("drop", { dataTransfer });
117
+ }
118
+ };
119
+
120
+ export async function go_to_testcase(
121
+ page: Page,
122
+ test_case: string
123
+ ): Promise<void> {
124
+ const url = page.url();
125
+ await page.goto(`${url.substring(0, url.length - 1)}_${test_case}_testcase`);
126
+ if (process.env?.GRADIO_SSR_MODE?.toLowerCase() === "true") {
127
+ await page.waitForSelector("#svelte-announcer");
128
+ }
129
+ }
6.4.0/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
+ }