Files
reverse-engineering-anything/tests/acceptance/applications/progressiveAnalysisViews.test.ts
T
morluto b138f52e12 refactor: prune internal seams and preserve configuration ownership (#10430)
* refactor: prune internal seams and preserve configuration ownership

* fix(setup): keep YAML aliases and inherited registration settings on refresh

- Reuse an unchanged YAML collection only while every alias in it still
  resolves after the edit; otherwise rebuild it so the alias becomes its
  value. A sibling anchor replaced or moved in the same refresh no longer
  leaves an unresolved alias in Hermes or Goose output.
- Keep a connection selector whose existing value already chooses stdio
  (for example an inherited Hermes transport: stdio) instead of superseding
  it; remote selectors are still replaced. Explicit stdio selectors are
  therefore no longer rewritten (Kimi expectation updated).
- When the whole Hermes registration is inherited through a merge,
  override it with that merge and write only the changed fields.

* fix(setup): edit registration fields whose YAML key is an alias

A key written as an alias to a string scalar (for example *field with
field: &field env) was treated as a source-only key, so any change to
its value was refused in Hermes and Goose. Resolve such keys to their
string, keep the aliased spelling while its anchor survives the edit,
and write the plain key when the same edit rebuilds the anchor.

* test(process): bound cleanup-boundary waits and always release gates

Both cancellation tests awaited a bare started promise and released
their gates only on success, so a missed boundary waited for the outer
deadline and could leave cleanup parked. Use awaitAcknowledgement and
release and join the cleanup in finally.

* fix(setup): repair only aliases an edit leaves unresolved

Replace the pre-edit alias survival guess, which expanded aliases to
sibling anchors that survive unchanged, with an exact repair: record
what each alias in the replaced value resolves to, write the edit, then
replace only aliases left without a preceding anchor by that value.

- Keep an aliased key's source position, so its anchor stays ahead of
  later aliases.
- Treat an aliased key as its string only when no other key in the map
  resolves to the same string; an ambiguous one stays guarded.
- Override an inherited Hermes registration only when the requested
  entry keeps every inherited field, so an explicit replacement can drop
  remote selectors.

* fix(setup): repair aliases whose binding an edit changes, keeping comments

An alias whose own anchor was removed could resolve to an earlier anchor
with the same name and silently change value. Record the node each alias
resolved to and repair any alias that no longer resolves to that node.
Carry the alias's comments and spacing onto the repaired value.

* test(setup): split the Hermes anchor and merge refresh cases

* fix(setup): refuse removing an overridden key its merge would restore

The merge-retention check skipped keys the mapping also overrides, so
dropping such a key (for example a remote url during an explicit stdio
replacement) let the retained merge restore it. Check removal before
skipping overridden keys, so the edit is refused with the existing
message instead of producing a registration that cannot be applied.
2026-11-14 04:08:37 -05:00

99 lines
4.0 KiB
TypeScript

import { readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { evidenceBundleSchema } from "../../../src/domain/evidenceBundle.js";
import { apiContractSchema } from "../../../src/domain/apiContract/apiContract.js";
import { analysisViewResultSchema } from "../../../src/domain/analysisView/analysisViewResult.js";
import { toolContract } from "../../../src/contracts/toolContracts.js";
import { toolOutputSchemaWithMetadata } from "../../../src/contracts/toolSchemaMetadata.js";
it("loads retained analysis views only when needed, preserving complete schemas, source facts and Evidence", async () => {
const root = await createTestTempDirectory("rea-progressive-api-");
const path = join(root, "api.json");
await writeFile(
path,
JSON.stringify({
openapi: "3.1.0",
info: { title: "Fixture", version: "1" },
paths: { "/items": { get: { operationId: "listItems" } } },
}),
);
const { client } = await connectLocalToolsMcp({
REA_MCP_TOOL_CATALOG_PROFILE: "hot-cold",
REA_MCP_TOOL_FAMILIES: "application,session",
});
const names = async () =>
(await client.listTools()).tools.map((tool) => tool.name);
expect(await names()).not.toContain("inspect_analysis_view");
const changed = vi.fn();
client.setNotificationHandler("notifications/tools/list_changed", changed);
const call = async (name: string, args: Record<string, unknown>) => {
const result = await client.callTool({ name, arguments: args });
expect(result.isError, JSON.stringify(result.content)).not.toBe(true);
return result;
};
await call("discover_tools", { names: ["inspect_api_contract"] });
expect(await names()).toContain("inspect_api_contract");
const view = {
kind: "api-contract",
collection: "operations",
paths: ["/items"],
fields: ["method", "path"],
};
const first = parseEvidence(
(await call("inspect_api_contract", { path, view })).structuredContent,
);
const selected = analysisViewResultSchema.parse(first.normalized_result);
expect(selected).toMatchObject({
kind: "api-contract",
items: [{ method: "GET", path: "/items", state: "declared" }],
});
expect(await names()).not.toContain("inspect_analysis_view");
expect(changed).toHaveBeenCalledTimes(1);
await rm(path);
await call("discover_tools", {
names: ["inspect_analysis_view", "export_evidence_bundle"],
});
const loaded = (await client.listTools()).tools.find(
(tool) => tool.name === "inspect_analysis_view",
);
expect(loaded?.outputSchema).toEqual(
toolOutputSchemaWithMetadata(toolContract("inspect_analysis_view"))[
"~standard"
].jsonSchema.output({ target: "draft-2020-12" }),
);
expect(changed).toHaveBeenCalledTimes(2);
const source = {
kind: "retained-evidence",
evidence_id: selected.parent_evidence_id,
};
const follow = await call("inspect_analysis_view", { source, view });
expect(parseEvidence(follow.structuredContent)).toEqual(first);
const bundlePath = join(root, "complete.json");
await call("export_evidence_bundle", { path: bundlePath });
const bundle = evidenceBundleSchema.parse(
JSON.parse(await readFile(bundlePath, "utf8")),
);
const parent = bundle.records.find(
(record) => record.evidence_id === selected.parent_evidence_id,
);
expect(parent).toBeDefined();
const report = apiContractSchema.parse(parent?.normalized_result);
expect(report.operations).toMatchObject([
{
method: "GET",
path: "/items",
declared_id: "listItems",
source_pointer: "/paths/~1items/get",
},
]);
expect(follow.structuredContent).toHaveProperty("evidence_links", [
selected.parent_evidence_id,
]);
await call("discover_tools", { names: ["inspect_analysis_view"] });
expect(changed).toHaveBeenCalledTimes(2);
});