@dxos/effect 0.8.3 → 0.8.4-main.28f8d3d
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/lib/browser/index.mjs +178 -14
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +178 -14
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/ast.d.ts +5 -1
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/context.d.ts +4 -0
- package/dist/types/src/context.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +15 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/errors.test.d.ts +2 -0
- package/dist/types/src/errors.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/jsonPath.d.ts.map +1 -1
- package/dist/types/src/layers.test.d.ts +2 -0
- package/dist/types/src/layers.test.d.ts.map +1 -0
- package/dist/types/src/resource.d.ts +4 -0
- package/dist/types/src/resource.d.ts.map +1 -0
- package/dist/types/src/resource.test.d.ts +2 -0
- package/dist/types/src/resource.test.d.ts.map +1 -0
- package/dist/types/src/testing.d.ts +44 -0
- package/dist/types/src/testing.d.ts.map +1 -0
- package/dist/types/src/url.d.ts +1 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -6
- package/src/ast.test.ts +19 -2
- package/src/ast.ts +15 -1
- package/src/context.ts +15 -0
- package/src/errors.test.ts +22 -0
- package/src/errors.ts +138 -0
- package/src/index.ts +4 -0
- package/src/jsonPath.test.ts +1 -1
- package/src/jsonPath.ts +1 -1
- package/src/layers.test.ts +106 -0
- package/src/resource.test.ts +32 -0
- package/src/resource.ts +25 -0
- package/src/testing.ts +79 -0
- package/src/url.ts +1 -1
- package/dist/lib/node/index.cjs +0 -487
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4-main.28f8d3d",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"type": "module",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
+
"source": "./src/index.ts",
|
|
12
13
|
"types": "./dist/types/src/index.d.ts",
|
|
13
14
|
"browser": "./dist/lib/browser/index.mjs",
|
|
14
15
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
@@ -24,13 +25,14 @@
|
|
|
24
25
|
],
|
|
25
26
|
"dependencies": {
|
|
26
27
|
"jsonpath-plus": "10.2.0",
|
|
27
|
-
"@dxos/invariant": "0.8.
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/
|
|
28
|
+
"@dxos/invariant": "0.8.4-main.28f8d3d",
|
|
29
|
+
"@dxos/context": "0.8.4-main.28f8d3d",
|
|
30
|
+
"@dxos/node-std": "0.8.4-main.28f8d3d",
|
|
31
|
+
"@dxos/util": "0.8.4-main.28f8d3d"
|
|
30
32
|
},
|
|
31
33
|
"devDependencies": {
|
|
32
|
-
"effect": "3.
|
|
33
|
-
"@dxos/log": "0.8.
|
|
34
|
+
"effect": "3.17.7",
|
|
35
|
+
"@dxos/log": "0.8.4-main.28f8d3d"
|
|
34
36
|
},
|
|
35
37
|
"peerDependencies": {
|
|
36
38
|
"effect": "^3.13.3"
|
package/src/ast.test.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Schema, SchemaAST } from 'effect';
|
|
6
6
|
import { describe, test } from 'vitest';
|
|
7
7
|
|
|
8
8
|
import { invariant } from '@dxos/invariant';
|
|
@@ -12,9 +12,10 @@ import {
|
|
|
12
12
|
findNode,
|
|
13
13
|
findProperty,
|
|
14
14
|
getAnnotation,
|
|
15
|
-
getDiscriminatingProps,
|
|
16
15
|
getDiscriminatedType,
|
|
16
|
+
getDiscriminatingProps,
|
|
17
17
|
getSimpleType,
|
|
18
|
+
isArrayType,
|
|
18
19
|
isOption,
|
|
19
20
|
isSimpleType,
|
|
20
21
|
visit,
|
|
@@ -178,4 +179,20 @@ describe('AST', () => {
|
|
|
178
179
|
);
|
|
179
180
|
}
|
|
180
181
|
});
|
|
182
|
+
|
|
183
|
+
test('Schema.pluck', ({ expect }) => {
|
|
184
|
+
const TestSchema = Schema.Struct({
|
|
185
|
+
name: Schema.String,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
expect(TestSchema.pipe(Schema.pluck('name'), Schema.typeSchema).ast).toEqual(SchemaAST.stringKeyword);
|
|
189
|
+
expect(() => TestSchema.pipe(Schema.pluck('missing' as any), Schema.typeSchema)).to.throw();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('isArray', ({ expect }) => {
|
|
193
|
+
expect(isArrayType(Schema.String.ast)).to.be.false;
|
|
194
|
+
expect(isArrayType(Schema.Array(Schema.String).ast)).to.be.true;
|
|
195
|
+
expect(isArrayType(findProperty(Schema.Struct({ a: Schema.Array(Schema.String) }), 'a' as JsonPath)!)).to.be.true;
|
|
196
|
+
expect(isArrayType(Schema.Union(Schema.String, Schema.Array(Schema.String)).ast)).to.be.false;
|
|
197
|
+
});
|
|
181
198
|
});
|
package/src/ast.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Option,
|
|
5
|
+
import { Option, Schema, SchemaAST, pipe } from 'effect';
|
|
6
|
+
import { isUndefinedKeyword } from 'effect/SchemaAST';
|
|
6
7
|
|
|
7
8
|
import { invariant } from '@dxos/invariant';
|
|
8
9
|
import { isNonNullable } from '@dxos/util';
|
|
@@ -454,3 +455,16 @@ export const mapAst = (
|
|
|
454
455
|
}
|
|
455
456
|
}
|
|
456
457
|
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* @returns true if AST is for Array(T) or optional(Array(T)).
|
|
461
|
+
*/
|
|
462
|
+
export const isArrayType = (node: SchemaAST.AST): boolean => {
|
|
463
|
+
return (
|
|
464
|
+
SchemaAST.isTupleType(node) ||
|
|
465
|
+
(SchemaAST.isUnion(node) &&
|
|
466
|
+
node.types.some(isArrayType) &&
|
|
467
|
+
node.types.some(isUndefinedKeyword) &&
|
|
468
|
+
node.types.length === 2)
|
|
469
|
+
);
|
|
470
|
+
};
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Effect, type Scope } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { Context } from '@dxos/context';
|
|
8
|
+
|
|
9
|
+
// TODO(dmaretskyi): Error handling.
|
|
10
|
+
export const contextFromScope = (): Effect.Effect<Context, never, Scope.Scope> =>
|
|
11
|
+
Effect.gen(function* () {
|
|
12
|
+
const ctx = new Context();
|
|
13
|
+
yield* Effect.addFinalizer(() => Effect.promise(() => ctx.dispose()));
|
|
14
|
+
return ctx;
|
|
15
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Data } from 'effect';
|
|
6
|
+
import { test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
class MyError extends Data.TaggedError('MyError')<{
|
|
9
|
+
message: string;
|
|
10
|
+
}> {
|
|
11
|
+
constructor() {
|
|
12
|
+
super({ message: 'My error message' });
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Experimenting with error formatting:
|
|
17
|
+
// - If the error doesn't have the message set, vitest will print the error as a JS object.
|
|
18
|
+
// - If the error has non-empty message, vitest will pretty-print the error.
|
|
19
|
+
test.skip('Data error formatting', () => {
|
|
20
|
+
console.log(JSON.stringify(new MyError(), null, 2));
|
|
21
|
+
throw new MyError();
|
|
22
|
+
});
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Cause, Chunk, Effect, Exit, GlobalValue, Option } from 'effect';
|
|
6
|
+
import type { AnySpan, Span } from 'effect/Tracer';
|
|
7
|
+
|
|
8
|
+
const spanSymbol = Symbol.for('effect/SpanAnnotation');
|
|
9
|
+
const originalSymbol = Symbol.for('effect/OriginalAnnotation');
|
|
10
|
+
const spanToTrace = GlobalValue.globalValue('effect/Tracer/spanToTrace', () => new WeakMap());
|
|
11
|
+
const locationRegex = /\((.*)\)/g;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Adds effect spans.
|
|
15
|
+
* Removes effect internal functions.
|
|
16
|
+
* Unwraps error proxy.
|
|
17
|
+
*/
|
|
18
|
+
const prettyErrorStack = (error: any, appendStacks: string[] = []): any => {
|
|
19
|
+
const span = error[spanSymbol];
|
|
20
|
+
|
|
21
|
+
const lines = typeof error.stack === 'string' ? error.stack.split('\n') : [];
|
|
22
|
+
const out = [];
|
|
23
|
+
|
|
24
|
+
let atStack = false;
|
|
25
|
+
for (let i = 0; i < lines.length; i++) {
|
|
26
|
+
if (!atStack && !lines[i].startsWith(' at ')) {
|
|
27
|
+
out.push(lines[i]);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
atStack = true;
|
|
31
|
+
|
|
32
|
+
if (lines[i].includes(' at new BaseEffectError') || lines[i].includes(' at new YieldableError')) {
|
|
33
|
+
i++;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (lines[i].includes('Generator.next')) {
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
if (lines[i].includes('effect_internal_function')) {
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
out.push(
|
|
43
|
+
lines[i]
|
|
44
|
+
.replace(/at .*effect_instruction_i.*\((.*)\)/, 'at $1')
|
|
45
|
+
.replace(/EffectPrimitive\.\w+/, '<anonymous>')
|
|
46
|
+
.replace(/at Arguments\./, 'at '),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (span) {
|
|
51
|
+
let current: Span | AnySpan | undefined = span;
|
|
52
|
+
let i = 0;
|
|
53
|
+
while (current && current._tag === 'Span' && i < 10) {
|
|
54
|
+
const stackFn = spanToTrace.get(current);
|
|
55
|
+
if (typeof stackFn === 'function') {
|
|
56
|
+
const stack = stackFn();
|
|
57
|
+
if (typeof stack === 'string') {
|
|
58
|
+
const locationMatchAll = stack.matchAll(locationRegex);
|
|
59
|
+
let match = false;
|
|
60
|
+
for (const [, location] of locationMatchAll) {
|
|
61
|
+
match = true;
|
|
62
|
+
out.push(` at ${current.name} (${location})`);
|
|
63
|
+
}
|
|
64
|
+
if (!match) {
|
|
65
|
+
out.push(` at ${current.name} (${stack.replace(/^at /, '')})`);
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
out.push(` at ${current.name}`);
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
out.push(` at ${current.name}`);
|
|
72
|
+
}
|
|
73
|
+
current = Option.getOrUndefined(current.parent);
|
|
74
|
+
i++;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
out.push(...appendStacks);
|
|
79
|
+
|
|
80
|
+
if (error[originalSymbol]) {
|
|
81
|
+
error = error[originalSymbol];
|
|
82
|
+
}
|
|
83
|
+
if (error.cause) {
|
|
84
|
+
error.cause = prettyErrorStack(error.cause);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
Object.defineProperty(error, 'stack', {
|
|
88
|
+
value: out.join('\n'),
|
|
89
|
+
writable: true,
|
|
90
|
+
enumerable: false,
|
|
91
|
+
configurable: true,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return error;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Runs the embedded effect asynchronously and throws any failures and defects as errors.
|
|
99
|
+
* Inserts effect spans as stack frames.
|
|
100
|
+
* The error will have stack frames of where the effect was run (if stack trace limit allows).
|
|
101
|
+
* Removes effect runtime internal stack frames.
|
|
102
|
+
*
|
|
103
|
+
* To be used in place of `Effect.runPromise`.
|
|
104
|
+
*
|
|
105
|
+
* @throws AggregateError if there are multiple errors.
|
|
106
|
+
*/
|
|
107
|
+
export const runAndForwardErrors = async <A, E>(
|
|
108
|
+
effect: Effect.Effect<A, E, never>,
|
|
109
|
+
options?: { signal?: AbortSignal },
|
|
110
|
+
): Promise<A> => {
|
|
111
|
+
const exit = await Effect.runPromiseExit(effect, options);
|
|
112
|
+
if (Exit.isSuccess(exit)) {
|
|
113
|
+
return exit.value;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (Cause.isEmpty(exit.cause)) {
|
|
117
|
+
throw new Error('Fiber failed without a cause');
|
|
118
|
+
} else if (Cause.isInterrupted(exit.cause)) {
|
|
119
|
+
throw new Error('Fiber was interrupted');
|
|
120
|
+
} else {
|
|
121
|
+
const errors = [...Chunk.toArray(Cause.failures(exit.cause)), ...Chunk.toArray(Cause.defects(exit.cause))];
|
|
122
|
+
|
|
123
|
+
const getStackFrames = (): string[] => {
|
|
124
|
+
const o: { stack: string } = {} as any;
|
|
125
|
+
Error.captureStackTrace(o, getStackFrames);
|
|
126
|
+
return o.stack.split('\n').slice(1);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const stackFrames = getStackFrames();
|
|
130
|
+
const newErrors = errors.map((error) => prettyErrorStack(error, stackFrames));
|
|
131
|
+
|
|
132
|
+
if (newErrors.length === 1) {
|
|
133
|
+
throw newErrors[0];
|
|
134
|
+
} else {
|
|
135
|
+
throw new AggregateError(newErrors);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
package/src/index.ts
CHANGED
package/src/jsonPath.test.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
|
-
import { createJsonPath, getField, isJsonPath,
|
|
7
|
+
import { type JsonPath, createJsonPath, getField, isJsonPath, splitJsonPath } from './jsonPath';
|
|
8
8
|
|
|
9
9
|
describe('createJsonPath', () => {
|
|
10
10
|
test('supported path subset', () => {
|
package/src/jsonPath.ts
CHANGED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { it } from '@effect/vitest';
|
|
6
|
+
import { Context, Duration, Effect, Layer, ManagedRuntime } from 'effect';
|
|
7
|
+
import { test } from 'vitest';
|
|
8
|
+
|
|
9
|
+
class ClientConfig extends Context.Tag('ClientConfig')<ClientConfig, { endpoint: string }>() {}
|
|
10
|
+
|
|
11
|
+
class Client extends Context.Tag('Client')<Client, { call: () => Effect.Effect<void> }>() {
|
|
12
|
+
static layer = Layer.effect(
|
|
13
|
+
Client,
|
|
14
|
+
Effect.gen(function* () {
|
|
15
|
+
const config = yield* ClientConfig;
|
|
16
|
+
return {
|
|
17
|
+
call: () => {
|
|
18
|
+
console.log('called', config.endpoint);
|
|
19
|
+
return Effect.void;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const ServerLive = Layer.scoped(
|
|
27
|
+
ClientConfig,
|
|
28
|
+
Effect.gen(function* () {
|
|
29
|
+
console.log('start server');
|
|
30
|
+
|
|
31
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
32
|
+
|
|
33
|
+
yield* Effect.addFinalizer(
|
|
34
|
+
Effect.fn(function* () {
|
|
35
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
36
|
+
console.log('stop server');
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
endpoint: 'http://localhost:8080',
|
|
42
|
+
};
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
it.effect.skip(
|
|
47
|
+
'test',
|
|
48
|
+
Effect.fn(
|
|
49
|
+
function* ({ expect: _ }) {
|
|
50
|
+
const client = yield* Client;
|
|
51
|
+
yield* client.call();
|
|
52
|
+
},
|
|
53
|
+
Effect.provide(Layer.provide(Client.layer, ServerLive)),
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
class ServerPlugin {
|
|
58
|
+
#runtime = ManagedRuntime.make(ServerLive);
|
|
59
|
+
|
|
60
|
+
readonly clientConfigLayer = Layer.effectContext(
|
|
61
|
+
this.#runtime.runtimeEffect.pipe(Effect.map((rt) => rt.context.pipe(Context.pick(ClientConfig)))),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
async dispose() {
|
|
65
|
+
await this.#runtime.dispose();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
class ClientPlugin {
|
|
70
|
+
constructor(private readonly _serverPlugin: ServerPlugin) {}
|
|
71
|
+
|
|
72
|
+
async run() {
|
|
73
|
+
const layer = Layer.provide(Client.layer, this._serverPlugin.clientConfigLayer);
|
|
74
|
+
|
|
75
|
+
await Effect.runPromise(
|
|
76
|
+
Effect.gen(function* () {
|
|
77
|
+
const client = yield* Client;
|
|
78
|
+
yield* client.call();
|
|
79
|
+
}).pipe(Effect.provide(layer)),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
test.skip('plugins', async () => {
|
|
85
|
+
const serverPlugin = new ServerPlugin();
|
|
86
|
+
console.log('ServerPlugin created');
|
|
87
|
+
|
|
88
|
+
await Effect.runPromise(Effect.sleep(Duration.millis(500)));
|
|
89
|
+
console.log('wake up');
|
|
90
|
+
|
|
91
|
+
{
|
|
92
|
+
const clientPlugin1 = new ClientPlugin(serverPlugin);
|
|
93
|
+
console.log('ClientPlugin1 created');
|
|
94
|
+
await clientPlugin1.run();
|
|
95
|
+
console.log('client1 run');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
{
|
|
99
|
+
const clientPlugin2 = new ClientPlugin(serverPlugin);
|
|
100
|
+
console.log('ClientPlugin2 created');
|
|
101
|
+
await clientPlugin2.run();
|
|
102
|
+
console.log('client2 run');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
await serverPlugin.dispose();
|
|
106
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { it } from '@effect/vitest';
|
|
6
|
+
import { Effect } from 'effect';
|
|
7
|
+
|
|
8
|
+
import { accuireReleaseResource } from './resource';
|
|
9
|
+
|
|
10
|
+
it.effect(
|
|
11
|
+
'acquire-release',
|
|
12
|
+
Effect.fn(function* ({ expect }) {
|
|
13
|
+
const events: string[] = [];
|
|
14
|
+
const makeResource = accuireReleaseResource(() => ({
|
|
15
|
+
open: () => {
|
|
16
|
+
events.push('open');
|
|
17
|
+
},
|
|
18
|
+
close: () => {
|
|
19
|
+
events.push('close');
|
|
20
|
+
},
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
yield* Effect.gen(function* () {
|
|
24
|
+
events.push('1');
|
|
25
|
+
const _resource = yield* makeResource;
|
|
26
|
+
events.push('2');
|
|
27
|
+
}).pipe(Effect.scoped);
|
|
28
|
+
|
|
29
|
+
events.push('3');
|
|
30
|
+
expect(events).to.deep.equal(['1', 'open', '2', 'close', '3']);
|
|
31
|
+
}),
|
|
32
|
+
);
|
package/src/resource.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Effect } from 'effect';
|
|
6
|
+
|
|
7
|
+
import type { Lifecycle } from '@dxos/context';
|
|
8
|
+
|
|
9
|
+
// TODO(dmaretskyi): Extract to effect-utils.
|
|
10
|
+
export const accuireReleaseResource = <T extends Lifecycle>(getResource: () => T) =>
|
|
11
|
+
Effect.acquireRelease(
|
|
12
|
+
Effect.gen(function* () {
|
|
13
|
+
const resource = getResource();
|
|
14
|
+
yield* Effect.promise(async () => {
|
|
15
|
+
resource.open?.();
|
|
16
|
+
return undefined;
|
|
17
|
+
});
|
|
18
|
+
return resource;
|
|
19
|
+
}),
|
|
20
|
+
(resource) =>
|
|
21
|
+
Effect.promise(async () => {
|
|
22
|
+
resource.close?.();
|
|
23
|
+
return undefined;
|
|
24
|
+
}),
|
|
25
|
+
);
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Effect } from 'effect';
|
|
6
|
+
import type { TestContext } from 'vitest';
|
|
7
|
+
|
|
8
|
+
// TODO(dmaretskyi): Add all different test tags here.
|
|
9
|
+
export type TestTag = 'flaky' | 'llm';
|
|
10
|
+
|
|
11
|
+
export namespace TestHelpers {
|
|
12
|
+
/**
|
|
13
|
+
* Skip the test if the condition is false.
|
|
14
|
+
*
|
|
15
|
+
* Exmaple:
|
|
16
|
+
* ```ts
|
|
17
|
+
* it.effect(
|
|
18
|
+
* 'should process an agentic loop using Claude',
|
|
19
|
+
* Effect.fn(function* ({ expect }) {
|
|
20
|
+
* // ...
|
|
21
|
+
* }),
|
|
22
|
+
* TestHelpers.runIf(process.env.ANTHROPIC_API_KEY),
|
|
23
|
+
* );
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const runIf =
|
|
27
|
+
(condition: unknown) =>
|
|
28
|
+
<A, E, R>(effect: Effect.Effect<A, E, R>, ctx: TestContext): Effect.Effect<A, E, R> =>
|
|
29
|
+
Effect.gen(function* () {
|
|
30
|
+
if (!condition) {
|
|
31
|
+
ctx.skip();
|
|
32
|
+
} else {
|
|
33
|
+
return yield* effect;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Skip the test if the condition is true.
|
|
39
|
+
*
|
|
40
|
+
* Exmaple:
|
|
41
|
+
* ```ts
|
|
42
|
+
* it.effect(
|
|
43
|
+
* 'should process an agentic loop using Claude',
|
|
44
|
+
* Effect.fn(function* ({ expect }) {
|
|
45
|
+
* // ...
|
|
46
|
+
* }),
|
|
47
|
+
* TestHelpers.skipIf(!process.env.ANTHROPIC_API_KEY),
|
|
48
|
+
* );
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export const skipIf =
|
|
52
|
+
(condition: unknown) =>
|
|
53
|
+
<A, E, R>(effect: Effect.Effect<A, E, R>, ctx: TestContext): Effect.Effect<A, E, R> =>
|
|
54
|
+
Effect.gen(function* () {
|
|
55
|
+
if (condition) {
|
|
56
|
+
ctx.skip();
|
|
57
|
+
} else {
|
|
58
|
+
return yield* effect;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Skips this test if the tag is not in the list of tags to run.
|
|
64
|
+
* Tags are specified in the `DX_TEST_TAGS` environment variable.
|
|
65
|
+
*
|
|
66
|
+
* @param tag
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
export const taggedTest =
|
|
70
|
+
(tag: TestTag) =>
|
|
71
|
+
<A, E, R>(effect: Effect.Effect<A, E, R>, ctx: TestContext): Effect.Effect<A, E, R> =>
|
|
72
|
+
Effect.gen(function* () {
|
|
73
|
+
if (!process.env.DX_TEST_TAGS?.includes(tag)) {
|
|
74
|
+
ctx.skip();
|
|
75
|
+
} else {
|
|
76
|
+
return yield* effect;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|