Update files
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { deriveSidebarShortcutAgentKeys, parseSidebarAgentKey } from "./sidebar-shortcuts";
|
||||
|
||||
describe("parseSidebarAgentKey", () => {
|
||||
it("parses serverId and agentId", () => {
|
||||
expect(parseSidebarAgentKey("server:agent")).toEqual({ serverId: "server", agentId: "agent" });
|
||||
});
|
||||
|
||||
it("returns null for invalid keys", () => {
|
||||
expect(parseSidebarAgentKey("")).toBeNull();
|
||||
expect(parseSidebarAgentKey("no-separator")).toBeNull();
|
||||
expect(parseSidebarAgentKey(":agent")).toBeNull();
|
||||
expect(parseSidebarAgentKey("server:")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveSidebarShortcutAgentKeys", () => {
|
||||
it("skips collapsed projects and preserves visual order", () => {
|
||||
const sections = [
|
||||
{
|
||||
projectKey: "p1",
|
||||
agents: [
|
||||
{ serverId: "s", id: "a1" },
|
||||
{ serverId: "s", id: "a2" },
|
||||
],
|
||||
},
|
||||
{
|
||||
projectKey: "p2",
|
||||
agents: [
|
||||
{ serverId: "s", id: "b1" },
|
||||
{ serverId: "s", id: "b2" },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
expect(deriveSidebarShortcutAgentKeys(sections, new Set(["p2"]), 9)).toEqual([
|
||||
"s:a1",
|
||||
"s:a2",
|
||||
]);
|
||||
});
|
||||
|
||||
it("limits to 9", () => {
|
||||
const sections = [
|
||||
{
|
||||
projectKey: "p1",
|
||||
agents: Array.from({ length: 20 }, (_, i) => ({ serverId: "s", id: `a${i + 1}` })),
|
||||
},
|
||||
];
|
||||
|
||||
expect(deriveSidebarShortcutAgentKeys(sections, new Set(), 9)).toHaveLength(9);
|
||||
expect(deriveSidebarShortcutAgentKeys(sections, new Set(), 9)[0]).toBe("s:a1");
|
||||
expect(deriveSidebarShortcutAgentKeys(sections, new Set(), 9)[8]).toBe("s:a9");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
export interface SidebarShortcutSection {
|
||||
projectKey: string;
|
||||
agents: Array<{ serverId: string; id: string }>;
|
||||
}
|
||||
|
||||
export function parseSidebarAgentKey(
|
||||
key: string
|
||||
): { serverId: string; agentId: string } | null {
|
||||
const sep = key.indexOf(":");
|
||||
if (sep === -1) {
|
||||
return null;
|
||||
}
|
||||
const serverId = key.slice(0, sep);
|
||||
const agentId = key.slice(sep + 1);
|
||||
if (!serverId || !agentId) {
|
||||
return null;
|
||||
}
|
||||
return { serverId, agentId };
|
||||
}
|
||||
|
||||
export function deriveSidebarShortcutAgentKeys(
|
||||
sections: SidebarShortcutSection[],
|
||||
collapsedProjectKeys: ReadonlySet<string>,
|
||||
limit = 9
|
||||
): string[] {
|
||||
const keys: string[] = [];
|
||||
const max = Math.max(0, Math.floor(limit));
|
||||
if (max === 0) {
|
||||
return keys;
|
||||
}
|
||||
|
||||
for (const section of sections) {
|
||||
if (collapsedProjectKeys.has(section.projectKey)) {
|
||||
continue;
|
||||
}
|
||||
for (const agent of section.agents) {
|
||||
keys.push(`${agent.serverId}:${agent.id}`);
|
||||
if (keys.length >= max) {
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user