AkurAI Build
Menu

popagent

public

Latest change 2fb6f198c4c71ef37dffc8ac5dca8482068a7bfc - Add governed AkurAI Build maintenance by AkurAI Build

import { beforeAll, describe, expect, test } from "bun:test";
import type { ChannelSettingsInput } from "./api-types";
import { ChannelStore } from "./channels";
import { AgentTaskStore } from "./tasks";

const store = new ChannelStore();
const tasks = new AgentTaskStore();

beforeAll(async () => {
  await tasks.init();
  await store.init();
});

describe("ChannelStore", () => {
  test("persists the native channel configuration", async () => {
    const original = await store.getSettings();
    const input: ChannelSettingsInput = {
      enabled: !original.enabled,
      channelName: original.channelName === "#general" ? "#dispatch" : "#general",
      dispatchMode: original.dispatchMode === "every-message" ? "mentions" : "every-message",
      contextMessages: original.contextMessages === 20 ? 12 : 20,
      streaming: !original.streaming,
      toolDisplay: original.toolDisplay === "timeline" ? "compact" : "timeline",
    };
    try {
      expect(await store.updateSettings(input)).toMatchObject(input);
      expect(await new ChannelStore().getSettings()).toMatchObject(input);
    } finally {
      const { updatedAt: _updatedAt, ...restore } = original;
      await store.updateSettings(restore);
    }
  });

  test("atomically persists a channel message and its queued task", async () => {
    const marker = `channel-test-${crypto.randomUUID()}`;
    const before = await store.listMessages("general", { limit: 100 });
    const result = await store.createDispatch({
      channelId: "general",
      workspaceId: "default",
      content: marker,
      model: "test/model",
      authorId: "popagent-user",
      authorName: "You",
    });
    try {
      expect(result.task.status).toBe("queued");
      expect(result.message).toMatchObject({
        channelId: "general",
        workspaceId: "default",
        content: marker,
        taskId: result.task.id,
        authorName: "You",
      });
      expect(await store.listMessages("general", { limit: 100 })).toContainEqual(
        expect.objectContaining({ id: result.message.id, taskId: result.task.id }),
      );
    } finally {
      await store.storage.db.none("DELETE FROM popagent_channel_messages WHERE content=$1", [marker]);
      await store.storage.db.none("DELETE FROM popagent_agent_tasks WHERE id=$1", [result.task.id]);
    }
    expect((await store.listMessages("general", { limit: 100 })).length).toBe(before.length);
  });

  test("announces a task start once in general", async () => {
    const task = await tasks.createTask({
      workspaceId: "default",
      prompt: "Inspect the repository for one useful improvement",
      model: "test/model",
    });
    await store.storage.db.none(
      "UPDATE popagent_agent_tasks SET source='self-update' WHERE id=$1",
      [task.id],
    );
    task.source = "self-update";
    try {
      const first = await store.announceTaskStart(task);
      const duplicate = await store.announceTaskStart(task);
      expect(first).toMatchObject({
        channelId: "general",
        authorId: "orchistrator",
        authorName: "Orchistrator",
        taskId: task.id,
      });
      expect(first?.content).toContain("Started autonomous task");
      const update = await store.announceTaskUpdate(task, "Researcher", "Inspecting repository rules");
      const completion = await store.announceTaskUpdate(task, "Orchistrator", "Task completed");
      expect(update).toMatchObject({ authorName: "Researcher", content: "Inspecting repository rules" });
      expect(completion).toMatchObject({ authorName: "Orchistrator", content: "Task completed" });
      expect(duplicate).toBeUndefined();
      expect(await store.findMessageByTask("general", task.id)).toMatchObject({ id: first?.id });
    } finally {
      await store.storage.db.none("DELETE FROM popagent_channel_messages WHERE task_id=$1 OR author_id=$2", [task.id, `task:${task.id}`]);
      await store.storage.db.none("DELETE FROM popagent_agent_tasks WHERE id=$1", [task.id]);
    }
  });

  test("labels Build maintenance task starts distinctly", async () => {
    const task = await tasks.createTask({
      workspaceId: "default",
      prompt: "Inspect Build evidence",
      model: "test/model",
    });
    await store.storage.db.none(
      "UPDATE popagent_agent_tasks SET source='build-maintenance' WHERE id=$1",
      [task.id],
    );
    task.source = "build-maintenance";
    try {
      const message = await store.announceTaskStart(task);
      expect(message?.content).toContain("Started Build maintenance task");
    } finally {
      await store.storage.db.none("DELETE FROM popagent_channel_messages WHERE task_id=$1", [task.id]);
      await store.storage.db.none("DELETE FROM popagent_agent_tasks WHERE id=$1", [task.id]);
    }
  });

  test("paginates history with stable message cursors", async () => {
    const marker = `channel-page-${crypto.randomUUID()}`;
    const first = await store.createMessage({ channelId: "general", workspaceId: "default", content: `${marker}-1`, authorId: "user", authorName: "You" });
    const second = await store.createMessage({ channelId: "general", workspaceId: "default", content: `${marker}-2`, authorId: "user", authorName: "You" });
    try {
      const older = await store.listMessages("general", { limit: 10, before: second.id });
      expect(older).toContainEqual(expect.objectContaining({ id: first.id }));
      expect(older.some((message) => message.id === second.id)).toBe(false);
      await expect(store.listMessages("general", { before: "missing" })).rejects.toThrow("cursor");
    } finally {
      await store.storage.db.none("DELETE FROM popagent_channel_messages WHERE content LIKE $1", [`${marker}%`]);
    }
  });

  test("reports durable history that blocks workspace deletion", async () => {
    const marker = `channel-workspace-${crypto.randomUUID()}`;
    const workspaceId = `workspace-${crypto.randomUUID()}`;
    await store.createMessage({ channelId: "general", workspaceId, content: marker, authorId: "user", authorName: "You" });
    try {
      expect(await store.hasWorkspaceMessages(workspaceId)).toBe(true);
      expect(await store.hasWorkspaceMessages("missing-workspace")).toBe(false);
    } finally {
      await store.storage.db.none("DELETE FROM popagent_channel_messages WHERE content=$1", [marker]);
    }
  });

  test("rejects unknown channels and enforces bounded history", async () => {
    await expect(store.createDispatch({
      channelId: "missing",
      workspaceId: "default",
      content: "test",
      model: "test/model",
      authorId: "popagent-user",
      authorName: "You",
    })).rejects.toThrow("Channel not found");
    await expect(store.listMessages("general", { limit: 0 })).rejects.toThrow("limit");
    await expect(store.listMessages("general", { limit: 201 })).rejects.toThrow("limit");
  });
});