INTEGRITY Dokumentace

Testování vašich agentů

Protože Agents běží na Cloudflare Workers a Durable Objects, lze je testovat stejnými nástroji a technikami jako Workers a Durable Objects.

Psaní a spouštění testů

Nastavení

Než napíšete svůj první test, nainstalujte potřebné balíčky:

npm install vitest@^4.1.0 @cloudflare/vitest-plugin --save-dev

Ujistěte se, že váš vitest.config.js má cloudflareTest nakonfigurovaný plugin:

import { cloudflareTest } from "@cloudflare/vitest-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
	plugins: [
		cloudflareTest({
			wrangler: { configPath: "./wrangler.jsonc" },
		}),
	],
});

Napište test

Testy používají vitest framework. Základní sada testů pro váš Agent může ověřit, jak Agent reaguje na požadavky, ale může také jednotkově testovat metody a stav Agenta.

import { env, exports } from "cloudflare:workers";
import {
	createExecutionContext,
	waitOnExecutionContext,
} from "cloudflare:test";
import { describe, it, expect } from "vitest";
import worker from "../src";
import { Env } from "../src";

interface ProvidedEnv extends Env {}

describe("make a request to my Agent", () => {
	// Unit testing approach
	it("responds with state", async () => {
		// Provide a valid URL that your Worker can use to route to your Agent
		// If you are using routeAgentRequest, this will be /agents/:agent/:name
		const request = new Request<unknown, IncomingRequestCfProperties>(
			"http://example.com/agents/my-agent/agent-123",
		);
		const ctx = createExecutionContext();
		const response = await worker.fetch(request, env, ctx);
		await waitOnExecutionContext(ctx);
		expect(await response.json()).toEqual({ hello: "from your agent" });
	});

	it("also responds with state", async () => {
		const request = new Request("http://example.com/agents/my-agent/agent-123");
		const response = await exports.default.fetch(request);
		expect(await response.json()).toEqual({ hello: "from your agent" });
	});
});

Spustit testy

Spouštění testů probíhá pomocí vitest CLI:

npm run test
# or run vitest directly
npx vitest
  MyAgent
    ✓ should return a greeting (1 ms)

Test Files  1 passed (1)

Zkontrolujte dokumentace k testování pro další příklady a konfiguraci testů.

Lokální spouštění Agents

Agenta můžete také spustit lokálně pomocí wrangler CLI:

npx wrangler dev
Your Worker and resources are simulated locally via Miniflare. For more information, see: https://developers.cloudflare.com/workers/testing/local-development.

Your worker has access to the following bindings:
- Durable Objects:
  - MyAgent: MyAgent
  Starting local server...
[wrangler:inf] Ready on http://localhost:53645

Tím se spustí lokální vývojový server se stejným runtime jako Cloudflare Workers, takže můžete kód svého Agenta upravovat a testovat lokálně bez nasazení.

Navštivte wrangler dev ↗ dokumentaci, kde najdete přehled příznaků CLI a možností konfigurace.