1
0
Fork 0
polaris-task-force/tests/int/award-metrics.int.spec.ts
Z8MB1E db9248244d feat(awards): add award automation platform
- award-rules collection: metric/operator/value threshold rules linked to
  awards, with auto-retire-lower-tiers and once-per-series flags, active
  toggle, and award-rules:* permissions
- src/lib/awards/metrics.ts: pure metric resolvers (years of service from
  dossier.enlistmentDate with account-age fallback, account age, missions
  attended, total XP) plus operator comparison, unit tested
- evaluateAwardRules() sweeper: idempotent, dry-run capable; grants land as
  profile rows with awardedBy null so the existing afterChange hook sends
  award:granted notifications; upgrades replace the lower series tier in
  place preserving the original date; emits award:rule-granted events
- award-tick payload bin for daily evaluation + admin Award Automation
  view with Evaluate now (dry run) preview and Apply grants
- seedYearsOfServiceRules.ts wires the four years-of-service ribbons into
  one advancement series and creates their rules (idempotent, dev seeded)
- migration add_award_rules_automation (table + awards event target enum)
2026-10-12 15:11:38 -04:00

97 lines
3.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
applyOperator,
qualifiesForRule,
resolveMetricValue,
sumProgressionXp,
yearsBetween,
type MemberFacts,
} from "@/lib/awards/metrics";
const NOW = new Date("2026-10-12T12:00:00Z");
const MS_PER_YEAR = 365.25 * 24 * 60 * 60 * 1000;
function facts(overrides: Partial<MemberFacts> = {}): MemberFacts {
return {
enlistmentDate: null,
accountCreatedAt: "2024-10-12T12:00:00Z",
missionsAttended: 0,
xpTotal: 0,
...overrides,
};
}
describe("award metric resolvers (pure)", () => {
describe("yearsBetween", () => {
it("computes fractional years", () => {
const twoYearsAgo = new Date(NOW.getTime() - 2 * MS_PER_YEAR).toISOString();
expect(yearsBetween(twoYearsAgo, NOW)).toBeCloseTo(2, 5);
});
it("clamps future and invalid dates to 0", () => {
expect(yearsBetween("2030-01-01T00:00:00Z", NOW)).toBe(0);
expect(yearsBetween("not-a-date", NOW)).toBe(0);
expect(yearsBetween(null, NOW)).toBe(0);
});
});
describe("resolveMetricValue", () => {
it("falls back to account age when enlistment date is missing", () => {
const value = resolveMetricValue("yearsOfService", facts(), NOW);
expect(value).toBeCloseTo(2, 1);
});
it("prefers enlistment date over account creation", () => {
const value = resolveMetricValue(
"yearsOfService",
facts({ enlistmentDate: "2026-01-12T12:00:00Z" }),
NOW,
);
expect(value).toBeLessThan(1);
});
it("resolves count metrics directly", () => {
const f = facts({ missionsAttended: 7, xpTotal: 1234 });
expect(resolveMetricValue("missionsAttended", f, NOW)).toBe(7);
expect(resolveMetricValue("xpTotal", f, NOW)).toBe(1234);
expect(resolveMetricValue("accountAge", f, NOW)).toBeCloseTo(2, 1);
});
});
describe("applyOperator / qualifiesForRule", () => {
it("gte includes the boundary, gt excludes it", () => {
expect(applyOperator("gte", 5, 5)).toBe(true);
expect(applyOperator("gt", 5, 5)).toBe(false);
expect(applyOperator("gte", 4.9, 5)).toBe(false);
expect(applyOperator("gt", 5.1, 5)).toBe(true);
});
it("qualifies via metric + operator + value", () => {
const rule = { metric: "missionsAttended" as const, operator: "gte" as const, value: 5 };
expect(qualifiesForRule(rule, facts({ missionsAttended: 5 }), NOW)).toBe(true);
expect(qualifiesForRule(rule, facts({ missionsAttended: 4 }), NOW)).toBe(false);
});
});
describe("sumProgressionXp", () => {
it("sums minigame, voiceover, and contributions buckets", () => {
const total = sumProgressionXp({
minigames: {
flappyBird: { xpEarned: 10 },
poker: { xpEarned: 5 },
blackjack: null,
},
voiceover: { xpEarned: 3 },
contributions: { xpEarned: 100 },
});
expect(total).toBe(118);
});
it("tolerates missing, null, and non-numeric values", () => {
expect(sumProgressionXp(undefined)).toBe(0);
expect(sumProgressionXp(null)).toBe(0);
expect(sumProgressionXp({ contributions: { xpEarned: null } })).toBe(0);
expect(sumProgressionXp({ minigames: { poker: { xpEarned: "nope" as never } } })).toBe(0);
});
});
});