Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

12.3 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ConnectionInfo, PersonaInfo } from "@/types/template";

const apiRequestMock = vi.hoisted(() => vi.fn());
const resolveAgentForTemplateMock = vi.hoisted(() => vi.fn());

vi.mock("@/lib/api-wrapper", async () => {
  const actual = await vi.importActual<typeof import("@/lib/api-wrapper")>(
    "@/lib/api-wrapper"
  );
  return {
    ...actual,
    apiRequest: apiRequestMock,
  };
});

vi.mock("@/lib/utils", async () => {
  const actual = await vi.importActual<typeof import("@/lib/utils")>("@/lib/utils");
  return {
    ...actual,
    getApiUrl: () => "http://api.local",
  };
});

vi.mock("@/lib/template-agent-resolution", async () => {
  const actual = await vi.importActual<typeof import("@/lib/template-agent-resolution")>(
    "@/lib/template-agent-resolution"
  );
  return {
    ...actual,
    resolveAgentForTemplate: resolveAgentForTemplateMock,
  };
});

import {
  buildConnectAppsInteraction,
  buildSeedAssistantMessage,
  createSeededAgentTask,
  hireAgentFromTemplate,
} from "./hire-agent";

function jsonResponse(data: unknown, init?: ResponseInit) {
  return new Response(JSON.stringify(data), {
    status: 200,
    headers: { "Content-Type": "application/json" },
    ...init,
  });
}

const STRINGS = {
  beforeWeStart: "A few things before I start:",
  closingNote: "Answer what you can - I'll default the rest.",
  connectAppsLabel: "Connect your apps",
};

const LEO_PERSONA: PersonaInfo = {
  name: "Leo",
  role: "Email Lead Response Agent",
  avatar: "/marketplace/avatars/leo.png",
  intro: "Hi — I'm Leo, your Email Lead Response Agent.",
  kickoff_questions: [
    "What makes a lead worth chasing for you?",
    "Which calendar should I offer times from?",
  ],
};

describe("buildSeedAssistantMessage", () => {
  it("joins intro, a kickoff-questions bullet list, and the closing note", () => {
    const message = buildSeedAssistantMessage(LEO_PERSONA, STRINGS);

    expect(message).toBe(
      [
        "Hi — I'm Leo, your Email Lead Response Agent.",
        "A few things before I start:\n\n- What makes a lead worth chasing for you?\n- Which calendar should I offer times from?",
        "Answer what you can - I'll default the rest.",
      ].join("\n\n")
    );
  });

  it("omits the kickoff-questions section, and the closing note that refers back to it, when there are none", () => {
    // closingNote's copy ("Answer what you can...") only makes sense as a
    // reply to the kickoff questions above it - a persona with none must
    // not get a dangling, contextless closing sentence.
    const persona: PersonaInfo = { ...LEO_PERSONA, kickoff_questions: [] };

    const message = buildSeedAssistantMessage(persona, STRINGS);

    expect(message).not.toContain("A few things before I start");
    expect(message).not.toContain(STRINGS.closingNote);
    expect(message).toBe("Hi — I'm Leo, your Email Lead Response Agent.");
  });
});

describe("buildConnectAppsInteraction", () => {
  it("builds a connect_apps interaction from the template's connection names", () => {
    const connections: ConnectionInfo[] = [
      { name: "LinkedIn", logo: "https://example.com/linkedin.png" },
      { name: "Google Drive", logo: "https://example.com/drive.png" },
    ];

    expect(buildConnectAppsInteraction(connections, "Connect your apps")).toEqual({
      type: "connect_apps",
      field: "connect_apps",
      label: "Connect your apps",
      apps: ["LinkedIn", "Google Drive"],
    });
  });

  it("returns null when there are no connections", () => {
    expect(buildConnectAppsInteraction([], "Connect your apps")).toBeNull();
  });

  it("drops a whitespace-only connection name instead of passing it through as an unmatchable app", () => {
    const connections: ConnectionInfo[] = [
      { name: "   " },
      { name: "LinkedIn", logo: "https://example.com/linkedin.png" },
    ];

    expect(buildConnectAppsInteraction(connections, "Connect your apps")).toEqual({
      type: "connect_apps",
      field: "connect_apps",
      label: "Connect your apps",
      apps: ["LinkedIn"],
    });
  });

  it("returns null when every connection name is whitespace-only", () => {
    expect(buildConnectAppsInteraction([{ name: "  " }], "Connect your apps")).toBeNull();
  });
});

describe("createSeededAgentTask", () => {
  beforeEach(() => {
    apiRequestMock.mockReset();
  });

  it("creates the first conversation for an existing agent with the welcome content", async () => {
    apiRequestMock.mockResolvedValueOnce(jsonResponse({ task_id: 91 }));

    const result = await createSeededAgentTask({
      agentId: 42,
      persona: LEO_PERSONA,
      strings: STRINGS,
      connections: [{ name: "Gmail" }],
    });

    expect(result).toEqual({ taskId: 91, agentId: 42 });
    const [url, init] = apiRequestMock.mock.calls[0];
    expect(url).toBe("http://api.local/api/chat/task/create");
    const body = JSON.parse((init as RequestInit).body as string);
    expect(body.agent_id).toBe(42);
    expect(body.seed_assistant_message).toContain("A few things before I start:");
    expect(body.seed_interactions).toEqual([
      {
        type: "connect_apps",
        field: "connect_apps",
        label: "Connect your apps",
        apps: ["Gmail"],
      },
    ]);
  });
});

describe("hireAgentFromTemplate", () => {
  beforeEach(() => {
    apiRequestMock.mockReset();
    resolveAgentForTemplateMock.mockReset();
  });

  it.each([{ connections: [] }, { connections: [{ name: "Gmail" }] }])("persists the onboarding summary and only relevant app connections: %j", async ({ connections }) => {
    resolveAgentForTemplateMock.mockResolvedValueOnce({ agent: { id: 42 }, created: true });
    apiRequestMock.mockResolvedValueOnce(jsonResponse({ task_id: 7 }));
    await hireAgentFromTemplate({
      templateId: "sales-email-lead-response-agent",
      persona: LEO_PERSONA,
      strings: STRINGS,
      connections,
      welcome: {
        openingMessage: "Hi, I'm Leo.",
        description: "Draft a reply for each inbound lead.",
        prompt: "Share an enquiry.",
      },
    });
    const body = JSON.parse(apiRequestMock.mock.calls[0][1].body);
    expect(body.seed_assistant_message).toBe("Hi, I'm Leo.");
    expect(body.seed_interactions[0]).toEqual({
      type: "teammate_summary",
      field: "teammate_summary",
      label: LEO_PERSONA.role,
      description: "Draft a reply for each inbound lead.",
      placeholder: "Share an enquiry.",
      apps: connections.map((connection) => connection.name),
    });
    expect(body.seed_interactions).toHaveLength(connections.length ? 2 : 1);
    if (connections.length) expect(body.seed_interactions[1].type).toBe("connect_apps");
    expect(body).not.toHaveProperty("message");
  });

  it("resolves under the persona name, then creates a task seeded with the opening message", async () => {
    resolveAgentForTemplateMock.mockResolvedValueOnce({
      agent: { id: 42, name: "Leo", template_id: "sales-email-lead-response-agent" },
      created: true,
    });
    apiRequestMock.mockResolvedValueOnce(jsonResponse({ task_id: 7 }));

    const result = await hireAgentFromTemplate({
      templateId: "sales-email-lead-response-agent",
      persona: LEO_PERSONA,
      strings: STRINGS,
    });

    expect(result).toEqual({ taskId: 7, agentId: 42, created: true });
    expect(resolveAgentForTemplateMock).toHaveBeenCalledWith(
      "sales-email-lead-response-agent",
      "Leo"
    );

    const [url, init] = apiRequestMock.mock.calls[0];
    expect(url).toBe("http://api.local/api/chat/task/create");
    const body = JSON.parse((init as RequestInit).body as string);
    expect(body.agent_id).toBe(42);
    expect(body.title).toBe("Leo — Email Lead Response Agent");
    expect(body.seed_assistant_message).toContain("Hi — I'm Leo");
    expect(body.seed_interactions).toBeUndefined();
  });

  it("attaches a seed_interactions connect_apps card when the template has connections", async () => {
    resolveAgentForTemplateMock.mockResolvedValueOnce({
      agent: { id: 42, name: "Leo", template_id: "sales-email-lead-response-agent" },
      created: true,
    });
    apiRequestMock.mockResolvedValueOnce(jsonResponse({ task_id: 7 }));

    const connections: ConnectionInfo[] = [
      { name: "HubSpot", logo: "https://example.com/hubspot.png" },
    ];

    await hireAgentFromTemplate({
      templateId: "sales-email-lead-response-agent",
      persona: LEO_PERSONA,
      strings: STRINGS,
      connections,
    });

    const [, init] = apiRequestMock.mock.calls[0];
    const body = JSON.parse((init as RequestInit).body as string);
    expect(body.seed_interactions).toEqual([
      {
        type: "connect_apps",
        field: "connect_apps",
        label: STRINGS.connectAppsLabel,
        apps: ["HubSpot"],
      },
    ]);
  });

  it("throws when the resolved agent id is missing", async () => {
    resolveAgentForTemplateMock.mockResolvedValueOnce({
      agent: { id: null, name: "Leo" },
      created: true,
    });

    await expect(
      hireAgentFromTemplate({
        templateId: "sales-email-lead-response-agent",
        persona: LEO_PERSONA,
        strings: STRINGS,
      })
    ).rejects.toThrow("Malformed resolve response");
    expect(apiRequestMock).not.toHaveBeenCalled();
  });

  it("throws on a non-ok task/create response", async () => {
    resolveAgentForTemplateMock.mockResolvedValueOnce({
      agent: { id: 42, name: "Leo" },
      created: false,
    });
    apiRequestMock.mockResolvedValueOnce(jsonResponse({ detail: "boom" }, { status: 500 }));

    await expect(
      hireAgentFromTemplate({
        templateId: "sales-email-lead-response-agent",
        persona: LEO_PERSONA,
        strings: STRINGS,
      })
    ).rejects.toThrow("Failed to create task for agent (500)");
  });

  it("throws on a malformed task/create response body", async () => {
    resolveAgentForTemplateMock.mockResolvedValueOnce({
      agent: { id: 42, name: "Leo" },
      created: false,
    });
    apiRequestMock.mockResolvedValueOnce(jsonResponse({}));

    await expect(
      hireAgentFromTemplate({
        templateId: "sales-email-lead-response-agent",
        persona: LEO_PERSONA,
        strings: STRINGS,
      })
    ).rejects.toThrow("Malformed task create response");
  });

  // Pins a PR review finding: resolve and task/create are 2 independent
  // network calls, each authenticated with whatever session apiRequest
  // finds live when IT fires - not one identity pinned at the start of
  // this function. A caller whose own identity-swap guard only checks
  // before invoking this function can't otherwise stop task/create running
  // under a DIFFERENT identity than the one that just resolved the agent,
  // if a swap lands in the gap between the two calls.
  it("aborts before task/create when abortIfIdentityChanged returns true after resolve", async () => {
    resolveAgentForTemplateMock.mockResolvedValueOnce({
      agent: { id: 42, name: "Leo" },
      created: true,
    });

    await expect(
      hireAgentFromTemplate({
        templateId: "sales-email-lead-response-agent",
        persona: LEO_PERSONA,
        strings: STRINGS,
        abortIfIdentityChanged: () => true,
      })
    ).rejects.toThrow("Aborted: authenticated identity changed mid-hire");

    expect(apiRequestMock).not.toHaveBeenCalled();
  });

  it("still creates the task when abortIfIdentityChanged returns false (identity unchanged)", async () => {
    resolveAgentForTemplateMock.mockResolvedValueOnce({
      agent: { id: 42, name: "Leo" },
      created: true,
    });
    apiRequestMock.mockResolvedValueOnce(jsonResponse({ task_id: 7 }));

    const result = await hireAgentFromTemplate({
      templateId: "sales-email-lead-response-agent",
      persona: LEO_PERSONA,
      strings: STRINGS,
      abortIfIdentityChanged: () => false,
    });

    expect(result).toEqual({ taskId: 7, agentId: 42, created: true });
    expect(apiRequestMock).toHaveBeenCalledTimes(1);
  });

  it("does not require abortIfIdentityChanged at all (other callers, e.g. the templates marketplace, don't pass it)", async () => {
    resolveAgentForTemplateMock.mockResolvedValueOnce({
      agent: { id: 42, name: "Leo" },
      created: true,
    });
    apiRequestMock.mockResolvedValueOnce(jsonResponse({ task_id: 7 }));

    await expect(
      hireAgentFromTemplate({
        templateId: "sales-email-lead-response-agent",
        persona: LEO_PERSONA,
        strings: STRINGS,
      })
    ).resolves.toEqual({ taskId: 7, agentId: 42, created: true });
  });
});