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

AgentActivityRows.test.ts

93 lines2.9 KB

1import type { RelayAgentActivityState } 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 RelayDb from "../db.ts";
7import * as AgentActivityRows from "./AgentActivityRows.ts";
8​
9const state: RelayAgentActivityState = {
10 environmentId: "env-1" as RelayAgentActivityState["environmentId"],
11 threadId: "thread-1" as RelayAgentActivityState["threadId"],
12 projectTitle: "Project",
13 threadTitle: "Thread",
14 modelTitle: "gpt-5.4",
15 phase: "running",
16 headline: "Running",
17 updatedAt: "2026-06-20T00:00:00.000Z",
18 deepLink: "/threads/env-1/thread-1",
19};
20​
21describe("AgentActivityRows", () => {
22 it.effect("preserves activity context on persistence failures", () => {
23 const cause = new Error("database unavailable");
24 const failingDb = {
25 insert: () => ({
26 values: () => ({
27 onConflictDoUpdate: () => Effect.fail(cause),
28 }),
29 }),
30 delete: () => ({
31 where: () => Effect.fail(cause),
32 }),
33 select: () => ({
34 from: () => ({
35 innerJoin: () => ({
36 where: () => ({
37 orderBy: () => Effect.fail(cause),
38 }),
39 }),
40 }),
41 }),
42 } as unknown as RelayDb.RelayDb["Service"];
43​
44 return Effect.gen(function* () {
45 const rows = yield* AgentActivityRows.AgentActivityRows;
46​
47 const upsertError = yield* rows
48 .upsert({ environmentPublicKey: "public-key", state })
49 .pipe(Effect.flip);
50 expect(upsertError).toMatchObject({
51 environmentId: "env-1",
52 threadId: "thread-1",
53 cause,
54 });
55 expect(upsertError.message).toBe(
56 "Failed to persist agent activity state for environment env-1, thread thread-1.",
57 );
58​
59 const deleteError = yield* rows
60 .remove({
61 environmentId: "env-1",
62 environmentPublicKey: "public-key",
63 threadId: "thread-1",
64 })
65 .pipe(Effect.flip);
66 expect(deleteError).toMatchObject({
67 environmentId: "env-1",
68 threadId: "thread-1",
69 cause,
70 });
71 expect(deleteError.message).toBe(
72 "Failed to delete agent activity state for environment env-1, thread thread-1.",
73 );
74​
75 const listError = yield* rows.listForUser({ userId: "user-2" }).pipe(Effect.flip);
76 expect(listError).toMatchObject({ userId: "user-2", cause });
77 expect(listError.message).toBe("Failed to list agent activity state for user user-2.");
78​
79 const getError = yield* rows
80 .getForUserThread({
81 userId: "user-2",
82 environmentId: "env-1",
83 threadId: "thread-1",
84 })
85 .pipe(Effect.flip);
86 expect(getError).toMatchObject({ userId: "user-2", cause });
87 }).pipe(
88 Effect.provide(
89 AgentActivityRows.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, failingDb))),
90 ),
91 );
92 });
93});