mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 13:41:17 -05:00
08db65115f
* test(application,desktop,browser,process): assert the specific refusal behind each bare toThrow Replace bare toThrow() with the errno code of a removed or occupied path, the Zod issue path and code, the YAML parser error code, the cleanup retry failure and the resources it still holds, the analysis error tag and reason, or the contract message each refusal reports. * test(desktop,process): assert retained cleanup resources and the nested metadata issue
177 lines
5.1 KiB
TypeScript
177 lines
5.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
|
|
import { readHomeFiles } from "./ComputerHomeFiles.js";
|
|
import {
|
|
AgentEnvComputer,
|
|
guestFileAnswerBytes,
|
|
guestFileAnswerRefusal,
|
|
guestFileAnswerWorkingBytes,
|
|
} from "./AgentEnvComputer.js";
|
|
|
|
describe("guest file encoding", () => {
|
|
const read = (encoded: string) =>
|
|
readHomeFiles(
|
|
async () => ({
|
|
result: {
|
|
parts: [
|
|
{
|
|
file: { bytes: encoded },
|
|
metadata: {
|
|
"urn:agentenv:data-objects/v1": { path: "selected.bin" },
|
|
},
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
(text) => text,
|
|
["selected.bin"],
|
|
);
|
|
|
|
it.each(["!!!", "aGVsbG8=garbage", "aGVsbG8", "Zh==", "AA-_"])(
|
|
"rejects malformed base64 %j before retaining file bytes",
|
|
async (encoded) => {
|
|
await expect(read(encoded)).rejects.toThrow(
|
|
"Expected canonical padded base64",
|
|
);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
Buffer.alloc(0),
|
|
Buffer.from("hello"),
|
|
Buffer.from([0, 255, 128, 0]),
|
|
])("preserves valid empty, text and binary files: %j", async (bytes) => {
|
|
await expect(read(bytes.toString("base64"))).resolves.toEqual([
|
|
{ path: "selected.bin", bytes },
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("guest data-object metadata", () => {
|
|
const namespace = [
|
|
"result",
|
|
"parts",
|
|
0,
|
|
"metadata",
|
|
"urn:agentenv:data-objects/v1",
|
|
];
|
|
it.each([
|
|
[null, namespace],
|
|
[42, namespace],
|
|
[{ path: 42 }, [...namespace, "path"]],
|
|
])("refuses malformed known-namespace facts: %j", async (metadata, path) => {
|
|
await expect(
|
|
readHomeFiles(
|
|
async () => ({
|
|
result: {
|
|
parts: [
|
|
{
|
|
file: { bytes: "aGVsbG8=" },
|
|
metadata: { "urn:agentenv:data-objects/v1": metadata },
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
(text) => text,
|
|
["selected.bin"],
|
|
),
|
|
).rejects.toMatchObject({
|
|
issues: [
|
|
{
|
|
path: [],
|
|
code: "invalid_union",
|
|
errors: expect.arrayContaining([
|
|
[expect.objectContaining({ path, code: "invalid_type" })],
|
|
]),
|
|
},
|
|
],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("guest file answers", () => {
|
|
it.each(
|
|
[true, false].flatMap((present) =>
|
|
[{ path: "other.txt" }, "download", 42, false, null, ["trace"]].map(
|
|
(unrelated) => ({ present, unrelated }),
|
|
),
|
|
),
|
|
)(
|
|
"uses only named data-object metadata (present: $present, unrelated: $unrelated)",
|
|
async ({ present, unrelated }) => {
|
|
const files = await readHomeFiles(
|
|
async () => ({
|
|
result: {
|
|
parts: [
|
|
{
|
|
kind: "file",
|
|
file: { bytes: Buffer.from("source bytes").toString("base64") },
|
|
metadata: {
|
|
"urn:unrelated": unrelated,
|
|
...(present
|
|
? {
|
|
"urn:agentenv:data-objects/v1": {
|
|
path: "selected.txt",
|
|
},
|
|
}
|
|
: {}),
|
|
},
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
(text) => text,
|
|
["selected.txt"],
|
|
);
|
|
expect(files).toEqual([
|
|
{
|
|
path: present ? "selected.txt" : undefined,
|
|
bytes: Buffer.from("source bytes"),
|
|
},
|
|
]);
|
|
},
|
|
);
|
|
|
|
it("counts base64, the envelope, and the copies held while the answer is parsed", () => {
|
|
expect(guestFileAnswerBytes(3, 1)).toBe(65_540);
|
|
expect(guestFileAnswerBytes(3, 2)).toBe(131_076);
|
|
const wire = 65_540;
|
|
expect(guestFileAnswerRefusal(3, 1, wire)).toBeInstanceOf(
|
|
AnalysisResourceConstraintError,
|
|
);
|
|
expect(guestFileAnswerRefusal(3, 1, wire * 3)).toBeUndefined();
|
|
expect(guestFileAnswerWorkingBytes(wire * 3)).toBe(wire);
|
|
});
|
|
|
|
it("refuses a reported size that expands past the working budget before naming a fetch", () => {
|
|
const ceiling = 1000;
|
|
const reported = 3000;
|
|
const refusal = guestFileAnswerRefusal(reported, 2, ceiling);
|
|
expect(refusal).toBeInstanceOf(AnalysisResourceConstraintError);
|
|
if (refusal === undefined) return;
|
|
expect(refusal.reportedLimits).toEqual({
|
|
guest_file_bytes: reported,
|
|
file_count: 2,
|
|
answer_bytes: 135_072,
|
|
answer_ceiling_bytes: ceiling,
|
|
answer_working_bytes: 333,
|
|
answer_copies: 3,
|
|
ceiling_source: "v8.heap_size_limit/4",
|
|
});
|
|
expect(refusal.remediationAction).toContain("v8.heap_size_limit/4");
|
|
expect(refusal.remediationAction).toContain("--max-old-space-size");
|
|
expect(refusal.remediationAction).toContain("Restart REA");
|
|
expect(guestFileAnswerBytes(reported, 2)).toBeGreaterThan(
|
|
guestFileAnswerWorkingBytes(ceiling),
|
|
);
|
|
});
|
|
|
|
it("does not ask the computer for a home file past that same ceiling", async () => {
|
|
const failure = await AgentEnvComputer.prototype.homeFiles
|
|
.call({}, ["out.json"], Number.MAX_SAFE_INTEGER)
|
|
.catch((cause: unknown) => cause);
|
|
expect(failure).toBeInstanceOf(AnalysisResourceConstraintError);
|
|
});
|
|
});
|