Files
SnapOtter/tests/unit/ai/outpaint.test.ts
T
SnapOtter 3b181dd1ac test: expand test coverage across unit, integration, e2e, and e2e-docker suites
Add ~210 new tests filling gaps identified by a comprehensive 14-agent
coverage audit. Unit+integration tests go from 9,388 to 9,484 (all passing).

Unit tests (+36):
- AI bridge: OOM fallback path, custom tier option
- Web lib: api-errors, format date/datetime, tool-i18n coverage

Integration tests (+19):
- Format matrix: ai-canvas-expand and find-duplicates added to cross-format matrix
- Adversarial: SVG XXE attacks, SQL injection in settings, request body size
  limits, race conditions with identical filenames

E2E Docker (+3):
- ai-canvas-expand tool coverage with HEIC input and edge cases

E2E GUI (~150+):
- Navigation: login rate limiting, ai-canvas-expand in parameterized list
- Responsive: dropzone visibility, text readability, dialog bounds at all viewports
- Keyboard: shortcuts verified from automate, files, tool, and fullscreen pages
- Tool UI: undo/state-reset for 16 tools, crop canvas drag handles, rotate/border
  live preview, linked aspect-ratio inputs for resize
- Batch: per-image undo isolation, batch compress/convert/rotate (not just resize)
- Pipeline: tool palette search, step collapse/expand visibility
- Settings: audit log entry verification, system settings persistence, teams CRUD,
  role permission toggling
- RBAC: user/editor 403 on roles/teams endpoints, privilege escalation prevention,
  cross-role tab parity documented as intentional
- Accessibility: skip-to-content link (WCAG 2.4.1), comprehensive color contrast
  for all headings/body/buttons in both themes with DOM-walking background detection
- Resilience: auth expiry 401 redirect, rate limit 429 handling
- Performance: JS heap memory stability for tool navigation, dialog cycling,
  upload/clear cycles, rapid page navigation
2026-05-15 21:35:02 +08:00

363 lines
10 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("sharp", () => {
const mockSharp = vi.fn(() => ({
png: vi.fn().mockReturnThis(),
toBuffer: vi.fn().mockResolvedValue(Buffer.from("mock-png-data")),
}));
return { default: mockSharp };
});
vi.mock("node:fs/promises", () => ({
readFile: vi.fn().mockResolvedValue(Buffer.from("mock-output-data")),
writeFile: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("../../../packages/ai/src/bridge.js", () => ({
runPythonWithProgress: vi.fn(),
parseStdoutJson: vi.fn(),
}));
import sharp from "sharp";
import { parseStdoutJson, runPythonWithProgress } from "../../../packages/ai/src/bridge.js";
import { type OutpaintOptions, outpaint } from "../../../packages/ai/src/outpainting.js";
const FAKE_INPUT = Buffer.from("fake-image-data");
const FAKE_OUTPUT_DIR = "/tmp/test-outpaint";
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(readFile).mockResolvedValue(Buffer.from("mock-output-data"));
vi.mocked(writeFile).mockResolvedValue(undefined);
vi.mocked(runPythonWithProgress).mockResolvedValue({
stdout: '{"success": true}',
stderr: "",
});
vi.mocked(parseStdoutJson).mockReturnValue({ success: true });
vi.mocked(sharp).mockImplementation(
() =>
({
png: vi.fn().mockReturnThis(),
toBuffer: vi.fn().mockResolvedValue(Buffer.from("mock-png-data")),
}) as unknown as ReturnType<typeof sharp>,
);
});
afterEach(() => {
vi.restoreAllMocks();
});
describe("outpaint", () => {
describe("module exports", () => {
it("exports outpaint as a function", () => {
expect(typeof outpaint).toBe("function");
});
it("accepts a valid OutpaintOptions object", () => {
const options: OutpaintOptions = {
extendTop: 100,
extendRight: 50,
extendBottom: 100,
extendLeft: 50,
};
expect(options.extendTop).toBe(100);
expect(options.extendRight).toBe(50);
expect(options.extendBottom).toBe(100);
expect(options.extendLeft).toBe(50);
});
});
describe("request serialization", () => {
it("calls outpaint.py with input, output, and extension args", async () => {
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 20,
extendBottom: 30,
extendLeft: 40,
};
await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR);
expect(runPythonWithProgress).toHaveBeenCalledWith(
"outpaint.py",
[
`${FAKE_OUTPUT_DIR}/input_outpaint.png`,
`${FAKE_OUTPUT_DIR}/output_outpaint.png`,
"10",
"20",
"30",
"40",
"balanced",
],
expect.any(Object),
);
});
it("passes custom tier value instead of default 'balanced'", async () => {
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 20,
extendBottom: 30,
extendLeft: 40,
tier: "high",
};
await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR);
expect(runPythonWithProgress).toHaveBeenCalledWith(
"outpaint.py",
[
`${FAKE_OUTPUT_DIR}/input_outpaint.png`,
`${FAKE_OUTPUT_DIR}/output_outpaint.png`,
"10",
"20",
"30",
"40",
"high",
],
expect.any(Object),
);
});
it("passes 'fast' tier value", async () => {
const options: OutpaintOptions = {
extendTop: 5,
extendRight: 5,
extendBottom: 5,
extendLeft: 5,
tier: "fast",
};
await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR);
const args = vi.mocked(runPythonWithProgress).mock.calls[0][1];
expect(args[6]).toBe("fast");
});
it("converts input to PNG via sharp", async () => {
const options: OutpaintOptions = {
extendTop: 0,
extendRight: 0,
extendBottom: 0,
extendLeft: 0,
};
await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR);
expect(sharp).toHaveBeenCalledTimes(1);
expect(sharp).toHaveBeenCalledWith(FAKE_INPUT);
});
it("writes input file to disk", async () => {
const options: OutpaintOptions = {
extendTop: 0,
extendRight: 0,
extendBottom: 0,
extendLeft: 0,
};
await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR);
expect(writeFile).toHaveBeenCalledWith(
`${FAKE_OUTPUT_DIR}/input_outpaint.png`,
Buffer.from("mock-png-data"),
);
});
});
describe("response parsing", () => {
it("returns output buffer on success", async () => {
const outpaintedBuf = Buffer.from("outpainted-result");
vi.mocked(readFile).mockResolvedValueOnce(outpaintedBuf);
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
const result = await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR);
expect(result).toBe(outpaintedBuf);
});
it("reads from the correct output path", async () => {
const options: OutpaintOptions = {
extendTop: 0,
extendRight: 0,
extendBottom: 0,
extendLeft: 0,
};
await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR);
expect(readFile).toHaveBeenCalledWith(`${FAKE_OUTPUT_DIR}/output_outpaint.png`);
});
});
describe("error handling", () => {
it("throws with custom error from Python", async () => {
vi.mocked(parseStdoutJson).mockReturnValue({
success: false,
error: "Model not loaded",
});
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await expect(outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR)).rejects.toThrow(
"Model not loaded",
);
});
it("throws fallback error when success: false without error string", async () => {
vi.mocked(parseStdoutJson).mockReturnValue({ success: false });
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await expect(outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR)).rejects.toThrow(
"Outpainting failed",
);
});
it("propagates bridge timeout", async () => {
vi.mocked(runPythonWithProgress).mockRejectedValue(new Error("Python script timed out"));
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await expect(outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR)).rejects.toThrow("timed out");
});
it("propagates bridge OOM", async () => {
vi.mocked(runPythonWithProgress).mockRejectedValue(
new Error("Process killed (out of memory)"),
);
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await expect(outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR)).rejects.toThrow("out of memory");
});
it("propagates parseStdoutJson errors", async () => {
vi.mocked(parseStdoutJson).mockImplementation(() => {
throw new Error("No JSON response from Python script");
});
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await expect(outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR)).rejects.toThrow(
"No JSON response from Python script",
);
});
it("propagates sharp conversion error", async () => {
vi.mocked(sharp).mockImplementation(
() =>
({
png: vi.fn().mockReturnThis(),
toBuffer: vi.fn().mockRejectedValue(new Error("Corrupt input image")),
}) as unknown as ReturnType<typeof sharp>,
);
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await expect(outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR)).rejects.toThrow(
"Corrupt input image",
);
});
});
describe("onProgress forwarding", () => {
it("passes onProgress to bridge", async () => {
const onProgress = vi.fn();
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR, onProgress);
expect(runPythonWithProgress).toHaveBeenCalledWith(
"outpaint.py",
expect.any(Array),
expect.objectContaining({ onProgress }),
);
});
it("omits onProgress when not provided", async () => {
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR);
const bridgeOptions = vi.mocked(runPythonWithProgress).mock.calls[0][2];
expect(bridgeOptions.onProgress).toBeUndefined();
});
});
describe("edge cases", () => {
it("handles zero extension on all sides", async () => {
const options: OutpaintOptions = {
extendTop: 0,
extendRight: 0,
extendBottom: 0,
extendLeft: 0,
};
await outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR);
expect(runPythonWithProgress).toHaveBeenCalledWith(
"outpaint.py",
expect.arrayContaining(["0", "0", "0", "0"]),
expect.any(Object),
);
});
it("propagates writeFile error", async () => {
vi.mocked(writeFile).mockRejectedValueOnce(new Error("ENOSPC: disk full"));
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await expect(outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR)).rejects.toThrow("disk full");
});
it("propagates readFile error after successful Python run", async () => {
vi.mocked(readFile).mockRejectedValueOnce(new Error("ENOENT: output missing"));
const options: OutpaintOptions = {
extendTop: 10,
extendRight: 10,
extendBottom: 10,
extendLeft: 10,
};
await expect(outpaint(FAKE_INPUT, options, FAKE_OUTPUT_DIR)).rejects.toThrow(
"output missing",
);
});
});
});