Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

LiveActivities.test.ts

140 lines4.8 KB

1import type {
2 RelayAgentActivityAggregateState,
3 RelayLiveActivityRegistrationRequest,
4} from "@t3tools/contracts/relay";
5import { describe, expect, it } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8​
9import * as RelayDb from "../db.ts";
10import { relayLiveActivities } from "../persistence/schema.ts";
11import * as LiveActivities from "./LiveActivities.ts";
12​
13const aggregate: RelayAgentActivityAggregateState = {
14 title: "T3 Code",
15 subtitle: "Agent work in progress",
16 activeCount: 1,
17 updatedAt: "2026-05-25T00:00:00.000Z",
18 activities: [
19 {
20 environmentId:
21 "env" as RelayAgentActivityAggregateState["activities"][number]["environmentId"],
22 threadId: "thread" as RelayAgentActivityAggregateState["activities"][number]["threadId"],
23 projectTitle: "Project",
24 threadTitle: "Thread",
25 modelTitle: "gpt-5.4",
26 phase: "running",
27 status: "Working",
28 updatedAt: "2026-05-25T00:00:00.000Z",
29 deepLink: "/threads/env/thread",
30 },
31 ],
32};
33​
34describe("LiveActivities", () => {
35 it.effect("keeps the arming time when the current card re-registers its token", () => {
36 const armedAt = "2026-05-25T00:00:00.000Z";
37 const insertedValues: Array<Record<string, unknown>> = [];
38 const conflictSets: Array<Record<string, unknown> | undefined> = [];
39 const fakeDb = {
40 select: () => ({
41 from: () => ({
42 where: () =>
43 Effect.succeed([{ activityPushToken: "current-token", remoteStartedAt: armedAt }]),
44 }),
45 }),
46 update: () => ({ set: () => ({ where: () => Effect.void }) }),
47 insert: () => ({
48 values: (values: Record<string, unknown>) => {
49 insertedValues.push(values);
50 return {
51 onConflictDoUpdate: (config: { readonly set?: Record<string, unknown> }) => {
52 conflictSets.push(config.set);
53 return Effect.void;
54 },
55 };
56 },
57 }),
58 } as unknown as RelayDb.RelayDb["Service"];
59 const register = (activityPushToken: string) =>
60 Effect.flatMap(LiveActivities.LiveActivities, (liveActivities) =>
61 liveActivities.register({
62 userId: "user-1",
63 registration: {
64 deviceId: "device-1" as RelayLiveActivityRegistrationRequest["deviceId"],
65 activityPushToken:
66 activityPushToken as RelayLiveActivityRegistrationRequest["activityPushToken"],
67 },
68 }),
69 );
70​
71 return Effect.gen(function* () {
72 yield* register("current-token");
73 yield* register("new-card-token");
74​
75 // The foreground refresh still clears the delivered baseline so the
76 // replay repaints the card, but only a new card counts as freshly armed.
77 expect(insertedValues[0]).toMatchObject({ remoteStartedAt: armedAt });
78 expect(conflictSets[0]).toMatchObject({ remoteStartedAt: armedAt, lastAggregateJson: null });
79 expect(insertedValues[1]?.remoteStartedAt).not.toBe(armedAt);
80 expect(conflictSets[1]?.remoteStartedAt).not.toBe(armedAt);
81 }).pipe(
82 Effect.provide(
83 LiveActivities.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
84 ),
85 );
86 });
87​
88 it.effect("preserves ended state when a delayed update delivery is marked", () => {
89 const insertedValues: Array<Record<string, unknown>> = [];
90 const conflictConfigs: Array<{
91 readonly set?: Record<string, unknown>;
92 }> = [];
93​
94 const fakeDb = {
95 insert: (table: unknown) => {
96 expect(table).toBe(relayLiveActivities);
97 return {
98 values: (values: Record<string, unknown>) => {
99 insertedValues.push(values);
100 return {
101 onConflictDoUpdate: (config: { readonly set?: Record<string, unknown> }) => {
102 conflictConfigs.push(config);
103 return Effect.void;
104 },
105 };
106 },
107 };
108 },
109 } as unknown as RelayDb.RelayDb["Service"];
110​
111 return Effect.gen(function* () {
112 const liveActivities = yield* LiveActivities.LiveActivities;
113 yield* liveActivities.markDelivery({
114 userId: "user-2",
115 deviceId: "device-1",
116 kind: "live_activity_update",
117 aggregate,
118 deliveredAt: "2026-05-25T00:00:10.000Z",
119 });
120​
121 expect(insertedValues).toEqual([
122 expect.objectContaining({
123 userId: "user-2",
124 deviceId: "device-1",
125 endedAt: null,
126 }),
127 ]);
128 expect(conflictConfigs[0]?.set).toEqual(
129 expect.objectContaining({
130 endedAt: relayLiveActivities.endedAt,
131 lastLiveActivityDeliveryAt: "2026-05-25T00:00:10.000Z",
132 }),
133 );
134 }).pipe(
135 Effect.provide(
136 LiveActivities.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
137 ),
138 );
139 });
140});