Keep fixed 30-second safety polling with separate recovery polling, confirm persistent heartbeat gaps, and refresh shared history after durable writes.
372 lines
10 KiB
TypeScript
372 lines
10 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import {
|
|
applyBidDelta,
|
|
mergeAuctionState,
|
|
shouldApplyIncomingAuctionState,
|
|
shouldApplyPolledAuctionState,
|
|
shouldResyncForBidDelta,
|
|
resolveCurrentBidPointAmount,
|
|
shouldResyncForAuctionIdentity,
|
|
createAuctionHeartbeatMonitor,
|
|
} from "../src/api/auction-state.ts";
|
|
import type { ApiAuction } from "../src/api/types.ts";
|
|
|
|
test("Redis-only snapshots retain the bid amount without a legacy alias", () => {
|
|
assert.equal(
|
|
resolveCurrentBidPointAmount({
|
|
currentHighestBidPointAmount: 310,
|
|
} as ApiAuction),
|
|
310,
|
|
);
|
|
assert.equal(
|
|
resolveCurrentBidPointAmount({ currentBid: 120 } as ApiAuction),
|
|
120,
|
|
);
|
|
assert.equal(
|
|
resolveCurrentBidPointAmount({
|
|
currentHighestBidPointAmount: 0,
|
|
currentBid: 120,
|
|
} as ApiAuction),
|
|
0,
|
|
);
|
|
assert.equal(
|
|
resolveCurrentBidPointAmount({
|
|
currentHighestBidPointAmount: NaN,
|
|
} as ApiAuction),
|
|
0,
|
|
);
|
|
});
|
|
|
|
test("heartbeats detect missed state and separately versioned side effects", () => {
|
|
const state = {
|
|
auctionLifecycleId: "life",
|
|
revision: 10,
|
|
matchSideEffectRevision: 2,
|
|
} as ApiAuction;
|
|
assert.equal(
|
|
shouldResyncForAuctionIdentity(state, {
|
|
auctionLifecycleId: "life",
|
|
revision: 11,
|
|
}),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
shouldResyncForAuctionIdentity(state, {
|
|
auctionLifecycleId: "life",
|
|
revision: 10,
|
|
matchSideEffectRevision: 3,
|
|
}),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
shouldResyncForAuctionIdentity(state, {
|
|
auctionLifecycleId: "life",
|
|
revision: 10,
|
|
matchSideEffectRevision: 2,
|
|
}),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
shouldResyncForAuctionIdentity(state, {
|
|
auctionLifecycleId: "new-life",
|
|
revision: 1,
|
|
}),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("a delayed heartbeat cannot pull an advanced socket backward", () => {
|
|
const advanced = { auctionLifecycleId: "life", revision: 12 } as ApiAuction;
|
|
assert.equal(
|
|
shouldResyncForAuctionIdentity(advanced, {
|
|
auctionLifecycleId: "life",
|
|
revision: 11,
|
|
matchSideEffectRevision: 9,
|
|
}),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("expired complete runtimes and legacy archives do not repeatedly resync", () => {
|
|
assert.equal(
|
|
shouldResyncForAuctionIdentity(
|
|
{ auctionLifecycleId: "life", phase: "complete" } as ApiAuction,
|
|
null,
|
|
),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
shouldResyncForAuctionIdentity({ phase: "bidding" } as ApiAuction, null),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
shouldResyncForAuctionIdentity(
|
|
{
|
|
auctionEngineVersion: "legacy-renamed",
|
|
auctionLifecycleId: "legacy-auction:1",
|
|
phase: "bidding",
|
|
} as ApiAuction,
|
|
null,
|
|
),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
shouldResyncForAuctionIdentity(
|
|
{
|
|
auctionEngineVersion: "redis-v1",
|
|
auctionLifecycleId: "life",
|
|
phase: "bidding",
|
|
} as ApiAuction,
|
|
null,
|
|
),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("a new in-flight bid does not confirm an earlier gap that already closed", () => {
|
|
const monitor = createAuctionHeartbeatMonitor();
|
|
const client = {
|
|
auctionEngineVersion: "redis-v1",
|
|
auctionLifecycleId: "life",
|
|
revision: 10,
|
|
} as ApiAuction;
|
|
assert.equal(
|
|
monitor.inspect(client, { auctionLifecycleId: "life", revision: 11 })
|
|
.needsResync,
|
|
false,
|
|
);
|
|
const caughtUp = { ...client, revision: 11 };
|
|
assert.equal(
|
|
monitor.inspect(caughtUp, { auctionLifecycleId: "life", revision: 12 })
|
|
.needsResync,
|
|
false,
|
|
);
|
|
assert.equal(
|
|
monitor.inspect(caughtUp, { auctionLifecycleId: "life", revision: 13 })
|
|
.needsResync,
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("heartbeat failures require two misses and reset after a healthy reply", () => {
|
|
const monitor = createAuctionHeartbeatMonitor();
|
|
assert.equal(monitor.recordFailure(), false);
|
|
assert.equal(monitor.recordFailure(), true);
|
|
monitor.inspect(
|
|
{ auctionEngineVersion: "legacy-renamed" } as ApiAuction,
|
|
null,
|
|
);
|
|
assert.equal(monitor.recordFailure(), false);
|
|
monitor.reset();
|
|
assert.equal(monitor.recordFailure(), false);
|
|
});
|
|
|
|
test("heartbeat confirmation covers side effects and missing runtimes", () => {
|
|
const monitor = createAuctionHeartbeatMonitor();
|
|
const client = {
|
|
auctionEngineVersion: "redis-v1",
|
|
auctionLifecycleId: "life",
|
|
revision: 10,
|
|
matchSideEffectRevision: 1,
|
|
} as ApiAuction;
|
|
const identity = {
|
|
auctionLifecycleId: "life",
|
|
revision: 10,
|
|
matchSideEffectRevision: 2,
|
|
};
|
|
assert.equal(monitor.inspect(client, identity).needsResync, false);
|
|
assert.equal(
|
|
monitor.inspect({ ...client, matchSideEffectRevision: 2 }, identity)
|
|
.isCurrent,
|
|
true,
|
|
);
|
|
assert.equal(monitor.inspect(client, null).needsResync, false);
|
|
assert.equal(monitor.inspect(client, null).needsResync, true);
|
|
monitor.reset();
|
|
const legacy = {
|
|
...client,
|
|
auctionEngineVersion: "legacy-renamed",
|
|
} as ApiAuction;
|
|
assert.equal(monitor.inspect(legacy, null).isCurrent, true);
|
|
assert.equal(monitor.inspect(legacy, null).needsResync, false);
|
|
});
|
|
|
|
const current = {
|
|
auctionLifecycleId: "life",
|
|
revision: 10,
|
|
phase: "bidding",
|
|
currentBidRoundId: "round-A",
|
|
currentBidRoundPlayerUserId: "A",
|
|
currentHighestBidPointAmount: 10,
|
|
auctionBidHistory: [],
|
|
bids: [],
|
|
} as unknown as ApiAuction;
|
|
const bid = {
|
|
auctionLifecycleId: "life",
|
|
revision: 11,
|
|
currentBidRoundId: "round-A",
|
|
currentHighestBidPointAmount: 20,
|
|
currentHighestBidderTeamIndex: 1,
|
|
auctionBidHistoryEntry: {
|
|
bidPointAmount: 20,
|
|
bidderTeamIndex: 1,
|
|
bidderUserId: "captain",
|
|
bidPlacedAtTimestampMS: 1000,
|
|
playerUserId: "A",
|
|
bidRoundId: "round-A",
|
|
},
|
|
};
|
|
|
|
test("a contiguous same-round bid updates price and preserves player identity", () => {
|
|
const next = applyBidDelta(current, bid)!;
|
|
assert.equal(shouldResyncForBidDelta(current, bid), false);
|
|
assert.equal(next.currentHighestBidPointAmount, 20);
|
|
assert.equal(next.currentBidRoundPlayerUserId, "A");
|
|
assert.equal(next.auctionBidHistory?.[0].bidRoundId, "round-A");
|
|
});
|
|
|
|
test("a revision gap resyncs instead of attaching a new player bid to the old structure", () => {
|
|
const missedTransition = {
|
|
...bid,
|
|
revision: 13,
|
|
currentBidRoundId: "round-B",
|
|
};
|
|
assert.equal(shouldResyncForBidDelta(current, missedTransition), true);
|
|
assert.equal(applyBidDelta(current, missedTransition), current);
|
|
});
|
|
|
|
test("a delta cannot open bidding or change the round by itself", () => {
|
|
assert.equal(
|
|
shouldResyncForBidDelta({ ...current, phase: "presenting" }, bid),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
shouldResyncForBidDelta(current, { ...bid, currentBidRoundId: "round-B" }),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("a full snapshot can repair the same revision but cannot regress auction revision", () => {
|
|
assert.equal(
|
|
shouldApplyIncomingAuctionState(current, {
|
|
...current,
|
|
currentBidRoundPlayerUserId: "B",
|
|
}),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
shouldApplyIncomingAuctionState(current, { ...current, revision: 9 }),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("full state repair preserves a newer independently versioned side effect", () => {
|
|
const live = {
|
|
...current,
|
|
matchSideEffectRevision: 3,
|
|
matchSideEffectStatus: "completed" as const,
|
|
};
|
|
const incoming = {
|
|
...current,
|
|
matchSideEffectRevision: 1,
|
|
matchSideEffectStatus: "pending" as const,
|
|
};
|
|
assert.equal(
|
|
mergeAuctionState(live, incoming).matchSideEffectStatus,
|
|
"completed",
|
|
);
|
|
assert.equal(
|
|
mergeAuctionState(live, { ...incoming, auctionLifecycleId: "new-life" })
|
|
.matchSideEffectStatus,
|
|
"pending",
|
|
);
|
|
});
|
|
|
|
test("replayed old bids do not resync or change the current state", () => {
|
|
const replay = { ...bid, revision: 9 };
|
|
assert.equal(shouldResyncForBidDelta(current, replay), false);
|
|
assert.equal(applyBidDelta(current, replay), current);
|
|
});
|
|
|
|
test("polled state repairs a socket that missed a transition", () => {
|
|
const polled = {
|
|
...current,
|
|
matchId: "match",
|
|
revision: 12,
|
|
currentBidRoundId: "round-B",
|
|
currentBidRoundPlayerUserId: "B",
|
|
currentHighestBidPointAmount: 0,
|
|
} as unknown as ApiAuction;
|
|
const socketState = { ...current, matchId: "match" } as ApiAuction;
|
|
assert.equal(
|
|
shouldApplyPolledAuctionState(socketState, polled, "match"),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("polled state never regresses or replaces the socket lifecycle", () => {
|
|
const socketState = { ...current, matchId: "match" } as ApiAuction;
|
|
const same = { ...socketState } as ApiAuction;
|
|
const older = { ...socketState, revision: 9 } as ApiAuction;
|
|
const otherLifecycle = {
|
|
...socketState,
|
|
auctionLifecycleId: "old-life",
|
|
revision: 99,
|
|
} as ApiAuction;
|
|
const otherMatch = { ...socketState, matchId: "other", revision: 11 };
|
|
assert.equal(shouldApplyPolledAuctionState(socketState, same, "match"), true);
|
|
assert.equal(
|
|
shouldApplyPolledAuctionState(socketState, older, "match"),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
shouldApplyPolledAuctionState(socketState, otherLifecycle, "match"),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
shouldApplyPolledAuctionState(socketState, otherMatch, "match"),
|
|
false,
|
|
);
|
|
assert.equal(shouldApplyPolledAuctionState(null, same, "match"), true);
|
|
});
|
|
|
|
test("polling initializes a missing socket baseline but rejects another match or missing identity", () => {
|
|
const incoming = {
|
|
...current,
|
|
matchId: "match",
|
|
currentBidRoundId: "round-B",
|
|
currentHighestBidPointAmount: 0,
|
|
} as ApiAuction;
|
|
assert.equal(shouldApplyPolledAuctionState(null, incoming, "match"), true);
|
|
assert.equal(
|
|
shouldApplyPolledAuctionState(null, incoming, "another-match"),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
shouldApplyPolledAuctionState(
|
|
null,
|
|
{ ...incoming, auctionLifecycleId: undefined },
|
|
"match",
|
|
),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("a prior side-effect generation cannot replace the status of a new transition", () => {
|
|
const old = {
|
|
...current,
|
|
matchSideEffectTransitionId: "old",
|
|
matchSideEffectRevision: 100,
|
|
matchSideEffectStatus: "failed" as const,
|
|
};
|
|
const next = {
|
|
...current,
|
|
revision: 12,
|
|
matchSideEffectTransitionId: "new",
|
|
matchSideEffectRevision: 3,
|
|
matchSideEffectStatus: "pending" as const,
|
|
};
|
|
assert.equal(mergeAuctionState(old, next).matchSideEffectStatus, "pending");
|
|
});
|