Files
PaperClipAI/tests/runner-e2e/chat-catalog-domains/runtime-email.test.ts
T

22 lines
6.6 KiB
TypeScript

import { expect, it } from "vitest";
import { actualOriginalEmailSend, actualUncertainEmailReads, gradeUnknownEmail, uncertainEmailReply } from "./runtime-email.js";
function valid() { return { companyId: "company", leadId: "ceo", scenarioId: "recovery.unknown-email-outcome", origin: "http://127.0.0.1:42123", reply: "", agents: [], tasks: [{ id: "email-task" }, { id: "mail-work" }], activity: [], links: [], humanQueries: [], runs: [1, 2, 3, 4].map(id => ({ id: String(id), agentId: "ceo", status: "succeeded" })), state: { actualNormalTaskSendBound: true, actualOriginalUncertain: true, setupSendBound: true, faultOriginalBinding: true, outageActiveDuringCanonical: true, originalUncertaintyRecorded: true, canonicalReadBound: true, canonicalReply: "The original provider receipt is unavailable and the send remains uncertain. I will check the existing publication rather than resend a possibly delivered email.", noNewAgentPublication: true, directMatchedSentCount: 1, originalProviderReceiptMatched: true, outageReleased: true, reconciliationBound: true, resolutionActorBound: true, finalOutcome: "sent", confirmationBound: true, confirmationReply: "The original email was accepted and its saved publication is reconciled with the provider message receipt. There is no proof of recipient delivery, and no second send was made.", finalDirectMatchedSentCount: 1, sameOriginalRequestRetained: true, sameOriginalConversationRetained: true } }; }
it("accepts real original uncertainty, independent one-send proof and authorized same-publication reconciliation", () => expect(gradeUnknownEmail(valid()).passed).toBe(true));
it("accepts natural uncertainty and accepted-send paraphrases", () => { const o = valid(); o.state.canonicalReply = "It may already have reached the provider. I cannot confirm the outcome, so I checked the original send record and will avoid a duplicate while we reconcile the receipt."; o.state.confirmationReply = "The provider's original message is now verified as sent; the same conversation remains linked. Recipient delivery is still unconfirmed."; expect(gradeUnknownEmail(o).passed).toBe(true); });
it("rejects another actual email, a new agent publication, or loss of original uncertainty", () => { const o = valid(); o.state.directMatchedSentCount = 2; expect(gradeUnknownEmail(o).passed).toBe(false); const p = valid(); p.state.noNewAgentPublication = false; expect(gradeUnknownEmail(p).passed).toBe(false); const q = valid(); q.state.originalUncertaintyRecorded = false; expect(gradeUnknownEmail(q).passed).toBe(false); });
it("rejects automatic resend or certainty unsupported by the actual uncertain record", () => { expect(uncertainEmailReply("It is uncertain, but I will resend it now while we check the provider receipt.")).toBe(false); expect(uncertainEmailReply("The provider receipt is unavailable, but it definitely failed. I will resend it.")).toBe(false); });
it("rejects claims of delivery from accepted-send evidence and wrong final reply phase", () => { const o = valid(); o.state.confirmationReply += " It was delivered."; expect(gradeUnknownEmail(o).passed).toBe(false); const p = valid(); p.state.confirmationBound = false; expect(gradeUnknownEmail(p).passed).toBe(false); });
const expected = { companyId: "company", endpointId: "endpoint", publicationId: "publication", issueId: "email-task" };
function actualRead(api = false) { const name = api ? "call_api" : "agentmail_delivery", input = api ? { operationId: "GET /api/companies/{companyId}/email/deliveries/{publicationId}", pathParams: { companyId: "company", publicationId: "publication" } } : { publicationId: "publication" }, receipt = { id: "publication", issueId: "email-task", outcome: "uncertain", providerMessageId: null, request: { endpointId: "endpoint" } }; return [{ seq: 1, eventType: "item.started", payload: { prpEvent: { payload: { item: { id: "read", type: "tool_use", name, input } } } } }, { seq: 2, eventType: "item.completed", payload: { prpEvent: { payload: { item: { id: "read", type: "tool_result", result: api ? { ok: true, status: 200, data: receipt } : receipt } } } } }, { seq: 3, eventType: "tool.execution.completed", payload: { prpEvent: { payload: { executionId: "read", name, status: "completed" } } } }]; }
it("accepts actual completed connector and equivalent scoped API receipt reads", () => { expect(actualUncertainEmailReads(actualRead(), expected)).toHaveLength(1); expect(actualUncertainEmailReads(actualRead(true), expected)).toHaveLength(1); });
it("rejects wrong receipt identity, uncompleted or wrong-tool results, and a write before inspection", () => { expect(actualUncertainEmailReads(actualRead(), { ...expected, publicationId: "other" })).toHaveLength(0); expect(actualUncertainEmailReads(actualRead().slice(0, 2), expected)).toHaveLength(0); const wrong = actualRead(); wrong[2]!.payload.prpEvent.payload = { executionId: "read", name: "different_tool", status: "completed" }; expect(actualUncertainEmailReads(wrong, expected)).toHaveLength(0); const early = { seq: 0, eventType: "item.started", payload: { prpEvent: { payload: { item: { id: "send", type: "tool_use", name: "agentmail_send", input: {} } } } } }; expect(actualUncertainEmailReads([early, ...actualRead()], expected)).toHaveLength(0); });
it("accepts uncertainty about delivery while rejecting appended affirmative delivery", () => { expect(uncertainEmailReply("I cannot confirm whether it was delivered; I checked the original provider receipt and will avoid another send.")).toBe(true); expect(uncertainEmailReply("The provider outcome is uncertain. It was delivered.")).toBe(false); });
it("requires genuine normal-task send attribution rather than chat execution or fixture-only state", () => {
const target = { companyId: "company", taskId: "mail-work", endpointId: "endpoint", publicationId: "publication" };
const events = [{ eventType: "item.started", payload: { prpEvent: { payload: { item: { id: "send", type: "tool_use", name: "agentmail_send", input: { request: { parentIssueId: "mail-work", endpointId: "endpoint", idempotencyKey: "publication" } } } } } } }, { eventType: "item.completed", payload: { prpEvent: { payload: { item: { id: "send", type: "tool_result", result: { id: "publication", issueId: "email-child" } } } } } }, { eventType: "tool.execution.completed", payload: { prpEvent: { payload: { executionId: "send", name: "agentmail_send", status: "completed" } } } }];
expect(actualOriginalEmailSend(events, target)).toBe(true); expect(actualOriginalEmailSend(events, { ...target, taskId: "agent-chat" })).toBe(false); expect(actualOriginalEmailSend(events.slice(0, 2), target)).toBe(false);
const o = valid(); o.state.actualNormalTaskSendBound = false; expect(gradeUnknownEmail(o).passed).toBe(false);
});