|
| 1 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
1 | 2 | import type { |
2 | 3 | RelayAgentActivityAggregateState, |
3 | 4 | RelayAgentActivityState, |
@@ -602,34 +603,89 @@ describe("ApnsDeliveries", () => { |
602 | 603 | }, |
603 | 604 | ); |
604 | 605 |
|
605 | | - it.effect( |
606 | | - "throttles updates for changed aggregates with stable counts and no pending attention", |
607 | | - () => { |
608 | | - const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = []; |
609 | | - const queuedJobs: Array<SignedApnsDeliveryJob> = []; |
610 | | - const changedAggregate: RelayAgentActivityAggregateState = { |
611 | | - ...aggregate, |
612 | | - updatedAt: "1970-01-01T00:00:04.000Z", |
613 | | - }; |
614 | | - const previousAggregateJson = JSON.stringify(aggregate); |
| 606 | + it.effect.each([ |
| 607 | + { |
| 608 | + label: "connecting becomes working", |
| 609 | + previousRows: [{ ...aggregate.activities[0]!, phase: "starting", status: "Connecting" }], |
| 610 | + }, |
| 611 | + { |
| 612 | + label: "input resumes working", |
| 613 | + previousRows: [{ ...aggregate.activities[0]!, phase: "waiting_for_input", status: "Input" }], |
| 614 | + }, |
| 615 | + { |
| 616 | + label: "a settled terminal row leaves", |
| 617 | + previousRows: [ |
| 618 | + aggregate.activities[0]!, |
| 619 | + { |
| 620 | + ...aggregate.activities[0]!, |
| 621 | + threadId: ThreadId.make("finished"), |
| 622 | + phase: "completed", |
| 623 | + status: "Done", |
| 624 | + }, |
| 625 | + ], |
| 626 | + }, |
| 627 | + { |
| 628 | + label: "the same thread id moves to another environment", |
| 629 | + previousRows: [ |
| 630 | + { ...aggregate.activities[0]!, environmentId: EnvironmentId.make("other-env") }, |
| 631 | + ], |
| 632 | + }, |
| 633 | + ] as const)("queues an update inside the throttle window when $label", ({ previousRows }) => { |
| 634 | + const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = []; |
| 635 | + const queuedJobs: Array<SignedApnsDeliveryJob> = []; |
| 636 | + const previousAggregate: RelayAgentActivityAggregateState = { |
| 637 | + ...aggregate, |
| 638 | + activities: previousRows, |
| 639 | + }; |
615 | 640 |
|
616 | | - return Effect.gen(function* () { |
617 | | - const deliveries = yield* ApnsDeliveries.ApnsDeliveries; |
618 | | - const result = yield* deliveries.sendForTarget({ |
619 | | - target: { |
620 | | - ...target, |
621 | | - last_aggregate_json: previousAggregateJson, |
622 | | - last_live_activity_delivery_at: "1970-01-01T00:00:04.000Z", |
623 | | - }, |
624 | | - aggregate: changedAggregate, |
625 | | - nowMs: 5_000, |
626 | | - }); |
| 641 | + return Effect.gen(function* () { |
| 642 | + const deliveries = yield* ApnsDeliveries.ApnsDeliveries; |
| 643 | + const result = yield* deliveries.sendForTarget({ |
| 644 | + target: { |
| 645 | + ...target, |
| 646 | + last_aggregate_json: JSON.stringify(previousAggregate), |
| 647 | + last_live_activity_delivery_at: "1970-01-01T00:00:04.000Z", |
| 648 | + }, |
| 649 | + aggregate, |
| 650 | + nowMs: 5_000, |
| 651 | + }); |
627 | 652 |
|
628 | | - expect(result).toBeNull(); |
629 | | - expect(queuedJobs).toEqual([]); |
630 | | - }).pipe(Effect.provide(layerFor({ attempts, queuedJobs }))); |
631 | | - }, |
632 | | - ); |
| 653 | + expect(result?.kind).toBe("live_activity_update"); |
| 654 | + expect(queuedJobs).toMatchObject([{ payload: { kind: "live_activity_update", aggregate } }]); |
| 655 | + expect(queuedJobs[0]?.payload.alert ?? null).toBeNull(); |
| 656 | + expect(attempts).toEqual([]); |
| 657 | + }).pipe(Effect.provide(layerFor({ attempts, queuedJobs }))); |
| 658 | + }); |
| 659 | + |
| 660 | + it.effect.each([ |
| 661 | + { label: "inside", nowMs: 5_000, queued: false }, |
| 662 | + { label: "after", nowMs: 19_000, queued: true }, |
| 663 | + ])("throttles timestamp-only updates $label the window", ({ nowMs, queued }) => { |
| 664 | + const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = []; |
| 665 | + const queuedJobs: Array<SignedApnsDeliveryJob> = []; |
| 666 | + const changedAggregate: RelayAgentActivityAggregateState = { |
| 667 | + ...aggregate, |
| 668 | + updatedAt: "1970-01-01T00:00:04.000Z", |
| 669 | + activities: [{ ...aggregate.activities[0]!, updatedAt: "1970-01-01T00:00:04.000Z" }], |
| 670 | + }; |
| 671 | + const previousAggregateJson = JSON.stringify(aggregate); |
| 672 | + |
| 673 | + return Effect.gen(function* () { |
| 674 | + const deliveries = yield* ApnsDeliveries.ApnsDeliveries; |
| 675 | + const result = yield* deliveries.sendForTarget({ |
| 676 | + target: { |
| 677 | + ...target, |
| 678 | + last_aggregate_json: previousAggregateJson, |
| 679 | + last_live_activity_delivery_at: "1970-01-01T00:00:04.000Z", |
| 680 | + }, |
| 681 | + aggregate: changedAggregate, |
| 682 | + nowMs, |
| 683 | + }); |
| 684 | + |
| 685 | + expect(result?.kind ?? null).toBe(queued ? "live_activity_update" : null); |
| 686 | + expect(queuedJobs).toHaveLength(queued ? 1 : 0); |
| 687 | + }).pipe(Effect.provide(layerFor({ attempts, queuedJobs }))); |
| 688 | + }); |
633 | 689 |
|
634 | 690 | it.effect("queues an end for an active Live Activity when Live Activities are disabled", () => { |
635 | 691 | const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = []; |
|
0 commit comments