mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
test(mcp): consolidate derived operation progress coverage (#12778)
* test(mcp): consolidate derived operation cancellation and progress checks * test(mcp): assert progress phases independently of wording * test(mcp): narrow unknown notification messages before checking phases
This commit is contained in:
1 parent
adb8e2ea35
commit
3caa46f980
1 file changed
+27
-61
@@ -1,9 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
runDerivedOperation,
|
||||
type DerivedOperationContext,
|
||||
} from "./runDerivedOperation.js";
|
||||
import { runDerivedOperation } from "./runDerivedOperation.js";
|
||||
import { EvidenceIntegrityError } from "../domain/evidenceErrors.js";
|
||||
import { FUNCTION_COMPARISON_EXAMPLE } from "../contracts/functionComparisonExample.js";
|
||||
import { EvidenceLedger } from "../application/investigation/EvidenceLedger.js";
|
||||
@@ -12,23 +9,19 @@ import { retainAnalysisResultSources } from "../application/investigation/record
|
||||
const context = (
|
||||
controller: AbortController,
|
||||
notifications: unknown[] = [],
|
||||
onMessage: (message: string) => void = () => undefined,
|
||||
) => ({
|
||||
mcpReq: {
|
||||
signal: controller.signal,
|
||||
_meta: { progressToken: "derived-test" },
|
||||
notify: (notification: unknown) => {
|
||||
notifications.push(notification);
|
||||
const message = (notification as { params?: { message?: unknown } })
|
||||
.params?.message;
|
||||
if (typeof message === "string") onMessage(message);
|
||||
return Promise.resolve();
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
describe("derived MCP operation boundary", () => {
|
||||
it("retains a completed observation and its source after terminal cancellation", async () => {
|
||||
it("retains completed Evidence when cancellation wins during terminal progress delivery", async () => {
|
||||
const controller = new AbortController();
|
||||
const reason = { requested: "completed-evidence-stop" };
|
||||
const source = FUNCTION_COMPARISON_EXAMPLE.left;
|
||||
@@ -37,9 +30,18 @@ describe("derived MCP operation boundary", () => {
|
||||
sources: [source],
|
||||
};
|
||||
const result = await runDerivedOperation(
|
||||
context(controller, [], (message) => {
|
||||
if (message.startsWith("complete:")) controller.abort(reason);
|
||||
}),
|
||||
{
|
||||
mcpReq: {
|
||||
...context(controller).mcpReq,
|
||||
notify: async (notification) => {
|
||||
const message = notification.params?.message;
|
||||
if (typeof message !== "string" || !message.startsWith("complete:"))
|
||||
return;
|
||||
await Promise.resolve();
|
||||
controller.abort(reason);
|
||||
},
|
||||
},
|
||||
},
|
||||
"compare",
|
||||
() => completed,
|
||||
(value) => value,
|
||||
@@ -48,6 +50,7 @@ describe("derived MCP operation boundary", () => {
|
||||
ok: false,
|
||||
error: {
|
||||
_tag: "AnalysisCancelledError",
|
||||
operation: "compare",
|
||||
cause: reason,
|
||||
partialObservation: completed.evidence,
|
||||
},
|
||||
@@ -107,56 +110,10 @@ describe("derived MCP operation progress and cancellation", () => {
|
||||
expect(result.ok ? undefined : result.error.cause).toBe(reason);
|
||||
});
|
||||
|
||||
it("preserves the request's abort reason when cancellation wins at terminal delivery", async () => {
|
||||
const controller = new AbortController();
|
||||
const reason = Object.freeze({ requested: "terminal-stop" });
|
||||
const result = await runDerivedOperation(
|
||||
context(controller, [], (message) => {
|
||||
if (message.startsWith("complete:")) controller.abort(reason);
|
||||
}),
|
||||
"compare",
|
||||
() => "must-not-publish",
|
||||
);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisCancelledError" },
|
||||
});
|
||||
expect(result.ok ? undefined : result.error.cause).toBe(reason);
|
||||
});
|
||||
|
||||
it("lets cancellation win while terminal progress delivery is pending", async () => {
|
||||
const controller = new AbortController();
|
||||
let terminalReached = false;
|
||||
const notifications: DerivedOperationContext = {
|
||||
mcpReq: {
|
||||
...context(controller).mcpReq,
|
||||
notify: async (notification) => {
|
||||
if (notification.params?.message === "complete: Completed compare") {
|
||||
terminalReached = true;
|
||||
await Promise.resolve();
|
||||
controller.abort();
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = await runDerivedOperation(
|
||||
notifications,
|
||||
"compare",
|
||||
() => "must-not-publish",
|
||||
);
|
||||
|
||||
expect(terminalReached).toBe(true);
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisCancelledError", operation: "compare" },
|
||||
});
|
||||
});
|
||||
|
||||
it("emits bounded monotonic progress with one terminal state", async () => {
|
||||
it("emits monotonic progress with one terminal state", async () => {
|
||||
const controller = new AbortController();
|
||||
const notifications: Array<{
|
||||
params?: { progress?: number; total?: number };
|
||||
params?: { progress?: number; total?: number; message?: string };
|
||||
}> = [];
|
||||
const result = await runDerivedOperation(
|
||||
context(controller, notifications),
|
||||
@@ -165,7 +122,16 @@ describe("derived MCP operation progress and cancellation", () => {
|
||||
);
|
||||
|
||||
expect(result).toEqual({ ok: true, value: "complete" });
|
||||
expect(notifications.map(({ params }) => params?.progress)).toEqual([1, 2]);
|
||||
const phases = notifications.map(
|
||||
({ params }) => params?.message?.split(":", 1)[0],
|
||||
);
|
||||
expect([
|
||||
["prepare", "complete"],
|
||||
["prepare", "compute", "complete"],
|
||||
]).toContainEqual(phases);
|
||||
expect(notifications.map(({ params }) => params?.progress)).toEqual(
|
||||
notifications.map((_notification, index) => index + 1),
|
||||
);
|
||||
expect(
|
||||
notifications.every(({ params }) => params?.total === undefined),
|
||||
).toBe(true);
|
||||
|
||||
Reference in new issue
Block a user