@ontrails/core 1.0.0-beta.0

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 (216) hide show
  1. package/.turbo/turbo-build.log +1 -0
  2. package/.turbo/turbo-lint.log +3 -0
  3. package/.turbo/turbo-typecheck.log +1 -0
  4. package/CHANGELOG.md +15 -0
  5. package/README.md +179 -0
  6. package/dist/adapters.d.ts +39 -0
  7. package/dist/adapters.d.ts.map +1 -0
  8. package/dist/adapters.js +2 -0
  9. package/dist/adapters.js.map +1 -0
  10. package/dist/blob-ref.d.ts +20 -0
  11. package/dist/blob-ref.d.ts.map +1 -0
  12. package/dist/blob-ref.js +22 -0
  13. package/dist/blob-ref.js.map +1 -0
  14. package/dist/branded.d.ts +36 -0
  15. package/dist/branded.d.ts.map +1 -0
  16. package/dist/branded.js +89 -0
  17. package/dist/branded.js.map +1 -0
  18. package/dist/collections.d.ts +31 -0
  19. package/dist/collections.d.ts.map +1 -0
  20. package/dist/collections.js +60 -0
  21. package/dist/collections.js.map +1 -0
  22. package/dist/context.d.ts +10 -0
  23. package/dist/context.d.ts.map +1 -0
  24. package/dist/context.js +15 -0
  25. package/dist/context.js.map +1 -0
  26. package/dist/derive.d.ts +33 -0
  27. package/dist/derive.d.ts.map +1 -0
  28. package/dist/derive.js +122 -0
  29. package/dist/derive.js.map +1 -0
  30. package/dist/errors.d.ts +83 -0
  31. package/dist/errors.d.ts.map +1 -0
  32. package/dist/errors.js +142 -0
  33. package/dist/errors.js.map +1 -0
  34. package/dist/event.d.ts +45 -0
  35. package/dist/event.d.ts.map +1 -0
  36. package/dist/event.js +17 -0
  37. package/dist/event.js.map +1 -0
  38. package/dist/fetch.d.ts +15 -0
  39. package/dist/fetch.d.ts.map +1 -0
  40. package/dist/fetch.js +102 -0
  41. package/dist/fetch.js.map +1 -0
  42. package/dist/guards.d.ts +17 -0
  43. package/dist/guards.d.ts.map +1 -0
  44. package/dist/guards.js +25 -0
  45. package/dist/guards.js.map +1 -0
  46. package/dist/health.d.ts +18 -0
  47. package/dist/health.d.ts.map +1 -0
  48. package/dist/health.js +5 -0
  49. package/dist/health.js.map +1 -0
  50. package/dist/hike.d.ts +36 -0
  51. package/dist/hike.d.ts.map +1 -0
  52. package/dist/hike.js +20 -0
  53. package/dist/hike.js.map +1 -0
  54. package/dist/index.d.ts +34 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +38 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/job.d.ts +24 -0
  59. package/dist/job.d.ts.map +1 -0
  60. package/dist/job.js +17 -0
  61. package/dist/job.js.map +1 -0
  62. package/dist/layer.d.ts +17 -0
  63. package/dist/layer.d.ts.map +1 -0
  64. package/dist/layer.js +21 -0
  65. package/dist/layer.js.map +1 -0
  66. package/dist/path-security.d.ts +28 -0
  67. package/dist/path-security.d.ts.map +1 -0
  68. package/dist/path-security.js +63 -0
  69. package/dist/path-security.js.map +1 -0
  70. package/dist/patterns/bulk.d.ts +15 -0
  71. package/dist/patterns/bulk.d.ts.map +1 -0
  72. package/dist/patterns/bulk.js +14 -0
  73. package/dist/patterns/bulk.js.map +1 -0
  74. package/dist/patterns/change.d.ts +10 -0
  75. package/dist/patterns/change.d.ts.map +1 -0
  76. package/dist/patterns/change.js +10 -0
  77. package/dist/patterns/change.js.map +1 -0
  78. package/dist/patterns/date-range.d.ts +10 -0
  79. package/dist/patterns/date-range.d.ts.map +1 -0
  80. package/dist/patterns/date-range.js +10 -0
  81. package/dist/patterns/date-range.js.map +1 -0
  82. package/dist/patterns/index.d.ts +9 -0
  83. package/dist/patterns/index.d.ts.map +1 -0
  84. package/dist/patterns/index.js +9 -0
  85. package/dist/patterns/index.js.map +1 -0
  86. package/dist/patterns/pagination.d.ts +18 -0
  87. package/dist/patterns/pagination.d.ts.map +1 -0
  88. package/dist/patterns/pagination.js +18 -0
  89. package/dist/patterns/pagination.js.map +1 -0
  90. package/dist/patterns/progress.d.ts +11 -0
  91. package/dist/patterns/progress.d.ts.map +1 -0
  92. package/dist/patterns/progress.js +11 -0
  93. package/dist/patterns/progress.js.map +1 -0
  94. package/dist/patterns/sorting.d.ts +13 -0
  95. package/dist/patterns/sorting.d.ts.map +1 -0
  96. package/dist/patterns/sorting.js +10 -0
  97. package/dist/patterns/sorting.js.map +1 -0
  98. package/dist/patterns/status.d.ts +15 -0
  99. package/dist/patterns/status.d.ts.map +1 -0
  100. package/dist/patterns/status.js +9 -0
  101. package/dist/patterns/status.js.map +1 -0
  102. package/dist/patterns/timestamps.d.ts +10 -0
  103. package/dist/patterns/timestamps.d.ts.map +1 -0
  104. package/dist/patterns/timestamps.js +10 -0
  105. package/dist/patterns/timestamps.js.map +1 -0
  106. package/dist/redaction/index.d.ts +4 -0
  107. package/dist/redaction/index.d.ts.map +1 -0
  108. package/dist/redaction/index.js +3 -0
  109. package/dist/redaction/index.js.map +1 -0
  110. package/dist/redaction/patterns.d.ts +9 -0
  111. package/dist/redaction/patterns.d.ts.map +1 -0
  112. package/dist/redaction/patterns.js +39 -0
  113. package/dist/redaction/patterns.js.map +1 -0
  114. package/dist/redaction/redactor.d.ts +27 -0
  115. package/dist/redaction/redactor.d.ts.map +1 -0
  116. package/dist/redaction/redactor.js +89 -0
  117. package/dist/redaction/redactor.js.map +1 -0
  118. package/dist/resilience.d.ts +34 -0
  119. package/dist/resilience.d.ts.map +1 -0
  120. package/dist/resilience.js +164 -0
  121. package/dist/resilience.js.map +1 -0
  122. package/dist/result.d.ts +57 -0
  123. package/dist/result.d.ts.map +1 -0
  124. package/dist/result.js +145 -0
  125. package/dist/result.js.map +1 -0
  126. package/dist/serialization.d.ts +27 -0
  127. package/dist/serialization.d.ts.map +1 -0
  128. package/dist/serialization.js +115 -0
  129. package/dist/serialization.js.map +1 -0
  130. package/dist/topo.d.ts +18 -0
  131. package/dist/topo.d.ts.map +1 -0
  132. package/dist/topo.js +74 -0
  133. package/dist/topo.js.map +1 -0
  134. package/dist/trail.d.ts +83 -0
  135. package/dist/trail.d.ts.map +1 -0
  136. package/dist/trail.js +16 -0
  137. package/dist/trail.js.map +1 -0
  138. package/dist/types.d.ts +46 -0
  139. package/dist/types.d.ts.map +1 -0
  140. package/dist/types.js +2 -0
  141. package/dist/types.js.map +1 -0
  142. package/dist/validate-topo.d.ts +24 -0
  143. package/dist/validate-topo.d.ts.map +1 -0
  144. package/dist/validate-topo.js +108 -0
  145. package/dist/validate-topo.js.map +1 -0
  146. package/dist/validation.d.ts +27 -0
  147. package/dist/validation.d.ts.map +1 -0
  148. package/dist/validation.js +134 -0
  149. package/dist/validation.js.map +1 -0
  150. package/dist/workspace.d.ts +25 -0
  151. package/dist/workspace.d.ts.map +1 -0
  152. package/dist/workspace.js +57 -0
  153. package/dist/workspace.js.map +1 -0
  154. package/package.json +21 -0
  155. package/src/__tests__/blob-ref.test.ts +103 -0
  156. package/src/__tests__/branded.test.ts +148 -0
  157. package/src/__tests__/collections.test.ts +126 -0
  158. package/src/__tests__/context.test.ts +66 -0
  159. package/src/__tests__/derive.test.ts +159 -0
  160. package/src/__tests__/errors.test.ts +309 -0
  161. package/src/__tests__/event.test.ts +82 -0
  162. package/src/__tests__/fetch.test.ts +217 -0
  163. package/src/__tests__/guards.test.ts +102 -0
  164. package/src/__tests__/hike.test.ts +117 -0
  165. package/src/__tests__/job.test.ts +98 -0
  166. package/src/__tests__/layer.test.ts +224 -0
  167. package/src/__tests__/path-security.test.ts +114 -0
  168. package/src/__tests__/patterns.test.ts +273 -0
  169. package/src/__tests__/redaction.test.ts +244 -0
  170. package/src/__tests__/resilience.test.ts +246 -0
  171. package/src/__tests__/result.test.ts +155 -0
  172. package/src/__tests__/serialization.test.ts +236 -0
  173. package/src/__tests__/topo.test.ts +184 -0
  174. package/src/__tests__/trail.test.ts +179 -0
  175. package/src/__tests__/validate-topo.test.ts +201 -0
  176. package/src/__tests__/validation.test.ts +283 -0
  177. package/src/__tests__/workspace.test.ts +183 -0
  178. package/src/adapters.ts +68 -0
  179. package/src/blob-ref.ts +39 -0
  180. package/src/branded.ts +135 -0
  181. package/src/collections.ts +99 -0
  182. package/src/context.ts +18 -0
  183. package/src/derive.ts +223 -0
  184. package/src/errors.ts +196 -0
  185. package/src/event.ts +77 -0
  186. package/src/fetch.ts +138 -0
  187. package/src/guards.ts +37 -0
  188. package/src/health.ts +23 -0
  189. package/src/hike.ts +77 -0
  190. package/src/index.ts +158 -0
  191. package/src/job.ts +20 -0
  192. package/src/layer.ts +44 -0
  193. package/src/path-security.ts +90 -0
  194. package/src/patterns/bulk.ts +16 -0
  195. package/src/patterns/change.ts +12 -0
  196. package/src/patterns/date-range.ts +12 -0
  197. package/src/patterns/index.ts +8 -0
  198. package/src/patterns/pagination.ts +22 -0
  199. package/src/patterns/progress.ts +13 -0
  200. package/src/patterns/sorting.ts +14 -0
  201. package/src/patterns/status.ts +11 -0
  202. package/src/patterns/timestamps.ts +12 -0
  203. package/src/redaction/index.ts +3 -0
  204. package/src/redaction/patterns.ts +47 -0
  205. package/src/redaction/redactor.ts +178 -0
  206. package/src/resilience.ts +234 -0
  207. package/src/result.ts +180 -0
  208. package/src/serialization.ts +183 -0
  209. package/src/topo.ts +123 -0
  210. package/src/trail.ts +130 -0
  211. package/src/types.ts +58 -0
  212. package/src/validate-topo.ts +151 -0
  213. package/src/validation.ts +182 -0
  214. package/src/workspace.ts +77 -0
  215. package/tsconfig.json +9 -0
  216. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,236 @@
1
+ import { describe, test, expect } from 'bun:test';
2
+
3
+ import {
4
+ ValidationError,
5
+ NetworkError,
6
+ RateLimitError,
7
+ InternalError,
8
+ TimeoutError,
9
+ NotFoundError,
10
+ } from '../errors.js';
11
+ import { serializeError, deserializeError } from '../serialization.js';
12
+ import { Result } from '../result.js';
13
+ import type { SerializedError } from '../serialization.js';
14
+
15
+ // ---------------------------------------------------------------------------
16
+ // serializeError
17
+ // ---------------------------------------------------------------------------
18
+
19
+ describe('serializeError', () => {
20
+ test('serializes a plain Error', () => {
21
+ const err = new Error('boom');
22
+ const serialized = serializeError(err);
23
+ expect(serialized.name).toBe('Error');
24
+ expect(serialized.message).toBe('boom');
25
+ expect(serialized.stack).toBeDefined();
26
+ expect(serialized.category).toBeUndefined();
27
+ expect(serialized.retryable).toBeUndefined();
28
+ });
29
+
30
+ test('serializes a TrailsError with category', () => {
31
+ const err = new NetworkError('disconnected');
32
+ const serialized = serializeError(err);
33
+ expect(serialized.name).toBe('NetworkError');
34
+ expect(serialized.message).toBe('disconnected');
35
+ expect(serialized.category).toBe('network');
36
+ expect(serialized.retryable).toBe(true);
37
+ });
38
+
39
+ test('serializes context', () => {
40
+ const err = new ValidationError('bad input', {
41
+ context: { field: 'email' },
42
+ });
43
+ const serialized = serializeError(err);
44
+ expect(serialized.context).toEqual({ field: 'email' });
45
+ });
46
+
47
+ test('serializes RateLimitError.retryAfter', () => {
48
+ const err = new RateLimitError('slow down', { retryAfter: 30 });
49
+ const serialized = serializeError(err);
50
+ expect(serialized.retryAfter).toBe(30);
51
+ });
52
+
53
+ test('retryAfter is undefined for non-RateLimitError', () => {
54
+ const err = new TimeoutError('timed out');
55
+ const serialized = serializeError(err);
56
+ expect(serialized.retryAfter).toBeUndefined();
57
+ });
58
+ });
59
+
60
+ // ---------------------------------------------------------------------------
61
+ // deserializeError
62
+ // ---------------------------------------------------------------------------
63
+
64
+ describe('deserializeError', () => {
65
+ test('reconstructs a validation error', () => {
66
+ const data: SerializedError = {
67
+ category: 'validation',
68
+ message: 'bad input',
69
+ name: 'ValidationError',
70
+ retryable: false,
71
+ };
72
+ const err = deserializeError(data);
73
+ expect(err).toBeInstanceOf(ValidationError);
74
+ expect(err.message).toBe('bad input');
75
+ expect(err.category).toBe('validation');
76
+ });
77
+
78
+ test('reconstructs a rate limit error with retryAfter', () => {
79
+ const data: SerializedError = {
80
+ category: 'rate_limit',
81
+ message: 'slow down',
82
+ name: 'RateLimitError',
83
+ retryAfter: 60,
84
+ retryable: true,
85
+ };
86
+ const err = deserializeError(data);
87
+ expect(err).toBeInstanceOf(RateLimitError);
88
+ expect((err as RateLimitError).retryAfter).toBe(60);
89
+ });
90
+
91
+ test('restores stack trace', () => {
92
+ const data: SerializedError = {
93
+ category: 'internal',
94
+ message: 'oops',
95
+ name: 'InternalError',
96
+ stack: 'Error: oops\n at test.ts:1:1',
97
+ };
98
+ const err = deserializeError(data);
99
+ expect(err.stack).toBe('Error: oops\n at test.ts:1:1');
100
+ });
101
+
102
+ test('restores context', () => {
103
+ const data: SerializedError = {
104
+ category: 'auth',
105
+ context: { endpoint: '/api/secret' },
106
+ message: 'unauthorized',
107
+ name: 'AuthError',
108
+ };
109
+ const err = deserializeError(data);
110
+ expect(err.context).toEqual({ endpoint: '/api/secret' });
111
+ });
112
+
113
+ test('defaults to InternalError when category is missing', () => {
114
+ const data: SerializedError = {
115
+ message: 'unknown',
116
+ name: 'SomeError',
117
+ };
118
+ const err = deserializeError(data);
119
+ expect(err).toBeInstanceOf(InternalError);
120
+ expect(err.category).toBe('internal');
121
+ });
122
+
123
+ test('round-trips through serialize/deserialize', () => {
124
+ const original = new NotFoundError('missing resource', {
125
+ context: { id: 'abc' },
126
+ });
127
+ const serialized = serializeError(original);
128
+ const restored = deserializeError(serialized);
129
+ expect(restored).toBeInstanceOf(NotFoundError);
130
+ expect(restored.message).toBe('missing resource');
131
+ expect(restored.context).toEqual({ id: 'abc' });
132
+ expect(restored.category).toBe('not_found');
133
+ });
134
+
135
+ test('handles all error categories', () => {
136
+ const categories = [
137
+ 'validation',
138
+ 'not_found',
139
+ 'conflict',
140
+ 'permission',
141
+ 'timeout',
142
+ 'rate_limit',
143
+ 'network',
144
+ 'internal',
145
+ 'auth',
146
+ 'cancelled',
147
+ ] as const;
148
+
149
+ for (const category of categories) {
150
+ const data: SerializedError = {
151
+ category,
152
+ message: 'test',
153
+ name: 'Test',
154
+ };
155
+ const err = deserializeError(data);
156
+ expect(err.category).toBe(category);
157
+ }
158
+ });
159
+ });
160
+
161
+ // ---------------------------------------------------------------------------
162
+ // Result.fromJson
163
+ // ---------------------------------------------------------------------------
164
+
165
+ describe('Result.fromJson (safeParse)', () => {
166
+ test('parses valid JSON', () => {
167
+ const result = Result.fromJson('{"key": "value"}');
168
+ expect(result.isOk()).toBe(true);
169
+ expect(result.unwrap()).toEqual({ key: 'value' });
170
+ });
171
+
172
+ test('parses JSON arrays', () => {
173
+ const result = Result.fromJson('[1, 2, 3]');
174
+ expect(result.isOk()).toBe(true);
175
+ expect(result.unwrap()).toEqual([1, 2, 3]);
176
+ });
177
+
178
+ test('parses JSON primitives', () => {
179
+ expect(Result.fromJson('"hello"').unwrap()).toBe('hello');
180
+ expect(Result.fromJson('42').unwrap()).toBe(42);
181
+ expect(Result.fromJson('true').unwrap()).toBe(true);
182
+ expect(Result.fromJson('null').unwrap()).toBe(null);
183
+ });
184
+
185
+ test('returns ValidationError for invalid JSON', () => {
186
+ const result = Result.fromJson('not json');
187
+ expect(result.isErr()).toBe(true);
188
+ const err = result as unknown as { error: ValidationError };
189
+ expect(err.error).toBeInstanceOf(ValidationError);
190
+ expect(err.error.message).toBe('Invalid JSON');
191
+ });
192
+
193
+ test('includes truncated input in context', () => {
194
+ const result = Result.fromJson('{bad}');
195
+ expect(result.isErr()).toBe(true);
196
+ const err = result as unknown as { error: ValidationError };
197
+ expect(err.error.context).toBeDefined();
198
+ expect(err.error.context?.['input']).toBe('{bad}');
199
+ });
200
+ });
201
+
202
+ // ---------------------------------------------------------------------------
203
+ // Result.toJson
204
+ // ---------------------------------------------------------------------------
205
+
206
+ describe('Result.toJson (safeStringify)', () => {
207
+ test('stringifies objects', () => {
208
+ const result = Result.toJson({ key: 'value' });
209
+ expect(result.isOk()).toBe(true);
210
+ expect(result.unwrap()).toBe('{"key":"value"}');
211
+ });
212
+
213
+ test('stringifies arrays', () => {
214
+ const result = Result.toJson([1, 2, 3]);
215
+ expect(result.isOk()).toBe(true);
216
+ expect(result.unwrap()).toBe('[1,2,3]');
217
+ });
218
+
219
+ test('stringifies primitives', () => {
220
+ expect(Result.toJson('hello').unwrap()).toBe('"hello"');
221
+ expect(Result.toJson(42).unwrap()).toBe('42');
222
+ expect(Result.toJson(true).unwrap()).toBe('true');
223
+ expect(Result.toJson(null).unwrap()).toBe('null');
224
+ });
225
+
226
+ test('handles circular references gracefully', () => {
227
+ const obj: Record<string, unknown> = { a: 1 };
228
+ obj['self'] = obj;
229
+ const result = Result.toJson(obj);
230
+ expect(result.isOk()).toBe(true);
231
+ const ok = result as unknown as { value: string };
232
+ const parsed = JSON.parse(ok.value) as Record<string, unknown>;
233
+ expect(parsed['a']).toBe(1);
234
+ expect(parsed['self']).toBe('[Circular]');
235
+ });
236
+ });
@@ -0,0 +1,184 @@
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) => ({
17
+ id,
18
+ implementation: noop,
19
+ input: z.object({ x: z.number() }),
20
+ kind: 'trail' as const,
21
+ output: z.object({ y: z.number() }),
22
+ });
23
+
24
+ const mockHike = (id: string) => ({
25
+ follows: [] as readonly string[],
26
+ id,
27
+ implementation: noop,
28
+ input: z.object({ q: z.string() }),
29
+ kind: 'hike' as const,
30
+ output: z.object({ r: z.string() }),
31
+ path: `/${id}`,
32
+ });
33
+
34
+ const mockEvent = (id: string) => ({
35
+ id,
36
+ kind: 'event' as const,
37
+ payload: z.object({ payload: z.string() }),
38
+ });
39
+
40
+ // ---------------------------------------------------------------------------
41
+ // topo()
42
+ // ---------------------------------------------------------------------------
43
+
44
+ describe('topo', () => {
45
+ test('returns Topo with name', () => {
46
+ const t = topo('my-app');
47
+ expect(t.name).toBe('my-app');
48
+ });
49
+
50
+ test('collects trails from modules', () => {
51
+ const mod = { myTrail: mockTrail('create-user') };
52
+ const t = topo('app', mod);
53
+
54
+ expect(t.trails.size).toBe(1);
55
+ expect(t.trails.get('create-user')).toBe(mod.myTrail);
56
+ });
57
+
58
+ test('auto-scans exports by kind discriminant', () => {
59
+ const mod = {
60
+ event1: mockEvent('e1'),
61
+ hike1: mockHike('r1'),
62
+ trail1: mockTrail('t1'),
63
+ };
64
+ const t = topo('app', mod);
65
+
66
+ expect(t.trails.size).toBe(1);
67
+ expect(t.hikes.size).toBe(1);
68
+ expect(t.events.size).toBe(1);
69
+ });
70
+
71
+ test('collects from multiple modules', () => {
72
+ const mod1 = { a: mockTrail('t1') };
73
+ const mod2 = { b: mockTrail('t2'), c: mockHike('r1') };
74
+ const t = topo('app', mod1, mod2);
75
+
76
+ expect(t.trails.size).toBe(2);
77
+ expect(t.hikes.size).toBe(1);
78
+ });
79
+
80
+ test('non-trail exports are silently ignored', () => {
81
+ const mod = {
82
+ config: { port: 3000 },
83
+ helper: () => 'not a trail',
84
+ name: 'some-string',
85
+ nothing: null,
86
+ num: 42,
87
+ trail1: mockTrail('t1'),
88
+ undef: undefined,
89
+ };
90
+ const t = topo('app', mod);
91
+
92
+ expect(t.trails.size).toBe(1);
93
+ expect(t.hikes.size).toBe(0);
94
+ expect(t.events.size).toBe(0);
95
+ });
96
+
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('Duplicate trail ID: "dup"');
103
+ });
104
+
105
+ test('rejects duplicate hike IDs', () => {
106
+ const mod1 = { a: mockHike('dup') };
107
+ const mod2 = { b: mockHike('dup') };
108
+
109
+ expect(() => topo('app', mod1, mod2)).toThrow(ValidationError);
110
+ expect(() => topo('app', mod1, mod2)).toThrow('Duplicate hike ID: "dup"');
111
+ });
112
+
113
+ test('rejects duplicate event IDs', () => {
114
+ const mod1 = { a: mockEvent('dup') };
115
+ const mod2 = { b: mockEvent('dup') };
116
+
117
+ expect(() => topo('app', mod1, mod2)).toThrow(ValidationError);
118
+ expect(() => topo('app', mod1, mod2)).toThrow('Duplicate event ID: "dup"');
119
+ });
120
+ });
121
+
122
+ // ---------------------------------------------------------------------------
123
+ // Topo
124
+ // ---------------------------------------------------------------------------
125
+
126
+ describe('Topo', () => {
127
+ const mod = {
128
+ e1: mockEvent('event-1'),
129
+ h1: mockHike('hike-1'),
130
+ t1: mockTrail('trail-1'),
131
+ t2: mockTrail('trail-2'),
132
+ };
133
+
134
+ // Build once for the describe block
135
+ const app = topo('app', mod);
136
+
137
+ describe('get()', () => {
138
+ test('retrieves trail by ID', () => {
139
+ expect(app.get('trail-1')).toBe(mod.t1);
140
+ });
141
+
142
+ test('retrieves hike by ID', () => {
143
+ expect(app.get('hike-1')).toBe(mod.h1);
144
+ });
145
+
146
+ test('returns undefined for unknown ID', () => {
147
+ expect(app.get('nope')).toBeUndefined();
148
+ });
149
+ });
150
+
151
+ describe('has()', () => {
152
+ test('returns true for known trail', () => {
153
+ expect(app.has('trail-1')).toBe(true);
154
+ });
155
+
156
+ test('returns true for known hike', () => {
157
+ expect(app.has('hike-1')).toBe(true);
158
+ });
159
+
160
+ test('returns false for unknown ID', () => {
161
+ expect(app.has('nope')).toBe(false);
162
+ });
163
+
164
+ test('returns false for event ID (events are not trails/hikes)', () => {
165
+ expect(app.has('event-1')).toBe(false);
166
+ });
167
+ });
168
+
169
+ describe('listing', () => {
170
+ test('list() returns all trails and hikes', () => {
171
+ const items = app.list();
172
+ expect(items).toHaveLength(3);
173
+ expect(items).toContain(mod.t1);
174
+ expect(items).toContain(mod.t2);
175
+ expect(items).toContain(mod.h1);
176
+ });
177
+
178
+ test('listEvents() returns all events', () => {
179
+ const items = app.listEvents();
180
+ expect(items).toHaveLength(1);
181
+ expect(items).toContain(mod.e1);
182
+ });
183
+ });
184
+ });
@@ -0,0 +1,179 @@
1
+ import { describe, test, expect } from 'bun:test';
2
+
3
+ import { z } from 'zod';
4
+
5
+ import { Result } from '../result';
6
+ import { trail } from '../trail';
7
+ import type { TrailContext } from '../types';
8
+
9
+ const stubCtx: TrailContext = {
10
+ requestId: 'test-123',
11
+ signal: AbortSignal.timeout(5000),
12
+ };
13
+
14
+ describe('trail()', () => {
15
+ const inputSchema = z.object({ name: z.string() });
16
+ const outputSchema = z.object({ greeting: z.string() });
17
+
18
+ const greet = trail('greet', {
19
+ description: 'Greet someone',
20
+ implementation: (input) => Result.ok({ greeting: `Hello, ${input.name}!` }),
21
+ input: inputSchema,
22
+ output: outputSchema,
23
+ });
24
+
25
+ describe('basics', () => {
26
+ test('returns correct id', () => {
27
+ expect(greet.id).toBe('greet');
28
+ });
29
+
30
+ test("returns kind 'trail'", () => {
31
+ expect(greet.kind).toBe('trail');
32
+ });
33
+
34
+ test('preserves input schema', () => {
35
+ const parsed = greet.input.safeParse({ name: 'World' });
36
+ expect(parsed.success).toBe(true);
37
+
38
+ const bad = greet.input.safeParse({ name: 42 });
39
+ expect(bad.success).toBe(false);
40
+ });
41
+
42
+ test('output schema is optional', () => {
43
+ const minimal = trail('noop', {
44
+ implementation: () => Result.ok(),
45
+ input: z.object({}),
46
+ });
47
+ expect(minimal.output).toBeUndefined();
48
+ });
49
+
50
+ test('implementation is callable', async () => {
51
+ const result = await greet.implementation({ name: 'World' }, stubCtx);
52
+ expect(result.isOk()).toBe(true);
53
+ expect(result.unwrap()).toEqual({ greeting: 'Hello, World!' });
54
+ });
55
+ });
56
+
57
+ describe('metadata', () => {
58
+ test('examples are stored', () => {
59
+ const withExamples = trail('echo', {
60
+ examples: [
61
+ { error: 'ValidationError', input: { text: '' }, name: 'error-case' },
62
+ { expected: { text: 'hi' }, input: { text: 'hi' }, name: 'basic' },
63
+ ],
64
+ implementation: (input) => Result.ok({ text: input.text }),
65
+ input: z.object({ text: z.string() }),
66
+ });
67
+ expect(withExamples.examples).toHaveLength(2);
68
+ const first = withExamples.examples?.[0];
69
+ expect(first?.name).toBe('error-case');
70
+ const second = withExamples.examples?.[1];
71
+ expect(second?.name).toBe('basic');
72
+ });
73
+
74
+ test('markers are stored', () => {
75
+ const withMarkers = trail('tagged', {
76
+ implementation: () => Result.ok(),
77
+ input: z.object({}),
78
+ markers: { domain: 'billing', tier: 1 },
79
+ });
80
+ expect(withMarkers.markers).toEqual({ domain: 'billing', tier: 1 });
81
+ });
82
+
83
+ test('detours are stored', () => {
84
+ const withDetours = trail('orchestrator', {
85
+ detours: {
86
+ onFailure: ['alert'],
87
+ onSuccess: ['notify', 'audit'],
88
+ },
89
+ implementation: () => Result.ok(),
90
+ input: z.object({}),
91
+ });
92
+ expect(withDetours.detours).toEqual({
93
+ onFailure: ['alert'],
94
+ onSuccess: ['notify', 'audit'],
95
+ });
96
+ });
97
+ });
98
+
99
+ describe('boolean flags', () => {
100
+ test('boolean flags default to undefined', () => {
101
+ const minimal = trail('bare', {
102
+ implementation: () => Result.ok(),
103
+ input: z.object({}),
104
+ });
105
+ expect(minimal.readOnly).toBeUndefined();
106
+ expect(minimal.destructive).toBeUndefined();
107
+ expect(minimal.idempotent).toBeUndefined();
108
+ });
109
+
110
+ test('boolean flags are preserved when set', () => {
111
+ const withFlags = trail('flagged', {
112
+ destructive: false,
113
+ idempotent: true,
114
+ implementation: () => Result.ok(),
115
+ input: z.object({}),
116
+ readOnly: true,
117
+ });
118
+ expect(withFlags.readOnly).toBe(true);
119
+ expect(withFlags.destructive).toBe(false);
120
+ expect(withFlags.idempotent).toBe(true);
121
+ });
122
+ });
123
+
124
+ describe('single-object overload', () => {
125
+ test('accepts spec with id property', () => {
126
+ const t = trail({
127
+ id: 'entity.show',
128
+ implementation: (input: { name: string }, _ctx: TrailContext) =>
129
+ Result.ok({ greeting: `Hi, ${input.name}` }),
130
+ input: inputSchema,
131
+ });
132
+ expect(t.id).toBe('entity.show');
133
+ expect(t.kind).toBe('trail');
134
+ });
135
+
136
+ test('preserves all spec fields', () => {
137
+ const t = trail({
138
+ description: 'A full trail',
139
+ destructive: false,
140
+ examples: [{ input: { name: 'World' }, name: 'test' }],
141
+ id: 'full',
142
+ implementation: (input: { name: string }, _ctx: TrailContext) =>
143
+ Result.ok({ greeting: `Hi, ${input.name}` }),
144
+ input: inputSchema,
145
+ output: outputSchema,
146
+ readOnly: true,
147
+ });
148
+ expect(t.description).toBe('A full trail');
149
+ expect(t.readOnly).toBe(true);
150
+ expect(t.examples).toHaveLength(1);
151
+ });
152
+
153
+ test('implementation is callable', async () => {
154
+ const t = trail({
155
+ id: 'callable',
156
+ implementation: (input: { x: number }) => Result.ok(input.x * 2),
157
+ input: z.object({ x: z.number() }),
158
+ });
159
+ const result = await t.implementation({ x: 5 }, stubCtx);
160
+ expect(result.isOk()).toBe(true);
161
+ expect(result.unwrap()).toBe(10);
162
+ });
163
+
164
+ test('sync implementations are normalized to an awaitable runtime function', async () => {
165
+ const t = trail('normalized', {
166
+ implementation: (input: { value: number }) =>
167
+ Result.ok(input.value + 1),
168
+ input: z.object({ value: z.number() }),
169
+ });
170
+
171
+ const promise = t.implementation({ value: 2 }, stubCtx);
172
+ expect(promise).toBeInstanceOf(Promise);
173
+
174
+ const result = await promise;
175
+ expect(result.isOk()).toBe(true);
176
+ expect(result.unwrap()).toBe(3);
177
+ });
178
+ });
179
+ });