1
0
Fork 0

feat(contributions): award 25 XP per journal entry, one per day

This commit is contained in:
Jason Fraley 2026-10-13 15:52:57 -04:00
parent f481efb108
commit e96fdb7f58
2 changed files with 59 additions and 57 deletions

View file

@ -98,9 +98,10 @@ export const CONTRIBUTION_RULES: Record<ContributionType, ContributionRule> = {
"journal-publish": {
type: "journal-publish",
label: "Journal entry published",
points: 5,
points: 25,
mode: "instant",
description: "Published a journal entry to the Chronicle with a substantive body.",
// A 25 XP daily cap means one qualifying entry per day earns.
description: "Published a journal entry to the Chronicle with a substantive body (one per day).",
dailyCap: 25,
maxPerMission: null,
minEditDeltaChars: null,

View file

@ -1,8 +1,9 @@
/**
* Publishing a journal entry earns contribution XP: a substantive publish
* grants 5 XP (immediate), drafts and short entries do not, editing a published
* entry never double-counts, the rolling daily cap applies, and deleting an
* entry pulls the XP back.
* grants 25 XP (immediate), drafts and short entries do not, editing a published
* entry never double-counts, only one entry a day earns, and deleting an entry
* pulls the XP back. Each grant test uses a fresh author because the rule's
* 25 XP daily cap allows a single entry per day.
*/
import { getPayload, type Payload } from "payload";
import config from "@/payload.config";
@ -53,7 +54,6 @@ function journalBody(text: string): Journal["body"] {
describe("Journal contribution XP", () => {
const userIds: number[] = [];
const journalIds: number[] = [];
let user: User;
const makeUser = async (label: string): Promise<User> => {
const created = (await payload.create({
@ -85,15 +85,33 @@ describe("Journal contribution XP", () => {
return res.docs[0]?.progression?.contributions?.xpEarned ?? 0;
};
const publish = async (title: string, body: Journal["body"], status: "draft" | "published") => {
const publish = async (
author: User,
title: string,
body: Journal["body"],
status: "draft" | "published",
) => {
authSpy.mockResolvedValue({ user: author });
const result = await createJournal({ title, body, status, occurredAt: null, mission: null });
if (result.data != null) journalIds.push(result.data);
return result;
};
const entriesForJournal = (journalId: number) =>
payload.find({
collection: "contribution-entries",
where: {
and: [
{ sourceCollection: { equals: "journals" } },
{ sourceId: { equals: journalId } },
],
},
depth: 0,
overrideAccess: true,
});
beforeAll(async () => {
payload = await getPayload({ config: await config });
user = await makeUser("author");
authSpy = vi.spyOn(payload, "auth");
}, TIMEOUT);
@ -120,37 +138,38 @@ describe("Journal contribution XP", () => {
}
}, TIMEOUT);
it("grants 5 XP when a substantive entry is published on create", async () => {
authSpy.mockResolvedValue({ user });
const before = await bucketFor(user.id);
const result = await publish("A long field report", journalBody("x".repeat(250)), "published");
it("grants 25 XP when a substantive entry is published on create", async () => {
const author = await makeUser("publish");
const before = await bucketFor(author.id);
const result = await publish(author, "A long field report", journalBody("x".repeat(250)), "published");
expect(result.success).toBe(true);
expect(await bucketFor(user.id)).toBe(before + 5);
expect(await bucketFor(author.id)).toBe(before + 25);
});
it("does not grant for a draft", async () => {
authSpy.mockResolvedValue({ user });
const before = await bucketFor(user.id);
const result = await publish("A private draft", journalBody("x".repeat(250)), "draft");
const author = await makeUser("draft");
const before = await bucketFor(author.id);
const result = await publish(author, "A private draft", journalBody("x".repeat(250)), "draft");
expect(result.success).toBe(true);
expect(await bucketFor(user.id)).toBe(before);
expect(await bucketFor(author.id)).toBe(before);
});
it("does not grant for a published entry below the substance threshold", async () => {
authSpy.mockResolvedValue({ user });
const before = await bucketFor(user.id);
const result = await publish("One word", journalBody("A".repeat(50)), "published");
const author = await makeUser("short");
const before = await bucketFor(author.id);
const result = await publish(author, "One word", journalBody("A".repeat(50)), "published");
expect(result.success).toBe(true);
expect(await bucketFor(user.id)).toBe(before);
expect(await bucketFor(author.id)).toBe(before);
});
it("grants on draft -> published and does not double-count on a later edit", async () => {
authSpy.mockResolvedValue({ user });
const created = await publish("Published later", journalBody("x".repeat(250)), "draft");
const author = await makeUser("later");
const created = await publish(author, "Published later", journalBody("x".repeat(250)), "draft");
expect(created.success).toBe(true);
const id = created.data as number;
const before = await bucketFor(user.id);
const before = await bucketFor(author.id);
authSpy.mockResolvedValue({ user: author });
await updateJournal(id, {
title: "Published later",
body: journalBody("x".repeat(250)),
@ -158,7 +177,7 @@ describe("Journal contribution XP", () => {
occurredAt: null,
mission: null,
});
expect(await bucketFor(user.id)).toBe(before + 5);
expect(await bucketFor(author.id)).toBe(before + 25);
// A subsequent edit of the already-published entry must not mint again.
await updateJournal(id, {
@ -168,53 +187,35 @@ describe("Journal contribution XP", () => {
occurredAt: null,
mission: null,
});
expect(await bucketFor(user.id)).toBe(before + 5);
expect(await bucketFor(author.id)).toBe(before + 25);
const entries = await payload.count({
collection: "contribution-entries",
where: {
and: [
{ sourceCollection: { equals: "journals" } },
{ sourceId: { equals: id } },
],
},
overrideAccess: true,
});
const entries = await entriesForJournal(id);
expect(entries.totalDocs).toBe(1);
});
it("pulls the XP back when the entry is deleted", async () => {
authSpy.mockResolvedValue({ user });
const created = await publish("Delete me", journalBody("x".repeat(250)), "published");
const author = await makeUser("delete");
const created = await publish(author, "Delete me", journalBody("x".repeat(250)), "published");
expect(created.success).toBe(true);
const id = created.data as number;
const before = await bucketFor(user.id);
const before = await bucketFor(author.id);
authSpy.mockResolvedValue({ user: author });
const result = await deleteJournal(id);
expect(result.success).toBe(true);
expect(await bucketFor(user.id)).toBe(before - 5);
expect(await bucketFor(author.id)).toBe(before - 25);
const revoked = await payload.find({
collection: "contribution-entries",
where: {
and: [
{ sourceCollection: { equals: "journals" } },
{ sourceId: { equals: id } },
],
},
depth: 0,
overrideAccess: true,
});
const revoked = await entriesForJournal(id);
expect(revoked.docs[0]?.status).toBe("revoked");
expect(revoked.docs[0]?.revokedReason).toContain("deleted");
});
it("caps journal XP at 25 per rolling day", async () => {
const capUser = await makeUser("cap");
it("allows a single journal entry per rolling day", async () => {
const author = await makeUser("cap");
let granted = 0;
for (let i = 0; i < 6; i += 1) {
for (let i = 0; i < 2; i += 1) {
const result = await recordContribution(payload, {
userId: capUser.id,
userId: author.id,
type: "journal-publish",
sourceCollection: "journals",
sourceId: 9_000_000 + i,
@ -222,8 +223,8 @@ describe("Journal contribution XP", () => {
});
if (result.status === "granted") granted += 1;
}
expect(granted).toBe(5);
expect(await bucketFor(capUser.id)).toBe(25);
expect(granted).toBe(1);
expect(await bucketFor(author.id)).toBe(25);
});
});