AkurAI Build
Menu

popagent

public

Latest change 4565eb356975518dfc0202eab29b56dff51e8f71 - add deterministic agent health validation by AkurAI Build

import { describe, expect, test } from "bun:test";
import { createCodeIntelligenceRoutes } from "./code-intelligence-routes";

function request(url: string, id = "workspace-a", init?: RequestInit) {
  return Object.assign(new Request(url, init), { params: { id } });
}

const workspaceStore = {
  get: async (id: string) => id === "workspace-a" ? { id } : undefined,
};

const service = {
  codeContext: async (workspaceId: string, query: string, limit: number) => ({ evidence: `${workspaceId}:${query}:${limit}` }),
  symbolContext: async (workspaceId: string, symbol: string, filePath?: string) => ({
    definition: `${workspaceId}:${symbol}:${filePath ?? "any"}`,
    callers: "callers",
    callees: "callees",
  }),
  changeImpact: async (workspaceId: string, paths: string[]) => ({
    paths,
    branchImpact: workspaceId,
    files: [],
    unresolved: [],
  }),
};

describe("code intelligence routes", () => {
  test("validate and route bounded workspace queries", async () => {
    const routes = createCodeIntelligenceRoutes({ workspaceStore: workspaceStore as never, service: service as never });

    const context = await routes["/api/workspaces/:id/code-context"]!(
      request("http://localhost/api/workspaces/workspace-a/code-context?q=browser%20policy&limit=6") as never,
    );
    expect(context.status).toBe(200);
    expect(await context.json()).toEqual({ evidence: "workspace-a:browser policy:6" });

    const symbol = await routes["/api/workspaces/:id/symbol-context"]!(
      request("http://localhost/api/workspaces/workspace-a/symbol-context?symbol=RepositoryBriefService&filePath=src%2Frepo-brief.ts") as never,
    );
    expect(symbol.status).toBe(200);
    expect(await symbol.json()).toEqual({
      definition: "workspace-a:RepositoryBriefService:src/repo-brief.ts",
      callers: "callers",
      callees: "callees",
    });

    const impact = await routes["/api/workspaces/:id/change-impact"]!(request(
      "http://localhost/api/workspaces/workspace-a/change-impact",
      "workspace-a",
      { method: "POST", headers: { "content-type": "application/json" }, body: '{"paths":["src/server.ts"]}' },
    ) as never);
    expect(impact.status).toBe(200);
    expect(await impact.json()).toEqual({
      paths: ["src/server.ts"],
      branchImpact: "workspace-a",
      files: [],
      unresolved: [],
    });
  });

  test("reject missing workspaces and invalid path input before indexing", async () => {
    const routes = createCodeIntelligenceRoutes({ workspaceStore: workspaceStore as never, service: service as never });

    expect((await routes["/api/workspaces/:id/code-context"]!(
      request("http://localhost/api/workspaces/missing/code-context?q=auth", "missing") as never,
    )).status).toBe(404);
    expect((await routes["/api/workspaces/:id/change-impact"]!(request(
      "http://localhost/api/workspaces/workspace-a/change-impact",
      "workspace-a",
      { method: "POST", headers: { "content-type": "application/json" }, body: '{"paths":["../secret"]}' },
    ) as never)).status).toBe(400);
  });
});