ai-chatbot/scripts/test-agent-generation-pending-guard.ts

67 lines
1.6 KiB
TypeScript

import {
AGENT_PENDING_SERVER_ACCEPTANCE_TIMEOUT_MS,
getAgentGenerationAcceptanceRemainingMs,
isAgentGenerationRequestAccepted,
} from "@/lib/agents/pending-generation-guard";
function expect(condition: boolean, message: string) {
if (!condition) {
throw new Error(message);
}
}
const pending = {
requestMessageId: "request-2",
serverUserTurnCountAtStart: 1,
userTurnCountAtStart: 1,
};
expect(
!isAgentGenerationRequestAccepted({
pending,
serverMessages: [{ id: "request-1", role: "user" }],
}),
"an unrelated persisted user message must not acknowledge the request"
);
expect(
isAgentGenerationRequestAccepted({
pending,
serverMessages: [
{ id: "request-1", role: "user" },
{ id: "request-2", role: "user" },
],
}),
"the exact persisted request message must acknowledge the request"
);
expect(
isAgentGenerationRequestAccepted({
pending: {
requestMessageId: null,
serverUserTurnCountAtStart: 1,
userTurnCountAtStart: 1,
},
serverMessages: [
{ id: "request-1", role: "user" },
{ id: "legacy-request", role: "user" },
],
}),
"legacy pending state must recover by persisted user message count"
);
expect(
getAgentGenerationAcceptanceRemainingMs({
startedAt: 1_000,
now: 31_000,
}) ===
AGENT_PENDING_SERVER_ACCEPTANCE_TIMEOUT_MS - 30_000,
"acceptance timeout must retain the expected remaining duration"
);
expect(
getAgentGenerationAcceptanceRemainingMs({
startedAt: 1_000,
now: 91_000,
}) === 0,
"expired acceptance timeout must clamp to zero"
);
console.log("agent generation pending guard tests passed");