Upload folder using huggingface_hub
Browse files- 6.5.0/tootils/package.json +25 -0
- 6.5.0/tootils/src/app-launcher.ts +174 -0
- 6.5.0/tootils/src/index.ts +225 -0
- 6.5.0/tootils/src/render.ts +234 -0
6.5.0/tootils/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@self/tootils",
|
| 3 |
+
"version": "0.9.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.48.0"
|
| 16 |
+
},
|
| 17 |
+
"exports": {
|
| 18 |
+
".": "./src/index.ts",
|
| 19 |
+
"./render": "./src/render.ts",
|
| 20 |
+
"./app-launcher": "./src/app-launcher.ts"
|
| 21 |
+
},
|
| 22 |
+
"scripts": {
|
| 23 |
+
"package": "svelte-package --input=. --tsconfig=../../tsconfig.json"
|
| 24 |
+
}
|
| 25 |
+
}
|
6.5.0/tootils/src/app-launcher.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { spawn, type ChildProcess } from "node:child_process";
|
| 2 |
+
import net from "net";
|
| 3 |
+
import path from "path";
|
| 4 |
+
import fs from "fs";
|
| 5 |
+
import os from "os";
|
| 6 |
+
import url from "url";
|
| 7 |
+
|
| 8 |
+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
| 9 |
+
const ROOT_DIR = path.resolve(__dirname, "../../..");
|
| 10 |
+
|
| 11 |
+
export interface GradioApp {
|
| 12 |
+
port: number;
|
| 13 |
+
process: ChildProcess;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
export function killGradioApp(process: ChildProcess): void {
|
| 17 |
+
try {
|
| 18 |
+
process.kill("SIGTERM");
|
| 19 |
+
} catch {
|
| 20 |
+
// Process may already be dead
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
export async function findFreePort(
|
| 25 |
+
startPort: number,
|
| 26 |
+
endPort: number
|
| 27 |
+
): Promise<number> {
|
| 28 |
+
for (let port = startPort; port < endPort; port++) {
|
| 29 |
+
if (await isPortFree(port)) {
|
| 30 |
+
return port;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
throw new Error(`Could not find free port in range ${startPort}-${endPort}`);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
function isPortFree(port: number): Promise<boolean> {
|
| 37 |
+
return new Promise((resolve, reject) => {
|
| 38 |
+
const sock = net.createConnection(port, "127.0.0.1");
|
| 39 |
+
sock.once("connect", () => {
|
| 40 |
+
sock.end();
|
| 41 |
+
resolve(false);
|
| 42 |
+
});
|
| 43 |
+
sock.once("error", (e: NodeJS.ErrnoException) => {
|
| 44 |
+
sock.destroy();
|
| 45 |
+
if (e.code === "ECONNREFUSED") {
|
| 46 |
+
resolve(true);
|
| 47 |
+
} else {
|
| 48 |
+
reject(e);
|
| 49 |
+
}
|
| 50 |
+
});
|
| 51 |
+
});
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
export function getTestcases(demoName: string): string[] {
|
| 55 |
+
const demoDir = path.join(ROOT_DIR, "demo", demoName);
|
| 56 |
+
if (!fs.existsSync(demoDir)) {
|
| 57 |
+
return [];
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
return fs
|
| 61 |
+
.readdirSync(demoDir)
|
| 62 |
+
.filter((f) => f.endsWith("_testcase.py"))
|
| 63 |
+
.map((f) => path.basename(f, ".py"));
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// Check if a testcase file exists for this demo
|
| 67 |
+
export function hasTestcase(demoName: string, testcaseName: string): boolean {
|
| 68 |
+
const testcaseFile = path.join(
|
| 69 |
+
ROOT_DIR,
|
| 70 |
+
"demo",
|
| 71 |
+
demoName,
|
| 72 |
+
`${testcaseName}_testcase.py`
|
| 73 |
+
);
|
| 74 |
+
return fs.existsSync(testcaseFile);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// Get the path to a demo's Python file
|
| 78 |
+
function getDemoFilePath(demoName: string, testcaseName?: string): string {
|
| 79 |
+
if (testcaseName) {
|
| 80 |
+
return path.join(ROOT_DIR, "demo", demoName, `${testcaseName}_testcase.py`);
|
| 81 |
+
}
|
| 82 |
+
return path.join(ROOT_DIR, "demo", demoName, "run.py");
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
export async function launchGradioApp(
|
| 86 |
+
demoName: string,
|
| 87 |
+
workerIndex: number = 0,
|
| 88 |
+
timeout: number = 60000,
|
| 89 |
+
testcaseName?: string
|
| 90 |
+
): Promise<GradioApp> {
|
| 91 |
+
// Partition ports by worker index to avoid collisions
|
| 92 |
+
const basePort = 7860 + workerIndex * 100;
|
| 93 |
+
const port = await findFreePort(basePort, basePort + 99);
|
| 94 |
+
|
| 95 |
+
// Get the path to the demo file
|
| 96 |
+
const demoFilePath = getDemoFilePath(demoName, testcaseName);
|
| 97 |
+
|
| 98 |
+
// Create unique directories for this instance to avoid cache conflicts
|
| 99 |
+
const instanceId = testcaseName
|
| 100 |
+
? `${demoName}_${testcaseName}_${port}`
|
| 101 |
+
: `${demoName}_${port}`;
|
| 102 |
+
const instanceDir = path.join(os.tmpdir(), `gradio_test_${instanceId}`);
|
| 103 |
+
const cacheDir = path.join(instanceDir, "cached_examples");
|
| 104 |
+
const tempDir = path.join(instanceDir, "temp");
|
| 105 |
+
|
| 106 |
+
if (!fs.existsSync(instanceDir)) {
|
| 107 |
+
fs.mkdirSync(instanceDir, { recursive: true });
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Run the demo file directly - the demo's own if __name__ == "__main__" block
|
| 111 |
+
// has the correct launch parameters (show_error, etc.)
|
| 112 |
+
const childProcess = spawn("python", [demoFilePath], {
|
| 113 |
+
stdio: "pipe",
|
| 114 |
+
cwd: ROOT_DIR,
|
| 115 |
+
env: {
|
| 116 |
+
...process.env,
|
| 117 |
+
PYTHONUNBUFFERED: "true",
|
| 118 |
+
GRADIO_ANALYTICS_ENABLED: "False",
|
| 119 |
+
GRADIO_IS_E2E_TEST: "1",
|
| 120 |
+
GRADIO_RESET_EXAMPLES_CACHE: "True",
|
| 121 |
+
// Control the port via environment variable
|
| 122 |
+
GRADIO_SERVER_PORT: port.toString(),
|
| 123 |
+
// Use unique directories per instance to avoid conflicts
|
| 124 |
+
GRADIO_EXAMPLES_CACHE: cacheDir,
|
| 125 |
+
GRADIO_TEMP_DIR: tempDir
|
| 126 |
+
}
|
| 127 |
+
});
|
| 128 |
+
|
| 129 |
+
childProcess.stdout?.setEncoding("utf8");
|
| 130 |
+
childProcess.stderr?.setEncoding("utf8");
|
| 131 |
+
|
| 132 |
+
// Wait for app to be ready
|
| 133 |
+
return new Promise<GradioApp>((resolve, reject) => {
|
| 134 |
+
const timeoutId = setTimeout(() => {
|
| 135 |
+
killGradioApp(childProcess);
|
| 136 |
+
reject(
|
| 137 |
+
new Error(`Gradio app ${demoName} failed to start within ${timeout}ms`)
|
| 138 |
+
);
|
| 139 |
+
}, timeout);
|
| 140 |
+
|
| 141 |
+
let output = "";
|
| 142 |
+
|
| 143 |
+
function handleOutput(data: string): void {
|
| 144 |
+
output += data;
|
| 145 |
+
// Check for Gradio's startup message
|
| 146 |
+
if (
|
| 147 |
+
data.includes("Running on local URL:") ||
|
| 148 |
+
data.includes(`Uvicorn running on`)
|
| 149 |
+
) {
|
| 150 |
+
clearTimeout(timeoutId);
|
| 151 |
+
resolve({ port, process: childProcess });
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
childProcess.stdout?.on("data", handleOutput);
|
| 156 |
+
childProcess.stderr?.on("data", handleOutput);
|
| 157 |
+
|
| 158 |
+
childProcess.on("error", (err) => {
|
| 159 |
+
clearTimeout(timeoutId);
|
| 160 |
+
reject(err);
|
| 161 |
+
});
|
| 162 |
+
|
| 163 |
+
childProcess.on("exit", (code) => {
|
| 164 |
+
if (code !== 0 && code !== null) {
|
| 165 |
+
clearTimeout(timeoutId);
|
| 166 |
+
reject(
|
| 167 |
+
new Error(
|
| 168 |
+
`Gradio app ${demoName} exited with code ${code}.\nOutput: ${output}`
|
| 169 |
+
)
|
| 170 |
+
);
|
| 171 |
+
}
|
| 172 |
+
});
|
| 173 |
+
});
|
| 174 |
+
}
|
6.5.0/tootils/src/index.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { test as base, type Locator, type Page } from "@playwright/test";
|
| 2 |
+
import { spy } from "tinyspy";
|
| 3 |
+
import url from "url";
|
| 4 |
+
import path from "path";
|
| 5 |
+
import fsPromises from "fs/promises";
|
| 6 |
+
import type { ChildProcess } from "node:child_process";
|
| 7 |
+
|
| 8 |
+
import type { SvelteComponent } from "svelte";
|
| 9 |
+
import type { SpyFn } from "tinyspy";
|
| 10 |
+
|
| 11 |
+
import { launchGradioApp, killGradioApp, hasTestcase } from "./app-launcher";
|
| 12 |
+
|
| 13 |
+
export function get_text<T extends HTMLElement>(el: T): string {
|
| 14 |
+
return el.innerText.trim();
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
export function wait(n: number): Promise<void> {
|
| 18 |
+
return new Promise((r) => setTimeout(r, n));
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
const ROOT_DIR = path.resolve(
|
| 22 |
+
url.fileURLToPath(import.meta.url),
|
| 23 |
+
"../../../.."
|
| 24 |
+
);
|
| 25 |
+
|
| 26 |
+
// Extract testcase name from test title if present
|
| 27 |
+
// Test titles can be:
|
| 28 |
+
// - "case eager_caching_examples: ..."
|
| 29 |
+
// - "test case multimodal_messages chatinterface works..."
|
| 30 |
+
function extractTestcaseFromTitle(
|
| 31 |
+
title: string,
|
| 32 |
+
demoName: string
|
| 33 |
+
): string | undefined {
|
| 34 |
+
// Try pattern: "case <name>:" or "test case <name> "
|
| 35 |
+
const patterns = [/^case\s+(\w+):/, /^test case\s+(\w+)\s/];
|
| 36 |
+
|
| 37 |
+
for (const pattern of patterns) {
|
| 38 |
+
const match = title.match(pattern);
|
| 39 |
+
if (match) {
|
| 40 |
+
const caseName = match[1];
|
| 41 |
+
// Check if this is a testcase (not the main demo)
|
| 42 |
+
if (hasTestcase(demoName, caseName)) {
|
| 43 |
+
return caseName;
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
return undefined;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Cache for launched apps - key is "demoName" or "demoName_testcaseName"
|
| 51 |
+
const appCache = new Map<
|
| 52 |
+
string,
|
| 53 |
+
{ port: number; process: ChildProcess; refCount: number }
|
| 54 |
+
>();
|
| 55 |
+
|
| 56 |
+
// Test fixture that launches Gradio app per test
|
| 57 |
+
const test_normal = base.extend<{ setup: void }>({
|
| 58 |
+
setup: [
|
| 59 |
+
async ({ page }, use, testInfo): Promise<void> => {
|
| 60 |
+
const { file, title } = testInfo;
|
| 61 |
+
const demoName = path.basename(file, ".spec.ts");
|
| 62 |
+
|
| 63 |
+
// Check if this is a reload test (they manage their own apps)
|
| 64 |
+
if (demoName.endsWith(".reload")) {
|
| 65 |
+
// For reload tests, don't launch an app - they handle it themselves
|
| 66 |
+
await use();
|
| 67 |
+
return;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// Check if this test is for a specific testcase
|
| 71 |
+
const testcaseName = extractTestcaseFromTitle(title, demoName);
|
| 72 |
+
|
| 73 |
+
// Cache key includes testcase if present
|
| 74 |
+
const cacheKey = testcaseName ? `${demoName}_${testcaseName}` : demoName;
|
| 75 |
+
|
| 76 |
+
let appInfo = appCache.get(cacheKey);
|
| 77 |
+
|
| 78 |
+
if (!appInfo) {
|
| 79 |
+
// Launch the app for this test
|
| 80 |
+
const workerIndex = testInfo.workerIndex;
|
| 81 |
+
try {
|
| 82 |
+
const { port, process } = await launchGradioApp(
|
| 83 |
+
demoName,
|
| 84 |
+
workerIndex,
|
| 85 |
+
60000,
|
| 86 |
+
testcaseName
|
| 87 |
+
);
|
| 88 |
+
appInfo = { port, process, refCount: 0 };
|
| 89 |
+
appCache.set(cacheKey, appInfo);
|
| 90 |
+
} catch (error) {
|
| 91 |
+
console.error(`Failed to launch app for ${cacheKey}:`, error);
|
| 92 |
+
throw error;
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
appInfo.refCount++;
|
| 97 |
+
|
| 98 |
+
// Navigate to the app
|
| 99 |
+
await page.goto(`http://localhost:${appInfo.port}`);
|
| 100 |
+
|
| 101 |
+
if (
|
| 102 |
+
process.env?.GRADIO_SSR_MODE?.toLowerCase() === "true" &&
|
| 103 |
+
!(
|
| 104 |
+
demoName.includes("multipage") ||
|
| 105 |
+
demoName.includes("chatinterface_deep_link")
|
| 106 |
+
)
|
| 107 |
+
) {
|
| 108 |
+
await page.waitForSelector("#svelte-announcer");
|
| 109 |
+
}
|
| 110 |
+
await page.waitForLoadState("load");
|
| 111 |
+
|
| 112 |
+
await use();
|
| 113 |
+
|
| 114 |
+
// Decrement ref count
|
| 115 |
+
appInfo.refCount--;
|
| 116 |
+
|
| 117 |
+
// Note: We don't kill the app here because other tests might
|
| 118 |
+
// still need it. The app will be killed when the process exits.
|
| 119 |
+
},
|
| 120 |
+
{ auto: true }
|
| 121 |
+
]
|
| 122 |
+
});
|
| 123 |
+
|
| 124 |
+
// Cleanup apps when the process exits
|
| 125 |
+
process.on("exit", () => {
|
| 126 |
+
for (const [, appInfo] of appCache) {
|
| 127 |
+
killGradioApp(appInfo.process);
|
| 128 |
+
}
|
| 129 |
+
});
|
| 130 |
+
|
| 131 |
+
process.on("SIGINT", () => {
|
| 132 |
+
for (const [, appInfo] of appCache) {
|
| 133 |
+
killGradioApp(appInfo.process);
|
| 134 |
+
}
|
| 135 |
+
process.exit(0);
|
| 136 |
+
});
|
| 137 |
+
|
| 138 |
+
process.on("SIGTERM", () => {
|
| 139 |
+
for (const [, appInfo] of appCache) {
|
| 140 |
+
killGradioApp(appInfo.process);
|
| 141 |
+
}
|
| 142 |
+
process.exit(0);
|
| 143 |
+
});
|
| 144 |
+
|
| 145 |
+
export const test = test_normal;
|
| 146 |
+
|
| 147 |
+
export async function wait_for_event(
|
| 148 |
+
component: SvelteComponent,
|
| 149 |
+
event: string
|
| 150 |
+
): Promise<SpyFn> {
|
| 151 |
+
const mock = spy();
|
| 152 |
+
return new Promise((res) => {
|
| 153 |
+
component.$on(event, () => {
|
| 154 |
+
mock();
|
| 155 |
+
res(mock);
|
| 156 |
+
});
|
| 157 |
+
});
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
export interface ActionReturn<
|
| 161 |
+
Parameter = never,
|
| 162 |
+
Attributes extends Record<string, any> = Record<never, any>
|
| 163 |
+
> {
|
| 164 |
+
update?: [Parameter] extends [never] ? never : (parameter: Parameter) => void;
|
| 165 |
+
destroy?: () => void;
|
| 166 |
+
/**
|
| 167 |
+
* ### DO NOT USE THIS
|
| 168 |
+
* This exists solely for type-checking and has no effect at runtime.
|
| 169 |
+
* Set this through the `Attributes` generic instead.
|
| 170 |
+
*/
|
| 171 |
+
$$_attributes?: Attributes;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
export { expect } from "@playwright/test";
|
| 175 |
+
export * from "./render";
|
| 176 |
+
|
| 177 |
+
export const drag_and_drop_file = async (
|
| 178 |
+
page: Page,
|
| 179 |
+
selector: string | Locator,
|
| 180 |
+
filePath: string,
|
| 181 |
+
fileName: string,
|
| 182 |
+
fileType = "",
|
| 183 |
+
count = 1
|
| 184 |
+
): Promise<void> => {
|
| 185 |
+
const buffer = (await fsPromises.readFile(filePath)).toString("base64");
|
| 186 |
+
|
| 187 |
+
const dataTransfer = await page.evaluateHandle(
|
| 188 |
+
async ({ bufferData, localFileName, localFileType, count }) => {
|
| 189 |
+
const dt = new DataTransfer();
|
| 190 |
+
|
| 191 |
+
const blobData = await fetch(bufferData).then((res) => res.blob());
|
| 192 |
+
|
| 193 |
+
const file = new File([blobData], localFileName, {
|
| 194 |
+
type: localFileType
|
| 195 |
+
});
|
| 196 |
+
|
| 197 |
+
for (let i = 0; i < count; i++) {
|
| 198 |
+
dt.items.add(file);
|
| 199 |
+
}
|
| 200 |
+
return dt;
|
| 201 |
+
},
|
| 202 |
+
{
|
| 203 |
+
bufferData: `data:application/octet-stream;base64,${buffer}`,
|
| 204 |
+
localFileName: fileName,
|
| 205 |
+
localFileType: fileType,
|
| 206 |
+
count
|
| 207 |
+
}
|
| 208 |
+
);
|
| 209 |
+
|
| 210 |
+
if (typeof selector === "string") {
|
| 211 |
+
await page.dispatchEvent(selector, "drop", { dataTransfer });
|
| 212 |
+
} else {
|
| 213 |
+
await selector.dispatchEvent("drop", { dataTransfer });
|
| 214 |
+
}
|
| 215 |
+
};
|
| 216 |
+
|
| 217 |
+
export async function go_to_testcase(
|
| 218 |
+
page: Page,
|
| 219 |
+
_test_case: string
|
| 220 |
+
): Promise<void> {
|
| 221 |
+
// With the new setup, each testcase launches its own Gradio app.
|
| 222 |
+
// The fixture detects the testcase from the test title and launches
|
| 223 |
+
// the correct app, so this function is now a no-op.
|
| 224 |
+
// The page is already at the correct testcase app.
|
| 225 |
+
}
|
6.5.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 |
+
}
|