Files
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

403 lines
12 KiB
TypeScript

/** Route-handler tests for the documents REST surface, driving handleDocumentsRoutes against a mocked document service and fetch impl. */
import { afterEach, describe, expect, it, vi } from "vitest";
import type { DocumentRouteContext } from "../src/routes.js";
import {
__setDocumentFetchImplForTests,
handleDocumentsRoutes,
} from "../src/routes.js";
const addDocument = vi.fn();
vi.mock("@elizaos/agent/api/documents-service-loader", () => ({
getDocumentsService: vi.fn(async () => ({
service: {
addDocument,
},
})),
getDocumentsServiceTimeoutMs: vi.fn(() => 0),
}));
type MockResponse = {
statusCode?: number;
body?: unknown;
headers: Record<string, string>;
setHeader: (name: string, value: string | number | readonly string[]) => void;
end: (chunk?: string) => void;
};
function buildCtx(args: {
method: string;
pathname: string;
body?: unknown;
runtime?: Partial<NonNullable<DocumentRouteContext["runtime"]>>;
}): {
ctx: DocumentRouteContext;
res: MockResponse;
} {
const getMemoryById = vi.fn();
const res: MockResponse = {
headers: {},
setHeader(name, value) {
res.headers[name.toLowerCase()] = Array.isArray(value)
? value.join(", ")
: String(value);
},
end(chunk) {
res.body = chunk ? JSON.parse(chunk) : undefined;
},
};
const ctx: DocumentRouteContext = {
req: { headers: {} } as DocumentRouteContext["req"],
res: res as DocumentRouteContext["res"],
method: args.method,
pathname: args.pathname,
url: new URL(`http://localhost${args.pathname}`),
runtime: {
agentId: "agent-id",
getSetting: () => undefined,
getMemoryById,
...args.runtime,
} as DocumentRouteContext["runtime"],
json(response, data, status = 200) {
response.statusCode = status;
response.setHeader("content-type", "application/json");
response.end(JSON.stringify(data));
},
error(response, message, status = 400) {
response.statusCode = status;
response.setHeader("content-type", "application/json");
response.end(JSON.stringify({ error: message }));
},
async readJsonBody<T>() {
return (args.body as T | undefined) ?? null;
},
decodePathComponent(value, response, label = "path component") {
try {
return decodeURIComponent(value);
} catch {
ctx.error(
response ?? res,
`Invalid ${label}: malformed URL encoding`,
400,
);
return null;
}
},
};
return { ctx, res };
}
describe("document routes", () => {
afterEach(() => {
vi.clearAllMocks();
__setDocumentFetchImplForTests(undefined);
});
it.each([
{},
{ url: {} },
{ url: " " },
])("rejects malformed URL upload body %# with a 400", async (body) => {
const fetchDocument = vi.fn();
__setDocumentFetchImplForTests(fetchDocument);
const { ctx, res } = buildCtx({
method: "POST",
pathname: "/api/documents/url",
body,
});
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(400);
expect(res.body).toEqual({ error: "url is required" });
expect(fetchDocument).not.toHaveBeenCalled();
expect(addDocument).not.toHaveBeenCalled();
});
it.each([
{ content: {}, filename: "doc.md" },
{ content: "hello", filename: {} },
{ content: " ", filename: "doc.md" },
{ content: "hello", filename: " " },
])("rejects malformed document upload body %# with a 400", async (body) => {
const { ctx, res } = buildCtx({
method: "POST",
pathname: "/api/documents",
body,
});
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(400);
expect(res.body).toEqual({
error: "content and filename must be non-empty strings",
});
expect(addDocument).not.toHaveBeenCalled();
});
it("rejects image uploads that would otherwise store placeholder text", async () => {
const { ctx, res } = buildCtx({
method: "POST",
pathname: "/api/documents",
body: {
content: "iVBORw0KGgo=",
filename: "photo.png",
contentType: "image/png",
},
});
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(400);
expect(res.body).toEqual({
error:
"Failed to add document: Image uploads require metadata.includeImageDescriptions=true so the document store can persist real searchable text.",
});
expect(addDocument).not.toHaveBeenCalled();
});
it("stores image uploads only after a real image description is produced", async () => {
const useModel = vi.fn(async () => ({
description: "A receipt for coffee with total $4.50.",
}));
addDocument.mockResolvedValueOnce({
clientDocumentId: "doc-id",
fragmentCount: 1,
});
const { ctx, res } = buildCtx({
method: "POST",
pathname: "/api/documents",
runtime: { useModel } as Partial<
NonNullable<DocumentRouteContext["runtime"]>
>,
body: {
content: "iVBORw0KGgo=",
filename: "receipt.png",
contentType: "image/png",
metadata: { includeImageDescriptions: true },
},
});
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(200);
expect(addDocument).toHaveBeenCalledWith(
expect.objectContaining({
contentType: "text/plain",
content:
"[Image: receipt.png]\n\nA receipt for coffee with total $4.50.",
}),
);
expect(res.body).toEqual({
ok: true,
documentId: "doc-id",
fragmentCount: 1,
});
});
it("rejects image uploads when the image description model fails", async () => {
const useModel = vi.fn(async () => {
throw new Error("vision unavailable");
});
const { ctx, res } = buildCtx({
method: "POST",
pathname: "/api/documents",
runtime: { useModel } as Partial<
NonNullable<DocumentRouteContext["runtime"]>
>,
body: {
content: "iVBORw0KGgo=",
filename: "receipt.png",
contentType: "image/png",
metadata: { includeImageDescriptions: true },
},
});
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(400);
expect(res.body).toEqual({
error:
"Failed to add document: Image description model failed: Error: vision unavailable",
});
expect(addDocument).not.toHaveBeenCalled();
});
it.each([
["GET", "/api/documents/%E0%A4%A"],
["GET", "/api/documents/%E0%A4%A/fragments"],
["PATCH", "/api/documents/%E0%A4%A"],
["DELETE", "/api/documents/%E0%A4%A"],
])("rejects malformed document id encoding for %s %s", async (method, pathname) => {
const { ctx, res } = buildCtx({ method, pathname });
const runtime = ctx.runtime as NonNullable<DocumentRouteContext["runtime"]>;
const getMemoryById = vi.mocked(runtime.getMemoryById);
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(400);
expect(res.body).toEqual({
error: "Invalid document id: malformed URL encoding",
});
expect(getMemoryById).not.toHaveBeenCalled();
});
it("links original bytes (mediaUrl/mediaHash/mediaFileName) when a file-storage service is present", async () => {
const store = vi.fn(
async (bytes: Buffer | Uint8Array, mimeType: string) => {
void bytes;
void mimeType;
return {
url: "/api/media/deadbeef.txt",
hash: "deadbeef",
fileName: "deadbeef.txt",
mimeType: "text/plain",
size: 11,
};
},
);
const getService = vi.fn(() => ({ store }));
addDocument.mockResolvedValueOnce({
clientDocumentId: "doc-id",
fragmentCount: 1,
});
const { ctx, res } = buildCtx({
method: "POST",
pathname: "/api/documents",
runtime: { getService } as Partial<
NonNullable<DocumentRouteContext["runtime"]>
>,
body: {
content: "hello world",
filename: "notes.txt",
contentType: "text/plain",
},
});
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(200);
expect(store).toHaveBeenCalledTimes(1);
// Text upload → bytes are UTF-8 of the original content.
expect((store.mock.calls[0][0] as Buffer).toString("utf8")).toBe(
"hello world",
);
expect(addDocument).toHaveBeenCalledWith(
expect.objectContaining({
metadata: expect.objectContaining({
mediaUrl: "/api/media/deadbeef.txt",
mediaHash: "deadbeef",
mediaFileName: "deadbeef.txt",
}),
}),
);
});
it("succeeds without a media link when no file-storage service is available", async () => {
addDocument.mockResolvedValueOnce({
clientDocumentId: "doc-id",
fragmentCount: 1,
});
const getService = vi.fn(() => null);
const { ctx, res } = buildCtx({
method: "POST",
pathname: "/api/documents",
runtime: { getService } as Partial<
NonNullable<DocumentRouteContext["runtime"]>
>,
body: {
content: "hello world",
filename: "notes.txt",
contentType: "text/plain",
},
});
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(200);
expect(res.body).toEqual({
ok: true,
documentId: "doc-id",
fragmentCount: 1,
});
const passedMetadata = (
addDocument.mock.calls[0][0] as {
metadata: Record<string, unknown>;
}
).metadata;
expect(passedMetadata.mediaUrl).toBeUndefined();
expect(passedMetadata.mediaHash).toBeUndefined();
expect(passedMetadata.mediaFileName).toBeUndefined();
});
it("does not fail the upload when the file-storage service throws", async () => {
const store = vi.fn(async () => {
throw new Error("disk full");
});
const getService = vi.fn(() => ({ store }));
const warn = vi.fn();
addDocument.mockResolvedValueOnce({
clientDocumentId: "doc-id",
fragmentCount: 1,
});
const { ctx, res } = buildCtx({
method: "POST",
pathname: "/api/documents",
runtime: { getService, logger: { warn } } as unknown as Partial<
NonNullable<DocumentRouteContext["runtime"]>
>,
body: {
content: "hello world",
filename: "notes.txt",
contentType: "text/plain",
},
});
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(200);
expect(store).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledTimes(1);
const passedMetadata = (
addDocument.mock.calls[0][0] as {
metadata: Record<string, unknown>;
}
).metadata;
expect(passedMetadata.mediaUrl).toBeUndefined();
});
it.each([
null,
42,
"not a document",
["hello"],
])("rejects non-object bulk item %# without throwing", async (document) => {
const { ctx, res } = buildCtx({
method: "POST",
pathname: "/api/documents/bulk",
body: { documents: [document] },
});
await expect(handleDocumentsRoutes(ctx)).resolves.toBe(true);
expect(res.statusCode).toBe(200);
expect(res.body).toEqual({
ok: false,
total: 1,
successCount: 0,
failureCount: 1,
results: [
{
index: 0,
ok: false,
filename: "document-1",
error: "content and filename must be non-empty strings",
},
],
});
expect(addDocument).not.toHaveBeenCalled();
});
});