gradio-pr-bot commited on
Commit
63eb75d
·
verified ·
1 Parent(s): 84da427

Upload folder using huggingface_hub

Browse files
6.11.0/tootils/package.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "@self/tootils",
3
- "version": "0.11.0",
4
  "description": "Internal test utilities",
5
  "type": "module",
6
  "main": "src/index.ts",
@@ -18,7 +18,8 @@
18
  ".": "./src/index.ts",
19
  "./render": "./src/render.ts",
20
  "./shared-prop-tests": "./src/shared-prop-tests.ts",
21
- "./app-launcher": "./src/app-launcher.ts"
 
22
  },
23
  "scripts": {
24
  "package": "svelte-package --input=. --tsconfig=../../tsconfig.json"
 
1
  {
2
  "name": "@self/tootils",
3
+ "version": "0.12.0",
4
  "description": "Internal test utilities",
5
  "type": "module",
6
  "main": "src/index.ts",
 
18
  ".": "./src/index.ts",
19
  "./render": "./src/render.ts",
20
  "./shared-prop-tests": "./src/shared-prop-tests.ts",
21
+ "./app-launcher": "./src/app-launcher.ts",
22
+ "./download-command": "./src/download-command.ts"
23
  },
24
  "scripts": {
25
  "package": "svelte-package --input=. --tsconfig=../../tsconfig.json"
6.11.0/tootils/src/download-command.ts ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { BrowserCommand } from "vitest/node";
2
+ import { resolve } from "path";
3
+ import { readFile } from "fs/promises";
4
+
5
+ /**
6
+ * Vitest browser command that captures a real file download triggered by
7
+ * clicking an element. Runs server-side with access to the Playwright page.
8
+ *
9
+ * Sets up page.waitForEvent("download") BEFORE clicking, so the download
10
+ * event is never missed.
11
+ */
12
+ export const expect_download: BrowserCommand<
13
+ [selector: string, options?: { timeout?: number }],
14
+ { suggested_filename: string; content: string | null }
15
+ > = async (context, selector, options) => {
16
+ const timeout = options?.timeout ?? 5000;
17
+ const provider = context.provider as any;
18
+ const page = provider.getPage(context.sessionId);
19
+ // Tests run inside an iframe; use the iframe locator to click
20
+ // but listen for the download event on the parent page.
21
+ const iframe = (context as any).iframe;
22
+
23
+ const [download] = await Promise.all([
24
+ page.waitForEvent("download", { timeout }),
25
+ iframe.locator(selector).click()
26
+ ]);
27
+
28
+ const suggested_filename = download.suggestedFilename();
29
+ const path = await download.path();
30
+
31
+ let content: string | null = null;
32
+ if (path) {
33
+ const fs = await import("fs/promises");
34
+ content = await fs.readFile(path, "utf-8");
35
+ }
36
+
37
+ return {
38
+ suggested_filename,
39
+ content
40
+ };
41
+ };
42
+
43
+ /**
44
+ * Vitest browser command that sets files on an <input type="file"> element.
45
+ * Resolves fixture URL paths (e.g. "/test/test_files/bus.png") to absolute
46
+ * disk paths and uses Playwright's setInputFiles().
47
+ */
48
+ export const set_file_inputs: BrowserCommand<
49
+ [file_urls: string[], selector?: string]
50
+ > = async (context, file_urls, selector) => {
51
+ const root = context.project.config.root;
52
+ const iframe = (context as any).iframe;
53
+
54
+ const paths = file_urls.map((url) => resolve(root, url.replace(/^\//, "")));
55
+
56
+ await iframe
57
+ .locator(selector ?? 'input[type="file"]')
58
+ .first()
59
+ .setInputFiles(paths);
60
+ };
61
+
62
+ interface Drop_file_spec {
63
+ data: string; // base64
64
+ name: string;
65
+ mime_type: string;
66
+ }
67
+
68
+ /**
69
+ * Vitest browser command that simulates dropping files onto an element.
70
+ * Reads files from disk, transfers them as base64 into the browser context,
71
+ * and dispatches dragenter, dragover, and drop events with a real DataTransfer.
72
+ */
73
+ export const drop_files: BrowserCommand<
74
+ [file_urls: string[], selector: string]
75
+ > = async (context, file_urls, selector) => {
76
+ const root = context.project.config.root;
77
+
78
+ const files: Drop_file_spec[] = await Promise.all(
79
+ file_urls.map(async (url) => {
80
+ const abs = resolve(root, url.replace(/^\//, ""));
81
+ const data = (await readFile(abs)).toString("base64");
82
+ const name = abs.split("/").pop()!;
83
+ const ext = name.split(".").pop()!.toLowerCase();
84
+ const mime_type = MIME_TYPES[ext] || "application/octet-stream";
85
+ return { data, name, mime_type };
86
+ })
87
+ );
88
+
89
+ const iframe = (context as any).iframe;
90
+ await iframe
91
+ .locator(selector)
92
+ .first()
93
+ .evaluate((target: Element, files: Drop_file_spec[]) => {
94
+ const dt = new DataTransfer();
95
+ for (const f of files) {
96
+ const bytes = Uint8Array.from(atob(f.data), (c) => c.charCodeAt(0));
97
+ dt.items.add(new File([bytes], f.name, { type: f.mime_type }));
98
+ }
99
+
100
+ target.dispatchEvent(
101
+ new DragEvent("dragenter", {
102
+ dataTransfer: dt,
103
+ bubbles: true
104
+ })
105
+ );
106
+ target.dispatchEvent(
107
+ new DragEvent("dragover", {
108
+ dataTransfer: dt,
109
+ bubbles: true
110
+ })
111
+ );
112
+ target.dispatchEvent(
113
+ new DragEvent("drop", { dataTransfer: dt, bubbles: true })
114
+ );
115
+ }, files);
116
+ };
117
+
118
+ const MIME_TYPES: Record<string, string> = {
119
+ txt: "text/plain",
120
+ csv: "text/csv",
121
+ json: "application/json",
122
+ pdf: "application/pdf",
123
+ jpg: "image/jpeg",
124
+ jpeg: "image/jpeg",
125
+ png: "image/png",
126
+ gif: "image/gif",
127
+ webp: "image/webp",
128
+ svg: "image/svg+xml",
129
+ mp4: "video/mp4",
130
+ webm: "video/webm",
131
+ ogg: "video/ogg",
132
+ avi: "video/x-msvideo",
133
+ wav: "audio/wav",
134
+ mp3: "audio/mpeg",
135
+ flac: "audio/flac"
136
+ };
6.11.0/tootils/src/download.ts ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { commands } from "vitest/browser";
2
+ import type { FileData } from "@gradio/client";
3
+
4
+ export interface DownloadResult {
5
+ suggested_filename: string;
6
+ content: string | null;
7
+ }
8
+
9
+ /**
10
+ * Clicks an element and captures the resulting file download.
11
+ *
12
+ * Uses a real browser download via Playwright under the hood —
13
+ * the file is actually downloaded and its content is readable.
14
+ *
15
+ * @param selector - CSS selector for the element to click
16
+ * @param options - Optional timeout (default 5000ms)
17
+ * @returns The downloaded file's suggested filename and text content
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * const { suggested_filename, content } = await download_file("a.download-link");
22
+ * expect(suggested_filename).toBe("data.csv");
23
+ * expect(content).toContain("col1,col2");
24
+ * ```
25
+ */
26
+ export async function download_file(
27
+ selector: string,
28
+ options?: { timeout?: number }
29
+ ): Promise<DownloadResult> {
30
+ return (commands as any).expect_download(selector, options);
31
+ }
32
+
33
+ /**
34
+ * Sets files on an `<input type="file">` element using real file fixtures.
35
+ *
36
+ * Accepts one or more FileData fixtures (e.g. TEST_JPG, TEST_PNG) and
37
+ * sets them on the file input, triggering the browser's native change event.
38
+ *
39
+ * @param files - One or more FileData fixtures to upload
40
+ * @param selector - CSS selector for the file input (default: 'input[type="file"]')
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * import { render, upload_file, TEST_JPG } from "@self/tootils/render";
45
+ *
46
+ * const { listen } = await render(ImageUpload, { interactive: true });
47
+ * const upload = listen("upload");
48
+ *
49
+ * await upload_file(TEST_JPG);
50
+ *
51
+ * expect(upload).toHaveBeenCalled();
52
+ * ```
53
+ */
54
+ export async function upload_file(
55
+ files: FileData | FileData[],
56
+ selector?: string
57
+ ): Promise<void> {
58
+ const file_list = Array.isArray(files) ? files : [files];
59
+ const urls = file_list.map((f) => f.url ?? f.path);
60
+ return (commands as any).set_file_inputs(urls, selector);
61
+ }
62
+
63
+ /**
64
+ * Simulates dragging and dropping files onto an element.
65
+ *
66
+ * Reads the fixture files from disk, constructs a real DataTransfer
67
+ * with File objects, and dispatches dragenter, dragover, and drop events
68
+ * on the target element.
69
+ *
70
+ * @param files - One or more FileData fixtures to drop
71
+ * @param selector - CSS selector for the drop target element
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * import { render, drop_file, TEST_JPG } from "@self/tootils/render";
76
+ *
77
+ * const { listen } = await render(ImageUpload, { interactive: true });
78
+ * const upload = listen("upload");
79
+ *
80
+ * await drop_file(TEST_JPG, "[aria-label='Click to upload or drop files']");
81
+ *
82
+ * await vi.waitFor(() => expect(upload).toHaveBeenCalled());
83
+ * ```
84
+ */
85
+ export async function drop_file(
86
+ files: FileData | FileData[],
87
+ selector: string
88
+ ): Promise<void> {
89
+ const file_list = Array.isArray(files) ? files : [files];
90
+ const urls = file_list.map((f) => f.url ?? f.path);
91
+ return (commands as any).drop_files(urls, selector);
92
+ }
6.11.0/tootils/src/fixtures.ts ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { FileData } from "@gradio/client";
2
+
3
+ const BASE = "/test/test_files";
4
+
5
+ function fixture(filename: string, mime_type: string, size: number): FileData {
6
+ const url = `${BASE}/${filename}`;
7
+ return new FileData({
8
+ path: url,
9
+ url,
10
+ orig_name: filename,
11
+ size,
12
+ mime_type
13
+ });
14
+ }
15
+
16
+ export const TEST_TXT = fixture("alphabet.txt", "text/plain", 26);
17
+ export const TEST_JPG = fixture("cheetah1.jpg", "image/jpeg", 20552);
18
+ export const TEST_PNG = fixture("bus.png", "image/png", 1951);
19
+ export const TEST_MP4 = fixture("video_sample.mp4", "video/mp4", 261179);
20
+ export const TEST_WAV = fixture("audio_sample.wav", "audio/wav", 16136);
21
+ export const TEST_PDF = fixture("sample_file.pdf", "application/pdf", 10558);
6.11.0/tootils/src/render.ts CHANGED
@@ -58,19 +58,21 @@ export async function render<
58
  props?: Omit<Props, "gradio" | "loading_status"> & {
59
  loading_status?: LoadingStatus;
60
  },
61
- _container?: HTMLElement
 
 
62
  ): Promise<
63
  RenderResult<T> & {
64
- listen: (event_name: string) => Mock;
65
  set_data: (data: Record<string, any>) => Promise<void>;
66
  get_data: () => Promise<Record<string, any>>;
67
  }
68
  > {
69
  let container: HTMLElement;
70
- if (!_container) {
71
  container = document.body;
72
  } else {
73
- container = _container;
74
  }
75
 
76
  const target = container.appendChild(document.createElement("div"));
@@ -94,6 +96,7 @@ export async function render<
94
  };
95
 
96
  const event_listeners = new Map<string, Set<(data: any) => void>>();
 
97
 
98
  function notify_listeners(event: string, data: any): void {
99
  const listeners = event_listeners.get(event);
@@ -105,15 +108,28 @@ export async function render<
105
  }
106
 
107
  const dispatcher = (_id: number, event: string, data: any): void => {
 
108
  notify_listeners(event, data);
109
  };
110
 
111
- function listen(event_name: string): Mock {
 
 
 
112
  const fn = vi.fn();
113
  if (!event_listeners.has(event_name)) {
114
  event_listeners.set(event_name, new Set());
115
  }
116
  event_listeners.get(event_name)!.add(fn);
 
 
 
 
 
 
 
 
 
117
  return fn;
118
  }
119
 
@@ -242,4 +258,24 @@ export type FireFunction = (
242
  event: Event
243
  ) => Promise<boolean>;
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  export * from "@testing-library/dom";
 
58
  props?: Omit<Props, "gradio" | "loading_status"> & {
59
  loading_status?: LoadingStatus;
60
  },
61
+ options?: {
62
+ container?: HTMLElement;
63
+ }
64
  ): Promise<
65
  RenderResult<T> & {
66
+ listen: (event_name: string, opts?: { retrospective?: boolean }) => Mock;
67
  set_data: (data: Record<string, any>) => Promise<void>;
68
  get_data: () => Promise<Record<string, any>>;
69
  }
70
  > {
71
  let container: HTMLElement;
72
+ if (!options?.container) {
73
  container = document.body;
74
  } else {
75
+ container = options.container;
76
  }
77
 
78
  const target = container.appendChild(document.createElement("div"));
 
96
  };
97
 
98
  const event_listeners = new Map<string, Set<(data: any) => void>>();
99
+ const event_buffer: Array<{ event: string; data: any }> = [];
100
 
101
  function notify_listeners(event: string, data: any): void {
102
  const listeners = event_listeners.get(event);
 
108
  }
109
 
110
  const dispatcher = (_id: number, event: string, data: any): void => {
111
+ event_buffer.push({ event, data });
112
  notify_listeners(event, data);
113
  };
114
 
115
+ function listen(
116
+ event_name: string,
117
+ opts?: { retrospective?: boolean }
118
+ ): Mock {
119
  const fn = vi.fn();
120
  if (!event_listeners.has(event_name)) {
121
  event_listeners.set(event_name, new Set());
122
  }
123
  event_listeners.get(event_name)!.add(fn);
124
+
125
+ if (opts?.retrospective) {
126
+ for (const entry of event_buffer) {
127
+ if (entry.event === event_name) {
128
+ fn(entry.data);
129
+ }
130
+ }
131
+ }
132
+
133
  return fn;
134
  }
135
 
 
258
  event: Event
259
  ) => Promise<boolean>;
260
 
261
+ export { download_file, upload_file, drop_file } from "./download.js";
262
+
263
+ /**
264
+ * Creates a mock client suitable for components that use file uploads.
265
+ * The upload mock echoes back the input FileData unchanged.
266
+ */
267
+ export function mock_client(): Record<string, any> {
268
+ return {
269
+ upload: async (file_data: any[]) => file_data,
270
+ stream: async () => ({ onmessage: null, close: () => {} })
271
+ };
272
+ }
273
+ export {
274
+ TEST_TXT,
275
+ TEST_JPG,
276
+ TEST_PNG,
277
+ TEST_MP4,
278
+ TEST_WAV,
279
+ TEST_PDF
280
+ } from "./fixtures.js";
281
  export * from "@testing-library/dom";