test: dedupe and optimize test suites

This commit is contained in:
Peter Steinberger
2026-02-19 15:18:50 +00:00
parent b0e55283d5
commit a1cb700a05
80 changed files with 2627 additions and 2962 deletions

View File

@@ -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: {

View File

@@ -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();