Files
SnapOtter/packages/ai/src/noise-removal.ts
T
SnapOtter 4acec0846c test: add comprehensive AI feature install/uninstall test coverage
- Add 55 unit tests for feature-status.ts (installed.json CRUD, cache
  behavior, install lock, model verification, crash recovery, composite
  state) using real temp directories
- Add 36 integration tests for full install/uninstall lifecycle against
  Docker containers (face-detection bundle, SSE progress, tool gates,
  shared model protection, concurrent install prevention, auth guards,
  container restart recovery)
- Fix noise-removal CPU timeout by adding megapixel-based timeout
  calculation (120s/MP, min 5 minutes)
- Fix Playwright auth storage state race condition (mkdirSync before
  saving analytics-user.json)
- Fix 2 skipped tests in fixes-verification.spec.ts by replacing
  external ~/Downloads/sample dependency with existing test fixtures
- Enable skipped analytics-consent settings toggle test
- Restructure features.spec.ts to manage bundle state (uninstall/
  reinstall OCR) so 501 guard tests run instead of skipping
- Update noise-removal test mock to include sharp metadata() method
2026-04-28 13:27:54 +08:00

60 lines
1.6 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import sharp from "sharp";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface NoiseRemovalOptions {
tier?: string;
strength?: number;
detailPreservation?: number;
colorNoise?: number;
format?: string;
quality?: number;
}
export interface NoiseRemovalResult {
buffer: Buffer;
width: number;
height: number;
format: string;
tier: string;
}
export async function noiseRemoval(
inputBuffer: Buffer,
outputDir: string,
options: NoiseRemovalOptions = {},
onProgress?: ProgressCallback,
): Promise<NoiseRemovalResult> {
const inputPath = join(outputDir, "input_denoise.png");
const outputPath = join(outputDir, "output_denoise.png");
const pngBuffer = await sharp(inputBuffer).png().toBuffer();
await writeFile(inputPath, pngBuffer);
const meta = await sharp(pngBuffer).metadata();
const megapixels = ((meta.width ?? 0) * (meta.height ?? 0)) / 1_000_000;
const timeout = Math.max(300_000, megapixels * 120_000);
const { stdout } = await runPythonWithProgress(
"noise_removal.py",
[inputPath, outputPath, JSON.stringify(options)],
{ onProgress, timeout },
);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Noise removal failed");
}
const actualOutputPath = result.output_path || outputPath;
const buffer = await readFile(actualOutputPath);
return {
buffer,
width: result.width,
height: result.height,
format: result.format ?? "png",
tier: result.tier ?? options.tier ?? "balanced",
};
}