@ontrails/core 1.0.0-beta.3 → 1.0.0-beta.32
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/CHANGELOG.md +323 -8
- package/README.md +114 -33
- package/package.json +11 -1
- package/src/activation-provenance.ts +111 -0
- package/src/activation-source-compatibility.ts +430 -0
- package/src/activation-source-projection.ts +221 -0
- package/src/activation-source.ts +91 -0
- package/src/blob-ref.ts +51 -0
- package/src/branded.ts +1 -1
- package/src/compose-batch.ts +69 -0
- package/src/compose-schema.ts +36 -0
- package/src/context.ts +44 -10
- package/src/contour.ts +346 -0
- package/src/derive.ts +250 -19
- package/src/detours.ts +8 -0
- package/src/diagnostics.ts +21 -0
- package/src/draft.ts +350 -0
- package/src/error-projection.ts +51 -0
- package/src/errors.ts +335 -48
- package/src/execute.ts +1577 -0
- package/src/fire.ts +1169 -0
- package/src/glob.ts +81 -0
- package/src/index.ts +526 -35
- package/src/internal/fork-ctx.ts +69 -0
- package/src/layer-projection.ts +193 -0
- package/src/layer.ts +43 -6
- package/src/observe.ts +361 -0
- package/src/path-scope.ts +66 -0
- package/src/path-security.ts +1 -1
- package/src/permits.ts +12 -0
- package/src/redaction/patterns.ts +6 -3
- package/src/resilience.ts +11 -11
- package/src/resource-config.ts +792 -0
- package/src/resource.ts +194 -0
- package/src/result.ts +18 -0
- package/src/run.ts +76 -0
- package/src/schedule-runtime.ts +689 -0
- package/src/schedule.ts +326 -0
- package/src/serialization.ts +92 -55
- package/src/signal-diagnostics.ts +633 -0
- package/src/signal-ref.ts +87 -0
- package/src/signal.ts +104 -0
- package/src/store/accessor-protocol.ts +56 -0
- package/src/store/index.ts +4 -0
- package/src/structured-examples.ts +248 -0
- package/src/surface-derivation.ts +91 -0
- package/src/surface-filter.ts +101 -0
- package/src/surface-versioning.ts +42 -0
- package/src/topo.ts +769 -67
- package/src/tracing.ts +345 -0
- package/src/trail-id-glob.ts +15 -0
- package/src/trail.ts +1168 -30
- package/src/trails/derive-trail.ts +842 -0
- package/src/trails/index.ts +9 -0
- package/src/trails/ingest.ts +139 -0
- package/src/trails-db.ts +206 -0
- package/src/transport-error-map.ts +174 -0
- package/src/type-utils.ts +87 -0
- package/src/types.ts +254 -12
- package/src/validate-established-topo.ts +69 -0
- package/src/validate-topo.ts +414 -56
- package/src/validation.ts +152 -4
- package/src/version-marker.ts +716 -0
- package/src/version-resolution.ts +308 -0
- package/src/version-runtime.ts +120 -0
- package/src/webhook.ts +278 -0
- package/src/workspace.ts +162 -1
- package/src/zod-wrappers.ts +72 -0
- package/.turbo/turbo-build.log +0 -1
- package/.turbo/turbo-lint.log +0 -3
- package/.turbo/turbo-typecheck.log +0 -1
- package/dist/adapters.d.ts +0 -39
- package/dist/adapters.d.ts.map +0 -1
- package/dist/adapters.js +0 -2
- package/dist/adapters.js.map +0 -1
- package/dist/blob-ref.d.ts +0 -20
- package/dist/blob-ref.d.ts.map +0 -1
- package/dist/blob-ref.js +0 -22
- package/dist/blob-ref.js.map +0 -1
- package/dist/branded.d.ts +0 -36
- package/dist/branded.d.ts.map +0 -1
- package/dist/branded.js +0 -89
- package/dist/branded.js.map +0 -1
- package/dist/collections.d.ts +0 -31
- package/dist/collections.d.ts.map +0 -1
- package/dist/collections.js +0 -60
- package/dist/collections.js.map +0 -1
- package/dist/context.d.ts +0 -10
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js +0 -15
- package/dist/context.js.map +0 -1
- package/dist/derive.d.ts +0 -33
- package/dist/derive.d.ts.map +0 -1
- package/dist/derive.js +0 -125
- package/dist/derive.js.map +0 -1
- package/dist/errors.d.ts +0 -83
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -142
- package/dist/errors.js.map +0 -1
- package/dist/event.d.ts +0 -45
- package/dist/event.d.ts.map +0 -1
- package/dist/event.js +0 -17
- package/dist/event.js.map +0 -1
- package/dist/fetch.d.ts +0 -15
- package/dist/fetch.d.ts.map +0 -1
- package/dist/fetch.js +0 -102
- package/dist/fetch.js.map +0 -1
- package/dist/guards.d.ts +0 -17
- package/dist/guards.d.ts.map +0 -1
- package/dist/guards.js +0 -25
- package/dist/guards.js.map +0 -1
- package/dist/health.d.ts +0 -18
- package/dist/health.d.ts.map +0 -1
- package/dist/health.js +0 -5
- package/dist/health.js.map +0 -1
- package/dist/hike.d.ts +0 -36
- package/dist/hike.d.ts.map +0 -1
- package/dist/hike.js +0 -20
- package/dist/hike.js.map +0 -1
- package/dist/index.d.ts +0 -36
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -40
- package/dist/index.js.map +0 -1
- package/dist/job.d.ts +0 -24
- package/dist/job.d.ts.map +0 -1
- package/dist/job.js +0 -17
- package/dist/job.js.map +0 -1
- package/dist/layer.d.ts +0 -17
- package/dist/layer.d.ts.map +0 -1
- package/dist/layer.js +0 -21
- package/dist/layer.js.map +0 -1
- package/dist/path-security.d.ts +0 -28
- package/dist/path-security.d.ts.map +0 -1
- package/dist/path-security.js +0 -63
- package/dist/path-security.js.map +0 -1
- package/dist/patterns/bulk.d.ts +0 -15
- package/dist/patterns/bulk.d.ts.map +0 -1
- package/dist/patterns/bulk.js +0 -14
- package/dist/patterns/bulk.js.map +0 -1
- package/dist/patterns/change.d.ts +0 -10
- package/dist/patterns/change.d.ts.map +0 -1
- package/dist/patterns/change.js +0 -10
- package/dist/patterns/change.js.map +0 -1
- package/dist/patterns/date-range.d.ts +0 -10
- package/dist/patterns/date-range.d.ts.map +0 -1
- package/dist/patterns/date-range.js +0 -10
- package/dist/patterns/date-range.js.map +0 -1
- package/dist/patterns/index.d.ts +0 -9
- package/dist/patterns/index.d.ts.map +0 -1
- package/dist/patterns/index.js +0 -9
- package/dist/patterns/index.js.map +0 -1
- package/dist/patterns/pagination.d.ts +0 -18
- package/dist/patterns/pagination.d.ts.map +0 -1
- package/dist/patterns/pagination.js +0 -18
- package/dist/patterns/pagination.js.map +0 -1
- package/dist/patterns/progress.d.ts +0 -11
- package/dist/patterns/progress.d.ts.map +0 -1
- package/dist/patterns/progress.js +0 -11
- package/dist/patterns/progress.js.map +0 -1
- package/dist/patterns/sorting.d.ts +0 -13
- package/dist/patterns/sorting.d.ts.map +0 -1
- package/dist/patterns/sorting.js +0 -10
- package/dist/patterns/sorting.js.map +0 -1
- package/dist/patterns/status.d.ts +0 -15
- package/dist/patterns/status.d.ts.map +0 -1
- package/dist/patterns/status.js +0 -9
- package/dist/patterns/status.js.map +0 -1
- package/dist/patterns/timestamps.d.ts +0 -10
- package/dist/patterns/timestamps.d.ts.map +0 -1
- package/dist/patterns/timestamps.js +0 -10
- package/dist/patterns/timestamps.js.map +0 -1
- package/dist/redaction/index.d.ts +0 -4
- package/dist/redaction/index.d.ts.map +0 -1
- package/dist/redaction/index.js +0 -3
- package/dist/redaction/index.js.map +0 -1
- package/dist/redaction/patterns.d.ts +0 -9
- package/dist/redaction/patterns.d.ts.map +0 -1
- package/dist/redaction/patterns.js +0 -39
- package/dist/redaction/patterns.js.map +0 -1
- package/dist/redaction/redactor.d.ts +0 -27
- package/dist/redaction/redactor.d.ts.map +0 -1
- package/dist/redaction/redactor.js +0 -89
- package/dist/redaction/redactor.js.map +0 -1
- package/dist/resilience.d.ts +0 -34
- package/dist/resilience.d.ts.map +0 -1
- package/dist/resilience.js +0 -164
- package/dist/resilience.js.map +0 -1
- package/dist/result.d.ts +0 -57
- package/dist/result.d.ts.map +0 -1
- package/dist/result.js +0 -156
- package/dist/result.js.map +0 -1
- package/dist/serialization.d.ts +0 -27
- package/dist/serialization.d.ts.map +0 -1
- package/dist/serialization.js +0 -153
- package/dist/serialization.js.map +0 -1
- package/dist/topo.d.ts +0 -18
- package/dist/topo.d.ts.map +0 -1
- package/dist/topo.js +0 -80
- package/dist/topo.js.map +0 -1
- package/dist/trail.d.ts +0 -83
- package/dist/trail.d.ts.map +0 -1
- package/dist/trail.js +0 -16
- package/dist/trail.js.map +0 -1
- package/dist/types.d.ts +0 -46
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/dist/validate-topo.d.ts +0 -24
- package/dist/validate-topo.d.ts.map +0 -1
- package/dist/validate-topo.js +0 -156
- package/dist/validate-topo.js.map +0 -1
- package/dist/validation.d.ts +0 -27
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -134
- package/dist/validation.js.map +0 -1
- package/dist/workspace.d.ts +0 -25
- package/dist/workspace.d.ts.map +0 -1
- package/dist/workspace.js +0 -57
- package/dist/workspace.js.map +0 -1
- package/src/__tests__/blob-ref.test.ts +0 -103
- package/src/__tests__/branded.test.ts +0 -148
- package/src/__tests__/collections.test.ts +0 -126
- package/src/__tests__/context.test.ts +0 -66
- package/src/__tests__/derive.test.ts +0 -203
- package/src/__tests__/errors.test.ts +0 -309
- package/src/__tests__/event.test.ts +0 -82
- package/src/__tests__/fetch.test.ts +0 -217
- package/src/__tests__/guards.test.ts +0 -102
- package/src/__tests__/hike.test.ts +0 -117
- package/src/__tests__/job.test.ts +0 -98
- package/src/__tests__/layer.test.ts +0 -224
- package/src/__tests__/path-security.test.ts +0 -114
- package/src/__tests__/patterns.test.ts +0 -273
- package/src/__tests__/redaction.test.ts +0 -244
- package/src/__tests__/resilience.test.ts +0 -246
- package/src/__tests__/result.test.ts +0 -155
- package/src/__tests__/serialization.test.ts +0 -401
- package/src/__tests__/topo.test.ts +0 -220
- package/src/__tests__/trail.test.ts +0 -179
- package/src/__tests__/validate-topo.test.ts +0 -280
- package/src/__tests__/validation.test.ts +0 -283
- package/src/__tests__/workspace.test.ts +0 -183
- package/src/adapters.ts +0 -68
- package/src/event.ts +0 -77
- package/src/health.ts +0 -23
- package/src/hike.ts +0 -77
- package/src/job.ts +0 -20
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from 'bun:test';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
NetworkError,
|
|
5
|
-
ValidationError,
|
|
6
|
-
TimeoutError,
|
|
7
|
-
CancelledError,
|
|
8
|
-
RateLimitError,
|
|
9
|
-
} from '../errors.js';
|
|
10
|
-
import {
|
|
11
|
-
retry,
|
|
12
|
-
withTimeout,
|
|
13
|
-
shouldRetry,
|
|
14
|
-
getBackoffDelay,
|
|
15
|
-
} from '../resilience.js';
|
|
16
|
-
import { Result } from '../result.js';
|
|
17
|
-
|
|
18
|
-
// ---------------------------------------------------------------------------
|
|
19
|
-
// shouldRetry
|
|
20
|
-
// ---------------------------------------------------------------------------
|
|
21
|
-
|
|
22
|
-
describe('shouldRetry', () => {
|
|
23
|
-
test('returns true for retryable TrailsErrors', () => {
|
|
24
|
-
expect(shouldRetry(new NetworkError('down'))).toBe(true);
|
|
25
|
-
expect(shouldRetry(new TimeoutError('slow'))).toBe(true);
|
|
26
|
-
expect(shouldRetry(new RateLimitError('throttled'))).toBe(true);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test('returns false for non-retryable TrailsErrors', () => {
|
|
30
|
-
expect(shouldRetry(new ValidationError('bad'))).toBe(false);
|
|
31
|
-
expect(shouldRetry(new CancelledError('stop'))).toBe(false);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('returns false for plain Error', () => {
|
|
35
|
-
expect(shouldRetry(new Error('generic'))).toBe(false);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// ---------------------------------------------------------------------------
|
|
40
|
-
// getBackoffDelay
|
|
41
|
-
// ---------------------------------------------------------------------------
|
|
42
|
-
|
|
43
|
-
describe('getBackoffDelay', () => {
|
|
44
|
-
test('returns a number >= 0', () => {
|
|
45
|
-
for (let i = 0; i < 20; i += 1) {
|
|
46
|
-
const delay = getBackoffDelay(0);
|
|
47
|
-
expect(delay).toBeGreaterThanOrEqual(0);
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test('respects maxDelay cap', () => {
|
|
52
|
-
for (let i = 0; i < 20; i += 1) {
|
|
53
|
-
const delay = getBackoffDelay(10, { maxDelay: 100 });
|
|
54
|
-
expect(delay).toBeLessThanOrEqual(100);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('uses provided baseDelay and backoffFactor', () => {
|
|
59
|
-
// With attempt=0, exponential = base * factor^0 = base
|
|
60
|
-
// Jitter is [0, base], so delay <= base
|
|
61
|
-
for (let i = 0; i < 20; i += 1) {
|
|
62
|
-
const delay = getBackoffDelay(0, { backoffFactor: 3, baseDelay: 500 });
|
|
63
|
-
expect(delay).toBeLessThanOrEqual(500);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test('scales with attempt number', () => {
|
|
68
|
-
// Higher attempts should have higher caps (on average)
|
|
69
|
-
const samples0 = Array.from({ length: 100 }, () =>
|
|
70
|
-
getBackoffDelay(0, { baseDelay: 100 })
|
|
71
|
-
);
|
|
72
|
-
const samples3 = Array.from({ length: 100 }, () =>
|
|
73
|
-
getBackoffDelay(3, { baseDelay: 100 })
|
|
74
|
-
);
|
|
75
|
-
const avg0 = samples0.reduce((a, b) => a + b, 0) / samples0.length;
|
|
76
|
-
const avg3 = samples3.reduce((a, b) => a + b, 0) / samples3.length;
|
|
77
|
-
// Attempt 3 should have a noticeably higher average
|
|
78
|
-
expect(avg3).toBeGreaterThan(avg0);
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// ---------------------------------------------------------------------------
|
|
83
|
-
// retry
|
|
84
|
-
// ---------------------------------------------------------------------------
|
|
85
|
-
|
|
86
|
-
describe('retry', () => {
|
|
87
|
-
test('returns Ok on first success', async () => {
|
|
88
|
-
let calls = 0;
|
|
89
|
-
const result = await retry(() => {
|
|
90
|
-
calls += 1;
|
|
91
|
-
return Promise.resolve(Result.ok('done'));
|
|
92
|
-
});
|
|
93
|
-
expect(result.isOk()).toBe(true);
|
|
94
|
-
expect(result.unwrap()).toBe('done');
|
|
95
|
-
expect(calls).toBe(1);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test('retries on retryable error', async () => {
|
|
99
|
-
const responses: Result<string, Error>[] = [
|
|
100
|
-
Result.err(new NetworkError('fail')),
|
|
101
|
-
Result.err(new NetworkError('fail')),
|
|
102
|
-
Result.ok('recovered'),
|
|
103
|
-
];
|
|
104
|
-
let calls = 0;
|
|
105
|
-
const result = await retry(
|
|
106
|
-
() => {
|
|
107
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- index is bounded by maxAttempts
|
|
108
|
-
const r = responses[calls]!;
|
|
109
|
-
calls += 1;
|
|
110
|
-
return Promise.resolve(r);
|
|
111
|
-
},
|
|
112
|
-
{ baseDelay: 1, maxAttempts: 3 }
|
|
113
|
-
);
|
|
114
|
-
expect(result.isOk()).toBe(true);
|
|
115
|
-
expect(result.unwrap()).toBe('recovered');
|
|
116
|
-
expect(calls).toBe(3);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('does not retry non-retryable errors', async () => {
|
|
120
|
-
let calls = 0;
|
|
121
|
-
const result = await retry(
|
|
122
|
-
() => {
|
|
123
|
-
calls += 1;
|
|
124
|
-
return Promise.resolve(Result.err(new ValidationError('bad')));
|
|
125
|
-
},
|
|
126
|
-
{ baseDelay: 1, maxAttempts: 3 }
|
|
127
|
-
);
|
|
128
|
-
expect(result.isErr()).toBe(true);
|
|
129
|
-
expect(calls).toBe(1);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
test('gives up after maxAttempts', async () => {
|
|
133
|
-
let calls = 0;
|
|
134
|
-
const result = await retry(
|
|
135
|
-
() => {
|
|
136
|
-
calls += 1;
|
|
137
|
-
return Promise.resolve(Result.err(new NetworkError('fail')));
|
|
138
|
-
},
|
|
139
|
-
{ baseDelay: 1, maxAttempts: 2 }
|
|
140
|
-
);
|
|
141
|
-
expect(result.isErr()).toBe(true);
|
|
142
|
-
expect(calls).toBe(2);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
test('respects custom shouldRetry predicate', async () => {
|
|
146
|
-
let calls = 0;
|
|
147
|
-
const result = await retry(
|
|
148
|
-
() => {
|
|
149
|
-
calls += 1;
|
|
150
|
-
return Promise.resolve(Result.err(new ValidationError('bad')));
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
baseDelay: 1,
|
|
154
|
-
maxAttempts: 3,
|
|
155
|
-
// always retry
|
|
156
|
-
shouldRetry: () => true,
|
|
157
|
-
}
|
|
158
|
-
);
|
|
159
|
-
expect(result.isErr()).toBe(true);
|
|
160
|
-
expect(calls).toBe(3);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
test('cancels on abort signal', async () => {
|
|
164
|
-
const controller = new AbortController();
|
|
165
|
-
controller.abort();
|
|
166
|
-
|
|
167
|
-
const result = await retry(
|
|
168
|
-
() => Promise.resolve(Result.ok('should not run')),
|
|
169
|
-
{
|
|
170
|
-
signal: controller.signal,
|
|
171
|
-
}
|
|
172
|
-
);
|
|
173
|
-
expect(result.isErr()).toBe(true);
|
|
174
|
-
const err = result as unknown as { error: Error };
|
|
175
|
-
expect(err.error).toBeInstanceOf(CancelledError);
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// ---------------------------------------------------------------------------
|
|
180
|
-
// withTimeout
|
|
181
|
-
// ---------------------------------------------------------------------------
|
|
182
|
-
|
|
183
|
-
describe('withTimeout', () => {
|
|
184
|
-
test('returns result when function completes in time', async () => {
|
|
185
|
-
const result = await withTimeout(
|
|
186
|
-
() => Promise.resolve(Result.ok('fast')),
|
|
187
|
-
1000
|
|
188
|
-
);
|
|
189
|
-
expect(result.isOk()).toBe(true);
|
|
190
|
-
expect(result.unwrap()).toBe('fast');
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
test('returns TimeoutError when function exceeds timeout', async () => {
|
|
194
|
-
const result = await withTimeout(
|
|
195
|
-
() =>
|
|
196
|
-
// oxlint-disable-next-line avoid-new -- Promise constructor needed for setTimeout-based delay
|
|
197
|
-
new Promise<Result<string, Error>>((resolve) => {
|
|
198
|
-
setTimeout(() => resolve(Result.ok('slow')), 500);
|
|
199
|
-
}),
|
|
200
|
-
10
|
|
201
|
-
);
|
|
202
|
-
expect(result.isErr()).toBe(true);
|
|
203
|
-
const err1 = result as unknown as { error: TimeoutError };
|
|
204
|
-
expect(err1.error).toBeInstanceOf(TimeoutError);
|
|
205
|
-
expect(err1.error.message).toContain('timed out');
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
test('returns CancelledError when signal is already aborted', async () => {
|
|
209
|
-
const controller = new AbortController();
|
|
210
|
-
controller.abort();
|
|
211
|
-
|
|
212
|
-
const result = await withTimeout(
|
|
213
|
-
() => Promise.resolve(Result.ok('value')),
|
|
214
|
-
1000,
|
|
215
|
-
controller.signal
|
|
216
|
-
);
|
|
217
|
-
expect(result.isErr()).toBe(true);
|
|
218
|
-
const err2 = result as unknown as { error: Error };
|
|
219
|
-
expect(err2.error).toBeInstanceOf(CancelledError);
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
test('handles function that throws', async () => {
|
|
223
|
-
const result = await withTimeout(
|
|
224
|
-
() => Promise.reject(new Error('unexpected')),
|
|
225
|
-
1000
|
|
226
|
-
);
|
|
227
|
-
expect(result.isErr()).toBe(true);
|
|
228
|
-
const err3 = result as unknown as { error: Error };
|
|
229
|
-
expect(err3.error.message).toBe('unexpected');
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
test('includes timeout ms in error context', async () => {
|
|
233
|
-
const result = await withTimeout(
|
|
234
|
-
() =>
|
|
235
|
-
// oxlint-disable-next-line avoid-new -- Promise constructor needed for setTimeout-based delay
|
|
236
|
-
new Promise<Result<string, Error>>((resolve) => {
|
|
237
|
-
setTimeout(() => resolve(Result.ok('slow')), 500);
|
|
238
|
-
}),
|
|
239
|
-
5
|
|
240
|
-
);
|
|
241
|
-
expect(result.isErr()).toBe(true);
|
|
242
|
-
const err4 = result as unknown as { error: TimeoutError };
|
|
243
|
-
expect(err4.error).toBeInstanceOf(TimeoutError);
|
|
244
|
-
expect(err4.error.context?.['timeoutMs']).toBe(5);
|
|
245
|
-
});
|
|
246
|
-
});
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from 'bun:test';
|
|
2
|
-
|
|
3
|
-
import { Result } from '../result';
|
|
4
|
-
|
|
5
|
-
describe('Result', () => {
|
|
6
|
-
describe('construction', () => {
|
|
7
|
-
test('Result.ok() creates a success result', () => {
|
|
8
|
-
const result = Result.ok(42);
|
|
9
|
-
expect(result.isOk()).toBe(true);
|
|
10
|
-
expect(result.isErr()).toBe(false);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('Result.err() creates a failure result', () => {
|
|
14
|
-
const result = Result.err(new Error('fail'));
|
|
15
|
-
expect(result.isOk()).toBe(false);
|
|
16
|
-
expect(result.isErr()).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('type narrowing', () => {
|
|
21
|
-
test('isOk() narrows to Ok with value access', () => {
|
|
22
|
-
const result: Result<number, Error> = Result.ok(42);
|
|
23
|
-
expect(result.isOk()).toBe(true);
|
|
24
|
-
expect(result.unwrap()).toBe(42);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('isErr() narrows to Err with error access', () => {
|
|
28
|
-
const error = new Error('boom');
|
|
29
|
-
const result: Result<number, Error> = Result.err(error);
|
|
30
|
-
expect(result.isErr()).toBe(true);
|
|
31
|
-
expect((result as { error: Error }).error).toBe(error);
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe('map()', () => {
|
|
36
|
-
test('transforms the value of an Ok', () => {
|
|
37
|
-
const result = Result.ok(10).map((n) => n * 2);
|
|
38
|
-
expect(result.unwrap()).toBe(20);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test('passes through on Err', () => {
|
|
42
|
-
const error = new Error('fail');
|
|
43
|
-
const result = Result.err<Error>(error).map((_n: number) => 999);
|
|
44
|
-
expect(result.isErr()).toBe(true);
|
|
45
|
-
expect((result as { error: Error }).error).toBe(error);
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
describe('flatMap()', () => {
|
|
50
|
-
test('chains successful results', () => {
|
|
51
|
-
const result = Result.ok(10).flatMap((n) => Result.ok(n + 5));
|
|
52
|
-
expect(result.unwrap()).toBe(15);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test('short-circuits on error', () => {
|
|
56
|
-
const error = new Error('first');
|
|
57
|
-
const result = Result.err<Error>(error).flatMap((_n: number) =>
|
|
58
|
-
Result.ok(999)
|
|
59
|
-
);
|
|
60
|
-
expect(result.isErr()).toBe(true);
|
|
61
|
-
expect((result as { error: Error }).error).toBe(error);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('chains into an error', () => {
|
|
65
|
-
const result = Result.ok(10).flatMap(() =>
|
|
66
|
-
Result.err(new Error('second'))
|
|
67
|
-
);
|
|
68
|
-
expect(result.isErr()).toBe(true);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
describe('mapErr()', () => {
|
|
73
|
-
test('transforms the error of an Err', () => {
|
|
74
|
-
const result = Result.err('bad').mapErr((e) => new Error(e));
|
|
75
|
-
expect(result.isErr()).toBe(true);
|
|
76
|
-
expect((result as { error: Error }).error).toBeInstanceOf(Error);
|
|
77
|
-
expect((result as { error: Error }).error.message).toBe('bad');
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test('passes through on Ok', () => {
|
|
81
|
-
const result = Result.ok(42).mapErr(() => 'transformed');
|
|
82
|
-
expect(result.unwrap()).toBe(42);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
describe('match()', () => {
|
|
87
|
-
test('dispatches to ok handler on success', () => {
|
|
88
|
-
const output = Result.ok(5).match({
|
|
89
|
-
err: (e) => `error:${e}`,
|
|
90
|
-
ok: (v) => `value:${v}`,
|
|
91
|
-
});
|
|
92
|
-
expect(output).toBe('value:5');
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test('dispatches to err handler on failure', () => {
|
|
96
|
-
const output = Result.err('oops').match({
|
|
97
|
-
err: (e) => `error:${e}`,
|
|
98
|
-
ok: (v) => `value:${v}`,
|
|
99
|
-
});
|
|
100
|
-
expect(output).toBe('error:oops');
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
describe('unwrap()', () => {
|
|
105
|
-
test('returns value on Ok', () => {
|
|
106
|
-
expect(Result.ok('hello').unwrap()).toBe('hello');
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test('throws on Err with Error instance', () => {
|
|
110
|
-
const error = new Error('boom');
|
|
111
|
-
expect(() => Result.err(error).unwrap()).toThrow('boom');
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test('throws wrapped error on Err with non-Error value', () => {
|
|
115
|
-
expect(() => Result.err('string error').unwrap()).toThrow('string error');
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
describe('unwrapOr()', () => {
|
|
120
|
-
test('returns value on Ok', () => {
|
|
121
|
-
expect(Result.ok(42).unwrapOr(0)).toBe(42);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test('returns fallback on Err', () => {
|
|
125
|
-
const result: Result<number, Error> = Result.err(new Error('fail'));
|
|
126
|
-
expect(result.unwrapOr(0)).toBe(0);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
describe('Result.combine()', () => {
|
|
131
|
-
test('collects all Ok values into an array', () => {
|
|
132
|
-
const results = [Result.ok(1), Result.ok(2), Result.ok(3)];
|
|
133
|
-
const combined = Result.combine(results);
|
|
134
|
-
expect(combined.unwrap()).toEqual([1, 2, 3]);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test('returns first error encountered', () => {
|
|
138
|
-
const err1 = new Error('first');
|
|
139
|
-
const err2 = new Error('second');
|
|
140
|
-
const results: Result<number, Error>[] = [
|
|
141
|
-
Result.ok(1),
|
|
142
|
-
Result.err(err1),
|
|
143
|
-
Result.err(err2),
|
|
144
|
-
];
|
|
145
|
-
const combined = Result.combine(results);
|
|
146
|
-
expect(combined.isErr()).toBe(true);
|
|
147
|
-
expect((combined as { error: Error }).error).toBe(err1);
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
test('returns Ok with empty array for empty input', () => {
|
|
151
|
-
const combined = Result.combine([]);
|
|
152
|
-
expect(combined.unwrap()).toEqual([]);
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
});
|