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,423 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
- import { writeFileSync, mkdtempSync, rmSync } from 'node:fs';
3
- import { join } from 'node:path';
4
- import { tmpdir } from 'node:os';
5
- import { ACL } from '../src/acl.js';
6
- import { ACLRuleError, ConfigNotFoundError } from '../src/errors.js';
7
- import { Context, createIdentity } from '../src/context.js';
8
-
9
- function makeContext(opts: {
10
- callerId?: string | null;
11
- callChain?: string[];
12
- identityType?: string;
13
- roles?: string[];
14
- } = {}): Context {
15
- const identity = opts.identityType
16
- ? createIdentity('test-user', opts.identityType, opts.roles ?? [])
17
- : null;
18
- return new Context(
19
- 'trace-test',
20
- opts.callerId ?? null,
21
- opts.callChain ?? [],
22
- null,
23
- identity,
24
- );
25
- }
26
-
27
- describe('ACL', () => {
28
- it('allows access when allow rule matches', () => {
29
- const acl = new ACL([
30
- { callers: ['module.a'], targets: ['module.b'], effect: 'allow', description: '' },
31
- ]);
32
- expect(acl.check('module.a', 'module.b')).toBe(true);
33
- });
34
-
35
- it('denies access when deny rule matches', () => {
36
- const acl = new ACL([
37
- { callers: ['module.a'], targets: ['module.b'], effect: 'deny', description: '' },
38
- ]);
39
- expect(acl.check('module.a', 'module.b')).toBe(false);
40
- });
41
-
42
- it('returns default deny when no rule matches', () => {
43
- const acl = new ACL([
44
- { callers: ['module.a'], targets: ['module.b'], effect: 'allow', description: '' },
45
- ]);
46
- expect(acl.check('module.x', 'module.y')).toBe(false);
47
- });
48
-
49
- it('first-match-wins: deny before allow', () => {
50
- const acl = new ACL([
51
- { callers: ['module.a'], targets: ['module.b'], effect: 'deny', description: '' },
52
- { callers: ['module.a'], targets: ['module.b'], effect: 'allow', description: '' },
53
- ]);
54
- expect(acl.check('module.a', 'module.b')).toBe(false);
55
- });
56
-
57
- it('first-match-wins: allow before deny', () => {
58
- const acl = new ACL([
59
- { callers: ['module.a'], targets: ['module.b'], effect: 'allow', description: '' },
60
- { callers: ['module.a'], targets: ['module.b'], effect: 'deny', description: '' },
61
- ]);
62
- expect(acl.check('module.a', 'module.b')).toBe(true);
63
- });
64
-
65
- it('default effect allow when no rules match', () => {
66
- const acl = new ACL([], 'allow');
67
- expect(acl.check('any', 'thing')).toBe(true);
68
- });
69
-
70
- it('maps null callerId to @external', () => {
71
- const acl = new ACL([
72
- { callers: ['@external'], targets: ['public.api'], effect: 'allow', description: '' },
73
- ]);
74
- expect(acl.check(null, 'public.api')).toBe(true);
75
- });
76
-
77
- it('does not match @external for real module caller', () => {
78
- const acl = new ACL([
79
- { callers: ['@external'], targets: ['public.api'], effect: 'allow', description: '' },
80
- ]);
81
- expect(acl.check('module.a', 'public.api')).toBe(false);
82
- });
83
-
84
- it('wildcard * matches all callers', () => {
85
- const acl = new ACL([
86
- { callers: ['*'], targets: ['public.api'], effect: 'allow', description: '' },
87
- ]);
88
- expect(acl.check('module.a', 'public.api')).toBe(true);
89
- expect(acl.check('module.b', 'public.api')).toBe(true);
90
- });
91
-
92
- it('wildcard * matches all targets', () => {
93
- const acl = new ACL([
94
- { callers: ['module.admin'], targets: ['*'], effect: 'allow', description: '' },
95
- ]);
96
- expect(acl.check('module.admin', 'anything')).toBe(true);
97
- });
98
-
99
- it('prefix wildcard matching', () => {
100
- const acl = new ACL([
101
- { callers: ['core.*'], targets: ['data.*'], effect: 'allow', description: '' },
102
- ]);
103
- expect(acl.check('core.auth', 'data.store')).toBe(true);
104
- expect(acl.check('other.x', 'data.y')).toBe(false);
105
- });
106
-
107
- it('@system matches system identity type', () => {
108
- const acl = new ACL([
109
- { callers: ['@system'], targets: ['*'], effect: 'allow', description: '' },
110
- ]);
111
- const ctx = makeContext({ identityType: 'system' });
112
- expect(acl.check('any.module', 'any.target', ctx)).toBe(true);
113
- });
114
-
115
- it('@system does not match non-system identity', () => {
116
- const acl = new ACL([
117
- { callers: ['@system'], targets: ['*'], effect: 'allow', description: '' },
118
- ]);
119
- const ctx = makeContext({ identityType: 'user' });
120
- expect(acl.check('any.module', 'any.target', ctx)).toBe(false);
121
- });
122
-
123
- it('conditions: identity_types allows matching type', () => {
124
- const acl = new ACL([{
125
- callers: ['*'], targets: ['admin'], effect: 'allow', description: '',
126
- conditions: { identity_types: ['admin'] },
127
- }]);
128
- const ctx = makeContext({ identityType: 'admin' });
129
- expect(acl.check('mod.a', 'admin', ctx)).toBe(true);
130
- });
131
-
132
- it('conditions: identity_types denies non-matching type', () => {
133
- const acl = new ACL([{
134
- callers: ['*'], targets: ['admin'], effect: 'allow', description: '',
135
- conditions: { identity_types: ['admin'] },
136
- }]);
137
- const ctx = makeContext({ identityType: 'user' });
138
- expect(acl.check('mod.a', 'admin', ctx)).toBe(false);
139
- });
140
-
141
- it('conditions: roles allows matching role', () => {
142
- const acl = new ACL([{
143
- callers: ['*'], targets: ['settings'], effect: 'allow', description: '',
144
- conditions: { roles: ['editor', 'admin'] },
145
- }]);
146
- const ctx = makeContext({ identityType: 'user', roles: ['editor'] });
147
- expect(acl.check('mod.a', 'settings', ctx)).toBe(true);
148
- });
149
-
150
- it('conditions: roles denies missing role', () => {
151
- const acl = new ACL([{
152
- callers: ['*'], targets: ['settings'], effect: 'allow', description: '',
153
- conditions: { roles: ['admin'] },
154
- }]);
155
- const ctx = makeContext({ identityType: 'user', roles: ['viewer'] });
156
- expect(acl.check('mod.a', 'settings', ctx)).toBe(false);
157
- });
158
-
159
- it('conditions: max_call_depth allows within limit', () => {
160
- const acl = new ACL([{
161
- callers: ['*'], targets: ['deep'], effect: 'allow', description: '',
162
- conditions: { max_call_depth: 3 },
163
- }]);
164
- const ctx = makeContext({ callChain: ['a', 'b'] });
165
- expect(acl.check('mod.a', 'deep', ctx)).toBe(true);
166
- });
167
-
168
- it('conditions: max_call_depth denies exceeding limit', () => {
169
- const acl = new ACL([{
170
- callers: ['*'], targets: ['deep'], effect: 'allow', description: '',
171
- conditions: { max_call_depth: 2 },
172
- }]);
173
- const ctx = makeContext({ callChain: ['a', 'b', 'c'] });
174
- expect(acl.check('mod.a', 'deep', ctx)).toBe(false);
175
- });
176
-
177
- it('conditions fail when no context provided', () => {
178
- const acl = new ACL([{
179
- callers: ['*'], targets: ['deep'], effect: 'allow', description: '',
180
- conditions: { max_call_depth: 5 },
181
- }]);
182
- expect(acl.check('mod.a', 'deep')).toBe(false);
183
- });
184
-
185
- it('addRule adds to highest priority', () => {
186
- const acl = new ACL([
187
- { callers: ['*'], targets: ['*'], effect: 'deny', description: '' },
188
- ]);
189
- expect(acl.check('mod.a', 'mod.b')).toBe(false);
190
-
191
- acl.addRule({ callers: ['mod.a'], targets: ['mod.b'], effect: 'allow', description: '' });
192
- expect(acl.check('mod.a', 'mod.b')).toBe(true);
193
- });
194
-
195
- it('removeRule removes matching rule', () => {
196
- const acl = new ACL([
197
- { callers: ['mod.a'], targets: ['mod.b'], effect: 'allow', description: '' },
198
- ]);
199
- expect(acl.check('mod.a', 'mod.b')).toBe(true);
200
-
201
- const removed = acl.removeRule(['mod.a'], ['mod.b']);
202
- expect(removed).toBe(true);
203
- expect(acl.check('mod.a', 'mod.b')).toBe(false);
204
- });
205
-
206
- it('removeRule returns false when no match', () => {
207
- const acl = new ACL([]);
208
- expect(acl.removeRule(['x'], ['y'])).toBe(false);
209
- });
210
- });
211
-
212
- describe('ACL.load', () => {
213
- let tmpDir: string;
214
-
215
- beforeEach(() => {
216
- tmpDir = mkdtempSync(join(tmpdir(), 'acl-test-'));
217
- });
218
-
219
- afterEach(() => {
220
- rmSync(tmpDir, { recursive: true, force: true });
221
- });
222
-
223
- it('loads valid ACL from a YAML file', () => {
224
- const yamlContent = `
225
- rules:
226
- - callers: ["module.a"]
227
- targets: ["module.b"]
228
- effect: allow
229
- description: "allow a to b"
230
- `;
231
- const filePath = join(tmpDir, 'acl.yaml');
232
- writeFileSync(filePath, yamlContent, 'utf-8');
233
-
234
- const acl = ACL.load(filePath);
235
- expect(acl.check('module.a', 'module.b')).toBe(true);
236
- expect(acl.check('module.x', 'module.y')).toBe(false);
237
- });
238
-
239
- it('loads ACL with custom default_effect from YAML', () => {
240
- const yamlContent = `
241
- default_effect: allow
242
- rules: []
243
- `;
244
- const filePath = join(tmpDir, 'acl.yaml');
245
- writeFileSync(filePath, yamlContent, 'utf-8');
246
-
247
- const acl = ACL.load(filePath);
248
- expect(acl.check('any.caller', 'any.target')).toBe(true);
249
- });
250
-
251
- it('throws ConfigNotFoundError for missing file', () => {
252
- const missingPath = join(tmpDir, 'nonexistent.yaml');
253
- expect(() => ACL.load(missingPath)).toThrow(ConfigNotFoundError);
254
- });
255
-
256
- it('throws ACLRuleError for invalid YAML syntax', () => {
257
- const filePath = join(tmpDir, 'bad.yaml');
258
- writeFileSync(filePath, ':\n :\n - [invalid', 'utf-8');
259
-
260
- expect(() => ACL.load(filePath)).toThrow(ACLRuleError);
261
- });
262
-
263
- it('throws ACLRuleError when YAML is not a mapping', () => {
264
- const filePath = join(tmpDir, 'array.yaml');
265
- writeFileSync(filePath, '- item1\n- item2\n', 'utf-8');
266
-
267
- expect(() => ACL.load(filePath)).toThrow(ACLRuleError);
268
- expect(() => ACL.load(filePath)).toThrow(/must be a mapping/);
269
- });
270
-
271
- it('throws ACLRuleError when YAML is a scalar', () => {
272
- const filePath = join(tmpDir, 'scalar.yaml');
273
- writeFileSync(filePath, 'just a string\n', 'utf-8');
274
-
275
- expect(() => ACL.load(filePath)).toThrow(ACLRuleError);
276
- expect(() => ACL.load(filePath)).toThrow(/must be a mapping/);
277
- });
278
-
279
- it('throws ACLRuleError when rules key is missing', () => {
280
- const filePath = join(tmpDir, 'norules.yaml');
281
- writeFileSync(filePath, 'default_effect: allow\n', 'utf-8');
282
-
283
- expect(() => ACL.load(filePath)).toThrow(ACLRuleError);
284
- expect(() => ACL.load(filePath)).toThrow(/missing required 'rules' key/);
285
- });
286
-
287
- it('throws ACLRuleError when rules is not an array', () => {
288
- const filePath = join(tmpDir, 'badrules.yaml');
289
- writeFileSync(filePath, 'rules: "not-a-list"\n', 'utf-8');
290
-
291
- expect(() => ACL.load(filePath)).toThrow(ACLRuleError);
292
- expect(() => ACL.load(filePath)).toThrow(/'rules' must be a list/);
293
- });
294
-
295
- it('loads ACL with multiple rules and conditions', () => {
296
- const yamlContent = `
297
- rules:
298
- - callers: ["*"]
299
- targets: ["admin.panel"]
300
- effect: allow
301
- description: "admin access"
302
- conditions:
303
- roles: ["admin"]
304
- - callers: ["*"]
305
- targets: ["*"]
306
- effect: deny
307
- description: "deny all"
308
- `;
309
- const filePath = join(tmpDir, 'multi.yaml');
310
- writeFileSync(filePath, yamlContent, 'utf-8');
311
-
312
- const acl = ACL.load(filePath);
313
- const adminCtx = makeContext({ identityType: 'user', roles: ['admin'] });
314
- const userCtx = makeContext({ identityType: 'user', roles: ['viewer'] });
315
-
316
- expect(acl.check('mod.a', 'admin.panel', adminCtx)).toBe(true);
317
- expect(acl.check('mod.a', 'admin.panel', userCtx)).toBe(false);
318
- });
319
- });
320
-
321
- describe('ACL.reload', () => {
322
- let tmpDir: string;
323
-
324
- beforeEach(() => {
325
- tmpDir = mkdtempSync(join(tmpdir(), 'acl-reload-'));
326
- });
327
-
328
- afterEach(() => {
329
- rmSync(tmpDir, { recursive: true, force: true });
330
- });
331
-
332
- it('reloads updated rules from the same YAML file', () => {
333
- const filePath = join(tmpDir, 'acl.yaml');
334
- writeFileSync(filePath, `
335
- rules:
336
- - callers: ["module.a"]
337
- targets: ["module.b"]
338
- effect: deny
339
- description: "initial deny"
340
- `, 'utf-8');
341
-
342
- const acl = ACL.load(filePath);
343
- expect(acl.check('module.a', 'module.b')).toBe(false);
344
-
345
- writeFileSync(filePath, `
346
- rules:
347
- - callers: ["module.a"]
348
- targets: ["module.b"]
349
- effect: allow
350
- description: "updated allow"
351
- `, 'utf-8');
352
-
353
- acl.reload();
354
- expect(acl.check('module.a', 'module.b')).toBe(true);
355
- });
356
-
357
- it('throws ACLRuleError when ACL was not loaded from a file', () => {
358
- const acl = new ACL([
359
- { callers: ['*'], targets: ['*'], effect: 'allow', description: '' },
360
- ]);
361
-
362
- expect(() => acl.reload()).toThrow(ACLRuleError);
363
- expect(() => acl.reload()).toThrow(/Cannot reload/);
364
- });
365
- });
366
-
367
- describe('ACL constructor validation', () => {
368
- it('throws ACLRuleError for invalid defaultEffect', () => {
369
- expect(() => new ACL([], 'block')).toThrow(ACLRuleError);
370
- expect(() => new ACL([], 'block')).toThrow(/Invalid default_effect/);
371
- });
372
-
373
- it('throws ACLRuleError for empty string defaultEffect', () => {
374
- expect(() => new ACL([], '')).toThrow(ACLRuleError);
375
- });
376
- });
377
-
378
- describe('ACL condition validation', () => {
379
- it('returns false when identity_types condition is not an array', () => {
380
- const acl = new ACL([{
381
- callers: ['*'], targets: ['target'], effect: 'allow', description: '',
382
- conditions: { identity_types: 'admin' },
383
- }]);
384
- const ctx = makeContext({ identityType: 'admin' });
385
- expect(acl.check('mod.a', 'target', ctx)).toBe(false);
386
- });
387
-
388
- it('returns false when roles condition is not an array', () => {
389
- const acl = new ACL([{
390
- callers: ['*'], targets: ['target'], effect: 'allow', description: '',
391
- conditions: { roles: 'admin' },
392
- }]);
393
- const ctx = makeContext({ identityType: 'user', roles: ['admin'] });
394
- expect(acl.check('mod.a', 'target', ctx)).toBe(false);
395
- });
396
-
397
- it('returns false when max_call_depth condition is not a number', () => {
398
- const acl = new ACL([{
399
- callers: ['*'], targets: ['target'], effect: 'allow', description: '',
400
- conditions: { max_call_depth: '5' },
401
- }]);
402
- const ctx = makeContext({ callChain: ['a'] });
403
- expect(acl.check('mod.a', 'target', ctx)).toBe(false);
404
- });
405
-
406
- it('returns false for roles condition when identity is null', () => {
407
- const acl = new ACL([{
408
- callers: ['*'], targets: ['target'], effect: 'allow', description: '',
409
- conditions: { roles: ['admin'] },
410
- }]);
411
- const ctx = makeContext({});
412
- expect(acl.check('mod.a', 'target', ctx)).toBe(false);
413
- });
414
-
415
- it('returns false for identity_types condition when identity is null', () => {
416
- const acl = new ACL([{
417
- callers: ['*'], targets: ['target'], effect: 'allow', description: '',
418
- conditions: { identity_types: ['admin'] },
419
- }]);
420
- const ctx = makeContext({});
421
- expect(acl.check('mod.a', 'target', ctx)).toBe(false);
422
- });
423
- });
@@ -1,227 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
- import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from 'node:fs';
3
- import { join } from 'node:path';
4
- import { tmpdir } from 'node:os';
5
- import { BindingLoader } from '../src/bindings.js';
6
- import { Registry } from '../src/registry/registry.js';
7
- import {
8
- BindingInvalidTargetError,
9
- BindingFileInvalidError,
10
- BindingModuleNotFoundError,
11
- BindingCallableNotFoundError,
12
- BindingNotCallableError,
13
- } from '../src/errors.js';
14
-
15
- let tmpDir: string;
16
- let loader: BindingLoader;
17
- let registry: Registry;
18
-
19
- beforeEach(() => {
20
- tmpDir = mkdtempSync(join(tmpdir(), 'apcore-binding-test-'));
21
- loader = new BindingLoader();
22
- registry = new Registry();
23
- });
24
-
25
- afterEach(() => {
26
- rmSync(tmpDir, { recursive: true, force: true });
27
- });
28
-
29
- function writeTempModule(filename: string, content: string): string {
30
- const filePath = join(tmpDir, filename);
31
- writeFileSync(filePath, content, 'utf-8');
32
- return filePath;
33
- }
34
-
35
- function writeTempYaml(filename: string, content: string): string {
36
- const filePath = join(tmpDir, filename);
37
- writeFileSync(filePath, content, 'utf-8');
38
- return filePath;
39
- }
40
-
41
- describe('BindingLoader', () => {
42
- describe('instantiation', () => {
43
- it('creates a new instance', () => {
44
- expect(new BindingLoader()).toBeInstanceOf(BindingLoader);
45
- });
46
-
47
- it('has loadBindings, loadBindingDir, and resolveTarget methods', () => {
48
- const bl = new BindingLoader();
49
- expect(typeof bl.loadBindings).toBe('function');
50
- expect(typeof bl.loadBindingDir).toBe('function');
51
- expect(typeof bl.resolveTarget).toBe('function');
52
- });
53
- });
54
-
55
- describe('resolveTarget', () => {
56
- it('throws BindingInvalidTargetError for target without colon', async () => {
57
- await expect(loader.resolveTarget('no_colon_here')).rejects.toThrow(BindingInvalidTargetError);
58
- });
59
-
60
- it('throws BindingModuleNotFoundError for non-existent module path', async () => {
61
- await expect(
62
- loader.resolveTarget('/nonexistent/path/to/module.mjs:someFunc'),
63
- ).rejects.toThrow(BindingModuleNotFoundError);
64
- });
65
-
66
- it('successfully resolves a function export from a real JS module', async () => {
67
- const modPath = writeTempModule(
68
- 'func_export.mjs',
69
- 'export function greet(name) { return `Hello, ${name}`; }\n',
70
- );
71
- const fn = await loader.resolveTarget(`${modPath}:greet`);
72
- expect(typeof fn).toBe('function');
73
- expect(fn('World')).toBe('Hello, World');
74
- });
75
-
76
- it('successfully resolves a class method', async () => {
77
- const modPath = writeTempModule(
78
- 'class_export.mjs',
79
- `export class Calculator {\n add(a, b) { return a + b; }\n}\n`,
80
- );
81
- const fn = await loader.resolveTarget(`${modPath}:Calculator.add`);
82
- expect(typeof fn).toBe('function');
83
- expect(fn(2, 3)).toBe(5);
84
- });
85
-
86
- it('throws BindingCallableNotFoundError for missing callable', async () => {
87
- const modPath = writeTempModule('missing_callable.mjs', 'export function exists() { return true; }\n');
88
- await expect(loader.resolveTarget(`${modPath}:doesNotExist`)).rejects.toThrow(BindingCallableNotFoundError);
89
- });
90
-
91
- it('throws BindingNotCallableError for non-function export', async () => {
92
- const modPath = writeTempModule('non_callable.mjs', 'export const MY_CONSTANT = 42;\n');
93
- await expect(loader.resolveTarget(`${modPath}:MY_CONSTANT`)).rejects.toThrow(BindingNotCallableError);
94
- });
95
- });
96
-
97
- describe('loadBindings', () => {
98
- it('throws BindingFileInvalidError for non-existent file', async () => {
99
- await expect(
100
- loader.loadBindings('/nonexistent/path/binding.yaml', registry),
101
- ).rejects.toThrow(BindingFileInvalidError);
102
- });
103
-
104
- it('throws BindingFileInvalidError for invalid YAML', async () => {
105
- const yamlPath = writeTempYaml('invalid.binding.yaml', '{ invalid yaml: [unclosed');
106
- await expect(loader.loadBindings(yamlPath, registry)).rejects.toThrow(BindingFileInvalidError);
107
- });
108
-
109
- it('throws BindingFileInvalidError for empty file', async () => {
110
- const yamlPath = writeTempYaml('empty.binding.yaml', '');
111
- await expect(loader.loadBindings(yamlPath, registry)).rejects.toThrow(BindingFileInvalidError);
112
- });
113
-
114
- it('throws BindingFileInvalidError for missing bindings key', async () => {
115
- const yamlPath = writeTempYaml('nokey.binding.yaml', 'other_key: value\n');
116
- await expect(loader.loadBindings(yamlPath, registry)).rejects.toThrow(BindingFileInvalidError);
117
- });
118
-
119
- it('throws BindingFileInvalidError for non-array bindings value', async () => {
120
- const yamlPath = writeTempYaml('notarray.binding.yaml', 'bindings: "not an array"\n');
121
- await expect(loader.loadBindings(yamlPath, registry)).rejects.toThrow(BindingFileInvalidError);
122
- });
123
-
124
- it('throws BindingFileInvalidError for binding entry missing module_id', async () => {
125
- const modPath = writeTempModule('dummy_mod.mjs', 'export function dummy() { return {}; }\n');
126
- const yamlPath = writeTempYaml('noid.binding.yaml', `bindings:\n - target: "${modPath}:dummy"\n`);
127
- await expect(loader.loadBindings(yamlPath, registry)).rejects.toThrow(BindingFileInvalidError);
128
- });
129
-
130
- it('throws BindingFileInvalidError for binding entry missing target', async () => {
131
- const yamlPath = writeTempYaml('notarget.binding.yaml', 'bindings:\n - module_id: "test.module"\n');
132
- await expect(loader.loadBindings(yamlPath, registry)).rejects.toThrow(BindingFileInvalidError);
133
- });
134
-
135
- it('successfully loads valid binding with inline schemas', async () => {
136
- const modPath = writeTempModule(
137
- 'inline_schema_mod.mjs',
138
- 'export function process(inputs) { return { result: inputs.name }; }\n',
139
- );
140
- const yamlPath = writeTempYaml(
141
- 'inline.binding.yaml',
142
- `bindings:\n - module_id: "test.inline"\n target: "${modPath}:process"\n description: "Inline schema test"\n version: "2.0.0"\n tags:\n - demo\n input_schema:\n type: object\n properties:\n name:\n type: string\n output_schema:\n type: object\n properties:\n result:\n type: string\n`,
143
- );
144
- const results = await loader.loadBindings(yamlPath, registry);
145
- expect(results).toHaveLength(1);
146
- expect(results[0].moduleId).toBe('test.inline');
147
- expect(results[0].description).toBe('Inline schema test');
148
- expect(results[0].version).toBe('2.0.0');
149
- });
150
-
151
- it('successfully loads binding with permissive fallback (no schema)', async () => {
152
- const modPath = writeTempModule('permissive_mod.mjs', 'export function loose(inputs) { return { ok: true }; }\n');
153
- const yamlPath = writeTempYaml(
154
- 'permissive.binding.yaml',
155
- `bindings:\n - module_id: "test.permissive"\n target: "${modPath}:loose"\n`,
156
- );
157
- const results = await loader.loadBindings(yamlPath, registry);
158
- expect(results).toHaveLength(1);
159
- expect(results[0].moduleId).toBe('test.permissive');
160
- expect(results[0].inputSchema).toBeDefined();
161
- expect(results[0].outputSchema).toBeDefined();
162
- });
163
-
164
- it('registers modules in the registry', async () => {
165
- const modPath = writeTempModule('registered_mod.mjs', 'export function handler() { return {}; }\n');
166
- const yamlPath = writeTempYaml(
167
- 'register.binding.yaml',
168
- `bindings:\n - module_id: "test.registered"\n target: "${modPath}:handler"\n`,
169
- );
170
- await loader.loadBindings(yamlPath, registry);
171
- expect(registry.has('test.registered')).toBe(true);
172
- });
173
-
174
- it('loads multiple binding entries from single file', async () => {
175
- const modPath = writeTempModule(
176
- 'multi_mod.mjs',
177
- `export function funcA() { return { a: true }; }\nexport function funcB() { return { b: true }; }\n`,
178
- );
179
- const yamlPath = writeTempYaml(
180
- 'multi.binding.yaml',
181
- `bindings:\n - module_id: "test.multi.a"\n target: "${modPath}:funcA"\n - module_id: "test.multi.b"\n target: "${modPath}:funcB"\n`,
182
- );
183
- const results = await loader.loadBindings(yamlPath, registry);
184
- expect(results).toHaveLength(2);
185
- expect(registry.has('test.multi.a')).toBe(true);
186
- expect(registry.has('test.multi.b')).toBe(true);
187
- });
188
- });
189
-
190
- describe('loadBindingDir', () => {
191
- it('throws BindingFileInvalidError for non-existent directory', async () => {
192
- await expect(loader.loadBindingDir('/nonexistent/dir/path', registry)).rejects.toThrow(BindingFileInvalidError);
193
- });
194
-
195
- it('loads all *.binding.yaml files in directory', async () => {
196
- const bindDir = join(tmpDir, 'bindings');
197
- mkdirSync(bindDir);
198
-
199
- const modPath = writeTempModule(
200
- 'dir_mod.mjs',
201
- `export function alpha() { return { alpha: true }; }\nexport function beta() { return { beta: true }; }\n`,
202
- );
203
-
204
- writeTempYaml(
205
- join('bindings', 'alpha.binding.yaml'),
206
- `bindings:\n - module_id: "dir.alpha"\n target: "${modPath}:alpha"\n`,
207
- );
208
- writeTempYaml(
209
- join('bindings', 'beta.binding.yaml'),
210
- `bindings:\n - module_id: "dir.beta"\n target: "${modPath}:beta"\n`,
211
- );
212
-
213
- const results = await loader.loadBindingDir(bindDir, registry);
214
- expect(results).toHaveLength(2);
215
- expect(registry.has('dir.alpha')).toBe(true);
216
- expect(registry.has('dir.beta')).toBe(true);
217
- });
218
-
219
- it('returns empty array for directory with no binding files', async () => {
220
- const emptyDir = join(tmpDir, 'empty');
221
- mkdirSync(emptyDir);
222
-
223
- const results = await loader.loadBindingDir(emptyDir, registry);
224
- expect(results).toHaveLength(0);
225
- });
226
- });
227
- });