-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.test.ts
More file actions
351 lines (316 loc) · 11.1 KB
/
Copy pathclient.test.ts
File metadata and controls
351 lines (316 loc) · 11.1 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { describe, expect, test, vi } from "vitest"
import {
actionToolNames,
allCustomerToolNames,
analyticalAgentToolNames,
type CustomerContextSearchInput,
createOutlitClient,
customerSourceTypeInputs,
customerSourceTypes,
defaultAgentToolNames,
getCustomerToolContract,
normalizeCustomerSourceType,
notificationSeverityValues,
resolveCustomerContextSearchInput,
sqlToolNames,
} from "../src/index.js"
describe("toolsets", () => {
test("exposes the notification action toolset", () => {
expect(actionToolNames).toEqual(["outlit_send_notification"])
})
test("keeps SQL out of the default agent toolset", () => {
expect(defaultAgentToolNames).toContain("outlit_search_customer_context")
expect(defaultAgentToolNames).toContain("outlit_get_customer")
expect(defaultAgentToolNames).toContain("outlit_send_notification")
expect(defaultAgentToolNames).not.toContain("outlit_query")
expect(defaultAgentToolNames).not.toContain("outlit_schema")
expect(sqlToolNames).toEqual(["outlit_schema", "outlit_query"])
expect(allCustomerToolNames).toContain("outlit_query")
expect(allCustomerToolNames).toContain("outlit_send_notification")
})
test("exposes an analytical agent toolset with only default tools plus SQL", () => {
expect(analyticalAgentToolNames).toEqual([...defaultAgentToolNames, ...sqlToolNames])
expect(analyticalAgentToolNames).toContain("outlit_send_notification")
expect(analyticalAgentToolNames).toContain("outlit_schema")
expect(analyticalAgentToolNames).toContain("outlit_query")
expect(allCustomerToolNames).toContain("outlit_send_notification")
expect(allCustomerToolNames).toContain("outlit_schema")
expect(allCustomerToolNames).toContain("outlit_query")
})
})
describe("tool contracts", () => {
test("exposes fact type and category filters on facts listing", () => {
const contract = getCustomerToolContract("outlit_list_facts")
const properties = contract.inputSchema.properties as Record<string, unknown>
expect(properties.factTypes).toEqual(
expect.objectContaining({
type: "array",
items: expect.objectContaining({
enum: expect.arrayContaining(["CHURN_RISK", "EXPANSION", "CHAMPION_RISK"]),
}),
}),
)
expect(properties.factCategories).toEqual(
expect.objectContaining({
type: "array",
items: expect.objectContaining({
enum: ["MEMORY", "CUSTOM"],
}),
}),
)
})
test("keeps anomaly detector filters out of the facts listing schema", () => {
const contract = getCustomerToolContract("outlit_list_facts")
const properties = contract.inputSchema.properties as Record<
string,
{ items?: { enum?: string[] } }
>
expect(properties.factTypes?.items?.enum).not.toContain("CORE_ACTION_DECAY")
expect(properties.factTypes?.items?.enum).not.toContain("ACTIVATION_RATE_DROP")
expect(properties.factTypes?.items?.enum).not.toContain("CHAMPION_AT_RISK")
expect(properties.factCategories?.items?.enum).not.toContain("CHURN")
expect(properties.factCategories?.items?.enum).not.toContain("JOURNEY")
})
test("exposes opportunity as the canonical CRM source type with aliases for inputs", () => {
expect(customerSourceTypes).toEqual([
"EMAIL",
"CALL",
"CALENDAR_EVENT",
"SUPPORT_TICKET",
"OPPORTUNITY",
])
expect(customerSourceTypeInputs).toEqual([
"EMAIL",
"CALL",
"CALENDAR_EVENT",
"SUPPORT_TICKET",
"OPPORTUNITY",
"CRM",
"CRM_OPPORTUNITY",
])
expect(normalizeCustomerSourceType("CRM")).toBe("OPPORTUNITY")
expect(normalizeCustomerSourceType("CRM_OPPORTUNITY")).toBe("OPPORTUNITY")
expect(normalizeCustomerSourceType("crm")).toBe("OPPORTUNITY")
expect(normalizeCustomerSourceType(" crm_opportunity ")).toBe("OPPORTUNITY")
expect(normalizeCustomerSourceType(" opportunity ")).toBe("OPPORTUNITY")
expect(normalizeCustomerSourceType("ZENDESK_TICKET")).toBeNull()
expect(normalizeCustomerSourceType("toString")).toBeNull()
expect(normalizeCustomerSourceType("constructor")).toBeNull()
expect(normalizeCustomerSourceType("__proto__")).toBeNull()
const exactSourceContract = getCustomerToolContract("outlit_get_source")
const exactSourceProperties = exactSourceContract.inputSchema.properties as Record<
string,
{ enum?: string[] }
>
expect(exactSourceProperties.sourceType?.enum).toEqual(customerSourceTypeInputs)
const searchContract = getCustomerToolContract("outlit_search_customer_context")
const searchProperties = searchContract.inputSchema.properties as Record<
string,
{ items?: { enum?: string[] } }
>
expect(searchProperties.sourceTypes?.items?.enum).toEqual(customerSourceTypeInputs)
})
test("exposes the notification contract and severity values", () => {
const contract = getCustomerToolContract("outlit_send_notification")
const properties = contract.inputSchema.properties as Record<
string,
{
enum?: string[]
type?: string
items?: { format?: string; type?: string }
}
>
expect(contract.inputSchema.required).toEqual(["title"])
expect(contract.inputSchema.additionalProperties).toBe(false)
expect(properties.markdown).toEqual(
expect.objectContaining({
type: "string",
}),
)
expect(properties.destinations).toBeUndefined()
expect(properties.destinationIds).toEqual(
expect.objectContaining({
type: "array",
minItems: 1,
maxItems: 10,
items: expect.objectContaining({
type: "string",
format: "uuid",
}),
}),
)
expect(properties.severity).toEqual(
expect.objectContaining({
enum: ["low", "medium", "high"],
type: "string",
}),
)
expect(notificationSeverityValues).toEqual(["low", "medium", "high"])
})
})
describe("createOutlitClient", () => {
test("defaults to the hosted Outlit tool endpoint", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({ ok: true })))
const client = createOutlitClient({
apiKey: "ok_abcdefghijklmnopqrstuvwxyz123456",
fetch: fetchMock,
})
await client.callTool("outlit_list_customers", {})
expect(client.baseUrl).toBe("https://app.outlit.ai")
expect(fetchMock).toHaveBeenCalledWith(
"https://app.outlit.ai/api/tools/call",
expect.any(Object),
)
})
test("calls the public tool endpoint with the selected customer tool", async () => {
const fetchMock = vi
.fn()
.mockResolvedValue(
new Response(JSON.stringify({ items: [{ id: "cust_123" }] }), { status: 200 }),
)
const client = createOutlitClient({
apiKey: "ok_abcdefghijklmnopqrstuvwxyz123456",
baseUrl: "https://example.outlit.test",
fetch: fetchMock,
})
const result = await client.callTool("outlit_list_customers", { limit: 10 })
expect(result).toEqual({ items: [{ id: "cust_123" }] })
expect(fetchMock).toHaveBeenCalledWith("https://example.outlit.test/api/tools/call", {
method: "POST",
headers: {
Authorization: "Bearer ok_abcdefghijklmnopqrstuvwxyz123456",
"Content-Type": "application/json",
},
body: JSON.stringify({
tool: "outlit_list_customers",
input: { limit: 10 },
}),
})
})
test("calls the notification tool endpoint with notification input", async () => {
const fetchMock = vi
.fn()
.mockResolvedValue(new Response(JSON.stringify({ ok: true }), { status: 200 }))
const client = createOutlitClient({
apiKey: "ok_abcdefghijklmnopqrstuvwxyz123456",
baseUrl: "https://example.outlit.test",
fetch: fetchMock,
})
await client.callTool("outlit_send_notification", {
title: "Reminder",
markdown: "**Reminder**\n\n- Customer: Acme",
severity: "low",
destinationIds: ["00000000-0000-4000-8000-000000000001"],
})
expect(fetchMock).toHaveBeenCalledWith("https://example.outlit.test/api/tools/call", {
method: "POST",
headers: {
Authorization: "Bearer ok_abcdefghijklmnopqrstuvwxyz123456",
"Content-Type": "application/json",
},
body: JSON.stringify({
tool: "outlit_send_notification",
input: {
title: "Reminder",
markdown: "**Reminder**\n\n- Customer: Acme",
severity: "low",
destinationIds: ["00000000-0000-4000-8000-000000000001"],
},
}),
})
})
test("rejects unknown tool names at runtime", async () => {
const client = createOutlitClient({
apiKey: "ok_abcdefghijklmnopqrstuvwxyz123456",
fetch: vi.fn(),
})
// @ts-expect-error Runtime guard should still reject invalid external input.
await expect(client.callTool("outlit_connect_integration", {})).rejects.toThrow(
"Unknown customer tool",
)
})
})
describe("resolveCustomerContextSearchInput", () => {
test("allows a null customer filter to match the schema contract", () => {
const input: CustomerContextSearchInput = {
query: "churn risk",
customer: null,
}
expect(input.customer).toBeNull()
})
test("rejects malformed date filters", () => {
expect(
resolveCustomerContextSearchInput({
query: "churn risk",
after: "not-a-date",
}),
).toEqual({
ok: false,
message: "--after must be a valid ISO 8601 datetime",
})
expect(
resolveCustomerContextSearchInput({
query: "churn risk",
before: "still-not-a-date",
}),
).toEqual({
ok: false,
message: "--before must be a valid ISO 8601 datetime",
})
})
test("rejects date-only filters because the schema requires datetimes", () => {
expect(
resolveCustomerContextSearchInput({
query: "churn risk",
after: "2025-01-01",
}),
).toEqual({
ok: false,
message: "--after must be a valid ISO 8601 datetime",
})
})
test("normalizes CRM source aliases in search input", () => {
expect(
resolveCustomerContextSearchInput({
query: "renewal",
sourceTypes: ["CALL", "CRM"],
}),
).toEqual({
ok: true,
request: {
query: "renewal",
customer: undefined,
topK: undefined,
after: undefined,
before: undefined,
sourceTypes: ["CALL", "OPPORTUNITY"],
},
})
expect(
resolveCustomerContextSearchInput({
query: "renewal",
sourceTypes: [" call ", "crm_opportunity", "opportunity"],
}),
).toEqual({
ok: true,
request: {
query: "renewal",
customer: undefined,
topK: undefined,
after: undefined,
before: undefined,
sourceTypes: ["CALL", "OPPORTUNITY"],
},
})
expect(
resolveCustomerContextSearchInput({
query: "renewal",
sourceTypes: ["ZENDESK_TICKET"],
}),
).toEqual({
ok: false,
message:
"Unknown source types: ZENDESK_TICKET. Allowed: EMAIL, CALL, CALENDAR_EVENT, SUPPORT_TICKET, OPPORTUNITY, CRM, CRM_OPPORTUNITY",
})
})
})