Keep a continued draft on screen, ask before Edit drops rewritten copy, and retry clearing the draft after publish.

This commit is contained in:
adilallo committed 2026-11-22 18:31:07 -07:00
1 parent 54a4871ad1
commit 7c08766092
11 files changed
+361 -41

No files matched your search

@@ -63,6 +63,66 @@ describe("publishedRuleEditWouldReplaceDraft", () => {
};
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", () => {
@@ -56,7 +56,7 @@ describe("useCreateFlowFinalize", () => {
vi.mocked(publishRule).mockReset();
vi.mocked(updatePublishedRule).mockReset();
vi.mocked(deleteServerDraft).mockReset();
vi.mocked(deleteServerDraft).mockResolvedValue(undefined);
vi.mocked(deleteServerDraft).mockResolvedValue(true);
vi.mocked(writeLastPublishedRule).mockReset();
vi.mocked(stateWithPendingCommunityAvatar).mockReset();
vi.mocked(stateWithPendingCommunityAvatar).mockImplementation(
@@ -111,6 +111,34 @@ describe("useCreateFlowFinalize", () => {
expect(deleteServerDraft).toHaveBeenCalledTimes(1);
});
it("retries clearing the server draft when the first delete fails", async () => {
vi.mocked(publishRule).mockResolvedValue({
ok: true,
id: "new-rule-id",
title: "Published title",
});
vi.mocked(deleteServerDraft)
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(true);
const { result } = renderHook(() =>
useCreateFlowFinalize({
state: emptyState,
router,
openLogin,
updateState,
loginReturnPath: "/create/final-review",
sessionUser: signedIn,
}),
);
await act(async () => {
await result.current.finalize();
});
expect(deleteServerDraft).toHaveBeenCalledTimes(2);
});
it("does not publish while the session is unresolved", async () => {
const { result } = renderHook(() =>
useCreateFlowFinalize({
+55 -6
View File
@@ -123,6 +123,60 @@ describe("POST /api/rules", () => {
expect(deleteRuleDraftForUserMock).toHaveBeenCalledWith("user-1");
});
it("retries draft deletion after publish and still returns the rule", async () => {
publishedRuleCreateMock.mockResolvedValueOnce({
id: "rule-solo",
title: "T",
summary: null,
createdAt: new Date("2026-01-01T00:00:00.000Z"),
});
deleteRuleDraftForUserMock
.mockRejectedValueOnce(new Error("draft delete failed"))
.mockResolvedValueOnce(undefined);
const res = await POST(
new NextRequest("https://x.test/api/rules", {
method: "POST",
body: JSON.stringify({
title: "T",
summary: null,
document: {},
}),
}),
undefined,
);
expect(res.status).toBe(200);
expect(deleteRuleDraftForUserMock).toHaveBeenCalledTimes(2);
});
it("publishes when draft deletion keeps failing", async () => {
publishedRuleCreateMock.mockResolvedValueOnce({
id: "rule-solo",
title: "T",
summary: null,
createdAt: new Date("2026-01-01T00:00:00.000Z"),
});
deleteRuleDraftForUserMock.mockRejectedValue(
new Error("draft delete failed"),
);
const res = await POST(
new NextRequest("https://x.test/api/rules", {
method: "POST",
body: JSON.stringify({
title: "T",
summary: null,
document: {},
}),
}),
undefined,
);
expect(res.status).toBe(200);
expect(deleteRuleDraftForUserMock).toHaveBeenCalledTimes(3);
});
it("stores place and group type from the published document", async () => {
publishedRuleCreateMock.mockResolvedValueOnce({
id: "rule-counts",
@@ -259,12 +313,7 @@ describe("POST /api/rules", () => {
createdAt: new Date("2026-01-02T00:00:00.000Z"),
};
transactionMock.mockImplementation(
async (
fn: (tx: {
publishedRule: { create: typeof vi.fn };
ruleStakeholder: { create: typeof vi.fn };
}) => Promise<unknown>,
) => {
async (fn: (tx: { publishedRule: { create: typeof vi.fn }; ruleStakeholder: { create: typeof vi.fn } }) => Promise<unknown>) => {
const tx = {
publishedRule: {
create: vi.fn().mockResolvedValue(created),
@@ -0,0 +1,70 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const clearPendingCommunityAvatarFile = vi.fn();
const forgetFlushedCommunityAvatarUrl = vi.fn();
vi.mock("../../lib/create/pendingCommunityAvatarUpload", () => ({
clearPendingCommunityAvatarFile: () => clearPendingCommunityAvatarFile(),
}));
vi.mock("../../lib/create/flushPendingCommunityAvatar", () => ({
forgetFlushedCommunityAvatarUrl: () => forgetFlushedCommunityAvatarUrl(),
}));
import {
CREATE_FLOW_ANONYMOUS_KEY,
CREATE_FLOW_TRANSFER_PENDING_KEY,
} from "../../app/(app)/create/utils/anonymousDraftStorage";
import { CORE_VALUE_DETAILS_STORAGE_KEY } from "../../app/(app)/create/utils/coreValueDetailsLocalStorage";
import { seedContinuedDraftLocalStorage } from "../../app/(app)/create/utils/seedContinuedDraftLocalStorage";
import type { CreateFlowState } from "../../app/(app)/create/types";
describe("seedContinuedDraftLocalStorage", () => {
beforeEach(() => {
localStorage.clear();
clearPendingCommunityAvatarFile.mockReset();
forgetFlushedCommunityAvatarUrl.mockReset();
localStorage.setItem(
CREATE_FLOW_ANONYMOUS_KEY,
JSON.stringify({ title: "Stale" }),
);
localStorage.setItem(
CORE_VALUE_DETAILS_STORAGE_KEY,
JSON.stringify({ old: { meaning: "old", signals: "" } }),
);
localStorage.setItem(CREATE_FLOW_TRANSFER_PENDING_KEY, "1");
});
it("writes the server draft over the browser mirror", () => {
const state: CreateFlowState = {
title: "Garden",
currentStep: "core-values",
coreValueDetailsByChipId: {
solidarity: { meaning: "We show up.", signals: "" },
},
};
seedContinuedDraftLocalStorage(state);
expect(
JSON.parse(localStorage.getItem(CREATE_FLOW_ANONYMOUS_KEY) ?? "{}"),
).toEqual(state);
expect(
JSON.parse(localStorage.getItem(CORE_VALUE_DETAILS_STORAGE_KEY) ?? "{}"),
).toEqual(state.coreValueDetailsByChipId);
expect(localStorage.getItem(CREATE_FLOW_TRANSFER_PENDING_KEY)).toBeNull();
expect(clearPendingCommunityAvatarFile).toHaveBeenCalledTimes(1);
expect(forgetFlushedCommunityAvatarUrl).toHaveBeenCalledTimes(1);
});
it("clears core-value details when the continued draft has none", () => {
seedContinuedDraftLocalStorage({ title: "Garden" });
expect(localStorage.getItem(CORE_VALUE_DETAILS_STORAGE_KEY)).toBeNull();
expect(
JSON.parse(localStorage.getItem(CREATE_FLOW_ANONYMOUS_KEY) ?? "{}"),
).toEqual({
title: "Garden",
});
});
});