import { describe, expect, it } from "vitest"; import { createRequire } from "module"; const require = createRequire(import.meta.url); const { buildImportSql, normalizeTreatment, sqlString } = require("../../scripts/export-harness-feature-flags"); describe("Harness feature flag exporter", () => { it("preserves custom treatment names while normalizing booleans and known treatments", () => { expect(normalizeTreatment(true)).toBe("on"); expect(normalizeTreatment(false)).toBe("off"); expect(normalizeTreatment(" ON ")).toBe("on"); expect(normalizeTreatment("false")).toBe("off"); expect(normalizeTreatment("control")).toBe("control"); expect(normalizeTreatment("variant-a")).toBe("variant-a"); expect(normalizeTreatment(" custom treatment ")).toBe("custom treatment"); expect(normalizeTreatment(null)).toBe("control"); expect(normalizeTreatment("")).toBe("control"); }); it("escapes SQL string values", () => { expect(sqlString("Dave's Shop")).toBe("'Dave''s Shop'"); }); it("escapes custom treatments in generated import SQL", () => { const sql = buildImportSql([ { customerKey: "SHOP'1", imexshopid: "SHOP'1", name: "Demo'Flag", treatment: "pilot's-choice", config: { text: "Dave's config" } } ]); expect(sql).toContain("('SHOP''1', 'Demo''Flag', 'pilot''s-choice'"); expect(sql).toContain(`'{"text":"Dave''s config"}'::jsonb`); }); it("includes an unmatched feature flag report query", () => { const sql = buildImportSql([ { customerKey: "SHOP1", imexshopid: "SHOP1", name: "Missing_Flag", treatment: "on", config: null } ]); expect(sql).toContain('AS "unmatched_feature_flag"'); expect(sql).toContain('LEFT JOIN "public"."feature_flags"'); expect(sql).toContain("('Missing_Flag')"); }); });