The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search"; |
| 2 | import { createInboxReturnTracker } from "@t3tools/client-runtime/state/thread-inbox"; |
| 3 | import { presentThreadShell } from "@t3tools/client-runtime/state/models"; |
| 4 | import * as DateTime from "effect/DateTime"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import { planPinnedReorder } from "@t3tools/client-runtime/state/thread-sort"; |
| 7 | import { |
| 8 | createPendingThreadOrder, |
| 9 | createThreadMovePlanner, |
| 10 | threadOrderAfterMove, |
| 11 | threadDropLifecycle, |
| 12 | reconcilePendingThreadOrder, |
| 13 | type PendingThreadOrder, |
| 14 | type ThreadMoveAvailability, |
| 15 | } from "./threadOrder"; |
| 16 | import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 17 | import { canSnooze, resolveSnoozePresets } from "@t3tools/client-runtime/state/thread-settled"; |
| 18 | import { |
| 19 | CommandId, |
| 20 | EnvironmentId, |
| 21 | MessageId, |
| 22 | OrchestrationV2ThreadShell, |
| 23 | ProjectId, |
| 24 | ProviderInstanceId, |
| 25 | RunId, |
| 26 | RuntimeRequestId, |
| 27 | ThreadId, |
| 28 | } from "@t3tools/contracts"; |
| 29 | import type { MenuAction } from "@react-native-menu/menu"; |
| 30 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 31 | |
| 32 | import type { PendingNewTask } from "../../state/use-pending-new-tasks"; |
| 33 | import { makeRawThreadShell, makeThreadShellFixture } from "../../test-fixtures"; |
| 34 | import { threadJumpTarget } from "../keyboard/threadKeyboardShortcuts"; |
| 35 | import { |
| 36 | buildThreadListV2Items, |
| 37 | buildThreadListV2ListItems, |
| 38 | getThreadListV2OrderedSection, |
| 39 | layoutThreadListV2RowMenuActions, |
| 40 | resolveThreadListV2SnoozeMenuSelection, |
| 41 | resolveThreadListV2SwipeActions, |
| 42 | resolveThreadListV2SnoozePresets, |
| 43 | resolveThreadListV2RowStatus, |
| 44 | resolveThreadListV2Status, |
| 45 | resolveThreadListV2WokeAt, |
| 46 | threadListV2ListItemsAreEqual, |
| 47 | threadListV2RowAccessibilityLabel, |
| 48 | threadHasUnseenCompletion, |
| 49 | type ThreadListV2ListItem, |
| 50 | } from "./threadListV2"; |
| 51 | |
| 52 | const environmentId = EnvironmentId.make("environment-1"); |
| 53 | const decodeThreadShell = Schema.decodeUnknownSync(OrchestrationV2ThreadShell); |
| 54 | |
| 55 | function makeThread( |
| 56 | input: Partial<EnvironmentThreadShell> & Pick<EnvironmentThreadShell, "id" | "title">, |
| 57 | ): EnvironmentThreadShell { |
| 58 | return makeThreadShellFixture({ |
| 59 | environmentId, |
| 60 | ...input, |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | const NOW = "2026-06-02T00:00:00.000Z"; |
| 65 | |
| 66 | describe("resolveThreadListV2SnoozeMenuSelection", () => { |
| 67 | it("recomputes the environment's presets instead of using old timestamps", () => { |
| 68 | const presets = [{ kind: "after", minutes: 360 }] as const; |
| 69 | const displayedPresets = resolveSnoozePresets(new Date(2026, 4, 8, 10), { presets }); |
| 70 | const selectedAt = new Date(2026, 4, 8, 10, 30); |
| 71 | const selection = resolveThreadListV2SnoozeMenuSelection({ |
| 72 | event: "snooze:after:360", |
| 73 | displayedPresets, |
| 74 | now: selectedAt, |
| 75 | presets, |
| 76 | }); |
| 77 | |
| 78 | expect(selection._tag).toBe("selected"); |
| 79 | if (selection._tag === "selected") { |
| 80 | expect(selection.preset.snoozedUntil).toBe( |
| 81 | new Date(selectedAt.getTime() + 6 * 60 * 60 * 1_000).toISOString(), |
| 82 | ); |
| 83 | } |
| 84 | }); |
| 85 | }); |
| 86 | |
| 87 | describe("resolveThreadListV2SnoozePresets", () => { |
| 88 | it("reads preset times from the list minute, not the clock at first render", () => { |
| 89 | using _timers = { [Symbol.dispose]: () => vi.useRealTimers() }; |
| 90 | vi.useFakeTimers({ now: new Date(2026, 4, 8, 10) }); |
| 91 | const laterMinute = new Date(2026, 4, 8, 19, 15).toISOString(); |
| 92 | |
| 93 | const presets = resolveThreadListV2SnoozePresets({ |
| 94 | secondary: "snooze", |
| 95 | now: laterMinute, |
| 96 | presets: undefined, |
| 97 | }); |
| 98 | |
| 99 | expect(presets.find((preset) => preset.id === "after:60")?.snoozedUntil).toBe( |
| 100 | new Date(2026, 4, 8, 20, 15).toISOString(), |
| 101 | ); |
| 102 | // The evening preset is gone by 19:15; a reading from 10:00 would still offer it. |
| 103 | expect(presets.map((preset) => preset.id)).not.toContain("at:today:18:00"); |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | describe("layoutThreadListV2RowMenuActions", () => { |
| 108 | const actions = (...ids: string[]): MenuAction[] => ids.map((id) => ({ id, title: id })); |
| 109 | const leafIds = (items: readonly MenuAction[]): string[] => |
| 110 | items.flatMap((item) => (item.subactions ? leafIds(item.subactions) : [item.id!])); |
| 111 | |
| 112 | it("keeps every action once while nesting iOS secondary actions under More", () => { |
| 113 | const full = { |
| 114 | direct: actions("new-thread-on-branch", "copy-thread", "copy-thread-id", "settle", "snooze"), |
| 115 | reorder: actions("arrange", "move-up", "move-down"), |
| 116 | pin: actions("unpin"), |
| 117 | secondary: actions("rename", "regenerate-title", "turn-into-project", "archive", "delete"), |
| 118 | }; |
| 119 | const flat = layoutThreadListV2RowMenuActions({ ...full, nested: false }); |
| 120 | const nested = layoutThreadListV2RowMenuActions({ ...full, nested: true }); |
| 121 | |
| 122 | expect(flat.map((action) => action.id)).toEqual( |
| 123 | [...full.direct, ...full.reorder, ...full.pin, ...full.secondary].map((action) => action.id), |
| 124 | ); |
| 125 | expect(nested.map((action) => action.id)).toEqual([ |
| 126 | "new-thread-on-branch", |
| 127 | "copy-thread", |
| 128 | "copy-thread-id", |
| 129 | "settle", |
| 130 | "snooze", |
| 131 | "unpin", |
| 132 | "more", |
| 133 | ]); |
| 134 | expect(nested.at(-1)?.subactions?.map((action) => action.id)).toEqual([ |
| 135 | "arrange-menu", |
| 136 | "rename", |
| 137 | "regenerate-title", |
| 138 | "turn-into-project", |
| 139 | "archive", |
| 140 | "delete", |
| 141 | ]); |
| 142 | expect(leafIds(nested).toSorted()).toEqual(leafIds(flat).toSorted()); |
| 143 | |
| 144 | // Slim rows only offer "Arrange threads…", which needs no submenu of its own. |
| 145 | const slim = layoutThreadListV2RowMenuActions({ |
| 146 | ...full, |
| 147 | reorder: actions("arrange"), |
| 148 | nested: true, |
| 149 | }); |
| 150 | expect(slim.at(-1)?.subactions?.[0]?.id).toBe("arrange"); |
| 151 | }); |
| 152 | }); |
| 153 | |
| 154 | describe("resolveThreadListV2Status", () => { |
| 155 | it("presents decoded thread waits as Waiting while preserving requests and older servers", () => { |
| 156 | const raw = makeRawThreadShell({ |
| 157 | status: "running", |
| 158 | activeRunId: RunId.make("run-wait"), |
| 159 | latestRunId: RunId.make("run-wait"), |
| 160 | }); |
| 161 | const present = (shell: unknown) => presentThreadShell(environmentId, decodeThreadShell(shell)); |
| 162 | |
| 163 | expect(resolveThreadListV2Status(present(raw))).toBe("working"); |
| 164 | const blocked = present({ ...raw, blockedOnThreadWait: true }); |
| 165 | expect( |
| 166 | resolveThreadListV2Status({ |
| 167 | ...blocked, |
| 168 | hasPendingApprovals: true, |
| 169 | hasPendingUserInput: true, |
| 170 | }), |
| 171 | ).toBe("approval"); |
| 172 | expect(resolveThreadListV2Status({ ...blocked, hasPendingUserInput: true })).toBe("input"); |
| 173 | expect(resolveThreadListV2Status(blocked)).toBe("waiting"); |
| 174 | expect(resolveThreadListV2Status(present({ ...raw, blockedOnThreadWait: false }))).toBe( |
| 175 | "working", |
| 176 | ); |
| 177 | }); |
| 178 | |
| 179 | it("settles a failure the user opened to ready in the row, keeping the raw status", () => { |
| 180 | const failed = presentThreadShell( |
| 181 | environmentId, |
| 182 | makeRawThreadShell({ |
| 183 | status: "failed", |
| 184 | latestRunId: RunId.make("run-failed"), |
| 185 | latestRunCompletedAt: DateTime.makeUnsafe("2026-06-01T23:59:00.000Z"), |
| 186 | lastVisitedAt: DateTime.makeUnsafe(NOW), |
| 187 | }), |
| 188 | ); |
| 189 | |
| 190 | expect(resolveThreadListV2Status(failed)).toBe("failed"); |
| 191 | expect(resolveThreadListV2RowStatus(failed)).toBe("ready"); |
| 192 | expect(resolveThreadListV2RowStatus({ ...failed, lastVisitedAt: null })).toBe("failed"); |
| 193 | }); |
| 194 | |
| 195 | it("shows a waiting guest message as Request, after the agent's own requests", () => { |
| 196 | const thread = presentThreadShell( |
| 197 | environmentId, |
| 198 | decodeThreadShell(makeRawThreadShell({ status: "running" })), |
| 199 | ); |
| 200 | const guest = { ...thread, pendingGuestMessageCount: 1 }; |
| 201 | |
| 202 | expect(resolveThreadListV2Status(guest)).toBe("request"); |
| 203 | expect(resolveThreadListV2Status({ ...guest, hasPendingUserInput: true })).toBe("input"); |
| 204 | expect(resolveThreadListV2Status({ ...guest, hasPendingApprovals: true })).toBe("approval"); |
| 205 | }); |
| 206 | |
| 207 | it.each([ |
| 208 | { roster: "dev server", task: { kind: "command" }, status: "ready" }, |
| 209 | { roster: "dev server watchdog", task: { kind: "monitor" }, status: "ready" }, |
| 210 | { roster: "awaited command", task: { kind: "command", awaited: true }, status: "waiting" }, |
| 211 | ] as const)( |
| 212 | "presents an unseen completion with a $roster roster as $status", |
| 213 | ({ task, status }) => { |
| 214 | const thread = presentThreadShell( |
| 215 | environmentId, |
| 216 | makeRawThreadShell({ |
| 217 | latestRunId: RunId.make("run-background-completion"), |
| 218 | status: "completed", |
| 219 | latestRunCompletedAt: DateTime.makeUnsafe(NOW), |
| 220 | lastVisitedAt: DateTime.makeUnsafe("2026-06-01T23:59:00.000Z"), |
| 221 | pendingBackgroundTasks: [{ taskId: "background-work", ...task }], |
| 222 | }), |
| 223 | ); |
| 224 | |
| 225 | expect(resolveThreadListV2Status(thread)).toBe(status); |
| 226 | expect(threadHasUnseenCompletion(thread)).toBe(true); |
| 227 | }, |
| 228 | ); |
| 229 | |
| 230 | it.each(["interrupted", "cancelled"] as const)( |
| 231 | "does not present a %s run as an unseen completion", |
| 232 | (status) => { |
| 233 | const thread = presentThreadShell( |
| 234 | environmentId, |
| 235 | makeRawThreadShell({ |
| 236 | latestRunId: RunId.make("run-stopped"), |
| 237 | status, |
| 238 | latestRunCompletedAt: DateTime.makeUnsafe(NOW), |
| 239 | lastVisitedAt: DateTime.makeUnsafe("2026-06-01T23:59:00.000Z"), |
| 240 | }), |
| 241 | ); |
| 242 | |
| 243 | expect(threadHasUnseenCompletion(thread)).toBe(false); |
| 244 | }, |
| 245 | ); |
| 246 | }); |
| 247 | |
| 248 | describe("threadListV2RowAccessibilityLabel", () => { |
| 249 | const row = { |
| 250 | title: "Fix login", |
| 251 | variant: "card", |
| 252 | woke: false, |
| 253 | statusLabel: undefined, |
| 254 | timeLabel: "5m", |
| 255 | snoozeWakeLabel: undefined, |
| 256 | providerNameLabel: "Codex", |
| 257 | hasQueuedMessages: false, |
| 258 | regeneratingTitle: false, |
| 259 | } as const; |
| 260 | |
| 261 | it("speaks the status or time the row shows beside its title", () => { |
| 262 | expect(threadListV2RowAccessibilityLabel({ ...row, statusLabel: "Approval" })).toBe( |
| 263 | "Fix login, Approval, Codex", |
| 264 | ); |
| 265 | expect(threadListV2RowAccessibilityLabel(row)).toBe("Fix login, 5m, Codex"); |
| 266 | expect(threadListV2RowAccessibilityLabel({ ...row, woke: true, statusLabel: "Done" })).toBe( |
| 267 | "Fix login, Woke, Codex", |
| 268 | ); |
| 269 | expect( |
| 270 | threadListV2RowAccessibilityLabel({ |
| 271 | ...row, |
| 272 | variant: "slim", |
| 273 | statusLabel: "Failed", |
| 274 | snoozeWakeLabel: "in 2h", |
| 275 | }), |
| 276 | ).toBe("Fix login, in 2h, Codex"); |
| 277 | }); |
| 278 | }); |
| 279 | |
| 280 | describe("resolveThreadListV2SwipeActions", () => { |
| 281 | it("offers snooze for a presented input request and moves it onto the snoozed shelf", () => { |
| 282 | const raw = makeRawThreadShell({ |
| 283 | pendingRuntimeRequest: { |
| 284 | id: RuntimeRequestId.make("question"), |
| 285 | kind: "user_input", |
| 286 | createdAt: DateTime.makeUnsafe(NOW), |
| 287 | }, |
| 288 | }); |
| 289 | const thread = presentThreadShell(environmentId, raw); |
| 290 | expect(thread.pendingUserInputAt).toBe(NOW); |
| 291 | expect( |
| 292 | resolveThreadListV2SwipeActions({ |
| 293 | variant: "card", |
| 294 | settlementSupported: true, |
| 295 | snoozeSupported: true, |
| 296 | snoozable: canSnooze(thread, { now: NOW }), |
| 297 | }).secondary, |
| 298 | ).toBe("snooze"); |
| 299 | |
| 300 | const layout = buildThreadListV2Items({ |
| 301 | threads: [{ ...thread, snoozedAt: NOW, snoozedUntil: "2026-06-03T00:00:00.000Z" }], |
| 302 | environmentId: null, |
| 303 | searchQuery: "", |
| 304 | now: NOW, |
| 305 | snoozedShelfExpanded: true, |
| 306 | snoozeEnvironmentIds: new Set([environmentId]), |
| 307 | }); |
| 308 | expect(layout.snoozedCount).toBe(1); |
| 309 | }); |
| 310 | }); |
| 311 | |
| 312 | describe("search lists ranked matches", () => { |
| 313 | const matchKey = (id: string) => |
| 314 | threadSearchMatchKey({ environmentId, threadId: ThreadId.make(id) }); |
| 315 | |
| 316 | it("lists matches by rank across shelves, then threads listed for their messages", () => { |
| 317 | const layout = buildThreadListV2Items({ |
| 318 | threads: [ |
| 319 | makeThread({ id: ThreadId.make("notes"), title: "Deploy notes" }), |
| 320 | makeThread({ id: ThreadId.make("mentioned"), title: "Weekly notes" }), |
| 321 | makeThread({ id: ThreadId.make("exact"), title: "Deploy", settledOverride: "settled" }), |
| 322 | ], |
| 323 | environmentId: null, |
| 324 | searchQuery: "deploy", |
| 325 | matchedThreadKeys: new Set([matchKey("mentioned"), matchKey("notes")]), |
| 326 | settledShelfExpanded: false, |
| 327 | now: NOW, |
| 328 | }); |
| 329 | // The exact title leads even from a collapsed Settled shelf, and keeps its look. |
| 330 | expect(layout.items.map((item) => [item.thread.id, item.variant])).toEqual([ |
| 331 | ["exact", "slim"], |
| 332 | ["notes", "card"], |
| 333 | ["mentioned", "card"], |
| 334 | ]); |
| 335 | expect([...layout.contentOnlyThreadKeys]).toEqual([matchKey("mentioned")]); |
| 336 | }); |
| 337 | }); |
| 338 | |
| 339 | describe("getThreadListV2OrderedSection", () => { |
| 340 | it("uses each saved order and excludes settled, snoozed, and archived rows", () => { |
| 341 | const threads = [ |
| 342 | makeThread({ id: ThreadId.make("active-later"), title: "Later", activeOrderKey: "t" }), |
| 343 | makeThread({ id: ThreadId.make("active-first"), title: "First", activeOrderKey: "f" }), |
| 344 | makeThread({ id: ThreadId.make("active-new"), title: "New" }), |
| 345 | makeThread({ |
| 346 | id: ThreadId.make("pinned-later"), |
| 347 | title: "Pinned later", |
| 348 | pinnedAt: NOW, |
| 349 | pinOrderKey: "t", |
| 350 | activeOrderKey: "f", |
| 351 | }), |
| 352 | makeThread({ |
| 353 | id: ThreadId.make("pinned-first"), |
| 354 | title: "Pinned first", |
| 355 | pinnedAt: NOW, |
| 356 | pinOrderKey: "f", |
| 357 | activeOrderKey: "t", |
| 358 | }), |
| 359 | makeThread({ id: ThreadId.make("settled"), title: "Settled", settledOverride: "settled" }), |
| 360 | makeThread({ id: ThreadId.make("archived"), title: "Archived", archivedAt: NOW }), |
| 361 | makeThread({ |
| 362 | id: ThreadId.make("snoozed"), |
| 363 | title: "Snoozed", |
| 364 | snoozedUntil: "2026-06-03T10:00:00.000Z", |
| 365 | snoozedAt: NOW, |
| 366 | }), |
| 367 | makeThread({ |
| 368 | id: ThreadId.make("pinned-snoozed"), |
| 369 | title: "Pinned snoozed", |
| 370 | pinnedAt: NOW, |
| 371 | snoozedUntil: "2026-06-03T10:00:00.000Z", |
| 372 | snoozedAt: NOW, |
| 373 | }), |
| 374 | ]; |
| 375 | expect( |
| 376 | getThreadListV2OrderedSection({ threads, section: "active", now: NOW }).map( |
| 377 | (thread) => thread.id, |
| 378 | ), |
| 379 | ).toEqual(["active-new", "active-first", "active-later"]); |
| 380 | expect( |
| 381 | getThreadListV2OrderedSection({ threads, section: "pinned", now: NOW }).map( |
| 382 | (thread) => thread.id, |
| 383 | ), |
| 384 | ).toEqual(["pinned-first", "pinned-later"]); |
| 385 | }); |
| 386 | }); |
| 387 | |
| 388 | describe("buildThreadListV2Items", () => { |
| 389 | it("hides snoozed threads and counts them — visibility parity with web", () => { |
| 390 | const layout = buildThreadListV2Items({ |
| 391 | threads: [ |
| 392 | makeThread({ id: ThreadId.make("active"), title: "Active" }), |
| 393 | makeThread({ |
| 394 | id: ThreadId.make("snoozed"), |
| 395 | title: "Snoozed", |
| 396 | snoozedUntil: "2026-06-03T09:00:00.000Z", |
| 397 | snoozedAt: "2026-06-01T12:00:00.000Z", |
| 398 | }), |
| 399 | makeThread({ |
| 400 | id: ThreadId.make("woken"), |
| 401 | title: "Woken", |
| 402 | snoozedUntil: "2026-06-01T18:00:00.000Z", |
| 403 | snoozedAt: "2026-06-01T12:00:00.000Z", |
| 404 | }), |
| 405 | ], |
| 406 | environmentId: null, |
| 407 | searchQuery: "", |
| 408 | now: NOW, |
| 409 | }); |
| 410 | |
| 411 | expect(layout.items.map((item) => item.thread.id)).toEqual(["active", "woken"]); |
| 412 | expect(layout.snoozedCount).toBe(1); |
| 413 | }); |
| 414 | |
| 415 | it("moves a settled pinned thread into the settled shelf — parity with web (#7969)", () => { |
| 416 | const layout = buildThreadListV2Items({ |
| 417 | threads: [ |
| 418 | makeThread({ id: ThreadId.make("active"), title: "Active" }), |
| 419 | makeThread({ |
| 420 | id: ThreadId.make("pinned-settled"), |
| 421 | title: "Pinned while settled", |
| 422 | pinnedAt: "2026-06-01T12:00:00.000Z", |
| 423 | settledOverride: "settled", |
| 424 | settledAt: "2026-06-01T12:00:00.000Z", |
| 425 | }), |
| 426 | ], |
| 427 | environmentId: null, |
| 428 | searchQuery: "", |
| 429 | now: NOW, |
| 430 | }); |
| 431 | |
| 432 | expect(layout.items.map((item) => item.thread.id)).toEqual(["active", "pinned-settled"]); |
| 433 | expect(layout.items.map((item) => item.pinned)).toEqual([false, false]); |
| 434 | expect(layout.settledCount).toBe(1); |
| 435 | }); |
| 436 | |
| 437 | it("snooze hides a pinned thread and wake restores it to the pinned block", () => { |
| 438 | const snoozedInput = { |
| 439 | threads: [ |
| 440 | makeThread({ id: ThreadId.make("active"), title: "Active" }), |
| 441 | makeThread({ |
| 442 | id: ThreadId.make("pinned-snoozed"), |
| 443 | title: "Pinned and snoozed", |
| 444 | pinnedAt: "2026-06-01T12:00:00.000Z", |
| 445 | snoozedUntil: "2026-06-03T09:00:00.000Z", |
| 446 | snoozedAt: "2026-06-01T11:00:00.000Z", |
| 447 | }), |
| 448 | ], |
| 449 | environmentId: null, |
| 450 | searchQuery: "", |
| 451 | }; |
| 452 | |
| 453 | const whileSnoozed = buildThreadListV2Items({ ...snoozedInput, now: NOW }); |
| 454 | expect(whileSnoozed.items.map((item) => item.thread.id)).toEqual(["active"]); |
| 455 | expect(whileSnoozed.snoozedCount).toBe(1); |
| 456 | |
| 457 | const afterWake = buildThreadListV2Items({ ...snoozedInput, now: "2026-06-03T10:00:00.000Z" }); |
| 458 | expect(afterWake.items.map((item) => item.thread.id)).toEqual(["pinned-snoozed", "active"]); |
| 459 | expect(afterWake.items[0]?.pinned).toBe(true); |
| 460 | expect(afterWake.snoozedCount).toBe(0); |
| 461 | }); |
| 462 | |
| 463 | it("builds snoozed rows between active and settled when the shelf is expanded", () => { |
| 464 | const layout = buildThreadListV2Items({ |
| 465 | threads: [ |
| 466 | makeThread({ id: ThreadId.make("active"), title: "Active" }), |
| 467 | makeThread({ |
| 468 | id: ThreadId.make("settled"), |
| 469 | title: "Settled", |
| 470 | settledOverride: "settled", |
| 471 | settledAt: NOW, |
| 472 | }), |
| 473 | makeThread({ |
| 474 | id: ThreadId.make("later"), |
| 475 | title: "Wakes later", |
| 476 | snoozedUntil: "2026-06-03T09:00:00.000Z", |
| 477 | snoozedAt: "2026-06-01T12:00:00.000Z", |
| 478 | }), |
| 479 | makeThread({ |
| 480 | id: ThreadId.make("sooner"), |
| 481 | title: "Wakes sooner", |
| 482 | snoozedUntil: "2026-06-02T09:00:00.000Z", |
| 483 | snoozedAt: "2026-06-01T12:00:00.000Z", |
| 484 | }), |
| 485 | ], |
| 486 | environmentId: null, |
| 487 | searchQuery: "", |
| 488 | now: NOW, |
| 489 | snoozedShelfExpanded: true, |
| 490 | }); |
| 491 | |
| 492 | expect(layout.items.map((item) => item.thread.id)).toEqual([ |
| 493 | "active", |
| 494 | "sooner", |
| 495 | "later", |
| 496 | "settled", |
| 497 | ]); |
| 498 | expect(layout.items.map((item) => item.snoozed)).toEqual([false, true, true, false]); |
| 499 | expect(layout.snoozedShelfHeaderIndex).toBe(1); |
| 500 | expect(layout.snoozedCount).toBe(2); |
| 501 | }); |
| 502 | |
| 503 | it("keeps the selected thread on a collapsed shelf", () => { |
| 504 | const layout = buildThreadListV2Items({ |
| 505 | threads: [ |
| 506 | makeThread({ |
| 507 | id: ThreadId.make("open"), |
| 508 | title: "Open", |
| 509 | snoozedUntil: "2026-06-03T09:00:00.000Z", |
| 510 | snoozedAt: "2026-06-01T12:00:00.000Z", |
| 511 | }), |
| 512 | makeThread({ |
| 513 | id: ThreadId.make("other"), |
| 514 | title: "Other", |
| 515 | snoozedUntil: "2026-06-03T10:00:00.000Z", |
| 516 | snoozedAt: "2026-06-01T12:00:00.000Z", |
| 517 | }), |
| 518 | ], |
| 519 | environmentId: null, |
| 520 | searchQuery: "", |
| 521 | now: NOW, |
| 522 | selectedThreadKey: `${environmentId}:open`, |
| 523 | }); |
| 524 | |
| 525 | expect(layout.items.map((item) => item.thread.id)).toEqual(["open"]); |
| 526 | expect(layout.items[0]?.snoozed).toBe(true); |
| 527 | expect(layout.snoozedCount).toBe(2); |
| 528 | }); |
| 529 | |
| 530 | it("keeps snoozed threads visible on environments without the snooze capability", () => { |
| 531 | const layout = buildThreadListV2Items({ |
| 532 | threads: [ |
| 533 | makeThread({ |
| 534 | id: ThreadId.make("snoozed"), |
| 535 | title: "Snoozed", |
| 536 | snoozedUntil: "2026-06-03T09:00:00.000Z", |
| 537 | snoozedAt: "2026-06-01T12:00:00.000Z", |
| 538 | }), |
| 539 | ], |
| 540 | environmentId: null, |
| 541 | searchQuery: "", |
| 542 | snoozeEnvironmentIds: new Set(), |
| 543 | now: NOW, |
| 544 | }); |
| 545 | |
| 546 | expect(layout.items.map((item) => item.thread.id)).toEqual(["snoozed"]); |
| 547 | expect(layout.snoozedCount).toBe(0); |
| 548 | }); |
| 549 | |
| 550 | it("partitions settled threads into a slim shelf", () => { |
| 551 | const layout = buildThreadListV2Items({ |
| 552 | threads: [ |
| 553 | makeThread({ id: ThreadId.make("active"), title: "Active" }), |
| 554 | makeThread({ |
| 555 | id: ThreadId.make("settled"), |
| 556 | title: "Settled", |
| 557 | settledOverride: "settled", |
| 558 | settledAt: NOW, |
| 559 | }), |
| 560 | makeThread({ |
| 561 | id: ThreadId.make("settled-2"), |
| 562 | title: "Settled 2", |
| 563 | settledOverride: "settled", |
| 564 | settledAt: NOW, |
| 565 | }), |
| 566 | ], |
| 567 | environmentId: null, |
| 568 | searchQuery: "", |
| 569 | now: NOW, |
| 570 | }); |
| 571 | |
| 572 | expect(layout.items.map((item) => [item.thread.id, item.variant])).toEqual([ |
| 573 | ["active", "card"], |
| 574 | ["settled", "slim"], |
| 575 | ["settled-2", "slim"], |
| 576 | ]); |
| 577 | expect(layout.items.map((item) => item.isLast)).toEqual([false, false, true]); |
| 578 | expect(layout.settledCount).toBe(2); |
| 579 | expect(layout.settledShelfHeaderIndex).toBe(1); |
| 580 | }); |
| 581 | |
| 582 | it("keeps settled threads in the tail and filters by search query", () => { |
| 583 | const { items } = buildThreadListV2Items({ |
| 584 | threads: [ |
| 585 | makeThread({ id: ThreadId.make("match"), title: "Fix login bug" }), |
| 586 | makeThread({ id: ThreadId.make("miss"), title: "Greeting" }), |
| 587 | makeThread({ |
| 588 | id: ThreadId.make("settled"), |
| 589 | title: "Fix login again", |
| 590 | settledOverride: "settled", |
| 591 | settledAt: NOW, |
| 592 | }), |
| 593 | ], |
| 594 | environmentId: null, |
| 595 | searchQuery: "login", |
| 596 | now: NOW, |
| 597 | }); |
| 598 | |
| 599 | expect(items.map((item) => [item.thread.id, item.variant])).toEqual([ |
| 600 | ["match", "card"], |
| 601 | ["settled", "slim"], |
| 602 | ]); |
| 603 | }); |
| 604 | |
| 605 | it("forgives a typo even when a hidden thread matches the query exactly", () => { |
| 606 | const { items } = buildThreadListV2Items({ |
| 607 | threads: [ |
| 608 | makeThread({ id: ThreadId.make("visible"), title: "Restore drag handles" }), |
| 609 | makeThread({ id: ThreadId.make("archived"), title: "Drga notes", archivedAt: NOW }), |
| 610 | ], |
| 611 | environmentId: null, |
| 612 | searchQuery: "drga", |
| 613 | now: NOW, |
| 614 | }); |
| 615 | |
| 616 | expect(items.map((item) => item.thread.id)).toEqual(["visible"]); |
| 617 | }); |
| 618 | |
| 619 | it("scopes the flat list to every environment member of a logical project", () => { |
| 620 | const remoteEnvironmentId = EnvironmentId.make("environment-remote"); |
| 621 | const projectId = ProjectId.make("project-1"); |
| 622 | const { items } = buildThreadListV2Items({ |
| 623 | threads: [ |
| 624 | makeThread({ id: ThreadId.make("local"), projectId, title: "Local" }), |
| 625 | makeThread({ |
| 626 | environmentId: remoteEnvironmentId, |
| 627 | id: ThreadId.make("remote"), |
| 628 | projectId, |
| 629 | title: "Remote", |
| 630 | }), |
| 631 | ], |
| 632 | environmentId: null, |
| 633 | projectRefs: [ |
| 634 | { environmentId, projectId }, |
| 635 | { environmentId: remoteEnvironmentId, projectId }, |
| 636 | ], |
| 637 | searchQuery: "", |
| 638 | now: NOW, |
| 639 | }); |
| 640 | |
| 641 | expect(items.map((item) => item.thread.id)).toEqual(["local", "remote"]); |
| 642 | }); |
| 643 | }); |
| 644 | |
| 645 | describe("buildThreadListV2Items settled paging", () => { |
| 646 | it("caps the settled tail at settledLimit and reports the hidden count", () => { |
| 647 | const threads = [ |
| 648 | makeThread({ id: ThreadId.make("active"), title: "Active" }), |
| 649 | ...Array.from({ length: 4 }, (_, index) => |
| 650 | makeThread({ |
| 651 | id: ThreadId.make(`settled-${index}`), |
| 652 | title: `Settled ${index}`, |
| 653 | settledOverride: "settled", |
| 654 | settledAt: `2026-06-01T0${index}:10:00.000Z`, |
| 655 | latestUserMessageAt: `2026-06-01T0${index}:00:00.000Z`, |
| 656 | latestRun: { |
| 657 | runId: RunId.make(`run-${index}`), |
| 658 | status: "completed", |
| 659 | requestedAt: `2026-06-01T0${index}:00:00.000Z`, |
| 660 | startedAt: `2026-06-01T0${index}:00:00.000Z`, |
| 661 | completedAt: `2026-06-01T0${index}:10:00.000Z`, |
| 662 | assistantMessageId: null, |
| 663 | }, |
| 664 | }), |
| 665 | ), |
| 666 | ]; |
| 667 | |
| 668 | const layout = buildThreadListV2Items({ |
| 669 | threads, |
| 670 | environmentId: null, |
| 671 | searchQuery: "", |
| 672 | settledLimit: 2, |
| 673 | now: NOW, |
| 674 | }); |
| 675 | |
| 676 | expect(layout.hiddenSettledCount).toBe(2); |
| 677 | expect(layout.items.filter((item) => item.variant === "slim")).toHaveLength(2); |
| 678 | expect(layout.items.map((item) => item.thread.id)).toEqual([ |
| 679 | "active", |
| 680 | "settled-3", |
| 681 | "settled-2", |
| 682 | ]); |
| 683 | }); |
| 684 | |
| 685 | it("lists only loaded settled rows and counts the threads the shell left out", () => { |
| 686 | const settled = (index: number) => |
| 687 | makeThread({ |
| 688 | id: ThreadId.make(`settled-${index}`), |
| 689 | title: `Settled ${index}`, |
| 690 | settledOverride: "settled", |
| 691 | settledAt: `2026-06-0${index}T00:00:00.000Z`, |
| 692 | }); |
| 693 | const threads = [settled(1), settled(2), settled(3)]; |
| 694 | const layout = buildThreadListV2Items({ |
| 695 | threads, |
| 696 | environmentId: null, |
| 697 | searchQuery: "", |
| 698 | settledLimit: 10, |
| 699 | now: NOW, |
| 700 | // settled-1 came back mid-session, below the loaded range; the route keeps it visible. |
| 701 | isListedSettled: (thread) => thread.id !== "settled-1", |
| 702 | unloadedSettledCount: 40, |
| 703 | selectedThreadKey: `${threads[0]!.environmentId}:settled-1`, |
| 704 | }); |
| 705 | expect(layout.items.map((item) => item.thread.id)).toEqual([ |
| 706 | "settled-3", |
| 707 | "settled-2", |
| 708 | "settled-1", |
| 709 | ]); |
| 710 | expect(layout.settledCount).toBe(43); |
| 711 | expect(layout.hiddenSettledCount).toBe(40); |
| 712 | |
| 713 | // A scope whose settled threads are all older still shows its Settled header. |
| 714 | const unloadedOnly = buildThreadListV2Items({ |
| 715 | threads: [], |
| 716 | environmentId: null, |
| 717 | searchQuery: "", |
| 718 | settledLimit: 10, |
| 719 | now: NOW, |
| 720 | unloadedSettledCount: 5, |
| 721 | }); |
| 722 | expect(unloadedOnly.settledCount).toBe(5); |
| 723 | expect(unloadedOnly.settledShelfHeaderIndex).toBe(0); |
| 724 | }); |
| 725 | }); |
| 726 | |
| 727 | describe("buildThreadListV2Items settled sort reuse", () => { |
| 728 | const otherEnvironmentId = EnvironmentId.make("environment-2"); |
| 729 | |
| 730 | function makeSettled( |
| 731 | id: string, |
| 732 | settledAt: string, |
| 733 | overrides: Partial<EnvironmentThreadShell> = {}, |
| 734 | ): EnvironmentThreadShell { |
| 735 | return makeThread({ |
| 736 | id: ThreadId.make(id), |
| 737 | title: id, |
| 738 | settledOverride: "settled", |
| 739 | settledAt, |
| 740 | ...overrides, |
| 741 | }); |
| 742 | } |
| 743 | |
| 744 | function settledOrder( |
| 745 | threads: ReadonlyArray<EnvironmentThreadShell>, |
| 746 | scope: EnvironmentId | null = null, |
| 747 | ) { |
| 748 | return buildThreadListV2Items({ |
| 749 | threads, |
| 750 | environmentId: scope, |
| 751 | searchQuery: "", |
| 752 | now: NOW, |
| 753 | }) |
| 754 | .items.filter((item) => item.variant === "slim") |
| 755 | .map((item) => `${item.thread.environmentId}:${item.thread.id}`); |
| 756 | } |
| 757 | |
| 758 | const older = makeSettled("older", "2026-06-01T01:00:00.000Z"); |
| 759 | const newer = makeSettled("newer", "2026-06-01T03:00:00.000Z"); |
| 760 | const middle = makeSettled("middle", "2026-06-01T02:00:00.000Z"); |
| 761 | |
| 762 | it("re-sorts when a settled row is replaced, added, or removed", () => { |
| 763 | expect(settledOrder([older, newer])).toEqual([ |
| 764 | `${environmentId}:newer`, |
| 765 | `${environmentId}:older`, |
| 766 | ]); |
| 767 | const resettledOlder = makeSettled("older", "2026-06-01T04:00:00.000Z"); |
| 768 | expect(settledOrder([resettledOlder, newer])).toEqual([ |
| 769 | `${environmentId}:older`, |
| 770 | `${environmentId}:newer`, |
| 771 | ]); |
| 772 | expect(settledOrder([resettledOlder, newer, middle])).toEqual([ |
| 773 | `${environmentId}:older`, |
| 774 | `${environmentId}:newer`, |
| 775 | `${environmentId}:middle`, |
| 776 | ]); |
| 777 | expect(settledOrder([newer, middle])).toEqual([ |
| 778 | `${environmentId}:newer`, |
| 779 | `${environmentId}:middle`, |
| 780 | ]); |
| 781 | }); |
| 782 | |
| 783 | it("recomputes when builds alternate between environments", () => { |
| 784 | const remoteOlder = makeSettled("remote-older", "2026-06-01T01:30:00.000Z", { |
| 785 | environmentId: otherEnvironmentId, |
| 786 | }); |
| 787 | const remoteNewer = makeSettled("remote-newer", "2026-06-01T05:00:00.000Z", { |
| 788 | environmentId: otherEnvironmentId, |
| 789 | }); |
| 790 | const threads = [older, newer, remoteOlder, remoteNewer]; |
| 791 | |
| 792 | for (let round = 0; round < 2; round += 1) { |
| 793 | expect(settledOrder(threads, environmentId)).toEqual([ |
| 794 | `${environmentId}:newer`, |
| 795 | `${environmentId}:older`, |
| 796 | ]); |
| 797 | expect(settledOrder(threads, otherEnvironmentId)).toEqual([ |
| 798 | `${otherEnvironmentId}:remote-newer`, |
| 799 | `${otherEnvironmentId}:remote-older`, |
| 800 | ]); |
| 801 | } |
| 802 | }); |
| 803 | }); |
| 804 | |
| 805 | function makePendingTask(id: string): PendingNewTask { |
| 806 | const creation = { |
| 807 | projectId: ProjectId.make("project-1"), |
| 808 | workspaceMode: "worktree" as const, |
| 809 | branch: null, |
| 810 | worktreePath: null, |
| 811 | }; |
| 812 | return { |
| 813 | kind: "pending", |
| 814 | key: `pending-task:${id}`, |
| 815 | environmentId, |
| 816 | projectId: creation.projectId, |
| 817 | projectTitle: undefined, |
| 818 | projectCwd: undefined, |
| 819 | branch: null, |
| 820 | title: id, |
| 821 | createdAt: NOW, |
| 822 | message: { |
| 823 | environmentId, |
| 824 | threadId: ThreadId.make(`thread-${id}`), |
| 825 | messageId: MessageId.make(id), |
| 826 | commandId: CommandId.make(`command-${id}`), |
| 827 | text: id, |
| 828 | attachments: [], |
| 829 | createdAt: NOW, |
| 830 | creation, |
| 831 | }, |
| 832 | creation, |
| 833 | }; |
| 834 | } |
| 835 | |
| 836 | describe("buildThreadListV2ListItems", () => { |
| 837 | const layout = buildThreadListV2Items({ |
| 838 | threads: [ |
| 839 | makeThread({ id: ThreadId.make("active"), title: "active" }), |
| 840 | makeThread({ |
| 841 | id: ThreadId.make("settled"), |
| 842 | title: "settled", |
| 843 | settledOverride: "settled", |
| 844 | settledAt: NOW, |
| 845 | }), |
| 846 | ], |
| 847 | environmentId: null, |
| 848 | searchQuery: "", |
| 849 | now: NOW, |
| 850 | }); |
| 851 | |
| 852 | it("leads the list with unsent drafts and queued tasks, preserving section dividers", () => { |
| 853 | const items = buildThreadListV2ListItems({ |
| 854 | items: layout.items, |
| 855 | pendingTasks: [ |
| 856 | { |
| 857 | kind: "draft", |
| 858 | key: "draft-task:new-task:unsent", |
| 859 | environmentId, |
| 860 | projectId: ProjectId.make("project-1"), |
| 861 | projectTitle: undefined, |
| 862 | projectCwd: undefined, |
| 863 | title: "unsent", |
| 864 | createdAt: NOW, |
| 865 | draftKey: "new-task:unsent", |
| 866 | draft: { |
| 867 | text: "unsent", |
| 868 | attachments: [], |
| 869 | project: { environmentId, projectId: ProjectId.make("project-1"), createdAt: NOW }, |
| 870 | }, |
| 871 | }, |
| 872 | makePendingTask("queued-1"), |
| 873 | makePendingTask("queued-2"), |
| 874 | ], |
| 875 | settledCount: layout.settledCount, |
| 876 | settledShelfHeaderIndex: layout.settledShelfHeaderIndex, |
| 877 | }); |
| 878 | |
| 879 | expect( |
| 880 | items.map((item) => |
| 881 | item.type === "v2-pending" |
| 882 | ? item.pendingTask.title |
| 883 | : item.type === "v2-thread" |
| 884 | ? item.item.thread.id |
| 885 | : item.type === "v2-snoozed-shelf" |
| 886 | ? "snoozed-shelf" |
| 887 | : "settled-shelf", |
| 888 | ), |
| 889 | ).toEqual(["unsent", "queued-1", "queued-2", "active", "settled-shelf", "settled"]); |
| 890 | expect( |
| 891 | items.flatMap((item) => (item.type === "v2-pending" ? [item.showPendingDivider] : [])), |
| 892 | ).toEqual([true, false, false]); |
| 893 | expect( |
| 894 | items.map((item) => |
| 895 | item.type === "v2-thread" || item.type === "v2-pending" ? item.showTrailingDivider : null, |
| 896 | ), |
| 897 | ).toEqual([true, true, true, false, null, false]); |
| 898 | }); |
| 899 | |
| 900 | it("places queued tasks above active rows and a collapsed snoozed shelf", () => { |
| 901 | const snoozedLayout = buildThreadListV2Items({ |
| 902 | threads: [ |
| 903 | makeThread({ id: ThreadId.make("active"), title: "active" }), |
| 904 | makeThread({ |
| 905 | id: ThreadId.make("snoozed"), |
| 906 | title: "snoozed", |
| 907 | snoozedUntil: "2026-06-03T09:00:00.000Z", |
| 908 | snoozedAt: "2026-06-01T12:00:00.000Z", |
| 909 | }), |
| 910 | makeThread({ |
| 911 | id: ThreadId.make("settled"), |
| 912 | title: "settled", |
| 913 | settledOverride: "settled", |
| 914 | settledAt: NOW, |
| 915 | }), |
| 916 | ], |
| 917 | environmentId: null, |
| 918 | searchQuery: "", |
| 919 | now: NOW, |
| 920 | }); |
| 921 | const items = buildThreadListV2ListItems({ |
| 922 | items: snoozedLayout.items, |
| 923 | pendingTasks: [makePendingTask("queued")], |
| 924 | snoozedCount: snoozedLayout.snoozedCount, |
| 925 | snoozedShelfExpanded: false, |
| 926 | snoozedShelfHeaderIndex: snoozedLayout.snoozedShelfHeaderIndex, |
| 927 | settledCount: snoozedLayout.settledCount, |
| 928 | settledShelfHeaderIndex: snoozedLayout.settledShelfHeaderIndex, |
| 929 | }); |
| 930 | |
| 931 | expect(items.map((item) => item.type)).toEqual([ |
| 932 | "v2-pending", |
| 933 | "v2-thread", |
| 934 | "v2-snoozed-shelf", |
| 935 | "v2-settled-shelf", |
| 936 | "v2-thread", |
| 937 | ]); |
| 938 | expect(threadJumpTarget(items, "thread.jump.1")?.id).toBe("active"); |
| 939 | expect(threadJumpTarget(items, "thread.jump.2")?.id).toBe("settled"); |
| 940 | expect(threadJumpTarget(items, "thread.jump.3")).toBeNull(); |
| 941 | }); |
| 942 | }); |
| 943 | |
| 944 | describe("pending mobile thread moves", () => { |
| 945 | function fixture(section: "active" | "pinned" = "active") { |
| 946 | const rows = ["a", "b", "c"].map((id, index) => |
| 947 | makeThread({ |
| 948 | id: ThreadId.make(id), |
| 949 | title: id === "a" ? "hidden" : "match", |
| 950 | createdAt: `2026-06-01T0${3 - index}:00:00.000Z`, |
| 951 | pinnedAt: section === "pinned" ? `2026-06-01T0${3 - index}:00:00.000Z` : null, |
| 952 | }), |
| 953 | ); |
| 954 | const ordered = getThreadListV2OrderedSection({ threads: rows, section, now: NOW }); |
| 955 | const orderedIds = ordered.map((row) => `${row.environmentId}:${row.id}`); |
| 956 | const movedId = orderedIds[2]!; |
| 957 | const assignments = planPinnedReorder({ |
| 958 | orderedIds: orderedIds.toSpliced(2, 1).toSpliced(1, 0, movedId), |
| 959 | keysById: new Map(orderedIds.map((id) => [id, null])), |
| 960 | movedId, |
| 961 | })!; |
| 962 | const pending = createPendingThreadOrder({ |
| 963 | section, |
| 964 | ordered, |
| 965 | movedId, |
| 966 | direction: "up", |
| 967 | assignments, |
| 968 | }); |
| 969 | const update = (current: EnvironmentThreadShell[], assignment: (typeof assignments)[number]) => |
| 970 | current.map((row) => |
| 971 | `${row.environmentId}:${row.id}` === assignment.id |
| 972 | ? { |
| 973 | ...row, |
| 974 | [section === "pinned" ? "pinOrderKey" : "activeOrderKey"]: assignment.orderKey, |
| 975 | } |
| 976 | : row, |
| 977 | ); |
| 978 | return { rows, assignments, pending, update }; |
| 979 | } |
| 980 | |
| 981 | function layout( |
| 982 | rows: EnvironmentThreadShell[], |
| 983 | pendingOrder: PendingThreadOrder | null, |
| 984 | searchQuery = "", |
| 985 | ) { |
| 986 | return buildThreadListV2Items({ |
| 987 | threads: rows, |
| 988 | pendingOrder, |
| 989 | environmentId: null, |
| 990 | searchQuery, |
| 991 | now: NOW, |
| 992 | }).items.map((item) => item.thread.id); |
| 993 | } |
| 994 | |
| 995 | it.each(["active", "pinned"] as const)( |
| 996 | "holds %s order through every intermediate key upsert", |
| 997 | (section) => { |
| 998 | const { rows, assignments, pending, update } = fixture(section); |
| 999 | let current = rows; |
| 1000 | let hold: PendingThreadOrder | null = pending; |
| 1001 | const desired = pending.orderedIds.map((id) => id.split(":")[1]); |
| 1002 | expect(layout(current, hold)).toEqual(desired); |
| 1003 | for (const assignment of assignments) { |
| 1004 | current = update(current, assignment); |
| 1005 | hold = reconcilePendingThreadOrder( |
| 1006 | hold!, |
| 1007 | getThreadListV2OrderedSection({ threads: current, section, now: NOW }), |
| 1008 | ); |
| 1009 | expect(hold).not.toBeNull(); |
| 1010 | expect(layout(current, hold)).toEqual(desired); |
| 1011 | } |
| 1012 | expect(reconcilePendingThreadOrder({ ...hold!, commandsComplete: true }, current)).toBeNull(); |
| 1013 | expect(layout(current, null)).toEqual(desired); |
| 1014 | }, |
| 1015 | ); |
| 1016 | |
| 1017 | it("keeps the action guard pending when receipts precede canonical shells", () => { |
| 1018 | const { rows, assignments, pending, update } = fixture(); |
| 1019 | let hold: PendingThreadOrder | null = { ...pending, commandsComplete: true }; |
| 1020 | let current = rows; |
| 1021 | expect(reconcilePendingThreadOrder(hold, current)).toBe(hold); |
| 1022 | for (const [index, assignment] of assignments.entries()) { |
| 1023 | current = update(current, assignment); |
| 1024 | hold = reconcilePendingThreadOrder(hold!, current); |
| 1025 | expect(hold === null).toBe(index === assignments.length - 1); |
| 1026 | expect(layout(current, hold)).toEqual(["a", "c", "b"]); |
| 1027 | } |
| 1028 | }); |
| 1029 | |
| 1030 | it("releases for real section membership and foreign key changes", () => { |
| 1031 | const { rows, pending } = fixture(); |
| 1032 | expect(reconcilePendingThreadOrder(pending, rows.slice(1))).toBeNull(); |
| 1033 | const newRow = makeThread({ id: ThreadId.make("new"), title: "new" }); |
| 1034 | expect(reconcilePendingThreadOrder(pending, [...rows, newRow])).toBeNull(); |
| 1035 | expect( |
| 1036 | reconcilePendingThreadOrder( |
| 1037 | pending, |
| 1038 | rows.map((row, index) => (index === 0 ? { ...row, activeOrderKey: "zz" } : row)), |
| 1039 | ), |
| 1040 | ).toBeNull(); |
| 1041 | const settled = rows.map((row, index) => |
| 1042 | index === 0 ? { ...row, settledOverride: "settled" as const } : row, |
| 1043 | ); |
| 1044 | expect(layout(settled, pending)).toEqual(layout(settled, null)); |
| 1045 | }); |
| 1046 | |
| 1047 | it("does not hide a concurrent return to a previously confirmed key", () => { |
| 1048 | const { rows, assignments, pending, update } = fixture(); |
| 1049 | const confirmed = reconcilePendingThreadOrder(pending, update(rows, assignments[0]!))!; |
| 1050 | expect(reconcilePendingThreadOrder(confirmed, rows)).toBeNull(); |
| 1051 | }); |
| 1052 | |
| 1053 | it("preserves the hold for activity but releases for a reopened sort anchor", () => { |
| 1054 | const { rows, pending } = fixture(); |
| 1055 | expect( |
| 1056 | reconcilePendingThreadOrder( |
| 1057 | pending, |
| 1058 | rows.map((row) => ({ ...row, updatedAt: NOW })), |
| 1059 | ), |
| 1060 | ).toBe(pending); |
| 1061 | expect( |
| 1062 | reconcilePendingThreadOrder( |
| 1063 | pending, |
| 1064 | rows.map((row, index) => (index === 0 ? { ...row, unsettledAt: NOW } : row)), |
| 1065 | ), |
| 1066 | ).toBeNull(); |
| 1067 | }); |
| 1068 | }); |
| 1069 | |
| 1070 | describe("mobile move availability", () => { |
| 1071 | const oldEnvironment = EnvironmentId.make("older-server"); |
| 1072 | function rows(section: "active" | "pinned", keys: readonly (string | null)[]) { |
| 1073 | return keys.map((key, index) => |
| 1074 | makeThread({ |
| 1075 | id: ThreadId.make(`move-${index}`), |
| 1076 | title: `Move ${index}`, |
| 1077 | environmentId: index === 1 ? oldEnvironment : environmentId, |
| 1078 | activeOrderKey: section === "active" ? key : null, |
| 1079 | pinOrderKey: section === "pinned" ? key : null, |
| 1080 | pinnedAt: section === "pinned" ? NOW : null, |
| 1081 | }), |
| 1082 | ); |
| 1083 | } |
| 1084 | |
| 1085 | it.each(["active", "pinned"] as const)( |
| 1086 | "keeps unsupported keyed %s neighbors as usable anchors", |
| 1087 | (section) => { |
| 1088 | const ordered = rows(section, ["bb", "dd", "ff"]); |
| 1089 | const plan = createThreadMovePlanner({ |
| 1090 | ordered, |
| 1091 | section, |
| 1092 | reorderableEnvironmentIds: new Set([environmentId]), |
| 1093 | }); |
| 1094 | const assignments = plan(`${environmentId}:move-0`, "down"); |
| 1095 | expect(assignments).toHaveLength(1); |
| 1096 | expect(assignments![0]!.id).toBe(`${environmentId}:move-0`); |
| 1097 | expect(assignments![0]!.orderKey > "dd").toBe(true); |
| 1098 | expect(assignments![0]!.orderKey < "ff").toBe(true); |
| 1099 | expect(plan(`${oldEnvironment}:move-1`, "up")).toBeNull(); |
| 1100 | expect(plan(`${environmentId}:move-0`, "up")).toBeNull(); |
| 1101 | }, |
| 1102 | ); |
| 1103 | |
| 1104 | it.each(["active", "pinned"] as const)( |
| 1105 | "disables %s moves requiring unsupported keyless materialization", |
| 1106 | (section) => { |
| 1107 | const ordered = rows(section, [null, null, null]); |
| 1108 | const plan = createThreadMovePlanner({ |
| 1109 | ordered, |
| 1110 | section, |
| 1111 | reorderableEnvironmentIds: new Set([environmentId]), |
| 1112 | }); |
| 1113 | expect(plan(`${environmentId}:move-0`, "down")).toBeNull(); |
| 1114 | expect(plan(`${environmentId}:move-2`, "up")).toBeNull(); |
| 1115 | const supported = createThreadMovePlanner({ |
| 1116 | ordered, |
| 1117 | section, |
| 1118 | reorderableEnvironmentIds: new Set([environmentId, oldEnvironment]), |
| 1119 | }); |
| 1120 | expect(supported(`${environmentId}:move-0`, "down")).toHaveLength(3); |
| 1121 | }, |
| 1122 | ); |
| 1123 | |
| 1124 | it.each(["active", "pinned"] as const)( |
| 1125 | "reserves snoozed %s keys when moving visible rows", |
| 1126 | (section) => { |
| 1127 | const ordered = rows(section, ["bb", "dd", "ff"]); |
| 1128 | const input = { ordered, section, reorderableEnvironmentIds: new Set([environmentId]) }; |
| 1129 | const collision = createThreadMovePlanner(input)(`${environmentId}:move-0`, "down")![0]! |
| 1130 | .orderKey; |
| 1131 | const hidden = { |
| 1132 | ...ordered[0]!, |
| 1133 | id: ThreadId.make("snoozed"), |
| 1134 | snoozedAt: NOW, |
| 1135 | snoozedUntil: "2099-01-01T00:00:00.000Z", |
| 1136 | pinOrderKey: section === "pinned" ? collision : null, |
| 1137 | activeOrderKey: section === "active" ? collision : null, |
| 1138 | }; |
| 1139 | const assignments = createThreadMovePlanner({ ...input, allThreads: [...ordered, hidden] })( |
| 1140 | `${environmentId}:move-0`, |
| 1141 | "down", |
| 1142 | ); |
| 1143 | expect(assignments).toHaveLength(1); |
| 1144 | expect(assignments![0]!.orderKey).not.toBe(collision); |
| 1145 | expect(assignments![0]!.orderKey > "dd" && assignments![0]!.orderKey < "ff").toBe(true); |
| 1146 | }, |
| 1147 | ); |
| 1148 | }); |
| 1149 | |
| 1150 | describe("thread drag destinations", () => { |
| 1151 | it("moves across multiple rows while keeping hidden anchors in place", () => { |
| 1152 | expect( |
| 1153 | threadOrderAfterMove(["a", "hidden", "b", "c"], "c", { |
| 1154 | targetId: "a", |
| 1155 | placement: "before", |
| 1156 | }), |
| 1157 | ).toEqual(["c", "a", "hidden", "b"]); |
| 1158 | expect( |
| 1159 | threadOrderAfterMove(["a", "hidden", "b", "c"], "a", { |
| 1160 | targetId: "b", |
| 1161 | placement: "after", |
| 1162 | }), |
| 1163 | ).toEqual(["hidden", "b", "a", "c"]); |
| 1164 | }); |
| 1165 | |
| 1166 | it.each(["active", "pinned"] as const)( |
| 1167 | "persists a dropped %s row and holds its order until confirmed", |
| 1168 | (section) => { |
| 1169 | const ordered = ["a", "b", "c", "d"].map((id) => |
| 1170 | makeThread({ |
| 1171 | id: ThreadId.make(id), |
| 1172 | title: id, |
| 1173 | pinnedAt: section === "pinned" ? NOW : null, |
| 1174 | }), |
| 1175 | ); |
| 1176 | const ids = ordered.map((row) => `${row.environmentId}:${row.id}`); |
| 1177 | const direction = { targetId: ids[0]!, placement: "before" as const }; |
| 1178 | const assignments = createThreadMovePlanner({ |
| 1179 | ordered, |
| 1180 | section, |
| 1181 | reorderableEnvironmentIds: new Set([environmentId]), |
| 1182 | })(ids[3]!, direction)!; |
| 1183 | const pending = createPendingThreadOrder({ |
| 1184 | section, |
| 1185 | ordered, |
| 1186 | movedId: ids[3]!, |
| 1187 | direction, |
| 1188 | assignments, |
| 1189 | }); |
| 1190 | expect(pending.orderedIds).toEqual([ids[3], ids[0], ids[1], ids[2]]); |
| 1191 | const confirmed = ordered.map((row) => ({ |
| 1192 | ...row, |
| 1193 | [section === "pinned" ? "pinOrderKey" : "activeOrderKey"]: assignments.find( |
| 1194 | (a) => a.id === `${row.environmentId}:${row.id}`, |
| 1195 | )!.orderKey, |
| 1196 | })); |
| 1197 | expect( |
| 1198 | getThreadListV2OrderedSection({ threads: confirmed, section, now: NOW }).map( |
| 1199 | (row) => `${row.environmentId}:${row.id}`, |
| 1200 | ), |
| 1201 | ).toEqual(pending.orderedIds); |
| 1202 | expect( |
| 1203 | reconcilePendingThreadOrder({ ...pending, commandsComplete: true }, confirmed), |
| 1204 | ).toBeNull(); |
| 1205 | }, |
| 1206 | ); |
| 1207 | |
| 1208 | it("refuses a drop that would need to rewrite an old server's keyless row", () => { |
| 1209 | const old = EnvironmentId.make("old-server"); |
| 1210 | const ordered = [environmentId, old, environmentId].map((env, index) => |
| 1211 | makeThread({ |
| 1212 | id: ThreadId.make(String(index)), |
| 1213 | title: String(index), |
| 1214 | environmentId: env, |
| 1215 | }), |
| 1216 | ); |
| 1217 | expect( |
| 1218 | createThreadMovePlanner({ |
| 1219 | ordered, |
| 1220 | section: "active", |
| 1221 | reorderableEnvironmentIds: new Set([environmentId]), |
| 1222 | })(`${environmentId}:2`, { targetId: `${environmentId}:0`, placement: "before" }), |
| 1223 | ).toBeNull(); |
| 1224 | }); |
| 1225 | }); |
| 1226 | |
| 1227 | it("allows a long drop past an old server even when both adjacent moves fail", () => { |
| 1228 | const old = EnvironmentId.make("old-server"); |
| 1229 | const ordered = [ |
| 1230 | makeThread({ id: ThreadId.make("a"), title: "a" }), |
| 1231 | makeThread({ id: ThreadId.make("b"), title: "b", environmentId: old }), |
| 1232 | makeThread({ id: ThreadId.make("c"), title: "c", activeOrderKey: "h" }), |
| 1233 | makeThread({ id: ThreadId.make("d"), title: "d", activeOrderKey: "p" }), |
| 1234 | ]; |
| 1235 | const planner = createThreadMovePlanner({ |
| 1236 | ordered, |
| 1237 | section: "active", |
| 1238 | reorderableEnvironmentIds: new Set([environmentId]), |
| 1239 | }); |
| 1240 | const movedId = `${environmentId}:a`; |
| 1241 | expect(planner(movedId, "up")).toBeNull(); |
| 1242 | expect(planner(movedId, "down")).toBeNull(); |
| 1243 | const assignments = planner(movedId, { targetId: `${environmentId}:d`, placement: "after" }); |
| 1244 | expect(assignments).toHaveLength(1); |
| 1245 | expect(assignments![0]!.id).toBe(movedId); |
| 1246 | expect(assignments![0]!.orderKey > "p").toBe(true); |
| 1247 | }); |
| 1248 | |
| 1249 | describe("cross-section thread drops", () => { |
| 1250 | it.each(["pinned", "active"] as const)("inserts into an empty %s section", (section) => { |
| 1251 | const thread = makeThread({ id: ThreadId.make("source"), title: "source" }); |
| 1252 | const id = `${thread.environmentId}:${thread.id}`; |
| 1253 | const destination = { section, targetId: null, placement: "before" as const }; |
| 1254 | expect(threadOrderAfterMove([], id, destination)).toEqual([id]); |
| 1255 | const plan = createThreadMovePlanner({ |
| 1256 | ordered: [], |
| 1257 | allThreads: [thread], |
| 1258 | section, |
| 1259 | reorderableEnvironmentIds: new Set([environmentId]), |
| 1260 | })(id, destination); |
| 1261 | expect(plan).toHaveLength(1); |
| 1262 | expect(plan![0]!.id).toBe(id); |
| 1263 | }); |
| 1264 | it("places an incoming row between existing anchors without rewriting them", () => { |
| 1265 | const a = makeThread({ id: ThreadId.make("a"), title: "a", pinOrderKey: "h" }); |
| 1266 | const b = makeThread({ id: ThreadId.make("b"), title: "b", pinOrderKey: "z" }); |
| 1267 | const source = makeThread({ id: ThreadId.make("source"), title: "source" }); |
| 1268 | const id = `${environmentId}:source`; |
| 1269 | const destination = { |
| 1270 | section: "pinned" as const, |
| 1271 | targetId: `${environmentId}:b`, |
| 1272 | placement: "before" as const, |
| 1273 | }; |
| 1274 | const plan = createThreadMovePlanner({ |
| 1275 | ordered: [a, b], |
| 1276 | allThreads: [a, b, source], |
| 1277 | section: "pinned", |
| 1278 | reorderableEnvironmentIds: new Set([environmentId]), |
| 1279 | })(id, destination); |
| 1280 | expect(plan).toHaveLength(1); |
| 1281 | expect(plan![0]!.orderKey > "h" && plan![0]!.orderKey < "z").toBe(true); |
| 1282 | expect( |
| 1283 | threadOrderAfterMove([`${environmentId}:a`, `${environmentId}:b`], id, destination), |
| 1284 | ).toEqual([`${environmentId}:a`, id, `${environmentId}:b`]); |
| 1285 | }); |
| 1286 | it("rejects removed targets and unsupported incoming sources", () => { |
| 1287 | expect( |
| 1288 | threadOrderAfterMove(["a"], "source", { |
| 1289 | section: "active", |
| 1290 | targetId: "gone", |
| 1291 | placement: "before", |
| 1292 | }), |
| 1293 | ).toBeNull(); |
| 1294 | const source = makeThread({ id: ThreadId.make("source"), title: "source" }); |
| 1295 | expect( |
| 1296 | createThreadMovePlanner({ |
| 1297 | ordered: [], |
| 1298 | allThreads: [source], |
| 1299 | section: "active", |
| 1300 | reorderableEnvironmentIds: new Set(), |
| 1301 | })(`${environmentId}:source`, { section: "active", targetId: null, placement: "before" }), |
| 1302 | ).toBeNull(); |
| 1303 | }); |
| 1304 | it("clears pinning, settlement and snooze when returning a parked thread to Active", () => { |
| 1305 | const thread = makeThread({ |
| 1306 | id: ThreadId.make("parked"), |
| 1307 | title: "parked", |
| 1308 | pinnedAt: NOW, |
| 1309 | settledOverride: "settled", |
| 1310 | snoozedAt: NOW, |
| 1311 | snoozedUntil: "2099-01-01T00:00:00.000Z", |
| 1312 | }); |
| 1313 | expect(threadDropLifecycle(thread, "active", NOW)).toEqual({ |
| 1314 | pin: false, |
| 1315 | unpin: true, |
| 1316 | unsettle: true, |
| 1317 | unsnooze: true, |
| 1318 | }); |
| 1319 | expect(threadDropLifecycle(thread, "pinned", NOW)).toEqual({ |
| 1320 | pin: true, |
| 1321 | unpin: false, |
| 1322 | unsettle: false, |
| 1323 | unsnooze: false, |
| 1324 | }); |
| 1325 | }); |
| 1326 | }); |
| 1327 | |
| 1328 | it("excludes subagents from navigation, search and ordering while retaining user forks", () => { |
| 1329 | const root = makeThread({ id: ThreadId.make("root"), title: "Root" }); |
| 1330 | const child = makeThread({ |
| 1331 | id: ThreadId.make("child"), |
| 1332 | title: "Child", |
| 1333 | lineage: { parentThreadId: root.id, rootThreadId: root.id, relationshipToParent: "subagent" }, |
| 1334 | }); |
| 1335 | const fork = makeThread({ |
| 1336 | id: ThreadId.make("fork"), |
| 1337 | title: "Fork", |
| 1338 | lineage: { parentThreadId: root.id, rootThreadId: root.id, relationshipToParent: "fork" }, |
| 1339 | }); |
| 1340 | const threads = [root, child, fork]; |
| 1341 | expect( |
| 1342 | buildThreadListV2Items({ threads, environmentId: null, searchQuery: "", now: NOW }).items.map( |
| 1343 | (item) => item.thread.id, |
| 1344 | ), |
| 1345 | ).toEqual([fork.id, root.id]); |
| 1346 | expect( |
| 1347 | buildThreadListV2Items({ threads, environmentId: null, searchQuery: "Child", now: NOW }).items, |
| 1348 | ).toEqual([]); |
| 1349 | expect( |
| 1350 | getThreadListV2OrderedSection({ threads, section: "active", now: NOW }).map( |
| 1351 | (thread) => thread.id, |
| 1352 | ), |
| 1353 | ).toEqual([fork.id, root.id]); |
| 1354 | }); |
| 1355 | |
| 1356 | const BASE_MS = Date.parse(NOW); |
| 1357 | const isoAt = (ms: number) => new Date(ms).toISOString(); |
| 1358 | const MINUTE_MS = 60_000; |
| 1359 | |
| 1360 | function buildTickList( |
| 1361 | threads: ReadonlyArray<EnvironmentThreadShell>, |
| 1362 | clockMs: number, |
| 1363 | pendingTasks: ReadonlyArray<PendingNewTask>, |
| 1364 | options?: { |
| 1365 | readonly snoozeEnvironmentIds?: ReadonlySet<EnvironmentId>; |
| 1366 | readonly queuedThreadKeys?: ReadonlySet<string>; |
| 1367 | readonly moveAvailability?: ReadonlyMap<string, ThreadMoveAvailability>; |
| 1368 | readonly shelfPreferencesLoading?: boolean; |
| 1369 | }, |
| 1370 | ): ThreadListV2ListItem[] { |
| 1371 | const now = isoAt(clockMs); |
| 1372 | const layout = buildThreadListV2Items({ |
| 1373 | threads, |
| 1374 | environmentId: null, |
| 1375 | searchQuery: "", |
| 1376 | now, |
| 1377 | snoozedShelfExpanded: true, |
| 1378 | }); |
| 1379 | return buildThreadListV2ListItems({ |
| 1380 | items: layout.items, |
| 1381 | pendingTasks, |
| 1382 | snoozedCount: layout.snoozedCount, |
| 1383 | snoozedShelfExpanded: true, |
| 1384 | snoozedShelfHeaderIndex: layout.snoozedShelfHeaderIndex, |
| 1385 | settledCount: layout.settledCount, |
| 1386 | settledShelfHeaderIndex: layout.settledShelfHeaderIndex, |
| 1387 | snoozeLabelNow: now, |
| 1388 | ...(options?.snoozeEnvironmentIds |
| 1389 | ? { snoozeEnvironmentIds: options.snoozeEnvironmentIds } |
| 1390 | : {}), |
| 1391 | ...(options?.queuedThreadKeys ? { queuedThreadKeys: options.queuedThreadKeys } : {}), |
| 1392 | ...(options?.moveAvailability ? { moveAvailability: options.moveAvailability } : {}), |
| 1393 | ...(options?.shelfPreferencesLoading !== undefined |
| 1394 | ? { shelfPreferencesLoading: options.shelfPreferencesLoading } |
| 1395 | : {}), |
| 1396 | }); |
| 1397 | } |
| 1398 | |
| 1399 | function itemsByThreadKey(items: ReadonlyArray<ThreadListV2ListItem>) { |
| 1400 | const byKey = new Map<string, ThreadListV2ListItem>(); |
| 1401 | for (const item of items) byKey.set(item.key, item); |
| 1402 | return byKey; |
| 1403 | } |
| 1404 | |
| 1405 | describe("threadListV2ListItemsAreEqual", () => { |
| 1406 | const thread = makeThread({ |
| 1407 | id: ThreadId.make("eq"), |
| 1408 | title: "eq", |
| 1409 | latestUserMessageAt: isoAt(BASE_MS - 5 * MINUTE_MS), |
| 1410 | }); |
| 1411 | const layout = buildThreadListV2Items({ |
| 1412 | threads: [thread], |
| 1413 | environmentId: null, |
| 1414 | searchQuery: "", |
| 1415 | now: NOW, |
| 1416 | }); |
| 1417 | const queued = makePendingTask("eq-queued"); |
| 1418 | const build = () => |
| 1419 | buildThreadListV2ListItems({ |
| 1420 | items: layout.items, |
| 1421 | pendingTasks: [queued], |
| 1422 | snoozeLabelNow: NOW, |
| 1423 | }); |
| 1424 | |
| 1425 | it("treats rebuilt wrappers over identical rows as equal", () => { |
| 1426 | const first = build(); |
| 1427 | const second = build(); |
| 1428 | expect(first.length).toBe(second.length); |
| 1429 | for (let index = 0; index < first.length; index += 1) { |
| 1430 | expect(first[index]).not.toBe(second[index]); |
| 1431 | expect(threadListV2ListItemsAreEqual(first[index]!, second[index]!)).toBe(true); |
| 1432 | } |
| 1433 | }); |
| 1434 | |
| 1435 | it("notices a changed wake countdown label", () => { |
| 1436 | const snoozedLayout = buildThreadListV2Items({ |
| 1437 | threads: [ |
| 1438 | makeThread({ |
| 1439 | id: ThreadId.make("wake"), |
| 1440 | title: "wake", |
| 1441 | snoozedAt: NOW, |
| 1442 | snoozedUntil: isoAt(BASE_MS + 61 * MINUTE_MS), |
| 1443 | }), |
| 1444 | ], |
| 1445 | environmentId: null, |
| 1446 | searchQuery: "", |
| 1447 | now: NOW, |
| 1448 | snoozedShelfExpanded: true, |
| 1449 | }); |
| 1450 | const earlier = buildThreadListV2ListItems({ |
| 1451 | items: snoozedLayout.items, |
| 1452 | pendingTasks: [], |
| 1453 | snoozedShelfExpanded: true, |
| 1454 | snoozeLabelNow: NOW, |
| 1455 | }); |
| 1456 | const later = buildThreadListV2ListItems({ |
| 1457 | items: snoozedLayout.items, |
| 1458 | pendingTasks: [], |
| 1459 | snoozedShelfExpanded: true, |
| 1460 | snoozeLabelNow: isoAt(BASE_MS + MINUTE_MS), |
| 1461 | }); |
| 1462 | const rows = (items: ThreadListV2ListItem[]) => { |
| 1463 | const row = items.find((item) => item.key === `v2-thread:${environmentId}:wake`); |
| 1464 | expect(row?.type).toBe("v2-thread"); |
| 1465 | if (row?.type !== "v2-thread") throw new Error("missing snoozed row"); |
| 1466 | return row; |
| 1467 | }; |
| 1468 | expect(rows(earlier).snoozeWakeLabelText).toBe("2h"); |
| 1469 | expect(rows(later).snoozeWakeLabelText).toBe("1h"); |
| 1470 | expect(threadListV2ListItemsAreEqual(rows(earlier), rows(later))).toBe(false); |
| 1471 | }); |
| 1472 | |
| 1473 | it("notices a trailing-divider flip caused by a neighbour change", () => { |
| 1474 | const threadA = makeThread({ id: ThreadId.make("flip-a"), title: "flip a" }); |
| 1475 | const threadB = makeThread({ id: ThreadId.make("flip-b"), title: "flip b" }); |
| 1476 | const bare = buildThreadListV2ListItems({ |
| 1477 | items: buildThreadListV2Items({ |
| 1478 | threads: [threadA, threadB], |
| 1479 | environmentId: null, |
| 1480 | searchQuery: "", |
| 1481 | now: NOW, |
| 1482 | }).items, |
| 1483 | pendingTasks: [], |
| 1484 | snoozeLabelNow: NOW, |
| 1485 | }); |
| 1486 | const settledB = makeThread({ |
| 1487 | id: ThreadId.make("flip-b"), |
| 1488 | title: "flip b", |
| 1489 | settledOverride: "settled", |
| 1490 | settledAt: NOW, |
| 1491 | }); |
| 1492 | const withSettled = buildThreadListV2ListItems({ |
| 1493 | items: buildThreadListV2Items({ |
| 1494 | threads: [threadA, settledB], |
| 1495 | environmentId: null, |
| 1496 | searchQuery: "", |
| 1497 | now: NOW, |
| 1498 | }).items, |
| 1499 | pendingTasks: [], |
| 1500 | settledCount: 1, |
| 1501 | settledShelfHeaderIndex: 1, |
| 1502 | snoozeLabelNow: NOW, |
| 1503 | }); |
| 1504 | const firstA = bare[0]!; |
| 1505 | const secondA = withSettled[0]!; |
| 1506 | expect(firstA.type === "v2-thread" && firstA.showTrailingDivider).toBe(true); |
| 1507 | expect(secondA.type === "v2-thread" && secondA.showTrailingDivider).toBe(false); |
| 1508 | expect(threadListV2ListItemsAreEqual(firstA, secondA)).toBe(false); |
| 1509 | }); |
| 1510 | }); |
| 1511 | |
| 1512 | describe("thread list Woke marker", () => { |
| 1513 | const wokeAt = isoAt(BASE_MS - MINUTE_MS); |
| 1514 | const thread = makeThread({ |
| 1515 | id: ThreadId.make("woke"), |
| 1516 | title: "Woke", |
| 1517 | latestUserMessageAt: isoAt(BASE_MS - 90 * MINUTE_MS), |
| 1518 | snoozedAt: isoAt(BASE_MS - 60 * MINUTE_MS), |
| 1519 | snoozedUntil: wokeAt, |
| 1520 | lastVisitedAt: null, |
| 1521 | }); |
| 1522 | |
| 1523 | it("keeps a timer wake through a plain visit until acknowledgement reaches its timestamp", () => { |
| 1524 | expect(resolveThreadListV2WokeAt(thread, { now: NOW })).toBe(wokeAt); |
| 1525 | expect( |
| 1526 | resolveThreadListV2WokeAt( |
| 1527 | { ...thread, lastVisitedAt: isoAt(BASE_MS - 30 * MINUTE_MS) }, |
| 1528 | { now: NOW }, |
| 1529 | ), |
| 1530 | ).toBe(wokeAt); |
| 1531 | expect( |
| 1532 | resolveThreadListV2WokeAt({ ...thread, lastVisitedAt: wokeAt }, { now: NOW }), |
| 1533 | ).toBeNull(); |
| 1534 | expect(resolveThreadListV2WokeAt({ ...thread, lastVisitedAt: "invalid" }, { now: NOW })).toBe( |
| 1535 | wokeAt, |
| 1536 | ); |
| 1537 | }); |
| 1538 | |
| 1539 | it("stays hidden while snoozed, after settling, and on servers without visited tracking", () => { |
| 1540 | expect(resolveThreadListV2WokeAt(thread, { now: isoAt(BASE_MS - 2 * MINUTE_MS) })).toBeNull(); |
| 1541 | expect( |
| 1542 | resolveThreadListV2WokeAt({ ...thread, settledOverride: "settled" }, { now: NOW }), |
| 1543 | ).toBeNull(); |
| 1544 | expect( |
| 1545 | resolveThreadListV2WokeAt({ ...thread, lastVisitedAt: undefined }, { now: NOW }), |
| 1546 | ).toBeNull(); |
| 1547 | expect(resolveThreadListV2WokeAt({ ...thread, snoozedUntil: null }, { now: NOW })).toBeNull(); |
| 1548 | }); |
| 1549 | |
| 1550 | it("keeps an early failure wake acknowledged after the scheduled time passes", () => { |
| 1551 | const completedAt = isoAt(BASE_MS - 2 * MINUTE_MS); |
| 1552 | const failed = makeThread({ |
| 1553 | ...thread, |
| 1554 | snoozedUntil: isoAt(BASE_MS + MINUTE_MS), |
| 1555 | runtime: { |
| 1556 | status: "failed", |
| 1557 | activeRunId: null, |
| 1558 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 1559 | providerName: "Codex", |
| 1560 | lastError: "Run failed", |
| 1561 | updatedAt: completedAt, |
| 1562 | }, |
| 1563 | latestRun: { |
| 1564 | runId: RunId.make("woke-run"), |
| 1565 | status: "failed", |
| 1566 | requestedAt: isoAt(BASE_MS - 3 * MINUTE_MS), |
| 1567 | startedAt: isoAt(BASE_MS - 3 * MINUTE_MS), |
| 1568 | completedAt, |
| 1569 | assistantMessageId: null, |
| 1570 | }, |
| 1571 | }); |
| 1572 | expect(resolveThreadListV2WokeAt(failed, { now: NOW })).toBe(completedAt); |
| 1573 | expect( |
| 1574 | resolveThreadListV2WokeAt( |
| 1575 | { ...failed, lastVisitedAt: completedAt }, |
| 1576 | { now: isoAt(BASE_MS + 2 * MINUTE_MS) }, |
| 1577 | ), |
| 1578 | ).toBeNull(); |
| 1579 | }); |
| 1580 | |
| 1581 | it("updates a recycled row at the wake boundary even without a new shell or minute tick", () => { |
| 1582 | const waking = { ...thread, snoozedUntil: isoAt(BASE_MS + 30_000) }; |
| 1583 | const layout = (now: string) => |
| 1584 | buildThreadListV2Items({ |
| 1585 | threads: [waking], |
| 1586 | environmentId: null, |
| 1587 | searchQuery: "", |
| 1588 | now, |
| 1589 | snoozeEnvironmentIds: new Set<EnvironmentId>(), |
| 1590 | }); |
| 1591 | const list = (now: string) => |
| 1592 | buildThreadListV2ListItems({ |
| 1593 | items: layout(now).items, |
| 1594 | pendingTasks: [], |
| 1595 | snoozeLabelNow: isoAt(BASE_MS), |
| 1596 | }); |
| 1597 | const before = list(NOW)[0]!; |
| 1598 | const after = list(isoAt(BASE_MS + 37_500))[0]!; |
| 1599 | expect(before.type === "v2-thread" && before.item.wokeAt).toBeNull(); |
| 1600 | expect(after.type === "v2-thread" && after.item.wokeAt).toBe(waking.snoozedUntil); |
| 1601 | expect(after.type === "v2-thread" && after.timeLabel).toBe(""); |
| 1602 | expect(threadListV2ListItemsAreEqual(before, after)).toBe(false); |
| 1603 | }); |
| 1604 | }); |
| 1605 | |
| 1606 | describe("buildThreadListV2ListItems row-state stamps", () => { |
| 1607 | const readyThread = makeThread({ |
| 1608 | id: ThreadId.make("stamp-ready"), |
| 1609 | title: "stamp ready", |
| 1610 | latestUserMessageAt: isoAt(BASE_MS - 5 * MINUTE_MS), |
| 1611 | }); |
| 1612 | const settledThread = makeThread({ |
| 1613 | id: ThreadId.make("stamp-settled"), |
| 1614 | title: "stamp settled", |
| 1615 | settledOverride: "settled", |
| 1616 | settledAt: isoAt(BASE_MS - 3 * 24 * 60 * MINUTE_MS), |
| 1617 | }); |
| 1618 | const allEnvironments = new Set<EnvironmentId>([environmentId]); |
| 1619 | |
| 1620 | it("ages a read row from its latest reply", () => { |
| 1621 | using _timers = { [Symbol.dispose]: () => vi.useRealTimers() }; |
| 1622 | vi.useFakeTimers({ now: BASE_MS }); |
| 1623 | const label = (latestUserMessageAt: string, completedAt: string) => { |
| 1624 | const thread = makeThread({ |
| 1625 | id: ThreadId.make("stamp-read"), |
| 1626 | title: "stamp read", |
| 1627 | latestUserMessageAt, |
| 1628 | lastVisitedAt: isoAt(BASE_MS), |
| 1629 | latestRun: { |
| 1630 | runId: RunId.make("stamp-read-run"), |
| 1631 | status: "completed", |
| 1632 | requestedAt: latestUserMessageAt, |
| 1633 | startedAt: latestUserMessageAt, |
| 1634 | completedAt, |
| 1635 | assistantMessageId: null, |
| 1636 | }, |
| 1637 | }); |
| 1638 | const row = buildTickList([thread], BASE_MS, [])[0]!; |
| 1639 | return row.type === "v2-thread" ? row.timeLabel : null; |
| 1640 | }; |
| 1641 | expect(label(isoAt(BASE_MS - 40 * MINUTE_MS), isoAt(BASE_MS - 2 * MINUTE_MS))).toBe("2m"); |
| 1642 | }); |
| 1643 | |
| 1644 | it("stamps queued outbox messages onto the matching row and notices removal", () => { |
| 1645 | const queued = buildTickList([readyThread, settledThread], BASE_MS, [], { |
| 1646 | queuedThreadKeys: new Set([`${environmentId}:stamp-ready`]), |
| 1647 | snoozeEnvironmentIds: allEnvironments, |
| 1648 | }); |
| 1649 | const plain = buildTickList([readyThread, settledThread], BASE_MS, [], { |
| 1650 | queuedThreadKeys: new Set<string>(), |
| 1651 | snoozeEnvironmentIds: allEnvironments, |
| 1652 | }); |
| 1653 | const byKey = (items: ThreadListV2ListItem[]) => itemsByThreadKey(items); |
| 1654 | const readyQueued = byKey(queued).get(`v2-thread:${environmentId}:stamp-ready`)!; |
| 1655 | const readyPlain = byKey(plain).get(`v2-thread:${environmentId}:stamp-ready`)!; |
| 1656 | const settledQueued = byKey(queued).get(`v2-thread:${environmentId}:stamp-settled`)!; |
| 1657 | const settledPlain = byKey(plain).get(`v2-thread:${environmentId}:stamp-settled`)!; |
| 1658 | expect(readyQueued.type === "v2-thread" && readyQueued.hasQueuedMessages).toBe(true); |
| 1659 | expect(readyPlain.type === "v2-thread" && readyPlain.hasQueuedMessages).toBe(false); |
| 1660 | expect(threadListV2ListItemsAreEqual(readyQueued, readyPlain)).toBe(false); |
| 1661 | expect(threadListV2ListItemsAreEqual(settledQueued, settledPlain)).toBe(true); |
| 1662 | }); |
| 1663 | |
| 1664 | it("notices move-availability changes on card rows without a shell update", () => { |
| 1665 | const permissive = new Map([ |
| 1666 | [`${environmentId}:stamp-ready`, { canMoveUp: true, canMoveDown: true }], |
| 1667 | [`${environmentId}:stamp-settled`, { canMoveUp: true, canMoveDown: true }], |
| 1668 | ]); |
| 1669 | const blocked = new Map([ |
| 1670 | [`${environmentId}:stamp-settled`, { canMoveUp: true, canMoveDown: true }], |
| 1671 | ]); |
| 1672 | const open = buildTickList([readyThread, settledThread], BASE_MS, [], { |
| 1673 | moveAvailability: permissive, |
| 1674 | snoozeEnvironmentIds: allEnvironments, |
| 1675 | }); |
| 1676 | const closed = buildTickList([readyThread, settledThread], BASE_MS, [], { |
| 1677 | moveAvailability: blocked, |
| 1678 | snoozeEnvironmentIds: allEnvironments, |
| 1679 | }); |
| 1680 | const readyOpen = itemsByThreadKey(open).get(`v2-thread:${environmentId}:stamp-ready`)!; |
| 1681 | const readyClosed = itemsByThreadKey(closed).get(`v2-thread:${environmentId}:stamp-ready`)!; |
| 1682 | expect(readyOpen.type === "v2-thread" && readyOpen.canMoveUp).toBe(true); |
| 1683 | expect(readyClosed.type === "v2-thread" && readyClosed.canMoveUp).toBe(false); |
| 1684 | expect(threadListV2ListItemsAreEqual(readyOpen, readyClosed)).toBe(false); |
| 1685 | const settledOpen = itemsByThreadKey(open).get(`v2-thread:${environmentId}:stamp-settled`)!; |
| 1686 | const settledClosed = itemsByThreadKey(closed).get(`v2-thread:${environmentId}:stamp-settled`)!; |
| 1687 | expect(settledOpen.type === "v2-thread" && settledOpen.canMoveUp).toBe(false); |
| 1688 | expect(threadListV2ListItemsAreEqual(settledOpen, settledClosed)).toBe(true); |
| 1689 | }); |
| 1690 | }); |
| 1691 | |
| 1692 | describe("Working section beta", () => { |
| 1693 | const running = { |
| 1694 | status: "running" as const, |
| 1695 | activeRunId: null, |
| 1696 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 1697 | providerName: "Codex", |
| 1698 | lastError: null, |
| 1699 | updatedAt: NOW, |
| 1700 | }; |
| 1701 | const finishedAt = (id: string, completedAt: string) => |
| 1702 | makeThread({ |
| 1703 | id: ThreadId.make(id), |
| 1704 | title: id, |
| 1705 | createdAt: "2026-06-01T00:00:00.000Z", |
| 1706 | latestRun: { |
| 1707 | runId: RunId.make(`run-${id}`), |
| 1708 | status: "completed", |
| 1709 | requestedAt: "2026-06-01T00:00:00.000Z", |
| 1710 | startedAt: "2026-06-01T00:00:00.000Z", |
| 1711 | completedAt, |
| 1712 | assistantMessageId: null, |
| 1713 | }, |
| 1714 | }); |
| 1715 | const threads = [ |
| 1716 | finishedAt("finished-early", "2026-06-01T01:00:00.000Z"), |
| 1717 | finishedAt("finished-late", "2026-06-01T03:00:00.000Z"), |
| 1718 | makeThread({ id: ThreadId.make("working"), title: "working", runtime: running }), |
| 1719 | makeThread({ |
| 1720 | id: ThreadId.make("asks-approval"), |
| 1721 | title: "asks-approval", |
| 1722 | createdAt: "2026-06-01T02:00:00.000Z", |
| 1723 | runtime: running, |
| 1724 | hasPendingApprovals: true, |
| 1725 | }), |
| 1726 | makeThread({ |
| 1727 | id: ThreadId.make("pinned-working"), |
| 1728 | title: "pinned-working", |
| 1729 | runtime: running, |
| 1730 | pinnedAt: "2026-06-01T00:00:00.000Z", |
| 1731 | }), |
| 1732 | ]; |
| 1733 | const build = (input: Partial<Parameters<typeof buildThreadListV2Items>[0]> = {}) => |
| 1734 | buildThreadListV2Items({ |
| 1735 | threads, |
| 1736 | environmentId: null, |
| 1737 | searchQuery: "", |
| 1738 | now: NOW, |
| 1739 | workingShelfEnabled: true, |
| 1740 | ...input, |
| 1741 | }); |
| 1742 | const ids = (layout: ReturnType<typeof buildThreadListV2Items>) => |
| 1743 | layout.items.map((item) => item.thread.id); |
| 1744 | |
| 1745 | it("folds unpinned working threads into a collapsed shelf", () => { |
| 1746 | const layout = build(); |
| 1747 | expect(ids(layout)).toEqual([ |
| 1748 | "pinned-working", |
| 1749 | "finished-late", |
| 1750 | "asks-approval", |
| 1751 | "finished-early", |
| 1752 | ]); |
| 1753 | expect(layout.workingCount).toBe(1); |
| 1754 | expect(layout.workingShelfHeaderIndex).toBe(4); |
| 1755 | |
| 1756 | const off = build({ workingShelfEnabled: false }); |
| 1757 | expect(ids(off)).toContain("working"); |
| 1758 | expect(off.workingCount).toBe(0); |
| 1759 | }); |
| 1760 | |
| 1761 | it("keeps an inactive row in Working during the shared grace and returns it on expiry", () => { |
| 1762 | vi.useFakeTimers(); |
| 1763 | vi.setSystemTime(new Date(NOW)); |
| 1764 | const tracker = createInboxReturnTracker(); |
| 1765 | const working = makeThread({ id: ThreadId.make("grace"), title: "grace", runtime: running }); |
| 1766 | const inactive = { ...working, runtime: null }; |
| 1767 | const render = () => |
| 1768 | build({ |
| 1769 | threads: [inactive], |
| 1770 | workingShelfExpanded: true, |
| 1771 | isWorking: tracker.isWorking, |
| 1772 | inboxReturnAt: tracker.returnedAt, |
| 1773 | }); |
| 1774 | try { |
| 1775 | tracker.observe([working]); |
| 1776 | tracker.observe([inactive]); |
| 1777 | expect(render().workingCount).toBe(1); |
| 1778 | expect(render().items[0]?.thread.runtime).toBeNull(); |
| 1779 | vi.advanceTimersByTime(2_000); |
| 1780 | expect(render().workingCount).toBe(0); |
| 1781 | expect(ids(render())).toEqual(["grace"]); |
| 1782 | expect(tracker.returnedAt(inactive)).toBe(Date.parse(NOW) + 2_000); |
| 1783 | } finally { |
| 1784 | tracker.observe(null); |
| 1785 | vi.useRealTimers(); |
| 1786 | } |
| 1787 | }); |
| 1788 | |
| 1789 | it("orders the inbox by the latest return this device observed", () => { |
| 1790 | const layout = build({ |
| 1791 | inboxReturnAt: (thread) => |
| 1792 | thread.id === "finished-early" ? Date.parse("2026-06-01T04:00:00.000Z") : undefined, |
| 1793 | }); |
| 1794 | expect(ids(layout).slice(1)).toEqual(["finished-early", "finished-late", "asks-approval"]); |
| 1795 | }); |
| 1796 | |
| 1797 | it("places the shelf after queued tasks and before snoozed and settled threads", () => { |
| 1798 | const layout = buildThreadListV2Items({ |
| 1799 | threads: [ |
| 1800 | makeThread({ id: ThreadId.make("active"), title: "active" }), |
| 1801 | makeThread({ id: ThreadId.make("working"), title: "working", runtime: running }), |
| 1802 | makeThread({ |
| 1803 | id: ThreadId.make("snoozed"), |
| 1804 | title: "snoozed", |
| 1805 | runtime: running, |
| 1806 | snoozedUntil: "2026-06-03T09:00:00.000Z", |
| 1807 | snoozedAt: "2026-06-01T12:00:00.000Z", |
| 1808 | }), |
| 1809 | makeThread({ |
| 1810 | id: ThreadId.make("settled"), |
| 1811 | title: "settled", |
| 1812 | settledOverride: "settled", |
| 1813 | settledAt: NOW, |
| 1814 | }), |
| 1815 | ], |
| 1816 | environmentId: null, |
| 1817 | searchQuery: "", |
| 1818 | now: NOW, |
| 1819 | workingShelfEnabled: true, |
| 1820 | workingShelfExpanded: true, |
| 1821 | snoozedShelfExpanded: true, |
| 1822 | }); |
| 1823 | const items = buildThreadListV2ListItems({ |
| 1824 | items: layout.items, |
| 1825 | pendingTasks: [makePendingTask("queued")], |
| 1826 | workingCount: layout.workingCount, |
| 1827 | workingShelfExpanded: true, |
| 1828 | workingShelfHeaderIndex: layout.workingShelfHeaderIndex, |
| 1829 | snoozedCount: layout.snoozedCount, |
| 1830 | snoozedShelfExpanded: true, |
| 1831 | snoozedShelfHeaderIndex: layout.snoozedShelfHeaderIndex, |
| 1832 | settledCount: layout.settledCount, |
| 1833 | settledShelfHeaderIndex: layout.settledShelfHeaderIndex, |
| 1834 | }); |
| 1835 | expect( |
| 1836 | items.map((item) => |
| 1837 | item.type === "v2-thread" |
| 1838 | ? item.item.thread.id |
| 1839 | : item.type === "v2-pending" |
| 1840 | ? item.pendingTask.title |
| 1841 | : item.type, |
| 1842 | ), |
| 1843 | ).toEqual([ |
| 1844 | "queued", |
| 1845 | "active", |
| 1846 | "v2-working-shelf", |
| 1847 | "working", |
| 1848 | "v2-snoozed-shelf", |
| 1849 | "snoozed", |
| 1850 | "v2-settled-shelf", |
| 1851 | "settled", |
| 1852 | ]); |
| 1853 | }); |
| 1854 | }); |
| 1855 | |
| 1856 | describe("queued messages keep a settled thread active", () => { |
| 1857 | const threads = [ |
| 1858 | makeThread({ id: ThreadId.make("active"), title: "Active" }), |
| 1859 | makeThread({ id: ThreadId.make("settled"), title: "Settled", settledOverride: "settled" }), |
| 1860 | makeThread({ |
| 1861 | id: ThreadId.make("settled-queued"), |
| 1862 | title: "Settled with outbox", |
| 1863 | settledOverride: "settled", |
| 1864 | }), |
| 1865 | ]; |
| 1866 | const queuedThreadKeys = new Set([`${environmentId}:settled-queued`]); |
| 1867 | |
| 1868 | it("lists the thread in the active block instead of the settled shelf", () => { |
| 1869 | const layout = buildThreadListV2Items({ |
| 1870 | threads, |
| 1871 | environmentId: null, |
| 1872 | searchQuery: "", |
| 1873 | now: NOW, |
| 1874 | queuedThreadKeys, |
| 1875 | }); |
| 1876 | expect(layout.items.map((item) => [item.thread.id, item.variant] as const)).toEqual([ |
| 1877 | ["active", "card"], |
| 1878 | ["settled-queued", "card"], |
| 1879 | ["settled", "slim"], |
| 1880 | ]); |
| 1881 | expect(layout.settledCount).toBe(1); |
| 1882 | }); |
| 1883 | }); |