@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,126 @@
1
+ import { describe, test, expect } from 'bun:test';
2
+
3
+ import {
4
+ chunk,
5
+ dedupe,
6
+ groupBy,
7
+ sortBy,
8
+ isNonEmptyArray,
9
+ } from '../collections';
10
+
11
+ describe('collections', () => {
12
+ describe('chunk()', () => {
13
+ test('splits into even chunks', () => {
14
+ expect(chunk([1, 2, 3, 4], 2)).toEqual([
15
+ [1, 2],
16
+ [3, 4],
17
+ ]);
18
+ });
19
+
20
+ test('handles remainder', () => {
21
+ expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
22
+ });
23
+
24
+ test('returns empty array for empty input', () => {
25
+ expect(chunk([], 3)).toEqual([]);
26
+ });
27
+
28
+ test('single-element chunks', () => {
29
+ expect(chunk([1, 2, 3], 1)).toEqual([[1], [2], [3]]);
30
+ });
31
+
32
+ test('chunk size larger than array', () => {
33
+ expect(chunk([1, 2], 10)).toEqual([[1, 2]]);
34
+ });
35
+
36
+ test('throws on size < 1', () => {
37
+ expect(() => chunk([1], 0)).toThrow(RangeError);
38
+ });
39
+ });
40
+
41
+ describe('dedupe()', () => {
42
+ test('removes primitive duplicates', () => {
43
+ expect(dedupe([1, 2, 2, 3, 1])).toEqual([1, 2, 3]);
44
+ });
45
+
46
+ test('dedupes by key function', () => {
47
+ const items = [
48
+ { id: 1, name: 'a' },
49
+ { id: 2, name: 'b' },
50
+ { id: 1, name: 'c' },
51
+ ];
52
+ const result = dedupe(items, (i) => i.id);
53
+ expect(result).toEqual([
54
+ { id: 1, name: 'a' },
55
+ { id: 2, name: 'b' },
56
+ ]);
57
+ });
58
+
59
+ test('preserves order', () => {
60
+ expect(dedupe([3, 1, 2, 1, 3])).toEqual([3, 1, 2]);
61
+ });
62
+
63
+ test('handles empty array', () => {
64
+ expect(dedupe([])).toEqual([]);
65
+ });
66
+ });
67
+
68
+ describe('groupBy()', () => {
69
+ test('groups by key', () => {
70
+ const items = [
71
+ { type: 'a', value: 1 },
72
+ { type: 'b', value: 2 },
73
+ { type: 'a', value: 3 },
74
+ ];
75
+ const groups = groupBy(items, (i) => i.type);
76
+ expect(groups).toEqual({
77
+ a: [
78
+ { type: 'a', value: 1 },
79
+ { type: 'a', value: 3 },
80
+ ],
81
+ b: [{ type: 'b', value: 2 }],
82
+ });
83
+ });
84
+
85
+ test('returns empty object for empty array', () => {
86
+ expect(groupBy([], () => 'key')).toEqual({});
87
+ });
88
+ });
89
+
90
+ describe('sortBy()', () => {
91
+ test('sorts by numeric key ascending', () => {
92
+ const items = [{ n: 3 }, { n: 1 }, { n: 2 }];
93
+ expect(sortBy(items, (i) => i.n)).toEqual([{ n: 1 }, { n: 2 }, { n: 3 }]);
94
+ });
95
+
96
+ test('sorts by string key alphabetically', () => {
97
+ const items = [{ name: 'charlie' }, { name: 'alice' }, { name: 'bob' }];
98
+ expect(sortBy(items, (i) => i.name)).toEqual([
99
+ { name: 'alice' },
100
+ { name: 'bob' },
101
+ { name: 'charlie' },
102
+ ]);
103
+ });
104
+
105
+ test('does not mutate the original array', () => {
106
+ const original = [{ n: 3 }, { n: 1 }];
107
+ sortBy(original, (i) => i.n);
108
+ expect(original[0]?.n).toBe(3);
109
+ });
110
+
111
+ test('returns empty array for empty input', () => {
112
+ expect(sortBy([], () => 0)).toEqual([]);
113
+ });
114
+ });
115
+
116
+ describe('isNonEmptyArray()', () => {
117
+ test('returns true for arrays with elements', () => {
118
+ expect(isNonEmptyArray([1])).toBe(true);
119
+ expect(isNonEmptyArray([1, 2, 3])).toBe(true);
120
+ });
121
+
122
+ test('returns false for empty array', () => {
123
+ expect(isNonEmptyArray([])).toBe(false);
124
+ });
125
+ });
126
+ });
@@ -0,0 +1,66 @@
1
+ import { describe, test, expect } from 'bun:test';
2
+
3
+ import { createTrailContext } from '../context.js';
4
+ import type { TrailContext } from '../types.js';
5
+
6
+ const UUID_RE =
7
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
8
+
9
+ describe('createTrailContext', () => {
10
+ test('generates a requestId in UUID format', () => {
11
+ const ctx = createTrailContext();
12
+ expect(ctx.requestId).toMatch(UUID_RE);
13
+ });
14
+
15
+ test('defaults cwd to process.cwd()', () => {
16
+ const ctx = createTrailContext();
17
+ expect(ctx.cwd).toBe(process.cwd());
18
+ });
19
+
20
+ test('defaults env to process.env', () => {
21
+ const ctx = createTrailContext();
22
+ expect(ctx.env).toBeTruthy();
23
+ expect(ctx.env).toBe(process.env);
24
+ });
25
+
26
+ test('provides a non-aborted signal by default', () => {
27
+ const ctx = createTrailContext();
28
+ expect(ctx.signal).toBeInstanceOf(AbortSignal);
29
+ expect(ctx.signal.aborted).toBe(false);
30
+ });
31
+
32
+ test('override values take precedence', () => {
33
+ const ac = new AbortController();
34
+ ac.abort();
35
+
36
+ const ctx = createTrailContext({
37
+ cwd: '/custom/dir',
38
+ env: { CUSTOM: 'yes' },
39
+ requestId: 'custom-id',
40
+ signal: ac.signal,
41
+ workspaceRoot: '/tmp',
42
+ });
43
+
44
+ expect(ctx.cwd).toBe('/custom/dir');
45
+ expect(ctx.env).toEqual({ CUSTOM: 'yes' });
46
+ expect(ctx.requestId).toBe('custom-id');
47
+ expect(ctx.signal.aborted).toBe(true);
48
+ expect(ctx.workspaceRoot).toBe('/tmp');
49
+ });
50
+
51
+ test('TrailContext is extensible with custom fields', () => {
52
+ const ctx: TrailContext = createTrailContext({
53
+ customField: 42,
54
+ nested: { key: 'value' },
55
+ });
56
+
57
+ expect(ctx['customField']).toBe(42);
58
+ expect(ctx['nested']).toEqual({ key: 'value' });
59
+ });
60
+
61
+ test('each call generates a unique requestId', () => {
62
+ const a = createTrailContext();
63
+ const b = createTrailContext();
64
+ expect(a.requestId).not.toBe(b.requestId);
65
+ });
66
+ });
@@ -0,0 +1,159 @@
1
+ import { describe, test, expect } from 'bun:test';
2
+
3
+ import { z } from 'zod';
4
+
5
+ import { deriveFields } from '../derive.js';
6
+
7
+ // ---------------------------------------------------------------------------
8
+ // Tests
9
+ // ---------------------------------------------------------------------------
10
+
11
+ describe('derive', () => {
12
+ describe('primitive types', () => {
13
+ test('z.string() derives string field', () => {
14
+ const schema = z.object({ name: z.string() });
15
+ const fields = deriveFields(schema);
16
+ expect(fields).toHaveLength(1);
17
+ expect(fields[0]).toMatchObject({
18
+ label: 'Name',
19
+ name: 'name',
20
+ required: true,
21
+ type: 'string',
22
+ });
23
+ });
24
+
25
+ test('z.number() derives number field', () => {
26
+ const schema = z.object({ count: z.number() });
27
+ const fields = deriveFields(schema);
28
+ expect(fields[0]).toMatchObject({
29
+ name: 'count',
30
+ required: true,
31
+ type: 'number',
32
+ });
33
+ });
34
+
35
+ test('z.boolean() derives boolean field', () => {
36
+ const schema = z.object({ verbose: z.boolean() });
37
+ const fields = deriveFields(schema);
38
+ expect(fields[0]).toMatchObject({
39
+ name: 'verbose',
40
+ required: true,
41
+ type: 'boolean',
42
+ });
43
+ });
44
+ });
45
+
46
+ describe('enum and multiselect', () => {
47
+ test('z.enum() derives enum field with options', () => {
48
+ const schema = z.object({ color: z.enum(['red', 'green', 'blue']) });
49
+ const fields = deriveFields(schema);
50
+ expect(fields[0]).toMatchObject({
51
+ name: 'color',
52
+ required: true,
53
+ type: 'enum',
54
+ });
55
+ expect(fields[0]?.options).toEqual([
56
+ { value: 'red' },
57
+ { value: 'green' },
58
+ { value: 'blue' },
59
+ ]);
60
+ });
61
+
62
+ test('z.array(z.enum()) derives multiselect field', () => {
63
+ const schema = z.object({
64
+ tags: z.array(z.enum(['a', 'b', 'c'])),
65
+ });
66
+ const fields = deriveFields(schema);
67
+ expect(fields[0]).toMatchObject({
68
+ name: 'tags',
69
+ required: true,
70
+ type: 'multiselect',
71
+ });
72
+ expect(fields[0]?.options).toEqual([
73
+ { value: 'a' },
74
+ { value: 'b' },
75
+ { value: 'c' },
76
+ ]);
77
+ });
78
+ });
79
+
80
+ describe('modifiers', () => {
81
+ test('.describe() sets label', () => {
82
+ const schema = z.object({
83
+ name: z.string().describe('Your full name'),
84
+ });
85
+ const fields = deriveFields(schema);
86
+ expect(fields[0]?.label).toBe('Your full name');
87
+ });
88
+
89
+ test('.default() sets default and marks not required', () => {
90
+ const schema = z.object({
91
+ port: z.number().default(3000),
92
+ });
93
+ const fields = deriveFields(schema);
94
+ expect(fields[0]?.required).toBe(false);
95
+ expect(fields[0]?.default).toBe(3000);
96
+ });
97
+
98
+ test('.optional() marks not required', () => {
99
+ const schema = z.object({
100
+ nickname: z.string().optional(),
101
+ });
102
+ const fields = deriveFields(schema);
103
+ expect(fields[0]?.required).toBe(false);
104
+ });
105
+ });
106
+
107
+ describe('overrides', () => {
108
+ test('override label replaces derived label', () => {
109
+ const schema = z.object({
110
+ name: z.string().describe('Derived label'),
111
+ });
112
+ const fields = deriveFields(schema, {
113
+ name: { label: 'Override label' },
114
+ });
115
+ expect(fields[0]?.label).toBe('Override label');
116
+ });
117
+
118
+ test('override options enrich enum values with labels and hints', () => {
119
+ const schema = z.object({
120
+ color: z.enum(['red', 'green', 'blue']),
121
+ });
122
+ const fields = deriveFields(schema, {
123
+ color: {
124
+ options: [
125
+ { hint: 'Hot color', label: 'Red', value: 'red' },
126
+ { label: 'Green', value: 'green' },
127
+ ],
128
+ },
129
+ });
130
+ expect(fields[0]?.options).toEqual([
131
+ { hint: 'Hot color', label: 'Red', value: 'red' },
132
+ { label: 'Green', value: 'green' },
133
+ { value: 'blue' },
134
+ ]);
135
+ });
136
+ });
137
+
138
+ describe('sorting and edge cases', () => {
139
+ test('returns fields sorted by name', () => {
140
+ const schema = z.object({
141
+ alpha: z.string(),
142
+ middle: z.string(),
143
+ zebra: z.string(),
144
+ });
145
+ const fields = deriveFields(schema);
146
+ expect(fields.map((f) => f.name)).toEqual(['alpha', 'middle', 'zebra']);
147
+ });
148
+
149
+ test('returns empty array for non-object schema', () => {
150
+ expect(deriveFields(z.string())).toEqual([]);
151
+ });
152
+
153
+ test('humanizes camelCase field names', () => {
154
+ const schema = z.object({ firstName: z.string() });
155
+ const fields = deriveFields(schema);
156
+ expect(fields[0]?.label).toBe('First Name');
157
+ });
158
+ });
159
+ });
@@ -0,0 +1,309 @@
1
+ import { describe, test, expect } from 'bun:test';
2
+
3
+ import {
4
+ TrailsError,
5
+ ValidationError,
6
+ AmbiguousError,
7
+ AssertionError,
8
+ NotFoundError,
9
+ AlreadyExistsError,
10
+ ConflictError,
11
+ PermissionError,
12
+ TimeoutError,
13
+ RateLimitError,
14
+ NetworkError,
15
+ InternalError,
16
+ AuthError,
17
+ CancelledError,
18
+ exitCodeMap,
19
+ statusCodeMap,
20
+ jsonRpcCodeMap,
21
+ retryableMap,
22
+ isRetryable,
23
+ isTrailsError,
24
+ } from '../errors.js';
25
+ import type { ErrorCategory } from '../errors.js';
26
+
27
+ // ---------------------------------------------------------------------------
28
+ // Error class matrix
29
+ // ---------------------------------------------------------------------------
30
+
31
+ const errorMatrix: readonly {
32
+ Class: new (
33
+ message: string,
34
+ options?: { cause?: Error; context?: Record<string, unknown> }
35
+ ) => TrailsError;
36
+ category: ErrorCategory;
37
+ retryable: boolean;
38
+ name: string;
39
+ }[] = [
40
+ {
41
+ Class: ValidationError,
42
+ category: 'validation',
43
+ name: 'ValidationError',
44
+ retryable: false,
45
+ },
46
+ {
47
+ Class: AmbiguousError,
48
+ category: 'validation',
49
+ name: 'AmbiguousError',
50
+ retryable: false,
51
+ },
52
+ {
53
+ Class: AssertionError,
54
+ category: 'internal',
55
+ name: 'AssertionError',
56
+ retryable: false,
57
+ },
58
+ {
59
+ Class: NotFoundError,
60
+ category: 'not_found',
61
+ name: 'NotFoundError',
62
+ retryable: false,
63
+ },
64
+ {
65
+ Class: AlreadyExistsError,
66
+ category: 'conflict',
67
+ name: 'AlreadyExistsError',
68
+ retryable: false,
69
+ },
70
+ {
71
+ Class: ConflictError,
72
+ category: 'conflict',
73
+ name: 'ConflictError',
74
+ retryable: false,
75
+ },
76
+ {
77
+ Class: PermissionError,
78
+ category: 'permission',
79
+ name: 'PermissionError',
80
+ retryable: false,
81
+ },
82
+ {
83
+ Class: TimeoutError,
84
+ category: 'timeout',
85
+ name: 'TimeoutError',
86
+ retryable: true,
87
+ },
88
+ {
89
+ Class: RateLimitError,
90
+ category: 'rate_limit',
91
+ name: 'RateLimitError',
92
+ retryable: true,
93
+ },
94
+ {
95
+ Class: NetworkError,
96
+ category: 'network',
97
+ name: 'NetworkError',
98
+ retryable: true,
99
+ },
100
+ {
101
+ Class: InternalError,
102
+ category: 'internal',
103
+ name: 'InternalError',
104
+ retryable: false,
105
+ },
106
+ { Class: AuthError, category: 'auth', name: 'AuthError', retryable: false },
107
+ {
108
+ Class: CancelledError,
109
+ category: 'cancelled',
110
+ name: 'CancelledError',
111
+ retryable: false,
112
+ },
113
+ ];
114
+
115
+ describe('error classes', () => {
116
+ // oxlint-disable-next-line prefer-each -- describe.each loses type safety on heterogeneous class matrix
117
+ for (const { Class, category, retryable, name } of errorMatrix) {
118
+ describe(name, () => {
119
+ test('sets correct category', () => {
120
+ const err = new Class('test');
121
+ expect(err.category).toBe(category);
122
+ });
123
+
124
+ test('sets correct retryable', () => {
125
+ const err = new Class('test');
126
+ expect(err.retryable).toBe(retryable);
127
+ });
128
+
129
+ test('sets message', () => {
130
+ const err = new Class('something went wrong');
131
+ expect(err.message).toBe('something went wrong');
132
+ });
133
+
134
+ test('sets name to constructor name', () => {
135
+ const err = new Class('test');
136
+ expect(err.name).toBe(name);
137
+ });
138
+
139
+ test('sets cause when provided', () => {
140
+ const cause = new Error('root cause');
141
+ const err = new Class('test', { cause });
142
+ expect(err.cause).toBe(cause);
143
+ });
144
+
145
+ test('sets context when provided', () => {
146
+ const ctx = { action: 'delete', userId: 'u_123' };
147
+ const err = new Class('test', { context: ctx });
148
+ expect(err.context).toEqual(ctx);
149
+ });
150
+
151
+ test('context is undefined when not provided', () => {
152
+ const err = new Class('test');
153
+ expect(err.context).toBeUndefined();
154
+ });
155
+
156
+ test('is instanceof TrailsError', () => {
157
+ const err = new Class('test');
158
+ expect(err).toBeInstanceOf(TrailsError);
159
+ });
160
+
161
+ test('is instanceof Error', () => {
162
+ const err = new Class('test');
163
+ expect(err).toBeInstanceOf(Error);
164
+ });
165
+
166
+ test('is instanceof its own class', () => {
167
+ const err = new Class('test');
168
+ expect(err).toBeInstanceOf(Class);
169
+ });
170
+ });
171
+ }
172
+ });
173
+
174
+ // ---------------------------------------------------------------------------
175
+ // RateLimitError.retryAfter
176
+ // ---------------------------------------------------------------------------
177
+
178
+ describe('RateLimitError.retryAfter', () => {
179
+ test('stores retryAfter when provided', () => {
180
+ const err = new RateLimitError('slow down', { retryAfter: 30 });
181
+ expect(err.retryAfter).toBe(30);
182
+ });
183
+
184
+ test('retryAfter is undefined when not provided', () => {
185
+ const err = new RateLimitError('slow down');
186
+ expect(err.retryAfter).toBeUndefined();
187
+ });
188
+
189
+ test('retryAfter coexists with cause and context', () => {
190
+ const cause = new Error('upstream');
191
+ const err = new RateLimitError('slow down', {
192
+ cause,
193
+ context: { endpoint: '/api' },
194
+ retryAfter: 60,
195
+ });
196
+ expect(err.retryAfter).toBe(60);
197
+ expect(err.cause).toBe(cause);
198
+ expect(err.context).toEqual({ endpoint: '/api' });
199
+ });
200
+ });
201
+
202
+ // ---------------------------------------------------------------------------
203
+ // Taxonomy maps
204
+ // ---------------------------------------------------------------------------
205
+
206
+ describe('exitCodeMap', () => {
207
+ test('maps all categories to expected exit codes', () => {
208
+ expect(exitCodeMap.validation).toBe(1);
209
+ expect(exitCodeMap.not_found).toBe(2);
210
+ expect(exitCodeMap.conflict).toBe(3);
211
+ expect(exitCodeMap.permission).toBe(4);
212
+ expect(exitCodeMap.timeout).toBe(5);
213
+ expect(exitCodeMap.rate_limit).toBe(6);
214
+ expect(exitCodeMap.network).toBe(7);
215
+ expect(exitCodeMap.internal).toBe(8);
216
+ expect(exitCodeMap.auth).toBe(9);
217
+ expect(exitCodeMap.cancelled).toBe(130);
218
+ });
219
+ });
220
+
221
+ describe('statusCodeMap', () => {
222
+ test('maps all categories to expected HTTP status codes', () => {
223
+ expect(statusCodeMap.validation).toBe(400);
224
+ expect(statusCodeMap.not_found).toBe(404);
225
+ expect(statusCodeMap.conflict).toBe(409);
226
+ expect(statusCodeMap.permission).toBe(403);
227
+ expect(statusCodeMap.timeout).toBe(504);
228
+ expect(statusCodeMap.rate_limit).toBe(429);
229
+ expect(statusCodeMap.network).toBe(502);
230
+ expect(statusCodeMap.internal).toBe(500);
231
+ expect(statusCodeMap.auth).toBe(401);
232
+ expect(statusCodeMap.cancelled).toBe(499);
233
+ });
234
+ });
235
+
236
+ describe('jsonRpcCodeMap', () => {
237
+ test('maps all categories to expected JSON-RPC codes', () => {
238
+ expect(jsonRpcCodeMap.validation).toBe(-32_602);
239
+ expect(jsonRpcCodeMap.not_found).toBe(-32_601);
240
+ expect(jsonRpcCodeMap.conflict).toBe(-32_603);
241
+ expect(jsonRpcCodeMap.permission).toBe(-32_600);
242
+ expect(jsonRpcCodeMap.timeout).toBe(-32_603);
243
+ expect(jsonRpcCodeMap.rate_limit).toBe(-32_603);
244
+ expect(jsonRpcCodeMap.network).toBe(-32_603);
245
+ expect(jsonRpcCodeMap.internal).toBe(-32_603);
246
+ expect(jsonRpcCodeMap.auth).toBe(-32_600);
247
+ expect(jsonRpcCodeMap.cancelled).toBe(-32_603);
248
+ });
249
+ });
250
+
251
+ describe('retryableMap', () => {
252
+ test('only timeout, rate_limit, and network are retryable', () => {
253
+ expect(retryableMap.timeout).toBe(true);
254
+ expect(retryableMap.rate_limit).toBe(true);
255
+ expect(retryableMap.network).toBe(true);
256
+
257
+ expect(retryableMap.validation).toBe(false);
258
+ expect(retryableMap.not_found).toBe(false);
259
+ expect(retryableMap.conflict).toBe(false);
260
+ expect(retryableMap.permission).toBe(false);
261
+ expect(retryableMap.internal).toBe(false);
262
+ expect(retryableMap.auth).toBe(false);
263
+ expect(retryableMap.cancelled).toBe(false);
264
+ });
265
+ });
266
+
267
+ // ---------------------------------------------------------------------------
268
+ // Helper functions
269
+ // ---------------------------------------------------------------------------
270
+
271
+ describe('isRetryable', () => {
272
+ test('returns true for retryable errors', () => {
273
+ expect(isRetryable(new TimeoutError('timed out'))).toBe(true);
274
+ expect(isRetryable(new RateLimitError('too fast'))).toBe(true);
275
+ expect(isRetryable(new NetworkError('disconnected'))).toBe(true);
276
+ });
277
+
278
+ test('returns false for non-retryable errors', () => {
279
+ expect(isRetryable(new ValidationError('bad input'))).toBe(false);
280
+ expect(isRetryable(new NotFoundError('missing'))).toBe(false);
281
+ expect(isRetryable(new AuthError('unauthorized'))).toBe(false);
282
+ expect(isRetryable(new InternalError('oops'))).toBe(false);
283
+ expect(isRetryable(new CancelledError('aborted'))).toBe(false);
284
+ });
285
+
286
+ test('returns false for plain Error', () => {
287
+ expect(isRetryable(new Error('generic'))).toBe(false);
288
+ });
289
+ });
290
+
291
+ describe('isTrailsError', () => {
292
+ test('returns true for TrailsError subclasses', () => {
293
+ expect(isTrailsError(new ValidationError('bad'))).toBe(true);
294
+ expect(isTrailsError(new NetworkError('down'))).toBe(true);
295
+ expect(isTrailsError(new CancelledError('stop'))).toBe(true);
296
+ });
297
+
298
+ test('returns false for plain Error', () => {
299
+ expect(isTrailsError(new Error('nope'))).toBe(false);
300
+ });
301
+
302
+ test('returns false for non-error values', () => {
303
+ expect(isTrailsError(null)).toBe(false);
304
+ expect(isTrailsError()).toBe(false);
305
+ expect(isTrailsError('string')).toBe(false);
306
+ expect(isTrailsError(42)).toBe(false);
307
+ expect(isTrailsError({})).toBe(false);
308
+ });
309
+ });