@temporalio/testing 1.11.8 → 1.12.0-rc.0

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/lib/index.js CHANGED
@@ -12,68 +12,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.MockActivityEnvironment = exports.defaultActivityInfo = exports.TestWorkflowEnvironment = exports.workflowInterceptorModules = exports.TimeSkippingWorkflowClient = void 0;
16
- require("abort-controller/polyfill"); // eslint-disable-line import/no-unassigned-import
15
+ exports.workflowInterceptorModules = exports.defaultActivityInfo = exports.MockActivityEnvironment = exports.TimeSkippingWorkflowClient = exports.TestEnvClient = exports.TestWorkflowEnvironment = void 0;
17
16
  const node_path_1 = __importDefault(require("node:path"));
18
- const node_events_1 = __importDefault(require("node:events"));
19
- const client_1 = require("@temporalio/client");
20
- const common_1 = require("@temporalio/common");
21
- const time_1 = require("@temporalio/common/lib/time");
22
- const worker_1 = require("@temporalio/worker");
23
- const logger_1 = require("@temporalio/worker/lib/logger");
24
- const activity_1 = require("@temporalio/worker/lib/activity");
25
- const core_bridge_1 = require("@temporalio/core-bridge");
26
- const internal_non_workflow_1 = require("@temporalio/common/lib/internal-non-workflow");
27
- const connection_1 = require("./connection");
28
- /**
29
- * A client with the exact same API as the "normal" client with 1 exception,
30
- * When this client waits on a Workflow's result, it will enable time skipping
31
- * in the test server.
32
- */
33
- class TimeSkippingWorkflowClient extends client_1.WorkflowClient {
34
- constructor(options) {
35
- super(options);
36
- this.enableTimeSkipping = options.enableTimeSkipping;
37
- this.testService = options.connection.testService;
38
- }
39
- /**
40
- * Gets the result of a Workflow execution.
41
- *
42
- * @see {@link WorkflowClient.result}
43
- */
44
- async result(workflowId, runId, opts) {
45
- if (this.enableTimeSkipping) {
46
- await this.testService.unlockTimeSkipping({});
47
- try {
48
- return await super.result(workflowId, runId, opts);
49
- }
50
- finally {
51
- await this.testService.lockTimeSkipping({});
52
- }
53
- }
54
- else {
55
- return await super.result(workflowId, runId, opts);
56
- }
57
- }
58
- }
59
- exports.TimeSkippingWorkflowClient = TimeSkippingWorkflowClient;
60
- /**
61
- * A client with the exact same API as the "normal" client with one exception:
62
- * when `TestEnvClient.workflow` (an instance of {@link TimeSkippingWorkflowClient}) waits on a Workflow's result, it will enable time skipping
63
- * in the Test Server.
64
- */
65
- class TestEnvClient extends client_1.Client {
66
- constructor(options) {
67
- super(options);
68
- // Recreate the client (this isn't optimal but it's better than adding public methods just for testing).
69
- // NOTE: we cast to "any" to work around `workflow` being a readonly attribute.
70
- this.workflow = new TimeSkippingWorkflowClient({
71
- ...this.workflow.options,
72
- connection: options.connection,
73
- enableTimeSkipping: options.enableTimeSkipping,
74
- });
75
- }
76
- }
17
+ var testing_workflow_environment_1 = require("./testing-workflow-environment");
18
+ Object.defineProperty(exports, "TestWorkflowEnvironment", { enumerable: true, get: function () { return testing_workflow_environment_1.TestWorkflowEnvironment; } });
19
+ var client_1 = require("./client");
20
+ Object.defineProperty(exports, "TestEnvClient", { enumerable: true, get: function () { return client_1.TestEnvClient; } });
21
+ Object.defineProperty(exports, "TimeSkippingWorkflowClient", { enumerable: true, get: function () { return client_1.TimeSkippingWorkflowClient; } });
22
+ var mocking_activity_environment_1 = require("./mocking-activity-environment");
23
+ Object.defineProperty(exports, "MockActivityEnvironment", { enumerable: true, get: function () { return mocking_activity_environment_1.MockActivityEnvironment; } });
24
+ Object.defineProperty(exports, "defaultActivityInfo", { enumerable: true, get: function () { return mocking_activity_environment_1.defaultActivityInfo; } });
77
25
  /**
78
26
  * Convenience workflow interceptors
79
27
  *
@@ -81,243 +29,4 @@ class TestEnvClient extends client_1.Client {
81
29
  * retryable `ApplicationFailure`s.
82
30
  */
83
31
  exports.workflowInterceptorModules = [node_path_1.default.join(__dirname, 'assert-to-failure-interceptor')];
84
- function addDefaults(opts) {
85
- return {
86
- client: {},
87
- ...opts,
88
- };
89
- }
90
- /**
91
- * An execution environment for running Workflow integration tests.
92
- *
93
- * Runs an external server.
94
- * By default, the Java test server is used which supports time skipping.
95
- */
96
- class TestWorkflowEnvironment {
97
- constructor(options, supportsTimeSkipping, server, connection, nativeConnection, namespace) {
98
- this.options = options;
99
- this.supportsTimeSkipping = supportsTimeSkipping;
100
- this.server = server;
101
- /**
102
- * Wait for `durationMs` in "server time".
103
- *
104
- * This awaits using regular setTimeout in regular environments, or manually skips time in time-skipping environments.
105
- *
106
- * Useful for simulating events far into the future like completion of long running activities.
107
- *
108
- * **Time skippping**:
109
- *
110
- * The time skippping server toggles between skipped time and normal time depending on what it needs to execute.
111
- *
112
- * This method is _likely_ to resolve in less than `durationMs` of "real time".
113
- *
114
- * @param durationMs number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
115
- *
116
- * @example
117
- *
118
- * `workflow.ts`
119
- *
120
- * ```ts
121
- * const activities = proxyActivities({ startToCloseTimeout: 2_000_000 });
122
- *
123
- * export async function raceActivityAndTimer(): Promise<string> {
124
- * return await Promise.race([
125
- * wf.sleep(500_000).then(() => 'timer'),
126
- * activities.longRunning().then(() => 'activity'),
127
- * ]);
128
- * }
129
- * ```
130
- *
131
- * `test.ts`
132
- *
133
- * ```ts
134
- * const worker = await Worker.create({
135
- * connection: testEnv.nativeConnection,
136
- * activities: {
137
- * async longRunning() {
138
- * await testEnv.sleep(1_000_000); // <-- sleep called here
139
- * },
140
- * },
141
- * // ...
142
- * });
143
- * ```
144
- */
145
- this.sleep = async (durationMs) => {
146
- if (this.supportsTimeSkipping) {
147
- await this.connection.testService.unlockTimeSkippingWithSleep({ duration: (0, time_1.msToTs)(durationMs) });
148
- }
149
- else {
150
- await new Promise((resolve) => setTimeout(resolve, (0, time_1.msToNumber)(durationMs)));
151
- }
152
- };
153
- this.connection = connection;
154
- this.nativeConnection = nativeConnection;
155
- this.namespace = namespace;
156
- this.client = new TestEnvClient({
157
- connection,
158
- namespace: this.namespace,
159
- enableTimeSkipping: supportsTimeSkipping,
160
- ...options.client,
161
- });
162
- // eslint-disable-next-line deprecation/deprecation
163
- this.asyncCompletionClient = this.client.activity;
164
- // eslint-disable-next-line deprecation/deprecation
165
- this.workflowClient = this.client.workflow;
166
- }
167
- /**
168
- * Start a time skipping workflow environment.
169
- *
170
- * This environment automatically skips to the next events in time when a workflow handle's `result` is awaited on
171
- * (which includes {@link WorkflowClient.execute}). Before the result is awaited on, time can be manually skipped
172
- * forward using {@link sleep}. The currently known time can be obtained via {@link currentTimeMs}.
173
- *
174
- * This environment will be powered by the Temporal Time Skipping Test Server (part of the [Java SDK](https://github.com/temporalio/sdk-java)).
175
- * Note that the Time Skipping Test Server does not support full capabilities of the regular Temporal Server, and may
176
- * occasionally present different behaviors. For general Workflow testing, it is generally preferable to use {@link createLocal}
177
- * instead.
178
- *
179
- * Users can reuse this environment for testing multiple independent workflows, but not concurrently. Time skipping,
180
- * which is automatically done when awaiting a workflow result and manually done on sleep, is global to the
181
- * environment, not to the workflow under test. We highly recommend running tests serially when using a single
182
- * environment or creating a separate environment per test.
183
- *
184
- * By default, the latest release of the Test Serveer will be downloaded and cached to a temporary directory
185
- * (e.g. `$TMPDIR/temporal-test-server-sdk-typescript-*` or `%TEMP%/temporal-test-server-sdk-typescript-*.exe`). Note
186
- * that existing cached binairies will be reused without validation that they are still up-to-date, until the SDK
187
- * itself is updated. Alternatively, a specific version number of the Test Server may be provided, or the path to an
188
- * existing Test Server binary may be supplied; see {@link LocalTestWorkflowEnvironmentOptions.server.executable}.
189
- *
190
- * Note that the Test Server implementation may be changed to another one in the future. Therefore, there is no
191
- * guarantee that Test Server options, and particularly those provided through the `extraArgs` array, will continue to
192
- * be supported in the future.
193
- *
194
- * IMPORTANT: At this time, the Time Skipping Test Server is not supported on ARM platforms. Execution on Apple
195
- * silicon Macs will work if Rosetta 2 is installed.
196
- */
197
- static async createTimeSkipping(opts) {
198
- return await this.create({
199
- server: { type: 'time-skipping', ...opts?.server },
200
- client: opts?.client,
201
- supportsTimeSkipping: true,
202
- });
203
- }
204
- /**
205
- * Start a full Temporal server locally.
206
- *
207
- * This environment is good for testing full server capabilities, but does not support time skipping like
208
- * {@link createTimeSkipping} does. {@link supportsTimeSkipping} will always return `false` for this environment.
209
- * {@link sleep} will sleep the actual amount of time and {@link currentTimeMs} will return the current time.
210
- *
211
- * This local environment will be powered by [Temporal CLI](https://github.com/temporalio/cli), which is a
212
- * self-contained executable for Temporal. By default, Temporal's database will not be persisted to disk, and no UI
213
- * will be launched.
214
- *
215
- * By default, the latest release of the CLI will be downloaded and cached to a temporary directory
216
- * (e.g. `$TMPDIR/temporal-sdk-typescript-*` or `%TEMP%/temporal-sdk-typescript-*.exe`). Note that existing cached
217
- * binairies will be reused without validation that they are still up-to-date, until the SDK itself is updated.
218
- * Alternatively, a specific version number of the CLI may be provided, or the path to an existing CLI binary may be
219
- * supplied; see {@link LocalTestWorkflowEnvironmentOptions.server.executable}.
220
- *
221
- * Note that the Dev Server implementation may be changed to another one in the future. Therefore, there is no
222
- * guarantee that Dev Server options, and particularly those provided through the `extraArgs` array, will continue to
223
- * be supported in the future.
224
- */
225
- static async createLocal(opts) {
226
- return await this.create({
227
- server: { type: 'dev-server', ...opts?.server },
228
- client: opts?.client,
229
- namespace: opts?.server?.namespace,
230
- supportsTimeSkipping: false,
231
- });
232
- }
233
- /**
234
- * Create a new test environment
235
- */
236
- static async create(opts) {
237
- const { supportsTimeSkipping, namespace, ...rest } = opts;
238
- const optsWithDefaults = addDefaults((0, internal_non_workflow_1.filterNullAndUndefined)(rest));
239
- const server = await worker_1.Runtime.instance().createEphemeralServer(optsWithDefaults.server);
240
- const address = (0, core_bridge_1.getEphemeralServerTarget)(server);
241
- const nativeConnection = await worker_1.NativeConnection.connect({ address });
242
- const connection = await connection_1.Connection.connect({ address });
243
- return new this(optsWithDefaults, supportsTimeSkipping, server, connection, nativeConnection, namespace);
244
- }
245
- /**
246
- * Kill the test server process and close the connection to it
247
- */
248
- async teardown() {
249
- await this.connection.close();
250
- await this.nativeConnection.close();
251
- await worker_1.Runtime.instance().shutdownEphemeralServer(this.server);
252
- }
253
- /**
254
- * Get the current time known to this environment.
255
- *
256
- * For non-time-skipping environments this is simply the system time. For time-skipping environments this is whatever
257
- * time has been skipped to.
258
- */
259
- async currentTimeMs() {
260
- if (this.supportsTimeSkipping) {
261
- const { time } = await this.connection.testService.getCurrentTime({});
262
- return (0, time_1.tsToMs)(time);
263
- }
264
- else {
265
- return Date.now();
266
- }
267
- }
268
- }
269
- exports.TestWorkflowEnvironment = TestWorkflowEnvironment;
270
- /**
271
- * Used as the default activity info for Activities executed in the {@link MockActivityEnvironment}
272
- */
273
- exports.defaultActivityInfo = {
274
- attempt: 1,
275
- taskQueue: 'test',
276
- isLocal: false,
277
- taskToken: Buffer.from('test'),
278
- activityId: 'test',
279
- activityType: 'unknown',
280
- workflowType: 'test',
281
- base64TaskToken: Buffer.from('test').toString('base64'),
282
- heartbeatTimeoutMs: undefined,
283
- heartbeatDetails: undefined,
284
- activityNamespace: 'default',
285
- workflowNamespace: 'default',
286
- workflowExecution: { workflowId: 'test', runId: 'dead-beef' },
287
- scheduledTimestampMs: 1,
288
- startToCloseTimeoutMs: 1000,
289
- scheduleToCloseTimeoutMs: 1000,
290
- currentAttemptScheduledTimestampMs: 1,
291
- };
292
- /**
293
- * An execution environment for testing Activities.
294
- *
295
- * Mocks Activity {@link Context | activity.Context} and exposes hooks for cancellation and heartbeats.
296
- *
297
- * Note that the `Context` object used by this environment will be reused for all activities that are run in this
298
- * environment. Consequently, once `cancel()` is called, any further activity that gets executed in this environment
299
- * will immediately be in a cancelled state.
300
- */
301
- class MockActivityEnvironment extends node_events_1.default.EventEmitter {
302
- constructor(info, opts) {
303
- super();
304
- this.cancel = () => undefined;
305
- const heartbeatCallback = (details) => this.emit('heartbeat', details);
306
- const loadedDataConverter = {
307
- payloadConverter: common_1.defaultPayloadConverter,
308
- payloadCodecs: [],
309
- failureConverter: common_1.defaultFailureConverter,
310
- };
311
- this.activity = new activity_1.Activity({ ...exports.defaultActivityInfo, ...info }, undefined, loadedDataConverter, heartbeatCallback, (0, logger_1.withMetadata)(opts?.logger ?? new worker_1.DefaultLogger(), { sdkComponent: common_1.SdkComponent.worker }), opts?.interceptors ?? []);
312
- this.context = this.activity.context;
313
- this.cancel = this.activity.cancel;
314
- }
315
- /**
316
- * Run a function in Activity Context
317
- */
318
- async run(fn, ...args) {
319
- return this.activity.runNoEncoding(fn, { args, headers: {} });
320
- }
321
- }
322
- exports.MockActivityEnvironment = MockActivityEnvironment;
323
32
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;;;;AAEH,qCAAmC,CAAC,kDAAkD;AACtF,0DAA6B;AAC7B,8DAAiC;AAEjC,+CAO4B;AAC5B,+CAO4B;AAC5B,sDAAyE;AACzE,+CAA2G;AAC3G,0DAA6D;AAC7D,8DAA2D;AAC3D,yDAMiC;AACjC,wFAAsF;AACtF,6CAAuD;AAevD;;;;GAIG;AACH,MAAa,0BAA2B,SAAQ,uBAAc;IAI5D,YAAY,OAA0C;QACpD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,CAAC;QACrD,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACM,KAAK,CAAC,MAAM,CACnB,UAAkB,EAClB,KAA0B,EAC1B,IAAwC;QAExC,IAAI,IAAI,CAAC,kBAAkB,EAAE,CAAC;YAC5B,MAAM,IAAI,CAAC,WAAW,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC;YAC9C,IAAI,CAAC;gBACH,OAAO,MAAM,KAAK,CAAC,MAAM,CAAC,UAAU,EAAE,KAAK,EAAE,IAAI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AAgB,EAAE,gCAAuB;YACzC,aAAa,EAAE,EAAE;YACjB,gBAAgB,EAAE,gCAAuB;SAC1C,CAAC;QACF,IAAI,CAAC,QAAQ,GAAG,IAAI,mBAAQ,CAC1B,EAAE,GAAG,2BAAmB,EAAE,GAAG,IAAI,EAAE,EACnC,SAAS,EACT,mBAAmB,EACnB,iBAAiB,EACjB,IAAA,qBAAY,EAAC,IAAI,EAAE,MAAM,IAAI,IAAI,sBAAa,EAAE,EAAE,EAAE,YAAY,EAAE,qBAAY,CAAC,MAAM,EAAE,CAAC,EACxF,IAAI,EAAE,YAAY,IAAI,EAAE,CACzB,CAAC;QACF,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IACrC,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,GAAG,CAAuD,EAAK,EAAE,GAAG,IAAO;QACtF,OAAO,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAgC,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAe,CAAC;IAC5G,CAAC;CACF;AA/BD,0DA+BC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;;;;AAEH,0DAA6B;AAE7B,+EAKwC;AAJtC,uIAAA,uBAAuB,OAAA;AAYzB,mCAOkB;AAFhB,uGAAA,aAAa,OAAA;AACb,oHAAA,0BAA0B,OAAA;AAG5B,+EAIwC;AAFtC,uIAAA,uBAAuB,OAAA;AACvB,mIAAA,mBAAmB,OAAA;AAGrB;;;;;GAKG;AACU,QAAA,0BAA0B,GAAG,CAAC,mBAAI,CAAC,IAAI,CAAC,SAAS,EAAE,+BAA+B,CAAC,CAAC,CAAC"}
@@ -0,0 +1,35 @@
1
+ import 'abort-controller/polyfill';
2
+ import events from 'node:events';
3
+ import * as activity from '@temporalio/activity';
4
+ import { ActivityFunction, Logger, MetricMeter } from '@temporalio/common';
5
+ import { ActivityInterceptorsFactory } from '@temporalio/worker';
6
+ export interface MockActivityEnvironmentOptions {
7
+ interceptors?: ActivityInterceptorsFactory[];
8
+ logger?: Logger;
9
+ metricMeter?: MetricMeter;
10
+ }
11
+ /**
12
+ * An execution environment for testing Activities.
13
+ *
14
+ * Mocks Activity {@link Context | activity.Context} and exposes hooks for cancellation and heartbeats.
15
+ *
16
+ * Note that the `Context` object used by this environment will be reused for all activities that are run in this
17
+ * environment. Consequently, once `cancel()` is called, any further activity that gets executed in this environment
18
+ * will immediately be in a cancelled state.
19
+ */
20
+ export declare class MockActivityEnvironment extends events.EventEmitter {
21
+ cancel: (reason?: any) => void;
22
+ readonly context: activity.Context;
23
+ private readonly activity;
24
+ constructor(info?: Partial<activity.Info>, opts?: MockActivityEnvironmentOptions);
25
+ /**
26
+ * Run a function in Activity Context
27
+ */
28
+ run<P extends any[], R, F extends ActivityFunction<P, R>>(fn: F, ...args: P): Promise<R>;
29
+ }
30
+ /**
31
+ * Used as the default activity info for Activities executed in the {@link MockActivityEnvironment}
32
+ *
33
+ * @hidden
34
+ */
35
+ export declare const defaultActivityInfo: activity.Info;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.defaultActivityInfo = exports.MockActivityEnvironment = void 0;
7
+ require("abort-controller/polyfill"); // eslint-disable-line import/no-unassigned-import
8
+ const node_events_1 = __importDefault(require("node:events"));
9
+ const common_1 = require("@temporalio/common");
10
+ const logger_1 = require("@temporalio/common/lib/logger");
11
+ const worker_1 = require("@temporalio/worker");
12
+ const activity_1 = require("@temporalio/worker/lib/activity");
13
+ /**
14
+ * An execution environment for testing Activities.
15
+ *
16
+ * Mocks Activity {@link Context | activity.Context} and exposes hooks for cancellation and heartbeats.
17
+ *
18
+ * Note that the `Context` object used by this environment will be reused for all activities that are run in this
19
+ * environment. Consequently, once `cancel()` is called, any further activity that gets executed in this environment
20
+ * will immediately be in a cancelled state.
21
+ */
22
+ class MockActivityEnvironment extends node_events_1.default.EventEmitter {
23
+ cancel = () => undefined;
24
+ context;
25
+ activity;
26
+ constructor(info, opts) {
27
+ super();
28
+ const heartbeatCallback = (details) => this.emit('heartbeat', details);
29
+ const loadedDataConverter = {
30
+ payloadConverter: common_1.defaultPayloadConverter,
31
+ payloadCodecs: [],
32
+ failureConverter: common_1.defaultFailureConverter,
33
+ };
34
+ this.activity = new activity_1.Activity({ ...exports.defaultActivityInfo, ...info }, undefined, loadedDataConverter, heartbeatCallback, logger_1.LoggerWithComposedMetadata.compose(opts?.logger ?? new worker_1.DefaultLogger(), { sdkComponent: common_1.SdkComponent.worker }), opts?.metricMeter ?? common_1.noopMetricMeter, opts?.interceptors ?? []);
35
+ this.context = this.activity.context;
36
+ this.cancel = this.activity.cancel;
37
+ }
38
+ /**
39
+ * Run a function in Activity Context
40
+ */
41
+ async run(fn, ...args) {
42
+ return this.activity.runNoEncoding(fn, { args, headers: {} });
43
+ }
44
+ }
45
+ exports.MockActivityEnvironment = MockActivityEnvironment;
46
+ /**
47
+ * Used as the default activity info for Activities executed in the {@link MockActivityEnvironment}
48
+ *
49
+ * @hidden
50
+ */
51
+ exports.defaultActivityInfo = {
52
+ attempt: 1,
53
+ taskQueue: 'test',
54
+ isLocal: false,
55
+ taskToken: Buffer.from('test'),
56
+ activityId: 'test',
57
+ activityType: 'unknown',
58
+ workflowType: 'test',
59
+ base64TaskToken: Buffer.from('test').toString('base64'),
60
+ heartbeatTimeoutMs: undefined,
61
+ heartbeatDetails: undefined,
62
+ activityNamespace: 'default',
63
+ workflowNamespace: 'default',
64
+ workflowExecution: { workflowId: 'test', runId: 'dead-beef' },
65
+ scheduledTimestampMs: 1,
66
+ startToCloseTimeoutMs: 1000,
67
+ scheduleToCloseTimeoutMs: 1000,
68
+ currentAttemptScheduledTimestampMs: 1,
69
+ priority: undefined,
70
+ };
71
+ //# sourceMappingURL=mocking-activity-environment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mocking-activity-environment.js","sourceRoot":"","sources":["../src/mocking-activity-environment.ts"],"names":[],"mappings":";;;;;;AAAA,qCAAmC,CAAC,kDAAkD;AACtF,8DAAiC;AAEjC,+CAQ4B;AAC5B,0DAA2E;AAC3E,+CAAgF;AAChF,8DAA2D;AAQ3D;;;;;;;;GAQG;AACH,MAAa,uBAAwB,SAAQ,qBAAM,CAAC,YAAY;IACvD,MAAM,GAA2B,GAAG,EAAE,CAAC,SAAS,CAAC;IACxC,OAAO,CAAmB;IACzB,QAAQ,CAAW;IAEpC,YAAY,IAA6B,EAAE,IAAqC;QAC9E,KAAK,EAAE,CAAC;QACR,MAAM,iBAAiB,GAAG,CAAC,OAAiB,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;QACjF,MAAM,mBAAmB,GAAG;YAC1B,gBAAgB,EAAE,gCAAuB;YACzC,aAAa,EAAE,EAAE;YACjB,gBAAgB,EAAE,gCAAuB;SAC1C,CAAC;QACF,IAAI,CAAC,QAAQ,GAAG,IAAI,mBAAQ,CAC1B,EAAE,GAAG,2BAAmB,EAAE,GAAG,IAAI,EAAE,EACnC,SAAS,EACT,mBAAmB,EACnB,iBAAiB,EACjB,mCAA0B,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,IAAI,IAAI,sBAAa,EAAE,EAAE,EAAE,YAAY,EAAE,qBAAY,CAAC,MAAM,EAAE,CAAC,EAC9G,IAAI,EAAE,WAAW,IAAI,wBAAe,EACpC,IAAI,EAAE,YAAY,IAAI,EAAE,CACzB,CAAC;QACF,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IACrC,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,GAAG,CAAuD,EAAK,EAAE,GAAG,IAAO;QACtF,OAAO,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAgC,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAe,CAAC;IAC5G,CAAC;CACF;AAhCD,0DAgCC;AAED;;;;GAIG;AACU,QAAA,mBAAmB,GAAkB;IAChD,OAAO,EAAE,CAAC;IACV,SAAS,EAAE,MAAM;IACjB,OAAO,EAAE,KAAK;IACd,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;IAC9B,UAAU,EAAE,MAAM;IAClB,YAAY,EAAE,SAAS;IACvB,YAAY,EAAE,MAAM;IACpB,eAAe,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;IACvD,kBAAkB,EAAE,SAAS;IAC7B,gBAAgB,EAAE,SAAS;IAC3B,iBAAiB,EAAE,SAAS;IAC5B,iBAAiB,EAAE,SAAS;IAC5B,iBAAiB,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE;IAC7D,oBAAoB,EAAE,CAAC;IACvB,qBAAqB,EAAE,IAAI;IAC3B,wBAAwB,EAAE,IAAI;IAC9B,kCAAkC,EAAE,CAAC;IACrC,QAAQ,EAAE,SAAS;CACpB,CAAC"}
package/lib/pkg.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ declare const _default: {
2
+ name: string;
3
+ version: string;
4
+ };
5
+ export default _default;
package/lib/pkg.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ // ../package.json is outside of the TS project rootDir which causes TS to complain about this import.
7
+ // We do not want to change the rootDir because it messes up the output structure.
8
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
9
+ // @ts-ignore
10
+ const package_json_1 = __importDefault(require("../package.json"));
11
+ exports.default = package_json_1.default;
12
+ //# sourceMappingURL=pkg.js.map
package/lib/pkg.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pkg.js","sourceRoot":"","sources":["../src/pkg.ts"],"names":[],"mappings":";;;;;AAAA,sGAAsG;AACtG,kFAAkF;AAClF,6DAA6D;AAC7D,aAAa;AACb,mEAAkC;AAElC,kBAAe,sBAAwC,CAAC"}