import { describe, expect, test } from "bun:test";
import { definitionDigest, type FeatureDefinition, type LoadedFeatureDefinition } from "./definitions";
import { evaluateFeature } from "./features";
const sha = "a".repeat(40);
const definition: FeatureDefinition = { id: "delivery-test", changeClass: "behavior-change", owner: "owner", fallback: "old behavior", defaultState: "OFF", readinessChecks: ["receipt"], observability: ["ledger"], reviewDate: "2026-09-01", cleanupTask: "#5", rollbackWindowDays: 7, removalCondition: "remove", introduced: { task: "#5", sha }, activationPolicy: "dark-only" };
const loaded: LoadedFeatureDefinition = { definition, digest: definitionDigest(definition), path: "test" };
const state = { featureId: definition.id, definitionDigest: loaded.digest, state: "ON" as const, deployedSha: sha, acceptanceReceiptId: "a", smokeReceiptId: "s", revision: 1, transitionedAt: "now" };
const readiness = { admitted: true, deploymentId: "web", deployedSha: sha };
describe("feature evaluation", () => {
 test("denies malformed and unavailable identities", () => { expect(evaluateFeature(loaded, { getDeliveryFeatureState: () => state }, { componentId: "web", deployedSha: "bad" }, readiness).reason).toBe("invalid-observed-identity"); expect(evaluateFeature(loaded, { getDeliveryFeatureState: () => { throw new Error(); } }, { componentId: "web", deployedSha: sha }, readiness).enabled).toBe(false); });
 test("denies corrupt stored identities and valid mismatches", () => { expect(evaluateFeature(loaded, { getDeliveryFeatureState: () => ({ ...state, deployedSha: "A".repeat(40) }) }, { componentId: "web", deployedSha: sha }, readiness).reason).toBe("corrupt-stored-identity"); expect(evaluateFeature(loaded, { getDeliveryFeatureState: () => state }, { componentId: "web", deployedSha: "b".repeat(40) }, readiness).reason).toBe("sha-mismatch"); });
 test("partial states require a bound eligibility decision", () => { const internal = { ...state, state: "INTERNAL" as const }; expect(evaluateFeature(loaded, { getDeliveryFeatureState: () => internal }, { componentId: "web", deployedSha: sha, subjectId: "u" }, readiness).enabled).toBe(false); expect(evaluateFeature(loaded, { getDeliveryFeatureState: () => internal }, { componentId: "web", deployedSha: sha, subjectId: "u" }, readiness, { eligibility: () => ({ featureId: definition.id, definitionDigest: loaded.digest, stateRevision: 1, deploymentId: "web", deployedSha: sha, subjectId: "u", tier: "INTERNAL" as const, eligible: true }) }).enabled).toBe(true); });
 test("denies expired definitions during steady-state evaluation", () => { expect(evaluateFeature(loaded, { getDeliveryFeatureState: () => state }, { componentId: "web", deployedSha: sha }, readiness, undefined, () => Date.parse("2026-09-02T00:00:00.000Z"))).toEqual({ state: "OFF", enabled: false, reason: "expired-definition" }); });
});
