1
0
Fork 0

test: isolate locker-sync and award-automation specs from cross-file db state

Vitest 4 orders serial test files by cached durations, so adding the interior
suite reshuffled the schedule and exposed two order-dependent specs.

- arma-locker-sync: floor the apply-command scan at a beforeAll id baseline and
  assert the two enqueue tests via per-test id deltas (the pattern the echo-guard
  describe already used) instead of absolute totals that accumulate rows when
  foreign online game-servers change enqueue fan-out
- award-automation: scope every evaluateAwardRules call to this file's ruleIds,
  and track veteran/rookie USER ids separately from PROFILE ids because plans
  key on userId while makeUser returned profile ids; equality only held while
  both sequences stayed aligned
This commit is contained in:
Jason Fraley 2026-10-21 02:14:43 -04:00
parent a01ccaa7f1
commit 1738a66e40
2 changed files with 48 additions and 16 deletions

View file

@ -36,6 +36,9 @@ let blueprintId: number;
let structureId: number;
let lockerId: number;
let serverId: number;
// Highest arma-commands id when this file starts (set in beforeAll). Rows at or
// below it belong to earlier spec files and must never affect total counts here.
let cmdBaseline = 0;
const commandIds: number[] = [];
let eventSeq = 0;
@ -55,7 +58,8 @@ async function findLockerApplyCommands(): Promise<{ id: number; payload: unknown
const found = await payload.find({
collection: "arma-commands",
where: { type: { equals: "locker.apply" } },
limit: 100,
sort: "-id",
limit: 200,
depth: 0,
overrideAccess: true,
});
@ -65,6 +69,7 @@ async function findLockerApplyCommands(): Promise<{ id: number; payload: unknown
// vitest's timing-based file order put those files first).
return found.docs
.filter((doc) => {
if (doc.id <= cmdBaseline) return false;
const commandPayload = (doc as { payload?: unknown }).payload;
const instanceRef = Array.isArray(commandPayload) ? commandPayload[0] : undefined;
return (
@ -172,6 +177,18 @@ describe("Arma locker sync", () => {
depth: 0,
});
structureId = structure.id;
// Isolate from arma-commands rows left by earlier spec files (foreign online
// servers change locker fan-out and pollute the total-count assertions).
const latest = await payload.find({
collection: "arma-commands",
where: { id: { greater_than: 0 } },
sort: "-id",
limit: 1,
depth: 0,
overrideAccess: true,
});
cmdBaseline = (latest.docs[0]?.id as number | undefined) ?? 0;
});
afterAll(async () => {
@ -407,9 +424,16 @@ describe("Arma locker sync", () => {
});
serverId = server.id;
// Delta-scoped, not absolute: earlier sections of this file enqueue their
// own applies once any game-server (ours or a foreign leftover) is online,
// so only rows created by THIS event may be counted.
const beforeIds = new Set((await findLockerApplyCommands()).map((command) => command.id));
await processLockerEvent(payload, lockerEvent("locker.request", [`personal:${STEAM_ID}`], serverId));
const commands = await findLockerApplyCommands();
const commands = (await findLockerApplyCommands()).filter(
(command) => !beforeIds.has(command.id),
);
commandIds.push(...commands.map((command) => command.id));
expect(commands).toHaveLength(1);
const [instanceRef, stock] = commands[0].payload as [string, [string, number][]];
@ -419,9 +443,10 @@ describe("Arma locker sync", () => {
});
it("enqueues nothing for unknown instances", async () => {
const beforeIds = new Set((await findLockerApplyCommands()).map((command) => command.id));
await processLockerEvent(payload, lockerEvent("locker.request", [`structure:${RUN}-missing`], serverId));
const commands = await findLockerApplyCommands();
expect(commands).toHaveLength(1);
const fresh = (await findLockerApplyCommands()).filter((command) => !beforeIds.has(command.id));
expect(fresh).toHaveLength(0);
});
});

View file

@ -38,8 +38,12 @@ describe("Award automation sweeper", () => {
let tier1Id: number;
let tier2Id: number;
let tier3Id: number;
// Award plans are keyed by USER id; these vars hold PROFILE ids, so track
// both: they only coincide while user/profile sequences stay aligned.
let veteranId: number;
let rookieId: number;
let veteranUserId: number;
let rookieUserId: number;
let ruleIds: number[] = [];
async function makeAward(name: string, seriesTier?: number): Promise<number> {
@ -59,7 +63,7 @@ describe("Award automation sweeper", () => {
return created.id;
}
async function makeUser(username: string): Promise<number> {
async function makeUser(username: string): Promise<{ userId: number; profileId: number }> {
const user = (await payload.create({
collection: "users",
data: {
@ -80,7 +84,7 @@ describe("Award automation sweeper", () => {
overrideAccess: true,
});
if (!profiles.docs[0]) throw new Error(`profile not auto-created for ${username}`);
return profiles.docs[0].id;
return { userId: user.id, profileId: profiles.docs[0].id };
}
async function makeRule(rule: {
@ -131,15 +135,17 @@ describe("Award automation sweeper", () => {
tier2Id = await makeAward(`${RUN} YOS Silver`, 2);
tier3Id = await makeAward(`${RUN} YOS Gold`, 3);
const veteranProfileId = await makeUser("veteran");
veteranId = veteranProfileId;
const rookieProfileId = await makeUser("rookie");
rookieId = rookieProfileId;
const veteran = await makeUser("veteran");
veteranId = veteran.profileId;
veteranUserId = veteran.userId;
const rookie = await makeUser("rookie");
rookieId = rookie.profileId;
rookieUserId = rookie.userId;
// Veteran: enlisted 3 years ago, 150 contribution XP.
await payload.update({
collection: "profiles",
id: veteranProfileId,
id: veteranId,
data: {
dossier: { enlistmentDate: new Date(Date.now() - 3 * YEAR_MS).toISOString() },
progression: { contributions: { xpEarned: 150 } },
@ -150,7 +156,7 @@ describe("Award automation sweeper", () => {
// Rookie: enlisted 6 months ago, 10 XP.
await payload.update({
collection: "profiles",
id: rookieProfileId,
id: rookieId,
data: {
dossier: { enlistmentDate: new Date(Date.now() - 0.5 * YEAR_MS).toISOString() },
progression: { contributions: { xpEarned: 10 } },
@ -191,15 +197,16 @@ describe("Award automation sweeper", () => {
});
it("dry run plans grants and upgrades without writing anything", async () => {
const result = await evaluateAwardRules(payload, { dryRun: true });
const result = await evaluateAwardRules(payload, { dryRun: true, ruleIds });
expect(result.dryRun).toBe(true);
expect(result.rulesEvaluated).toBe(4); // inactive rule excluded
const veteranPlans = result.plans.filter((p) => p.userId === veteranId);
const veteranPlans = result.plans.filter((p) => p.userId === veteranUserId);
expect(veteranPlans.map((p) => p.awardId).sort()).toEqual(
[medalId, tier1Id, tier2Id].sort(),
);
expect(result.plans.some((p) => p.userId === rookieUserId)).toBe(false);
const upgradePlan = result.plans.find((p) => p.kind === "upgrade");
expect(upgradePlan?.awardId).toBe(tier2Id);
expect(upgradePlan?.replacesAwardId).toBe(tier1Id);
@ -213,7 +220,7 @@ describe("Award automation sweeper", () => {
});
it("apply writes grants and upgrades, then is idempotent", async () => {
const applied = await evaluateAwardRules(payload);
const applied = await evaluateAwardRules(payload, { ruleIds });
expect(applied.granted).toBe(2); // medal + tier 1
expect(applied.upgraded).toBe(1); // tier 1 -> tier 2 in place
@ -229,7 +236,7 @@ describe("Award automation sweeper", () => {
expect(await profileAwards(rookieId)).toHaveLength(0);
// Second run: nothing new to grant.
const rerun = await evaluateAwardRules(payload);
const rerun = await evaluateAwardRules(payload, { ruleIds });
expect(rerun.plans).toHaveLength(0);
expect(rerun.skips.some((s) => s.reason === "already-held")).toBe(true);
expect(await profileAwards(veteranId)).toHaveLength(2);