The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { presentThreadShell } from "@t3tools/client-runtime/state/models"; |
| 2 | import { threadKey } from "@t3tools/client-runtime/state/entities"; |
| 3 | import { isThreadWorking as isSidebarThreadWorking } from "@t3tools/client-runtime/state/thread-inbox"; |
| 4 | import * as DateTime from "effect/DateTime"; |
| 5 | import { deriveActiveWorkStartedAt } from "../session-logic.ts"; |
| 6 | import { describe, expect, it } from "vite-plus/test"; |
| 7 | import * as Cause from "effect/Cause"; |
| 8 | import { AsyncResult } from "effect/reactivity"; |
| 9 | import { |
| 10 | shouldCreateNewThreadInCurrentProject, |
| 11 | deleteSelectedThreadEntries, |
| 12 | getFallbackThreadIdAfterDelete, |
| 13 | hasUnseenCompletion, |
| 14 | orderItemsByPreferredIds, |
| 15 | pinOrderKeyBetween, |
| 16 | resolveSidebarSweepKeys, |
| 17 | resolveSidebarRowStatus, |
| 18 | resolveSidebarThreadStatus, |
| 19 | resolveThreadStatusPill, |
| 20 | resolveWindowTitleThreadStatus, |
| 21 | resolveWorkingStartedAt, |
| 22 | searchSidebarThreads, |
| 23 | shouldRecedeSidebarThread, |
| 24 | sortInboxThreadsByReturn, |
| 25 | resolveSidebarDropTarget, |
| 26 | planSidebarThreadDrop, |
| 27 | resolveSidebarReorderableThreadKeys, |
| 28 | sortPinnedThreadsForSidebar, |
| 29 | sortScopedProjectsForSidebar, |
| 30 | sortSidebarV2ProjectGroups, |
| 31 | shouldNavigateAfterThreadPark, |
| 32 | type SidebarListItem, |
| 33 | type SidebarListMarker, |
| 34 | type SidebarSection, |
| 35 | resolveSidebarDropVerb, |
| 36 | groupSidebarThreadsByProject, |
| 37 | resolveSidebarFolderDot, |
| 38 | sidebarFolderExpansionKeys, |
| 39 | } from "./Sidebar.logic"; |
| 40 | import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment"; |
| 41 | import { |
| 42 | sortActiveThreadsByOrderKey, |
| 43 | sortPinnedThreadsByOrderKey, |
| 44 | } from "@t3tools/client-runtime/state/thread-sort"; |
| 45 | import { resolveProjectExpanded } from "../uiStateStore"; |
| 46 | import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search"; |
| 47 | import { EnvironmentId, ProjectId, ProviderInstanceId, RunId, ThreadId } from "@t3tools/contracts"; |
| 48 | |
| 49 | import { |
| 50 | DEFAULT_INTERACTION_MODE, |
| 51 | DEFAULT_RUNTIME_MODE, |
| 52 | type Project, |
| 53 | type Thread, |
| 54 | } from "../types"; |
| 55 | import { makeThreadFixture, type ThreadFixtureOverrides } from "../test-fixtures"; |
| 56 | import { |
| 57 | completeSidebarRouteReveal, |
| 58 | pendingSidebarRouteReveal, |
| 59 | resolveSidebarRouteReveal, |
| 60 | } from "./sidebar/routeReveal"; |
| 61 | |
| 62 | const localEnvironmentId = EnvironmentId.make("environment-local"); |
| 63 | |
| 64 | describe("deleteSelectedThreadEntries", () => { |
| 65 | const entries = [{ threadKey: "one" }, { threadKey: "two" }, { threadKey: "three" }] as const; |
| 66 | const success = AsyncResult.success(undefined); |
| 67 | const failure = AsyncResult.failure(Cause.fail(new Error("Delete failed"))); |
| 68 | const interrupted = AsyncResult.failure(Cause.interrupt()); |
| 69 | |
| 70 | it("waits for each delete and excludes only earlier successes from worktree checks", async () => { |
| 71 | let resolveDelete!: (result: typeof success) => void; |
| 72 | const pendingDelete = new Promise<typeof success>((resolve) => { |
| 73 | resolveDelete = resolve; |
| 74 | }); |
| 75 | const worktreeChecks: { threadKey: string; deletedThreadKeys: string[] }[] = []; |
| 76 | const deletion = deleteSelectedThreadEntries({ |
| 77 | entries, |
| 78 | delete: async ({ threadKey }, deletedThreadKeys) => { |
| 79 | worktreeChecks.push({ threadKey, deletedThreadKeys: [...deletedThreadKeys] }); |
| 80 | return threadKey === "one" ? pendingDelete : success; |
| 81 | }, |
| 82 | }); |
| 83 | |
| 84 | expect(worktreeChecks).toEqual([{ threadKey: "one", deletedThreadKeys: [] }]); |
| 85 | resolveDelete(success); |
| 86 | const outcome = await deletion; |
| 87 | |
| 88 | expect(worktreeChecks).toEqual([ |
| 89 | { threadKey: "one", deletedThreadKeys: [] }, |
| 90 | { threadKey: "two", deletedThreadKeys: ["one"] }, |
| 91 | { threadKey: "three", deletedThreadKeys: ["one", "two"] }, |
| 92 | ]); |
| 93 | expect(outcome).toEqual({ |
| 94 | deletedThreadKeys: new Set(["one", "two", "three"]), |
| 95 | firstFailure: null, |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | it("continues after ordinary failures and keeps the first failure", async () => { |
| 100 | const laterFailure = AsyncResult.failure(Cause.fail(new Error("Later failure"))); |
| 101 | const deletedKeysAtLastEntry: string[][] = []; |
| 102 | const outcome = await deleteSelectedThreadEntries({ |
| 103 | entries: [...entries, { threadKey: "four" }], |
| 104 | delete: async ({ threadKey }, deletedThreadKeys) => { |
| 105 | if (threadKey === "one") return failure; |
| 106 | if (threadKey === "three") return laterFailure; |
| 107 | if (threadKey === "four") deletedKeysAtLastEntry.push([...deletedThreadKeys]); |
| 108 | return success; |
| 109 | }, |
| 110 | }); |
| 111 | |
| 112 | expect(deletedKeysAtLastEntry).toEqual([["two"]]); |
| 113 | expect(outcome).toEqual({ |
| 114 | deletedThreadKeys: new Set(["two", "four"]), |
| 115 | firstFailure: failure, |
| 116 | }); |
| 117 | }); |
| 118 | |
| 119 | it.each([ |
| 120 | { firstResult: success, deletedThreadKeys: new Set(["one"]), firstFailure: null }, |
| 121 | { firstResult: failure, deletedThreadKeys: new Set<string>(), firstFailure: failure }, |
| 122 | ])("stops on interruption and preserves earlier results %#", async (testCase) => { |
| 123 | const attemptedThreadKeys: string[] = []; |
| 124 | const outcome = await deleteSelectedThreadEntries({ |
| 125 | entries, |
| 126 | delete: async ({ threadKey }) => { |
| 127 | attemptedThreadKeys.push(threadKey); |
| 128 | return threadKey === "one" ? testCase.firstResult : interrupted; |
| 129 | }, |
| 130 | }); |
| 131 | |
| 132 | expect(attemptedThreadKeys).toEqual(["one", "two"]); |
| 133 | expect(outcome).toEqual({ |
| 134 | deletedThreadKeys: testCase.deletedThreadKeys, |
| 135 | firstFailure: testCase.firstFailure, |
| 136 | }); |
| 137 | }); |
| 138 | }); |
| 139 | |
| 140 | function makeLatestRun(overrides?: { |
| 141 | completedAt?: string | null; |
| 142 | startedAt?: string | null; |
| 143 | }): NonNullable<Thread["latestRun"]> { |
| 144 | return { |
| 145 | runId: "turn-1" as never, |
| 146 | status: "completed", |
| 147 | assistantMessageId: null, |
| 148 | requestedAt: "2026-03-09T10:00:00.000Z", |
| 149 | startedAt: |
| 150 | overrides?.startedAt !== undefined ? overrides.startedAt : "2026-03-09T10:00:00.000Z", |
| 151 | completedAt: |
| 152 | overrides?.completedAt !== undefined ? overrides.completedAt : "2026-03-09T10:05:00.000Z", |
| 153 | }; |
| 154 | } |
| 155 | |
| 156 | describe("orderItemsByPreferredIds", () => { |
| 157 | it("resolves legacy preference aliases without materializing project state", () => { |
| 158 | const ordered = orderItemsByPreferredIds({ |
| 159 | items: [ |
| 160 | { id: "physical-a", cwd: "/work/a" }, |
| 161 | { id: "physical-b", cwd: "/work/b" }, |
| 162 | { id: "physical-c", cwd: "/work/c" }, |
| 163 | ], |
| 164 | preferredIds: ["legacy:/work/c", "legacy:/work/a"], |
| 165 | getId: (project) => project.id, |
| 166 | getPreferenceIds: (project) => [project.id, `legacy:${project.cwd}`], |
| 167 | }); |
| 168 | |
| 169 | expect(ordered.map((project) => project.id)).toEqual([ |
| 170 | "physical-c", |
| 171 | "physical-a", |
| 172 | "physical-b", |
| 173 | ]); |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | describe("resolveSidebarThreadStatus", () => { |
| 178 | const runtime = { |
| 179 | status: "running" as const, |
| 180 | activeRunId: null, |
| 181 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 182 | providerName: "Codex", |
| 183 | lastError: null, |
| 184 | updatedAt: "2026-03-09T10:00:00.000Z", |
| 185 | }; |
| 186 | |
| 187 | const idle = { hasPendingApprovals: false, hasPendingUserInput: false, runtime: null }; |
| 188 | |
| 189 | it("prioritizes awaiting input over a running runtime, below approval", () => { |
| 190 | expect( |
| 191 | resolveSidebarThreadStatus({ |
| 192 | ...idle, |
| 193 | hasPendingUserInput: true, |
| 194 | runtime, |
| 195 | blockedOnThreadWait: true, |
| 196 | }), |
| 197 | ).toBe("input"); |
| 198 | expect( |
| 199 | resolveSidebarThreadStatus({ |
| 200 | ...idle, |
| 201 | hasPendingApprovals: true, |
| 202 | hasPendingUserInput: true, |
| 203 | blockedOnThreadWait: true, |
| 204 | runtime, |
| 205 | }), |
| 206 | ).toBe("approval"); |
| 207 | }); |
| 208 | |
| 209 | it("shows a waiting guest message as a Request, below the agent's own requests", () => { |
| 210 | expect(resolveSidebarThreadStatus({ ...idle, pendingGuestMessageCount: 1, runtime })).toBe( |
| 211 | "request", |
| 212 | ); |
| 213 | expect( |
| 214 | resolveSidebarThreadStatus({ |
| 215 | ...idle, |
| 216 | hasPendingUserInput: true, |
| 217 | pendingGuestMessageCount: 1, |
| 218 | }), |
| 219 | ).toBe("input"); |
| 220 | }); |
| 221 | |
| 222 | it("keeps usage-limit stops Limited and visible until the thread recovers", () => { |
| 223 | const limited = { |
| 224 | ...runtime, |
| 225 | status: "failed" as const, |
| 226 | lastError: "Plan limit reached", |
| 227 | lastErrorClass: "usage_limit" as const, |
| 228 | }; |
| 229 | expect(resolveSidebarThreadStatus({ ...idle, runtime: limited })).toBe("limited"); |
| 230 | expect( |
| 231 | resolveSidebarThreadStatus({ ...idle, runtime: { ...limited, status: "running" } }), |
| 232 | ).toBe("working"); |
| 233 | expect( |
| 234 | resolveSidebarThreadStatus({ ...idle, runtime: { ...limited, status: "completed" } }), |
| 235 | ).toBe("ready"); |
| 236 | expect( |
| 237 | shouldRecedeSidebarThread({ |
| 238 | status: "limited", |
| 239 | isUnread: false, |
| 240 | isWoke: false, |
| 241 | isActive: false, |
| 242 | isSelected: false, |
| 243 | }), |
| 244 | ).toBe(false); |
| 245 | }); |
| 246 | |
| 247 | it("reports worktree setup as preparing, and failed only while the latest run failed", () => { |
| 248 | expect( |
| 249 | resolveSidebarThreadStatus({ |
| 250 | ...idle, |
| 251 | runtime: { ...runtime, status: "preparing" as const }, |
| 252 | }), |
| 253 | ).toBe("preparing"); |
| 254 | expect( |
| 255 | resolveSidebarThreadStatus({ |
| 256 | ...idle, |
| 257 | runtime: { ...runtime, status: "failed" as const, lastError: "boom" }, |
| 258 | }), |
| 259 | ).toBe("failed"); |
| 260 | expect( |
| 261 | resolveSidebarThreadStatus({ |
| 262 | ...idle, |
| 263 | runtime: { ...runtime, status: "completed" as const, lastError: "persisted" }, |
| 264 | }), |
| 265 | ).toBe("ready"); |
| 266 | expect( |
| 267 | resolveSidebarThreadStatus({ |
| 268 | ...idle, |
| 269 | runtime: { ...runtime, status: "idle" as const, lastError: "persisted" }, |
| 270 | }), |
| 271 | ).toBe("waiting"); |
| 272 | }); |
| 273 | |
| 274 | it("titles the window with live work and requests only", () => { |
| 275 | const cases = [ |
| 276 | [{ ...idle, hasPendingApprovals: true, runtime }, "Needs approval"], |
| 277 | [{ ...idle, hasPendingUserInput: true }, "Needs your input"], |
| 278 | [{ ...idle, runtime: { ...runtime, status: "preparing" as const } }, "Working"], |
| 279 | [{ ...idle, runtime }, "Working"], |
| 280 | [{ ...idle, runtime, pendingGuestMessageCount: 1 }, "Working"], |
| 281 | [{ ...idle, runtime, blockedOnThreadWait: true }, "Working"], |
| 282 | [{ ...idle, runtime: { ...runtime, status: "idle" as const } }, null], |
| 283 | [{ ...idle, runtime: { ...runtime, status: "failed" as const } }, null], |
| 284 | [{ ...idle, runtime: { ...runtime, status: "completed" as const } }, null], |
| 285 | [idle, null], |
| 286 | ] as const; |
| 287 | for (const [thread, title] of cases) { |
| 288 | expect(resolveWindowTitleThreadStatus(thread)).toBe(title); |
| 289 | } |
| 290 | }); |
| 291 | }); |
| 292 | |
| 293 | describe("resolveSidebarRowStatus", () => { |
| 294 | const failedRuntime = { |
| 295 | status: "failed" as const, |
| 296 | activeRunId: null, |
| 297 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 298 | providerName: "Codex", |
| 299 | lastError: "Provider turn failed.", |
| 300 | updatedAt: "2026-03-09T10:05:00.000Z", |
| 301 | }; |
| 302 | const failed = { |
| 303 | hasActionableProposedPlan: false, |
| 304 | hasPendingApprovals: false, |
| 305 | hasPendingUserInput: false, |
| 306 | interactionMode: "default" as const, |
| 307 | runtime: failedRuntime, |
| 308 | latestRun: { ...makeLatestRun(), status: "failed" as const }, |
| 309 | }; |
| 310 | |
| 311 | it("settles a failure the user opened to ready, without showing Done", () => { |
| 312 | expect(resolveSidebarRowStatus({ ...failed, lastVisitedAt: undefined })).toBe("failed"); |
| 313 | |
| 314 | const opened = { ...failed, lastVisitedAt: "2026-03-09T10:06:00.000Z" }; |
| 315 | expect(resolveSidebarRowStatus(opened)).toBe("ready"); |
| 316 | expect(hasUnseenCompletion(opened)).toBe(false); |
| 317 | // A runtime failure without a failed run has no date, so a visit cannot settle it. |
| 318 | expect(resolveSidebarRowStatus({ ...opened, latestRun: null })).toBe("failed"); |
| 319 | }); |
| 320 | |
| 321 | it("does not show a stopped run as an unseen completion", () => { |
| 322 | for (const status of ["interrupted", "cancelled"] as const) { |
| 323 | const stopped = { |
| 324 | ...failed, |
| 325 | runtime: { ...failedRuntime, status, lastError: null }, |
| 326 | latestRun: { ...makeLatestRun(), status }, |
| 327 | lastVisitedAt: "2026-03-09T10:01:00.000Z", |
| 328 | }; |
| 329 | expect(hasUnseenCompletion(stopped)).toBe(false); |
| 330 | expect(resolveThreadStatusPill({ thread: stopped })).toBeNull(); |
| 331 | } |
| 332 | }); |
| 333 | }); |
| 334 | |
| 335 | describe("searchSidebarThreads", () => { |
| 336 | const searchThread = (id: string, title: string, latestUserMessageAt: string) => ({ |
| 337 | environmentId: localEnvironmentId, |
| 338 | id: ThreadId.make(id), |
| 339 | title, |
| 340 | branch: null, |
| 341 | createdAt: "2026-01-01T00:00:00.000Z", |
| 342 | updatedAt: latestUserMessageAt, |
| 343 | latestUserMessageAt, |
| 344 | opens: null, |
| 345 | }); |
| 346 | const threads = [ |
| 347 | searchThread("thread-1", "Fix workspace search", "2026-10-20T00:00:00.000Z"), |
| 348 | searchThread("thread-2", "Review providers", "2026-10-22T00:00:00.000Z"), |
| 349 | searchThread("thread-3", "WORKTREE cleanup", "2026-09-01T00:00:00.000Z"), |
| 350 | searchThread("thread-4", "Homework notes", "2026-10-22T00:00:00.000Z"), |
| 351 | ]; |
| 352 | const now = Date.parse("2026-10-23T00:00:00.000Z"); |
| 353 | const contentKeys = (...ids: ReadonlyArray<string>) => |
| 354 | new Set( |
| 355 | ids.map((id) => |
| 356 | threadSearchMatchKey({ environmentId: localEnvironmentId, threadId: ThreadId.make(id) }), |
| 357 | ), |
| 358 | ); |
| 359 | |
| 360 | it("ranks word matches by recent use, then lists message matches once", () => { |
| 361 | expect(searchSidebarThreads(threads, "work", contentKeys("thread-1", "thread-2"), now)).toEqual( |
| 362 | { |
| 363 | titleMatches: [threads[0], threads[2], threads[3]], |
| 364 | contentMatches: [threads[1]], |
| 365 | }, |
| 366 | ); |
| 367 | }); |
| 368 | }); |
| 369 | |
| 370 | describe("resolveWorkingStartedAt", () => { |
| 371 | const runtime = { |
| 372 | status: "running" as const, |
| 373 | providerName: "Codex", |
| 374 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 375 | activeRunId: RunId.make("turn-1"), |
| 376 | lastError: null, |
| 377 | updatedAt: "2026-03-09T10:02:00.000Z", |
| 378 | }; |
| 379 | |
| 380 | it("does not invent a start from activity updates when the newest run completed", () => { |
| 381 | expect( |
| 382 | resolveWorkingStartedAt({ |
| 383 | latestRun: makeLatestRun(), |
| 384 | runtime, |
| 385 | }), |
| 386 | ).toBeNull(); |
| 387 | }); |
| 388 | |
| 389 | it.each(["queued", "cancelled"] as const)( |
| 390 | "shares the detail timer when a newer run is %s", |
| 391 | (status) => { |
| 392 | const activityStartedAt = "2026-03-09T10:00:00.000Z"; |
| 393 | const latestRun = { |
| 394 | ...makeLatestRun(), |
| 395 | runId: RunId.make("newer-run"), |
| 396 | status, |
| 397 | startedAt: null, |
| 398 | completedAt: status === "queued" ? null : "2026-03-09T10:05:00.000Z", |
| 399 | }; |
| 400 | for (const updatedAt of ["2026-03-09T10:30:00.000Z", "2026-03-09T10:50:00.000Z"]) { |
| 401 | const activeRuntime = { ...runtime, updatedAt, activityStartedAt }; |
| 402 | expect(resolveWorkingStartedAt({ latestRun, runtime: activeRuntime })).toBe( |
| 403 | activityStartedAt, |
| 404 | ); |
| 405 | expect(deriveActiveWorkStartedAt(latestRun, activeRuntime, updatedAt)).toBe( |
| 406 | activityStartedAt, |
| 407 | ); |
| 408 | } |
| 409 | expect( |
| 410 | deriveActiveWorkStartedAt( |
| 411 | latestRun, |
| 412 | { ...runtime, activityStartedAt: null }, |
| 413 | "2026-03-09T10:50:00.000Z", |
| 414 | ), |
| 415 | ).toBeNull(); |
| 416 | }, |
| 417 | ); |
| 418 | }); |
| 419 | |
| 420 | describe("resolveThreadStatusPill", () => { |
| 421 | const baseThread = { |
| 422 | hasActionableProposedPlan: false, |
| 423 | hasPendingApprovals: false, |
| 424 | hasPendingUserInput: false, |
| 425 | interactionMode: "plan" as const, |
| 426 | latestRun: null, |
| 427 | lastVisitedAt: undefined, |
| 428 | runtime: { |
| 429 | status: "running" as const, |
| 430 | providerName: "Codex", |
| 431 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 432 | activeRunId: "turn-1" as never, |
| 433 | lastError: null, |
| 434 | updatedAt: "2026-03-09T10:00:00.000Z", |
| 435 | }, |
| 436 | }; |
| 437 | |
| 438 | it("shows an active thread wait without a pulse and resumes Working when it ends", () => { |
| 439 | const blocked = { ...baseThread, blockedOnThreadWait: true }; |
| 440 | expect(resolveSidebarThreadStatus(blocked)).toBe("waiting"); |
| 441 | expect(resolveThreadStatusPill({ thread: blocked })).toMatchObject({ |
| 442 | label: "Waiting", |
| 443 | pulse: false, |
| 444 | }); |
| 445 | |
| 446 | const resumed = { ...blocked, blockedOnThreadWait: false }; |
| 447 | expect(resolveSidebarThreadStatus(resumed)).toBe("working"); |
| 448 | expect(resolveThreadStatusPill({ thread: resumed })).toMatchObject({ |
| 449 | label: "Working", |
| 450 | pulse: true, |
| 451 | }); |
| 452 | }); |
| 453 | |
| 454 | it("shows waiting for an idle thread with pending background tasks", () => { |
| 455 | expect( |
| 456 | resolveThreadStatusPill({ |
| 457 | thread: { |
| 458 | ...baseThread, |
| 459 | pendingBackgroundTasks: [ |
| 460 | { taskId: "bg-1", description: "Watch build", kind: "monitor", awaited: true }, |
| 461 | ], |
| 462 | runtime: { |
| 463 | ...baseThread.runtime, |
| 464 | status: "idle", |
| 465 | activeRunId: null, |
| 466 | }, |
| 467 | }, |
| 468 | }), |
| 469 | ).toMatchObject({ |
| 470 | label: "Waiting", |
| 471 | colorClass: "text-sidebar-muted-foreground", |
| 472 | dotClass: "bg-sidebar-muted-foreground", |
| 473 | pulse: false, |
| 474 | }); |
| 475 | }); |
| 476 | |
| 477 | it("shows plan ready when a settled plan turn has a proposed plan ready for follow-up", () => { |
| 478 | expect( |
| 479 | resolveThreadStatusPill({ |
| 480 | thread: { |
| 481 | ...baseThread, |
| 482 | hasActionableProposedPlan: true, |
| 483 | latestRun: makeLatestRun(), |
| 484 | runtime: { |
| 485 | ...baseThread.runtime, |
| 486 | status: "completed", |
| 487 | activeRunId: null, |
| 488 | }, |
| 489 | }, |
| 490 | }), |
| 491 | ).toMatchObject({ label: "Plan Ready", pulse: false }); |
| 492 | }); |
| 493 | |
| 494 | it("does not manufacture completed state without a client visit marker", () => { |
| 495 | expect( |
| 496 | resolveThreadStatusPill({ |
| 497 | thread: { |
| 498 | ...baseThread, |
| 499 | latestRun: makeLatestRun(), |
| 500 | runtime: { |
| 501 | ...baseThread.runtime, |
| 502 | status: "completed", |
| 503 | activeRunId: null, |
| 504 | }, |
| 505 | }, |
| 506 | }), |
| 507 | ).toBeNull(); |
| 508 | }); |
| 509 | |
| 510 | it("shows completed when there is an unseen completion and no active blocker", () => { |
| 511 | expect( |
| 512 | resolveThreadStatusPill({ |
| 513 | thread: { |
| 514 | ...baseThread, |
| 515 | interactionMode: "default", |
| 516 | latestRun: makeLatestRun(), |
| 517 | lastVisitedAt: "2026-03-09T10:04:00.000Z", |
| 518 | runtime: { |
| 519 | ...baseThread.runtime, |
| 520 | status: "completed", |
| 521 | activeRunId: null, |
| 522 | }, |
| 523 | }, |
| 524 | }), |
| 525 | ).toMatchObject({ label: "Completed", pulse: false }); |
| 526 | }); |
| 527 | |
| 528 | it("shows failed, not completed, when the unseen run failed", () => { |
| 529 | const failed = { |
| 530 | ...baseThread, |
| 531 | interactionMode: "default" as const, |
| 532 | latestRun: { ...makeLatestRun(), status: "failed" as const }, |
| 533 | lastVisitedAt: "2026-03-09T10:04:00.000Z", |
| 534 | runtime: { |
| 535 | ...baseThread.runtime, |
| 536 | status: "failed" as const, |
| 537 | activeRunId: null, |
| 538 | lastError: "Provider turn failed.", |
| 539 | }, |
| 540 | }; |
| 541 | expect(resolveThreadStatusPill({ thread: failed })).toMatchObject({ |
| 542 | label: "Failed", |
| 543 | pulse: false, |
| 544 | }); |
| 545 | // The failure sends the thread to the inbox, so its pill must not say it is still waiting. |
| 546 | const watching = { |
| 547 | ...failed, |
| 548 | pendingBackgroundTasks: [ |
| 549 | { |
| 550 | taskId: "pr-watch", |
| 551 | description: "Watching pull request #7", |
| 552 | kind: "monitor" as const, |
| 553 | awaited: true as const, |
| 554 | }, |
| 555 | ], |
| 556 | }; |
| 557 | expect(isSidebarThreadWorking(watching)).toBe(false); |
| 558 | expect(resolveThreadStatusPill({ thread: watching })).toMatchObject({ label: "Failed" }); |
| 559 | // A run blocked on another thread is still waiting, as its sidebar row says. |
| 560 | const blocked = { |
| 561 | ...watching, |
| 562 | runtime: { ...failed.runtime, status: "running" as const }, |
| 563 | blockedOnThreadWait: true, |
| 564 | }; |
| 565 | expect(resolveSidebarThreadStatus(blocked)).toBe("waiting"); |
| 566 | expect(resolveThreadStatusPill({ thread: blocked })).toMatchObject({ label: "Waiting" }); |
| 567 | }); |
| 568 | |
| 569 | it("shows failed, not plan ready, when a plan run proposed a plan and then failed", () => { |
| 570 | expect( |
| 571 | resolveThreadStatusPill({ |
| 572 | thread: { |
| 573 | ...baseThread, |
| 574 | hasActionableProposedPlan: true, |
| 575 | latestRun: { ...makeLatestRun(), status: "failed" }, |
| 576 | lastVisitedAt: "2026-03-09T10:04:00.000Z", |
| 577 | runtime: { ...baseThread.runtime, status: "failed", activeRunId: null }, |
| 578 | }, |
| 579 | }), |
| 580 | ).toMatchObject({ label: "Failed" }); |
| 581 | }); |
| 582 | |
| 583 | it("drops the failed pill once the failure has been seen", () => { |
| 584 | expect( |
| 585 | resolveThreadStatusPill({ |
| 586 | thread: { |
| 587 | ...baseThread, |
| 588 | interactionMode: "default", |
| 589 | latestRun: { ...makeLatestRun(), status: "failed" }, |
| 590 | lastVisitedAt: "2026-03-09T10:06:00.000Z", |
| 591 | runtime: { ...baseThread.runtime, status: "failed", activeRunId: null }, |
| 592 | }, |
| 593 | }), |
| 594 | ).toBeNull(); |
| 595 | }); |
| 596 | }); |
| 597 | |
| 598 | function makeProject(overrides: Partial<Project> = {}): Project { |
| 599 | const { defaultModelSelection, ...rest } = overrides; |
| 600 | return { |
| 601 | id: ProjectId.make("project-1"), |
| 602 | environmentId: localEnvironmentId, |
| 603 | title: "Project", |
| 604 | workspaceRoot: "/tmp/project", |
| 605 | repositoryIdentity: null, |
| 606 | defaultModelSelection: { |
| 607 | instanceId: ProviderInstanceId.make("codex"), |
| 608 | model: "gpt-5.4", |
| 609 | ...defaultModelSelection, |
| 610 | }, |
| 611 | createdAt: "2026-03-09T10:00:00.000Z", |
| 612 | updatedAt: "2026-03-09T10:00:00.000Z", |
| 613 | scripts: [], |
| 614 | ...rest, |
| 615 | }; |
| 616 | } |
| 617 | |
| 618 | function makeThread(overrides: ThreadFixtureOverrides = {}): Thread { |
| 619 | return makeThreadFixture({ |
| 620 | id: ThreadId.make("thread-1"), |
| 621 | environmentId: localEnvironmentId, |
| 622 | projectId: ProjectId.make("project-1"), |
| 623 | title: "Thread", |
| 624 | modelSelection: { |
| 625 | instanceId: ProviderInstanceId.make("codex"), |
| 626 | model: "gpt-5.4", |
| 627 | ...overrides?.modelSelection, |
| 628 | }, |
| 629 | runtimeMode: DEFAULT_RUNTIME_MODE, |
| 630 | interactionMode: DEFAULT_INTERACTION_MODE, |
| 631 | runtime: null, |
| 632 | messages: [], |
| 633 | proposedPlans: [], |
| 634 | createdAt: "2026-03-09T10:00:00.000Z", |
| 635 | archivedAt: null, |
| 636 | settledOverride: null, |
| 637 | settledAt: null, |
| 638 | deletedAt: null, |
| 639 | updatedAt: "2026-03-09T10:00:00.000Z", |
| 640 | latestRun: null, |
| 641 | branch: null, |
| 642 | worktreePath: null, |
| 643 | ...overrides, |
| 644 | }); |
| 645 | } |
| 646 | |
| 647 | describe("getFallbackThreadIdAfterDelete", () => { |
| 648 | it("skips other threads being deleted in the same action", () => { |
| 649 | const fallbackThreadId = getFallbackThreadIdAfterDelete({ |
| 650 | threads: [ |
| 651 | makeThread({ |
| 652 | id: ThreadId.make("thread-active"), |
| 653 | projectId: ProjectId.make("project-1"), |
| 654 | createdAt: "2026-03-09T10:05:00.000Z", |
| 655 | messages: [], |
| 656 | }), |
| 657 | makeThread({ |
| 658 | id: ThreadId.make("thread-newest"), |
| 659 | projectId: ProjectId.make("project-1"), |
| 660 | createdAt: "2026-03-09T10:10:00.000Z", |
| 661 | messages: [], |
| 662 | }), |
| 663 | makeThread({ |
| 664 | id: ThreadId.make("thread-next"), |
| 665 | projectId: ProjectId.make("project-1"), |
| 666 | createdAt: "2026-03-09T10:07:00.000Z", |
| 667 | messages: [], |
| 668 | }), |
| 669 | ], |
| 670 | deletedThreadId: ThreadId.make("thread-active"), |
| 671 | deletedThreadIds: new Set([ThreadId.make("thread-active"), ThreadId.make("thread-newest")]), |
| 672 | sortOrder: "created_at", |
| 673 | now: "2026-03-09T10:10:00.000Z", |
| 674 | delegatedUnderParent: new Set(), |
| 675 | }); |
| 676 | |
| 677 | expect(fallbackThreadId).toBe(ThreadId.make("thread-next")); |
| 678 | }); |
| 679 | |
| 680 | it.each([ |
| 681 | [ |
| 682 | "snoozed", |
| 683 | { snoozedUntil: "2099-01-01T00:00:00.000Z", snoozedAt: "2026-03-09T10:06:00.000Z" }, |
| 684 | ], |
| 685 | ["delegated", { id: ThreadId.make("thread-delegated") }], |
| 686 | ["settled", { settledOverride: "settled", settledAt: "2026-03-09T10:06:00.000Z" }], |
| 687 | [ |
| 688 | "side chat", |
| 689 | { |
| 690 | lineage: { |
| 691 | rootThreadId: ThreadId.make("thread-visible"), |
| 692 | parentThreadId: ThreadId.make("thread-visible"), |
| 693 | relationshipToParent: "fork", |
| 694 | sideChat: true, |
| 695 | }, |
| 696 | }, |
| 697 | ], |
| 698 | [ |
| 699 | "subagent", |
| 700 | { |
| 701 | lineage: { |
| 702 | rootThreadId: ThreadId.make("thread-visible"), |
| 703 | parentThreadId: ThreadId.make("thread-visible"), |
| 704 | relationshipToParent: "subagent", |
| 705 | }, |
| 706 | }, |
| 707 | ], |
| 708 | ] as const)("skips a %s thread the sidebar does not list", (_state, parked) => { |
| 709 | const fallbackThreadId = getFallbackThreadIdAfterDelete({ |
| 710 | threads: [ |
| 711 | makeThread({ |
| 712 | id: ThreadId.make("thread-visible"), |
| 713 | createdAt: "2026-03-09T10:01:00.000Z", |
| 714 | updatedAt: "2026-03-09T10:01:00.000Z", |
| 715 | }), |
| 716 | makeThread({ |
| 717 | id: ThreadId.make("thread-parked"), |
| 718 | createdAt: "2026-03-09T10:02:00.000Z", |
| 719 | updatedAt: "2026-03-09T10:06:00.000Z", |
| 720 | ...parked, |
| 721 | }), |
| 722 | makeThread({ |
| 723 | id: ThreadId.make("thread-active"), |
| 724 | createdAt: "2026-03-09T10:03:00.000Z", |
| 725 | updatedAt: "2026-03-09T10:03:00.000Z", |
| 726 | }), |
| 727 | ], |
| 728 | deletedThreadId: ThreadId.make("thread-active"), |
| 729 | sortOrder: "created_at", |
| 730 | now: "2026-03-09T10:10:00.000Z", |
| 731 | delegatedUnderParent: new Set([ |
| 732 | threadKey({ |
| 733 | environmentId: localEnvironmentId, |
| 734 | threadId: ThreadId.make("thread-delegated"), |
| 735 | }), |
| 736 | ]), |
| 737 | }); |
| 738 | |
| 739 | expect(fallbackThreadId).toBe(ThreadId.make("thread-visible")); |
| 740 | }); |
| 741 | }); |
| 742 | |
| 743 | describe("sortScopedProjectsForSidebar", () => { |
| 744 | it("keeps identical project ids in different environments separate", () => { |
| 745 | const remoteEnvironmentId = EnvironmentId.make("environment-remote"); |
| 746 | const sharedProjectId = ProjectId.make("shared-project"); |
| 747 | const projects = [ |
| 748 | makeProject({ |
| 749 | environmentId: localEnvironmentId, |
| 750 | id: sharedProjectId, |
| 751 | title: "Local project", |
| 752 | }), |
| 753 | makeProject({ |
| 754 | environmentId: remoteEnvironmentId, |
| 755 | id: sharedProjectId, |
| 756 | title: "Remote project", |
| 757 | }), |
| 758 | ]; |
| 759 | const threads = [ |
| 760 | makeThread({ |
| 761 | environmentId: localEnvironmentId, |
| 762 | projectId: sharedProjectId, |
| 763 | updatedAt: "2026-03-09T10:02:00.000Z", |
| 764 | }), |
| 765 | makeThread({ |
| 766 | environmentId: remoteEnvironmentId, |
| 767 | projectId: sharedProjectId, |
| 768 | updatedAt: "2026-03-09T10:10:00.000Z", |
| 769 | }), |
| 770 | ]; |
| 771 | |
| 772 | const sorted = sortScopedProjectsForSidebar(projects, threads, "updated_at"); |
| 773 | |
| 774 | expect(sorted.map((project) => project.title)).toEqual(["Remote project", "Local project"]); |
| 775 | }); |
| 776 | |
| 777 | it("does not use archived threads as project activity", () => { |
| 778 | const projects = [ |
| 779 | makeProject({ |
| 780 | id: ProjectId.make("project-visible"), |
| 781 | title: "Visible project", |
| 782 | updatedAt: "2026-03-09T10:01:00.000Z", |
| 783 | }), |
| 784 | makeProject({ |
| 785 | id: ProjectId.make("project-archived"), |
| 786 | title: "Archived-only project", |
| 787 | updatedAt: "2026-03-09T10:00:00.000Z", |
| 788 | }), |
| 789 | ]; |
| 790 | const threads = [ |
| 791 | makeThread({ |
| 792 | id: ThreadId.make("thread-visible"), |
| 793 | projectId: ProjectId.make("project-visible"), |
| 794 | updatedAt: "2026-03-09T10:02:00.000Z", |
| 795 | }), |
| 796 | makeThread({ |
| 797 | id: ThreadId.make("thread-archived"), |
| 798 | projectId: ProjectId.make("project-archived"), |
| 799 | updatedAt: "2026-03-09T10:10:00.000Z", |
| 800 | archivedAt: "2026-03-09T10:11:00.000Z", |
| 801 | }), |
| 802 | ]; |
| 803 | |
| 804 | const sorted = sortScopedProjectsForSidebar(projects, threads, "updated_at"); |
| 805 | |
| 806 | expect(sorted.map((project) => project.title)).toEqual([ |
| 807 | "Visible project", |
| 808 | "Archived-only project", |
| 809 | ]); |
| 810 | }); |
| 811 | }); |
| 812 | |
| 813 | describe("sortSidebarV2ProjectGroups", () => { |
| 814 | it("does not let a hidden subagent thread reorder projects", () => { |
| 815 | const olderProjectId = ProjectId.make("project-older"); |
| 816 | const newerProjectId = ProjectId.make("project-newer"); |
| 817 | const olderRootThreadId = ThreadId.make("thread-older-root"); |
| 818 | const projects = [ |
| 819 | { |
| 820 | ...makeProject({ id: olderProjectId, title: "A older project" }), |
| 821 | projectKey: "logical-older", |
| 822 | memberProjectRefs: [{ environmentId: localEnvironmentId, projectId: olderProjectId }], |
| 823 | }, |
| 824 | { |
| 825 | ...makeProject({ id: newerProjectId, title: "Z newer project" }), |
| 826 | projectKey: "logical-newer", |
| 827 | memberProjectRefs: [{ environmentId: localEnvironmentId, projectId: newerProjectId }], |
| 828 | }, |
| 829 | ]; |
| 830 | const threads = [ |
| 831 | makeThread({ |
| 832 | id: olderRootThreadId, |
| 833 | projectId: olderProjectId, |
| 834 | updatedAt: "2026-03-09T10:01:00.000Z", |
| 835 | }), |
| 836 | makeThread({ |
| 837 | id: ThreadId.make("thread-newer-root"), |
| 838 | projectId: newerProjectId, |
| 839 | updatedAt: "2026-03-09T10:05:00.000Z", |
| 840 | }), |
| 841 | makeThread({ |
| 842 | id: ThreadId.make("thread-hidden-subagent"), |
| 843 | projectId: olderProjectId, |
| 844 | updatedAt: "2026-03-09T10:10:00.000Z", |
| 845 | lineage: { |
| 846 | rootThreadId: olderRootThreadId, |
| 847 | parentThreadId: olderRootThreadId, |
| 848 | relationshipToParent: "subagent", |
| 849 | }, |
| 850 | }), |
| 851 | ]; |
| 852 | |
| 853 | expect( |
| 854 | sortSidebarV2ProjectGroups(projects, threads, "updated_at", new Set()).map( |
| 855 | (project) => project.projectKey, |
| 856 | ), |
| 857 | ).toEqual(["logical-newer", "logical-older"]); |
| 858 | }); |
| 859 | }); |
| 860 | |
| 861 | describe("pinOrderKeyBetween", () => { |
| 862 | it("stays strictly ordered under repeated top insertion", () => { |
| 863 | let head: string | null = null; |
| 864 | const keys: string[] = []; |
| 865 | for (let i = 0; i < 100; i += 1) { |
| 866 | const key: string = pinOrderKeyBetween(null, head)!; |
| 867 | expect(key).not.toBeNull(); |
| 868 | if (head !== null) expect(key < head).toBe(true); |
| 869 | keys.push(key); |
| 870 | head = key; |
| 871 | } |
| 872 | expect(new Set(keys).size).toBe(100); |
| 873 | }); |
| 874 | |
| 875 | it("stays strictly ordered under repeated middle insertion", () => { |
| 876 | let low = pinOrderKeyBetween(null, null)!; |
| 877 | let high = pinOrderKeyBetween(low, null)!; |
| 878 | for (let i = 0; i < 100; i += 1) { |
| 879 | const key: string = pinOrderKeyBetween(low, high)!; |
| 880 | expect(low < key && key < high).toBe(true); |
| 881 | if (i % 2 === 0) low = key; |
| 882 | else high = key; |
| 883 | } |
| 884 | }); |
| 885 | |
| 886 | it("returns null for corrupt or out-of-order bounds instead of throwing", () => { |
| 887 | expect(pinOrderKeyBetween("z", "a")).toBeNull(); |
| 888 | expect(pinOrderKeyBetween("A!", null)).toBeNull(); |
| 889 | expect(pinOrderKeyBetween(null, "ma")).toBeNull(); |
| 890 | expect(pinOrderKeyBetween("m", "m")).toBeNull(); |
| 891 | }); |
| 892 | }); |
| 893 | |
| 894 | describe("sortPinnedThreadsForSidebar", () => { |
| 895 | const pinnable = (input: { id: string; createdAt: string; pinOrderKey?: string | null }) => ({ |
| 896 | id: input.id, |
| 897 | createdAt: input.createdAt, |
| 898 | pinOrderKey: input.pinOrderKey ?? null, |
| 899 | }); |
| 900 | |
| 901 | it("sorts keyed threads by key ahead of keyless threads in creation order", () => { |
| 902 | const sorted = sortPinnedThreadsForSidebar([ |
| 903 | pinnable({ id: "keyless-old", createdAt: "2026-03-09T08:00:00.000Z" }), |
| 904 | pinnable({ id: "second", createdAt: "2026-03-09T09:00:00.000Z", pinOrderKey: "t" }), |
| 905 | pinnable({ id: "keyless-new", createdAt: "2026-03-09T12:00:00.000Z" }), |
| 906 | pinnable({ id: "first", createdAt: "2026-03-09T07:00:00.000Z", pinOrderKey: "g" }), |
| 907 | ]); |
| 908 | |
| 909 | expect(sorted.map((thread) => thread.id)).toEqual([ |
| 910 | "first", |
| 911 | "second", |
| 912 | "keyless-new", |
| 913 | "keyless-old", |
| 914 | ]); |
| 915 | }); |
| 916 | |
| 917 | it("breaks equal keys by id so raced writes render identically everywhere", () => { |
| 918 | const sorted = sortPinnedThreadsForSidebar([ |
| 919 | pinnable({ id: "b", createdAt: "2026-03-09T10:00:00.000Z", pinOrderKey: "m" }), |
| 920 | pinnable({ id: "a", createdAt: "2026-03-09T11:00:00.000Z", pinOrderKey: "m" }), |
| 921 | ]); |
| 922 | |
| 923 | expect(sorted.map((thread) => thread.id)).toEqual(["a", "b"]); |
| 924 | }); |
| 925 | }); |
| 926 | |
| 927 | describe("resolveSidebarSweepKeys", () => { |
| 928 | const ordered = ["a", "b", "c", "d", "blocked"]; |
| 929 | const canSettle = (key: string) => key !== "blocked"; |
| 930 | |
| 931 | it("leaves out rows that cannot settle and rows that left the list", () => { |
| 932 | expect(resolveSidebarSweepKeys(ordered, "c", "blocked", canSettle)).toEqual(["c", "d"]); |
| 933 | expect(resolveSidebarSweepKeys(ordered, "gone", "a", canSettle)).toEqual([]); |
| 934 | }); |
| 935 | }); |
| 936 | |
| 937 | describe("navigation after parking a thread", () => { |
| 938 | it.each([ |
| 939 | ["settle", "settled", null, "thread", true], |
| 940 | ["settle", "active", null, "thread", false], |
| 941 | ["settle", "settled", null, "other-thread", false], |
| 942 | ["snooze", null, "2099-01-01T00:00:00.000Z", "thread", true], |
| 943 | ["snooze", null, null, "thread", false], |
| 944 | ["snooze", null, "2026-09-12T09:00:00.000Z", "thread", false], |
| 945 | ["snooze", null, "2099-01-01T00:00:00.000Z", "thread", false, true], |
| 946 | ["snooze", null, "2099-01-01T00:00:00.000Z", "other-thread", false], |
| 947 | ] as const)( |
| 948 | "%s with state %s / %s on %s navigates: %s", |
| 949 | ( |
| 950 | action, |
| 951 | settledOverride, |
| 952 | snoozedUntil, |
| 953 | currentThreadKey, |
| 954 | expected, |
| 955 | hasPendingApprovals: boolean = false, |
| 956 | ) => { |
| 957 | expect( |
| 958 | shouldNavigateAfterThreadPark({ |
| 959 | threadKey: "thread", |
| 960 | currentThreadKey, |
| 961 | action, |
| 962 | now: "2026-09-12T10:00:00.000Z", |
| 963 | thread: { |
| 964 | settledOverride, |
| 965 | snoozedUntil, |
| 966 | snoozedAt: null, |
| 967 | session: null, |
| 968 | latestTurn: null, |
| 969 | hasPendingApprovals, |
| 970 | hasPendingUserInput: false, |
| 971 | }, |
| 972 | }), |
| 973 | ).toBe(expected); |
| 974 | }, |
| 975 | ); |
| 976 | }); |
| 977 | |
| 978 | describe("unseen completion with background work", () => { |
| 979 | it.each([ |
| 980 | { |
| 981 | roster: "dev server", |
| 982 | task: { kind: "command" }, |
| 983 | status: "ready", |
| 984 | receded: false, |
| 985 | pill: "Completed", |
| 986 | }, |
| 987 | { |
| 988 | roster: "dev server watchdog", |
| 989 | task: { kind: "monitor" }, |
| 990 | status: "ready", |
| 991 | receded: false, |
| 992 | pill: "Completed", |
| 993 | }, |
| 994 | { |
| 995 | roster: "awaited command", |
| 996 | task: { kind: "command", awaited: true }, |
| 997 | status: "waiting", |
| 998 | receded: true, |
| 999 | pill: "Waiting", |
| 1000 | }, |
| 1001 | ] as const)("presents a completed thread with a $roster roster", (expected) => { |
| 1002 | const thread = presentThreadShell(localEnvironmentId, { |
| 1003 | ...makeThreadFixture().source, |
| 1004 | latestRunId: RunId.make("run-background-completion"), |
| 1005 | status: "completed", |
| 1006 | latestRunCompletedAt: DateTime.makeUnsafe("2026-06-20T01:00:00.000Z"), |
| 1007 | lastVisitedAt: DateTime.makeUnsafe("2026-06-20T00:59:00.000Z"), |
| 1008 | pendingBackgroundTasks: [{ taskId: "background-work", ...expected.task }], |
| 1009 | }); |
| 1010 | const status = resolveSidebarThreadStatus(thread); |
| 1011 | const isUnread = hasUnseenCompletion(thread); |
| 1012 | |
| 1013 | expect(isUnread).toBe(true); |
| 1014 | expect(status).toBe(expected.status); |
| 1015 | expect( |
| 1016 | shouldRecedeSidebarThread({ |
| 1017 | status, |
| 1018 | isUnread, |
| 1019 | isWoke: false, |
| 1020 | isActive: false, |
| 1021 | isSelected: false, |
| 1022 | }), |
| 1023 | ).toBe(expected.receded); |
| 1024 | expect(isSidebarThreadWorking(thread)).toBe(expected.receded); |
| 1025 | expect(resolveThreadStatusPill({ thread })).toMatchObject({ label: expected.pill }); |
| 1026 | }); |
| 1027 | }); |
| 1028 | |
| 1029 | describe("Working shelf (beta)", () => { |
| 1030 | const runtime = { |
| 1031 | status: "running" as const, |
| 1032 | activeRunId: null, |
| 1033 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 1034 | providerName: "Codex", |
| 1035 | lastError: null, |
| 1036 | updatedAt: "2026-03-09T10:00:00.000Z", |
| 1037 | }; |
| 1038 | const backgroundTask = { |
| 1039 | taskId: "bg-1", |
| 1040 | description: "Watch build", |
| 1041 | kind: "monitor" as const, |
| 1042 | awaited: true as const, |
| 1043 | }; |
| 1044 | const idle = { |
| 1045 | hasActionableProposedPlan: false, |
| 1046 | hasPendingApprovals: false, |
| 1047 | hasPendingUserInput: false, |
| 1048 | interactionMode: "default" as const, |
| 1049 | latestRun: makeLatestRun(), |
| 1050 | runtime: null, |
| 1051 | }; |
| 1052 | const waiting = { |
| 1053 | ...idle, |
| 1054 | runtime: { ...runtime, status: "idle" as const }, |
| 1055 | pendingBackgroundTasks: [backgroundTask], |
| 1056 | }; |
| 1057 | |
| 1058 | it("folds away running threads and threads waiting on background work only", () => { |
| 1059 | expect(isSidebarThreadWorking({ ...idle, runtime })).toBe(true); |
| 1060 | expect(isSidebarThreadWorking(waiting)).toBe(true); |
| 1061 | expect(isSidebarThreadWorking(idle)).toBe(false); |
| 1062 | expect(isSidebarThreadWorking({ ...idle, runtime, hasPendingApprovals: true })).toBe(false); |
| 1063 | expect(isSidebarThreadWorking({ ...idle, runtime, hasPendingUserInput: true })).toBe(false); |
| 1064 | expect( |
| 1065 | isSidebarThreadWorking({ |
| 1066 | ...waiting, |
| 1067 | runtime: { ...runtime, status: "failed" as const, lastError: "boom" }, |
| 1068 | }), |
| 1069 | ).toBe(false); |
| 1070 | }); |
| 1071 | |
| 1072 | it("keeps a ready plan in the inbox while background work runs", () => { |
| 1073 | expect( |
| 1074 | isSidebarThreadWorking({ |
| 1075 | ...waiting, |
| 1076 | interactionMode: "plan", |
| 1077 | hasActionableProposedPlan: true, |
| 1078 | }), |
| 1079 | ).toBe(false); |
| 1080 | }); |
| 1081 | |
| 1082 | describe("sortInboxThreadsByReturn", () => { |
| 1083 | const thread = ( |
| 1084 | id: string, |
| 1085 | input: { createdAt: string; completedAt?: string | null; unsettledAt?: string }, |
| 1086 | ) => ({ |
| 1087 | id: ThreadId.make(id), |
| 1088 | environmentId: localEnvironmentId, |
| 1089 | createdAt: input.createdAt, |
| 1090 | unsettledAt: input.unsettledAt ?? null, |
| 1091 | latestRun: |
| 1092 | input.completedAt === undefined |
| 1093 | ? null |
| 1094 | : { ...makeLatestRun({ completedAt: input.completedAt }), requestedAt: input.createdAt }, |
| 1095 | }); |
| 1096 | |
| 1097 | it("puts the thread that finished last on top, whatever its age", () => { |
| 1098 | const sorted = sortInboxThreadsByReturn([ |
| 1099 | thread("new", { createdAt: "2026-03-09T11:00:00.000Z" }), |
| 1100 | thread("old-finished-now", { |
| 1101 | createdAt: "2026-03-01T09:00:00.000Z", |
| 1102 | completedAt: "2026-03-09T12:00:00.000Z", |
| 1103 | }), |
| 1104 | thread("reopened", { |
| 1105 | createdAt: "2026-03-02T09:00:00.000Z", |
| 1106 | unsettledAt: "2026-03-09T11:30:00.000Z", |
| 1107 | }), |
| 1108 | ]); |
| 1109 | expect(sorted.map((entry) => entry.id)).toEqual(["old-finished-now", "reopened", "new"]); |
| 1110 | }); |
| 1111 | |
| 1112 | it("counts a return the server does not stamp, like an approval request", () => { |
| 1113 | const waiting = thread("asks-approval", { |
| 1114 | createdAt: "2026-03-09T09:00:00.000Z", |
| 1115 | completedAt: null, |
| 1116 | }); |
| 1117 | const finished = thread("finished", { |
| 1118 | createdAt: "2026-03-09T09:30:00.000Z", |
| 1119 | completedAt: "2026-03-09T11:00:00.000Z", |
| 1120 | }); |
| 1121 | expect(sortInboxThreadsByReturn([finished, waiting]).map((entry) => entry.id)).toEqual([ |
| 1122 | "finished", |
| 1123 | "asks-approval", |
| 1124 | ]); |
| 1125 | expect( |
| 1126 | sortInboxThreadsByReturn([finished, waiting], (entry) => |
| 1127 | entry === waiting ? Date.parse("2026-03-09T11:05:00.000Z") : undefined, |
| 1128 | ).map((entry) => entry.id), |
| 1129 | ).toEqual(["asks-approval", "finished"]); |
| 1130 | }); |
| 1131 | }); |
| 1132 | |
| 1133 | describe("dragging", () => { |
| 1134 | const marker = (name: SidebarListMarker): SidebarListItem => ({ kind: "marker", marker: name }); |
| 1135 | const row = (key: string, section: SidebarSection): SidebarListItem => ({ |
| 1136 | kind: "thread", |
| 1137 | key, |
| 1138 | section, |
| 1139 | }); |
| 1140 | const items: readonly SidebarListItem[] = [ |
| 1141 | marker("pinned-header"), |
| 1142 | row("p1", "pinned"), |
| 1143 | marker("pinned-divider"), |
| 1144 | row("a1", "active"), |
| 1145 | row("a2", "active"), |
| 1146 | marker("working-header"), |
| 1147 | row("w1", "working"), |
| 1148 | marker("settled-header"), |
| 1149 | row("s1", "settled"), |
| 1150 | ]; |
| 1151 | |
| 1152 | it("never drops into the Working shelf, and keeps it out of the inbox order", () => { |
| 1153 | expect(resolveSidebarDropTarget(items, "a1", "w1")).toBeNull(); |
| 1154 | expect(resolveSidebarDropTarget(items, "p1", "a2")).toEqual({ |
| 1155 | section: "active", |
| 1156 | pinnedOrder: [], |
| 1157 | activeOrder: ["a1", "a2", "p1"], |
| 1158 | }); |
| 1159 | expect(resolveSidebarDropVerb("active", "working")).toBeNull(); |
| 1160 | }); |
| 1161 | |
| 1162 | it.each([ |
| 1163 | ["pinned", "p1"], |
| 1164 | ["active", "a2"], |
| 1165 | ["settled", "s1"], |
| 1166 | ] as const)( |
| 1167 | "does not move a Working thread into %s when its reference drag ends in the sidebar", |
| 1168 | (section, overKey) => { |
| 1169 | const target = resolveSidebarDropTarget(items, "w1", overKey); |
| 1170 | expect(target?.section).toBe(section); |
| 1171 | expect( |
| 1172 | planSidebarThreadDrop({ |
| 1173 | activeKey: "w1", |
| 1174 | activeSection: "working", |
| 1175 | target: target!, |
| 1176 | pinnedOrder: ["p1"], |
| 1177 | pinnedKeysById: new Map([["p1", "m"]]), |
| 1178 | activeOrder: ["a1", "a2"], |
| 1179 | activeKeysById: new Map([ |
| 1180 | ["a1", "f"], |
| 1181 | ["a2", "t"], |
| 1182 | ]), |
| 1183 | activeTimeOrdered: true, |
| 1184 | }), |
| 1185 | ).toEqual({ kind: "none" }); |
| 1186 | }, |
| 1187 | ); |
| 1188 | |
| 1189 | it("arranges the rows it can write when another row's server cannot store an order", () => { |
| 1190 | const plan = planSidebarThreadDrop({ |
| 1191 | activeKey: "a2", |
| 1192 | activeSection: "active", |
| 1193 | target: { |
| 1194 | section: "active", |
| 1195 | pinnedOrder: [], |
| 1196 | activeOrder: ["offline", "a2", "a1"], |
| 1197 | }, |
| 1198 | pinnedOrder: [], |
| 1199 | pinnedKeysById: new Map(), |
| 1200 | activeOrder: ["offline", "a1", "a2"], |
| 1201 | activeKeysById: new Map([ |
| 1202 | ["offline", null], |
| 1203 | ["a1", null], |
| 1204 | ["a2", null], |
| 1205 | ]), |
| 1206 | activeReorderableKeys: new Set(["a1", "a2"]), |
| 1207 | }); |
| 1208 | expect(plan.kind).toBe("move-active"); |
| 1209 | if (plan.kind !== "move-active") return; |
| 1210 | expect(plan.assignments.map(({ id }) => id)).toEqual(["a2", "a1"]); |
| 1211 | const [a2, a1] = plan.assignments.map(({ orderKey }) => orderKey); |
| 1212 | expect(a2! < a1!).toBe(true); |
| 1213 | |
| 1214 | const above = planSidebarThreadDrop({ |
| 1215 | activeKey: "a2", |
| 1216 | activeSection: "active", |
| 1217 | target: { section: "active", pinnedOrder: [], activeOrder: ["a2", "offline"] }, |
| 1218 | pinnedOrder: [], |
| 1219 | pinnedKeysById: new Map(), |
| 1220 | activeOrder: ["offline", "a2"], |
| 1221 | activeKeysById: new Map([ |
| 1222 | ["offline", "m"], |
| 1223 | ["a2", "t"], |
| 1224 | ]), |
| 1225 | activeReorderableKeys: new Set(["a2"]), |
| 1226 | }); |
| 1227 | expect(above.kind === "move-active" && above.assignments[0]!.orderKey < "m").toBe(true); |
| 1228 | }); |
| 1229 | |
| 1230 | describe("reordering with cached offline configuration", () => { |
| 1231 | const onlineEnvironmentId = EnvironmentId.make("online"); |
| 1232 | const offlineEnvironmentId = EnvironmentId.make("offline"); |
| 1233 | const capabilities = { |
| 1234 | repositoryIdentity: true, |
| 1235 | threadPinning: true, |
| 1236 | threadPinReorder: true, |
| 1237 | threadActiveReorder: true, |
| 1238 | }; |
| 1239 | const config = (environmentId: EnvironmentId) => ({ |
| 1240 | environment: { |
| 1241 | environmentId, |
| 1242 | label: environmentId, |
| 1243 | platform: { os: "linux" as const, arch: "x64" as const }, |
| 1244 | serverVersion: "0.1.1", |
| 1245 | capabilities, |
| 1246 | }, |
| 1247 | }); |
| 1248 | const serverConfigs = new Map([ |
| 1249 | [onlineEnvironmentId, config(onlineEnvironmentId)], |
| 1250 | [offlineEnvironmentId, config(offlineEnvironmentId)], |
| 1251 | ]); |
| 1252 | const thread = ( |
| 1253 | id: string, |
| 1254 | environmentId = onlineEnvironmentId, |
| 1255 | key: string | null = null, |
| 1256 | ) => ({ |
| 1257 | id: ThreadId.make(id), |
| 1258 | environmentId, |
| 1259 | createdAt: `2026-03-09T${id === "offline" ? "12" : id === "online" ? "11" : "10"}:00:00.000Z`, |
| 1260 | activeOrderKey: key, |
| 1261 | pinOrderKey: key, |
| 1262 | }); |
| 1263 | const keyOf = (entry: ReturnType<typeof thread>) => |
| 1264 | scopedThreadKey(scopeThreadRef(entry.environmentId, entry.id)); |
| 1265 | const online = thread("online"); |
| 1266 | const offline = thread("offline", offlineEnvironmentId); |
| 1267 | const membership = (connectedEnvironmentIds = [onlineEnvironmentId]) => |
| 1268 | resolveSidebarReorderableThreadKeys({ |
| 1269 | threads: [online, offline], |
| 1270 | serverConfigs, |
| 1271 | connectedEnvironmentIds, |
| 1272 | }); |
| 1273 | const planDrop = (input: { |
| 1274 | section: "active" | "pinned"; |
| 1275 | rows: ReturnType<typeof thread>[]; |
| 1276 | moved: ReturnType<typeof thread>; |
| 1277 | order: ReturnType<typeof thread>[]; |
| 1278 | from?: "active" | "pinned"; |
| 1279 | connectedEnvironmentIds?: EnvironmentId[]; |
| 1280 | }) => { |
| 1281 | const writable = resolveSidebarReorderableThreadKeys({ |
| 1282 | threads: input.rows, |
| 1283 | serverConfigs, |
| 1284 | connectedEnvironmentIds: input.connectedEnvironmentIds ?? [onlineEnvironmentId], |
| 1285 | }); |
| 1286 | const sort = (rows: typeof input.rows) => |
| 1287 | input.section === "active" |
| 1288 | ? sortActiveThreadsByOrderKey(rows) |
| 1289 | : sortPinnedThreadsByOrderKey(rows); |
| 1290 | const originalOrder = sort(input.rows).map(keyOf); |
| 1291 | const targetOrder = input.order.map(keyOf); |
| 1292 | const keysById = new Map(input.rows.map((entry) => [keyOf(entry), entry.activeOrderKey])); |
| 1293 | const plan = planSidebarThreadDrop({ |
| 1294 | activeKey: keyOf(input.moved), |
| 1295 | activeSection: input.from ?? input.section, |
| 1296 | target: { section: input.section, pinnedOrder: targetOrder, activeOrder: targetOrder }, |
| 1297 | pinnedOrder: originalOrder, |
| 1298 | activeOrder: originalOrder, |
| 1299 | pinnedKeysById: keysById, |
| 1300 | activeKeysById: keysById, |
| 1301 | reorderableKeys: writable.draggableThreadKeys, |
| 1302 | activeReorderableKeys: writable.activeReorderableThreadKeys, |
| 1303 | }); |
| 1304 | const assignments = |
| 1305 | plan.kind === "move-active" || plan.kind === "reorder-pinned" |
| 1306 | ? plan.assignments |
| 1307 | : plan.kind === "pin" |
| 1308 | ? [ |
| 1309 | ...plan.extraAssignments, |
| 1310 | ...(plan.orderKey === undefined |
| 1311 | ? [] |
| 1312 | : [{ id: keyOf(input.moved), orderKey: plan.orderKey }]), |
| 1313 | ] |
| 1314 | : []; |
| 1315 | const assignedKeys = new Map(assignments.map(({ id, orderKey }) => [id, orderKey])); |
| 1316 | const actualOrder = sort( |
| 1317 | input.rows.map((entry) => ({ |
| 1318 | ...entry, |
| 1319 | activeOrderKey: assignedKeys.get(keyOf(entry)) ?? entry.activeOrderKey, |
| 1320 | pinOrderKey: assignedKeys.get(keyOf(entry)) ?? entry.pinOrderKey, |
| 1321 | })), |
| 1322 | ).map(keyOf); |
| 1323 | |
| 1324 | return { plan, assignments, actualOrder, targetOrder }; |
| 1325 | }; |
| 1326 | |
| 1327 | it("excludes cached capable offline rows from both writable sets", () => { |
| 1328 | const writable = membership(); |
| 1329 | |
| 1330 | expect([...writable.draggableThreadKeys]).toEqual([keyOf(online)]); |
| 1331 | expect([...writable.activeReorderableThreadKeys]).toEqual([keyOf(online)]); |
| 1332 | expect(serverConfigs.has(offlineEnvironmentId)).toBe(true); |
| 1333 | }); |
| 1334 | |
| 1335 | it("updates membership through disconnect, reconnect, and missing configuration", () => { |
| 1336 | expect(membership([]).draggableThreadKeys.size).toBe(0); |
| 1337 | const connected = membership([onlineEnvironmentId, offlineEnvironmentId]); |
| 1338 | expect(connected.draggableThreadKeys.size).toBe(2); |
| 1339 | expect(connected.activeReorderableThreadKeys.size).toBe(2); |
| 1340 | |
| 1341 | const changed = resolveSidebarReorderableThreadKeys({ |
| 1342 | threads: [online, offline], |
| 1343 | connectedEnvironmentIds: [onlineEnvironmentId, offlineEnvironmentId], |
| 1344 | serverConfigs: new Map([[onlineEnvironmentId, config(onlineEnvironmentId)]]), |
| 1345 | }); |
| 1346 | expect([...changed.draggableThreadKeys]).toEqual([keyOf(online)]); |
| 1347 | expect([...changed.activeReorderableThreadKeys]).toEqual([keyOf(online)]); |
| 1348 | }); |
| 1349 | |
| 1350 | it.each(["active", "pinned"] as const)( |
| 1351 | "replans a disconnected drag source in %s", |
| 1352 | (section) => { |
| 1353 | const bound = thread("offline", offlineEnvironmentId, "m"); |
| 1354 | const moved = thread("online", onlineEnvironmentId, "t"); |
| 1355 | const input = { section, rows: [bound, moved], moved, order: [moved, bound] }; |
| 1356 | expect(planDrop(input).plan.kind).toBe( |
| 1357 | section === "active" ? "move-active" : "reorder-pinned", |
| 1358 | ); |
| 1359 | |
| 1360 | const disconnected = planDrop({ ...input, connectedEnvironmentIds: [] }); |
| 1361 | |
| 1362 | expect(disconnected.plan).toEqual({ kind: "none" }); |
| 1363 | expect(disconnected.assignments).toEqual([]); |
| 1364 | }, |
| 1365 | ); |
| 1366 | |
| 1367 | it.each(["active", "pinned"] as const)("preserves a keyed offline bound in %s", (section) => { |
| 1368 | const bound = thread("offline", offlineEnvironmentId, "m"); |
| 1369 | const moved = thread("online", onlineEnvironmentId, "t"); |
| 1370 | const result = planDrop({ section, rows: [bound, moved], moved, order: [moved, bound] }); |
| 1371 | |
| 1372 | expect(result.plan.kind).toBe(section === "active" ? "move-active" : "reorder-pinned"); |
| 1373 | expect(result.assignments.map(({ id }) => id)).toEqual([keyOf(moved)]); |
| 1374 | expect(result.actualOrder).toEqual(result.targetOrder); |
| 1375 | }); |
| 1376 | |
| 1377 | it.each(["active", "pinned"] as const)( |
| 1378 | "rejects rebalancing a keyed offline bound in %s", |
| 1379 | (section) => { |
| 1380 | const bound = thread("offline", offlineEnvironmentId, "m"); |
| 1381 | const neighbor = thread("neighbor"); |
| 1382 | const result = planDrop({ |
| 1383 | section, |
| 1384 | rows: [bound, online, neighbor], |
| 1385 | moved: online, |
| 1386 | order: [bound, neighbor, online], |
| 1387 | }); |
| 1388 | |
| 1389 | expect(result.plan).toEqual({ kind: "none" }); |
| 1390 | expect(result.assignments).toEqual([]); |
| 1391 | }, |
| 1392 | ); |
| 1393 | |
| 1394 | it.each(["active", "pinned"] as const)( |
| 1395 | "rejects crossing the frozen keyless boundary in %s", |
| 1396 | (section) => { |
| 1397 | const bound = { |
| 1398 | ...offline, |
| 1399 | createdAt: |
| 1400 | section === "active" ? "2026-03-09T12:00:00.000Z" : "2026-03-09T10:00:00.000Z", |
| 1401 | }; |
| 1402 | const order = section === "active" ? [online, bound] : [bound, online]; |
| 1403 | const result = planDrop({ section, rows: [bound, online], moved: online, order }); |
| 1404 | |
| 1405 | expect(result.plan).toEqual({ kind: "none" }); |
| 1406 | expect(result.assignments).toEqual([]); |
| 1407 | }, |
| 1408 | ); |
| 1409 | |
| 1410 | it.each(["active", "pinned"] as const)( |
| 1411 | "allows realizable moves beside a frozen keyless row in %s", |
| 1412 | (section) => { |
| 1413 | const neighbor = thread("neighbor"); |
| 1414 | const order = |
| 1415 | section === "active" ? [offline, neighbor, online] : [online, neighbor, offline]; |
| 1416 | const result = planDrop({ |
| 1417 | section, |
| 1418 | rows: [offline, online, neighbor], |
| 1419 | moved: online, |
| 1420 | order, |
| 1421 | }); |
| 1422 | |
| 1423 | expect(result.plan.kind).toBe(section === "active" ? "move-active" : "reorder-pinned"); |
| 1424 | expect(result.assignments.length).toBeGreaterThan(0); |
| 1425 | expect(result.assignments.every(({ id }) => id !== keyOf(offline))).toBe(true); |
| 1426 | expect(result.actualOrder).toEqual(result.targetOrder); |
| 1427 | }, |
| 1428 | ); |
| 1429 | |
| 1430 | it.each(["active", "pinned"] as const)( |
| 1431 | "checks fallback assignments to other rows at the %s boundary", |
| 1432 | (section) => { |
| 1433 | const neighbor = thread("neighbor"); |
| 1434 | const order = |
| 1435 | section === "active" ? [neighbor, offline, online] : [online, offline, neighbor]; |
| 1436 | const result = planDrop({ |
| 1437 | section, |
| 1438 | rows: [offline, online, neighbor], |
| 1439 | moved: online, |
| 1440 | order, |
| 1441 | }); |
| 1442 | |
| 1443 | expect(result.plan).toEqual({ kind: "none" }); |
| 1444 | }, |
| 1445 | ); |
| 1446 | |
| 1447 | it.each(["active", "pinned"] as const)( |
| 1448 | "rejects impossible pin/unpin placement across the %s boundary", |
| 1449 | (section) => { |
| 1450 | const order = section === "active" ? [online, offline] : [offline, online]; |
| 1451 | const result = planDrop({ |
| 1452 | section, |
| 1453 | rows: [offline, online], |
| 1454 | moved: online, |
| 1455 | order, |
| 1456 | from: section === "active" ? "pinned" : "active", |
| 1457 | }); |
| 1458 | |
| 1459 | expect(result.plan).toEqual({ kind: "none" }); |
| 1460 | }, |
| 1461 | ); |
| 1462 | }); |
| 1463 | |
| 1464 | it("only changes lifecycle when the inbox is time-ordered", () => { |
| 1465 | const base = { |
| 1466 | pinnedOrder: ["p1"], |
| 1467 | pinnedKeysById: new Map([["p1", "m"]]), |
| 1468 | activeOrder: ["a1", "a2"], |
| 1469 | activeKeysById: new Map([ |
| 1470 | ["a1", "f"], |
| 1471 | ["a2", "t"], |
| 1472 | ]), |
| 1473 | activeTimeOrdered: true, |
| 1474 | }; |
| 1475 | expect( |
| 1476 | planSidebarThreadDrop({ |
| 1477 | ...base, |
| 1478 | activeKey: "a1", |
| 1479 | activeSection: "active", |
| 1480 | target: { section: "active", pinnedOrder: ["p1"], activeOrder: ["a2", "a1"] }, |
| 1481 | }), |
| 1482 | ).toEqual({ kind: "none" }); |
| 1483 | expect( |
| 1484 | planSidebarThreadDrop({ |
| 1485 | ...base, |
| 1486 | activeKey: "p1", |
| 1487 | activeSection: "pinned", |
| 1488 | target: { section: "active", pinnedOrder: [], activeOrder: ["a1", "p1", "a2"] }, |
| 1489 | }), |
| 1490 | ).toEqual({ |
| 1491 | kind: "move-active", |
| 1492 | order: null, |
| 1493 | assignments: [], |
| 1494 | unpin: true, |
| 1495 | unsettle: false, |
| 1496 | unsnooze: false, |
| 1497 | }); |
| 1498 | }); |
| 1499 | }); |
| 1500 | }); |
| 1501 | |
| 1502 | describe("pendingSidebarRouteReveal", () => { |
| 1503 | it("keeps a revealed thread settled across a mount that reads no route params", () => { |
| 1504 | const reveal = { observedKey: null, pendingKey: null }; |
| 1505 | expect(pendingSidebarRouteReveal(reveal, "env:a1")).toBe("env:a1"); |
| 1506 | completeSidebarRouteReveal(reveal); |
| 1507 | |
| 1508 | // Back from Settings: location commits before the thread match does. |
| 1509 | expect(pendingSidebarRouteReveal(reveal, null)).toBeNull(); |
| 1510 | expect(pendingSidebarRouteReveal(reveal, "env:a1")).toBeNull(); |
| 1511 | |
| 1512 | expect(pendingSidebarRouteReveal(reveal, "env:b1")).toBe("env:b1"); |
| 1513 | }); |
| 1514 | |
| 1515 | it("keeps a pending reveal through a render without route params", () => { |
| 1516 | const reveal = { observedKey: null, pendingKey: null }; |
| 1517 | pendingSidebarRouteReveal(reveal, "env:a1"); |
| 1518 | expect(pendingSidebarRouteReveal(reveal, null)).toBeNull(); |
| 1519 | expect(pendingSidebarRouteReveal(reveal, "env:a1")).toBe("env:a1"); |
| 1520 | }); |
| 1521 | }); |
| 1522 | |
| 1523 | describe("resolveSidebarRouteReveal", () => { |
| 1524 | const scopedProjectKeys = new Set(["environment-test:project-scoped"]); |
| 1525 | const hidden = makeThreadFixture({ projectId: ProjectId.make("project-other") }); |
| 1526 | const hiddenKey = "environment-test:thread-test"; |
| 1527 | const findThread = (key: string) => (key === hiddenKey ? hidden : undefined); |
| 1528 | |
| 1529 | it("widens the scope for a newly opened thread the scope hides", () => { |
| 1530 | expect( |
| 1531 | resolveSidebarRouteReveal({ |
| 1532 | reveal: { observedKey: "environment-test:other", pendingKey: null }, |
| 1533 | routeKey: hiddenKey, |
| 1534 | routeKeyInPlace: false, |
| 1535 | isSearching: false, |
| 1536 | scopedProjectKeys, |
| 1537 | delegatedUnderParent: new Set(), |
| 1538 | findThread, |
| 1539 | }), |
| 1540 | ).toEqual({ pendingKey: hiddenKey, widensScope: true }); |
| 1541 | }); |
| 1542 | |
| 1543 | it("never widens for a thread whose reveal already completed", () => { |
| 1544 | // A remount's once-per-mount scroll must keep the scope the user chose. |
| 1545 | expect( |
| 1546 | resolveSidebarRouteReveal({ |
| 1547 | reveal: { observedKey: hiddenKey, pendingKey: null }, |
| 1548 | routeKey: hiddenKey, |
| 1549 | routeKeyInPlace: false, |
| 1550 | isSearching: false, |
| 1551 | scopedProjectKeys, |
| 1552 | delegatedUnderParent: new Set(), |
| 1553 | findThread, |
| 1554 | }), |
| 1555 | ).toEqual({ pendingKey: null, widensScope: false }); |
| 1556 | }); |
| 1557 | |
| 1558 | it("widens again for a thread reopened before the thread between them listed its row", () => { |
| 1559 | // The user scoped away from the open thread, then went to a thread whose row |
| 1560 | // never mounted, then came back. |
| 1561 | const reveal = { observedKey: hiddenKey, pendingKey: null }; |
| 1562 | const resolve = (routeKey: string) => |
| 1563 | resolveSidebarRouteReveal({ |
| 1564 | reveal, |
| 1565 | routeKey, |
| 1566 | routeKeyInPlace: false, |
| 1567 | isSearching: false, |
| 1568 | scopedProjectKeys, |
| 1569 | delegatedUnderParent: new Set(), |
| 1570 | findThread, |
| 1571 | }); |
| 1572 | resolve("environment-test:unlisted"); |
| 1573 | expect(resolve(hiddenKey)).toEqual({ pendingKey: hiddenKey, widensScope: true }); |
| 1574 | }); |
| 1575 | |
| 1576 | it("holds a pending reveal without widening while searching", () => { |
| 1577 | expect( |
| 1578 | resolveSidebarRouteReveal({ |
| 1579 | reveal: { observedKey: hiddenKey, pendingKey: hiddenKey }, |
| 1580 | routeKey: hiddenKey, |
| 1581 | routeKeyInPlace: false, |
| 1582 | isSearching: true, |
| 1583 | scopedProjectKeys, |
| 1584 | delegatedUnderParent: new Set(), |
| 1585 | findThread, |
| 1586 | }), |
| 1587 | ).toEqual({ pendingKey: hiddenKey, widensScope: false }); |
| 1588 | }); |
| 1589 | }); |
| 1590 | |
| 1591 | describe("sidebarFolderExpansionKeys", () => { |
| 1592 | it("honors a collapse the legacy sidebar saved under a member's cwd", () => { |
| 1593 | const keys = sidebarFolderExpansionKeys({ |
| 1594 | projectKey: "github.com/pingdotgg/t3code", |
| 1595 | memberProjects: [{ physicalProjectKey: "local:/code/t3code", workspaceRoot: "/code/t3code" }], |
| 1596 | }); |
| 1597 | const cwdKey = keys.at(-1)!; |
| 1598 | |
| 1599 | expect(resolveProjectExpanded({}, keys)).toBe(true); |
| 1600 | expect(resolveProjectExpanded({ [cwdKey]: false }, keys)).toBe(false); |
| 1601 | expect(resolveProjectExpanded({ [keys[0]!]: true, [cwdKey]: false }, keys)).toBe(true); |
| 1602 | }); |
| 1603 | }); |
| 1604 | |
| 1605 | describe("groupSidebarThreadsByProject", () => { |
| 1606 | it("puts each thread in its project's folder and drops empty projects unless kept", () => { |
| 1607 | const remoteEnvironmentId = EnvironmentId.make("environment-remote"); |
| 1608 | const t3code = ProjectId.make("t3code"); |
| 1609 | const t3codeRemote = ProjectId.make("t3code-remote"); |
| 1610 | const fleet = ProjectId.make("fleet"); |
| 1611 | const thread = (id: string, environmentId: EnvironmentId, projectId: ProjectId) => ({ |
| 1612 | id, |
| 1613 | environmentId, |
| 1614 | projectId, |
| 1615 | }); |
| 1616 | const groups = [ |
| 1617 | { |
| 1618 | projectKey: "t3code", |
| 1619 | memberProjectRefs: [ |
| 1620 | { environmentId: localEnvironmentId, projectId: t3code }, |
| 1621 | { environmentId: remoteEnvironmentId, projectId: t3codeRemote }, |
| 1622 | ], |
| 1623 | }, |
| 1624 | { |
| 1625 | projectKey: "fleet", |
| 1626 | memberProjectRefs: [{ environmentId: localEnvironmentId, projectId: fleet }], |
| 1627 | }, |
| 1628 | { |
| 1629 | projectKey: "notes", |
| 1630 | memberProjectRefs: [ |
| 1631 | { environmentId: localEnvironmentId, projectId: ProjectId.make("notes") }, |
| 1632 | ], |
| 1633 | }, |
| 1634 | ]; |
| 1635 | |
| 1636 | const threads = [ |
| 1637 | thread("a", localEnvironmentId, t3code), |
| 1638 | thread("orphan", localEnvironmentId, ProjectId.make("deleted")), |
| 1639 | thread("b", remoteEnvironmentId, t3codeRemote), |
| 1640 | ]; |
| 1641 | const summarize = (keepEmpty: (group: (typeof groups)[number]) => boolean) => |
| 1642 | groupSidebarThreadsByProject(groups, threads, keepEmpty).map((folder) => ({ |
| 1643 | projectKey: folder.group.projectKey, |
| 1644 | threads: folder.threads.map((entry) => entry.id), |
| 1645 | })); |
| 1646 | |
| 1647 | expect(summarize(() => true)).toEqual([ |
| 1648 | { projectKey: "t3code", threads: ["a", "b"] }, |
| 1649 | { projectKey: "fleet", threads: [] }, |
| 1650 | { projectKey: "notes", threads: [] }, |
| 1651 | ]); |
| 1652 | expect(summarize(() => false)).toEqual([{ projectKey: "t3code", threads: ["a", "b"] }]); |
| 1653 | // A pinned project keeps its empty folder while other empty projects drop out. |
| 1654 | expect(summarize((group) => group.projectKey === "fleet")).toEqual([ |
| 1655 | { projectKey: "t3code", threads: ["a", "b"] }, |
| 1656 | { projectKey: "fleet", threads: [] }, |
| 1657 | ]); |
| 1658 | }); |
| 1659 | }); |
| 1660 | |
| 1661 | describe("resolveSidebarFolderDot", () => { |
| 1662 | it("flags the most urgent hidden thread and stays quiet otherwise", () => { |
| 1663 | const idle = { |
| 1664 | hasActionableProposedPlan: false, |
| 1665 | hasPendingApprovals: false, |
| 1666 | hasPendingUserInput: false, |
| 1667 | interactionMode: "default" as const, |
| 1668 | latestRun: makeLatestRun(), |
| 1669 | lastVisitedAt: "2026-03-09T10:06:00.000Z", |
| 1670 | runtime: null, |
| 1671 | }; |
| 1672 | const runtime = { |
| 1673 | status: "running" as const, |
| 1674 | activeRunId: null, |
| 1675 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 1676 | providerName: "Codex", |
| 1677 | lastError: null, |
| 1678 | updatedAt: "2026-03-09T10:00:00.000Z", |
| 1679 | }; |
| 1680 | const working = { ...idle, runtime }; |
| 1681 | const unread = { ...idle, lastVisitedAt: "2026-03-09T10:04:00.000Z" }; |
| 1682 | const limited = { |
| 1683 | ...idle, |
| 1684 | runtime: { ...runtime, status: "failed" as const, lastErrorClass: "usage_limit" as const }, |
| 1685 | }; |
| 1686 | |
| 1687 | expect(resolveSidebarFolderDot([idle])).toBeNull(); |
| 1688 | // A waiting thread stays quiet even with an unread completion, like its row. |
| 1689 | expect( |
| 1690 | resolveSidebarFolderDot([{ ...unread, runtime: { ...runtime, status: "idle" as const } }]), |
| 1691 | ).toBeNull(); |
| 1692 | expect(resolveSidebarFolderDot([idle, working])).toBe("working"); |
| 1693 | expect(resolveSidebarFolderDot([working, unread])).toBe("unread"); |
| 1694 | expect(resolveSidebarFolderDot([unread, limited])).toBe("attention"); |
| 1695 | }); |
| 1696 | }); |
| 1697 | |
| 1698 | describe("shouldCreateNewThreadInCurrentProject", () => { |
| 1699 | it("opens the picker when several projects are open, or only archived ones exist", () => { |
| 1700 | expect(shouldCreateNewThreadInCurrentProject(false, 2, 3)).toBe(false); |
| 1701 | expect(shouldCreateNewThreadInCurrentProject(false, 0, 2)).toBe(false); |
| 1702 | expect(shouldCreateNewThreadInCurrentProject(false, 1, 3)).toBe(true); |
| 1703 | expect(shouldCreateNewThreadInCurrentProject(false, 0, 0)).toBe(true); |
| 1704 | expect(shouldCreateNewThreadInCurrentProject(true, 2, 2)).toBe(true); |
| 1705 | }); |
| 1706 | }); |