apcore-js 0.5.0 → 0.7.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 (319) hide show
  1. package/README.md +1 -1
  2. package/dist/acl.d.ts +27 -0
  3. package/dist/acl.d.ts.map +1 -0
  4. package/dist/acl.js +175 -0
  5. package/dist/acl.js.map +1 -0
  6. package/dist/approval.d.ts +85 -0
  7. package/dist/approval.d.ts.map +1 -0
  8. package/dist/approval.js +73 -0
  9. package/dist/approval.js.map +1 -0
  10. package/dist/async-task.d.ts +90 -0
  11. package/dist/async-task.d.ts.map +1 -0
  12. package/dist/async-task.js +215 -0
  13. package/dist/async-task.js.map +1 -0
  14. package/dist/bindings.d.ts +12 -0
  15. package/dist/bindings.d.ts.map +1 -0
  16. package/dist/bindings.js +185 -0
  17. package/dist/bindings.js.map +1 -0
  18. package/dist/cancel.d.ts +14 -0
  19. package/dist/cancel.d.ts.map +1 -0
  20. package/dist/cancel.js +27 -0
  21. package/dist/cancel.js.map +1 -0
  22. package/dist/config.d.ts +9 -0
  23. package/dist/config.d.ts.map +1 -0
  24. package/dist/config.js +23 -0
  25. package/dist/config.js.map +1 -0
  26. package/dist/context.d.ts +50 -0
  27. package/dist/context.d.ts.map +1 -0
  28. package/dist/context.js +87 -0
  29. package/dist/context.js.map +1 -0
  30. package/dist/decorator.d.ts +57 -0
  31. package/dist/decorator.d.ts.map +1 -0
  32. package/dist/decorator.js +74 -0
  33. package/dist/decorator.js.map +1 -0
  34. package/dist/errors.d.ts +204 -0
  35. package/dist/errors.d.ts.map +1 -0
  36. package/dist/errors.js +364 -0
  37. package/dist/errors.js.map +1 -0
  38. package/dist/executor.d.ts +82 -0
  39. package/dist/executor.d.ts.map +1 -0
  40. package/dist/executor.js +489 -0
  41. package/dist/executor.js.map +1 -0
  42. package/dist/extensions.d.ts +58 -0
  43. package/dist/extensions.d.ts.map +1 -0
  44. package/dist/extensions.js +239 -0
  45. package/dist/extensions.js.map +1 -0
  46. package/{src/index.ts → dist/index.d.ts} +6 -63
  47. package/dist/index.d.ts.map +1 -0
  48. package/dist/index.js +45 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/middleware/adapters.d.ts +18 -0
  51. package/dist/middleware/adapters.d.ts.map +1 -0
  52. package/dist/middleware/adapters.js +25 -0
  53. package/dist/middleware/adapters.js.map +1 -0
  54. package/dist/middleware/base.d.ts +10 -0
  55. package/dist/middleware/base.d.ts.map +1 -0
  56. package/dist/middleware/base.js +15 -0
  57. package/dist/middleware/base.js.map +1 -0
  58. package/{src/middleware/index.ts → dist/middleware/index.d.ts} +1 -0
  59. package/dist/middleware/index.d.ts.map +1 -0
  60. package/dist/middleware/index.js +5 -0
  61. package/dist/middleware/index.js.map +1 -0
  62. package/dist/middleware/logging.d.ts +25 -0
  63. package/dist/middleware/logging.d.ts.map +1 -0
  64. package/dist/middleware/logging.js +64 -0
  65. package/dist/middleware/logging.js.map +1 -0
  66. package/dist/middleware/manager.d.ts +21 -0
  67. package/dist/middleware/manager.d.ts.map +1 -0
  68. package/dist/middleware/manager.js +77 -0
  69. package/dist/middleware/manager.js.map +1 -0
  70. package/dist/module.d.ts +31 -0
  71. package/dist/module.d.ts.map +1 -0
  72. package/dist/module.js +12 -0
  73. package/dist/module.js.map +1 -0
  74. package/dist/observability/context-logger.d.ts +54 -0
  75. package/dist/observability/context-logger.d.ts.map +1 -0
  76. package/dist/observability/context-logger.js +151 -0
  77. package/dist/observability/context-logger.js.map +1 -0
  78. package/{src/observability/index.ts → dist/observability/index.d.ts} +1 -0
  79. package/dist/observability/index.d.ts.map +1 -0
  80. package/dist/observability/index.js +4 -0
  81. package/dist/observability/index.js.map +1 -0
  82. package/dist/observability/metrics.d.ts +30 -0
  83. package/dist/observability/metrics.d.ts.map +1 -0
  84. package/dist/observability/metrics.js +177 -0
  85. package/dist/observability/metrics.js.map +1 -0
  86. package/dist/observability/tracing.d.ts +62 -0
  87. package/dist/observability/tracing.d.ts.map +1 -0
  88. package/dist/observability/tracing.js +184 -0
  89. package/dist/observability/tracing.js.map +1 -0
  90. package/dist/registry/dependencies.d.ts +6 -0
  91. package/dist/registry/dependencies.d.ts.map +1 -0
  92. package/dist/registry/dependencies.js +83 -0
  93. package/dist/registry/dependencies.js.map +1 -0
  94. package/dist/registry/entry-point.d.ts +6 -0
  95. package/dist/registry/entry-point.d.ts.map +1 -0
  96. package/dist/registry/entry-point.js +55 -0
  97. package/dist/registry/entry-point.js.map +1 -0
  98. package/{src/registry/index.ts → dist/registry/index.d.ts} +1 -0
  99. package/dist/registry/index.d.ts.map +1 -0
  100. package/dist/registry/index.js +8 -0
  101. package/dist/registry/index.js.map +1 -0
  102. package/dist/registry/metadata.d.ts +9 -0
  103. package/dist/registry/metadata.d.ts.map +1 -0
  104. package/dist/registry/metadata.js +105 -0
  105. package/dist/registry/metadata.js.map +1 -0
  106. package/dist/registry/registry.d.ts +102 -0
  107. package/dist/registry/registry.d.ts.map +1 -0
  108. package/dist/registry/registry.js +534 -0
  109. package/dist/registry/registry.js.map +1 -0
  110. package/dist/registry/scanner.d.ts +7 -0
  111. package/dist/registry/scanner.d.ts.map +1 -0
  112. package/dist/registry/scanner.js +164 -0
  113. package/dist/registry/scanner.js.map +1 -0
  114. package/dist/registry/schema-export.d.ts +9 -0
  115. package/dist/registry/schema-export.d.ts.map +1 -0
  116. package/dist/registry/schema-export.js +132 -0
  117. package/dist/registry/schema-export.js.map +1 -0
  118. package/dist/registry/types.d.ts +29 -0
  119. package/dist/registry/types.d.ts.map +1 -0
  120. package/dist/registry/types.js +5 -0
  121. package/dist/registry/types.js.map +1 -0
  122. package/dist/registry/validation.d.ts +9 -0
  123. package/dist/registry/validation.d.ts.map +1 -0
  124. package/dist/registry/validation.js +33 -0
  125. package/dist/registry/validation.js.map +1 -0
  126. package/dist/schema/annotations.d.ts +8 -0
  127. package/dist/schema/annotations.d.ts.map +1 -0
  128. package/dist/schema/annotations.js +52 -0
  129. package/dist/schema/annotations.js.map +1 -0
  130. package/dist/schema/exporter.d.ts +13 -0
  131. package/dist/schema/exporter.d.ts.map +1 -0
  132. package/dist/schema/exporter.js +71 -0
  133. package/dist/schema/exporter.js.map +1 -0
  134. package/dist/schema/index.d.ts +9 -0
  135. package/dist/schema/index.d.ts.map +1 -0
  136. package/{src/schema/index.ts → dist/schema/index.js} +1 -7
  137. package/dist/schema/index.js.map +1 -0
  138. package/dist/schema/loader.d.ts +30 -0
  139. package/dist/schema/loader.d.ts.map +1 -0
  140. package/dist/schema/loader.js +260 -0
  141. package/dist/schema/loader.js.map +1 -0
  142. package/dist/schema/ref-resolver.d.ts +19 -0
  143. package/dist/schema/ref-resolver.d.ts.map +1 -0
  144. package/dist/schema/ref-resolver.js +212 -0
  145. package/dist/schema/ref-resolver.js.map +1 -0
  146. package/dist/schema/strict.d.ts +7 -0
  147. package/dist/schema/strict.d.ts.map +1 -0
  148. package/dist/schema/strict.js +127 -0
  149. package/dist/schema/strict.js.map +1 -0
  150. package/dist/schema/types.d.ts +53 -0
  151. package/dist/schema/types.d.ts.map +1 -0
  152. package/dist/schema/types.js +31 -0
  153. package/dist/schema/types.js.map +1 -0
  154. package/dist/schema/validator.d.ts +16 -0
  155. package/dist/schema/validator.d.ts.map +1 -0
  156. package/dist/schema/validator.js +71 -0
  157. package/dist/schema/validator.js.map +1 -0
  158. package/dist/trace-context.d.ts +35 -0
  159. package/dist/trace-context.d.ts.map +1 -0
  160. package/dist/trace-context.js +86 -0
  161. package/dist/trace-context.js.map +1 -0
  162. package/dist/utils/index.d.ts +11 -0
  163. package/dist/utils/index.d.ts.map +1 -0
  164. package/dist/utils/index.js +32 -0
  165. package/dist/utils/index.js.map +1 -0
  166. package/dist/utils/pattern.d.ts +5 -0
  167. package/dist/utils/pattern.d.ts.map +1 -0
  168. package/dist/utils/pattern.js +31 -0
  169. package/dist/utils/pattern.js.map +1 -0
  170. package/package.json +24 -3
  171. package/.claude/settings.local.json +0 -12
  172. package/.github/workflows/ci.yml +0 -39
  173. package/.gitmessage +0 -60
  174. package/.pre-commit-config.yaml +0 -28
  175. package/CHANGELOG.md +0 -214
  176. package/CLAUDE.md +0 -68
  177. package/apcore-logo.svg +0 -79
  178. package/planning/acl-system/overview.md +0 -54
  179. package/planning/acl-system/plan.md +0 -92
  180. package/planning/acl-system/state.json +0 -76
  181. package/planning/acl-system/tasks/acl-core.md +0 -226
  182. package/planning/acl-system/tasks/acl-rule.md +0 -92
  183. package/planning/acl-system/tasks/conditional-rules.md +0 -259
  184. package/planning/acl-system/tasks/pattern-matching.md +0 -152
  185. package/planning/acl-system/tasks/yaml-loading.md +0 -271
  186. package/planning/core-executor/overview.md +0 -53
  187. package/planning/core-executor/plan.md +0 -88
  188. package/planning/core-executor/state.json +0 -76
  189. package/planning/core-executor/tasks/async-support.md +0 -106
  190. package/planning/core-executor/tasks/execution-pipeline.md +0 -113
  191. package/planning/core-executor/tasks/redaction.md +0 -85
  192. package/planning/core-executor/tasks/safety-checks.md +0 -65
  193. package/planning/core-executor/tasks/setup.md +0 -75
  194. package/planning/decorator-bindings/overview.md +0 -62
  195. package/planning/decorator-bindings/plan.md +0 -104
  196. package/planning/decorator-bindings/state.json +0 -87
  197. package/planning/decorator-bindings/tasks/binding-directory.md +0 -79
  198. package/planning/decorator-bindings/tasks/binding-loader.md +0 -148
  199. package/planning/decorator-bindings/tasks/explicit-schemas.md +0 -85
  200. package/planning/decorator-bindings/tasks/function-module.md +0 -127
  201. package/planning/decorator-bindings/tasks/module-factory.md +0 -89
  202. package/planning/decorator-bindings/tasks/schema-modes.md +0 -142
  203. package/planning/middleware-system/overview.md +0 -48
  204. package/planning/middleware-system/plan.md +0 -102
  205. package/planning/middleware-system/state.json +0 -65
  206. package/planning/middleware-system/tasks/adapters.md +0 -170
  207. package/planning/middleware-system/tasks/base.md +0 -115
  208. package/planning/middleware-system/tasks/logging-middleware.md +0 -304
  209. package/planning/middleware-system/tasks/manager.md +0 -313
  210. package/planning/observability/overview.md +0 -53
  211. package/planning/observability/plan.md +0 -119
  212. package/planning/observability/state.json +0 -98
  213. package/planning/observability/tasks/context-logger.md +0 -201
  214. package/planning/observability/tasks/exporters.md +0 -121
  215. package/planning/observability/tasks/metrics-collector.md +0 -162
  216. package/planning/observability/tasks/metrics-middleware.md +0 -141
  217. package/planning/observability/tasks/obs-logging-middleware.md +0 -179
  218. package/planning/observability/tasks/span-model.md +0 -120
  219. package/planning/observability/tasks/tracing-middleware.md +0 -179
  220. package/planning/overview.md +0 -81
  221. package/planning/registry-system/overview.md +0 -57
  222. package/planning/registry-system/plan.md +0 -114
  223. package/planning/registry-system/state.json +0 -109
  224. package/planning/registry-system/tasks/dependencies.md +0 -157
  225. package/planning/registry-system/tasks/entry-point.md +0 -148
  226. package/planning/registry-system/tasks/metadata.md +0 -198
  227. package/planning/registry-system/tasks/registry-core.md +0 -323
  228. package/planning/registry-system/tasks/scanner.md +0 -172
  229. package/planning/registry-system/tasks/schema-export.md +0 -261
  230. package/planning/registry-system/tasks/types.md +0 -124
  231. package/planning/registry-system/tasks/validation.md +0 -177
  232. package/planning/schema-system/overview.md +0 -56
  233. package/planning/schema-system/plan.md +0 -121
  234. package/planning/schema-system/state.json +0 -98
  235. package/planning/schema-system/tasks/exporter.md +0 -153
  236. package/planning/schema-system/tasks/loader.md +0 -106
  237. package/planning/schema-system/tasks/ref-resolver.md +0 -133
  238. package/planning/schema-system/tasks/strict-mode.md +0 -140
  239. package/planning/schema-system/tasks/typebox-generation.md +0 -133
  240. package/planning/schema-system/tasks/types-and-annotations.md +0 -160
  241. package/planning/schema-system/tasks/validator.md +0 -149
  242. package/src/acl.ts +0 -200
  243. package/src/async-task.ts +0 -267
  244. package/src/bindings.ts +0 -207
  245. package/src/cancel.ts +0 -32
  246. package/src/config.ts +0 -24
  247. package/src/context.ts +0 -160
  248. package/src/decorator.ts +0 -110
  249. package/src/errors.ts +0 -429
  250. package/src/executor.ts +0 -493
  251. package/src/extensions.ts +0 -265
  252. package/src/middleware/adapters.ts +0 -54
  253. package/src/middleware/base.ts +0 -33
  254. package/src/middleware/logging.ts +0 -103
  255. package/src/middleware/manager.ts +0 -105
  256. package/src/module.ts +0 -43
  257. package/src/observability/context-logger.ts +0 -203
  258. package/src/observability/metrics.ts +0 -214
  259. package/src/observability/tracing.ts +0 -252
  260. package/src/registry/dependencies.ts +0 -99
  261. package/src/registry/entry-point.ts +0 -64
  262. package/src/registry/metadata.ts +0 -111
  263. package/src/registry/registry.ts +0 -580
  264. package/src/registry/scanner.ts +0 -168
  265. package/src/registry/schema-export.ts +0 -181
  266. package/src/registry/types.ts +0 -32
  267. package/src/registry/validation.ts +0 -38
  268. package/src/schema/annotations.ts +0 -68
  269. package/src/schema/exporter.ts +0 -90
  270. package/src/schema/loader.ts +0 -273
  271. package/src/schema/ref-resolver.ts +0 -244
  272. package/src/schema/strict.ts +0 -136
  273. package/src/schema/types.ts +0 -73
  274. package/src/schema/validator.ts +0 -82
  275. package/src/trace-context.ts +0 -102
  276. package/src/utils/index.ts +0 -5
  277. package/src/utils/pattern.ts +0 -30
  278. package/tests/async-task.test.ts +0 -335
  279. package/tests/helpers.ts +0 -30
  280. package/tests/integration/test-acl-safety.test.ts +0 -269
  281. package/tests/integration/test-binding-executor.test.ts +0 -194
  282. package/tests/integration/test-e2e-flow.test.ts +0 -117
  283. package/tests/integration/test-error-propagation.test.ts +0 -259
  284. package/tests/integration/test-middleware-chain.test.ts +0 -120
  285. package/tests/integration/test-observability-integration.test.ts +0 -438
  286. package/tests/observability/test-context-logger.test.ts +0 -123
  287. package/tests/observability/test-metrics.test.ts +0 -186
  288. package/tests/observability/test-tracing.test.ts +0 -303
  289. package/tests/registry/test-dependencies.test.ts +0 -70
  290. package/tests/registry/test-entry-point.test.ts +0 -133
  291. package/tests/registry/test-metadata.test.ts +0 -265
  292. package/tests/registry/test-registry.test.ts +0 -1397
  293. package/tests/registry/test-scanner.test.ts +0 -257
  294. package/tests/registry/test-schema-export.test.ts +0 -355
  295. package/tests/registry/test-validation.test.ts +0 -75
  296. package/tests/schema/test-annotations.test.ts +0 -137
  297. package/tests/schema/test-exporter.test.ts +0 -172
  298. package/tests/schema/test-loader.test.ts +0 -461
  299. package/tests/schema/test-ref-resolver.test.ts +0 -530
  300. package/tests/schema/test-strict.test.ts +0 -348
  301. package/tests/schema/test-validator.test.ts +0 -64
  302. package/tests/test-acl.test.ts +0 -423
  303. package/tests/test-bindings.test.ts +0 -227
  304. package/tests/test-cancel.test.ts +0 -71
  305. package/tests/test-config.test.ts +0 -76
  306. package/tests/test-context.test.ts +0 -266
  307. package/tests/test-decorator.test.ts +0 -173
  308. package/tests/test-errors.test.ts +0 -647
  309. package/tests/test-executor-stream.test.ts +0 -208
  310. package/tests/test-executor.test.ts +0 -252
  311. package/tests/test-extensions.test.ts +0 -310
  312. package/tests/test-logging-middleware.test.ts +0 -150
  313. package/tests/test-middleware-manager.test.ts +0 -185
  314. package/tests/test-middleware.test.ts +0 -86
  315. package/tests/test-trace-context.test.ts +0 -251
  316. package/tests/utils/test-pattern.test.ts +0 -109
  317. package/tsconfig.build.json +0 -8
  318. package/tsconfig.json +0 -20
  319. package/vitest.config.ts +0 -18
@@ -1,461 +0,0 @@
1
- import { mkdirSync, writeFileSync, rmSync } from 'node:fs';
2
- import { join } from 'node:path';
3
- import { tmpdir } from 'node:os';
4
- import { describe, it, expect, beforeEach, afterEach } from 'vitest';
5
- import { Type } from '@sinclair/typebox';
6
- import { Value } from '@sinclair/typebox/value';
7
- import { Config } from '../../src/config.js';
8
- import { SchemaNotFoundError, SchemaParseError } from '../../src/errors.js';
9
- import { SchemaLoader, jsonSchemaToTypeBox } from '../../src/schema/loader.js';
10
-
11
- describe('SchemaLoader', () => {
12
- let tmpDir: string;
13
- let schemasDir: string;
14
-
15
- beforeEach(() => {
16
- tmpDir = join(tmpdir(), `apcore-test-loader-${Date.now()}-${Math.random().toString(36).slice(2)}`);
17
- schemasDir = join(tmpDir, 'schemas');
18
- mkdirSync(schemasDir, { recursive: true });
19
- });
20
-
21
- afterEach(() => {
22
- rmSync(tmpDir, { recursive: true, force: true });
23
- });
24
-
25
- function writeSchema(relPath: string, content: string): void {
26
- const fullPath = join(schemasDir, relPath);
27
- const dir = fullPath.replace(/\/[^/]+$/, '');
28
- mkdirSync(dir, { recursive: true });
29
- writeFileSync(fullPath, content, 'utf-8');
30
- }
31
-
32
- function makeConfig(overrides?: Record<string, unknown>): Config {
33
- return new Config({
34
- schema: { root: schemasDir, strategy: 'yaml_first', ...overrides },
35
- });
36
- }
37
-
38
- describe('load', () => {
39
- it('loads a valid YAML schema file', () => {
40
- writeSchema('greeter.schema.yaml', `
41
- description: A greeter module
42
- input_schema:
43
- type: object
44
- properties:
45
- name:
46
- type: string
47
- required:
48
- - name
49
- output_schema:
50
- type: object
51
- properties:
52
- message:
53
- type: string
54
- `);
55
- const loader = new SchemaLoader(makeConfig(), schemasDir);
56
- const sd = loader.load('greeter');
57
-
58
- expect(sd.moduleId).toBe('greeter');
59
- expect(sd.description).toBe('A greeter module');
60
- expect(sd.inputSchema).toEqual({
61
- type: 'object',
62
- properties: { name: { type: 'string' } },
63
- required: ['name'],
64
- });
65
- expect(sd.version).toBe('1.0.0');
66
- });
67
-
68
- it('loads a schema with dot-separated module ID (nested path)', () => {
69
- writeSchema('math/add.schema.yaml', `
70
- description: Add numbers
71
- input_schema:
72
- type: object
73
- properties:
74
- a:
75
- type: number
76
- output_schema:
77
- type: object
78
- properties:
79
- result:
80
- type: number
81
- `);
82
- const loader = new SchemaLoader(makeConfig(), schemasDir);
83
- const sd = loader.load('math.add');
84
-
85
- expect(sd.moduleId).toBe('math.add');
86
- expect(sd.description).toBe('Add numbers');
87
- });
88
-
89
- it('throws SchemaNotFoundError for non-existent schema', () => {
90
- const loader = new SchemaLoader(makeConfig(), schemasDir);
91
- expect(() => loader.load('nonexistent')).toThrow(SchemaNotFoundError);
92
- });
93
-
94
- it('throws SchemaParseError for invalid YAML', () => {
95
- writeSchema('bad.schema.yaml', '{ invalid yaml:: [');
96
- const loader = new SchemaLoader(makeConfig(), schemasDir);
97
- expect(() => loader.load('bad')).toThrow(SchemaParseError);
98
- });
99
-
100
- it('throws SchemaParseError for empty file', () => {
101
- writeSchema('empty.schema.yaml', '');
102
- const loader = new SchemaLoader(makeConfig(), schemasDir);
103
- expect(() => loader.load('empty')).toThrow(SchemaParseError);
104
- });
105
-
106
- it('throws SchemaParseError for array YAML', () => {
107
- writeSchema('arr.schema.yaml', '- item1\n- item2\n');
108
- const loader = new SchemaLoader(makeConfig(), schemasDir);
109
- expect(() => loader.load('arr')).toThrow(SchemaParseError);
110
- });
111
-
112
- it('throws SchemaParseError when required field is missing', () => {
113
- writeSchema('noinput.schema.yaml', `
114
- description: Missing input_schema
115
- output_schema:
116
- type: object
117
- `);
118
- const loader = new SchemaLoader(makeConfig(), schemasDir);
119
- expect(() => loader.load('noinput')).toThrow(SchemaParseError);
120
- expect(() => loader.load('noinput')).toThrow(/Missing required field/);
121
- });
122
-
123
- it('merges definitions and $defs', () => {
124
- writeSchema('withdefs.schema.yaml', `
125
- description: Has definitions
126
- input_schema:
127
- type: object
128
- output_schema:
129
- type: object
130
- definitions:
131
- Foo:
132
- type: string
133
- $defs:
134
- Bar:
135
- type: integer
136
- `);
137
- const loader = new SchemaLoader(makeConfig(), schemasDir);
138
- const sd = loader.load('withdefs');
139
- expect(sd.definitions).toEqual({ Foo: { type: 'string' }, Bar: { type: 'integer' } });
140
- });
141
-
142
- it('returns cached result on second call', () => {
143
- writeSchema('cached.schema.yaml', `
144
- description: Cached
145
- input_schema:
146
- type: object
147
- output_schema:
148
- type: object
149
- `);
150
- const loader = new SchemaLoader(makeConfig(), schemasDir);
151
- const first = loader.load('cached');
152
- const second = loader.load('cached');
153
- expect(first).toBe(second);
154
- });
155
-
156
- it('preserves optional fields (version, documentation, errorSchema)', () => {
157
- writeSchema('full.schema.yaml', `
158
- description: Full schema
159
- version: "2.0.0"
160
- documentation: "Some docs"
161
- input_schema:
162
- type: object
163
- output_schema:
164
- type: object
165
- error_schema:
166
- type: object
167
- properties:
168
- code:
169
- type: string
170
- `);
171
- const loader = new SchemaLoader(makeConfig(), schemasDir);
172
- const sd = loader.load('full');
173
- expect(sd.version).toBe('2.0.0');
174
- expect(sd.documentation).toBe('Some docs');
175
- expect(sd.errorSchema).toEqual({ type: 'object', properties: { code: { type: 'string' } } });
176
- });
177
- });
178
-
179
- describe('resolve', () => {
180
- it('resolves a schema definition into TypeBox schemas', () => {
181
- writeSchema('resolvable.schema.yaml', `
182
- description: Resolvable
183
- input_schema:
184
- type: object
185
- properties:
186
- query:
187
- type: string
188
- required:
189
- - query
190
- output_schema:
191
- type: object
192
- properties:
193
- result:
194
- type: string
195
- `);
196
- const loader = new SchemaLoader(makeConfig(), schemasDir);
197
- const sd = loader.load('resolvable');
198
- const [inputRs, outputRs] = loader.resolve(sd);
199
-
200
- expect(inputRs.moduleId).toBe('resolvable');
201
- expect(inputRs.direction).toBe('input');
202
- expect(Value.Check(inputRs.schema, { query: 'hello' })).toBe(true);
203
- expect(Value.Check(inputRs.schema, {})).toBe(false);
204
-
205
- expect(outputRs.moduleId).toBe('resolvable');
206
- expect(outputRs.direction).toBe('output');
207
- expect(Value.Check(outputRs.schema, { result: 'world' })).toBe(true);
208
- });
209
- });
210
-
211
- describe('getSchema', () => {
212
- const validYaml = `
213
- description: Test module
214
- input_schema:
215
- type: object
216
- properties:
217
- x:
218
- type: string
219
- required:
220
- - x
221
- output_schema:
222
- type: object
223
- properties:
224
- y:
225
- type: string
226
- `;
227
-
228
- it('uses yaml_first strategy and finds YAML', () => {
229
- writeSchema('mod.schema.yaml', validYaml);
230
- const loader = new SchemaLoader(makeConfig(), schemasDir);
231
- const [inputRs, outputRs] = loader.getSchema('mod');
232
-
233
- expect(inputRs.direction).toBe('input');
234
- expect(outputRs.direction).toBe('output');
235
- expect(Value.Check(inputRs.schema, { x: 'hi' })).toBe(true);
236
- });
237
-
238
- it('uses yaml_first strategy and falls back to native when YAML not found', () => {
239
- const loader = new SchemaLoader(makeConfig(), schemasDir);
240
- const nativeInput = Type.Object({ a: Type.String() });
241
- const nativeOutput = Type.Object({ b: Type.Number() });
242
-
243
- const [inputRs, outputRs] = loader.getSchema('missing', nativeInput, nativeOutput);
244
-
245
- expect(inputRs.direction).toBe('input');
246
- expect(inputRs.moduleId).toBe('missing');
247
- expect(Value.Check(inputRs.schema, { a: 'test' })).toBe(true);
248
- expect(outputRs.direction).toBe('output');
249
- expect(Value.Check(outputRs.schema, { b: 42 })).toBe(true);
250
- });
251
-
252
- it('uses yaml_first strategy and throws when YAML not found and no native schemas', () => {
253
- const loader = new SchemaLoader(makeConfig(), schemasDir);
254
- expect(() => loader.getSchema('missing')).toThrow(SchemaNotFoundError);
255
- });
256
-
257
- it('uses native_first strategy and prefers native when available', () => {
258
- writeSchema('native.schema.yaml', validYaml);
259
- const config = makeConfig({ strategy: 'native_first' });
260
- const loader = new SchemaLoader(config, schemasDir);
261
-
262
- const nativeInput = Type.Object({ custom: Type.Boolean() });
263
- const nativeOutput = Type.Object({ out: Type.Boolean() });
264
-
265
- const [inputRs] = loader.getSchema('native', nativeInput, nativeOutput);
266
- // Should use native, not YAML
267
- expect(Value.Check(inputRs.schema, { custom: true })).toBe(true);
268
- expect(Value.Check(inputRs.schema, { x: 'string' })).toBe(false);
269
- });
270
-
271
- it('uses native_first strategy and falls back to YAML when no native', () => {
272
- writeSchema('fallback.schema.yaml', validYaml);
273
- const config = makeConfig({ strategy: 'native_first' });
274
- const loader = new SchemaLoader(config, schemasDir);
275
-
276
- const [inputRs] = loader.getSchema('fallback');
277
- expect(Value.Check(inputRs.schema, { x: 'hi' })).toBe(true);
278
- });
279
-
280
- it('uses yaml_only strategy', () => {
281
- writeSchema('yamlonly.schema.yaml', validYaml);
282
- const config = makeConfig({ strategy: 'yaml_only' });
283
- const loader = new SchemaLoader(config, schemasDir);
284
-
285
- const [inputRs] = loader.getSchema('yamlonly');
286
- expect(Value.Check(inputRs.schema, { x: 'hi' })).toBe(true);
287
- });
288
-
289
- it('uses yaml_only strategy and throws when YAML not found', () => {
290
- const config = makeConfig({ strategy: 'yaml_only' });
291
- const loader = new SchemaLoader(config, schemasDir);
292
-
293
- const nativeInput = Type.Object({ a: Type.String() });
294
- const nativeOutput = Type.Object({ b: Type.Number() });
295
- // yaml_only ignores native schemas
296
- expect(() => loader.getSchema('nope', nativeInput, nativeOutput)).toThrow(SchemaNotFoundError);
297
- });
298
-
299
- it('caches getSchema results', () => {
300
- writeSchema('cacheme.schema.yaml', validYaml);
301
- const loader = new SchemaLoader(makeConfig(), schemasDir);
302
-
303
- const first = loader.getSchema('cacheme');
304
- const second = loader.getSchema('cacheme');
305
- expect(first).toBe(second);
306
- });
307
- });
308
-
309
- describe('clearCache', () => {
310
- it('clears all caches so next load/getSchema reloads from disk', () => {
311
- writeSchema('clearable.schema.yaml', `
312
- description: Clearable
313
- input_schema:
314
- type: object
315
- properties:
316
- v:
317
- type: string
318
- output_schema:
319
- type: object
320
- `);
321
- const loader = new SchemaLoader(makeConfig(), schemasDir);
322
-
323
- const sd1 = loader.load('clearable');
324
- expect(sd1.description).toBe('Clearable');
325
-
326
- // Overwrite file
327
- writeSchema('clearable.schema.yaml', `
328
- description: Updated
329
- input_schema:
330
- type: object
331
- output_schema:
332
- type: object
333
- `);
334
-
335
- // Without clear, returns cached
336
- const sd2 = loader.load('clearable');
337
- expect(sd2.description).toBe('Clearable');
338
-
339
- // After clear, reloads from disk
340
- loader.clearCache();
341
- const sd3 = loader.load('clearable');
342
- expect(sd3.description).toBe('Updated');
343
- });
344
- });
345
-
346
- describe('constructor', () => {
347
- it('uses config schema.root when schemasDir not provided', () => {
348
- writeSchema('fromconfig.schema.yaml', `
349
- description: From config
350
- input_schema:
351
- type: object
352
- output_schema:
353
- type: object
354
- `);
355
- const config = new Config({ schema: { root: schemasDir } });
356
- const loader = new SchemaLoader(config);
357
- const sd = loader.load('fromconfig');
358
- expect(sd.description).toBe('From config');
359
- });
360
-
361
- it('uses default ./schemas when config has no schema.root', () => {
362
- const config = new Config({});
363
- // This just constructs without error; actual path may not exist
364
- const loader = new SchemaLoader(config);
365
- expect(loader).toBeInstanceOf(SchemaLoader);
366
- });
367
- });
368
- });
369
-
370
- describe('jsonSchemaToTypeBox', () => {
371
- it('converts string type', () => {
372
- const schema = jsonSchemaToTypeBox({ type: 'string' });
373
- expect(Value.Check(schema, 'hello')).toBe(true);
374
- expect(Value.Check(schema, 123)).toBe(false);
375
- });
376
-
377
- it('converts integer type', () => {
378
- const schema = jsonSchemaToTypeBox({ type: 'integer' });
379
- expect(Value.Check(schema, 42)).toBe(true);
380
- expect(Value.Check(schema, 3.14)).toBe(false);
381
- });
382
-
383
- it('converts number type', () => {
384
- const schema = jsonSchemaToTypeBox({ type: 'number' });
385
- expect(Value.Check(schema, 3.14)).toBe(true);
386
- expect(Value.Check(schema, 'abc')).toBe(false);
387
- });
388
-
389
- it('converts boolean type', () => {
390
- const schema = jsonSchemaToTypeBox({ type: 'boolean' });
391
- expect(Value.Check(schema, true)).toBe(true);
392
- expect(Value.Check(schema, 'true')).toBe(false);
393
- });
394
-
395
- it('converts null type', () => {
396
- const schema = jsonSchemaToTypeBox({ type: 'null' });
397
- expect(Value.Check(schema, null)).toBe(true);
398
- expect(Value.Check(schema, undefined)).toBe(false);
399
- });
400
-
401
- it('converts object with properties', () => {
402
- const schema = jsonSchemaToTypeBox({
403
- type: 'object',
404
- properties: {
405
- name: { type: 'string' },
406
- age: { type: 'integer' },
407
- },
408
- required: ['name'],
409
- });
410
- expect(Value.Check(schema, { name: 'Alice', age: 30 })).toBe(true);
411
- expect(Value.Check(schema, { name: 'Alice' })).toBe(true);
412
- expect(Value.Check(schema, { age: 30 })).toBe(false);
413
- });
414
-
415
- it('converts array type', () => {
416
- const schema = jsonSchemaToTypeBox({
417
- type: 'array',
418
- items: { type: 'string' },
419
- });
420
- expect(Value.Check(schema, ['a', 'b'])).toBe(true);
421
- expect(Value.Check(schema, [1, 2])).toBe(false);
422
- });
423
-
424
- it('converts enum', () => {
425
- const schema = jsonSchemaToTypeBox({ enum: ['a', 'b', 'c'] });
426
- expect(Value.Check(schema, 'a')).toBe(true);
427
- expect(Value.Check(schema, 'd')).toBe(false);
428
- });
429
-
430
- it('converts anyOf', () => {
431
- const schema = jsonSchemaToTypeBox({
432
- anyOf: [{ type: 'string' }, { type: 'number' }],
433
- });
434
- expect(Value.Check(schema, 'hello')).toBe(true);
435
- expect(Value.Check(schema, 42)).toBe(true);
436
- expect(Value.Check(schema, true)).toBe(false);
437
- });
438
-
439
- it('returns Unknown for unrecognized schema', () => {
440
- const schema = jsonSchemaToTypeBox({});
441
- expect(Value.Check(schema, 'anything')).toBe(true);
442
- expect(Value.Check(schema, 42)).toBe(true);
443
- });
444
-
445
- it('converts string with constraints', () => {
446
- const schema = jsonSchemaToTypeBox({ type: 'string', minLength: 2, maxLength: 5 });
447
- expect(Value.Check(schema, 'ab')).toBe(true);
448
- expect(Value.Check(schema, 'a')).toBe(false);
449
- expect(Value.Check(schema, 'abcdef')).toBe(false);
450
- });
451
-
452
- it('converts object without properties', () => {
453
- const schema = jsonSchemaToTypeBox({ type: 'object' });
454
- expect(Value.Check(schema, { any: 'value' })).toBe(true);
455
- });
456
-
457
- it('converts array without items', () => {
458
- const schema = jsonSchemaToTypeBox({ type: 'array' });
459
- expect(Value.Check(schema, [1, 'two', true])).toBe(true);
460
- });
461
- });