Write the adopted team Rule before the server listens so deploy cannot feature the old draft body.

This commit is contained in:
adilallo committed 2026-11-12 15:55:36 -07:00
1 parent 29a8d2b32b
commit 1ec1f48d35
8 files changed
+232 -86

No files matched your search

+122 -40
View File
@@ -1,9 +1,15 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { publishedRuleAnalyticsFromDocument } from "../../lib/create/publishedRuleAnalytics";
import {
TEAM_RULE_DRAFT_TITLE,
TEAM_RULE_TITLE,
buildAdoptedTeamRule,
} from "../../lib/create/teamRule";
import { RULE_VISIBILITY } from "../../lib/ruleVisibility";
import { TEAM_RULE_DRAFT_TITLE, TEAM_RULE_TITLE } from "../../lib/create/teamRule";
const findFirstMock = vi.fn();
const findManyMock = vi.fn();
const updateMock = vi.fn();
const updateManyMock = vi.fn();
const createMock = vi.fn();
import { seedTeamRule } from "../../prisma/seed/teamRule";
@@ -11,28 +17,45 @@ import { seedTeamRule } from "../../prisma/seed/teamRule";
function prismaStub() {
return {
publishedRule: {
findFirst: findFirstMock,
findMany: findManyMock,
update: updateMock,
updateMany: updateManyMock,
create: createMock,
},
};
}
function staleRow(
id: string,
title: string,
visibility: string = RULE_VISIBILITY.private,
) {
return {
id,
title,
summary: "September draft",
document: { stale: true },
visibility,
communityLocation: null,
orgType: null,
};
}
beforeEach(() => {
findFirstMock.mockReset();
findManyMock.mockReset();
updateMock.mockReset();
updateManyMock.mockReset();
createMock.mockReset();
updateMock.mockResolvedValue({});
updateManyMock.mockResolvedValue({ count: 1 });
});
describe("seedTeamRule", () => {
it("replaces the old draft row in place", async () => {
findFirstMock
.mockResolvedValueOnce({ id: "draft-1" })
.mockResolvedValueOnce(null);
updateMock.mockResolvedValueOnce({
id: "draft-1",
title: TEAM_RULE_TITLE,
});
it("replaces the old draft row in place, including the document", async () => {
const adopted = buildAdoptedTeamRule();
findManyMock.mockResolvedValueOnce([
staleRow("draft-1", TEAM_RULE_DRAFT_TITLE),
]);
const result = await seedTeamRule(prismaStub() as never);
@@ -41,31 +64,39 @@ describe("seedTeamRule", () => {
id: "draft-1",
title: TEAM_RULE_TITLE,
});
expect(findFirstMock).toHaveBeenNthCalledWith(1, {
where: { title: TEAM_RULE_DRAFT_TITLE },
expect(findManyMock).toHaveBeenCalledWith({
where: {
title: { in: [TEAM_RULE_DRAFT_TITLE, TEAM_RULE_TITLE] },
},
orderBy: { createdAt: "asc" },
select: { id: true },
select: {
id: true,
title: true,
summary: true,
document: true,
visibility: true,
communityLocation: true,
orgType: true,
},
});
expect(updateMock).toHaveBeenCalledWith(
expect.objectContaining({
where: { id: "draft-1" },
data: expect.objectContaining({
title: TEAM_RULE_TITLE,
visibility: RULE_VISIBILITY.public_and_featured,
}),
}),
);
expect(updateMock).toHaveBeenCalledWith({
where: { id: "draft-1" },
data: {
title: adopted.title,
summary: adopted.summary,
document: adopted.document,
visibility: RULE_VISIBILITY.public_and_featured,
...publishedRuleAnalyticsFromDocument(adopted.document),
},
});
expect(updateManyMock).not.toHaveBeenCalled();
expect(createMock).not.toHaveBeenCalled();
});
it("updates an already-renamed adopted row", async () => {
findFirstMock
.mockResolvedValueOnce(null)
.mockResolvedValueOnce({ id: "adopted-1" });
updateMock.mockResolvedValueOnce({
id: "adopted-1",
title: TEAM_RULE_TITLE,
});
findManyMock.mockResolvedValueOnce([
staleRow("adopted-1", TEAM_RULE_TITLE),
]);
const result = await seedTeamRule(prismaStub() as never);
@@ -76,7 +107,8 @@ describe("seedTeamRule", () => {
});
it("creates the adopted Rule when neither row exists", async () => {
findFirstMock.mockResolvedValueOnce(null).mockResolvedValueOnce(null);
const adopted = buildAdoptedTeamRule();
findManyMock.mockResolvedValueOnce([]);
createMock.mockResolvedValueOnce({
id: "new-1",
title: TEAM_RULE_TITLE,
@@ -89,14 +121,64 @@ describe("seedTeamRule", () => {
id: "new-1",
title: TEAM_RULE_TITLE,
});
expect(createMock).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
userId: null,
title: TEAM_RULE_TITLE,
visibility: RULE_VISIBILITY.public_and_featured,
}),
}),
expect(createMock).toHaveBeenCalledWith({
data: {
userId: null,
title: adopted.title,
summary: adopted.summary,
document: adopted.document,
visibility: RULE_VISIBILITY.public_and_featured,
...publishedRuleAnalyticsFromDocument(adopted.document),
},
select: { id: true, title: true },
});
});
it("unpublishes a second featured row instead of leaving its document public", async () => {
findManyMock.mockResolvedValueOnce([
staleRow(
"adopted-1",
TEAM_RULE_TITLE,
RULE_VISIBILITY.public_and_featured,
),
staleRow("draft-1", TEAM_RULE_DRAFT_TITLE),
]);
const result = await seedTeamRule(prismaStub() as never);
expect(result).toEqual({
action: "updated",
id: "draft-1",
title: TEAM_RULE_TITLE,
});
expect(updateMock).toHaveBeenCalledWith(
expect.objectContaining({ where: { id: "draft-1" } }),
);
expect(updateManyMock).toHaveBeenCalledWith({
where: { id: { in: ["adopted-1"] } },
data: { visibility: RULE_VISIBILITY.private },
});
});
it("does not rewrite a row that already matches the adopted Rule", async () => {
const adopted = buildAdoptedTeamRule();
const analytics = publishedRuleAnalyticsFromDocument(adopted.document);
findManyMock.mockResolvedValueOnce([
{
id: "adopted-1",
title: adopted.title,
summary: adopted.summary,
document: adopted.document,
visibility: RULE_VISIBILITY.public_and_featured,
communityLocation: analytics.communityLocation,
orgType: analytics.orgType,
},
]);
const result = await seedTeamRule(prismaStub() as never);
expect(result.action).toBe("unchanged");
expect(updateMock).not.toHaveBeenCalled();
expect(updateManyMock).not.toHaveBeenCalled();
});
});