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

UserFacingErrors.test.ts

163 lines7.2 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import * as SqlClient from "effect/sql/SqlClient";
8import { SqlError, classifySqliteError } from "effect/sql/SqlError";
9​
10import { toPersistenceSqlError } from "../persistence/Errors.ts";
11import { userFacingDispatchErrorMessage } from "./UserFacingErrors.ts";
12​
13describe("userFacingDispatchErrorMessage", () => {
14 it("returns the deepest actionable domain error instead of generic dispatch wrappers", () => {
15 const message = userFacingDispatchErrorMessage({
16 message: "Failed to dispatch orchestration command checkpoint.rollback (command-1).",
17 cause: {
18 message: "Provider adapter failed while dispatching orchestration command command-1.",
19 cause: {
20 message:
21 "claudeAgent cannot satisfy rollback for command command-1: provider conversation rollback is unavailable",
22 },
23 },
24 });
25​
26 assert.equal(
27 message,
28 "claudeAgent cannot satisfy rollback for command command-1: provider conversation rollback is unavailable",
29 );
30 });
31​
32 it("translates policy capability rejections into provider-named prose", () => {
33 assert.equal(
34 userFacingDispatchErrorMessage({
35 message: "Failed to dispatch orchestration command checkpoint.rollback (command-1).",
36 cause: {
37 _tag: "CommandPolicyCapabilityUnsupportedError",
38 commandId: "command-1",
39 threadId: "thread-1",
40 providerInstanceId: "pi",
41 capability: "rollback_snapshot",
42 detail: "rollback must return a provider thread snapshot",
43 message:
44 "pi cannot satisfy rollback_snapshot for command command-1: rollback must return a provider thread snapshot",
45 },
46 }),
47 "Pi did not report its rewound conversation state, so the checkpoint was not restored.",
48 );
49 assert.equal(
50 userFacingDispatchErrorMessage({
51 message: "Failed to dispatch orchestration command checkpoint.rollback (command-2).",
52 cause: {
53 _tag: "CommandPolicyCapabilityUnsupportedError",
54 commandId: "command-2",
55 threadId: "thread-1",
56 providerInstanceId: "grok",
57 capability: "rollback",
58 detail: "provider conversation rollback is unavailable",
59 },
60 }),
61 "Grok cannot rewind its conversation, so this checkpoint cannot be restored on this thread.",
62 );
63 });
64​
65 it("uses explicit detail fields as user-facing messages", () => {
66 assert.equal(
67 userFacingDispatchErrorMessage({
68 message: "Failed to dispatch orchestration command message.dispatch (command-1).",
69 cause: {
70 detail: "Claude provider thread provider-thread-1 has no live query.",
71 },
72 }),
73 "Claude provider thread provider-thread-1 has no live query.",
74 );
75 });
76​
77 it.effect("explains a real database open failure through dispatch and persistence wrappers", () =>
78 Effect.gen(function* () {
79 const fs = yield* FileSystem.FileSystem;
80 const path = yield* Path.Path;
81 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-database-error-" });
82 const cause = yield* Effect.void.pipe(
83 Effect.provide(
84 NodeSqliteClient.layer({ filename: path.join(directory, "missing", "state.sqlite") }),
85 ),
86 Effect.flip,
87 );
88 const message = userFacingDispatchErrorMessage({
89 message: "Failed to dispatch orchestration command message.dispatch (command-1).",
90 cause: toPersistenceSqlError("OrchestrationEventStore.append:query")(cause),
91 });
92​
93 assert.include(message, "server could not open a database file (SQLITE_CANTOPEN)");
94 assert.include(message, "machine running the server");
95 assert.include(message, "free disk space");
96 assert.include(message, "data and temporary directories exist and are writable");
97 assert.include(message, "then retry");
98 assert.notInclude(message, directory);
99 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
100 );
101​
102 it.effect("explains a full database and allows a retry after capacity is restored", () =>
103 Effect.gen(function* () {
104 const sql = yield* SqlClient.SqlClient;
105 yield* sql`CREATE TABLE entries (payload BLOB)`;
106 yield* sql`PRAGMA max_page_count = 2`;
107 const cause = yield* sql`INSERT INTO entries VALUES (zeroblob(16384))`.pipe(Effect.flip);
108 const message = userFacingDispatchErrorMessage({ cause });
109​
110 assert.include(message, "database or disk is full (SQLITE_FULL)");
111 assert.include(message, "Free space on the machine running the server, then retry");
112 assert.include(message, "database size limit");
113​
114 yield* sql`PRAGMA max_page_count = 100`;
115 yield* sql`INSERT INTO entries VALUES (zeroblob(16384))`;
116 const rows = yield* sql<{ count: number }>`SELECT COUNT(*) AS count FROM entries`;
117 assert.equal(rows[0]?.count, 1);
118 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
119 );
120​
121 it.effect("gives server permission guidance for a real read-only failure", () =>
122 Effect.gen(function* () {
123 const sql = yield* SqlClient.SqlClient;
124 yield* sql`CREATE TABLE entries (payload TEXT)`;
125 yield* sql`PRAGMA query_only = ON`;
126 const cause = yield* sql`INSERT INTO entries VALUES ('private-message')`.pipe(Effect.flip);
127 const message = userFacingDispatchErrorMessage(cause);
128​
129 assert.include(message, "server database is read-only (SQLITE_READONLY)");
130 assert.include(message, "data directory is writable");
131 assert.notInclude(message, "private-message");
132 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
133 );
134​
135 it.each([
136 [5, "SQLITE_BUSY", "Wait a moment, then retry"],
137 [6, "SQLITE_LOCKED", "Wait a moment, then retry"],
138 [10 | (3 << 8), "SQLITE_IOERR", "free disk space and disk health"],
139 [14 | (6 << 8), "SQLITE_CANTOPEN", "exist and are writable"],
140 [11, "SQLITE_CORRUPT", "Preserve the server data directory"],
141 [26, "SQLITE_NOTADB", "configured database path"],
142 ] as const)("maps SQLite code %i to %s with recovery guidance", (errcode, code, guidance) => {
143 const cause = Object.assign(new Error("private-query-data"), { errcode, errno: errcode });
144 const error = new SqlError({ reason: classifySqliteError(cause) });
145 const message = userFacingDispatchErrorMessage({ cause: error });
146​
147 assert.include(message, code);
148 assert.include(message, guidance);
149 assert.notInclude(message, "private-query-data");
150 });
151​
152 it("keeps unknown SQL failures contextual without exposing driver text or parameters", () => {
153 const cause = Object.assign(new Error("private-query-data"), { errcode: 1 });
154 const error = new SqlError({ reason: classifySqliteError(cause) });
155 const message = userFacingDispatchErrorMessage({
156 cause: toPersistenceSqlError("OrchestrationEventStore.append:query")(error),
157 });
158​
159 assert.include(message, "server could not complete this database request");
160 assert.include(message, "server logs");
161 assert.notInclude(message, "private-query-data");
162 });
163});