repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/leaks/__tests__/lifecycle.test.ts
RawFile outline
- fixture line 5
- detector line 7
- configuredEngine line 24
- leaks durable lifecycle line 32
- hydrates persisted mappings after a fresh engine reload line 33
- blocks unknown placeholders at exact recursive paths line 44
- hydrates and inspects property names without prototype mutation line 58
- blocks hydrated property-name collisions line 89
- blocks incomplete cyclic and unsupported traversal line 96
- allows shared acyclic objects line 108
- leaves provider values visible when mapping append fails line 116
- clear shadows old mappings with a new generation line 132
import { describe, expect, it } from "vitest";
import { __test } from "../index.ts";
import type { PersistedEntry } from "../src/placeholders.ts";
const fixture = "lifecycle-fixture-value-123456";
const detector = (text: string) => {
const start = text.indexOf(fixture);
return start < 0
? []
: [
{
start,
end: start + fixture.length,
value: fixture,
labels: [{ tag: "TOKEN", priority: 10, source: "custom" as const }],
kind: "custom" as const,
confidence: 100,
wholeSpan: false,
},
];
};
function configuredEngine(entries: PersistedEntry[] = []) {
const engine = new __test.LeaksEngine({ detectors: [detector] });
engine.configurePersistence(entries, (customType, data) =>
entries.push({ type: "custom", customType, data }),
);
return engine;
}
describe("leaks durable lifecycle", () => {
it("hydrates persisted mappings after a fresh engine reload", () => {
const entries: PersistedEntry[] = [];
const first = configuredEngine(entries);
const placeholder = first.redactText(fixture).text;
const resumed = configuredEngine(entries);
const result = resumed.hydrateToolInput({ nested: [`run ${placeholder}`] });
expect(result.issue).toBeUndefined();
expect(result.value).toEqual({ nested: [`run ${fixture}`] });
});
it("blocks unknown placeholders at exact recursive paths", () => {
const engine = configuredEngine();
const result = engine.hydrateToolInput({
args: ["__LEAK_TOKEN_abcdef123456__"],
});
expect(result.issue).toEqual({ path: "${0}[0]", label: "TOKEN" });
expect(
engine.hydrateToolInput(
"__LEAK_V1_TOKEN_1234567890abcdef1234567890abcdef__",
).issue?.label,
).toBe("TOKEN");
});
it("hydrates and inspects property names without prototype mutation", () => {
const entries: PersistedEntry[] = [];
const engine = configuredEngine(entries);
const redacted = engine.redactDeep({ [fixture]: "value" }).value as Record<
string,
string
>;
const placeholder = Object.keys(redacted)[0];
const hydrated = engine.hydrateToolInput(redacted);
expect(hydrated.issue).toBeUndefined();
expect(Object.keys(hydrated.value)).toEqual([fixture]);
const unknown = Object.create(null) as Record<string, string>;
Object.defineProperty(unknown, "__LEAK_TOKEN_abcdef123456__", {
value: "value",
enumerable: true,
});
expect(engine.hydrateToolInput(unknown).issue?.path).toContain(".key");
const proto = Object.create(null) as Record<string, string>;
Object.defineProperty(proto, "__proto__", {
value: placeholder,
enumerable: true,
});
const restored = engine.restoreDeep(proto).value;
expect(Object.hasOwn(restored, "__proto__")).toBe(true);
expect(Object.getOwnPropertyDescriptor(restored, "__proto__")?.value).toBe(
fixture,
);
});
it("blocks hydrated property-name collisions", () => {
const engine = configuredEngine();
const placeholder = engine.redactText(fixture).text;
const input = { [placeholder]: "first", [fixture]: "second" };
expect(engine.hydrateToolInput(input).issue?.label).toBe("KEY_COLLISION");
});
it("blocks incomplete cyclic and unsupported traversal", () => {
const engine = configuredEngine();
const cycle: unknown[] = [];
cycle.push(cycle);
expect(engine.hydrateToolInput(cycle).issue?.label).toBe("TRAVERSAL_CYCLE");
expect(engine.hydrateToolInput({ value: new Date() }).issue).toEqual({
path: "${0}",
label: "UNSUPPORTED_OBJECT",
});
});
it("allows shared acyclic objects", () => {
const engine = configuredEngine();
const shared = { value: "ordinary" };
expect(
engine.hydrateToolInput({ left: shared, right: shared }).issue,
).toBeUndefined();
});
it("leaves provider values visible when mapping append fails", () => {
const engine = new __test.LeaksEngine({ detectors: [detector] });
let calls = 0;
engine.configurePersistence([], () => {
calls++;
if (calls > 1) throw new Error("disk full");
});
const result = engine.redactText(fixture);
expect(result.text).toBe(fixture);
expect(result.count).toBe(0);
expect(engine.stats().warnings.map((warning) => warning.code)).toContain(
"placeholder.state",
);
});
it("clear shadows old mappings with a new generation", () => {
const entries: PersistedEntry[] = [];
const store = new __test.PlaceholderStore();
const append = (customType: string, data: unknown) =>
entries.push({ type: "custom" as const, customType, data });
store.configure(entries, append);
const oldPlaceholder = store.create(fixture, [
{ tag: "TOKEN", priority: 10, source: "custom" },
]) as string;
store.clear();
const newPlaceholder = store.create(`${fixture}-new`, [
{ tag: "TOKEN", priority: 10, source: "custom" },
]) as string;
const resumed = new __test.PlaceholderStore();
resumed.configure(entries, append);
expect(resumed.restore(oldPlaceholder)).toBe(oldPlaceholder);
expect(resumed.restore(newPlaceholder)).toBe(`${fixture}-new`);
});
});