Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

3.6 KB
Raw Blame
import { describe, expect, it, vi } from "vitest";
import { dispatchLocalAgentMention, mentionAck, parseLocalAgentMention, resolveLocalAgentMention } from "./mso-agent-mentions.mjs";

const rows = [
  { id: "session-b", name: "milo", alias: "agent-b", label: "[milo]", title: "Session B", titleSource: "auto", status: "idle", consumerConnected: true },
  { id: "session-z", name: "zahra", alias: "agent-c", label: "[zahra]", title: "Design review", titleSource: "manual", status: "idle", consumerConnected: true },
  { id: "session-o", name: "luna", alias: "agent-d", label: "[luna]", title: "Offline", titleSource: "auto", status: "offline", consumerConnected: false },
  { id: "session-g", name: "ghost", alias: "agent-e", label: "[ghost]", title: "Lease only", titleSource: "auto", status: "idle", consumerConnected: false },
];

describe("local agent @mention routing", () => {
  it("parses and resolves active public names only", () => {
    expect(parseLocalAgentMention("@milo tebak: apa yang punya banyak kunci?")).toEqual({ target: "milo", prompt: "tebak: apa yang punya banyak kunci?" });
    expect(resolveLocalAgentMention(rows, "milo").id).toBe("session-b");
    expect(resolveLocalAgentMention(rows, "zahra").id).toBe("session-z");
    expect(() => resolveLocalAgentMention(rows, "agent-b")).toThrow(/not found/i);
    expect(() => resolveLocalAgentMention(rows, "luna")).toThrow(/not found/i);
    expect(() => resolveLocalAgentMention(rows, "ghost")).toThrow(/not found/i);
  });

  it("fails unknown mentions with active public names only", () => {
    expect(() => resolveLocalAgentMention(rows, "missing")).toThrow(/@missing not found.*@milo.*@zahra/i);
  });

  it("dispatches a mention as a correlated request and persists the parent request", async () => {
    const history: unknown[] = [];
    const session = { agentSession: { id: "session-a" }, history };
    const api = vi.fn(async (_path: string, init?: RequestInit) => {
      if (!init?.method) return { agents: rows };
      const body = JSON.parse(String(init.body));
      expect(body).toMatchObject({ target: "session-b", intent: "request", requiresUserRelay: true, activeOnly: true });
      return {
        status: "delivered", target: rows[0],
        message: { id: "localmsg_11111111-1111-1111-1111-111111111111", correlationId: "localcorr_22222222-2222-2222-2222-222222222222", createdAt: new Date().toISOString() },
      };
    });
    const persist = vi.fn(async () => {});
    const out = await dispatchLocalAgentMention(session, "@milo tebak ini", { api, persist });
    expect(out?.result.status).toBe("delivered");
    expect(history).toEqual(expect.arrayContaining([expect.objectContaining({ role: "local_request", targetSessionId: "session-b", requiresUserRelay: true })]));
    expect(persist).toHaveBeenCalledOnce();
  });

  it("reports offline/queued UX without pretending the target ran", () => {
    expect(mentionAck({ target: { label: "[milo]" }, status: "target_offline" })).toMatch(/queued · target offline/i);
    expect(mentionAck({ target: { label: "[milo]" }, status: "queued" })).toMatch(/target busy/i);
  });

  it("does not expose a lease-only target as mentionable", () => {
    expect(() => resolveLocalAgentMention(rows, "ghost")).toThrow(/not found/i);
  });

  it("reports bounded lookup timeout without sending anything", async () => {
    const timeout = new Error("timeout"); timeout.name = "TimeoutError";
    await expect(dispatchLocalAgentMention({ agentSession: { id: "session-a" }, history: [] }, "@milo hi", {
      api: vi.fn(async () => { throw timeout; }), persist: vi.fn(),
    })).rejects.toThrow(/lookup timed out; no message was sent/i);
  });
});