import { describe, expect, it } from "vitest"; import { compressText, looksExactSensitive } from "../src/defluffer"; const profile = "standardGuardedDedupe" as const; describe("defluffer", () => { it("compresses fluff around JSON but preserves detected JSON exactly", () => { const json = '{"userRepository":"primary","active":true,"retryCount":5,"mode":"production"}'; const input = `Hello there! Could you please transform the following JSON without changing any keys or string values: ${json}. Thank you so much!`; const result = compressText(input, profile); expect(result.safe).toBe(true); expect(result.text).toContain(json); expect(result.text).not.toContain("Hello there"); expect(result.text).not.toContain("Thank you so much"); expect(result.changed).toBe(true); }); it("does not treat exact JSON instructions as whole-prompt skip markers", () => { expect( looksExactSensitive("Return only valid JSON without changing any keys."), ).toBeUndefined(); }); it("preserves unfenced YAML blocks while compressing surrounding text", () => { const yaml = "service:\n name: user-repository\n active: true\n retry_count: 5"; const input = `Hello! Could you please update this YAML but do not rename keys:\n${yaml}\n\nThank you so much!`; const result = compressText(input, profile); expect(result.safe).toBe(true); expect(result.text).toContain(yaml); expect(result.text).not.toContain("Hello!"); expect(result.text).not.toContain("Thank you so much"); }); it("preserves fenced code blocks exactly", () => { const code = "```javascript\nfunction calculateMaximum(array) {\n if (array === null) return 0;\n return Math.max(...array);\n}\n```"; const input = `Could you please refactor this code without using any external libraries?\n\n${code}\n\nThank you.`; const result = compressText(input, profile); expect(result.safe).toBe(true); expect(result.text).toContain(code); expect(result.text).toContain("without external libs"); }); it("keeps whole-prompt skip markers for legal/defined-term prompts", () => { expect( looksExactSensitive("Summarize without changing legal meaning."), ).toBe("legal meaning"); expect( looksExactSensitive("Do not replace defined terms such as Application."), ).toBe("defined terms"); }); it("dedupes transcript filler", () => { const input = "Um yeah so basically basically I just need, like, a summary of this call."; const result = compressText(input, profile); expect(result.safe).toBe(true); expect(result.text).toContain("I need"); expect(result.text).not.toMatch(/basically basically/i); expect(result.savedTokens).toBeGreaterThan(0); }); it("avoids punctuation and grammar artifacts in polite task prompts", () => { const input = "Hello there, could you please provide a step by step guide in order to configure the application due to the fact that I am really trying to figure out how to set up the database configuration? Thank you so much."; const result = compressText(input, profile); expect(result.safe).toBe(true); expect(result.text).not.toMatch(/^\s*[,;:]/); expect(result.text).not.toMatch(/\bI\s+am\s+need\b/i); expect(result.text).not.toMatch(/[?!]\./); expect(result.text).toContain("I need to set up"); expect(result.savedTokens).toBeGreaterThan(0); }); it("leaves arbitrary literary prose unchanged instead of normalizing style", () => { const input = "Pray tell me the meaning of life casual LLM? Art thou good? Ipsum"; const result = compressText(input, profile); expect(result.safe).toBe(true); expect(result.changed).toBe(false); expect(result.text).toBe(input); }); });