mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 06:47:26 +00:00
test: dedupe and optimize test suites
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { beforeAll, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const fetchWithSsrFGuardMock = vi.fn();
|
||||
|
||||
@@ -10,6 +10,15 @@ async function waitForMicrotaskTurn(): Promise<void> {
|
||||
await new Promise<void>((resolve) => queueMicrotask(resolve));
|
||||
}
|
||||
|
||||
let fetchWithGuard: typeof import("./input-files.js").fetchWithGuard;
|
||||
let extractImageContentFromSource: typeof import("./input-files.js").extractImageContentFromSource;
|
||||
let extractFileContentFromSource: typeof import("./input-files.js").extractFileContentFromSource;
|
||||
|
||||
beforeAll(async () => {
|
||||
({ fetchWithGuard, extractImageContentFromSource, extractFileContentFromSource } =
|
||||
await import("./input-files.js"));
|
||||
});
|
||||
|
||||
describe("fetchWithGuard", () => {
|
||||
it("rejects oversized streamed payloads and cancels the stream", async () => {
|
||||
let canceled = false;
|
||||
@@ -40,7 +49,6 @@ describe("fetchWithGuard", () => {
|
||||
finalUrl: "https://example.com/file.bin",
|
||||
});
|
||||
|
||||
const { fetchWithGuard } = await import("./input-files.js");
|
||||
await expect(
|
||||
fetchWithGuard({
|
||||
url: "https://example.com/file.bin",
|
||||
@@ -64,7 +72,6 @@ describe("base64 size guards", () => {
|
||||
kind: "images",
|
||||
expectedError: "Image too large",
|
||||
run: async (data: string) => {
|
||||
const { extractImageContentFromSource } = await import("./input-files.js");
|
||||
return await extractImageContentFromSource(
|
||||
{ type: "base64", data, mediaType: "image/png" },
|
||||
{
|
||||
@@ -81,7 +88,6 @@ describe("base64 size guards", () => {
|
||||
kind: "files",
|
||||
expectedError: "File too large",
|
||||
run: async (data: string) => {
|
||||
const { extractFileContentFromSource } = await import("./input-files.js");
|
||||
return await extractFileContentFromSource({
|
||||
source: { type: "base64", data, mediaType: "text/plain", filename: "x.txt" },
|
||||
limits: {
|
||||
|
||||
@@ -2,7 +2,6 @@ import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { PassThrough } from "node:stream";
|
||||
import JSZip from "jszip";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createPinnedLookup } from "../infra/net/ssrf.js";
|
||||
import { captureEnv } from "../test-utils/env.js";
|
||||
@@ -92,41 +91,6 @@ describe("media store redirects", () => {
|
||||
expect(await fs.readFile(saved.path, "utf8")).toBe("redirected");
|
||||
});
|
||||
|
||||
it("sniffs xlsx from zip content when headers and url extension are missing", async () => {
|
||||
mockRequest.mockImplementationOnce((_url, _opts, cb) => {
|
||||
const { req, res } = createMockHttpExchange();
|
||||
|
||||
res.statusCode = 200;
|
||||
res.headers = {};
|
||||
setImmediate(() => {
|
||||
cb(res as unknown);
|
||||
const zip = new JSZip();
|
||||
zip.file(
|
||||
"[Content_Types].xml",
|
||||
'<Types><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/></Types>',
|
||||
);
|
||||
zip.file("xl/workbook.xml", "<workbook/>");
|
||||
void zip
|
||||
.generateAsync({ type: "nodebuffer" })
|
||||
.then((buf) => {
|
||||
res.write(buf);
|
||||
res.end();
|
||||
})
|
||||
.catch((err) => {
|
||||
res.destroy(err);
|
||||
});
|
||||
});
|
||||
|
||||
return req;
|
||||
});
|
||||
|
||||
const saved = await saveMediaSource("https://example.com/download");
|
||||
expect(saved.contentType).toBe(
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
);
|
||||
expect(path.extname(saved.path)).toBe(".xlsx");
|
||||
});
|
||||
|
||||
it("fails when redirect response omits location header", async () => {
|
||||
mockRequest.mockImplementationOnce((_url, _opts, cb) => {
|
||||
const { req, res } = createMockHttpExchange();
|
||||
|
||||
Reference in New Issue
Block a user