AkurAI Build
Menu

popagent

public

Latest change 6edc7e928c57dab23c0973ed3f7ee70b3ebb72d5 - Initial popagent baseline by AkurAI Build

import { afterAll, describe, expect, test } from "bun:test";
import type {
  ChannelListResponse,
  ChannelMessageListResponse,
  ChannelSettings,
} from "./api-types";
import { channels } from "./channels";
import { startServer } from "./server";

const server = startServer(0);
const base = `http://127.0.0.1:${server.port}`;

afterAll(() => server.stop(true));

describe("native channel API", () => {
  test("lists #general and validates persisted settings", async () => {
    const listResponse = await fetch(`${base}/api/channels`);
    expect(listResponse.status).toBe(200);
    expect((await listResponse.json() as ChannelListResponse).channels).toContainEqual(
      expect.objectContaining({ id: "general", name: expect.stringMatching(/^#/) }),
    );

    const original = await (await fetch(`${base}/api/settings/channels`)).json() as ChannelSettings;
    const { updatedAt: _updatedAt, ...originalInput } = original;
    const input = {
      ...originalInput,
      dispatchMode: original.dispatchMode === "mentions" ? "every-message" as const : "mentions" as const,
      contextMessages: original.contextMessages === 16 ? 17 : 16,
    };
    try {
      const response = await fetch(`${base}/api/settings/channels`, {
        method: "PATCH",
        headers: { "content-type": "application/json" },
        body: JSON.stringify(input),
      });
      expect(response.status).toBe(200);
      expect(await response.json()).toMatchObject(input);
      expect((await fetch(`${base}/api/settings/channels`, {
        method: "PATCH",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ ...input, contextMessages: 101 }),
      })).status).toBe(400);
    } finally {
      await fetch(`${base}/api/settings/channels`, {
        method: "PATCH",
        headers: { "content-type": "application/json" },
        body: JSON.stringify(originalInput),
      });
    }
  });

  test("posts a dispatch and returns its task-backed history", async () => {
    const marker = `channel-api-${crypto.randomUUID()}`;
    const settings = await channels.getSettings();
    const { updatedAt: _updatedAt, ...settingsInput } = settings;
    await channels.updateSettings({ ...settingsInput, enabled: true, dispatchMode: "every-message" });
    let taskId: string | undefined;
    try {
      const postResponse = await fetch(`${base}/api/channels/general/messages`, {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ content: marker, workspaceId: "default" }),
      });
      expect(postResponse.status).toBe(201);
      const posted = await postResponse.json() as { message: { id: string; taskId: string }; task: { id: string; status: string } };
      taskId = posted.task.id;
      expect(posted.message.taskId).toBe(taskId);
      expect(posted.task.status).toBe("queued");

      const historyResponse = await fetch(`${base}/api/channels/general/messages?limit=50`);
      expect(historyResponse.status).toBe(200);
      const history = await historyResponse.json() as ChannelMessageListResponse;
      expect(history.messages).toContainEqual(expect.objectContaining({
        content: marker,
        taskId,
        task: expect.objectContaining({ status: "queued" }),
      }));
    } finally {
      await channels.storage.db.none("DELETE FROM popagent_channel_messages WHERE content=$1", [marker]);
      if (taskId) await channels.storage.db.none("DELETE FROM popagent_agent_tasks WHERE id=$1", [taskId]);
      await channels.updateSettings(settingsInput);
    }
  });

  test("stores but does not dispatch messages without a mention in mentions mode", async () => {
    const marker = `channel-mention-${crypto.randomUUID()}`;
    const settings = await channels.getSettings();
    const { updatedAt: _updatedAt, ...settingsInput } = settings;
    await channels.updateSettings({ ...settingsInput, enabled: true, dispatchMode: "mentions" });
    try {
      const response = await fetch(`${base}/api/channels/general/messages`, {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ content: marker, workspaceId: "default" }),
      });
      expect(response.status).toBe(201);
      expect(await response.json()).toMatchObject({ message: { content: marker, taskId: null }, task: null });
    } finally {
      await channels.storage.db.none("DELETE FROM popagent_channel_messages WHERE content=$1", [marker]);
      await channels.updateSettings(settingsInput);
    }
  });
});