@slates/test 1.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts ADDED
@@ -0,0 +1,332 @@
1
+ import { createVitestConfig } from '@lowerdeck/testing-tools';
2
+ import {
3
+ createLocalSlateTransport,
4
+ createSlatesClient,
5
+ SlateProtocolError
6
+ } from '@slates/client';
7
+ import type { SlatesProtocolClientOptions } from '@slates/client';
8
+ import {
9
+ createSlatesClientFromProfile,
10
+ openSlatesCliStore,
11
+ SlatesProfileRecord
12
+ } from '@slates/profiles';
13
+ import { readFile } from 'fs/promises';
14
+
15
+ export interface SlatesRuntimeContext {
16
+ integration: string | null;
17
+ profileId: string | null;
18
+ profile: SlatesProfileRecord | null;
19
+ storePath: string;
20
+ cliDir: string;
21
+ }
22
+
23
+ export type SlatesTestClient = ReturnType<typeof createSlatesClient>;
24
+
25
+ type LocalSlate = Parameters<typeof createLocalSlateTransport>[0]['slate'];
26
+
27
+ export interface ExpectedSlateAction {
28
+ id: string;
29
+ name?: string;
30
+ description?: string;
31
+ readOnly?: boolean;
32
+ destructive?: boolean;
33
+ invocationType?: 'polling' | 'webhook';
34
+ }
35
+
36
+ let getExpect = () => {
37
+ let maybeExpect = (globalThis as typeof globalThis & { expect?: typeof import('vitest').expect })
38
+ .expect;
39
+
40
+ if (!maybeExpect) {
41
+ throw new Error('Vitest expect is not available in the current runtime.');
42
+ }
43
+
44
+ return maybeExpect;
45
+ };
46
+
47
+ export let loadSlatesRuntimeContext = async (
48
+ opts: {
49
+ cwd?: string;
50
+ profile?: string | null;
51
+ } = {}
52
+ ): Promise<SlatesRuntimeContext> => {
53
+ let runtimeContextPath = process.env.SLATES_TEST_CONTEXT_PATH;
54
+ if (runtimeContextPath) {
55
+ let raw = await readFile(runtimeContextPath, 'utf-8');
56
+ let parsed = JSON.parse(raw) as {
57
+ integration?: string | null;
58
+ profileId: string | null;
59
+ storePath: string;
60
+ cliDir: string;
61
+ };
62
+ let store = await openSlatesCliStore({ storePath: parsed.storePath });
63
+ let profile = store.getProfile(opts.profile ?? parsed.profileId ?? null);
64
+
65
+ return {
66
+ integration: parsed.integration ?? store.scope?.key ?? null,
67
+ profileId: profile?.id ?? parsed.profileId ?? null,
68
+ profile,
69
+ storePath: parsed.storePath,
70
+ cliDir: parsed.cliDir
71
+ };
72
+ }
73
+
74
+ let store = process.env.SLATES_STORE_PATH
75
+ ? await openSlatesCliStore({ storePath: process.env.SLATES_STORE_PATH })
76
+ : await openSlatesCliStore({ cwd: opts.cwd });
77
+ let profileId = opts.profile ?? process.env.SLATES_PROFILE_ID ?? null;
78
+ let profile = store.getProfile(profileId);
79
+
80
+ return {
81
+ integration: process.env.SLATES_INTEGRATION ?? store.scope?.key ?? null,
82
+ profileId: profile?.id ?? null,
83
+ profile,
84
+ storePath: store.storePath,
85
+ cliDir: store.dirPath
86
+ };
87
+ };
88
+
89
+ export let loadSlatesProfile = async (
90
+ opts: { cwd?: string; profile?: string | null } = {}
91
+ ) => {
92
+ let context = await loadSlatesRuntimeContext(opts);
93
+ if (!context.profile) {
94
+ throw new Error('No Slates profile is available for the current test context.');
95
+ }
96
+
97
+ return context.profile;
98
+ };
99
+
100
+ export let createSlatesTestClient = async (
101
+ opts: { cwd?: string; profile?: string | null } = {}
102
+ ) => {
103
+ let context = await loadSlatesRuntimeContext(opts);
104
+ if (!context.profile) {
105
+ throw new Error('No Slates profile is available for the current test context.');
106
+ }
107
+
108
+ let store = await openSlatesCliStore({ storePath: context.storePath });
109
+ return createSlatesClientFromProfile(context.profile, { cwd: opts.cwd, store });
110
+ };
111
+
112
+ export let withSlateProfile = async <T>(
113
+ profileName: string | null | undefined,
114
+ cb: (ctx: { profile: SlatesProfileRecord }) => Promise<T>
115
+ ) => {
116
+ let profile = await loadSlatesProfile({ profile: profileName });
117
+ return cb({ profile });
118
+ };
119
+
120
+ export let expectToolCall = async (d: {
121
+ client?: Awaited<ReturnType<typeof createSlatesTestClient>>;
122
+ profile?: string | null;
123
+ toolId: string;
124
+ input: Record<string, any>;
125
+ output?: Record<string, any>;
126
+ }) => {
127
+ let expect = getExpect();
128
+ let client = d.client ?? (await createSlatesTestClient({ profile: d.profile }));
129
+ let result = await client.invokeTool(d.toolId, d.input);
130
+
131
+ if (d.output) {
132
+ expect(result.output).toMatchObject(d.output);
133
+ }
134
+
135
+ return result;
136
+ };
137
+
138
+ export let createLocalSlateTestClient = (opts: {
139
+ slate: LocalSlate;
140
+ state?: SlatesProtocolClientOptions['state'];
141
+ participants?: SlatesProtocolClientOptions['participants'];
142
+ }) =>
143
+ createSlatesClient({
144
+ transport: createLocalSlateTransport({ slate: opts.slate }),
145
+ state: opts.state,
146
+ participants: opts.participants
147
+ });
148
+
149
+ export let getSlateContract = async (client: SlatesTestClient) => {
150
+ let [provider, actions, authMethods, configSchema] = await Promise.all([
151
+ client.identify(),
152
+ client.listActions(),
153
+ client.listAuthMethods(),
154
+ client.getConfigSchema()
155
+ ]);
156
+
157
+ return {
158
+ provider: provider.provider,
159
+ actions: actions.actions,
160
+ tools: actions.actions.filter(action => action.type === 'action.tool'),
161
+ triggers: actions.actions.filter(action => action.type === 'action.trigger'),
162
+ authMethods: authMethods.authenticationMethods,
163
+ configSchema: configSchema.schema
164
+ };
165
+ };
166
+
167
+ let expectActionMatches = (actual: Record<string, any> | undefined, expected: ExpectedSlateAction) => {
168
+ let expect = getExpect();
169
+ expect(actual).toBeTruthy();
170
+ expect(actual?.id).toBe(expected.id);
171
+
172
+ if (expected.name !== undefined) {
173
+ expect(actual?.name).toBe(expected.name);
174
+ }
175
+
176
+ if (expected.description !== undefined) {
177
+ expect(actual?.description).toBe(expected.description);
178
+ }
179
+
180
+ if (expected.readOnly !== undefined) {
181
+ expect(actual?.tags?.readOnly ?? false).toBe(expected.readOnly);
182
+ }
183
+
184
+ if (expected.destructive !== undefined) {
185
+ expect(actual?.tags?.destructive ?? false).toBe(expected.destructive);
186
+ }
187
+
188
+ if (expected.invocationType !== undefined) {
189
+ expect((actual as { invocation?: { type?: string } } | undefined)?.invocation?.type).toBe(
190
+ expected.invocationType
191
+ );
192
+ }
193
+ };
194
+
195
+ export let expectSlateContract = async (d: {
196
+ client: SlatesTestClient;
197
+ provider?: {
198
+ id: string;
199
+ name?: string;
200
+ description?: string;
201
+ };
202
+ toolIds?: string[];
203
+ triggerIds?: string[];
204
+ authMethodIds?: string[];
205
+ tools?: ExpectedSlateAction[];
206
+ triggers?: ExpectedSlateAction[];
207
+ }) => {
208
+ let expect = getExpect();
209
+ let contract = await getSlateContract(d.client);
210
+
211
+ if (d.provider) {
212
+ expect(contract.provider.id).toBe(d.provider.id);
213
+ if (d.provider.name !== undefined) {
214
+ expect(contract.provider.name).toBe(d.provider.name);
215
+ }
216
+ if (d.provider.description !== undefined) {
217
+ expect(contract.provider.description).toBe(d.provider.description);
218
+ }
219
+ }
220
+
221
+ if (d.toolIds) {
222
+ expect(contract.tools.map(action => action.id)).toEqual(d.toolIds);
223
+ }
224
+
225
+ if (d.triggerIds) {
226
+ expect(contract.triggers.map(action => action.id)).toEqual(d.triggerIds);
227
+ }
228
+
229
+ if (d.authMethodIds) {
230
+ expect(contract.authMethods.map(method => method.id)).toEqual(d.authMethodIds);
231
+ }
232
+
233
+ for (let tool of d.tools ?? []) {
234
+ expectActionMatches(contract.tools.find(action => action.id === tool.id), tool);
235
+ }
236
+
237
+ for (let trigger of d.triggers ?? []) {
238
+ expectActionMatches(contract.triggers.find(action => action.id === trigger.id), trigger);
239
+ }
240
+
241
+ return contract;
242
+ };
243
+
244
+ export let registerSlateTriggerWebhook = async (d: {
245
+ client: SlatesTestClient;
246
+ triggerId: string;
247
+ webhookBaseUrl: string;
248
+ }) => d.client.registerTriggerWebhook(d.triggerId, d.webhookBaseUrl);
249
+
250
+ export let handleSlateTriggerWebhook = async (d: {
251
+ client: SlatesTestClient;
252
+ triggerId: string;
253
+ url: string;
254
+ method?: string;
255
+ headers?: Record<string, string>;
256
+ body?: string | Uint8Array | null;
257
+ state?: any;
258
+ }) =>
259
+ d.client.handleTriggerWebhook({
260
+ actionId: d.triggerId,
261
+ url: d.url,
262
+ method: d.method ?? 'POST',
263
+ headers: d.headers,
264
+ body: d.body,
265
+ state: d.state
266
+ });
267
+
268
+ export let unregisterSlateTriggerWebhook = async (d: {
269
+ client: SlatesTestClient;
270
+ triggerId: string;
271
+ webhookBaseUrl: string;
272
+ registrationDetails: any;
273
+ state?: any;
274
+ }) =>
275
+ d.client.unregisterTriggerWebhook({
276
+ actionId: d.triggerId,
277
+ webhookBaseUrl: d.webhookBaseUrl,
278
+ registrationDetails: d.registrationDetails,
279
+ state: d.state
280
+ });
281
+
282
+ export let mapSlateTriggerEvent = async (d: {
283
+ client: SlatesTestClient;
284
+ triggerId: string;
285
+ input: Record<string, any>;
286
+ output?: Record<string, any>;
287
+ type?: string;
288
+ }) => {
289
+ let expect = getExpect();
290
+ let result = await d.client.mapTriggerEvent(d.triggerId, d.input);
291
+
292
+ if (d.type !== undefined) {
293
+ expect(result.type).toBe(d.type);
294
+ }
295
+
296
+ if (d.output) {
297
+ expect(result.output).toMatchObject(d.output);
298
+ }
299
+
300
+ return result;
301
+ };
302
+
303
+ export let expectSlateError = async (
304
+ run: Promise<unknown> | (() => Promise<unknown>),
305
+ expected: Record<string, any> | string | RegExp
306
+ ) => {
307
+ let expect = getExpect();
308
+ try {
309
+ await (typeof run === 'function' ? run() : run);
310
+ } catch (error) {
311
+ if (typeof expected === 'string' || expected instanceof RegExp) {
312
+ let message = error instanceof Error ? error.message : String(error);
313
+ if (expected instanceof RegExp) {
314
+ expect(message).toMatch(expected);
315
+ } else {
316
+ expect(message).toContain(expected);
317
+ }
318
+ } else if (error instanceof SlateProtocolError) {
319
+ expect(error.data).toMatchObject(expected);
320
+ } else {
321
+ expect(error).toMatchObject(expected);
322
+ }
323
+
324
+ return error;
325
+ }
326
+
327
+ throw new Error('Expected the operation to fail, but it completed successfully.');
328
+ };
329
+
330
+ export let createSlatesVitestConfig = (
331
+ config: Parameters<typeof createVitestConfig>[0] = {}
332
+ ) => createVitestConfig(config);