Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

6.8 KB
Raw Blame
/**
 * Regression: clicking the "currently selected" Project in the Project dropdown must not clear
 * the Agent list and the Sessions under it.
 *
 * Old bug: setCurrentProjectId unconditionally called setAgents([]) + setAgentsLoading(true),
 * while currentProjectId itself didn't change — reloadAgents' effect depends on it and wouldn't
 * re-run, so the sidebar's Agents and the Sessions hanging under them would all disappear and
 * never come back (reproducible by simply clicking the current Project in the dropdown).
 *
 * A Project is deliberately created with a display name different from the username: a newly
 * registered user's initial Project display name defaults to the username, and the bottom-left
 * user menu also shows the username, so locating by name would match both.
 */
import { test, expect } from "@playwright/test";
import { provisionAndLogin } from "./auth.mjs";

const BASE = process.env.BASE_URL;
const MOCK = process.env.MOCK_URL;
const U = "projswitcher";
const P = "password123";
const PROJ = "SwitchTarget";

test("clicking the current Project in the dropdown: Agent and Session lists must not disappear", async ({
  page,
}) => {
  await provisionAndLogin(page.request, U, P);

  const initial = (await (await page.request.get(`${BASE}/api/projects`)).json()).projects[0];

  // A non-admin's Project id must be prefixed with <username>-.
  const created = await page.request.post(`${BASE}/api/projects`, {
    data: { projectId: `${U}-target`, name: PROJ },
  });
  expect(created.ok(), "create project").toBeTruthy();
  const projectId = (await created.json()).project.projectId;

  // Configure Model + credentials on both Projects: a Project without credentials pops an
  // onboarding overlay (fixed inset-0) as soon as the page loads, which would block every click on the sidebar.
  for (const id of [initial.projectId, projectId]) {
    const put = await page.request.put(`${BASE}/api/projects/${id}/models`, {
      data: {
        defaultModel: { provider: "custom", modelId: "claude-4-8" },
        models: [
          {
            provider: "custom",
            modelId: "claude-4-8",
            apiKey: "sk-mock",
            baseUrl: MOCK,
            contextWindow: 200000,
          },
        ],
      },
    });
    expect(put.ok(), `put models ${id}`).toBeTruthy();
  }

  await page.goto("/");

  // First wait for the initial Project's sidebar to settle: during this time it fetches the
  // Agent list and lands on the draft page (the brand heading marks the draft page); operating
  // the dropdown midway through would get closed by a subsequent re-render.
  const generalAgent = page.getByText("General Agent").first();
  const draftTitle = page.getByRole("heading", { name: "Adelie" });
  await expect(generalAgent).toBeVisible();
  await expect(draftTitle).toBeVisible();

  // Switch to this newly created Project (a different id: takes the normal switch path).
  const byName = page.getByRole("button", { name: PROJ });
  await page.getByRole("button", { name: U }).first().click(); // the initial Project's display name defaults to the username
  await byName.first().click();

  // Wait for the new Project's sidebar to settle too (Agent list + draft page).
  await expect(generalAgent).toBeVisible();
  await expect(draftTitle).toBeVisible();

  // The key action: click this "currently selected" Project again in the dropdown.
  await byName.first().click(); // the top trigger button (currently displaying PROJ)
  await expect(byName).toHaveCount(2); // the trigger button + the same-named item in the menu
  await byName.nth(1).click();

  // The regression point: both the Agent and the draft page are still there (before the fix,
  // these two assertions would fail — the sidebar gets cleared and stuck in loading).
  await expect(generalAgent).toBeVisible();
  await expect(draftTitle).toBeVisible();

  // —— Regression (#74 comment): switching Project on the plugin library page must not leave
  // installation state from the previous Project —— both Projects' default_agent share the same
  // name (agentId is the page state table's key): first uninstall agent-tuning from the current
  // (target) Project via the API, then switch over from the initial Project's plugin library page
  // — before the fix, the old Project's snapshot would overwrite the freshly fetched data, so
  // "Manage installation" would always show "Installed."
  //
  // Every skill of the plugin, not just one: an install is marked by *any* part of the plugin
  // being present (`pluginInstalled` — a partial copy is what an update completes), so removing
  // a single skill leaves the row legitimately reading "Installed". The list is the plugin's own
  // (`plugins/agent-tuning/skills/`), spelled out here like the rest of this spec's fixtures.
  const AGENT_TUNING_SKILLS = [
    "agent-evaluation",
    "agent-initialization",
    "agent-optimization",
    "benchmark-design",
  ];
  for (const skill of AGENT_TUNING_SKILLS) {
    const del = await page.request.delete(
      `${BASE}/api/projects/${projectId}/agents/default_agent/skills/${skill}`,
    );
    expect(del.ok(), `uninstall ${skill} on target project`).toBeTruthy();
  }
  // And the hook packages, when the plugin ships one (it does not today): same rule, so the
  // fixture does not rot again if one is added.
  for (const hook of ["agent-tuning"]) {
    const res = await page.request.delete(
      `${BASE}/api/projects/${projectId}/agents/default_agent/hooks/${hook}`,
    );
    expect([204, 404], `uninstall hook ${hook} on target project`).toContain(res.status());
  }

  // Switch back to the initial Project, populating the plugin library page's snapshot (default_agent has everything preinstalled -> Installed).
  await byName.first().click();
  await page.getByRole("button", { name: U }).first().click();
  await expect(generalAgent).toBeVisible();
  await page.getByRole("link", { name: "插件市场" }).click();
  await expect(page).toHaveURL(/\/plugins$/);
  // The installed section is folded on entry; its cards are inert until it is opened. The
  // library's plugin is agent-tuning; agent-initialization is one of its skills.
  await page.getByRole("button", { name: /已安装的插件|Installed plugins/ }).click();
  await page.getByRole("button", { name: "管理安装 agent-tuning" }).click();
  await expect(page.getByRole("button", { name: "卸载 default_agent" })).toBeVisible();
  await page.keyboard.press("Escape");

  // Staying on the plugin library page while switching to target: the same-named default_agent's state must flip to "Install" (not installed).
  await page.getByRole("button", { name: U }).first().click();
  await byName.first().click();
  await page.getByRole("button", { name: "管理安装 agent-tuning" }).click();
  await expect(page.getByRole("button", { name: "安装 default_agent" })).toBeVisible();
  await page.keyboard.press("Escape");
});