@dxos/effect 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8
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/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/browser/chunk-CGS2ULMK.mjs +11 -0
- package/dist/lib/browser/chunk-CGS2ULMK.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +568 -329
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing.mjs +31 -0
- package/dist/lib/browser/testing.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +568 -329
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing.mjs +31 -0
- package/dist/lib/node-esm/testing.mjs.map +7 -0
- package/dist/types/src/EffectEx.d.ts +5 -0
- package/dist/types/src/EffectEx.d.ts.map +1 -0
- package/dist/types/src/Performance.d.ts +25 -0
- package/dist/types/src/Performance.d.ts.map +1 -0
- package/dist/types/src/RuntimeProvider.d.ts +21 -0
- package/dist/types/src/RuntimeProvider.d.ts.map +1 -0
- package/dist/types/src/SchemaEx.d.ts +4 -0
- package/dist/types/src/SchemaEx.d.ts.map +1 -0
- package/dist/types/src/atom-kvs.d.ts +19 -0
- package/dist/types/src/atom-kvs.d.ts.map +1 -0
- package/dist/types/src/dynamic-runtime.d.ts +56 -0
- package/dist/types/src/dynamic-runtime.d.ts.map +1 -0
- package/dist/types/src/dynamic-runtime.test.d.ts +2 -0
- package/dist/types/src/dynamic-runtime.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +8 -7
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/{ast.d.ts → internal/ast.d.ts} +47 -22
- package/dist/types/src/internal/ast.d.ts.map +1 -0
- package/dist/types/src/internal/async-task-tagging.d.ts +6 -0
- package/dist/types/src/internal/async-task-tagging.d.ts.map +1 -0
- package/dist/types/src/{context.d.ts → internal/context.d.ts} +2 -1
- package/dist/types/src/internal/context.d.ts.map +1 -0
- package/dist/types/src/internal/errors.d.ts +61 -0
- package/dist/types/src/internal/errors.d.ts.map +1 -0
- package/dist/types/src/{jsonPath.d.ts → internal/json-path.d.ts} +12 -4
- package/dist/types/src/internal/json-path.d.ts.map +1 -0
- package/dist/types/src/internal/resource.d.ts +8 -0
- package/dist/types/src/internal/resource.d.ts.map +1 -0
- package/dist/types/src/{url.d.ts → internal/url.d.ts} +3 -1
- package/dist/types/src/internal/url.d.ts.map +1 -0
- package/dist/types/src/interrupt.test.d.ts +2 -0
- package/dist/types/src/interrupt.test.d.ts.map +1 -0
- package/dist/types/src/json-path.test.d.ts +2 -0
- package/dist/types/src/json-path.test.d.ts.map +1 -0
- package/dist/types/src/otel.d.ts +17 -0
- package/dist/types/src/otel.d.ts.map +1 -0
- package/dist/types/src/otel.test.d.ts +2 -0
- package/dist/types/src/otel.test.d.ts.map +1 -0
- package/dist/types/src/testing.d.ts +25 -11
- package/dist/types/src/testing.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +8 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +33 -13
- package/src/EffectEx.ts +18 -0
- package/src/Performance.ts +45 -0
- package/src/RuntimeProvider.ts +35 -0
- package/src/SchemaEx.ts +46 -0
- package/src/ast.test.ts +38 -10
- package/src/atom-kvs.ts +35 -0
- package/src/dynamic-runtime.test.ts +465 -0
- package/src/dynamic-runtime.ts +195 -0
- package/src/errors.test.ts +1 -1
- package/src/index.ts +12 -7
- package/src/{ast.ts → internal/ast.ts} +161 -96
- package/src/internal/async-task-tagging.ts +51 -0
- package/src/{context.ts → internal/context.ts} +2 -1
- package/src/internal/errors.ts +255 -0
- package/src/{jsonPath.ts → internal/json-path.ts} +29 -4
- package/src/{resource.ts → internal/resource.ts} +10 -5
- package/src/{url.ts → internal/url.ts} +5 -2
- package/src/interrupt.test.ts +35 -0
- package/src/{jsonPath.test.ts → json-path.test.ts} +55 -8
- package/src/layers.test.ts +10 -4
- package/src/otel.test.ts +126 -0
- package/src/otel.ts +45 -0
- package/src/resource.test.ts +3 -3
- package/src/sanity.test.ts +30 -15
- package/src/testing.ts +29 -22
- package/src/types.ts +11 -0
- package/src/url.test.ts +2 -2
- package/dist/types/src/ast.d.ts.map +0 -1
- package/dist/types/src/context.d.ts.map +0 -1
- package/dist/types/src/errors.d.ts +0 -15
- package/dist/types/src/errors.d.ts.map +0 -1
- package/dist/types/src/jsonPath.d.ts.map +0 -1
- package/dist/types/src/jsonPath.test.d.ts +0 -2
- package/dist/types/src/jsonPath.test.d.ts.map +0 -1
- package/dist/types/src/resource.d.ts +0 -4
- package/dist/types/src/resource.d.ts.map +0 -1
- package/dist/types/src/url.d.ts.map +0 -1
- package/src/errors.ts +0 -138
|
@@ -2,20 +2,26 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Option from 'effect/Option';
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
6
7
|
import { JSONPath } from 'jsonpath-plus';
|
|
7
8
|
|
|
8
9
|
import { invariant } from '@dxos/invariant';
|
|
10
|
+
import { getDeep, setDeep } from '@dxos/util';
|
|
9
11
|
|
|
10
12
|
export type JsonProp = string & { __JsonPath: true; __JsonProp: true };
|
|
11
13
|
export type JsonPath = string & { __JsonPath: true };
|
|
12
14
|
|
|
15
|
+
// TODO(burdon): Start with "$."?
|
|
16
|
+
|
|
13
17
|
const PATH_REGEX = /^($|[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*|\[\d+\](?:\.)?)*$)/;
|
|
18
|
+
|
|
14
19
|
const PROP_REGEX = /^\w+$/;
|
|
15
20
|
|
|
16
21
|
/**
|
|
17
22
|
* https://www.ietf.org/archive/id/draft-goessner-dispatch-jsonpath-00.html
|
|
18
23
|
*/
|
|
24
|
+
// TODO(burdon): Keys could be arbitrary strings.
|
|
19
25
|
export const JsonPath = Schema.String.pipe(Schema.pattern(PATH_REGEX)).annotations({
|
|
20
26
|
title: 'JSON path',
|
|
21
27
|
description: 'JSON path to a property',
|
|
@@ -49,7 +55,7 @@ export const isJsonPath = (value: unknown): value is JsonPath => {
|
|
|
49
55
|
* @param path Array of string or number segments
|
|
50
56
|
* @returns Valid JsonPath or undefined if invalid
|
|
51
57
|
*/
|
|
52
|
-
export const createJsonPath = (path: (string | number)[]): JsonPath => {
|
|
58
|
+
export const createJsonPath = (path: readonly (string | number)[]): JsonPath => {
|
|
53
59
|
const candidatePath = path
|
|
54
60
|
.map((p, i) => {
|
|
55
61
|
if (typeof p === 'number') {
|
|
@@ -79,7 +85,7 @@ export const fromEffectValidationPath = (effectPath: string): JsonPath => {
|
|
|
79
85
|
* Splits a JsonPath into its constituent parts.
|
|
80
86
|
* Handles property access and array indexing.
|
|
81
87
|
*/
|
|
82
|
-
export const splitJsonPath = (path: JsonPath): string[] => {
|
|
88
|
+
export const splitJsonPath = (path: JsonPath): (string | number)[] => {
|
|
83
89
|
if (!isJsonPath(path)) {
|
|
84
90
|
return [];
|
|
85
91
|
}
|
|
@@ -87,14 +93,33 @@ export const splitJsonPath = (path: JsonPath): string[] => {
|
|
|
87
93
|
return (
|
|
88
94
|
path
|
|
89
95
|
.match(/[a-zA-Z_$][\w$]*|\[\d+\]/g)
|
|
90
|
-
?.map((part) =>
|
|
96
|
+
?.map((part) => part.replace(/[[\]]/g, ''))
|
|
97
|
+
.map((part) => {
|
|
98
|
+
const parsed = Number.parseInt(part, 10);
|
|
99
|
+
return Number.isNaN(parsed) ? part : parsed;
|
|
100
|
+
}) ?? []
|
|
91
101
|
);
|
|
92
102
|
};
|
|
93
103
|
|
|
94
104
|
/**
|
|
95
105
|
* Applies a JsonPath to an object.
|
|
96
106
|
*/
|
|
107
|
+
// TODO(burdon): Reconcile with getValue.
|
|
97
108
|
export const getField = (object: any, path: JsonPath): any => {
|
|
98
109
|
// By default, JSONPath returns an array of results.
|
|
99
110
|
return JSONPath({ path, json: object })[0];
|
|
100
111
|
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Get value from object using JsonPath.
|
|
115
|
+
*/
|
|
116
|
+
export const getValue = <T extends object>(obj: T, path: JsonPath): any => {
|
|
117
|
+
return getDeep(obj, splitJsonPath(path));
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Set value on object using JsonPath.
|
|
122
|
+
*/
|
|
123
|
+
export const setValue = <T extends object>(obj: T, path: JsonPath, value: any): T => {
|
|
124
|
+
return setDeep(obj, splitJsonPath(path), value);
|
|
125
|
+
};
|
|
@@ -2,24 +2,29 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import type * as Scope from 'effect/Scope';
|
|
6
7
|
|
|
7
8
|
import type { Lifecycle } from '@dxos/context';
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Acquires a resource and releases it when the scope is closed.
|
|
12
|
+
*/
|
|
13
|
+
export const acquireReleaseResource = <T extends Lifecycle>(
|
|
14
|
+
getResource: () => T,
|
|
15
|
+
): Effect.Effect<T, never, Scope.Scope> =>
|
|
11
16
|
Effect.acquireRelease(
|
|
12
17
|
Effect.gen(function* () {
|
|
13
18
|
const resource = getResource();
|
|
14
19
|
yield* Effect.promise(async () => {
|
|
15
|
-
resource.open?.();
|
|
20
|
+
await resource.open?.();
|
|
16
21
|
return undefined;
|
|
17
22
|
});
|
|
18
23
|
return resource;
|
|
19
24
|
}),
|
|
20
25
|
(resource) =>
|
|
21
26
|
Effect.promise(async () => {
|
|
22
|
-
resource.close?.();
|
|
27
|
+
await resource.close?.();
|
|
23
28
|
return undefined;
|
|
24
29
|
}),
|
|
25
30
|
);
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Function from 'effect/Function';
|
|
6
|
+
import * as Option from 'effect/Option';
|
|
7
|
+
import type * as Schema from 'effect/Schema';
|
|
8
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
6
9
|
|
|
7
10
|
import { decamelize } from '@dxos/util';
|
|
8
11
|
|
|
@@ -59,7 +62,7 @@ export class UrlParser<T extends Record<string, any>> {
|
|
|
59
62
|
if (value !== undefined) {
|
|
60
63
|
const field = this._schema.fields[key];
|
|
61
64
|
if (field) {
|
|
62
|
-
const { key: serializedKey } = pipe(
|
|
65
|
+
const { key: serializedKey } = Function.pipe(
|
|
63
66
|
getParamKeyAnnotation(field.ast),
|
|
64
67
|
Option.getOrElse(() => ({
|
|
65
68
|
key: decamelize(key),
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { it } from '@effect/vitest';
|
|
6
|
+
import * as Cause from 'effect/Cause';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Fiber from 'effect/Fiber';
|
|
9
|
+
|
|
10
|
+
import { runAndForwardErrors } from './internal/errors';
|
|
11
|
+
|
|
12
|
+
const doWork = Effect.fn('doWork')(function* () {
|
|
13
|
+
yield* Effect.sleep('1 minute');
|
|
14
|
+
return 'work done';
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it.effect.skip(
|
|
18
|
+
'call a function to generate a research report',
|
|
19
|
+
Effect.fnUntraced(
|
|
20
|
+
function* (_) {
|
|
21
|
+
const resultFiber = yield* doWork().pipe(Effect.fork);
|
|
22
|
+
setTimeout(() => {
|
|
23
|
+
void runAndForwardErrors(Fiber.interrupt(resultFiber));
|
|
24
|
+
}, 2_000);
|
|
25
|
+
|
|
26
|
+
const result = yield* resultFiber;
|
|
27
|
+
console.log({ result });
|
|
28
|
+
},
|
|
29
|
+
Effect.catchAllCause((cause) => {
|
|
30
|
+
// console.log(inspect(cause, { depth: null, colors: true }));
|
|
31
|
+
console.log(Cause.pretty(cause));
|
|
32
|
+
return Effect.failCause(cause);
|
|
33
|
+
}),
|
|
34
|
+
),
|
|
35
|
+
);
|
|
@@ -4,7 +4,15 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
type JsonPath,
|
|
9
|
+
createJsonPath,
|
|
10
|
+
getField,
|
|
11
|
+
getValue,
|
|
12
|
+
isJsonPath,
|
|
13
|
+
setValue,
|
|
14
|
+
splitJsonPath,
|
|
15
|
+
} from './internal/json-path';
|
|
8
16
|
|
|
9
17
|
describe('createJsonPath', () => {
|
|
10
18
|
test('supported path subset', () => {
|
|
@@ -32,9 +40,9 @@ describe('createJsonPath', () => {
|
|
|
32
40
|
|
|
33
41
|
test('path splitting', () => {
|
|
34
42
|
const cases = [
|
|
35
|
-
['foo.bar[0].baz', ['foo', 'bar',
|
|
36
|
-
['users[1].name', ['users',
|
|
37
|
-
['data[0][1]', ['data',
|
|
43
|
+
['foo.bar[0].baz', ['foo', 'bar', 0, 'baz']],
|
|
44
|
+
['users[1].name', ['users', 1, 'name']],
|
|
45
|
+
['data[0][1]', ['data', 0, 1]],
|
|
38
46
|
['simple.path', ['simple', 'path']],
|
|
39
47
|
['root', ['root']],
|
|
40
48
|
] as const;
|
|
@@ -47,15 +55,15 @@ describe('createJsonPath', () => {
|
|
|
47
55
|
test('path splitting - extended cases', () => {
|
|
48
56
|
const cases = [
|
|
49
57
|
// Multiple consecutive array indices.
|
|
50
|
-
['matrix[0][1][2]', ['matrix',
|
|
58
|
+
['matrix[0][1][2]', ['matrix', 0, 1, 2]],
|
|
51
59
|
// Properties with underscores and $.
|
|
52
60
|
['$_foo.bar_baz', ['$_foo', 'bar_baz']],
|
|
53
61
|
// Deep nesting.
|
|
54
|
-
['very.deep.nested[0].property.path[5]', ['very', 'deep', 'nested',
|
|
62
|
+
['very.deep.nested[0].property.path[5]', ['very', 'deep', 'nested', 0, 'property', 'path', 5]],
|
|
55
63
|
// Single character properties.
|
|
56
|
-
['a[0].b.c', ['a',
|
|
64
|
+
['a[0].b.c', ['a', 0, 'b', 'c']],
|
|
57
65
|
// Properties containing numbers.
|
|
58
|
-
['prop123.item456[7]', ['prop123', 'item456',
|
|
66
|
+
['prop123.item456[7]', ['prop123', 'item456', 7]],
|
|
59
67
|
] as const;
|
|
60
68
|
|
|
61
69
|
cases.forEach(([input, expected]) => {
|
|
@@ -99,3 +107,42 @@ describe('createJsonPath', () => {
|
|
|
99
107
|
expect(getField({ a: 'foo' }, 'a' as JsonPath)).toBe('foo');
|
|
100
108
|
});
|
|
101
109
|
});
|
|
110
|
+
|
|
111
|
+
describe('Types', () => {
|
|
112
|
+
test('checks sanity', async ({ expect }) => {
|
|
113
|
+
const obj = {};
|
|
114
|
+
expect(obj).to.exist;
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('get/set deep', () => {
|
|
119
|
+
test('get/set operations', ({ expect }) => {
|
|
120
|
+
const obj = {
|
|
121
|
+
name: 'test',
|
|
122
|
+
items: ['a', 'b', 'c'],
|
|
123
|
+
nested: {
|
|
124
|
+
prop: 'value',
|
|
125
|
+
arr: [1, 2, 3],
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// Basic property access.
|
|
130
|
+
expect(getValue(obj, 'name' as JsonPath)).toBe('test');
|
|
131
|
+
|
|
132
|
+
// Array index access.
|
|
133
|
+
expect(getValue(obj, 'items[1]' as JsonPath)).toBe('b');
|
|
134
|
+
|
|
135
|
+
// Nested property access.
|
|
136
|
+
expect(getValue(obj, 'nested.prop' as JsonPath)).toBe('value');
|
|
137
|
+
|
|
138
|
+
// Nested array access.
|
|
139
|
+
expect(getValue(obj, 'nested.arr[2]' as JsonPath)).toBe(3);
|
|
140
|
+
|
|
141
|
+
// Setting values.
|
|
142
|
+
const updated1 = setValue(obj, 'items[1]' as JsonPath, 'x');
|
|
143
|
+
expect(updated1.items[1]).toBe('x');
|
|
144
|
+
|
|
145
|
+
const updated2 = setValue(obj, 'nested.arr[0]' as JsonPath, 99);
|
|
146
|
+
expect(updated2.nested.arr[0]).toBe(99);
|
|
147
|
+
});
|
|
148
|
+
});
|
package/src/layers.test.ts
CHANGED
|
@@ -3,9 +3,15 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { it } from '@effect/vitest';
|
|
6
|
-
import
|
|
6
|
+
import * as Context from 'effect/Context';
|
|
7
|
+
import * as Duration from 'effect/Duration';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Layer from 'effect/Layer';
|
|
10
|
+
import * as ManagedRuntime from 'effect/ManagedRuntime';
|
|
7
11
|
import { test } from 'vitest';
|
|
8
12
|
|
|
13
|
+
import { runAndForwardErrors } from './internal/errors';
|
|
14
|
+
|
|
9
15
|
class ClientConfig extends Context.Tag('ClientConfig')<ClientConfig, { endpoint: string }>() {}
|
|
10
16
|
|
|
11
17
|
class Client extends Context.Tag('Client')<Client, { call: () => Effect.Effect<void> }>() {
|
|
@@ -46,7 +52,7 @@ const ServerLive = Layer.scoped(
|
|
|
46
52
|
it.effect.skip(
|
|
47
53
|
'test',
|
|
48
54
|
Effect.fn(
|
|
49
|
-
function* (
|
|
55
|
+
function* (_) {
|
|
50
56
|
const client = yield* Client;
|
|
51
57
|
yield* client.call();
|
|
52
58
|
},
|
|
@@ -72,7 +78,7 @@ class ClientPlugin {
|
|
|
72
78
|
async run() {
|
|
73
79
|
const layer = Layer.provide(Client.layer, this._serverPlugin.clientConfigLayer);
|
|
74
80
|
|
|
75
|
-
await
|
|
81
|
+
await runAndForwardErrors(
|
|
76
82
|
Effect.gen(function* () {
|
|
77
83
|
const client = yield* Client;
|
|
78
84
|
yield* client.call();
|
|
@@ -85,7 +91,7 @@ test.skip('plugins', async () => {
|
|
|
85
91
|
const serverPlugin = new ServerPlugin();
|
|
86
92
|
console.log('ServerPlugin created');
|
|
87
93
|
|
|
88
|
-
await
|
|
94
|
+
await runAndForwardErrors(Effect.sleep(Duration.millis(500)));
|
|
89
95
|
console.log('wake up');
|
|
90
96
|
|
|
91
97
|
{
|
package/src/otel.test.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { afterAll, beforeAll, it } from '@effect/vitest';
|
|
6
|
+
import { SpanStatusCode, trace } from '@opentelemetry/api';
|
|
7
|
+
import { type Logger, SeverityNumber, logs } from '@opentelemetry/api-logs';
|
|
8
|
+
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
9
|
+
import { ConsoleLogRecordExporter, LoggerProvider, SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
|
|
10
|
+
import { NodeSDK } from '@opentelemetry/sdk-node';
|
|
11
|
+
import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-node';
|
|
12
|
+
import {
|
|
13
|
+
ATTR_CODE_FILE_PATH,
|
|
14
|
+
ATTR_CODE_LINE_NUMBER,
|
|
15
|
+
ATTR_CODE_STACKTRACE,
|
|
16
|
+
ATTR_SERVICE_NAME,
|
|
17
|
+
} from '@opentelemetry/semantic-conventions';
|
|
18
|
+
import * as Duration from 'effect/Duration';
|
|
19
|
+
import * as Effect from 'effect/Effect';
|
|
20
|
+
import { beforeEach } from 'vitest';
|
|
21
|
+
|
|
22
|
+
import { LogLevel, type LogProcessor, log } from '@dxos/log';
|
|
23
|
+
|
|
24
|
+
import { layerOtel } from './otel';
|
|
25
|
+
|
|
26
|
+
const resource = resourceFromAttributes({
|
|
27
|
+
[ATTR_SERVICE_NAME]: 'test',
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const sdk = new NodeSDK({
|
|
31
|
+
traceExporter: new ConsoleSpanExporter(),
|
|
32
|
+
resource,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// and add a processor to export log record
|
|
36
|
+
const loggerProvider = new LoggerProvider({
|
|
37
|
+
processors: [new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())],
|
|
38
|
+
resource,
|
|
39
|
+
});
|
|
40
|
+
logs.setGlobalLoggerProvider(loggerProvider);
|
|
41
|
+
|
|
42
|
+
// You can also use global singleton
|
|
43
|
+
const logger = logs.getLogger('test');
|
|
44
|
+
|
|
45
|
+
const makeOtelLogProcessor = (logger: Logger): LogProcessor => {
|
|
46
|
+
return (config, entry) => {
|
|
47
|
+
let severity: SeverityNumber = SeverityNumber.UNSPECIFIED;
|
|
48
|
+
switch (entry.level) {
|
|
49
|
+
case LogLevel.DEBUG:
|
|
50
|
+
severity = SeverityNumber.DEBUG;
|
|
51
|
+
break;
|
|
52
|
+
case LogLevel.INFO:
|
|
53
|
+
severity = SeverityNumber.INFO;
|
|
54
|
+
break;
|
|
55
|
+
case LogLevel.WARN:
|
|
56
|
+
severity = SeverityNumber.WARN;
|
|
57
|
+
break;
|
|
58
|
+
case LogLevel.ERROR:
|
|
59
|
+
severity = SeverityNumber.ERROR;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
logger.emit({
|
|
64
|
+
body: entry.error ? ('stack' in entry.error ? entry.error.stack : String(entry.error)) : entry.message,
|
|
65
|
+
severityNumber: severity,
|
|
66
|
+
attributes: {
|
|
67
|
+
[ATTR_CODE_FILE_PATH]: entry.meta?.F,
|
|
68
|
+
[ATTR_CODE_LINE_NUMBER]: entry.meta?.L,
|
|
69
|
+
[ATTR_CODE_STACKTRACE]: entry.error?.stack,
|
|
70
|
+
...(typeof entry.context === 'object'
|
|
71
|
+
? entry.context
|
|
72
|
+
: {
|
|
73
|
+
ctx: entry.context,
|
|
74
|
+
}),
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
log.addProcessor(makeOtelLogProcessor(logger));
|
|
81
|
+
|
|
82
|
+
beforeAll(() => {
|
|
83
|
+
sdk.start();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
afterAll(async () => {
|
|
87
|
+
await loggerProvider.shutdown();
|
|
88
|
+
await sdk.shutdown();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
beforeEach((ctx) => {
|
|
92
|
+
const span = trace.getTracer('testing-framework').startSpan(ctx.task.name);
|
|
93
|
+
ctx.onTestFailed((ctx) => {
|
|
94
|
+
// TODO(dmaretskyi): Record result.
|
|
95
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
96
|
+
span.end();
|
|
97
|
+
});
|
|
98
|
+
ctx.onTestFinished((ctx) => {
|
|
99
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
100
|
+
span.end();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const foo = Effect.fn('foo')(function* () {
|
|
105
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
106
|
+
|
|
107
|
+
log('log inside foo', { detail: 123 });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const bar = Effect.fn('bar')(function* () {
|
|
111
|
+
yield* foo();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const baz = Effect.fn('baz')(function* () {
|
|
115
|
+
yield* bar();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it.live(
|
|
119
|
+
'Test Suite One',
|
|
120
|
+
Effect.fnUntraced(
|
|
121
|
+
function* () {
|
|
122
|
+
yield* baz();
|
|
123
|
+
},
|
|
124
|
+
Effect.provide(layerOtel(Effect.succeed({}))),
|
|
125
|
+
),
|
|
126
|
+
);
|
package/src/otel.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Resource from '@effect/opentelemetry/Resource';
|
|
6
|
+
import * as Tracer from '@effect/opentelemetry/Tracer';
|
|
7
|
+
import { type Attributes, trace } from '@opentelemetry/api';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import type * as Function from 'effect/Function';
|
|
10
|
+
import * as Layer from 'effect/Layer';
|
|
11
|
+
|
|
12
|
+
export interface Configuration {
|
|
13
|
+
readonly resource?:
|
|
14
|
+
| {
|
|
15
|
+
readonly serviceName: string;
|
|
16
|
+
readonly serviceVersion?: string;
|
|
17
|
+
readonly attributes?: Attributes;
|
|
18
|
+
}
|
|
19
|
+
| undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Based on https://github.com/Effect-TS/effect/blob/main/packages/opentelemetry/src/NodeSdk.ts
|
|
23
|
+
export const layerOtel: {
|
|
24
|
+
(evaluate: Function.LazyArg<Configuration>): Layer.Layer<Resource.Resource>;
|
|
25
|
+
<R, E>(evaluate: Effect.Effect<Configuration, E, R>): Layer.Layer<Resource.Resource, E, R>;
|
|
26
|
+
} = (
|
|
27
|
+
evaluate: Function.LazyArg<Configuration> | Effect.Effect<Configuration, any, any>,
|
|
28
|
+
): Layer.Layer<Resource.Resource> =>
|
|
29
|
+
Layer.unwrapEffect(
|
|
30
|
+
Effect.map(
|
|
31
|
+
Effect.isEffect(evaluate) ? (evaluate as Effect.Effect<Configuration>) : Effect.sync(evaluate),
|
|
32
|
+
(config) => {
|
|
33
|
+
const ResourceLive = Resource.layerFromEnv(config.resource && Resource.configToAttributes(config.resource));
|
|
34
|
+
|
|
35
|
+
const provider = trace.getTracerProvider();
|
|
36
|
+
const TracerLive = Layer.provide(Tracer.layer, Layer.succeed(Tracer.OtelTracerProvider, provider));
|
|
37
|
+
|
|
38
|
+
// TODO(wittjosiah): Add metrics and logger layers.
|
|
39
|
+
const MetricsLive = Layer.empty;
|
|
40
|
+
const LoggerLive = Layer.empty;
|
|
41
|
+
|
|
42
|
+
return Layer.mergeAll(TracerLive, MetricsLive, LoggerLive).pipe(Layer.provideMerge(ResourceLive));
|
|
43
|
+
},
|
|
44
|
+
),
|
|
45
|
+
);
|
package/src/resource.test.ts
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { it } from '@effect/vitest';
|
|
6
|
-
import
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { acquireReleaseResource } from './internal/resource';
|
|
9
9
|
|
|
10
10
|
it.effect(
|
|
11
11
|
'acquire-release',
|
|
12
12
|
Effect.fn(function* ({ expect }) {
|
|
13
13
|
const events: string[] = [];
|
|
14
|
-
const makeResource =
|
|
14
|
+
const makeResource = acquireReleaseResource(() => ({
|
|
15
15
|
open: () => {
|
|
16
16
|
events.push('open');
|
|
17
17
|
},
|
package/src/sanity.test.ts
CHANGED
|
@@ -2,14 +2,17 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Function from 'effect/Function';
|
|
6
7
|
import { describe, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { log } from '@dxos/log';
|
|
9
10
|
|
|
11
|
+
import { runAndForwardErrors } from './internal/errors';
|
|
12
|
+
|
|
10
13
|
describe('sanity tests', () => {
|
|
11
14
|
test('function pipeline', async ({ expect }) => {
|
|
12
|
-
const result = pipe(
|
|
15
|
+
const result = Function.pipe(
|
|
13
16
|
10,
|
|
14
17
|
(value) => value + 3,
|
|
15
18
|
(value) => value * 2,
|
|
@@ -18,36 +21,48 @@ describe('sanity tests', () => {
|
|
|
18
21
|
});
|
|
19
22
|
|
|
20
23
|
test('effect pipeline (mixing types)', async ({ expect }) => {
|
|
21
|
-
const result = await
|
|
22
|
-
pipe(
|
|
24
|
+
const result = await runAndForwardErrors(
|
|
25
|
+
Function.pipe(
|
|
23
26
|
Effect.promise(() => Promise.resolve(100)),
|
|
24
|
-
Effect.tap((value) =>
|
|
27
|
+
Effect.tap((value) => {
|
|
28
|
+
log('tap', { value });
|
|
29
|
+
}),
|
|
25
30
|
Effect.map((value: number) => String(value)),
|
|
26
|
-
Effect.tap((value) =>
|
|
31
|
+
Effect.tap((value) => {
|
|
32
|
+
log('tap', { value });
|
|
33
|
+
}),
|
|
27
34
|
Effect.map((value: string) => value.length),
|
|
28
|
-
Effect.tap((value) =>
|
|
35
|
+
Effect.tap((value) => {
|
|
36
|
+
log('tap', { value });
|
|
37
|
+
}),
|
|
29
38
|
),
|
|
30
39
|
);
|
|
31
40
|
expect(result).to.eq(3);
|
|
32
41
|
});
|
|
33
42
|
|
|
34
43
|
test('effect pipeline (mixing sync/async)', async ({ expect }) => {
|
|
35
|
-
const result = await
|
|
36
|
-
pipe(
|
|
44
|
+
const result = await runAndForwardErrors(
|
|
45
|
+
Function.pipe(
|
|
37
46
|
Effect.succeed(100),
|
|
38
|
-
Effect.tap((value) =>
|
|
47
|
+
Effect.tap((value) => {
|
|
48
|
+
log('tap', { value });
|
|
49
|
+
}),
|
|
39
50
|
Effect.flatMap((value) => Effect.promise(() => Promise.resolve(String(value)))),
|
|
40
|
-
Effect.tap((value) =>
|
|
51
|
+
Effect.tap((value) => {
|
|
52
|
+
log('tap', { value });
|
|
53
|
+
}),
|
|
41
54
|
Effect.map((value) => value.length),
|
|
42
|
-
Effect.tap((value) =>
|
|
55
|
+
Effect.tap((value) => {
|
|
56
|
+
log('tap', { value });
|
|
57
|
+
}),
|
|
43
58
|
),
|
|
44
59
|
);
|
|
45
60
|
expect(result).to.eq(3);
|
|
46
61
|
});
|
|
47
62
|
|
|
48
63
|
test('error handling', async ({ expect }) => {
|
|
49
|
-
|
|
50
|
-
pipe(
|
|
64
|
+
runAndForwardErrors(
|
|
65
|
+
Function.pipe(
|
|
51
66
|
Effect.succeed(10),
|
|
52
67
|
Effect.map((value) => value * 2),
|
|
53
68
|
Effect.flatMap((value) =>
|
|
@@ -62,7 +77,7 @@ describe('sanity tests', () => {
|
|
|
62
77
|
),
|
|
63
78
|
)
|
|
64
79
|
.then(() => expect.fail())
|
|
65
|
-
.catch((error) => {
|
|
80
|
+
.catch((error: any) => {
|
|
66
81
|
expect(error).to.be.instanceOf(Error);
|
|
67
82
|
});
|
|
68
83
|
});
|
package/src/testing.ts
CHANGED
|
@@ -2,24 +2,22 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
6
7
|
import type { TestContext } from 'vitest';
|
|
7
8
|
|
|
8
|
-
// TODO(dmaretskyi): Add all different test tags here.
|
|
9
|
-
export type TestTag = 'flaky' | 'llm';
|
|
10
|
-
|
|
11
9
|
export namespace TestHelpers {
|
|
12
10
|
/**
|
|
13
11
|
* Skip the test if the condition is false.
|
|
14
12
|
*
|
|
15
|
-
*
|
|
13
|
+
* Example:
|
|
16
14
|
* ```ts
|
|
17
15
|
* it.effect(
|
|
18
16
|
* 'should process an agentic loop using Claude',
|
|
19
17
|
* Effect.fn(function* ({ expect }) {
|
|
20
18
|
* // ...
|
|
21
19
|
* }),
|
|
22
|
-
* TestHelpers.runIf(process.env.
|
|
20
|
+
* TestHelpers.runIf(process.env.DX_ANTHROPIC_API_KEY),
|
|
23
21
|
* );
|
|
24
22
|
* ```
|
|
25
23
|
*/
|
|
@@ -37,14 +35,14 @@ export namespace TestHelpers {
|
|
|
37
35
|
/**
|
|
38
36
|
* Skip the test if the condition is true.
|
|
39
37
|
*
|
|
40
|
-
*
|
|
38
|
+
* Example:
|
|
41
39
|
* ```ts
|
|
42
40
|
* it.effect(
|
|
43
41
|
* 'should process an agentic loop using Claude',
|
|
44
42
|
* Effect.fn(function* ({ expect }) {
|
|
45
43
|
* // ...
|
|
46
44
|
* }),
|
|
47
|
-
* TestHelpers.skipIf(!process.env.
|
|
45
|
+
* TestHelpers.skipIf(!process.env.DX_ANTHROPIC_API_KEY),
|
|
48
46
|
* );
|
|
49
47
|
* ```
|
|
50
48
|
*/
|
|
@@ -60,20 +58,29 @@ export namespace TestHelpers {
|
|
|
60
58
|
});
|
|
61
59
|
|
|
62
60
|
/**
|
|
63
|
-
*
|
|
64
|
-
* Tags are specified in the `DX_TEST_TAGS` environment variable.
|
|
61
|
+
* Provide TestContext from test parameters.
|
|
65
62
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
63
|
+
* Example:
|
|
64
|
+
* ```ts
|
|
65
|
+
* it.effect(
|
|
66
|
+
* 'with context',
|
|
67
|
+
* Effect.fn(function* ({ expect }) {
|
|
68
|
+
* const ctx = yield* TestContextService;
|
|
69
|
+
* }),
|
|
70
|
+
* TestHelpers.provideTestContext,
|
|
71
|
+
* );
|
|
72
|
+
* ```
|
|
68
73
|
*/
|
|
69
|
-
export const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (!process.env.DX_TEST_TAGS?.includes(tag)) {
|
|
74
|
-
ctx.skip();
|
|
75
|
-
} else {
|
|
76
|
-
return yield* effect;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
74
|
+
export const provideTestContext = <A, E, R>(
|
|
75
|
+
effect: Effect.Effect<A, E, R>,
|
|
76
|
+
ctx: TestContext,
|
|
77
|
+
): Effect.Effect<A, E, Exclude<R, TestContextService>> => Effect.provideService(effect, TestContextService, ctx);
|
|
79
78
|
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Exposes vitest test context as an effect service.
|
|
82
|
+
*/
|
|
83
|
+
export class TestContextService extends Context.Tag('@dxos/effect/TestContextService')<
|
|
84
|
+
TestContextService,
|
|
85
|
+
TestContext
|
|
86
|
+
>() {}
|