Files
elizaos--eliza/plugins/plugin-coding-tools/src/actions/read.test.ts
T
wehub-resource-sync 426e9eeabd
Voice Workbench / headless workbench (mocked backends) (push) Has been cancelled
Voice Workbench / real acoustic lane (nightly, provisioned only) (push) Has been cancelled
ci / test (push) Has been cancelled
ci / lint-and-format (push) Has been cancelled
ci / build (push) Has been cancelled
ci / dev-startup (push) Has been cancelled
gitleaks / gitleaks (push) Has been cancelled
Markdown Links / Relative Markdown Links (push) Has been cancelled
Quality (Extended) / Homepage Build (PR smoke) (push) Has been cancelled
Quality (Extended) / Comment-only diff guard (push) Has been cancelled
Quality (Extended) / Format + Type Safety Ratchet (push) Has been cancelled
Quality (Extended) / Develop Gate (secret scan + UI determinism) (push) Has been cancelled
Quality (Extended) / Develop Gate (lint) (push) Has been cancelled
Chat shell gestures / Chat shell gesture + parity e2e (push) Has been cancelled
Cloud Gateway Discord / Test (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx @biomejs/biome check packages/lifeops-bench/src, benchmark-lint) (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx vitest run --config packages/lifeops-bench/vitest.config.ts --root packages/lifeops-bench --passWithNoTests, benchmark-tests) (push) Has been cancelled
Build Agent Image / build-and-push (push) Has been cancelled
Dev Smoke / bun run dev onboarding chat (push) Has been cancelled
Dev Smoke / Vite HMR dependency-level smoke (push) Has been cancelled
Electrobun Submodule Guard / electrobun gitlink is fetchable (push) Has been cancelled
Publish @elizaos/example-code / check_npm (push) Has been cancelled
Publish @elizaos/example-code / publish_npm (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / verify_version (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / publish_npm (push) Has been cancelled
Sandbox Live Smoke / Sandbox live smoke (push) Has been cancelled
Snap Build & Test / Build Snap (amd64) (push) Has been cancelled
Snap Build & Test / Build Snap (arm64) (push) Has been cancelled
Test Packaging / elizaos CLI global-install smoke (node + bun) (push) Has been cancelled
Cloud Gateway Webhook / Test (push) Has been cancelled
Cloud Tests / lint-and-types (push) Has been cancelled
Cloud Tests / unit-tests (push) Has been cancelled
Cloud Tests / integration-tests (push) Has been cancelled
Cloud Tests / e2e-tests (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Apps Worker (Product 2) / Determine environment (push) Has been cancelled
Deploy Apps Worker (Product 2) / Deploy apps worker to apps-control host (${{ needs.determine-env.outputs.environment }}) (push) Has been cancelled
Deploy Eliza Provisioning Worker / Determine environment (push) Has been cancelled
Deploy Eliza Provisioning Worker / Deploy worker to Hetzner host (${{ needs.determine-env.outputs.environment }} @ ${{ needs.determine-env.outputs.deployment_sha }}) (push) Has been cancelled
Dev Smoke / Classify changed paths (push) Has been cancelled
supply-chain / sbom (push) Has been cancelled
supply-chain / vulnerability-scan (push) Has been cancelled
Build, Push & Deploy to Phala Cloud / build-and-push (push) Has been cancelled
Test Packaging / Validate Packaging Configs (push) Has been cancelled
Test Packaging / Build & Test PyPI Package (push) Has been cancelled
Test Packaging / PyPI on Python ${{ matrix.python }} (push) Has been cancelled
Test Packaging / Pack & Test JS Tarballs (push) Has been cancelled
UI Fixture E2E / ui-fixture-e2e (push) Has been cancelled
UI Fixture E2E / fixture-e2e (push) Has been cancelled
UI Story Gate / story-gate (push) Has been cancelled
vault-ci / test (macos-latest) (push) Has been cancelled
vault-ci / test (ubuntu-latest) (push) Has been cancelled
vault-ci / test (windows-latest) (push) Has been cancelled
vault-ci / app-core wiring tests (push) Has been cancelled
verify-patches / verify patches/CHECKSUMS.sha256 (push) Has been cancelled
Voice Benchmark Smoke / voice-emotion fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voiceagentbench fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench-quality unit smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench TypeScript unit (no audio) (push) Has been cancelled
Voice Benchmark Smoke / voice bench smoke summary (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/app-core test bun run --cwd packages/elizaos test bun run --cwd packages/cloud/shared test], app-and-cli) (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/scenario-runner test bun run --cwd packages/vault test bun run --cwd packages/security test bun run --cwd plugins/plugin-coding-tools test], framework-packages) (push) Has been cancelled
Windows CI / windows ([bun run --cwd plugins/plugin-elizacloud test bun run --cwd plugins/plugin-discord test bun run --cwd plugins/plugin-anthropic test bun run --cwd plugins/plugin-openai test bun run --cwd plugins/plugin-app-control test bun run --cwd plugins/pl… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run build --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/agent --concurrency=4 node packages/scripts/run-bash-linux-only.mjs scripts/verify-riscv64-buildpaths.sh node packages/scripts/run… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run typecheck --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/cloud-shared --concurrency=4 bun run --cwd packages/core test bun run --cwd packages/shared test], core-runtime, 75) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:43:05 +08:00

268 lines
8.4 KiB
TypeScript

/** Tests for the FILE `read` handler: line/size caps and read recording, over the real filesystem. */
import * as fs from "node:fs/promises";
import * as path from "node:path";
import {
CAPABILITY_ROUTER_SERVICE_TYPE,
CapabilityError,
type ElizaCapabilityRouter,
type IAgentRuntime,
UnavailableCapabilityRouter,
} from "@elizaos/core";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { setupEnv, type TestEnv } from "./_test-helpers.js";
import { readFileHandler } from "./read.js";
describe("READ", () => {
let env: TestEnv;
beforeEach(async () => {
env = await setupEnv("read-test");
});
afterEach(async () => {
await env.cleanup();
});
it("reads a small file and returns numbered lines", async () => {
const file = path.join(env.tmpDir, "hello.txt");
await fs.writeFile(file, "line one\nline two\nline three", "utf8");
const result = await readFileHandler(env.runtime, env.message, undefined, {
parameters: { file_path: file },
});
expect(result.success).toBe(true);
expect(result.text).toContain(file);
expect(result.text).toContain("\tline one");
expect(result.text).toContain("\tline two");
expect(result.text).toContain("\tline three");
const data = result.data as Record<string, unknown> | undefined;
expect(data?.totalLines).toBe(3);
expect(data?.lines).toBe(3);
});
it("right-pads line numbers to 6 chars and uses tab separator", async () => {
const file = path.join(env.tmpDir, "lines.txt");
await fs.writeFile(file, "alpha\nbeta", "utf8");
const result = await readFileHandler(env.runtime, env.message, undefined, {
parameters: { file_path: file },
});
expect(result.success).toBe(true);
expect(result.text).toContain(" 1\talpha");
expect(result.text).toContain(" 2\tbeta");
});
it("respects offset and limit and marks truncated", async () => {
const file = path.join(env.tmpDir, "long.txt");
const lines = Array.from({ length: 50 }, (_, i) => `line ${i + 1}`);
await fs.writeFile(file, lines.join("\n"), "utf8");
const result = await readFileHandler(env.runtime, env.message, undefined, {
parameters: { file_path: file, offset: 10, limit: 5 },
});
expect(result.success).toBe(true);
expect(result.text).toContain("\tline 11");
expect(result.text).toContain("\tline 15");
expect(result.text).not.toContain("\tline 10");
expect(result.text).not.toContain("\tline 16");
const data = result.data as Record<string, unknown> | undefined;
expect(data?.truncated).toBe(true);
});
it("records the read in FileStateService", async () => {
const file = path.join(env.tmpDir, "track.txt");
await fs.writeFile(file, "hello", "utf8");
const result = await readFileHandler(env.runtime, env.message, undefined, {
parameters: { file_path: file },
});
expect(result.success).toBe(true);
const data = result.data as Record<string, unknown> | undefined;
const resolved = String(data?.path);
const meta = env.fileState.get("test-room", resolved);
expect(meta).toBeDefined();
expect(meta?.path).toBe(resolved);
});
it("prefers capability router for file content when available", async () => {
const file = path.join(env.tmpDir, "routed.txt");
await fs.writeFile(file, "local file content", "utf8");
const calls: string[] = [];
const router: ElizaCapabilityRouter = {
environment: "desktop",
availability: async () => ({
environment: "desktop",
available: true,
capabilities: {
fs: true,
pty: false,
git: false,
model: false,
plugin: false,
},
}),
fs: {
list: async () => {
throw new CapabilityError({
code: "CAPABILITY_UNAVAILABLE",
message: "fs unavailable",
capability: "fs",
method: "fs.list",
});
},
readText: async (params) => {
calls.push(params.path);
return {
path: params.path,
text: "routed line one\nrouted line two",
size: 29,
truncated: false,
};
},
writeText: async () => {
throw new CapabilityError({
code: "CAPABILITY_UNAVAILABLE",
message: "fs unavailable",
capability: "fs",
method: "fs.writeText",
});
},
},
pty: {
runCommand: async () => {
throw new CapabilityError({
code: "CAPABILITY_UNAVAILABLE",
message: "terminal unavailable",
capability: "pty",
method: "pty.command.run",
});
},
},
git: {
status: async () => {
throw new CapabilityError({
code: "CAPABILITY_UNAVAILABLE",
message: "git unavailable",
capability: "git",
method: "git.status",
});
},
diff: async () => {
throw new CapabilityError({
code: "CAPABILITY_UNAVAILABLE",
message: "git unavailable",
capability: "git",
method: "git.diff",
});
},
commandRun: async () => {
throw new CapabilityError({
code: "CAPABILITY_UNAVAILABLE",
message: "git unavailable",
capability: "git",
method: "git.command.run",
});
},
},
model: {
status: async () => {
throw new CapabilityError({
code: "CAPABILITY_UNAVAILABLE",
message: "model unavailable",
capability: "model",
method: "model.status",
});
},
},
plugin: new UnavailableCapabilityRouter("desktop").plugin,
};
const runtime = {
...env.runtime,
getService: <T>(serviceType: string): T | null =>
serviceType === CAPABILITY_ROUTER_SERVICE_TYPE
? (router as T)
: env.runtime.getService<T>(serviceType),
} as IAgentRuntime;
const result = await readFileHandler(runtime, env.message, undefined, {
parameters: { file_path: file },
});
expect(result.success).toBe(true);
expect(result.text).toContain("routed line one");
expect(result.text).not.toContain("local file content");
expect(calls).toEqual([file]);
const meta = env.fileState.get("test-room", file);
expect(meta).toBeDefined();
});
it("rejects relative paths", async () => {
const result = await readFileHandler(env.runtime, env.message, undefined, {
parameters: { file_path: "relative/path.txt" },
});
expect(result.success).toBe(false);
expect(result.text).toContain("invalid_param");
});
it("rejects paths under the blocklist", async () => {
const file = path.join(env.blockedPath, "secret.txt");
await fs.writeFile(file, "data");
const result = await readFileHandler(env.runtime, env.message, undefined, {
parameters: { file_path: file },
});
expect(result.success).toBe(false);
expect(result.text).toContain("path_blocked");
});
it("rejects files larger than CODING_TOOLS_MAX_FILE_SIZE_BYTES", async () => {
const env2 = await setupEnv("read-big", {
extraSettings: { CODING_TOOLS_MAX_FILE_SIZE_BYTES: 32 },
});
try {
const file = path.join(env2.tmpDir, "big.txt");
await fs.writeFile(file, "x".repeat(64), "utf8");
const result = await readFileHandler(
env2.runtime,
env2.message,
undefined,
{
parameters: { file_path: file },
},
);
expect(result.success).toBe(false);
expect(result.text).toContain("io_error");
expect(result.text).toContain("offset/limit");
} finally {
await env2.cleanup();
}
});
it("rejects binary files containing NUL bytes", async () => {
const file = path.join(env.tmpDir, "binary.bin");
await fs.writeFile(file, Buffer.from([0x68, 0x69, 0x00, 0x21]));
const result = await readFileHandler(env.runtime, env.message, undefined, {
parameters: { file_path: file },
});
expect(result.success).toBe(false);
expect(result.text).toContain("binary file");
});
it("fails when roomId is missing", async () => {
const result = await readFileHandler(
env.runtime,
{} as typeof env.message,
undefined,
{ parameters: { file_path: path.join(env.tmpDir, "any.txt") } },
);
expect(result.success).toBe(false);
expect(result.text).toContain("roomId");
});
});