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