The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { makeAggregateState } from "./agentActivityAggregate.ts"; |
| 2 | import type { RelayAgentActivityState, RelayDeliveryResult } from "@t3tools/contracts/relay"; |
| 3 | import { describe, expect, it } from "@effect/vitest"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Redacted from "effect/Redacted"; |
| 7 | import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto"; |
| 8 | import { FetchHttpClient } from "effect/http"; |
| 9 | |
| 10 | import * as RelayConfiguration from "../Config.ts"; |
| 11 | import * as ApnsClient from "./ApnsClient.ts"; |
| 12 | import * as ApnsProviderTokens from "./ApnsProviderTokens.ts"; |
| 13 | import * as ApnsDeliveryQueue from "./ApnsDeliveryQueue.ts"; |
| 14 | import * as DeliveryAttempts from "./DeliveryAttempts.ts"; |
| 15 | import type { SignedApnsDeliveryJob } from "./apnsDeliveryJobs.ts"; |
| 16 | |
| 17 | import * as AgentActivityRows from "./AgentActivityRows.ts"; |
| 18 | import * as EnvironmentLinks from "../environments/EnvironmentLinks.ts"; |
| 19 | import * as LiveActivities from "./LiveActivities.ts"; |
| 20 | import * as AgentActivityPublisher from "./AgentActivityPublisher.ts"; |
| 21 | import * as FcmDeliveries from "./FcmDeliveries.ts"; |
| 22 | import * as ApnsDeliveries from "./ApnsDeliveries.ts"; |
| 23 | |
| 24 | const layerPublisher = AgentActivityPublisher.layer.pipe( |
| 25 | Layer.provide( |
| 26 | Layer.succeed(FcmDeliveries.FcmDeliveries, { |
| 27 | enqueue: () => Effect.succeed(null), |
| 28 | process: () => Effect.void, |
| 29 | }), |
| 30 | ), |
| 31 | ); |
| 32 | |
| 33 | const state: RelayAgentActivityState = { |
| 34 | environmentId: "env" as RelayAgentActivityState["environmentId"], |
| 35 | threadId: "thread" as RelayAgentActivityState["threadId"], |
| 36 | projectTitle: "Project", |
| 37 | threadTitle: "Thread", |
| 38 | modelTitle: "gpt-5.4", |
| 39 | phase: "running", |
| 40 | headline: "Running", |
| 41 | updatedAt: "1970-01-01T00:00:00.000Z", |
| 42 | deepLink: "/threads/env/thread", |
| 43 | }; |
| 44 | |
| 45 | function target(deviceId: string): LiveActivities.TargetRow { |
| 46 | return { |
| 47 | user_id: "dev:julius", |
| 48 | device_id: deviceId, |
| 49 | platform: "ios", |
| 50 | ios_major_version: 18, |
| 51 | app_version: "1.0.0", |
| 52 | bundle_id: null, |
| 53 | aps_environment: null, |
| 54 | push_token: null, |
| 55 | push_to_start_token: "start-token", |
| 56 | preferences_json: "{}", |
| 57 | activity_push_token: null, |
| 58 | remote_start_queued_at: null, |
| 59 | remote_started_at: null, |
| 60 | ended_at: null, |
| 61 | last_aggregate_json: null, |
| 62 | last_live_activity_delivery_at: null, |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | function makeLiveActivities( |
| 67 | overrides: Partial<LiveActivities.LiveActivities["Service"]> = {}, |
| 68 | ): LiveActivities.LiveActivities["Service"] { |
| 69 | return { |
| 70 | register: () => Effect.void, |
| 71 | listTargets: () => Effect.succeed([]), |
| 72 | listIdleArmedTargets: () => Effect.succeed([]), |
| 73 | markDelivery: () => Effect.void, |
| 74 | |
| 75 | invalidateDeliveryToken: () => Effect.void, |
| 76 | ...overrides, |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | function makeAgentActivityRows( |
| 81 | overrides: Partial<AgentActivityRows.AgentActivityRows["Service"]> = {}, |
| 82 | ): AgentActivityRows.AgentActivityRows["Service"] { |
| 83 | return { |
| 84 | upsert: () => Effect.void, |
| 85 | remove: () => Effect.void, |
| 86 | pruneTerminal: () => Effect.void, |
| 87 | listForUser: () => Effect.succeed([state]), |
| 88 | getForUserThread: () => Effect.succeed(state), |
| 89 | ...overrides, |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | function makeEnvironmentLinks( |
| 94 | overrides: Partial<EnvironmentLinks.EnvironmentLinks["Service"]> = {}, |
| 95 | ): EnvironmentLinks.EnvironmentLinks["Service"] { |
| 96 | return { |
| 97 | upsert: () => Effect.void, |
| 98 | listDeliveryUsersForEnvironment: () => |
| 99 | Effect.succeed([ |
| 100 | { |
| 101 | userId: "dev:julius", |
| 102 | notificationsEnabled: true, |
| 103 | liveActivitiesEnabled: true, |
| 104 | }, |
| 105 | ]), |
| 106 | listForUser: () => Effect.succeed([]), |
| 107 | getForUser: () => Effect.succeed(null), |
| 108 | findActiveManagedForEnvironment: () => Effect.succeed([]), |
| 109 | updateLiveActivityPreference: () => Effect.die("unexpected Live Activity preference update"), |
| 110 | setHoldWebhooksWhileOffline: () => Effect.void, |
| 111 | revokeForUser: () => Effect.succeed(false), |
| 112 | ...overrides, |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | function makeApnsDeliveries( |
| 117 | overrides: Partial<ApnsDeliveries.ApnsDeliveries["Service"]> = {}, |
| 118 | ): ApnsDeliveries.ApnsDeliveries["Service"] { |
| 119 | return { |
| 120 | processBatch: () => Effect.void, |
| 121 | sendForTarget: () => Effect.succeed(null), |
| 122 | sendLiveActivity: () => |
| 123 | Effect.succeed({ |
| 124 | deviceId: "device", |
| 125 | kind: "live_activity_start", |
| 126 | ok: true, |
| 127 | apnsStatus: 200, |
| 128 | apnsReason: null, |
| 129 | apnsId: "apns-id", |
| 130 | }), |
| 131 | sendPushNotification: () => |
| 132 | Effect.succeed({ |
| 133 | deviceId: "device", |
| 134 | kind: "push_notification", |
| 135 | ok: true, |
| 136 | apnsStatus: 200, |
| 137 | apnsReason: null, |
| 138 | apnsId: "apns-id", |
| 139 | }), |
| 140 | processSignedJob: () => |
| 141 | Effect.succeed({ |
| 142 | deviceId: "device", |
| 143 | kind: "live_activity_start", |
| 144 | ok: true, |
| 145 | apnsStatus: 200, |
| 146 | apnsReason: null, |
| 147 | apnsId: "apns-id", |
| 148 | }), |
| 149 | ...overrides, |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | describe("AgentActivityPublisher", () => { |
| 154 | it.effect("routes Android publication and registration replay to FCM alongside iOS", () => { |
| 155 | const android = { ...target("android"), platform: "android" as const, ios_major_version: null }; |
| 156 | const ios = target("ios"); |
| 157 | const fcmCalls: Array<Parameters<FcmDeliveries.FcmDeliveries["Service"]["enqueue"]>[0]> = []; |
| 158 | const appleDevices: string[] = []; |
| 159 | return Effect.gen(function* () { |
| 160 | const publisher = yield* AgentActivityPublisher.AgentActivityPublisher; |
| 161 | yield* publisher.publish({ |
| 162 | environmentId: state.environmentId, |
| 163 | environmentPublicKey: "key", |
| 164 | threadId: state.threadId, |
| 165 | state, |
| 166 | }); |
| 167 | yield* publisher.replayForLiveActivityRegistration({ |
| 168 | userId: android.user_id, |
| 169 | deviceId: android.device_id, |
| 170 | }); |
| 171 | expect(fcmCalls).toEqual([ |
| 172 | { target: android, state }, |
| 173 | { target: android, state: null, replay: true }, |
| 174 | ]); |
| 175 | expect(appleDevices).toEqual(["ios"]); |
| 176 | }).pipe( |
| 177 | Effect.provide( |
| 178 | AgentActivityPublisher.layer.pipe( |
| 179 | Layer.provide( |
| 180 | Layer.mergeAll( |
| 181 | Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()), |
| 182 | Layer.succeed(EnvironmentLinks.EnvironmentLinks, makeEnvironmentLinks()), |
| 183 | Layer.succeed( |
| 184 | LiveActivities.LiveActivities, |
| 185 | makeLiveActivities({ listTargets: () => Effect.succeed([android, ios]) }), |
| 186 | ), |
| 187 | Layer.succeed( |
| 188 | ApnsDeliveries.ApnsDeliveries, |
| 189 | makeApnsDeliveries({ |
| 190 | sendForTarget: (input) => |
| 191 | Effect.sync(() => { |
| 192 | appleDevices.push(input.target.device_id); |
| 193 | return null; |
| 194 | }), |
| 195 | }), |
| 196 | ), |
| 197 | Layer.succeed(FcmDeliveries.FcmDeliveries, { |
| 198 | enqueue: (input) => |
| 199 | Effect.sync(() => { |
| 200 | fcmCalls.push(input); |
| 201 | return null; |
| 202 | }), |
| 203 | process: () => Effect.void, |
| 204 | }), |
| 205 | ), |
| 206 | ), |
| 207 | ), |
| 208 | ), |
| 209 | ); |
| 210 | }); |
| 211 | |
| 212 | it.effect.each([ |
| 213 | { |
| 214 | name: "skips the activity query without a running iOS Live Activity", |
| 215 | activityToken: null, |
| 216 | listCalls: 0, |
| 217 | }, |
| 218 | { |
| 219 | name: "aggregates activity for a running iOS Live Activity", |
| 220 | activityToken: "activity-token", |
| 221 | listCalls: 1, |
| 222 | }, |
| 223 | ])("$name", ({ activityToken, listCalls }) => { |
| 224 | const android = { ...target("android"), platform: "android" as const, ios_major_version: null }; |
| 225 | const ios = { ...target("ios"), activity_push_token: activityToken }; |
| 226 | let listed = 0; |
| 227 | const hasAggregate: boolean[] = []; |
| 228 | return Effect.gen(function* () { |
| 229 | const publisher = yield* AgentActivityPublisher.AgentActivityPublisher; |
| 230 | yield* publisher.publish({ |
| 231 | environmentId: state.environmentId, |
| 232 | environmentPublicKey: "key", |
| 233 | threadId: state.threadId, |
| 234 | state, |
| 235 | }); |
| 236 | expect(listed).toBe(listCalls); |
| 237 | expect(hasAggregate).toEqual([listCalls > 0]); |
| 238 | }).pipe( |
| 239 | Effect.provide( |
| 240 | AgentActivityPublisher.layer.pipe( |
| 241 | Layer.provide( |
| 242 | Layer.mergeAll( |
| 243 | Layer.succeed( |
| 244 | AgentActivityRows.AgentActivityRows, |
| 245 | makeAgentActivityRows({ |
| 246 | listForUser: () => |
| 247 | Effect.sync(() => { |
| 248 | listed += 1; |
| 249 | return [state]; |
| 250 | }), |
| 251 | }), |
| 252 | ), |
| 253 | Layer.succeed(EnvironmentLinks.EnvironmentLinks, makeEnvironmentLinks()), |
| 254 | Layer.succeed( |
| 255 | LiveActivities.LiveActivities, |
| 256 | makeLiveActivities({ listTargets: () => Effect.succeed([android, ios]) }), |
| 257 | ), |
| 258 | Layer.succeed( |
| 259 | ApnsDeliveries.ApnsDeliveries, |
| 260 | makeApnsDeliveries({ |
| 261 | sendForTarget: (input) => |
| 262 | Effect.sync(() => { |
| 263 | hasAggregate.push(input.aggregate !== null); |
| 264 | return null; |
| 265 | }), |
| 266 | }), |
| 267 | ), |
| 268 | Layer.succeed(FcmDeliveries.FcmDeliveries, { |
| 269 | enqueue: () => Effect.succeed(null), |
| 270 | process: () => Effect.void, |
| 271 | }), |
| 272 | ), |
| 273 | ), |
| 274 | ), |
| 275 | ), |
| 276 | ); |
| 277 | }); |
| 278 | |
| 279 | it.effect("replays the latest aggregate when a Live Activity token registers", () => { |
| 280 | const registeredTarget: LiveActivities.TargetRow = { |
| 281 | ...target("device-1"), |
| 282 | push_to_start_token: null, |
| 283 | activity_push_token: "activity-token", |
| 284 | remote_start_queued_at: null, |
| 285 | remote_started_at: "1970-01-01T00:00:01.000Z", |
| 286 | }; |
| 287 | const sent: Array<Parameters<ApnsDeliveries.ApnsDeliveries["Service"]["sendForTarget"]>[0]> = |
| 288 | []; |
| 289 | const deliveryResult: RelayDeliveryResult = { |
| 290 | deviceId: "device-1", |
| 291 | kind: "live_activity_update", |
| 292 | ok: true, |
| 293 | apnsStatus: null, |
| 294 | apnsReason: null, |
| 295 | apnsId: "queued", |
| 296 | }; |
| 297 | |
| 298 | return Effect.gen(function* () { |
| 299 | const result = yield* Effect.gen(function* () { |
| 300 | const publisher = yield* AgentActivityPublisher.AgentActivityPublisher; |
| 301 | return yield* publisher.replayForLiveActivityRegistration({ |
| 302 | userId: "dev:julius", |
| 303 | deviceId: "device-1", |
| 304 | }); |
| 305 | }).pipe( |
| 306 | Effect.provide( |
| 307 | layerPublisher.pipe( |
| 308 | Layer.provide( |
| 309 | Layer.mergeAll( |
| 310 | Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()), |
| 311 | Layer.succeed(EnvironmentLinks.EnvironmentLinks, makeEnvironmentLinks()), |
| 312 | Layer.succeed( |
| 313 | LiveActivities.LiveActivities, |
| 314 | makeLiveActivities({ |
| 315 | listTargets: () => Effect.succeed([registeredTarget, target("device-2")]), |
| 316 | }), |
| 317 | ), |
| 318 | Layer.succeed( |
| 319 | ApnsDeliveries.ApnsDeliveries, |
| 320 | makeApnsDeliveries({ |
| 321 | sendForTarget: (input) => |
| 322 | Effect.sync(() => { |
| 323 | sent.push(input); |
| 324 | return deliveryResult; |
| 325 | }), |
| 326 | }), |
| 327 | ), |
| 328 | ), |
| 329 | ), |
| 330 | ), |
| 331 | ), |
| 332 | ); |
| 333 | |
| 334 | expect(result).toEqual(deliveryResult); |
| 335 | expect(sent).toHaveLength(1); |
| 336 | expect(sent[0]?.target.device_id).toBe("device-1"); |
| 337 | expect(sent[0]?.aggregate).toMatchObject({ |
| 338 | activeCount: 1, |
| 339 | activities: [ |
| 340 | { |
| 341 | environmentId: state.environmentId, |
| 342 | threadId: state.threadId, |
| 343 | status: "Working", |
| 344 | }, |
| 345 | ], |
| 346 | }); |
| 347 | }); |
| 348 | }); |
| 349 | |
| 350 | it.effect.each([ |
| 351 | { phase: "waiting_for_input", pushToStartToken: null, body: "Input: Project" }, |
| 352 | { phase: "waiting_for_approval", pushToStartToken: "start-token", body: "Approval: Project" }, |
| 353 | ] as const)( |
| 354 | "queues $phase notifications for notification-only links and respects link opt-out", |
| 355 | ({ phase, pushToStartToken, body }) => { |
| 356 | const publishedState: RelayAgentActivityState = { ...state, phase }; |
| 357 | const queuedJobs: Array<SignedApnsDeliveryJob> = []; |
| 358 | let notificationsEnabled = true; |
| 359 | let liveActivitiesEnabled = false; |
| 360 | const dependencies = Layer.mergeAll( |
| 361 | Layer.succeed( |
| 362 | AgentActivityRows.AgentActivityRows, |
| 363 | makeAgentActivityRows({ |
| 364 | listForUser: () => |
| 365 | liveActivitiesEnabled |
| 366 | ? Effect.succeed([publishedState]) |
| 367 | : Effect.die("Notification-only delivery must not read rows"), |
| 368 | }), |
| 369 | ), |
| 370 | Layer.succeed( |
| 371 | EnvironmentLinks.EnvironmentLinks, |
| 372 | makeEnvironmentLinks({ |
| 373 | listDeliveryUsersForEnvironment: () => |
| 374 | Effect.succeed([ |
| 375 | { userId: "dev:julius", notificationsEnabled, liveActivitiesEnabled }, |
| 376 | ]), |
| 377 | }), |
| 378 | ), |
| 379 | Layer.succeed( |
| 380 | LiveActivities.LiveActivities, |
| 381 | makeLiveActivities({ |
| 382 | listTargets: () => |
| 383 | Effect.succeed([ |
| 384 | { |
| 385 | ...target("device-1"), |
| 386 | push_token: "apns-device-token", |
| 387 | push_to_start_token: pushToStartToken, |
| 388 | preferences_json: JSON.stringify({ |
| 389 | liveActivitiesEnabled: true, |
| 390 | notificationsEnabled: true, |
| 391 | notifyOnApproval: true, |
| 392 | notifyOnInput: true, |
| 393 | notifyOnCompletion: true, |
| 394 | notifyOnFailure: true, |
| 395 | }), |
| 396 | }, |
| 397 | ]), |
| 398 | }), |
| 399 | ), |
| 400 | Layer.succeed(ApnsDeliveryQueue.ApnsDeliveryQueueSender, { |
| 401 | send: (job) => |
| 402 | Effect.sync(() => { |
| 403 | queuedJobs.push(job); |
| 404 | }), |
| 405 | }), |
| 406 | Layer.succeed(DeliveryAttempts.DeliveryAttempts, { |
| 407 | pruneExpired: Effect.succeed(0), |
| 408 | claimSourceJob: () => Effect.succeed("claimed" as const), |
| 409 | completeSourceJob: () => Effect.void, |
| 410 | }), |
| 411 | RelayConfiguration.layer({ |
| 412 | relayIssuer: "https://relay.example.test", |
| 413 | apns: { |
| 414 | environment: "sandbox", |
| 415 | teamId: "team-id", |
| 416 | keyId: "key-id", |
| 417 | privateKey: Redacted.make("unused-key"), |
| 418 | bundleId: "com.t3tools.t3code.dev", |
| 419 | }, |
| 420 | apnsDeliveryJobSigningSecret: Redacted.make("job-signing-secret"), |
| 421 | clerkSecretKey: Redacted.make("clerk-secret"), |
| 422 | clerkPublishableKey: "pk_test_test", |
| 423 | clerkJwtAudience: "t3-code-relay", |
| 424 | cloudMintPrivateKey: Redacted.make("cloud-private-key"), |
| 425 | cloudMintPublicKey: "cloud-public-key", |
| 426 | managedEndpointBaseDomain: undefined, |
| 427 | managedEndpointNamespace: undefined, |
| 428 | }), |
| 429 | ); |
| 430 | const deliveriesLayer = ApnsDeliveries.layer.pipe( |
| 431 | Layer.provide(NodeCryptoLayer.layerMac), |
| 432 | Layer.provide( |
| 433 | ApnsClient.layer.pipe( |
| 434 | Layer.provide(ApnsProviderTokens.layer), |
| 435 | Layer.provide(FetchHttpClient.layer), |
| 436 | ), |
| 437 | ), |
| 438 | Layer.provide(ApnsDeliveryQueue.layer.pipe(Layer.provide(NodeCryptoLayer.layer))), |
| 439 | ); |
| 440 | |
| 441 | return Effect.gen(function* () { |
| 442 | const publisher = yield* AgentActivityPublisher.AgentActivityPublisher; |
| 443 | const publish = () => |
| 444 | publisher.publish({ |
| 445 | environmentId: state.environmentId, |
| 446 | environmentPublicKey: "environment-public-key", |
| 447 | threadId: state.threadId, |
| 448 | state: publishedState, |
| 449 | }); |
| 450 | expect((yield* publish()).deliveries).toMatchObject([ |
| 451 | { deviceId: "device-1", kind: "push_notification", queued: true }, |
| 452 | ]); |
| 453 | expect(queuedJobs.map((job) => job.payload)).toMatchObject([ |
| 454 | { kind: "push_notification", notification: { body, threadId: state.threadId } }, |
| 455 | ]); |
| 456 | |
| 457 | notificationsEnabled = false; |
| 458 | liveActivitiesEnabled = true; |
| 459 | expect((yield* publish()).deliveries).toEqual([]); |
| 460 | expect(queuedJobs).toHaveLength(1); |
| 461 | }).pipe( |
| 462 | Effect.provide( |
| 463 | layerPublisher.pipe(Layer.provide(deliveriesLayer), Layer.provide(dependencies)), |
| 464 | ), |
| 465 | ); |
| 466 | }, |
| 467 | ); |
| 468 | }); |
| 469 | |
| 470 | describe("isExpiredAgentActivityState", () => { |
| 471 | it("treats rows with unparseable timestamps as expired", () => { |
| 472 | expect( |
| 473 | AgentActivityPublisher.isExpiredAgentActivityState({ ...state, updatedAt: "not-a-date" }, 0), |
| 474 | ).toBe(true); |
| 475 | }); |
| 476 | }); |
| 477 | |
| 478 | describe("makeAggregateState", () => { |
| 479 | const hourMs = 60 * 60 * 1_000; |
| 480 | |
| 481 | it("drops expired rows from the aggregate", () => { |
| 482 | const fresh: RelayAgentActivityState = { |
| 483 | ...state, |
| 484 | threadId: "thread-fresh" as RelayAgentActivityState["threadId"], |
| 485 | updatedAt: "1970-01-01T03:00:00.000Z", |
| 486 | }; |
| 487 | const aggregate = makeAggregateState({ |
| 488 | activeStates: [state, fresh], |
| 489 | terminalState: null, |
| 490 | nowMs: 3 * hourMs, |
| 491 | }); |
| 492 | |
| 493 | expect(aggregate?.activeCount).toBe(1); |
| 494 | expect(aggregate?.activities).toMatchObject([{ threadId: "thread-fresh" }]); |
| 495 | }); |
| 496 | |
| 497 | it("returns null when every row has expired and nothing terminal remains", () => { |
| 498 | expect( |
| 499 | makeAggregateState({ |
| 500 | activeStates: [state], |
| 501 | terminalState: null, |
| 502 | nowMs: 3 * hourMs, |
| 503 | }), |
| 504 | ).toBeNull(); |
| 505 | }); |
| 506 | |
| 507 | it("still reports the terminal state when active rows have expired", () => { |
| 508 | const terminalState: RelayAgentActivityState = { |
| 509 | ...state, |
| 510 | phase: "completed", |
| 511 | updatedAt: "1970-01-01T03:00:00.000Z", |
| 512 | }; |
| 513 | const aggregate = makeAggregateState({ |
| 514 | activeStates: [state], |
| 515 | terminalState, |
| 516 | nowMs: 3 * hourMs, |
| 517 | }); |
| 518 | |
| 519 | expect(aggregate?.activeCount).toBe(0); |
| 520 | expect(aggregate?.activities).toMatchObject([{ phase: "completed" }]); |
| 521 | }); |
| 522 | |
| 523 | it("keeps a recently finished thread visible as Done beside active agents", () => { |
| 524 | const active: RelayAgentActivityState = { |
| 525 | ...state, |
| 526 | threadId: "thread-active" as RelayAgentActivityState["threadId"], |
| 527 | updatedAt: "1970-01-01T00:58:00.000Z", |
| 528 | }; |
| 529 | const justCompleted: RelayAgentActivityState = { |
| 530 | ...state, |
| 531 | threadId: "thread-done" as RelayAgentActivityState["threadId"], |
| 532 | phase: "completed", |
| 533 | updatedAt: "1970-01-01T00:59:00.000Z", |
| 534 | }; |
| 535 | const aggregate = makeAggregateState({ |
| 536 | activeStates: [active, justCompleted], |
| 537 | terminalState: null, |
| 538 | nowMs: hourMs, |
| 539 | }); |
| 540 | |
| 541 | expect(aggregate?.activeCount).toBe(1); |
| 542 | expect(aggregate?.subtitle).toBe("Agent work in progress"); |
| 543 | expect(aggregate?.activities).toMatchObject([ |
| 544 | { threadId: "thread-active", phase: "running" }, |
| 545 | { threadId: "thread-done", phase: "completed", status: "Done" }, |
| 546 | ]); |
| 547 | expect(aggregate?.updatedAt).toBe("1970-01-01T00:59:00.000Z"); |
| 548 | }); |
| 549 | |
| 550 | it("keeps showing recently finished work when nothing is active", () => { |
| 551 | const lingeringCompleted: RelayAgentActivityState = { |
| 552 | ...state, |
| 553 | phase: "completed", |
| 554 | updatedAt: "1970-01-01T00:59:00.000Z", |
| 555 | }; |
| 556 | const aggregate = makeAggregateState({ |
| 557 | activeStates: [lingeringCompleted], |
| 558 | terminalState: null, |
| 559 | nowMs: hourMs, |
| 560 | }); |
| 561 | |
| 562 | expect(aggregate).toMatchObject({ |
| 563 | activeCount: 0, |
| 564 | subtitle: "Agent work completed", |
| 565 | activities: [{ phase: "completed", status: "Done" }], |
| 566 | }); |
| 567 | expect( |
| 568 | makeAggregateState({ |
| 569 | activeStates: [{ ...lingeringCompleted, updatedAt: "1970-01-01T00:44:00.000Z" }], |
| 570 | terminalState: null, |
| 571 | nowMs: hourMs, |
| 572 | }), |
| 573 | ).toBeNull(); |
| 574 | }); |
| 575 | |
| 576 | it("gives active agents the display slots before finished ones", () => { |
| 577 | const mkActive = (id: string): RelayAgentActivityState => ({ |
| 578 | ...state, |
| 579 | threadId: id as RelayAgentActivityState["threadId"], |
| 580 | updatedAt: "1970-01-01T00:58:00.000Z", |
| 581 | }); |
| 582 | const justCompleted: RelayAgentActivityState = { |
| 583 | ...state, |
| 584 | threadId: "thread-done" as RelayAgentActivityState["threadId"], |
| 585 | phase: "completed", |
| 586 | updatedAt: "1970-01-01T00:59:00.000Z", |
| 587 | }; |
| 588 | const aggregate = makeAggregateState({ |
| 589 | activeStates: [ |
| 590 | mkActive("a-1"), |
| 591 | mkActive("a-2"), |
| 592 | mkActive("a-3"), |
| 593 | mkActive("a-4"), |
| 594 | mkActive("a-5"), |
| 595 | justCompleted, |
| 596 | ], |
| 597 | terminalState: null, |
| 598 | nowMs: hourMs, |
| 599 | }); |
| 600 | |
| 601 | expect(aggregate?.activeCount).toBe(5); |
| 602 | expect(aggregate?.activities).toMatchObject([ |
| 603 | { threadId: "a-1" }, |
| 604 | { threadId: "a-2" }, |
| 605 | { threadId: "a-3" }, |
| 606 | { threadId: "a-4" }, |
| 607 | { threadId: "a-5" }, |
| 608 | ]); |
| 609 | }); |
| 610 | }); |