import { describe, it, expect } from "bun:test"; import { parseConditionals, type ConditionalContext } from "./conditional-parser"; describe("parseConditionals", () => { const baseContext: ConditionalContext = { isAuthenticated: true, privilegeLevel: "user", userId: "test-user", currentDate: new Date("2025-06-01"), featureFlags: { "beta-feature": true }, env: { NODE_ENV: "development", VERCEL_ENV: "development" } }; it("should show content for authenticated users", () => { const html = `

Secret content

`; const result = parseConditionals(html, baseContext); expect(result).toContain("Secret content"); expect(result).not.toContain("conditional-block"); }); it("should hide content for anonymous users when condition is authenticated", () => { const html = `

Secret content

`; const anonContext: ConditionalContext = { ...baseContext, isAuthenticated: false, privilegeLevel: "anonymous" }; const result = parseConditionals(html, anonContext); expect(result).not.toContain("Secret content"); }); it("should evaluate admin-only content", () => { const html = `

Admin panel

`; const userResult = parseConditionals(html, baseContext); expect(userResult).not.toContain("Admin panel"); const adminContext: ConditionalContext = { ...baseContext, privilegeLevel: "admin" }; const adminResult = parseConditionals(html, adminContext); expect(adminResult).toContain("Admin panel"); }); it("should handle date before condition", () => { const html = `

Available until 2026

`; const result = parseConditionals(html, baseContext); expect(result).toContain("Available until 2026"); }); it("should handle date after condition", () => { const html = `

Available after 2024

`; const result = parseConditionals(html, baseContext); expect(result).toContain("Available after 2024"); }); it("should handle date between condition", () => { const html = `

2025 content

`; const result = parseConditionals(html, baseContext); expect(result).toContain("2025 content"); }); it("should handle feature flag conditions", () => { const html = `

Beta content

`; const result = parseConditionals(html, baseContext); expect(result).toContain("Beta content"); }); it("should hide content when feature flag is false", () => { const html = `

Disabled content

`; const result = parseConditionals(html, baseContext); expect(result).not.toContain("Disabled content"); }); it("should handle showWhen=false (inverted logic)", () => { const html = `

Not authenticated content

`; const result = parseConditionals(html, baseContext); expect(result).not.toContain("Not authenticated content"); const anonContext: ConditionalContext = { ...baseContext, isAuthenticated: false, privilegeLevel: "anonymous" }; const anonResult = parseConditionals(html, anonContext); expect(anonResult).toContain("Not authenticated content"); }); it("should handle multiple conditional blocks", () => { const html = `

Public content

Auth content

More public

Admin content

`; const result = parseConditionals(html, baseContext); expect(result).toContain("Public content"); expect(result).toContain("Auth content"); expect(result).toContain("More public"); expect(result).not.toContain("Admin content"); }); it("should handle empty HTML", () => { const result = parseConditionals("", baseContext); expect(result).toBe(""); }); it("should handle HTML with no conditionals", () => { const html = "

Regular content

"; const result = parseConditionals(html, baseContext); expect(result).toBe(html); }); it("should default to hiding unknown condition types", () => { const html = `

Unknown type content

`; const result = parseConditionals(html, baseContext); expect(result).not.toContain("Unknown type content"); }); it("should handle complex nested HTML in conditional content", () => { const html = `

Title

console.log('test');
`; const result = parseConditionals(html, baseContext); expect(result).toContain("

Title

"); expect(result).toContain("