-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-errors.test.ts
146 lines (130 loc) · 5.17 KB
/
common-errors.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {
SystemError,
PARSE_ERROR,
METHOD_NOT_FOUND,
INVALID_REQUEST,
INVALID_PARAMS,
INTERNAL_ERROR,
TASK_NOT_FOUND,
TASK_NOT_CANCELABLE,
UNSUPPORTED_OPERATION,
PUSH_NOTIFICATION_NOT_SUPPORTED,
FAILED_UPDATE,
ErrorCodeParseError,
ErrorCodeInvalidParams,
ErrorCodeInternalError,
ErrorCodeInvalidRequest,
ErrorCodeMethodNotFound,
ErrorCodeTaskNotFound,
ErrorCodeTaskNotCancelable,
ErrorCodeUnsupportedOperation,
ErrorCodePushNotificationNotSupported,
configureLogger,
} from "../src/index.js";
configureLogger({ level: "silent" });
describe("Error Handling Utilities", () => {
describe("SystemError", () => {
it("should create a SystemError with proper attributes", () => {
const error = new SystemError("System operation failed", -12345);
expect(error.name).toBe("RpcError");
expect(error.message).toBe("System operation failed");
expect(error.code).toBe(-12345);
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(SystemError);
});
it("should include optional data in error object", () => {
const data = { detail: "Additional error information" };
const error = new SystemError("Error with data", -32000, data);
expect(error.data).toEqual(data);
});
});
describe("Error Factories", () => {
it("should create Parse Error", () => {
const error = PARSE_ERROR();
expect(error).toBeInstanceOf(SystemError);
expect(error.code).toBe(ErrorCodeParseError);
expect(error.message).toBe("Invalid JSON payload");
});
it("should create Invalid Request Error", () => {
const error = INVALID_REQUEST();
expect(error).toBeInstanceOf(SystemError);
expect(error.code).toBe(ErrorCodeInvalidRequest);
expect(error.message).toBe("Request payload validation error");
});
it("should create Method Not Found Error", () => {
const error = METHOD_NOT_FOUND();
expect(error).toBeInstanceOf(SystemError);
expect(error.code).toBe(ErrorCodeMethodNotFound);
expect(error.message).toBe("Method not found");
});
it("should create Invalid Params Error", () => {
const error = INVALID_PARAMS();
expect(error).toBeInstanceOf(SystemError);
expect(error.code).toBe(ErrorCodeInvalidParams);
expect(error.message).toBe("Invalid parameters");
});
it("should create Internal Error", () => {
const error = INTERNAL_ERROR();
expect(error).toBeInstanceOf(SystemError);
expect(error.code).toBe(ErrorCodeInternalError);
expect(error.message).toBe("Internal error");
});
it("should create Task Not Found Error", () => {
const error = TASK_NOT_FOUND();
expect(error).toBeInstanceOf(SystemError);
expect(error.code).toBe(ErrorCodeTaskNotFound);
expect(error.message).toBe("Task not found");
});
it("should create Task Not Cancelable Error", () => {
const error = TASK_NOT_CANCELABLE();
expect(error).toBeInstanceOf(SystemError);
expect(error.code).toBe(ErrorCodeTaskNotCancelable);
expect(error.message).toBe("Task cannot be canceled");
});
it("should create Unsupported Operation Error", () => {
const error = UNSUPPORTED_OPERATION();
expect(error).toBeInstanceOf(SystemError);
expect(error.code).toBe(ErrorCodeUnsupportedOperation);
expect(error.message).toBe("This operation is not supported");
});
it("should create Push Notification Not Supported Error", () => {
const error = PUSH_NOTIFICATION_NOT_SUPPORTED();
expect(error).toBeInstanceOf(SystemError);
expect(error.code).toBe(ErrorCodePushNotificationNotSupported);
expect(error.message).toBe("Push Notification is not supported");
});
it("should include data in created error", () => {
const data = { field: "taskId", reason: "not found" };
const error = TASK_NOT_FOUND(data);
expect(error.data).toEqual(data);
});
});
describe("FAILED_UPDATE", () => {
it("should create a failed task update", () => {
const message = "Task execution failed";
const update = FAILED_UPDATE(message);
// Test the structure without relying on specific type
const failedUpdate = update as any;
expect(failedUpdate.state).toBe("failed");
expect(failedUpdate.message).toBeDefined();
expect(failedUpdate.message.role).toBe("agent");
expect(Array.isArray(failedUpdate.message.parts)).toBe(true);
expect(failedUpdate.message.parts).toHaveLength(1);
expect(failedUpdate.message.parts[0].type).toBe("text");
expect(failedUpdate.message.parts[0].text).toBe(message);
});
});
describe("Error Constants", () => {
it("should have the correct error code values", () => {
expect(ErrorCodeParseError).toBe(-32700);
expect(ErrorCodeInvalidParams).toBe(-32602);
expect(ErrorCodeInternalError).toBe(-32603);
expect(ErrorCodeInvalidRequest).toBe(-32600);
expect(ErrorCodeMethodNotFound).toBe(-32601);
expect(ErrorCodeTaskNotFound).toBe(-32001);
expect(ErrorCodeTaskNotCancelable).toBe(-32002);
expect(ErrorCodeUnsupportedOperation).toBe(-32004);
expect(ErrorCodePushNotificationNotSupported).toBe(-32003);
});
});
});