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

ProjectionThreadActivities.test.ts

100 lines3.5 KB

1import { EventId, ThreadId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as SqlClient from "effect/unstable/sql/SqlClient";
6​
7import { ProjectionThreadActivityRepository } from "../Services/ProjectionThreadActivities.ts";
8import { ProjectionThreadActivityRepositoryLive } from "./ProjectionThreadActivities.ts";
9import { SqlitePersistenceMemory } from "./Sqlite.ts";
10​
11const layer = it.layer(
12 ProjectionThreadActivityRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
13);
14​
15layer("ProjectionThreadActivityRepository", (it) => {
16 it.effect("reads only the latest matching task activity", () =>
17 Effect.gen(function* () {
18 const repository = yield* ProjectionThreadActivityRepository;
19 const sql = yield* SqlClient.SqlClient;
20 const threadId = ThreadId.make("thread-latest-task-activity");
21​
22 yield* sql`
23 INSERT INTO projection_thread_activities (
24 activity_id, thread_id, turn_id, tone, kind, summary, payload_json, sequence, created_at
25 )
26 VALUES
27 (
28 'activity-task-unrelated-tool', ${threadId}, NULL, 'tool', 'tool.completed',
29 'large tool output', 'not-json', 1, '2026-03-01T00:00:00.000Z'
30 ),
31 (
32 'activity-task-started', ${threadId}, NULL, 'info', 'task.started',
33 'started', '{"taskId":"task-1","title":"Initial title"}', 2,
34 '2026-03-01T00:00:01.000Z'
35 ),
36 (
37 'activity-task-progress', ${threadId}, NULL, 'info', 'task.progress',
38 'progress', '{"taskId":"task-1","title":"Updated title"}', 3,
39 '2026-03-01T00:00:02.000Z'
40 ),
41 (
42 'activity-task-other', ${threadId}, NULL, 'info', 'task.progress',
43 'other', '{"taskId":"task-2","title":"Other title"}', 4,
44 '2026-03-01T00:00:03.000Z'
45 )
46 `;
47​
48 yield* repository.upsert({
49 activityId: EventId.make("activity-task-untitled"),
50 threadId,
51 turnId: null,
52 tone: "info",
53 kind: "task.progress",
54 summary: "Still running",
55 payload: { taskId: "task-1" },
56 sequence: 5,
57 createdAt: "2026-03-01T00:00:04.000Z",
58 });
59 yield* repository.upsert({
60 activityId: EventId.make("activity-task-blank-title"),
61 threadId,
62 turnId: null,
63 tone: "info",
64 kind: "task.progress",
65 summary: "Still running",
66 payload: { taskId: "task-1", title: " \t\n\u00a0" },
67 sequence: 6,
68 createdAt: "2026-03-01T00:00:05.000Z",
69 });
70​
71 const recent = yield* repository.listByThreadId({
72 threadId,
73 activityKinds: ["task.progress"],
74 limit: 2,
75 });
76 assert.deepEqual(
77 recent.map((entry) => entry.activityId),
78 ["activity-task-untitled", "activity-task-blank-title"],
79 );
80​
81 const activity = yield* repository.getLatestTaskActivity({
82 threadId,
83 taskId: "task-1",
84 });
85 assert.equal(activity._tag, "Some");
86 if (activity._tag === "Some") {
87 assert.equal(activity.value.activityId, EventId.make("activity-task-progress"));
88 assert.deepEqual(activity.value.payload, {
89 taskId: "task-1",
90 title: "Updated title",
91 });
92 }
93​
94 assert.equal(
95 (yield* repository.getLatestTaskActivity({ threadId, taskId: "missing" }))._tag,
96 "None",
97 );
98 }),
99 );
100});