import { describe, expect, it } from "vitest"; import { applyDecisionApproachKeyResources, decisionApproachKeyResourceCheckboxIds, decisionApproachKeyResourceIdsFromLabels, decisionApproachScopeForPublish, decisionApproachKeyResourceItems, decisionApproachKeyResourceLabels, selectedKeyResourceLabelsFromCheckedIds, stringArraysEqual, syncDecisionApproachKeyResourceDetails, decisionApproachDefaultScopeChips, withDecisionApproachKeyResourceScopes, } from "../../lib/create/decisionApproachKeyResources"; import type { DecisionApproachDetailEntry } from "../../app/(app)/create/types"; const emptyEntry = (): DecisionApproachDetailEntry => ({ corePrinciple: "p", applicableScope: ["Daily Operations"], selectedApplicableScope: [], stepByStepInstructions: "s", consensusLevel: 75, objectionsDeadlocks: "o", }); describe("decisionApproachKeyResources", () => { it("reads the decision checklist from messages", () => { expect(decisionApproachKeyResourceItems()).toEqual([ { id: "everyday", label: "Everyday decisions" }, { id: "big", label: "Big decisions" }, { id: "amend", label: "Changing this Rule" }, { id: "emergency", label: "Emergencies" }, { id: "finances", label: "Shared money" }, { id: "spending", label: "Spending" }, { id: "roles", label: "Roles and working groups" }, ]); }); it("selects checked key-resource labels without rewriting applicableScope", () => { const next = applyDecisionApproachKeyResources(emptyEntry(), [ "Shared money", ]); expect(next.applicableScope).toEqual(["Daily Operations"]); expect(next.selectedApplicableScope).toEqual(["Shared money"]); }); it("keeps non-key chip selections when syncing sidebar checks", () => { const next = applyDecisionApproachKeyResources( { ...emptyEntry(), applicableScope: ["Shared money", "Daily Operations"], selectedApplicableScope: ["Daily Operations"], }, ["Shared money"], ); expect(next.applicableScope).toEqual([ "Shared money", "Daily Operations", ]); expect(next.selectedApplicableScope).toEqual([ "Daily Operations", "Shared money", ]); }); it("publishes default scopes plus checked key resources", () => { expect( decisionApproachScopeForPublish( ["Daily Operations", "Minor Expenditures"], ["Shared money"], ), ).toEqual([ "Daily Operations", "Minor Expenditures", "Shared money", ]); expect( decisionApproachScopeForPublish( ["Daily Operations", "Minor Expenditures"], ["Daily Operations", "Shared money"], ), ).toEqual(["Daily Operations", "Shared money"]); expect( decisionApproachScopeForPublish( ["Daily Operations", "Minor Expenditures"], [], ), ).toEqual(["Daily Operations", "Minor Expenditures"]); }); it("maps checkbox ids to labels and back", () => { expect(selectedKeyResourceLabelsFromCheckedIds(["amend", "big"])).toEqual( ["Big decisions", "Changing this Rule"], ); expect( decisionApproachKeyResourceIdsFromLabels([ "Big decisions", "Unknown", "Changing this Rule", ]), ).toEqual(["big", "amend"]); }); it("derives sidebar checks from the open draft or selected approaches", () => { const detailsById = { "lazy-consensus": { ...emptyEntry(), selectedApplicableScope: ["Shared money"], }, "do-ocracy": { ...emptyEntry(), selectedApplicableScope: ["Big decisions"], }, }; expect( decisionApproachKeyResourceCheckboxIds({ detailsById, selectedApproachIds: ["lazy-consensus", "do-ocracy"], reminderIds: ["spending"], }), ).toEqual(["big", "finances", "spending"]); expect( decisionApproachKeyResourceCheckboxIds({ detailsById, selectedApproachIds: ["lazy-consensus", "do-ocracy"], reminderIds: ["spending"], openDraft: { ...emptyEntry(), selectedApplicableScope: ["Changing this Rule"], }, }), ).toEqual(["amend"]); }); it("syncs existing and selected approach details", () => { const next = syncDecisionApproachKeyResourceDetails( { "lazy-consensus": emptyEntry() }, ["do-ocracy"], ["Changing this Rule"], (id) => ({ ...emptyEntry(), corePrinciple: id, applicableScope: ["Volunteer Tasks"], }), ); expect(next["lazy-consensus"]?.selectedApplicableScope).toEqual([ "Changing this Rule", ]); expect(next["do-ocracy"]?.corePrinciple).toBe("do-ocracy"); expect(next["do-ocracy"]?.applicableScope).toEqual(["Volunteer Tasks"]); expect(next["do-ocracy"]?.selectedApplicableScope).toEqual([ "Changing this Rule", ]); }); it("compares string arrays by order", () => { expect(stringArraysEqual(["a", "b"], ["a", "b"])).toBe(true); expect(stringArraysEqual(["a", "b"], ["b", "a"])).toBe(false); expect(decisionApproachKeyResourceLabels()).toHaveLength(7); }); it("unions signed-off default chips then key-resource labels onto an existing scope list", () => { expect(decisionApproachDefaultScopeChips()).toEqual([ "Changing the Rule", "Money", "Projects", "Asking someone to leave", ]); expect( withDecisionApproachKeyResourceScopes(["Daily Operations"]), ).toEqual([ "Changing the Rule", "Money", "Projects", "Asking someone to leave", "Daily Operations", "Everyday decisions", "Big decisions", "Changing this Rule", "Emergencies", "Shared money", "Spending", "Roles and working groups", ]); }); });