Fastify

10 Setup Tests

Test Fastify Anvia routes, streams, and provider boundaries.

Use Fastify inject for route tests. Mock the agent so unit tests do not call providers.

1. Install Test Tools

pnpm add -D vitest

2. Test The JSON Route

import { describe, expect, it, vi } from "vitest";
import { app } from "../src/app";

vi.mock("../src/ai/support-agent", () => ({
  supportAgent: {
    prompt: () => ({
      send: async () => ({
        output: "Reset links expire after 30 minutes.",
        usage: { totalTokens: 12 },
        messages: [],
      }),
    }),
  },
}));

describe("POST /api/support", () => {
  it("returns the agent output", async () => {
    const response = await app.inject({
      method: "POST",
      url: "/api/support",
      payload: { message: "How long does a reset link last?" },
    });

    expect(response.statusCode).toBe(200);
    expect(response.json().output).toBe("Reset links expire after 30 minutes.");
  });
});

3. Test The Stream Route

const response = await app.inject({
  method: "POST",
  url: "/api/support/stream",
  payload: { message: "Hello" },
});

expect(response.headers["content-type"]).toContain("application/x-ndjson");

Mock readableStream() with a small ReadableStream that emits one final event.

4. Test Studio Without A Port

import { Studio } from "@anvia/studio";
import { supportAgent } from "../src/ai/support-agent";

const studio = new Studio([supportAgent]);
const response = await studio.fetch(
  new Request("http://studio.test/agents/support/runs", {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ message: "Hello" }),
  }),
);

expect(response.status).toBe(200);

Next

Related guides: Testing, Tools and Pipelines, and Studio and Providers.