Files
community-rule/tests/unit/ruleShare.test.ts
T
adilallo 5cbda35e32 Start every Rule private, with a rotating link and files that follow its readers.
New Rules stay off lists until the person who holds the pen turns on a share link, or the team features one. Uploaded files are readable only by those readers, stripped of location metadata, and deleted with the Rule, draft, or account.
2026-11-09 19:54:04 -07:00

229 lines
6.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { RULE_VISIBILITY } from "../../lib/ruleVisibility";
const findUniqueMock = vi.fn();
const updateMock = vi.fn();
const isRuleAdminEmailMock = vi.fn();
const allocateShareSlugMock = vi.fn();
vi.mock("../../lib/server/db", () => ({
prisma: {
publishedRule: {
findUnique: (...args: unknown[]) => findUniqueMock(...args),
update: (...args: unknown[]) => updateMock(...args),
},
},
}));
vi.mock("../../lib/server/ruleAdmins", () => ({
isRuleAdminEmail: (...args: unknown[]) => isRuleAdminEmailMock(...args),
}));
vi.mock("../../lib/server/shareSlug", () => ({
allocateShareSlug: (...args: unknown[]) => allocateShareSlugMock(...args),
}));
import { patchOwnedRuleShare } from "../../lib/server/ruleShare";
const owner = { id: "user-1", email: "owner@example.org" } as never;
function row(overrides: Record<string, unknown> = {}) {
return {
id: "rule-1",
title: "Juniper Street Food Share",
userId: "user-1",
visibility: RULE_VISIBILITY.private,
shareSlug: null,
stakeholders: [],
...overrides,
};
}
beforeEach(() => {
findUniqueMock.mockReset();
updateMock.mockReset();
isRuleAdminEmailMock.mockReset();
allocateShareSlugMock.mockReset();
isRuleAdminEmailMock.mockReturnValue(false);
updateMock.mockResolvedValue({});
});
describe("patchOwnedRuleShare", () => {
it("allocates a new slug when turning a private Rule on", async () => {
findUniqueMock
.mockResolvedValueOnce(row())
.mockResolvedValueOnce(
row({
visibility: RULE_VISIBILITY.shared_by_link,
shareSlug: "k7f2-9qxm",
}),
);
allocateShareSlugMock.mockResolvedValueOnce("k7f2-9qxm");
const result = await patchOwnedRuleShare({
ruleId: "rule-1",
user: owner,
origin: "https://communityrule.info",
visibility: "shared_by_link",
communityAgreed: true,
});
expect(result.ok).toBe(true);
expect(allocateShareSlugMock).toHaveBeenCalledTimes(1);
expect(updateMock).toHaveBeenCalledWith(
expect.objectContaining({
data: {
visibility: RULE_VISIBILITY.shared_by_link,
shareSlug: "k7f2-9qxm",
},
}),
);
});
it("requires community agreement the first time a link turns on", async () => {
findUniqueMock.mockResolvedValueOnce(row());
const result = await patchOwnedRuleShare({
ruleId: "rule-1",
user: owner,
origin: "https://communityrule.info",
visibility: "shared_by_link",
});
expect(result).toEqual({ ok: false, reason: "community_agreed" });
expect(updateMock).not.toHaveBeenCalled();
});
it("forbids the owner from setting public and featured", async () => {
findUniqueMock.mockResolvedValueOnce(row());
const result = await patchOwnedRuleShare({
ruleId: "rule-1",
user: owner,
origin: "https://communityrule.info",
visibility: RULE_VISIBILITY.public_and_featured,
});
expect(result).toEqual({ ok: false, reason: "forbidden" });
expect(updateMock).not.toHaveBeenCalled();
});
it("lets a named admin set public and featured", async () => {
isRuleAdminEmailMock.mockReturnValue(true);
const admin = { id: "admin-1", email: "ops@example.org" } as never;
findUniqueMock
.mockResolvedValueOnce(row())
.mockResolvedValueOnce(
row({
visibility: RULE_VISIBILITY.public_and_featured,
user: { email: "owner@example.org" },
}),
);
const result = await patchOwnedRuleShare({
ruleId: "rule-1",
user: admin,
origin: "https://communityrule.info",
visibility: RULE_VISIBILITY.public_and_featured,
});
expect(result.ok).toBe(true);
expect(updateMock).toHaveBeenCalledWith(
expect.objectContaining({
data: {
visibility: RULE_VISIBILITY.public_and_featured,
shareSlug: null,
},
}),
);
});
it("allocates a new slug after a link is turned off and on again", async () => {
findUniqueMock
.mockResolvedValueOnce(row())
.mockResolvedValueOnce(
row({
visibility: RULE_VISIBILITY.shared_by_link,
shareSlug: "abcd-efgh",
}),
);
allocateShareSlugMock.mockResolvedValueOnce("abcd-efgh");
const first = await patchOwnedRuleShare({
ruleId: "rule-1",
user: owner,
origin: "https://communityrule.info",
visibility: "shared_by_link",
communityAgreed: true,
});
expect(first.ok).toBe(true);
findUniqueMock
.mockResolvedValueOnce(
row({
visibility: RULE_VISIBILITY.shared_by_link,
shareSlug: "abcd-efgh",
}),
)
.mockResolvedValueOnce(row());
const off = await patchOwnedRuleShare({
ruleId: "rule-1",
user: owner,
origin: "https://communityrule.info",
visibility: "private",
});
expect(off.ok).toBe(true);
findUniqueMock
.mockResolvedValueOnce(row())
.mockResolvedValueOnce(
row({
visibility: RULE_VISIBILITY.shared_by_link,
shareSlug: "wxyz-1234",
}),
);
allocateShareSlugMock.mockResolvedValueOnce("wxyz-1234");
const second = await patchOwnedRuleShare({
ruleId: "rule-1",
user: owner,
origin: "https://communityrule.info",
visibility: "shared_by_link",
communityAgreed: true,
});
expect(second.ok).toBe(true);
expect(allocateShareSlugMock).toHaveBeenCalledTimes(2);
expect(updateMock).toHaveBeenLastCalledWith(
expect.objectContaining({
data: {
visibility: RULE_VISIBILITY.shared_by_link,
shareSlug: "wxyz-1234",
},
}),
);
});
it("clears the slug when the link is turned off", async () => {
findUniqueMock
.mockResolvedValueOnce(
row({
visibility: RULE_VISIBILITY.shared_by_link,
shareSlug: "k7f2-9qxm",
}),
)
.mockResolvedValueOnce(row());
const result = await patchOwnedRuleShare({
ruleId: "rule-1",
user: owner,
origin: "https://communityrule.info",
visibility: "private",
});
expect(result.ok).toBe(true);
expect(updateMock).toHaveBeenCalledWith(
expect.objectContaining({
data: {
visibility: RULE_VISIBILITY.private,
shareSlug: null,
},
}),
);
});
});