Files
sass-mohfarawati/tests/portfolio-storage.test.ts
Moh e2e06be86e test: add comprehensive automated test coverage
- Vitest multi-project setup (unit / integration / component)
- Real-Postgres integration harness via in-process PGlite (TEST_DATABASE_URL
  override), migrations applied per worker; production code untouched
- Unit: routing, locale, validation/Zod schemas, metadata, mail, site-theme,
  marquee, media, portfolio helpers, plus architecture-rule tests
- Integration: Prisma data layer, API routes, and all server actions
- Component: UI primitives and form components (jsdom + Testing Library)
- 371 tests passing
2026-08-06 02:27:20 +02:00

56 lines
1.9 KiB
TypeScript

import { mkdir, stat, writeFile } from "fs/promises";
import path from "path";
import { afterEach, describe, expect, it } from "vitest";
import {
MEDIA_UPLOAD_ROOT,
isManagedMediaFilePath,
removeManagedMediaFile,
resolveMediaUploadPath,
sanitizeBaseName,
} from "../lib/media-storage";
import { canManageUploads } from "./helpers/fs-capability";
const createdFiles: string[] = [];
afterEach(async () => {
await Promise.all(
createdFiles.splice(0).map(async (filePath) => {
await removeManagedMediaFile(filePath);
}),
);
});
describe("media storage helpers", () => {
it("sanitizes upload names safely", () => {
expect(sanitizeBaseName("Brand Redesign 2026!.svg")).toBe("brand-redesign-2026-svg");
});
it("detects managed media upload paths", () => {
expect(isManagedMediaFilePath("/uploads/media/covers/test.svg")).toBe(true);
expect(isManagedMediaFilePath("https://example.com/test.svg")).toBe(false);
expect(isManagedMediaFilePath("../test.svg")).toBe(false);
});
it("resolves managed paths inside the upload root", () => {
const resolvedPath = resolveMediaUploadPath("/uploads/media/assets/test.svg");
expect(resolvedPath.startsWith(MEDIA_UPLOAD_ROOT)).toBe(true);
expect(resolvedPath.endsWith(path.join("assets", "test.svg"))).toBe(true);
});
it.skipIf(!canManageUploads)("removes a managed file from disk", async () => {
const relativePath = `/uploads/media/tests/${Date.now()}-temp.txt`;
const absolutePath = resolveMediaUploadPath(relativePath);
await mkdir(path.dirname(absolutePath), { recursive: true });
await writeFile(absolutePath, "temporary-test-file", "utf8");
createdFiles.push(relativePath);
await expect(stat(absolutePath)).resolves.toBeDefined();
await expect(removeManagedMediaFile(relativePath)).resolves.toBe(true);
await expect(stat(absolutePath)).rejects.toThrow();
});
});