@dxos/effect 0.8.4-main.2e9d522 → 0.8.4-main.3c1ae3b
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 +271 -230
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing.mjs +37 -0
- package/dist/lib/browser/testing.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +271 -230
- 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 +37 -0
- package/dist/lib/node-esm/testing.mjs.map +7 -0
- package/dist/types/src/ast.d.ts +35 -22
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/context.d.ts +2 -1
- package/dist/types/src/context.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +31 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -3
- package/dist/types/src/index.d.ts.map +1 -1
- 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/{jsonPath.d.ts → json-path.d.ts} +10 -2
- package/dist/types/src/json-path.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/layers.test.d.ts +2 -0
- package/dist/types/src/layers.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/resource.d.ts +6 -2
- package/dist/types/src/resource.d.ts.map +1 -1
- package/dist/types/src/testing.d.ts +33 -1
- package/dist/types/src/testing.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +3 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +27 -8
- package/src/ast.test.ts +14 -11
- package/src/ast.ts +118 -92
- package/src/context.ts +2 -1
- package/src/errors.test.ts +1 -1
- package/src/errors.ts +73 -20
- package/src/index.ts +2 -3
- package/src/interrupt.test.ts +33 -0
- package/src/{jsonPath.test.ts → json-path.test.ts} +40 -1
- package/src/{jsonPath.ts → json-path.ts} +29 -1
- package/src/layers.test.ts +110 -0
- package/src/otel.test.ts +126 -0
- package/src/otel.ts +45 -0
- package/src/resource.test.ts +5 -4
- package/src/resource.ts +8 -3
- package/src/sanity.test.ts +24 -11
- package/src/testing.ts +53 -1
- package/src/url.test.ts +1 -1
- package/src/url.ts +5 -2
- 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
|
@@ -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, getValue, isJsonPath, setValue, splitJsonPath } from './json-path';
|
|
8
8
|
|
|
9
9
|
describe('createJsonPath', () => {
|
|
10
10
|
test('supported path subset', () => {
|
|
@@ -99,3 +99,42 @@ describe('createJsonPath', () => {
|
|
|
99
99
|
expect(getField({ a: 'foo' }, 'a' as JsonPath)).toBe('foo');
|
|
100
100
|
});
|
|
101
101
|
});
|
|
102
|
+
|
|
103
|
+
describe('Types', () => {
|
|
104
|
+
test('checks sanity', async ({ expect }) => {
|
|
105
|
+
const obj = {};
|
|
106
|
+
expect(obj).to.exist;
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('get/set deep', () => {
|
|
111
|
+
test('get/set operations', ({ expect }) => {
|
|
112
|
+
const obj = {
|
|
113
|
+
name: 'test',
|
|
114
|
+
items: ['a', 'b', 'c'],
|
|
115
|
+
nested: {
|
|
116
|
+
prop: 'value',
|
|
117
|
+
arr: [1, 2, 3],
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Basic property access.
|
|
122
|
+
expect(getValue(obj, 'name' as JsonPath)).toBe('test');
|
|
123
|
+
|
|
124
|
+
// Array index access.
|
|
125
|
+
expect(getValue(obj, 'items[1]' as JsonPath)).toBe('b');
|
|
126
|
+
|
|
127
|
+
// Nested property access.
|
|
128
|
+
expect(getValue(obj, 'nested.prop' as JsonPath)).toBe('value');
|
|
129
|
+
|
|
130
|
+
// Nested array access.
|
|
131
|
+
expect(getValue(obj, 'nested.arr[2]' as JsonPath)).toBe(3);
|
|
132
|
+
|
|
133
|
+
// Setting values.
|
|
134
|
+
const updated1 = setValue(obj, 'items[1]' as JsonPath, 'x');
|
|
135
|
+
expect(updated1.items[1]).toBe('x');
|
|
136
|
+
|
|
137
|
+
const updated2 = setValue(obj, 'nested.arr[0]' as JsonPath, 99);
|
|
138
|
+
expect(updated2.nested.arr[0]).toBe(99);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -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',
|
|
@@ -94,7 +100,29 @@ export const splitJsonPath = (path: JsonPath): string[] => {
|
|
|
94
100
|
/**
|
|
95
101
|
* Applies a JsonPath to an object.
|
|
96
102
|
*/
|
|
103
|
+
// TODO(burdon): Reconcile with getValue.
|
|
97
104
|
export const getField = (object: any, path: JsonPath): any => {
|
|
98
105
|
// By default, JSONPath returns an array of results.
|
|
99
106
|
return JSONPath({ path, json: object })[0];
|
|
100
107
|
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Get value from object using JsonPath.
|
|
111
|
+
*/
|
|
112
|
+
export const getValue = <T extends object>(obj: T, path: JsonPath): any => {
|
|
113
|
+
return getDeep(
|
|
114
|
+
obj,
|
|
115
|
+
splitJsonPath(path).map((p) => p.replace(/[[\]]/g, '')),
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Set value on object using JsonPath.
|
|
121
|
+
*/
|
|
122
|
+
export const setValue = <T extends object>(obj: T, path: JsonPath, value: any): T => {
|
|
123
|
+
return setDeep(
|
|
124
|
+
obj,
|
|
125
|
+
splitJsonPath(path).map((p) => p.replace(/[[\]]/g, '')),
|
|
126
|
+
value,
|
|
127
|
+
);
|
|
128
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { it } from '@effect/vitest';
|
|
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';
|
|
11
|
+
import { test } from 'vitest';
|
|
12
|
+
|
|
13
|
+
class ClientConfig extends Context.Tag('ClientConfig')<ClientConfig, { endpoint: string }>() {}
|
|
14
|
+
|
|
15
|
+
class Client extends Context.Tag('Client')<Client, { call: () => Effect.Effect<void> }>() {
|
|
16
|
+
static layer = Layer.effect(
|
|
17
|
+
Client,
|
|
18
|
+
Effect.gen(function* () {
|
|
19
|
+
const config = yield* ClientConfig;
|
|
20
|
+
return {
|
|
21
|
+
call: () => {
|
|
22
|
+
console.log('called', config.endpoint);
|
|
23
|
+
return Effect.void;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ServerLive = Layer.scoped(
|
|
31
|
+
ClientConfig,
|
|
32
|
+
Effect.gen(function* () {
|
|
33
|
+
console.log('start server');
|
|
34
|
+
|
|
35
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
36
|
+
|
|
37
|
+
yield* Effect.addFinalizer(
|
|
38
|
+
Effect.fn(function* () {
|
|
39
|
+
yield* Effect.sleep(Duration.millis(100));
|
|
40
|
+
console.log('stop server');
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
endpoint: 'http://localhost:8080',
|
|
46
|
+
};
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
it.effect.skip(
|
|
51
|
+
'test',
|
|
52
|
+
Effect.fn(
|
|
53
|
+
function* (_) {
|
|
54
|
+
const client = yield* Client;
|
|
55
|
+
yield* client.call();
|
|
56
|
+
},
|
|
57
|
+
Effect.provide(Layer.provide(Client.layer, ServerLive)),
|
|
58
|
+
),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
class ServerPlugin {
|
|
62
|
+
#runtime = ManagedRuntime.make(ServerLive);
|
|
63
|
+
|
|
64
|
+
readonly clientConfigLayer = Layer.effectContext(
|
|
65
|
+
this.#runtime.runtimeEffect.pipe(Effect.map((rt) => rt.context.pipe(Context.pick(ClientConfig)))),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
async dispose() {
|
|
69
|
+
await this.#runtime.dispose();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class ClientPlugin {
|
|
74
|
+
constructor(private readonly _serverPlugin: ServerPlugin) {}
|
|
75
|
+
|
|
76
|
+
async run() {
|
|
77
|
+
const layer = Layer.provide(Client.layer, this._serverPlugin.clientConfigLayer);
|
|
78
|
+
|
|
79
|
+
await Effect.runPromise(
|
|
80
|
+
Effect.gen(function* () {
|
|
81
|
+
const client = yield* Client;
|
|
82
|
+
yield* client.call();
|
|
83
|
+
}).pipe(Effect.provide(layer)),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
test.skip('plugins', async () => {
|
|
89
|
+
const serverPlugin = new ServerPlugin();
|
|
90
|
+
console.log('ServerPlugin created');
|
|
91
|
+
|
|
92
|
+
await Effect.runPromise(Effect.sleep(Duration.millis(500)));
|
|
93
|
+
console.log('wake up');
|
|
94
|
+
|
|
95
|
+
{
|
|
96
|
+
const clientPlugin1 = new ClientPlugin(serverPlugin);
|
|
97
|
+
console.log('ClientPlugin1 created');
|
|
98
|
+
await clientPlugin1.run();
|
|
99
|
+
console.log('client1 run');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
{
|
|
103
|
+
const clientPlugin2 = new ClientPlugin(serverPlugin);
|
|
104
|
+
console.log('ClientPlugin2 created');
|
|
105
|
+
await clientPlugin2.run();
|
|
106
|
+
console.log('client2 run');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
await serverPlugin.dispose();
|
|
110
|
+
});
|
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,16 +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 './resource';
|
|
9
9
|
|
|
10
10
|
it.effect(
|
|
11
11
|
'acquire-release',
|
|
12
12
|
Effect.fn(function* ({ expect }) {
|
|
13
13
|
const events: string[] = [];
|
|
14
|
-
|
|
15
|
-
const makeResource = accuireReleaseResource(() => ({
|
|
14
|
+
const makeResource = acquireReleaseResource(() => ({
|
|
16
15
|
open: () => {
|
|
17
16
|
events.push('open');
|
|
18
17
|
},
|
|
@@ -20,11 +19,13 @@ it.effect(
|
|
|
20
19
|
events.push('close');
|
|
21
20
|
},
|
|
22
21
|
}));
|
|
22
|
+
|
|
23
23
|
yield* Effect.gen(function* () {
|
|
24
24
|
events.push('1');
|
|
25
25
|
const _resource = yield* makeResource;
|
|
26
26
|
events.push('2');
|
|
27
27
|
}).pipe(Effect.scoped);
|
|
28
|
+
|
|
28
29
|
events.push('3');
|
|
29
30
|
expect(events).to.deep.equal(['1', 'open', '2', 'close', '3']);
|
|
30
31
|
}),
|
package/src/resource.ts
CHANGED
|
@@ -2,12 +2,17 @@
|
|
|
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();
|
package/src/sanity.test.ts
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
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
|
|
|
10
11
|
describe('sanity tests', () => {
|
|
11
12
|
test('function pipeline', async ({ expect }) => {
|
|
12
|
-
const result = pipe(
|
|
13
|
+
const result = Function.pipe(
|
|
13
14
|
10,
|
|
14
15
|
(value) => value + 3,
|
|
15
16
|
(value) => value * 2,
|
|
@@ -19,13 +20,19 @@ describe('sanity tests', () => {
|
|
|
19
20
|
|
|
20
21
|
test('effect pipeline (mixing types)', async ({ expect }) => {
|
|
21
22
|
const result = await Effect.runPromise(
|
|
22
|
-
pipe(
|
|
23
|
+
Function.pipe(
|
|
23
24
|
Effect.promise(() => Promise.resolve(100)),
|
|
24
|
-
Effect.tap((value) =>
|
|
25
|
+
Effect.tap((value) => {
|
|
26
|
+
log('tap', { value });
|
|
27
|
+
}),
|
|
25
28
|
Effect.map((value: number) => String(value)),
|
|
26
|
-
Effect.tap((value) =>
|
|
29
|
+
Effect.tap((value) => {
|
|
30
|
+
log('tap', { value });
|
|
31
|
+
}),
|
|
27
32
|
Effect.map((value: string) => value.length),
|
|
28
|
-
Effect.tap((value) =>
|
|
33
|
+
Effect.tap((value) => {
|
|
34
|
+
log('tap', { value });
|
|
35
|
+
}),
|
|
29
36
|
),
|
|
30
37
|
);
|
|
31
38
|
expect(result).to.eq(3);
|
|
@@ -33,13 +40,19 @@ describe('sanity tests', () => {
|
|
|
33
40
|
|
|
34
41
|
test('effect pipeline (mixing sync/async)', async ({ expect }) => {
|
|
35
42
|
const result = await Effect.runPromise(
|
|
36
|
-
pipe(
|
|
43
|
+
Function.pipe(
|
|
37
44
|
Effect.succeed(100),
|
|
38
|
-
Effect.tap((value) =>
|
|
45
|
+
Effect.tap((value) => {
|
|
46
|
+
log('tap', { value });
|
|
47
|
+
}),
|
|
39
48
|
Effect.flatMap((value) => Effect.promise(() => Promise.resolve(String(value)))),
|
|
40
|
-
Effect.tap((value) =>
|
|
49
|
+
Effect.tap((value) => {
|
|
50
|
+
log('tap', { value });
|
|
51
|
+
}),
|
|
41
52
|
Effect.map((value) => value.length),
|
|
42
|
-
Effect.tap((value) =>
|
|
53
|
+
Effect.tap((value) => {
|
|
54
|
+
log('tap', { value });
|
|
55
|
+
}),
|
|
43
56
|
),
|
|
44
57
|
);
|
|
45
58
|
expect(result).to.eq(3);
|
|
@@ -47,7 +60,7 @@ describe('sanity tests', () => {
|
|
|
47
60
|
|
|
48
61
|
test('error handling', async ({ expect }) => {
|
|
49
62
|
Effect.runPromise(
|
|
50
|
-
pipe(
|
|
63
|
+
Function.pipe(
|
|
51
64
|
Effect.succeed(10),
|
|
52
65
|
Effect.map((value) => value * 2),
|
|
53
66
|
Effect.flatMap((value) =>
|
package/src/testing.ts
CHANGED
|
@@ -2,9 +2,16 @@
|
|
|
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
|
|
|
9
|
+
// TODO(dmaretskyi): Add all different test tags here.
|
|
10
|
+
export type TestTag =
|
|
11
|
+
| 'flaky' // Flaky tests.
|
|
12
|
+
| 'llm' // Tests with AI.
|
|
13
|
+
| 'sync'; // Sync with external services.
|
|
14
|
+
|
|
8
15
|
export namespace TestHelpers {
|
|
9
16
|
/**
|
|
10
17
|
* Skip the test if the condition is false.
|
|
@@ -55,4 +62,49 @@ export namespace TestHelpers {
|
|
|
55
62
|
return yield* effect;
|
|
56
63
|
}
|
|
57
64
|
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Skips this test if the tag is not in the list of tags to run.
|
|
68
|
+
* Tags are specified in the `DX_TEST_TAGS` environment variable.
|
|
69
|
+
*
|
|
70
|
+
* @param tag
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
export const taggedTest =
|
|
74
|
+
(tag: TestTag) =>
|
|
75
|
+
<A, E, R>(effect: Effect.Effect<A, E, R>, ctx: TestContext): Effect.Effect<A, E, R> =>
|
|
76
|
+
Effect.gen(function* () {
|
|
77
|
+
if (!process.env.DX_TEST_TAGS?.includes(tag)) {
|
|
78
|
+
ctx.skip();
|
|
79
|
+
} else {
|
|
80
|
+
return yield* effect;
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Provide TestContext from test parameters.
|
|
86
|
+
*
|
|
87
|
+
* Exmaple:
|
|
88
|
+
* ```ts
|
|
89
|
+
* it.effect(
|
|
90
|
+
* 'with context',
|
|
91
|
+
* Effect.fn(function* ({ expect }) {
|
|
92
|
+
* const ctx = yield* TestContextService;
|
|
93
|
+
* }),
|
|
94
|
+
* TestHelpers.provideTestContext,
|
|
95
|
+
* );
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export const provideTestContext = <A, E, R>(
|
|
99
|
+
effect: Effect.Effect<A, E, R>,
|
|
100
|
+
ctx: TestContext,
|
|
101
|
+
): Effect.Effect<A, E, Exclude<R, TestContextService>> => Effect.provideService(effect, TestContextService, ctx);
|
|
58
102
|
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Exposes vitest test context as an effect service.
|
|
106
|
+
*/
|
|
107
|
+
export class TestContextService extends Context.Tag('@dxos/effect/TestContextService')<
|
|
108
|
+
TestContextService,
|
|
109
|
+
TestContext
|
|
110
|
+
>() {}
|
package/src/url.test.ts
CHANGED
package/src/url.ts
CHANGED
|
@@ -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),
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jsonPath.d.ts","sourceRoot":"","sources":["../../../src/jsonPath.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AAKxC,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG;IAAE,UAAU,EAAE,IAAI,CAAC;IAAC,UAAU,EAAE,IAAI,CAAA;CAAE,CAAC;AACvE,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG;IAAE,UAAU,EAAE,IAAI,CAAA;CAAE,CAAC;AAKrD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAGR,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACrC,eAAO,MAAM,QAAQ,EAIT,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAEpC,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,QAEpD,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,KAAG,QAa1D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,wBAAwB,GAAI,YAAY,MAAM,KAAG,QAK7D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,QAAQ,KAAG,MAAM,EAUpD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,QAAQ,GAAG,EAAE,MAAM,QAAQ,KAAG,GAGtD,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jsonPath.test.d.ts","sourceRoot":"","sources":["../../../src/jsonPath.test.ts"],"names":[],"mappings":""}
|