repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/leaks/__tests__/unit.test.ts

Raw
File outline
import { describe, expect, it } from "vitest";
import { __test } from "../index.ts";

const fixtureSecret = ["shell", "fixture", "secret", "value", "123456"].join(
	"-",
);

describe("leaks engine shell", () => {
	it("creates active engine with empty volatile state", () => {
		const engine = new __test.LeaksEngine();

		expect(engine.stats()).toMatchObject({
			active: true,
			customRegexCount: 0,
			learnedCount: 0,
			mappingCount: 0,
			lastProviderRedactionCount: 0,
			totalProviderRedactionCount: 0,
		});
		expect(engine.redactDeep(1, "provider")).toEqual({
			value: 1,
			changed: false,
			count: 0,
		});
		expect(engine.redactDeep(null, "provider")).toEqual({
			value: null,
			changed: false,
			count: 0,
		});
		const image = { type: "image", data: fixtureSecret };
		expect(engine.redactDeep(image, "provider").value).toEqual(image);
	});

	it("does not implement rejected compatibility settings", () => {
		expect(__test.RUNTIME_SETTING_KEYS).toEqual(["regex"]);
		for (const key of ["enabled", "keywords", "exclude", "secretFiles"]) {
			expect(__test.RUNTIME_SETTING_KEYS).not.toContain(key);
		}
	});

	it("fails open when a detector throws", () => {
		const engine = new __test.LeaksEngine({
			detectors: [
				() => {
					throw new Error("detector broke");
				},
			],
		});

		const result = engine.redactText(`send ${fixtureSecret}`, "provider");

		expect(result).toMatchObject({
			text: `send ${fixtureSecret}`,
			count: 0,
			labels: [],
			placeholders: [],
		});
		expect(engine.stats().warningCount).toBe(1);
	});

	it("keeps warning text secret-free", () => {
		const engine = new __test.LeaksEngine({
			detectors: [
				() => {
					throw new Error(`detector saw ${fixtureSecret}`);
				},
			],
		});

		engine.redactText(`send ${fixtureSecret}`, "provider");

		const warnings = engine.stats().warnings.map((warning) => warning.message);
		expect(warnings.join("\n")).not.toContain(fixtureSecret);
		expect(warnings).toEqual([
			"detector failure; redaction pass left unchanged",
		]);
	});
});