import { describe, expect, it } from "vitest"; import { createFlowStateForEditingPublishedRule, createFlowStateMatchesPublishedRule, publishedRuleEditWouldReplaceDraft, } from "../../lib/create/beginEditingPublishedRule"; import type { StoredLastPublishedRule } from "../../lib/create/lastPublishedRule"; import type { CreateFlowState } from "../../app/(app)/create/types"; const published: StoredLastPublishedRule = { id: "rule-qa", title: "QA Tenants Union", summary: "How we share the building.", document: { coreValues: [ { chipId: "accountability", label: "Accountability", meaning: "We say what we will do.", signals: "", }, ], methodSelections: { communication: [ { id: "signal", label: "Signal", sections: { corePrinciple: "We write it down.", logisticsAdmin: "A shared thread.", codeOfConduct: "No pile-ons.", }, }, ], }, }, }; describe("publishedRuleEditWouldReplaceDraft", () => { it("is false when there is no draft", () => { expect(publishedRuleEditWouldReplaceDraft({}, published)).toBe(false); }); it("is false when the draft is already this finished Rule", () => { const state = createFlowStateForEditingPublishedRule(published); expect(publishedRuleEditWouldReplaceDraft(state, published)).toBe(false); expect(createFlowStateMatchesPublishedRule(state, published)).toBe(true); }); it("is true when the draft is a different community", () => { const state: CreateFlowState = { title: "The CommunityRule Project", selectedCoreValueIds: ["solidarity"], selectedCommunicationMethodIds: ["loomio"], }; expect(publishedRuleEditWouldReplaceDraft(state, published)).toBe(true); }); it("is true when the draft is already marked as an edit of another Rule", () => { const state: CreateFlowState = { ...createFlowStateForEditingPublishedRule(published), editingPublishedRuleId: "some-other-rule", }; expect(publishedRuleEditWouldReplaceDraft(state, published)).toBe(true); }); it("is true when the same cards have different description, details, or field blocks", () => { const loaded = createFlowStateForEditingPublishedRule(published); const rewrittenContext: CreateFlowState = { ...loaded, communityContext: "A different description.", summary: "A different description.", }; expect( createFlowStateMatchesPublishedRule(rewrittenContext, published), ).toBe(false); expect( publishedRuleEditWouldReplaceDraft(rewrittenContext, published), ).toBe(true); const rewrittenMeaning: CreateFlowState = { ...loaded, coreValueDetailsByChipId: { accountability: { meaning: "We changed what this means.", signals: "", }, }, }; expect( publishedRuleEditWouldReplaceDraft(rewrittenMeaning, published), ).toBe(true); const rewrittenMethod: CreateFlowState = { ...loaded, communicationMethodDetailsById: { signal: { corePrinciple: "We say it out loud.", logisticsAdmin: "A shared thread.", codeOfConduct: "No pile-ons.", }, }, }; expect(publishedRuleEditWouldReplaceDraft(rewrittenMethod, published)).toBe( true, ); const rewrittenBlocks: CreateFlowState = { ...loaded, customMethodCardFieldBlocksById: { signal: [ { kind: "text", id: "notes", blockTitle: "Notes", placeholderText: "What changed", }, ], }, }; expect(publishedRuleEditWouldReplaceDraft(rewrittenBlocks, published)).toBe( true, ); }); }); describe("createFlowStateForEditingPublishedRule", () => { it("loads the finished Rule's own name and cards, not leftover draft fields", () => { const state = createFlowStateForEditingPublishedRule(published); expect(state.title).toBe("QA Tenants Union"); expect(state.editingPublishedRuleId).toBe("rule-qa"); expect(state.selectedCommunicationMethodIds).toEqual(["signal"]); expect(state.selectedOrganizationTypeIds).toBeUndefined(); expect(state.communityAvatarUrl).toBeUndefined(); expect(state.sections).toEqual([]); }); });