@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/dist/index.cjs ADDED
@@ -0,0 +1,240 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createLocalSlateTestClient: () => createLocalSlateTestClient,
24
+ createSlatesTestClient: () => createSlatesTestClient,
25
+ createSlatesVitestConfig: () => createSlatesVitestConfig,
26
+ expectSlateContract: () => expectSlateContract,
27
+ expectSlateError: () => expectSlateError,
28
+ expectToolCall: () => expectToolCall,
29
+ getSlateContract: () => getSlateContract,
30
+ handleSlateTriggerWebhook: () => handleSlateTriggerWebhook,
31
+ loadSlatesProfile: () => loadSlatesProfile,
32
+ loadSlatesRuntimeContext: () => loadSlatesRuntimeContext,
33
+ mapSlateTriggerEvent: () => mapSlateTriggerEvent,
34
+ registerSlateTriggerWebhook: () => registerSlateTriggerWebhook,
35
+ unregisterSlateTriggerWebhook: () => unregisterSlateTriggerWebhook,
36
+ withSlateProfile: () => withSlateProfile
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
39
+ var import_testing_tools = require("@lowerdeck/testing-tools");
40
+ var import_client = require("@slates/client");
41
+ var import_profiles = require("@slates/profiles");
42
+ var import_promises = require("fs/promises");
43
+ var getExpect = () => {
44
+ let maybeExpect = globalThis.expect;
45
+ if (!maybeExpect) {
46
+ throw new Error("Vitest expect is not available in the current runtime.");
47
+ }
48
+ return maybeExpect;
49
+ };
50
+ var loadSlatesRuntimeContext = async (opts = {}) => {
51
+ let runtimeContextPath = process.env.SLATES_TEST_CONTEXT_PATH;
52
+ if (runtimeContextPath) {
53
+ let raw = await (0, import_promises.readFile)(runtimeContextPath, "utf-8");
54
+ let parsed = JSON.parse(raw);
55
+ let store2 = await (0, import_profiles.openSlatesCliStore)({ storePath: parsed.storePath });
56
+ let profile2 = store2.getProfile(opts.profile ?? parsed.profileId ?? null);
57
+ return {
58
+ integration: parsed.integration ?? store2.scope?.key ?? null,
59
+ profileId: profile2?.id ?? parsed.profileId ?? null,
60
+ profile: profile2,
61
+ storePath: parsed.storePath,
62
+ cliDir: parsed.cliDir
63
+ };
64
+ }
65
+ let store = process.env.SLATES_STORE_PATH ? await (0, import_profiles.openSlatesCliStore)({ storePath: process.env.SLATES_STORE_PATH }) : await (0, import_profiles.openSlatesCliStore)({ cwd: opts.cwd });
66
+ let profileId = opts.profile ?? process.env.SLATES_PROFILE_ID ?? null;
67
+ let profile = store.getProfile(profileId);
68
+ return {
69
+ integration: process.env.SLATES_INTEGRATION ?? store.scope?.key ?? null,
70
+ profileId: profile?.id ?? null,
71
+ profile,
72
+ storePath: store.storePath,
73
+ cliDir: store.dirPath
74
+ };
75
+ };
76
+ var loadSlatesProfile = async (opts = {}) => {
77
+ let context = await loadSlatesRuntimeContext(opts);
78
+ if (!context.profile) {
79
+ throw new Error("No Slates profile is available for the current test context.");
80
+ }
81
+ return context.profile;
82
+ };
83
+ var createSlatesTestClient = async (opts = {}) => {
84
+ let context = await loadSlatesRuntimeContext(opts);
85
+ if (!context.profile) {
86
+ throw new Error("No Slates profile is available for the current test context.");
87
+ }
88
+ let store = await (0, import_profiles.openSlatesCliStore)({ storePath: context.storePath });
89
+ return (0, import_profiles.createSlatesClientFromProfile)(context.profile, { cwd: opts.cwd, store });
90
+ };
91
+ var withSlateProfile = async (profileName, cb) => {
92
+ let profile = await loadSlatesProfile({ profile: profileName });
93
+ return cb({ profile });
94
+ };
95
+ var expectToolCall = async (d) => {
96
+ let expect = getExpect();
97
+ let client = d.client ?? await createSlatesTestClient({ profile: d.profile });
98
+ let result = await client.invokeTool(d.toolId, d.input);
99
+ if (d.output) {
100
+ expect(result.output).toMatchObject(d.output);
101
+ }
102
+ return result;
103
+ };
104
+ var createLocalSlateTestClient = (opts) => (0, import_client.createSlatesClient)({
105
+ transport: (0, import_client.createLocalSlateTransport)({ slate: opts.slate }),
106
+ state: opts.state,
107
+ participants: opts.participants
108
+ });
109
+ var getSlateContract = async (client) => {
110
+ let [provider, actions, authMethods, configSchema] = await Promise.all([
111
+ client.identify(),
112
+ client.listActions(),
113
+ client.listAuthMethods(),
114
+ client.getConfigSchema()
115
+ ]);
116
+ return {
117
+ provider: provider.provider,
118
+ actions: actions.actions,
119
+ tools: actions.actions.filter((action) => action.type === "action.tool"),
120
+ triggers: actions.actions.filter((action) => action.type === "action.trigger"),
121
+ authMethods: authMethods.authenticationMethods,
122
+ configSchema: configSchema.schema
123
+ };
124
+ };
125
+ var expectActionMatches = (actual, expected) => {
126
+ let expect = getExpect();
127
+ expect(actual).toBeTruthy();
128
+ expect(actual?.id).toBe(expected.id);
129
+ if (expected.name !== void 0) {
130
+ expect(actual?.name).toBe(expected.name);
131
+ }
132
+ if (expected.description !== void 0) {
133
+ expect(actual?.description).toBe(expected.description);
134
+ }
135
+ if (expected.readOnly !== void 0) {
136
+ expect(actual?.tags?.readOnly ?? false).toBe(expected.readOnly);
137
+ }
138
+ if (expected.destructive !== void 0) {
139
+ expect(actual?.tags?.destructive ?? false).toBe(expected.destructive);
140
+ }
141
+ if (expected.invocationType !== void 0) {
142
+ expect(actual?.invocation?.type).toBe(
143
+ expected.invocationType
144
+ );
145
+ }
146
+ };
147
+ var expectSlateContract = async (d) => {
148
+ let expect = getExpect();
149
+ let contract = await getSlateContract(d.client);
150
+ if (d.provider) {
151
+ expect(contract.provider.id).toBe(d.provider.id);
152
+ if (d.provider.name !== void 0) {
153
+ expect(contract.provider.name).toBe(d.provider.name);
154
+ }
155
+ if (d.provider.description !== void 0) {
156
+ expect(contract.provider.description).toBe(d.provider.description);
157
+ }
158
+ }
159
+ if (d.toolIds) {
160
+ expect(contract.tools.map((action) => action.id)).toEqual(d.toolIds);
161
+ }
162
+ if (d.triggerIds) {
163
+ expect(contract.triggers.map((action) => action.id)).toEqual(d.triggerIds);
164
+ }
165
+ if (d.authMethodIds) {
166
+ expect(contract.authMethods.map((method) => method.id)).toEqual(d.authMethodIds);
167
+ }
168
+ for (let tool of d.tools ?? []) {
169
+ expectActionMatches(contract.tools.find((action) => action.id === tool.id), tool);
170
+ }
171
+ for (let trigger of d.triggers ?? []) {
172
+ expectActionMatches(contract.triggers.find((action) => action.id === trigger.id), trigger);
173
+ }
174
+ return contract;
175
+ };
176
+ var registerSlateTriggerWebhook = async (d) => d.client.registerTriggerWebhook(d.triggerId, d.webhookBaseUrl);
177
+ var handleSlateTriggerWebhook = async (d) => d.client.handleTriggerWebhook({
178
+ actionId: d.triggerId,
179
+ url: d.url,
180
+ method: d.method ?? "POST",
181
+ headers: d.headers,
182
+ body: d.body,
183
+ state: d.state
184
+ });
185
+ var unregisterSlateTriggerWebhook = async (d) => d.client.unregisterTriggerWebhook({
186
+ actionId: d.triggerId,
187
+ webhookBaseUrl: d.webhookBaseUrl,
188
+ registrationDetails: d.registrationDetails,
189
+ state: d.state
190
+ });
191
+ var mapSlateTriggerEvent = async (d) => {
192
+ let expect = getExpect();
193
+ let result = await d.client.mapTriggerEvent(d.triggerId, d.input);
194
+ if (d.type !== void 0) {
195
+ expect(result.type).toBe(d.type);
196
+ }
197
+ if (d.output) {
198
+ expect(result.output).toMatchObject(d.output);
199
+ }
200
+ return result;
201
+ };
202
+ var expectSlateError = async (run, expected) => {
203
+ let expect = getExpect();
204
+ try {
205
+ await (typeof run === "function" ? run() : run);
206
+ } catch (error) {
207
+ if (typeof expected === "string" || expected instanceof RegExp) {
208
+ let message = error instanceof Error ? error.message : String(error);
209
+ if (expected instanceof RegExp) {
210
+ expect(message).toMatch(expected);
211
+ } else {
212
+ expect(message).toContain(expected);
213
+ }
214
+ } else if (error instanceof import_client.SlateProtocolError) {
215
+ expect(error.data).toMatchObject(expected);
216
+ } else {
217
+ expect(error).toMatchObject(expected);
218
+ }
219
+ return error;
220
+ }
221
+ throw new Error("Expected the operation to fail, but it completed successfully.");
222
+ };
223
+ var createSlatesVitestConfig = (config = {}) => (0, import_testing_tools.createVitestConfig)(config);
224
+ // Annotate the CommonJS export names for ESM import in node:
225
+ 0 && (module.exports = {
226
+ createLocalSlateTestClient,
227
+ createSlatesTestClient,
228
+ createSlatesVitestConfig,
229
+ expectSlateContract,
230
+ expectSlateError,
231
+ expectToolCall,
232
+ getSlateContract,
233
+ handleSlateTriggerWebhook,
234
+ loadSlatesProfile,
235
+ loadSlatesRuntimeContext,
236
+ mapSlateTriggerEvent,
237
+ registerSlateTriggerWebhook,
238
+ unregisterSlateTriggerWebhook,
239
+ withSlateProfile
240
+ });