Download api/src/misc/run-test.js from Dieiol919/cobalt: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/Dieiol919/cobalt/resolve/main/api/src/misc/run-test.js
- Command line
-
hf download hf://Dieiol919/cobalt/api/src/misc/run-test.js
-
curl -L -o run-test.js https://huggingface.co/Dieiol919/cobalt/resolve/main/api/src/misc/run-test.js
1.62 kB
| import { normalizeRequest } from "../processing/request.js"; | |
| import match from "../processing/match.js"; | |
| import { extract } from "../processing/url.js"; | |
| export async function runTest(url, params, expect) { | |
| const { success, data: normalized } = await normalizeRequest({ url, ...params }); | |
| if (!success) { | |
| throw "invalid request"; | |
| } | |
| const parsed = extract(normalized.url); | |
| if (parsed === null) { | |
| throw `invalid url: ${normalized.url}`; | |
| } | |
| const result = await match({ | |
| host: parsed.host, | |
| patternMatch: parsed.patternMatch, | |
| params: normalized, | |
| }); | |
| let error = []; | |
| if (expect.status !== result.body.status) { | |
| const detail = `${expect.status} (expected) != ${result.body.status} (actual)`; | |
| error.push(`status mismatch: ${detail}`); | |
| if (result.body.status === 'error') { | |
| error.push(`error code: ${result.body?.error?.code}`); | |
| } | |
| } | |
| if (expect.errorCode && expect.errorCode !== result.body?.error?.code) { | |
| const detail = `${expect.errorCode} (expected) != ${result.body.error.code} (actual)` | |
| error.push(`error mismatch: ${detail}`); | |
| } | |
| if (expect.code !== result.status) { | |
| const detail = `${expect.code} (expected) != ${result.status} (actual)`; | |
| error.push(`status code mismatch: ${detail}`); | |
| } | |
| if (error.length) { | |
| if (result.body.text) { | |
| error.push(`error message: ${result.body.text}`); | |
| } | |
| throw error.join('\n'); | |
| } | |
| if (result.body.status === 'tunnel') { | |
| // TODO: stream testing | |
| } | |
| } | |