@ontrails/core 1.0.0-beta.5 → 1.0.0-beta.50

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.
Files changed (252) hide show
  1. package/CHANGELOG.md +459 -14
  2. package/README.md +103 -28
  3. package/package.json +16 -1
  4. package/src/activation-provenance.ts +116 -0
  5. package/src/activation-source-compatibility.ts +430 -0
  6. package/src/activation-source-derivation.ts +227 -0
  7. package/src/activation-source.ts +93 -0
  8. package/src/blob-ref.ts +51 -0
  9. package/src/branded.ts +1 -1
  10. package/src/compose-batch.ts +69 -0
  11. package/src/compose-schema.ts +36 -0
  12. package/src/context.ts +59 -11
  13. package/src/derive.ts +271 -19
  14. package/src/detours.ts +8 -0
  15. package/src/diagnostics.ts +21 -0
  16. package/src/draft.ts +350 -0
  17. package/src/entity.ts +346 -0
  18. package/src/error-rendering.ts +87 -0
  19. package/src/errors.ts +335 -48
  20. package/src/execute.ts +1577 -0
  21. package/src/fire.ts +1172 -0
  22. package/src/glob.ts +81 -0
  23. package/src/index.ts +569 -33
  24. package/src/internal/fork-ctx.ts +69 -0
  25. package/src/layer-field-rendering.ts +193 -0
  26. package/src/layer.ts +43 -6
  27. package/src/observe.ts +361 -0
  28. package/src/path-scope.ts +66 -0
  29. package/src/path-security.ts +11 -3
  30. package/src/permits.ts +12 -0
  31. package/src/queue.ts +163 -0
  32. package/src/redaction/patterns.ts +6 -3
  33. package/src/resilience.ts +11 -11
  34. package/src/resource-config.ts +804 -0
  35. package/src/resource.ts +194 -0
  36. package/src/result.ts +18 -0
  37. package/src/run.ts +76 -0
  38. package/src/runtime-builtins.ts +69 -0
  39. package/src/schedule-runtime.ts +689 -0
  40. package/src/schedule.ts +326 -0
  41. package/src/serialization.ts +92 -55
  42. package/src/sha256.ts +136 -0
  43. package/src/signal-diagnostics.ts +633 -0
  44. package/src/signal-ref.ts +111 -0
  45. package/src/signal.ts +104 -0
  46. package/src/store/accessor-protocol.ts +56 -0
  47. package/src/store/index.ts +4 -0
  48. package/src/structured-examples.ts +248 -0
  49. package/src/surface-derivation.ts +91 -0
  50. package/src/surface-filter.ts +101 -0
  51. package/src/surface-overlay.ts +694 -0
  52. package/src/surface-versioning.ts +42 -0
  53. package/src/topo.ts +762 -38
  54. package/src/tracing.ts +346 -0
  55. package/src/trail-id-glob.ts +15 -0
  56. package/src/trail.ts +1244 -34
  57. package/src/trails/derive-trail.ts +835 -0
  58. package/src/trails/index.ts +9 -0
  59. package/src/trails/ingest.ts +152 -0
  60. package/src/trails-db.ts +212 -0
  61. package/src/transport-error-map.ts +163 -0
  62. package/src/type-utils.ts +63 -10
  63. package/src/types.ts +254 -12
  64. package/src/validate-established-topo.ts +73 -0
  65. package/src/validate-topo.ts +569 -55
  66. package/src/validation.ts +152 -4
  67. package/src/version-marker.ts +716 -0
  68. package/src/version-resolution.ts +308 -0
  69. package/src/version-runtime.ts +120 -0
  70. package/src/webhook.ts +461 -0
  71. package/src/workspace.ts +183 -16
  72. package/src/zod-wrappers.ts +72 -0
  73. package/.turbo/turbo-build.log +0 -1
  74. package/.turbo/turbo-lint.log +0 -3
  75. package/.turbo/turbo-typecheck.log +0 -1
  76. package/dist/adapters.d.ts +0 -39
  77. package/dist/adapters.d.ts.map +0 -1
  78. package/dist/adapters.js +0 -2
  79. package/dist/adapters.js.map +0 -1
  80. package/dist/blob-ref.d.ts +0 -20
  81. package/dist/blob-ref.d.ts.map +0 -1
  82. package/dist/blob-ref.js +0 -22
  83. package/dist/blob-ref.js.map +0 -1
  84. package/dist/branded.d.ts +0 -36
  85. package/dist/branded.d.ts.map +0 -1
  86. package/dist/branded.js +0 -89
  87. package/dist/branded.js.map +0 -1
  88. package/dist/collections.d.ts +0 -31
  89. package/dist/collections.d.ts.map +0 -1
  90. package/dist/collections.js +0 -60
  91. package/dist/collections.js.map +0 -1
  92. package/dist/context.d.ts +0 -10
  93. package/dist/context.d.ts.map +0 -1
  94. package/dist/context.js +0 -15
  95. package/dist/context.js.map +0 -1
  96. package/dist/derive.d.ts +0 -33
  97. package/dist/derive.d.ts.map +0 -1
  98. package/dist/derive.js +0 -125
  99. package/dist/derive.js.map +0 -1
  100. package/dist/errors.d.ts +0 -83
  101. package/dist/errors.d.ts.map +0 -1
  102. package/dist/errors.js +0 -142
  103. package/dist/errors.js.map +0 -1
  104. package/dist/event.d.ts +0 -45
  105. package/dist/event.d.ts.map +0 -1
  106. package/dist/event.js +0 -17
  107. package/dist/event.js.map +0 -1
  108. package/dist/fetch.d.ts +0 -15
  109. package/dist/fetch.d.ts.map +0 -1
  110. package/dist/fetch.js +0 -102
  111. package/dist/fetch.js.map +0 -1
  112. package/dist/guards.d.ts +0 -17
  113. package/dist/guards.d.ts.map +0 -1
  114. package/dist/guards.js +0 -25
  115. package/dist/guards.js.map +0 -1
  116. package/dist/health.d.ts +0 -18
  117. package/dist/health.d.ts.map +0 -1
  118. package/dist/health.js +0 -5
  119. package/dist/health.js.map +0 -1
  120. package/dist/index.d.ts +0 -36
  121. package/dist/index.d.ts.map +0 -1
  122. package/dist/index.js +0 -39
  123. package/dist/index.js.map +0 -1
  124. package/dist/job.d.ts +0 -24
  125. package/dist/job.d.ts.map +0 -1
  126. package/dist/job.js +0 -17
  127. package/dist/job.js.map +0 -1
  128. package/dist/layer.d.ts +0 -17
  129. package/dist/layer.d.ts.map +0 -1
  130. package/dist/layer.js +0 -21
  131. package/dist/layer.js.map +0 -1
  132. package/dist/path-security.d.ts +0 -28
  133. package/dist/path-security.d.ts.map +0 -1
  134. package/dist/path-security.js +0 -63
  135. package/dist/path-security.js.map +0 -1
  136. package/dist/patterns/bulk.d.ts +0 -15
  137. package/dist/patterns/bulk.d.ts.map +0 -1
  138. package/dist/patterns/bulk.js +0 -14
  139. package/dist/patterns/bulk.js.map +0 -1
  140. package/dist/patterns/change.d.ts +0 -10
  141. package/dist/patterns/change.d.ts.map +0 -1
  142. package/dist/patterns/change.js +0 -10
  143. package/dist/patterns/change.js.map +0 -1
  144. package/dist/patterns/date-range.d.ts +0 -10
  145. package/dist/patterns/date-range.d.ts.map +0 -1
  146. package/dist/patterns/date-range.js +0 -10
  147. package/dist/patterns/date-range.js.map +0 -1
  148. package/dist/patterns/index.d.ts +0 -9
  149. package/dist/patterns/index.d.ts.map +0 -1
  150. package/dist/patterns/index.js +0 -9
  151. package/dist/patterns/index.js.map +0 -1
  152. package/dist/patterns/pagination.d.ts +0 -18
  153. package/dist/patterns/pagination.d.ts.map +0 -1
  154. package/dist/patterns/pagination.js +0 -18
  155. package/dist/patterns/pagination.js.map +0 -1
  156. package/dist/patterns/progress.d.ts +0 -11
  157. package/dist/patterns/progress.d.ts.map +0 -1
  158. package/dist/patterns/progress.js +0 -11
  159. package/dist/patterns/progress.js.map +0 -1
  160. package/dist/patterns/sorting.d.ts +0 -13
  161. package/dist/patterns/sorting.d.ts.map +0 -1
  162. package/dist/patterns/sorting.js +0 -10
  163. package/dist/patterns/sorting.js.map +0 -1
  164. package/dist/patterns/status.d.ts +0 -15
  165. package/dist/patterns/status.d.ts.map +0 -1
  166. package/dist/patterns/status.js +0 -9
  167. package/dist/patterns/status.js.map +0 -1
  168. package/dist/patterns/timestamps.d.ts +0 -10
  169. package/dist/patterns/timestamps.d.ts.map +0 -1
  170. package/dist/patterns/timestamps.js +0 -10
  171. package/dist/patterns/timestamps.js.map +0 -1
  172. package/dist/redaction/index.d.ts +0 -4
  173. package/dist/redaction/index.d.ts.map +0 -1
  174. package/dist/redaction/index.js +0 -3
  175. package/dist/redaction/index.js.map +0 -1
  176. package/dist/redaction/patterns.d.ts +0 -9
  177. package/dist/redaction/patterns.d.ts.map +0 -1
  178. package/dist/redaction/patterns.js +0 -39
  179. package/dist/redaction/patterns.js.map +0 -1
  180. package/dist/redaction/redactor.d.ts +0 -27
  181. package/dist/redaction/redactor.d.ts.map +0 -1
  182. package/dist/redaction/redactor.js +0 -89
  183. package/dist/redaction/redactor.js.map +0 -1
  184. package/dist/resilience.d.ts +0 -34
  185. package/dist/resilience.d.ts.map +0 -1
  186. package/dist/resilience.js +0 -164
  187. package/dist/resilience.js.map +0 -1
  188. package/dist/result.d.ts +0 -57
  189. package/dist/result.d.ts.map +0 -1
  190. package/dist/result.js +0 -156
  191. package/dist/result.js.map +0 -1
  192. package/dist/serialization.d.ts +0 -27
  193. package/dist/serialization.d.ts.map +0 -1
  194. package/dist/serialization.js +0 -153
  195. package/dist/serialization.js.map +0 -1
  196. package/dist/topo.d.ts +0 -16
  197. package/dist/topo.d.ts.map +0 -1
  198. package/dist/topo.js +0 -66
  199. package/dist/topo.js.map +0 -1
  200. package/dist/trail.d.ts +0 -89
  201. package/dist/trail.d.ts.map +0 -1
  202. package/dist/trail.js +0 -18
  203. package/dist/trail.js.map +0 -1
  204. package/dist/type-utils.d.ts +0 -14
  205. package/dist/type-utils.d.ts.map +0 -1
  206. package/dist/type-utils.js +0 -12
  207. package/dist/type-utils.js.map +0 -1
  208. package/dist/types.d.ts +0 -46
  209. package/dist/types.d.ts.map +0 -1
  210. package/dist/types.js +0 -2
  211. package/dist/types.js.map +0 -1
  212. package/dist/validate-topo.d.ts +0 -24
  213. package/dist/validate-topo.d.ts.map +0 -1
  214. package/dist/validate-topo.js +0 -158
  215. package/dist/validate-topo.js.map +0 -1
  216. package/dist/validation.d.ts +0 -27
  217. package/dist/validation.d.ts.map +0 -1
  218. package/dist/validation.js +0 -134
  219. package/dist/validation.js.map +0 -1
  220. package/dist/workspace.d.ts +0 -25
  221. package/dist/workspace.d.ts.map +0 -1
  222. package/dist/workspace.js +0 -57
  223. package/dist/workspace.js.map +0 -1
  224. package/src/__tests__/blob-ref.test.ts +0 -103
  225. package/src/__tests__/branded.test.ts +0 -148
  226. package/src/__tests__/collections.test.ts +0 -126
  227. package/src/__tests__/context.test.ts +0 -66
  228. package/src/__tests__/derive.test.ts +0 -203
  229. package/src/__tests__/errors.test.ts +0 -309
  230. package/src/__tests__/event.test.ts +0 -82
  231. package/src/__tests__/fetch.test.ts +0 -217
  232. package/src/__tests__/guards.test.ts +0 -102
  233. package/src/__tests__/job.test.ts +0 -98
  234. package/src/__tests__/layer.test.ts +0 -212
  235. package/src/__tests__/path-security.test.ts +0 -114
  236. package/src/__tests__/patterns.test.ts +0 -273
  237. package/src/__tests__/redaction.test.ts +0 -244
  238. package/src/__tests__/resilience.test.ts +0 -246
  239. package/src/__tests__/result.test.ts +0 -155
  240. package/src/__tests__/serialization.test.ts +0 -401
  241. package/src/__tests__/topo.test.ts +0 -181
  242. package/src/__tests__/trail.test.ts +0 -217
  243. package/src/__tests__/type-utils.test.ts +0 -63
  244. package/src/__tests__/validate-topo.test.ts +0 -278
  245. package/src/__tests__/validation.test.ts +0 -283
  246. package/src/__tests__/workspace.test.ts +0 -183
  247. package/src/adapters.ts +0 -68
  248. package/src/event.ts +0 -77
  249. package/src/health.ts +0 -23
  250. package/src/job.ts +0 -20
  251. package/tsconfig.json +0 -9
  252. package/tsconfig.tsbuildinfo +0 -1
@@ -1,401 +0,0 @@
1
- import { describe, test, expect } from 'bun:test';
2
-
3
- import {
4
- ValidationError,
5
- AmbiguousError,
6
- AssertionError,
7
- NetworkError,
8
- RateLimitError,
9
- InternalError,
10
- TimeoutError,
11
- NotFoundError,
12
- AlreadyExistsError,
13
- ConflictError,
14
- PermissionError,
15
- AuthError,
16
- CancelledError,
17
- } from '../errors.js';
18
- import {
19
- serializeError,
20
- deserializeError,
21
- safeStringify,
22
- } from '../serialization.js';
23
- import { Result } from '../result.js';
24
- import type { SerializedError } from '../serialization.js';
25
-
26
- // ---------------------------------------------------------------------------
27
- // serializeError
28
- // ---------------------------------------------------------------------------
29
-
30
- describe('serializeError', () => {
31
- test('serializes a plain Error', () => {
32
- const err = new Error('boom');
33
- const serialized = serializeError(err);
34
- expect(serialized.name).toBe('Error');
35
- expect(serialized.message).toBe('boom');
36
- expect(serialized.stack).toBeDefined();
37
- expect(serialized.category).toBeUndefined();
38
- expect(serialized.retryable).toBeUndefined();
39
- });
40
-
41
- test('serializes a TrailsError with category', () => {
42
- const err = new NetworkError('disconnected');
43
- const serialized = serializeError(err);
44
- expect(serialized.name).toBe('NetworkError');
45
- expect(serialized.message).toBe('disconnected');
46
- expect(serialized.category).toBe('network');
47
- expect(serialized.retryable).toBe(true);
48
- });
49
-
50
- test('serializes context', () => {
51
- const err = new ValidationError('bad input', {
52
- context: { field: 'email' },
53
- });
54
- const serialized = serializeError(err);
55
- expect(serialized.context).toEqual({ field: 'email' });
56
- });
57
-
58
- test('serializes RateLimitError.retryAfter', () => {
59
- const err = new RateLimitError('slow down', { retryAfter: 30 });
60
- const serialized = serializeError(err);
61
- expect(serialized.retryAfter).toBe(30);
62
- });
63
-
64
- test('retryAfter is undefined for non-RateLimitError', () => {
65
- const err = new TimeoutError('timed out');
66
- const serialized = serializeError(err);
67
- expect(serialized.retryAfter).toBeUndefined();
68
- });
69
- });
70
-
71
- // ---------------------------------------------------------------------------
72
- // deserializeError
73
- // ---------------------------------------------------------------------------
74
-
75
- describe('deserializeError', () => {
76
- test('reconstructs a validation error', () => {
77
- const data: SerializedError = {
78
- category: 'validation',
79
- message: 'bad input',
80
- name: 'ValidationError',
81
- retryable: false,
82
- };
83
- const err = deserializeError(data);
84
- expect(err).toBeInstanceOf(ValidationError);
85
- expect(err.message).toBe('bad input');
86
- expect(err.category).toBe('validation');
87
- });
88
-
89
- test('reconstructs a rate limit error with retryAfter', () => {
90
- const data: SerializedError = {
91
- category: 'rate_limit',
92
- message: 'slow down',
93
- name: 'RateLimitError',
94
- retryAfter: 60,
95
- retryable: true,
96
- };
97
- const err = deserializeError(data);
98
- expect(err).toBeInstanceOf(RateLimitError);
99
- expect((err as RateLimitError).retryAfter).toBe(60);
100
- });
101
-
102
- test('restores stack trace', () => {
103
- const data: SerializedError = {
104
- category: 'internal',
105
- message: 'oops',
106
- name: 'InternalError',
107
- stack: 'Error: oops\n at test.ts:1:1',
108
- };
109
- const err = deserializeError(data);
110
- expect(err.stack).toBe('Error: oops\n at test.ts:1:1');
111
- });
112
-
113
- test('restores context', () => {
114
- const data: SerializedError = {
115
- category: 'auth',
116
- context: { endpoint: '/api/secret' },
117
- message: 'unauthorized',
118
- name: 'AuthError',
119
- };
120
- const err = deserializeError(data);
121
- expect(err.context).toEqual({ endpoint: '/api/secret' });
122
- });
123
-
124
- test('defaults to InternalError when category is missing', () => {
125
- const data: SerializedError = {
126
- message: 'unknown',
127
- name: 'SomeError',
128
- };
129
- const err = deserializeError(data);
130
- expect(err).toBeInstanceOf(InternalError);
131
- expect(err.category).toBe('internal');
132
- });
133
-
134
- test('round-trips through serialize/deserialize', () => {
135
- const original = new NotFoundError('missing resource', {
136
- context: { id: 'abc' },
137
- });
138
- const serialized = serializeError(original);
139
- const restored = deserializeError(serialized);
140
- expect(restored).toBeInstanceOf(NotFoundError);
141
- expect(restored.message).toBe('missing resource');
142
- expect(restored.context).toEqual({ id: 'abc' });
143
- expect(restored.category).toBe('not_found');
144
- });
145
-
146
- test('handles all error categories', () => {
147
- const categories = [
148
- 'validation',
149
- 'not_found',
150
- 'conflict',
151
- 'permission',
152
- 'timeout',
153
- 'rate_limit',
154
- 'network',
155
- 'internal',
156
- 'auth',
157
- 'cancelled',
158
- ] as const;
159
-
160
- for (const category of categories) {
161
- const data: SerializedError = {
162
- category,
163
- message: 'test',
164
- name: 'Test',
165
- };
166
- const err = deserializeError(data);
167
- expect(err.category).toBe(category);
168
- }
169
- });
170
-
171
- describe('round-trips all subclasses by name', () => {
172
- const subclasses = [
173
- { Ctor: ValidationError, category: 'validation' },
174
- { Ctor: AmbiguousError, category: 'validation' },
175
- { Ctor: AssertionError, category: 'internal' },
176
- { Ctor: NotFoundError, category: 'not_found' },
177
- { Ctor: AlreadyExistsError, category: 'conflict' },
178
- { Ctor: ConflictError, category: 'conflict' },
179
- { Ctor: PermissionError, category: 'permission' },
180
- { Ctor: TimeoutError, category: 'timeout' },
181
- { Ctor: NetworkError, category: 'network' },
182
- { Ctor: InternalError, category: 'internal' },
183
- { Ctor: AuthError, category: 'auth' },
184
- { Ctor: CancelledError, category: 'cancelled' },
185
- ] as const;
186
-
187
- test.each(subclasses)(
188
- '$Ctor.name round-trips with correct identity',
189
- ({ Ctor, category }) => {
190
- const original = new Ctor(`test ${Ctor.name}`, {
191
- context: { key: 'value' },
192
- });
193
- const serialized = serializeError(original);
194
- const restored = deserializeError(serialized);
195
-
196
- expect(restored).toBeInstanceOf(Ctor);
197
- expect(restored.constructor.name).toBe(Ctor.name);
198
- expect(restored.name).toBe(Ctor.name);
199
- expect(restored.category).toBe(category);
200
- expect(restored.message).toBe(`test ${Ctor.name}`);
201
- expect(restored.context).toEqual({ key: 'value' });
202
- }
203
- );
204
-
205
- test('RateLimitError round-trips with retryAfter', () => {
206
- const original = new RateLimitError('slow down', {
207
- context: { endpoint: '/api' },
208
- retryAfter: 42,
209
- });
210
- const serialized = serializeError(original);
211
- const restored = deserializeError(serialized);
212
-
213
- expect(restored).toBeInstanceOf(RateLimitError);
214
- expect(restored.constructor.name).toBe('RateLimitError');
215
- expect((restored as RateLimitError).retryAfter).toBe(42);
216
- expect(restored.context).toEqual({ endpoint: '/api' });
217
- });
218
-
219
- test('falls back to category when name is unknown', () => {
220
- const data: SerializedError = {
221
- category: 'conflict',
222
- message: 'custom error',
223
- name: 'CustomConflictError',
224
- };
225
- const err = deserializeError(data);
226
- expect(err).toBeInstanceOf(ConflictError);
227
- expect(err.category).toBe('conflict');
228
- });
229
- });
230
- });
231
-
232
- // ---------------------------------------------------------------------------
233
- // Result.fromJson
234
- // ---------------------------------------------------------------------------
235
-
236
- describe('Result.fromJson (safeParse)', () => {
237
- test('parses valid JSON', () => {
238
- const result = Result.fromJson('{"key": "value"}');
239
- expect(result.isOk()).toBe(true);
240
- expect(result.unwrap()).toEqual({ key: 'value' });
241
- });
242
-
243
- test('parses JSON arrays', () => {
244
- const result = Result.fromJson('[1, 2, 3]');
245
- expect(result.isOk()).toBe(true);
246
- expect(result.unwrap()).toEqual([1, 2, 3]);
247
- });
248
-
249
- test('parses JSON primitives', () => {
250
- expect(Result.fromJson('"hello"').unwrap()).toBe('hello');
251
- expect(Result.fromJson('42').unwrap()).toBe(42);
252
- expect(Result.fromJson('true').unwrap()).toBe(true);
253
- expect(Result.fromJson('null').unwrap()).toBe(null);
254
- });
255
-
256
- test('returns ValidationError for invalid JSON', () => {
257
- const result = Result.fromJson('not json');
258
- expect(result.isErr()).toBe(true);
259
- const err = result as unknown as { error: ValidationError };
260
- expect(err.error).toBeInstanceOf(ValidationError);
261
- expect(err.error.message).toBe('Invalid JSON');
262
- });
263
-
264
- test('includes truncated input in context', () => {
265
- const result = Result.fromJson('{bad}');
266
- expect(result.isErr()).toBe(true);
267
- const err = result as unknown as { error: ValidationError };
268
- expect(err.error.context).toBeDefined();
269
- expect(err.error.context?.['input']).toBe('{bad}');
270
- });
271
- });
272
-
273
- // ---------------------------------------------------------------------------
274
- // Result.toJson
275
- // ---------------------------------------------------------------------------
276
-
277
- describe('Result.toJson (safeStringify)', () => {
278
- test('stringifies objects', () => {
279
- const result = Result.toJson({ key: 'value' });
280
- expect(result.isOk()).toBe(true);
281
- expect(result.unwrap()).toBe('{"key":"value"}');
282
- });
283
-
284
- test('stringifies arrays', () => {
285
- const result = Result.toJson([1, 2, 3]);
286
- expect(result.isOk()).toBe(true);
287
- expect(result.unwrap()).toBe('[1,2,3]');
288
- });
289
-
290
- test('stringifies primitives', () => {
291
- expect(Result.toJson('hello').unwrap()).toBe('"hello"');
292
- expect(Result.toJson(42).unwrap()).toBe('42');
293
- expect(Result.toJson(true).unwrap()).toBe('true');
294
- expect(Result.toJson(null).unwrap()).toBe('null');
295
- });
296
-
297
- test('handles circular references gracefully', () => {
298
- const obj: Record<string, unknown> = { a: 1 };
299
- obj['self'] = obj;
300
- const result = Result.toJson(obj);
301
- expect(result.isOk()).toBe(true);
302
- const ok = result as unknown as { value: string };
303
- const parsed = JSON.parse(ok.value) as Record<string, unknown>;
304
- expect(parsed['a']).toBe(1);
305
- expect(parsed['self']).toBe('[Circular]');
306
- });
307
-
308
- test('serializes shared references in a DAG without marking as circular', () => {
309
- const shared = { x: 1 };
310
- const obj = { a: shared, b: shared };
311
- const result = Result.toJson(obj);
312
- expect(result.isOk()).toBe(true);
313
- const parsed = JSON.parse(result.unwrap()) as Record<string, unknown>;
314
- expect(parsed['a']).toEqual({ x: 1 });
315
- expect(parsed['b']).toEqual({ x: 1 });
316
- });
317
-
318
- test('detects deep circular references', () => {
319
- const inner: Record<string, unknown> = { value: 'deep' };
320
- const obj: Record<string, unknown> = { child: { nested: inner } };
321
- inner['back'] = obj;
322
- const result = Result.toJson(obj);
323
- expect(result.isOk()).toBe(true);
324
- const parsed = JSON.parse(result.unwrap()) as Record<string, unknown>;
325
- const child = parsed['child'] as Record<string, unknown>;
326
- const nested = child['nested'] as Record<string, unknown>;
327
- expect(nested['value']).toBe('deep');
328
- expect(nested['back']).toBe('[Circular]');
329
- });
330
-
331
- test('handles shared ref used in sibling subtrees of a DAG', () => {
332
- const shared = { id: 42 };
333
- const obj = {
334
- left: { extra: 'l', ref: shared },
335
- right: { extra: 'r', ref: shared },
336
- };
337
- const result = Result.toJson(obj);
338
- expect(result.isOk()).toBe(true);
339
- const parsed = JSON.parse(result.unwrap()) as Record<
340
- string,
341
- Record<string, unknown>
342
- >;
343
- expect(parsed['left']?.['ref']).toEqual({ id: 42 });
344
- expect(parsed['right']?.['ref']).toEqual({ id: 42 });
345
- });
346
- });
347
-
348
- // ---------------------------------------------------------------------------
349
- // safeStringify (shared DAG / circular detection)
350
- // ---------------------------------------------------------------------------
351
-
352
- describe('safeStringify', () => {
353
- test('serializes shared references in a DAG without marking as circular', () => {
354
- const shared = { x: 1 };
355
- const obj = { a: shared, b: shared };
356
- const result = safeStringify(obj);
357
- expect(result.isOk()).toBe(true);
358
- const parsed = JSON.parse(result.unwrap()) as Record<string, unknown>;
359
- expect(parsed['a']).toEqual({ x: 1 });
360
- expect(parsed['b']).toEqual({ x: 1 });
361
- });
362
-
363
- test('detects true circular references', () => {
364
- const obj: Record<string, unknown> = { a: 1 };
365
- obj['self'] = obj;
366
- const result = safeStringify(obj);
367
- expect(result.isOk()).toBe(true);
368
- const parsed = JSON.parse(result.unwrap()) as Record<string, unknown>;
369
- expect(parsed['a']).toBe(1);
370
- expect(parsed['self']).toBe('[Circular]');
371
- });
372
-
373
- test('detects deep circular references', () => {
374
- const inner: Record<string, unknown> = { value: 'deep' };
375
- const obj: Record<string, unknown> = { child: { nested: inner } };
376
- inner['back'] = obj;
377
- const result = safeStringify(obj);
378
- expect(result.isOk()).toBe(true);
379
- const parsed = JSON.parse(result.unwrap()) as Record<string, unknown>;
380
- const child = parsed['child'] as Record<string, unknown>;
381
- const nested = child['nested'] as Record<string, unknown>;
382
- expect(nested['value']).toBe('deep');
383
- expect(nested['back']).toBe('[Circular]');
384
- });
385
-
386
- test('handles shared ref used in sibling subtrees of a DAG', () => {
387
- const shared = { id: 42 };
388
- const obj = {
389
- left: { extra: 'l', ref: shared },
390
- right: { extra: 'r', ref: shared },
391
- };
392
- const result = safeStringify(obj);
393
- expect(result.isOk()).toBe(true);
394
- const parsed = JSON.parse(result.unwrap()) as Record<
395
- string,
396
- Record<string, unknown>
397
- >;
398
- expect(parsed['left']?.['ref']).toEqual({ id: 42 });
399
- expect(parsed['right']?.['ref']).toEqual({ id: 42 });
400
- });
401
- });
@@ -1,181 +0,0 @@
1
- import { describe, test, expect } from 'bun:test';
2
-
3
- import { z } from 'zod';
4
-
5
- import { ValidationError } from '../errors.js';
6
- import { Result } from '../result.js';
7
- import { topo } from '../topo.js';
8
-
9
- // ---------------------------------------------------------------------------
10
- // Mock factories
11
- // ---------------------------------------------------------------------------
12
-
13
- // oxlint-disable-next-line require-await -- satisfies async interface without needing await
14
- const noop = async () => Result.ok();
15
-
16
- const mockTrail = (id: string, follow?: readonly string[]) => ({
17
- follow: Object.freeze([...(follow ?? [])]),
18
- id,
19
- input: z.object({ x: z.number() }),
20
- kind: 'trail' as const,
21
- output: z.object({ y: z.number() }),
22
- run: noop,
23
- });
24
-
25
- const mockEvent = (id: string) => ({
26
- id,
27
- kind: 'event' as const,
28
- payload: z.object({ payload: z.string() }),
29
- });
30
-
31
- // ---------------------------------------------------------------------------
32
- // topo()
33
- // ---------------------------------------------------------------------------
34
-
35
- describe('topo', () => {
36
- describe('collection', () => {
37
- test('returns Topo with name', () => {
38
- const t = topo('my-app');
39
- expect(t.name).toBe('my-app');
40
- });
41
-
42
- test('collects trails from modules', () => {
43
- const mod = { myTrail: mockTrail('create-user') };
44
- const t = topo('app', mod);
45
-
46
- expect(t.trails.size).toBe(1);
47
- expect(t.trails.get('create-user')).toBe(mod.myTrail);
48
- });
49
-
50
- test('auto-scans exports by kind discriminant', () => {
51
- const mod = {
52
- event1: mockEvent('e1'),
53
- trail1: mockTrail('t1'),
54
- trail2: mockTrail('t2', ['t1']),
55
- };
56
- const t = topo('app', mod);
57
-
58
- expect(t.trails.size).toBe(2);
59
- expect(t.events.size).toBe(1);
60
- });
61
-
62
- test('collects from multiple modules', () => {
63
- const mod1 = { a: mockTrail('t1') };
64
- const mod2 = { b: mockTrail('t2'), c: mockTrail('t3', ['t1']) };
65
- const t = topo('app', mod1, mod2);
66
-
67
- expect(t.trails.size).toBe(3);
68
- });
69
-
70
- test('non-trail exports are silently ignored', () => {
71
- const mod = {
72
- config: { port: 3000 },
73
- helper: () => 'not a trail',
74
- name: 'some-string',
75
- nothing: null,
76
- num: 42,
77
- trail1: mockTrail('t1'),
78
- undef: undefined,
79
- };
80
- const t = topo('app', mod);
81
-
82
- expect(t.trails.size).toBe(1);
83
- expect(t.events.size).toBe(0);
84
- });
85
-
86
- test('trail with follow registers correctly', () => {
87
- const mod = { t: mockTrail('trail-1', ['trail-2']) };
88
- const t = topo('app', mod);
89
-
90
- expect(t.trails.size).toBe(1);
91
- const registered = t.trails.get('trail-1');
92
- expect(registered?.follow).toEqual(['trail-2']);
93
- });
94
- });
95
-
96
- describe('duplicate rejection', () => {
97
- test('rejects duplicate trail IDs', () => {
98
- const mod1 = { a: mockTrail('dup') };
99
- const mod2 = { b: mockTrail('dup') };
100
-
101
- expect(() => topo('app', mod1, mod2)).toThrow(ValidationError);
102
- expect(() => topo('app', mod1, mod2)).toThrow(
103
- 'Duplicate trail ID: "dup"'
104
- );
105
- });
106
-
107
- test('rejects duplicate event IDs', () => {
108
- const mod1 = { a: mockEvent('dup') };
109
- const mod2 = { b: mockEvent('dup') };
110
-
111
- expect(() => topo('app', mod1, mod2)).toThrow(ValidationError);
112
- expect(() => topo('app', mod1, mod2)).toThrow(
113
- 'Duplicate event ID: "dup"'
114
- );
115
- });
116
- });
117
- });
118
-
119
- // ---------------------------------------------------------------------------
120
- // Topo
121
- // ---------------------------------------------------------------------------
122
-
123
- describe('Topo', () => {
124
- const mod = {
125
- e1: mockEvent('event-1'),
126
- t1: mockTrail('trail-1'),
127
- t2: mockTrail('trail-2'),
128
- t3: mockTrail('trail-3', ['trail-1']),
129
- };
130
-
131
- // Build once for the describe block
132
- const app = topo('app', mod);
133
-
134
- describe('get()', () => {
135
- test('retrieves trail by ID', () => {
136
- expect(app.get('trail-1')).toBe(mod.t1);
137
- });
138
-
139
- test('retrieves trail with follow by ID', () => {
140
- expect(app.get('trail-3')).toBe(mod.t3);
141
- });
142
-
143
- test('returns undefined for unknown ID', () => {
144
- expect(app.get('nope')).toBeUndefined();
145
- });
146
- });
147
-
148
- describe('has()', () => {
149
- test('returns true for known trail', () => {
150
- expect(app.has('trail-1')).toBe(true);
151
- });
152
-
153
- test('returns true for trail with follow', () => {
154
- expect(app.has('trail-3')).toBe(true);
155
- });
156
-
157
- test('returns false for unknown ID', () => {
158
- expect(app.has('nope')).toBe(false);
159
- });
160
-
161
- test('returns false for event ID (events are not trails)', () => {
162
- expect(app.has('event-1')).toBe(false);
163
- });
164
- });
165
-
166
- describe('listing', () => {
167
- test('list() returns all trails (with and without follow)', () => {
168
- const items = app.list();
169
- expect(items).toHaveLength(3);
170
- expect(items).toContain(mod.t1);
171
- expect(items).toContain(mod.t2);
172
- expect(items).toContain(mod.t3);
173
- });
174
-
175
- test('listEvents() returns all events', () => {
176
- const items = app.listEvents();
177
- expect(items).toHaveLength(1);
178
- expect(items).toContain(mod.e1);
179
- });
180
- });
181
- });