Open History to see the last commit
1import type { RelayAgentActivityState, RelayDeliveryResult } from "@t3tools/contracts/relay";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5
6import * as AgentActivityRows from "./AgentActivityRows.ts";
7import * as EnvironmentLinks from "../environments/EnvironmentLinks.ts";
8import * as LiveActivities from "./LiveActivities.ts";
9import * as AgentActivityPublisher from "./AgentActivityPublisher.ts";
10import * as ApnsDeliveries from "./ApnsDeliveries.ts";
11
12const state: RelayAgentActivityState = {
13 environmentId: "env" as RelayAgentActivityState["environmentId"],
14 threadId: "thread" as RelayAgentActivityState["threadId"],
15 projectTitle: "Project",
16 threadTitle: "Thread",
17 modelTitle: "gpt-5.4",
18 phase: "running",
19 headline: "Running",
20 updatedAt: "1970-01-01T00:00:00.000Z",
21 deepLink: "/threads/env/thread",
22};
23
24function target(deviceId: string): LiveActivities.TargetRow {
25 return {
26 user_id: "dev:julius",
27 device_id: deviceId,
28 platform: "ios",
29 ios_major_version: 18,
30 app_version: "1.0.0",
31 bundle_id: null,
32 aps_environment: null,
33 push_token: null,
34 push_to_start_token: "start-token",
35 preferences_json: "{}",
36 activity_push_token: null,
37 remote_start_queued_at: null,
38 remote_started_at: null,
39 ended_at: null,
40 last_aggregate_json: null,
41 last_live_activity_delivery_at: null,
42 };
43}
44
45function makeLiveActivities(
46 overrides: Partial<LiveActivities.LiveActivities["Service"]> = {},
47): LiveActivities.LiveActivities["Service"] {
48 return {
49 register: () => Effect.void,
50 listTargets: () => Effect.succeed([]),
51 markDelivery: () => Effect.void,
52 markStartQueued: () => Effect.void,
53 clearStartQueued: () => Effect.void,
54 invalidateDeliveryToken: () => Effect.void,
55 ...overrides,
56 };
57}
58
59function makeAgentActivityRows(
60 overrides: Partial<AgentActivityRows.AgentActivityRows["Service"]> = {},
61): AgentActivityRows.AgentActivityRows["Service"] {
62 return {
63 upsert: () => Effect.void,
64 remove: () => Effect.void,
65 pruneTerminal: () => Effect.void,
66 listForUser: () => Effect.succeed([state]),
67 getForUserThread: () => Effect.succeed(state),
68 ...overrides,
69 };
70}
71
72function makeEnvironmentLinks(
73 overrides: Partial<EnvironmentLinks.EnvironmentLinks["Service"]> = {},
74): EnvironmentLinks.EnvironmentLinks["Service"] {
75 return {
76 upsert: () => Effect.void,
77 listUsersForEnvironment: () => Effect.succeed(["dev:julius"]),
78 listDeliveryUsersForEnvironment: () =>
79 Effect.succeed([
80 {
81 userId: "dev:julius",
82 notificationsEnabled: true,
83 liveActivitiesEnabled: true,
84 },
85 ]),
86 listPublicKeysForEnvironment: () => Effect.succeed([]),
87 listForUser: () => Effect.succeed([]),
88 getForUser: () => Effect.succeed(null),
89 revokeForUser: () => Effect.succeed(false),
90 ...overrides,
91 };
92}
93
94function makeApnsDeliveries(
95 overrides: Partial<ApnsDeliveries.ApnsDeliveries["Service"]> = {},
96): ApnsDeliveries.ApnsDeliveries["Service"] {
97 return {
98 sendForTarget: () => Effect.succeed(null),
99 sendPushNotificationForTarget: () => Effect.succeed(null),
100 sendLiveActivity: () =>
101 Effect.succeed({
102 deviceId: "device",
103 kind: "live_activity_start",
104 ok: true,
105 apnsStatus: 200,
106 apnsReason: null,
107 apnsId: "apns-id",
108 }),
109 sendPushNotification: () =>
110 Effect.succeed({
111 deviceId: "device",
112 kind: "push_notification",
113 ok: true,
114 apnsStatus: 200,
115 apnsReason: null,
116 apnsId: "apns-id",
117 }),
118 processSignedJob: () =>
119 Effect.succeed({
120 deviceId: "device",
121 kind: "live_activity_start",
122 ok: true,
123 apnsStatus: 200,
124 apnsReason: null,
125 apnsId: "apns-id",
126 }),
127 ...overrides,
128 };
129}
130
131describe("AgentActivityPublisher", () => {
132 it.effect("replays the latest aggregate when a Live Activity token registers", () => {
133 const registeredTarget: LiveActivities.TargetRow = {
134 ...target("device-1"),
135 push_to_start_token: null,
136 activity_push_token: "activity-token",
137 remote_start_queued_at: null,
138 remote_started_at: "1970-01-01T00:00:01.000Z",
139 };
140 const sent: Array<Parameters<ApnsDeliveries.ApnsDeliveries["Service"]["sendForTarget"]>[0]> =
141 [];
142 const deliveryResult: RelayDeliveryResult = {
143 deviceId: "device-1",
144 kind: "live_activity_update",
145 ok: true,
146 apnsStatus: null,
147 apnsReason: null,
148 apnsId: "queued",
149 };
150
151 return Effect.gen(function* () {
152 const result = yield* Effect.gen(function* () {
153 const publisher = yield* AgentActivityPublisher.AgentActivityPublisher;
154 return yield* publisher.replayForLiveActivityRegistration({
155 userId: "dev:julius",
156 deviceId: "device-1",
157 });
158 }).pipe(
159 Effect.provide(
160 AgentActivityPublisher.layer.pipe(
161 Layer.provide(
162 Layer.mergeAll(
163 Layer.succeed(AgentActivityRows.AgentActivityRows, makeAgentActivityRows()),
164 Layer.succeed(EnvironmentLinks.EnvironmentLinks, makeEnvironmentLinks()),
165 Layer.succeed(
166 LiveActivities.LiveActivities,
167 makeLiveActivities({
168 listTargets: () => Effect.succeed([registeredTarget, target("device-2")]),
169 }),
170 ),
171 Layer.succeed(
172 ApnsDeliveries.ApnsDeliveries,
173 makeApnsDeliveries({
174 sendForTarget: (input) =>
175 Effect.sync(() => {
176 sent.push(input);
177 return deliveryResult;
178 }),
179 }),
180 ),
181 ),
182 ),
183 ),
184 ),
185 );
186
187 expect(result).toEqual(deliveryResult);
188 expect(sent).toHaveLength(1);
189 expect(sent[0]?.target.device_id).toBe("device-1");
190 expect(sent[0]?.aggregate).toMatchObject({
191 activeCount: 1,
192 activities: [
193 {
194 environmentId: state.environmentId,
195 threadId: state.threadId,
196 status: "Working",
197 },
198 ],
199 });
200 });
201 });
202
203 it.effect("publishes listed targets through the APNs delivery service", () => {
204 const firstTarget = target("device-1");
205 const secondTarget = target("device-2");
206 const deliveryResult: RelayDeliveryResult = {
207 deviceId: "device-1",
208 kind: "live_activity_start",
209 ok: true,
210 apnsStatus: 200,
211 apnsReason: null,
212 apnsId: "apns-id",
213 };
214 const sentTargets: Array<string> = [];
215 const deliveryLookups: Array<{
216 readonly environmentId: string;
217 readonly environmentPublicKey: string;
218 }> = [];
219 const upserts: Array<Parameters<AgentActivityRows.AgentActivityRows["Service"]["upsert"]>[0]> =
220 [];
221
222 return Effect.gen(function* () {
223 const result = yield* Effect.gen(function* () {
224 const publisher = yield* AgentActivityPublisher.AgentActivityPublisher;
225 return yield* publisher.publish({
226 environmentId: "env",
227 environmentPublicKey: "environment-public-key",
228 threadId: "thread",
229 state,
230 });
231 }).pipe(
232 Effect.provide(
233 AgentActivityPublisher.layer.pipe(
234 Layer.provide(
235 Layer.mergeAll(
236 Layer.succeed(
237 AgentActivityRows.AgentActivityRows,
238 makeAgentActivityRows({
239 upsert: (input) =>
240 Effect.sync(() => {
241 upserts.push(input);
242 }),
243 }),
244 ),
245 Layer.succeed(
246 EnvironmentLinks.EnvironmentLinks,
247 makeEnvironmentLinks({
248 listDeliveryUsersForEnvironment: (input) =>
249 Effect.sync(() => {
250 deliveryLookups.push(input);
251 return [
252 {
253 userId: "dev:julius",
254 notificationsEnabled: true,
255 liveActivitiesEnabled: true,
256 },
257 ];
258 }),
259 }),
260 ),
261 Layer.succeed(
262 LiveActivities.LiveActivities,
263 makeLiveActivities({
264 listTargets: () => Effect.succeed([firstTarget, secondTarget]),
265 }),
266 ),
267 Layer.succeed(
268 ApnsDeliveries.ApnsDeliveries,
269 makeApnsDeliveries({
270 sendForTarget: (input) =>
271 Effect.sync(() => {
272 sentTargets.push(input.target.device_id);
273 return input.target.device_id === "device-1" ? deliveryResult : null;
274 }),
275 }),
276 ),
277 ),
278 ),
279 ),
280 ),
281 );
282
283 expect(sentTargets).toEqual(["device-1", "device-2"]);
284 expect(deliveryLookups).toEqual([
285 {
286 environmentId: "env",
287 environmentPublicKey: "environment-public-key",
288 },
289 ]);
290 expect(upserts).toMatchObject([
291 {
292 environmentPublicKey: "environment-public-key",
293 state: {
294 environmentId: "env",
295 threadId: "thread",
296 },
297 },
298 ]);
299 expect(result).toEqual({ ok: true, deliveries: [deliveryResult] });
300 });
301 });
302
303 it.effect("ends the last remote Live Activity with a terminal content state", () => {
304 const completedState: RelayAgentActivityState = {
305 ...state,
306 phase: "completed",
307 headline: "Done",
308 updatedAt: "1970-01-01T00:00:10.000Z",
309 };
310 const sentAggregates: Array<
311 Parameters<ApnsDeliveries.ApnsDeliveries["Service"]["sendForTarget"]>[0]
312 > = [];
313 const upserts: Array<Parameters<AgentActivityRows.AgentActivityRows["Service"]["upsert"]>[0]> =
314 [];
315
316 return Effect.gen(function* () {
317 const result = yield* Effect.gen(function* () {
318 const publisher = yield* AgentActivityPublisher.AgentActivityPublisher;
319 return yield* publisher.publish({
320 environmentId: "env",
321 environmentPublicKey: "environment-public-key",
322 threadId: "thread",
323 state: completedState,
324 });
325 }).pipe(
326 Effect.provide(
327 AgentActivityPublisher.layer.pipe(
328 Layer.provide(
329 Layer.mergeAll(
330 Layer.succeed(
331 AgentActivityRows.AgentActivityRows,
332 makeAgentActivityRows({
333 upsert: (input) =>
334 Effect.sync(() => {
335 upserts.push(input);
336 }),
337 listForUser: () => Effect.succeed([]),
338 }),
339 ),
340 Layer.succeed(EnvironmentLinks.EnvironmentLinks, makeEnvironmentLinks()),
341 Layer.succeed(
342 LiveActivities.LiveActivities,
343 makeLiveActivities({
344 listTargets: () =>
345 Effect.succeed([
346 {
347 ...target("device-1"),
348 push_to_start_token: null,
349 activity_push_token: "activity-token",
350 remote_started_at: "1970-01-01T00:00:00.000Z",
351 },
352 ]),
353 }),
354 ),
355 Layer.succeed(
356 ApnsDeliveries.ApnsDeliveries,
357 makeApnsDeliveries({
358 sendForTarget: (input) =>
359 Effect.sync(() => {
360 sentAggregates.push(input);
361 return {
362 deviceId: input.target.device_id,
363 kind: "live_activity_end",
364 ok: true,
365 apnsStatus: null,
366 apnsReason: null,
367 apnsId: "queued",
368 };
369 }),
370 }),
371 ),
372 ),
373 ),
374 ),
375 ),
376 );
377
378 expect(result.deliveries).toMatchObject([
379 {
380 deviceId: "device-1",
381 kind: "live_activity_end",
382 ok: true,
383 },
384 ]);
385 // Terminal states are persisted (and later pruned by the cron) so the
386 // finished thread can keep a Done row in later aggregates.
387 expect(upserts).toEqual([
388 {
389 environmentPublicKey: "environment-public-key",
390 state: completedState,
391 },
392 ]);
393 expect(sentAggregates).toHaveLength(1);
394 expect(sentAggregates[0]?.aggregate).toMatchObject({
395 activeCount: 0,
396 subtitle: "Agent work completed",
397 activities: [
398 {
399 environmentId: completedState.environmentId,
400 threadId: completedState.threadId,
401 phase: "completed",
402 status: "Done",
403 },
404 ],
405 });
406 });
407 });
408
409 it.effect("queues push notifications for notification-only environment links", () => {
410 const notificationState: RelayAgentActivityState = {
411 ...state,
412 phase: "waiting_for_input",
413 headline: "Needs input",
414 };
415 const liveAggregates: Array<
416 Parameters<ApnsDeliveries.ApnsDeliveries["Service"]["sendForTarget"]>[0]
417 > = [];
418 const pushAggregates: Array<
419 Parameters<ApnsDeliveries.ApnsDeliveries["Service"]["sendPushNotificationForTarget"]>[0]
420 > = [];
421
422 return Effect.gen(function* () {
423 const result = yield* Effect.gen(function* () {
424 const publisher = yield* AgentActivityPublisher.AgentActivityPublisher;
425 return yield* publisher.publish({
426 environmentId: "env",
427 environmentPublicKey: "environment-public-key",
428 threadId: "thread",
429 state: notificationState,
430 });
431 }).pipe(
432 Effect.provide(
433 AgentActivityPublisher.layer.pipe(
434 Layer.provide(
435 Layer.mergeAll(
436 Layer.succeed(
437 AgentActivityRows.AgentActivityRows,
438 makeAgentActivityRows({
439 listForUser: () => Effect.succeed([]),
440 }),
441 ),
442 Layer.succeed(
443 EnvironmentLinks.EnvironmentLinks,
444 makeEnvironmentLinks({
445 listDeliveryUsersForEnvironment: () =>
446 Effect.succeed([
447 {
448 userId: "dev:julius",
449 notificationsEnabled: true,
450 liveActivitiesEnabled: false,
451 },
452 ]),
453 }),
454 ),
455 Layer.succeed(
456 LiveActivities.LiveActivities,
457 makeLiveActivities({
458 listTargets: () =>
459 Effect.succeed([
460 {
461 ...target("device-1"),
462 push_token: "apns-device-token",
463 push_to_start_token: null,
464 },
465 ]),
466 }),
467 ),
468 Layer.succeed(
469 ApnsDeliveries.ApnsDeliveries,
470 makeApnsDeliveries({
471 sendForTarget: (input) =>
472 Effect.sync(() => {
473 liveAggregates.push(input);
474 return null;
475 }),
476 sendPushNotificationForTarget: (input) =>
477 Effect.sync(() => {
478 pushAggregates.push(input);
479 return {
480 deviceId: input.target.device_id,
481 kind: "push_notification",
482 ok: true,
483 queued: true,
484 apnsStatus: null,
485 apnsReason: null,
486 apnsId: null,
487 };
488 }),
489 }),
490 ),
491 ),
492 ),
493 ),
494 ),
495 );
496
497 expect(liveAggregates).toMatchObject([{ aggregate: null }]);
498 expect(pushAggregates).toHaveLength(1);
499 expect(pushAggregates[0]?.aggregate).toMatchObject({
500 activeCount: 1,
501 activities: [
502 {
503 phase: "waiting_for_input",
504 status: "Input",
505 threadId: notificationState.threadId,
506 },
507 ],
508 });
509 expect(result.deliveries).toMatchObject([
510 {
511 deviceId: "device-1",
512 kind: "push_notification",
513 queued: true,
514 },
515 ]);
516 });
517 });
518
519 it.effect(
520 "does not build Live Activity aggregates for links with Live Activities disabled",
521 () => {
522 const notificationState: RelayAgentActivityState = {
523 ...state,
524 phase: "waiting_for_approval",
525 headline: "Needs approval",
526 };
527 const liveAggregates: Array<
528 Parameters<ApnsDeliveries.ApnsDeliveries["Service"]["sendForTarget"]>[0]
529 > = [];
530 const pushAggregates: Array<
531 Parameters<ApnsDeliveries.ApnsDeliveries["Service"]["sendPushNotificationForTarget"]>[0]
532 > = [];
533
534 return Effect.gen(function* () {
535 const result = yield* Effect.gen(function* () {
536 const publisher = yield* AgentActivityPublisher.AgentActivityPublisher;
537 return yield* publisher.publish({
538 environmentId: "env",
539 environmentPublicKey: "environment-public-key",
540 threadId: "thread",
541 state: notificationState,
542 });
543 }).pipe(
544 Effect.provide(
545 AgentActivityPublisher.layer.pipe(
546 Layer.provide(
547 Layer.mergeAll(
548 Layer.succeed(
549 AgentActivityRows.AgentActivityRows,
550 makeAgentActivityRows({
551 listForUser: () =>
552 Effect.succeed([
553 {
554 ...state,
555 environmentId: "other-env" as RelayAgentActivityState["environmentId"],
556 threadId: "other-thread" as RelayAgentActivityState["threadId"],
557 },
558 ]),
559 }),
560 ),
561 Layer.succeed(
562 EnvironmentLinks.EnvironmentLinks,
563 makeEnvironmentLinks({
564 listDeliveryUsersForEnvironment: () =>
565 Effect.succeed([
566 {
567 userId: "dev:julius",
568 notificationsEnabled: true,
569 liveActivitiesEnabled: false,
570 },
571 ]),
572 }),
573 ),
574 Layer.succeed(
575 LiveActivities.LiveActivities,
576 makeLiveActivities({
577 listTargets: () =>
578 Effect.succeed([
579 {
580 ...target("device-1"),
581 push_token: "apns-device-token",
582 push_to_start_token: "push-to-start-token",
583 },
584 ]),
585 }),
586 ),
587 Layer.succeed(
588 ApnsDeliveries.ApnsDeliveries,
589 makeApnsDeliveries({
590 sendForTarget: (input) =>
591 Effect.sync(() => {
592 liveAggregates.push(input);
593 return null;
594 }),
595 sendPushNotificationForTarget: (input) =>
596 Effect.sync(() => {
597 pushAggregates.push(input);
598 return {
599 deviceId: input.target.device_id,
600 kind: "push_notification",
601 ok: true,
602 queued: true,
603 apnsStatus: null,
604 apnsReason: null,
605 apnsId: null,
606 };
607 }),
608 }),
609 ),
610 ),
611 ),
612 ),
613 ),
614 );
615
616 expect(liveAggregates).toMatchObject([{ aggregate: null }]);
617 expect(pushAggregates).toHaveLength(1);
618 expect(pushAggregates[0]?.aggregate?.activities).toMatchObject([
619 {
620 environmentId: notificationState.environmentId,
621 threadId: notificationState.threadId,
622 phase: "waiting_for_approval",
623 },
624 ]);
625 expect(result.deliveries).toMatchObject([
626 {
627 deviceId: "device-1",
628 kind: "push_notification",
629 queued: true,
630 },
631 ]);
632 });
633 },
634 );
635});
636
637describe("isExpiredAgentActivityState", () => {
638 const hourMs = 60 * 60 * 1_000;
639
640 it("expires running rows after two hours without an update", () => {
641 expect(AgentActivityPublisher.isExpiredAgentActivityState(state, 2 * hourMs - 1)).toBe(false);
642 expect(AgentActivityPublisher.isExpiredAgentActivityState(state, 2 * hourMs + 1)).toBe(true);
643 });
644
645 it("keeps waiting rows for a day", () => {
646 const waiting: RelayAgentActivityState = { ...state, phase: "waiting_for_approval" };
647 expect(AgentActivityPublisher.isExpiredAgentActivityState(waiting, 23 * hourMs)).toBe(false);
648 expect(AgentActivityPublisher.isExpiredAgentActivityState(waiting, 25 * hourMs)).toBe(true);
649 });
650
651 it("treats rows with unparseable timestamps as expired", () => {
652 expect(
653 AgentActivityPublisher.isExpiredAgentActivityState({ ...state, updatedAt: "not-a-date" }, 0),
654 ).toBe(true);
655 });
656});
657
658describe("makeAggregateState", () => {
659 const hourMs = 60 * 60 * 1_000;
660
661 it("drops expired rows from the aggregate", () => {
662 const fresh: RelayAgentActivityState = {
663 ...state,
664 threadId: "thread-fresh" as RelayAgentActivityState["threadId"],
665 updatedAt: "1970-01-01T03:00:00.000Z",
666 };
667 const aggregate = AgentActivityPublisher.makeAggregateState({
668 activeStates: [state, fresh],
669 terminalState: null,
670 nowMs: 3 * hourMs,
671 });
672
673 expect(aggregate?.activeCount).toBe(1);
674 expect(aggregate?.activities).toMatchObject([{ threadId: "thread-fresh" }]);
675 });
676
677 it("returns null when every row has expired and nothing terminal remains", () => {
678 expect(
679 AgentActivityPublisher.makeAggregateState({
680 activeStates: [state],
681 terminalState: null,
682 nowMs: 3 * hourMs,
683 }),
684 ).toBeNull();
685 });
686
687 it("still reports the terminal state when active rows have expired", () => {
688 const terminalState: RelayAgentActivityState = {
689 ...state,
690 phase: "completed",
691 updatedAt: "1970-01-01T03:00:00.000Z",
692 };
693 const aggregate = AgentActivityPublisher.makeAggregateState({
694 activeStates: [state],
695 terminalState,
696 nowMs: 3 * hourMs,
697 });
698
699 expect(aggregate?.activeCount).toBe(0);
700 expect(aggregate?.activities).toMatchObject([{ phase: "completed" }]);
701 });
702
703 it("keeps a recently finished thread visible as Done beside active agents", () => {
704 const active: RelayAgentActivityState = {
705 ...state,
706 threadId: "thread-active" as RelayAgentActivityState["threadId"],
707 updatedAt: "1970-01-01T00:58:00.000Z",
708 };
709 const justCompleted: RelayAgentActivityState = {
710 ...state,
711 threadId: "thread-done" as RelayAgentActivityState["threadId"],
712 phase: "completed",
713 updatedAt: "1970-01-01T00:59:00.000Z",
714 };
715 const aggregate = AgentActivityPublisher.makeAggregateState({
716 activeStates: [active, justCompleted],
717 terminalState: null,
718 nowMs: hourMs,
719 });
720
721 expect(aggregate?.activeCount).toBe(1);
722 expect(aggregate?.subtitle).toBe("Agent work in progress");
723 expect(aggregate?.activities).toMatchObject([
724 { threadId: "thread-active", phase: "running" },
725 { threadId: "thread-done", phase: "completed", status: "Done" },
726 ]);
727 expect(aggregate?.updatedAt).toBe("1970-01-01T00:59:00.000Z");
728 });
729
730 it("drops finished threads from the aggregate after the display window", () => {
731 const active: RelayAgentActivityState = {
732 ...state,
733 threadId: "thread-active" as RelayAgentActivityState["threadId"],
734 updatedAt: "1970-01-01T00:58:00.000Z",
735 };
736 const staleCompleted: RelayAgentActivityState = {
737 ...state,
738 threadId: "thread-done" as RelayAgentActivityState["threadId"],
739 phase: "completed",
740 updatedAt: "1970-01-01T00:44:00.000Z",
741 };
742 const aggregate = AgentActivityPublisher.makeAggregateState({
743 activeStates: [active, staleCompleted],
744 terminalState: null,
745 nowMs: hourMs,
746 });
747
748 expect(aggregate?.activities).toMatchObject([{ threadId: "thread-active" }]);
749 });
750
751 it("keeps showing recently finished work when nothing is active", () => {
752 const lingeringCompleted: RelayAgentActivityState = {
753 ...state,
754 phase: "completed",
755 updatedAt: "1970-01-01T00:59:00.000Z",
756 };
757 const aggregate = AgentActivityPublisher.makeAggregateState({
758 activeStates: [lingeringCompleted],
759 terminalState: null,
760 nowMs: hourMs,
761 });
762
763 // An armed card never renders an empty state: recently finished threads
764 // keep Done content on it, and once they age out the aggregate becomes
765 // null and the delivery layer ends the card.
766 expect(aggregate).toMatchObject({
767 activeCount: 0,
768 subtitle: "Agent work completed",
769 activities: [{ phase: "completed", status: "Done" }],
770 });
771 expect(
772 AgentActivityPublisher.makeAggregateState({
773 activeStates: [{ ...lingeringCompleted, updatedAt: "1970-01-01T00:44:00.000Z" }],
774 terminalState: null,
775 nowMs: hourMs,
776 }),
777 ).toBeNull();
778 });
779
780 it("gives active agents the display slots before finished ones", () => {
781 const mkActive = (id: string): RelayAgentActivityState => ({
782 ...state,
783 threadId: id as RelayAgentActivityState["threadId"],
784 updatedAt: "1970-01-01T00:58:00.000Z",
785 });
786 const justCompleted: RelayAgentActivityState = {
787 ...state,
788 threadId: "thread-done" as RelayAgentActivityState["threadId"],
789 phase: "completed",
790 updatedAt: "1970-01-01T00:59:00.000Z",
791 };
792 const aggregate = AgentActivityPublisher.makeAggregateState({
793 activeStates: [
794 mkActive("a-1"),
795 mkActive("a-2"),
796 mkActive("a-3"),
797 mkActive("a-4"),
798 mkActive("a-5"),
799 justCompleted,
800 ],
801 terminalState: null,
802 nowMs: hourMs,
803 });
804
805 expect(aggregate?.activeCount).toBe(5);
806 expect(aggregate?.activities).toMatchObject([
807 { threadId: "a-1" },
808 { threadId: "a-2" },
809 { threadId: "a-3" },
810 { threadId: "a-4" },
811 { threadId: "a-5" },
812 ]);
813 });
814});