|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { GatewayBrowserClient } from "../../api/gateway.ts"; |
| 3 | +import { createGateway, createSessionsHarness, mountSidebar } from "../app-sidebar.ts"; |
| 4 | +import { waitForFast } from "../wait-for.ts"; |
| 5 | +import "../../components/app-sidebar.ts"; |
| 6 | + |
| 7 | +describe("AppSidebar child session cap", () => { |
| 8 | + it("caps visible children until requested and resets the cap after collapse", async () => { |
| 9 | + const gateway = createGateway({} as GatewayBrowserClient); |
| 10 | + const childKeys = Array.from( |
| 11 | + { length: 6 }, |
| 12 | + (_, index) => `agent:main:subagent:child-${index + 1}`, |
| 13 | + ); |
| 14 | + const harness = createSessionsHarness("main", ["agent:main:parent"]); |
| 15 | + harness.list.mockResolvedValue({ |
| 16 | + ts: 100_000, |
| 17 | + path: "", |
| 18 | + count: childKeys.length, |
| 19 | + defaults: { modelProvider: null, model: null, contextTokens: null }, |
| 20 | + sessions: childKeys.map((key, index) => ({ |
| 21 | + key, |
| 22 | + spawnedBy: "agent:main:parent", |
| 23 | + kind: "direct" as const, |
| 24 | + label: `Subagent: Child ${index + 1}`, |
| 25 | + updatedAt: index + 1, |
| 26 | + })), |
| 27 | + }); |
| 28 | + const { sidebar } = await mountSidebar(gateway, harness.sessions); |
| 29 | + harness.publishList({ |
| 30 | + result: { |
| 31 | + ts: 2, |
| 32 | + path: "", |
| 33 | + count: 1, |
| 34 | + defaults: { modelProvider: null, model: null, contextTokens: null }, |
| 35 | + sessions: [ |
| 36 | + { |
| 37 | + key: "agent:main:parent", |
| 38 | + kind: "direct", |
| 39 | + label: "Parent task", |
| 40 | + updatedAt: 1, |
| 41 | + childSessions: childKeys, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + }); |
| 46 | + await sidebar.updateComplete; |
| 47 | + |
| 48 | + const toggle = sidebar.querySelector<HTMLButtonElement>("[data-child-session-toggle]"); |
| 49 | + toggle?.click(); |
| 50 | + await waitForFast(() => |
| 51 | + expect(sidebar.querySelectorAll(".sidebar-recent-session--child")).toHaveLength(4), |
| 52 | + ); |
| 53 | + const showMore = sidebar.querySelector<HTMLButtonElement>("[data-show-more-children]"); |
| 54 | + expect(showMore?.textContent?.trim()).toBe("Show 2 more"); |
| 55 | + expect(showMore?.getAttribute("aria-label")).toBe("Show 2 more"); |
| 56 | + expect(sidebar.textContent).not.toContain("Subagent:"); |
| 57 | + |
| 58 | + showMore?.click(); |
| 59 | + await waitForFast(() => |
| 60 | + expect(sidebar.querySelectorAll(".sidebar-recent-session--child")).toHaveLength(6), |
| 61 | + ); |
| 62 | + expect(sidebar.querySelector("[data-show-more-children]")).toBeNull(); |
| 63 | + |
| 64 | + toggle?.click(); |
| 65 | + await sidebar.updateComplete; |
| 66 | + toggle?.click(); |
| 67 | + await waitForFast(() => |
| 68 | + expect(sidebar.querySelectorAll(".sidebar-recent-session--child")).toHaveLength(4), |
| 69 | + ); |
| 70 | + expect(sidebar.querySelector("[data-show-more-children]")?.textContent).toContain( |
| 71 | + "Show 2 more", |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it("keeps live children visible past the cap", async () => { |
| 76 | + const gateway = createGateway({} as GatewayBrowserClient); |
| 77 | + const childKeys = Array.from( |
| 78 | + { length: 6 }, |
| 79 | + (_, index) => `agent:main:subagent:child-${index + 1}`, |
| 80 | + ); |
| 81 | + const harness = createSessionsHarness("main", ["agent:main:parent"]); |
| 82 | + harness.list.mockResolvedValue({ |
| 83 | + ts: 100_000, |
| 84 | + path: "", |
| 85 | + count: childKeys.length, |
| 86 | + defaults: { modelProvider: null, model: null, contextTokens: null }, |
| 87 | + sessions: [ |
| 88 | + ...childKeys.map((key, index) => ({ |
| 89 | + key, |
| 90 | + spawnedBy: "agent:main:parent", |
| 91 | + kind: "direct" as const, |
| 92 | + label: `Subagent: Child ${index + 1}`, |
| 93 | + updatedAt: index + 1, |
| 94 | + })), |
| 95 | + // Quiet child beyond the cap with a RUNNING grandchild: the branch |
| 96 | + // must bypass the cap via the transitive runningChildCount. |
| 97 | + { |
| 98 | + key: "agent:main:subagent:grandchild", |
| 99 | + spawnedBy: "agent:main:subagent:child-6", |
| 100 | + kind: "direct" as const, |
| 101 | + label: "Subagent: Grandchild run", |
| 102 | + updatedAt: 10, |
| 103 | + status: "running" as const, |
| 104 | + hasActiveRun: true, |
| 105 | + }, |
| 106 | + ], |
| 107 | + }); |
| 108 | + const { sidebar } = await mountSidebar(gateway, harness.sessions); |
| 109 | + harness.publishList({ |
| 110 | + result: { |
| 111 | + ts: 2, |
| 112 | + path: "", |
| 113 | + count: 1, |
| 114 | + defaults: { modelProvider: null, model: null, contextTokens: null }, |
| 115 | + sessions: [ |
| 116 | + { |
| 117 | + key: "agent:main:parent", |
| 118 | + kind: "direct", |
| 119 | + label: "Parent task", |
| 120 | + updatedAt: 1, |
| 121 | + childSessions: childKeys, |
| 122 | + }, |
| 123 | + ], |
| 124 | + }, |
| 125 | + }); |
| 126 | + await sidebar.updateComplete; |
| 127 | + |
| 128 | + sidebar |
| 129 | + .querySelector<HTMLButtonElement>('[data-child-session-toggle="agent:main:parent"]') |
| 130 | + ?.click(); |
| 131 | + await waitForFast(() => |
| 132 | + expect( |
| 133 | + sidebar.querySelector('[data-session-key="agent:main:subagent:child-6"]'), |
| 134 | + ).not.toBeNull(), |
| 135 | + ); |
| 136 | + expect(sidebar.querySelector('[data-session-key="agent:main:subagent:child-5"]')).toBeNull(); |
| 137 | + expect(sidebar.querySelector("[data-show-more-children]")?.textContent?.trim()).toBe( |
| 138 | + "Show 1 more", |
| 139 | + ); |
| 140 | + }); |
| 141 | +}); |
0 commit comments