mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
test: coverage campaign and mutation testing across five packages (#628)
Coverage 83.6 to 87.36% lines, 81.63 to 84.14% branches. Mutation testing across five packages: image-engine 85, media-engine 92, doc-engine 87, shared+enterprise 86, apps/api security and jobs slice. Runs all five lanes weekly. Fixes the silently-broken mutation CI (babel pin), a redact-pdf envelope-shape test bug, an untested enterprise license valid-signature path, and an audit test that only exercised a hand-copied reproduction. Test and config only, no product code changes beyond the babel pin and one test-only oidc export. Full suite: 16,712 pass, 0 fail.
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { Readable } from "node:stream";
|
||||
import { afterAll, describe, expect, it } from "vitest";
|
||||
import { env } from "../../../apps/api/src/config.js";
|
||||
import {
|
||||
computeWorkspaceUsedBytes,
|
||||
copyObjectToFile,
|
||||
copyReadableToFile,
|
||||
deleteObject,
|
||||
deletePrefix,
|
||||
getObjectBuffer,
|
||||
getObjectSize,
|
||||
getObjectStream,
|
||||
listJobDirs,
|
||||
listObjects,
|
||||
objectExists,
|
||||
putObject,
|
||||
@@ -124,4 +131,175 @@ describe("object-storage (local backend)", () => {
|
||||
);
|
||||
expect(readFileSync(destination)).toEqual(source);
|
||||
});
|
||||
|
||||
it("reads a stored object back as a single concatenated buffer", async () => {
|
||||
await putObject(key, Buffer.from("hello world"));
|
||||
const buf = await getObjectBuffer(key);
|
||||
expect(buf).toBeInstanceOf(Buffer);
|
||||
expect(buf.toString()).toBe("hello world");
|
||||
});
|
||||
|
||||
it("rejects copyObjectToFile with a negative maxBytes ceiling", async () => {
|
||||
await expect(
|
||||
copyObjectToFile(copyKey, join(copyDir, "never.bin"), { maxBytes: -1 }),
|
||||
).rejects.toThrow(/non-negative safe integer/);
|
||||
});
|
||||
|
||||
it("rejects copyObjectToFile with a non-safe-integer maxBytes ceiling", async () => {
|
||||
await expect(
|
||||
copyObjectToFile(copyKey, join(copyDir, "never.bin"), {
|
||||
maxBytes: Number.MAX_SAFE_INTEGER + 2,
|
||||
}),
|
||||
).rejects.toThrow(/non-negative safe integer/);
|
||||
});
|
||||
|
||||
it("spools an arbitrary readable straight to a destination file", async () => {
|
||||
const destination = join(copyDir, "readable.bin");
|
||||
const written = await copyReadableToFile(
|
||||
Readable.from([Buffer.from("ab"), Buffer.from("cd")]),
|
||||
destination,
|
||||
{ maxBytes: 10 },
|
||||
);
|
||||
expect(written).toBe(4);
|
||||
expect(readFileSync(destination).toString()).toBe("abcd");
|
||||
});
|
||||
|
||||
it("rejects copyReadableToFile with a negative maxBytes ceiling", async () => {
|
||||
await expect(
|
||||
copyReadableToFile(Readable.from([Buffer.from("x")]), join(copyDir, "never2.bin"), {
|
||||
maxBytes: -5,
|
||||
}),
|
||||
).rejects.toThrow(/non-negative safe integer/);
|
||||
});
|
||||
|
||||
it("enforces the byte cap while streaming a readable to a file and cleans up", async () => {
|
||||
const destination = join(copyDir, "readable-capped.bin");
|
||||
await expect(
|
||||
copyReadableToFile(Readable.from([Buffer.alloc(16, 1)]), destination, { maxBytes: 8 }),
|
||||
).rejects.toMatchObject({ statusCode: 413 });
|
||||
expect(existsSync(destination)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("object-storage local prefix + listing operations", () => {
|
||||
// These operate on the real per-fork WORKSPACE_PATH (STORAGE_MODE defaults to
|
||||
// "local"), so use a job id unique to this file to avoid colliding with the
|
||||
// round-trip suite above.
|
||||
const jobId = `listjob-${process.pid}`;
|
||||
const uploadDir = join(env.WORKSPACE_PATH, "uploads", jobId);
|
||||
const outputDir = join(env.WORKSPACE_PATH, "outputs", jobId);
|
||||
|
||||
afterAll(async () => {
|
||||
await deletePrefix(`uploads/${jobId}`).catch(() => {});
|
||||
await deletePrefix(`outputs/${jobId}`).catch(() => {});
|
||||
});
|
||||
|
||||
it("lists only files under a prefix with their sizes and mtimes", async () => {
|
||||
await mkdir(uploadDir, { recursive: true });
|
||||
await writeFile(join(uploadDir, "a.txt"), Buffer.from("hi"));
|
||||
await writeFile(join(uploadDir, "b.txt"), Buffer.from("world!"));
|
||||
// A nested directory must be ignored: listObjects only reports files.
|
||||
await mkdir(join(uploadDir, "nested"), { recursive: true });
|
||||
|
||||
const listed = await listObjects(`uploads/${jobId}`);
|
||||
const byKey = new Map(listed.map((o) => [o.key, o]));
|
||||
expect(byKey.get(`uploads/${jobId}/a.txt`)?.size).toBe(2);
|
||||
expect(byKey.get(`uploads/${jobId}/b.txt`)?.size).toBe(6);
|
||||
expect(byKey.has(`uploads/${jobId}/nested`)).toBe(false);
|
||||
for (const o of listed) expect(o.mtimeMs).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("returns an empty list for a prefix whose directory does not exist", async () => {
|
||||
expect(await listObjects(`outputs/missing-${process.pid}`)).toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects listObjects for a prefix outside the allowed roots", async () => {
|
||||
await expect(listObjects("secrets/job-1")).rejects.toThrow(/invalid prefix/i);
|
||||
});
|
||||
|
||||
it("rejects listObjects for a prefix containing a parent traversal", async () => {
|
||||
await expect(listObjects("uploads/..")).rejects.toThrow(/invalid prefix/i);
|
||||
});
|
||||
|
||||
it("lists top-level job directories under a prefix, ignoring stray files", async () => {
|
||||
await mkdir(outputDir, { recursive: true });
|
||||
await writeFile(join(outputDir, "result.bin"), Buffer.from("done"));
|
||||
// A file directly under outputs/ must not be reported as a job dir.
|
||||
const strayFile = join(env.WORKSPACE_PATH, "outputs", `stray-${process.pid}.txt`);
|
||||
await mkdir(join(env.WORKSPACE_PATH, "outputs"), { recursive: true });
|
||||
await writeFile(strayFile, Buffer.from("x"));
|
||||
|
||||
try {
|
||||
const dirs = await listJobDirs("outputs");
|
||||
const keys = dirs.map((d) => d.key);
|
||||
expect(keys).toContain(`outputs/${jobId}`);
|
||||
expect(keys).not.toContain(`outputs/stray-${process.pid}.txt`);
|
||||
const entry = dirs.find((d) => d.key === `outputs/${jobId}`);
|
||||
expect(entry?.size).toBe(0);
|
||||
expect(entry?.mtimeMs).toBeGreaterThan(0);
|
||||
} finally {
|
||||
rmSync(strayFile, { force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("returns an empty job-dir list when the prefix root is absent", async () => {
|
||||
// Point listJobDirs at a prefix root that has never been created. uploads/
|
||||
// may exist from the listing test, so isolate via a throwaway workspace.
|
||||
const originalWorkspace = env.WORKSPACE_PATH;
|
||||
const emptyRoot = mkdtempSync(join(tmpdir(), "snapotter-empty-ws-"));
|
||||
(env as { WORKSPACE_PATH: string }).WORKSPACE_PATH = emptyRoot;
|
||||
try {
|
||||
expect(await listJobDirs("uploads")).toEqual([]);
|
||||
} finally {
|
||||
(env as { WORKSPACE_PATH: string }).WORKSPACE_PATH = originalWorkspace;
|
||||
rmSync(emptyRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("removes an entire job prefix from local storage", async () => {
|
||||
await mkdir(uploadDir, { recursive: true });
|
||||
await writeFile(join(uploadDir, "doomed.bin"), Buffer.from("bye"));
|
||||
expect(existsSync(uploadDir)).toBe(true);
|
||||
|
||||
await deletePrefix(`uploads/${jobId}`);
|
||||
expect(existsSync(uploadDir)).toBe(false);
|
||||
});
|
||||
|
||||
it("treats deleting a non-existent prefix as a no-op (force removal)", async () => {
|
||||
await expect(deletePrefix(`uploads/gone-${process.pid}`)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("rejects deletePrefix for a prefix outside the allowed roots", async () => {
|
||||
await expect(deletePrefix("etc/passwd")).rejects.toThrow(/invalid prefix/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe("object-storage missing-key + robustness paths", () => {
|
||||
it("reports a missing object as non-existent instead of throwing", async () => {
|
||||
expect(await objectExists(`outputs/absent-${process.pid}/none.bin`)).toBe(false);
|
||||
});
|
||||
|
||||
it("propagates a stat error as a rejection from getObjectSize on a missing key", async () => {
|
||||
await expect(getObjectSize(`outputs/absent-${process.pid}/none.bin`)).rejects.toBeDefined();
|
||||
});
|
||||
|
||||
it("silently ignores deleting an object that is not present", async () => {
|
||||
await expect(deleteObject(`outputs/absent-${process.pid}/none.bin`)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("skips a workspace entry that is a file where a job directory was expected", async () => {
|
||||
// computeWorkspaceUsedBytes shallow-walks <root>/uploads/<jobDir>. When a
|
||||
// plain file sits directly under uploads/, readdir on it throws ENOTDIR and
|
||||
// the walk must `continue` past it, contributing 0.
|
||||
const root = mkdtempSync(join(tmpdir(), "snapotter-wscap-file-"));
|
||||
try {
|
||||
await mkdir(join(root, "uploads"), { recursive: true });
|
||||
await writeFile(join(root, "uploads", "not-a-dir"), Buffer.alloc(500));
|
||||
await mkdir(join(root, "uploads", "realjob"), { recursive: true });
|
||||
await writeFile(join(root, "uploads", "realjob", "f.bin"), Buffer.alloc(700));
|
||||
expect(await computeWorkspaceUsedBytes(root)).toBe(700);
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user