@pikku/core 0.12.0 → 0.12.2

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 (290) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/errors/errors.d.ts +6 -0
  3. package/dist/errors/errors.js +10 -0
  4. package/dist/errors/index.d.ts +1 -0
  5. package/dist/errors/index.js +1 -0
  6. package/dist/function/function-runner.d.ts +3 -3
  7. package/dist/function/function-runner.js +66 -44
  8. package/dist/function/functions.types.d.ts +1 -0
  9. package/dist/handle-error.d.ts +2 -2
  10. package/dist/handle-error.js +2 -2
  11. package/dist/index.d.ts +5 -3
  12. package/dist/index.js +2 -1
  13. package/dist/internal.d.ts +3 -0
  14. package/dist/internal.js +2 -0
  15. package/dist/middleware/auth-bearer.d.ts +1 -1
  16. package/dist/middleware/auth-bearer.js +1 -1
  17. package/dist/middleware/auth-cookie.d.ts +2 -2
  18. package/dist/middleware/auth-cookie.js +1 -1
  19. package/dist/middleware/cors.d.ts +2 -2
  20. package/dist/middleware/cors.js +45 -37
  21. package/dist/middleware-runner.d.ts +1 -1
  22. package/dist/middleware-runner.js +1 -0
  23. package/dist/permissions.d.ts +2 -2
  24. package/dist/permissions.js +1 -0
  25. package/dist/pikku-state.d.ts +9 -8
  26. package/dist/pikku-state.js +34 -17
  27. package/dist/schema.d.ts +4 -4
  28. package/dist/schema.js +9 -5
  29. package/dist/services/ai-agent-runner-service.d.ts +22 -3
  30. package/dist/services/ai-run-state-service.d.ts +1 -1
  31. package/dist/services/ai-storage-service.d.ts +1 -1
  32. package/dist/services/content-service.d.ts +6 -0
  33. package/dist/services/gateway-service.d.ts +19 -0
  34. package/dist/services/gopass-secrets.d.ts +1 -1
  35. package/dist/services/gopass-secrets.js +3 -0
  36. package/dist/services/in-memory-ai-run-state-service.d.ts +15 -0
  37. package/dist/services/in-memory-ai-run-state-service.js +44 -0
  38. package/dist/services/in-memory-trigger-service.d.ts +0 -1
  39. package/dist/services/in-memory-trigger-service.js +6 -8
  40. package/dist/services/index.d.ts +5 -2
  41. package/dist/services/index.js +2 -0
  42. package/dist/services/local-content.d.ts +2 -1
  43. package/dist/services/local-content.js +6 -1
  44. package/dist/services/local-gateway-service.d.ts +22 -0
  45. package/dist/services/local-gateway-service.js +51 -0
  46. package/dist/services/local-secrets.d.ts +2 -2
  47. package/dist/services/local-variables.d.ts +1 -1
  48. package/dist/services/logger-console.d.ts +2 -1
  49. package/dist/services/scheduler-service.d.ts +0 -12
  50. package/dist/services/scheduler-service.js +0 -6
  51. package/dist/services/scoped-secret-service.d.ts +1 -1
  52. package/dist/services/trigger-service.d.ts +0 -7
  53. package/dist/services/user-session-service.d.ts +3 -3
  54. package/dist/services/workflow-service.d.ts +2 -3
  55. package/dist/types/core.types.d.ts +27 -19
  56. package/dist/types/state.types.d.ts +22 -15
  57. package/dist/utils.d.ts +5 -5
  58. package/dist/utils.js +12 -9
  59. package/dist/wirings/ai-agent/ai-agent-assistant-ui.d.ts +5 -0
  60. package/dist/wirings/ai-agent/ai-agent-assistant-ui.js +168 -0
  61. package/dist/wirings/ai-agent/ai-agent-helpers.d.ts +28 -0
  62. package/dist/wirings/ai-agent/ai-agent-helpers.js +40 -0
  63. package/dist/wirings/ai-agent/ai-agent-memory.js +13 -2
  64. package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +4 -5
  65. package/dist/wirings/ai-agent/ai-agent-prepare.js +80 -31
  66. package/dist/wirings/ai-agent/ai-agent-registry.d.ts +1 -1
  67. package/dist/wirings/ai-agent/ai-agent-runner.js +107 -8
  68. package/dist/wirings/ai-agent/ai-agent-stream.d.ts +2 -1
  69. package/dist/wirings/ai-agent/ai-agent-stream.js +263 -89
  70. package/dist/wirings/ai-agent/ai-agent.types.d.ts +96 -4
  71. package/dist/wirings/ai-agent/index.d.ts +3 -1
  72. package/dist/wirings/ai-agent/index.js +2 -0
  73. package/dist/wirings/channel/channel-common.d.ts +2 -2
  74. package/dist/wirings/channel/channel-handler.d.ts +7 -4
  75. package/dist/wirings/channel/channel-handler.js +15 -5
  76. package/dist/wirings/channel/channel-middleware-runner.js +1 -0
  77. package/dist/wirings/channel/channel-runner.d.ts +5 -5
  78. package/dist/wirings/channel/channel-runner.js +3 -2
  79. package/dist/wirings/channel/channel-store.d.ts +1 -1
  80. package/dist/wirings/channel/channel.types.d.ts +10 -6
  81. package/dist/wirings/channel/index.d.ts +1 -1
  82. package/dist/wirings/channel/local/local-channel-handler.d.ts +7 -0
  83. package/dist/wirings/channel/local/local-channel-handler.js +17 -0
  84. package/dist/wirings/channel/local/local-channel-runner.d.ts +7 -2
  85. package/dist/wirings/channel/local/local-channel-runner.js +18 -4
  86. package/dist/wirings/channel/local/local-eventhub-service.d.ts +2 -2
  87. package/dist/wirings/channel/log-channels.d.ts +1 -1
  88. package/dist/wirings/channel/pikku-abstract-channel-handler.d.ts +2 -1
  89. package/dist/wirings/channel/pikku-abstract-channel-handler.js +1 -0
  90. package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +4 -4
  91. package/dist/wirings/channel/serverless/serverless-channel-runner.js +26 -20
  92. package/dist/wirings/cli/channel/cli-channel-runner.d.ts +1 -1
  93. package/dist/wirings/cli/cli-runner.d.ts +2 -2
  94. package/dist/wirings/cli/cli-runner.js +3 -0
  95. package/dist/wirings/cli/cli.types.d.ts +3 -3
  96. package/dist/wirings/cli/command-parser.d.ts +1 -1
  97. package/dist/wirings/gateway/gateway-runner.d.ts +24 -0
  98. package/dist/wirings/gateway/gateway-runner.js +325 -0
  99. package/dist/wirings/gateway/gateway.types.d.ts +127 -0
  100. package/dist/wirings/gateway/index.d.ts +2 -0
  101. package/dist/wirings/gateway/index.js +1 -0
  102. package/dist/wirings/http/http-runner.d.ts +9 -9
  103. package/dist/wirings/http/http-runner.js +36 -14
  104. package/dist/wirings/http/http.types.d.ts +2 -6
  105. package/dist/wirings/http/log-http-routes.d.ts +1 -1
  106. package/dist/wirings/http/pikku-fetch-http-request.d.ts +1 -1
  107. package/dist/wirings/http/pikku-fetch-http-response.d.ts +2 -2
  108. package/dist/wirings/http/pikku-fetch-http-response.js +6 -1
  109. package/dist/wirings/http/routers/http-router.d.ts +1 -1
  110. package/dist/wirings/http/routers/path-to-regex.d.ts +2 -2
  111. package/dist/wirings/mcp/mcp-endpoint-registry.d.ts +1 -1
  112. package/dist/wirings/mcp/mcp-runner.d.ts +0 -3
  113. package/dist/wirings/mcp/mcp-runner.js +4 -2
  114. package/dist/wirings/mcp/mcp.types.d.ts +2 -2
  115. package/dist/wirings/oauth2/oauth2-client.d.ts +3 -3
  116. package/dist/wirings/oauth2/wire-oauth2-credential.d.ts +1 -1
  117. package/dist/wirings/queue/index.d.ts +1 -1
  118. package/dist/wirings/queue/index.js +1 -1
  119. package/dist/wirings/queue/queue-runner.d.ts +1 -12
  120. package/dist/wirings/queue/queue-runner.js +4 -12
  121. package/dist/wirings/queue/queue.types.d.ts +3 -9
  122. package/dist/wirings/queue/register-queue-helper.d.ts +2 -2
  123. package/dist/wirings/queue/register-queue-helper.js +1 -1
  124. package/dist/wirings/queue/validate-worker-config.d.ts +1 -1
  125. package/dist/wirings/rpc/index.d.ts +2 -0
  126. package/dist/wirings/rpc/index.js +1 -0
  127. package/dist/wirings/rpc/rpc-runner.d.ts +36 -14
  128. package/dist/wirings/rpc/rpc-runner.js +56 -28
  129. package/dist/wirings/rpc/rpc-types.d.ts +14 -2
  130. package/dist/wirings/rpc/wire-addon.d.ts +10 -0
  131. package/dist/wirings/rpc/wire-addon.js +9 -0
  132. package/dist/wirings/scheduler/index.d.ts +1 -1
  133. package/dist/wirings/scheduler/index.js +1 -1
  134. package/dist/wirings/scheduler/log-schedulers.d.ts +1 -1
  135. package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -10
  136. package/dist/wirings/scheduler/scheduler-runner.js +4 -25
  137. package/dist/wirings/scheduler/scheduler.types.d.ts +2 -2
  138. package/dist/wirings/trigger/pikku-trigger-service.d.ts +2 -6
  139. package/dist/wirings/trigger/pikku-trigger-service.js +11 -16
  140. package/dist/wirings/trigger/trigger-runner.d.ts +1 -5
  141. package/dist/wirings/trigger/trigger-runner.js +4 -3
  142. package/dist/wirings/workflow/pikku-workflow-service.d.ts +2 -6
  143. package/dist/wirings/workflow/pikku-workflow-service.js +20 -29
  144. package/dist/wirings/workflow/workflow.types.d.ts +2 -2
  145. package/package.json +4 -2
  146. package/src/crypto-utils.test.ts +116 -0
  147. package/src/errors/error.test.ts +143 -2
  148. package/src/errors/errors.ts +10 -0
  149. package/src/errors/index.ts +1 -0
  150. package/src/function/function-runner.test.ts +2 -2
  151. package/src/function/function-runner.ts +72 -49
  152. package/src/function/functions.types.ts +1 -0
  153. package/src/handle-error.test.ts +361 -0
  154. package/src/handle-error.ts +4 -4
  155. package/src/index.ts +3 -10
  156. package/src/internal.ts +6 -0
  157. package/src/middleware/auth-apikey.test.ts +1 -1
  158. package/src/middleware/auth-bearer.test.ts +1 -1
  159. package/src/middleware/auth-bearer.ts +2 -5
  160. package/src/middleware/auth-cookie.test.ts +1 -1
  161. package/src/middleware/auth-cookie.ts +3 -5
  162. package/src/middleware/cors.test.ts +424 -0
  163. package/src/middleware/cors.ts +14 -6
  164. package/src/middleware/remote-auth.test.ts +488 -0
  165. package/src/middleware-runner.test.ts +1 -1
  166. package/src/middleware-runner.ts +2 -1
  167. package/src/permissions.test.ts +2 -2
  168. package/src/permissions.ts +3 -2
  169. package/src/pikku-state.test.ts +224 -0
  170. package/src/pikku-state.ts +45 -28
  171. package/src/schema.test.ts +198 -6
  172. package/src/schema.ts +11 -7
  173. package/src/services/ai-agent-runner-service.ts +15 -3
  174. package/src/services/ai-run-state-service.ts +1 -1
  175. package/src/services/ai-storage-service.ts +1 -1
  176. package/src/services/content-service.ts +7 -0
  177. package/src/services/gateway-service.ts +20 -0
  178. package/src/services/gopass-secrets.ts +4 -1
  179. package/src/services/in-memory-ai-run-state-service.ts +73 -0
  180. package/src/services/in-memory-trigger-service.ts +6 -10
  181. package/src/services/in-memory-workflow-service.test.ts +351 -0
  182. package/src/services/index.ts +4 -1
  183. package/src/services/local-content.ts +9 -3
  184. package/src/services/local-gateway-service.ts +62 -0
  185. package/src/services/local-secrets.test.ts +80 -0
  186. package/src/services/local-secrets.ts +2 -2
  187. package/src/services/local-variables.test.ts +61 -0
  188. package/src/services/local-variables.ts +1 -1
  189. package/src/services/logger-console.test.ts +118 -0
  190. package/src/services/logger-console.ts +2 -1
  191. package/src/services/scheduler-service.ts +0 -18
  192. package/src/services/scoped-secret-service.test.ts +74 -0
  193. package/src/services/scoped-secret-service.ts +1 -1
  194. package/src/services/trigger-service.ts +0 -21
  195. package/src/services/typed-secret-service.test.ts +93 -0
  196. package/src/services/typed-variables-service.test.ts +73 -0
  197. package/src/services/user-session-service.test.ts +113 -0
  198. package/src/services/user-session-service.ts +3 -3
  199. package/src/services/workflow-service.ts +2 -12
  200. package/src/time-utils.test.ts +1 -1
  201. package/src/types/core.types.ts +28 -19
  202. package/src/types/state.types.ts +32 -15
  203. package/src/utils.test.ts +350 -0
  204. package/src/utils.ts +14 -13
  205. package/src/wirings/ai-agent/ai-agent-assistant-ui.test.ts +363 -0
  206. package/src/wirings/ai-agent/ai-agent-assistant-ui.ts +238 -0
  207. package/src/wirings/ai-agent/ai-agent-helpers.test.ts +152 -0
  208. package/src/wirings/ai-agent/ai-agent-helpers.ts +76 -0
  209. package/src/wirings/ai-agent/ai-agent-memory.ts +10 -1
  210. package/src/wirings/ai-agent/ai-agent-model-config.test.ts +115 -0
  211. package/src/wirings/ai-agent/ai-agent-prepare.ts +98 -46
  212. package/src/wirings/ai-agent/ai-agent-registry.ts +1 -1
  213. package/src/wirings/ai-agent/ai-agent-runner.test.ts +245 -3
  214. package/src/wirings/ai-agent/ai-agent-runner.ts +121 -7
  215. package/src/wirings/ai-agent/ai-agent-stream.test.ts +430 -24
  216. package/src/wirings/ai-agent/ai-agent-stream.ts +390 -104
  217. package/src/wirings/ai-agent/ai-agent.types.ts +91 -4
  218. package/src/wirings/ai-agent/index.ts +13 -0
  219. package/src/wirings/channel/channel-common.ts +2 -2
  220. package/src/wirings/channel/channel-handler.ts +43 -11
  221. package/src/wirings/channel/channel-middleware-runner.ts +1 -0
  222. package/src/wirings/channel/channel-runner.ts +6 -6
  223. package/src/wirings/channel/channel-store.ts +1 -1
  224. package/src/wirings/channel/channel.types.ts +15 -8
  225. package/src/wirings/channel/index.ts +1 -0
  226. package/src/wirings/channel/local/local-channel-handler.ts +26 -0
  227. package/src/wirings/channel/local/local-channel-runner.test.ts +19 -12
  228. package/src/wirings/channel/local/local-channel-runner.ts +33 -14
  229. package/src/wirings/channel/local/local-eventhub-service.test.ts +2 -2
  230. package/src/wirings/channel/local/local-eventhub-service.ts +2 -2
  231. package/src/wirings/channel/log-channels.ts +1 -1
  232. package/src/wirings/channel/pikku-abstract-channel-handler.test.ts +2 -0
  233. package/src/wirings/channel/pikku-abstract-channel-handler.ts +8 -1
  234. package/src/wirings/channel/serverless/serverless-channel-runner.ts +38 -33
  235. package/src/wirings/cli/channel/cli-channel-runner.ts +1 -1
  236. package/src/wirings/cli/cli-runner.test.ts +1 -1
  237. package/src/wirings/cli/cli-runner.ts +14 -4
  238. package/src/wirings/cli/cli.types.ts +3 -3
  239. package/src/wirings/cli/command-parser.test.ts +1 -1
  240. package/src/wirings/cli/command-parser.ts +1 -1
  241. package/src/wirings/gateway/gateway-runner.test.ts +474 -0
  242. package/src/wirings/gateway/gateway-runner.ts +411 -0
  243. package/src/wirings/gateway/gateway.types.ts +149 -0
  244. package/src/wirings/gateway/index.ts +13 -0
  245. package/src/wirings/http/http-routes.test.ts +1 -1
  246. package/src/wirings/http/http-runner.test.ts +9 -17
  247. package/src/wirings/http/http-runner.ts +46 -27
  248. package/src/wirings/http/http.types.ts +1 -14
  249. package/src/wirings/http/log-http-routes.ts +1 -1
  250. package/src/wirings/http/pikku-fetch-http-request.ts +1 -1
  251. package/src/wirings/http/pikku-fetch-http-response.ts +12 -5
  252. package/src/wirings/http/routers/http-router.ts +1 -1
  253. package/src/wirings/http/routers/path-to-regex.test.ts +1 -1
  254. package/src/wirings/http/routers/path-to-regex.ts +4 -3
  255. package/src/wirings/mcp/mcp-endpoint-registry.ts +5 -1
  256. package/src/wirings/mcp/mcp-runner.ts +9 -15
  257. package/src/wirings/mcp/mcp.types.ts +2 -2
  258. package/src/wirings/oauth2/oauth2-client.ts +3 -3
  259. package/src/wirings/oauth2/wire-oauth2-credential.ts +1 -1
  260. package/src/wirings/queue/index.ts +0 -1
  261. package/src/wirings/queue/queue-runner.test.ts +52 -25
  262. package/src/wirings/queue/queue-runner.ts +8 -30
  263. package/src/wirings/queue/queue.types.ts +3 -13
  264. package/src/wirings/queue/register-queue-helper.ts +3 -5
  265. package/src/wirings/queue/validate-worker-config.test.ts +108 -0
  266. package/src/wirings/queue/validate-worker-config.ts +4 -1
  267. package/src/wirings/rpc/index.ts +2 -0
  268. package/src/wirings/rpc/rpc-runner.ts +92 -49
  269. package/src/wirings/rpc/rpc-types.ts +17 -2
  270. package/src/wirings/rpc/wire-addon.ts +20 -0
  271. package/src/wirings/scheduler/index.ts +0 -1
  272. package/src/wirings/scheduler/log-schedulers.ts +1 -1
  273. package/src/wirings/scheduler/scheduler-runner.test.ts +49 -60
  274. package/src/wirings/scheduler/scheduler-runner.ts +9 -56
  275. package/src/wirings/scheduler/scheduler.types.ts +2 -2
  276. package/src/wirings/secret/validate-secret-definitions.test.ts +140 -0
  277. package/src/wirings/trigger/pikku-trigger-service.ts +17 -39
  278. package/src/wirings/trigger/trigger-runner.test.ts +79 -0
  279. package/src/wirings/trigger/trigger-runner.ts +8 -11
  280. package/src/wirings/variable/validate-variable-definitions.test.ts +91 -0
  281. package/src/wirings/workflow/graph/graph-runner.test.ts +14 -22
  282. package/src/wirings/workflow/graph/template.test.ts +49 -0
  283. package/src/wirings/workflow/pikku-workflow-service.test.ts +15 -27
  284. package/src/wirings/workflow/pikku-workflow-service.ts +29 -45
  285. package/src/wirings/workflow/workflow-helpers.test.ts +129 -0
  286. package/src/wirings/workflow/workflow.types.ts +2 -2
  287. package/tsconfig.tsbuildinfo +1 -1
  288. package/src/wirings/workflow/workflow-utils.ts +0 -0
  289. /package/dist/{wirings/workflow/workflow-utils.d.ts → services/gateway-service.js} +0 -0
  290. /package/dist/wirings/{workflow/workflow-utils.js → gateway/gateway.types.js} +0 -0
@@ -0,0 +1,350 @@
1
+ import { describe, test, beforeEach } from 'node:test'
2
+ import assert from 'node:assert'
3
+ import {
4
+ closeWireServices,
5
+ createWeakUID,
6
+ isSerializable,
7
+ getTagGroups,
8
+ freezeDedupe,
9
+ stopSingletonServices,
10
+ } from './utils.js'
11
+ import {
12
+ resetPikkuState,
13
+ pikkuState,
14
+ initializePikkuState,
15
+ } from './pikku-state.js'
16
+
17
+ beforeEach(() => {
18
+ resetPikkuState()
19
+ })
20
+
21
+ describe('createWeakUID', () => {
22
+ test('should return a string', () => {
23
+ const uid = createWeakUID()
24
+ assert.strictEqual(typeof uid, 'string')
25
+ })
26
+
27
+ test('should return unique values', () => {
28
+ const uid1 = createWeakUID()
29
+ const uid2 = createWeakUID()
30
+ assert.notStrictEqual(uid1, uid2)
31
+ })
32
+
33
+ test('should contain a dash separator', () => {
34
+ const uid = createWeakUID()
35
+ assert.ok(uid.includes('-'))
36
+ })
37
+ })
38
+
39
+ describe('isSerializable', () => {
40
+ test('should return true for objects', () => {
41
+ assert.strictEqual(isSerializable({ key: 'value' }), true)
42
+ })
43
+
44
+ test('should return true for arrays', () => {
45
+ assert.strictEqual(isSerializable([1, 2, 3]), true)
46
+ })
47
+
48
+ test('should return true for numbers', () => {
49
+ assert.strictEqual(isSerializable(42), true)
50
+ })
51
+
52
+ test('should return true for booleans', () => {
53
+ assert.strictEqual(isSerializable(true), true)
54
+ })
55
+
56
+ test('should return true for null', () => {
57
+ assert.strictEqual(isSerializable(null), true)
58
+ })
59
+
60
+ test('should return false for strings', () => {
61
+ assert.strictEqual(isSerializable('hello'), false)
62
+ })
63
+
64
+ test('should return false for ArrayBuffer', () => {
65
+ assert.strictEqual(isSerializable(new ArrayBuffer(8)), false)
66
+ })
67
+
68
+ test('should return false for Uint8Array', () => {
69
+ assert.strictEqual(isSerializable(new Uint8Array(8)), false)
70
+ })
71
+
72
+ test('should return false for Int8Array', () => {
73
+ assert.strictEqual(isSerializable(new Int8Array(8)), false)
74
+ })
75
+
76
+ test('should return false for Uint16Array', () => {
77
+ assert.strictEqual(isSerializable(new Uint16Array(8)), false)
78
+ })
79
+
80
+ test('should return false for Float64Array', () => {
81
+ assert.strictEqual(isSerializable(new Float64Array(8)), false)
82
+ })
83
+ })
84
+
85
+ describe('getTagGroups', () => {
86
+ test('should return exact match', () => {
87
+ const groups = { billing: 'billing-middleware' as any }
88
+ const result = getTagGroups(groups, 'billing')
89
+ assert.deepStrictEqual(result, ['billing-middleware'])
90
+ })
91
+
92
+ test('should return parent tag for namespaced tag', () => {
93
+ const groups = { billing: 'billing-middleware' as any }
94
+ const result = getTagGroups(groups, 'billing:read')
95
+ assert.deepStrictEqual(result, ['billing-middleware'])
96
+ })
97
+
98
+ test('should return both exact and parent matches', () => {
99
+ const groups = {
100
+ billing: 'billing-middleware' as any,
101
+ 'billing:read': 'billing-read-middleware' as any,
102
+ }
103
+ const result = getTagGroups(groups, 'billing:read')
104
+ assert.deepStrictEqual(result, [
105
+ 'billing-read-middleware',
106
+ 'billing-middleware',
107
+ ])
108
+ })
109
+
110
+ test('should resolve multi-level namespace', () => {
111
+ const groups = {
112
+ billing: 'b' as any,
113
+ 'billing:read': 'br' as any,
114
+ 'billing:read:admin': 'bra' as any,
115
+ }
116
+ const result = getTagGroups(groups, 'billing:read:admin')
117
+ assert.deepStrictEqual(result, ['bra', 'br', 'b'])
118
+ })
119
+
120
+ test('should return empty array for unmatched tag', () => {
121
+ const groups = { billing: 'b' as any }
122
+ const result = getTagGroups(groups, 'other')
123
+ assert.deepStrictEqual(result, [])
124
+ })
125
+
126
+ test('should skip missing intermediate groups', () => {
127
+ const groups = {
128
+ billing: 'b' as any,
129
+ // no billing:read
130
+ 'billing:read:admin': 'bra' as any,
131
+ }
132
+ const result = getTagGroups(groups, 'billing:read:admin')
133
+ assert.deepStrictEqual(result, ['bra', 'b'])
134
+ })
135
+ })
136
+
137
+ describe('freezeDedupe', () => {
138
+ test('should return frozen empty array for undefined', () => {
139
+ const result = freezeDedupe(undefined)
140
+ assert.strictEqual(result.length, 0)
141
+ assert.ok(Object.isFrozen(result))
142
+ })
143
+
144
+ test('should return frozen empty array for empty array', () => {
145
+ const result = freezeDedupe([])
146
+ assert.strictEqual(result.length, 0)
147
+ assert.ok(Object.isFrozen(result))
148
+ })
149
+
150
+ test('should return same frozen reference for empty arrays', () => {
151
+ const result1 = freezeDedupe([])
152
+ const result2 = freezeDedupe(undefined)
153
+ assert.strictEqual(result1, result2)
154
+ })
155
+
156
+ test('should return frozen single element array', () => {
157
+ const result = freezeDedupe(['a'])
158
+ assert.deepStrictEqual([...result], ['a'])
159
+ assert.ok(Object.isFrozen(result))
160
+ })
161
+
162
+ test('should deduplicate array', () => {
163
+ const a = () => {}
164
+ const b = () => {}
165
+ const result = freezeDedupe([a, b, a, b])
166
+ assert.strictEqual(result.length, 2)
167
+ assert.strictEqual(result[0], a)
168
+ assert.strictEqual(result[1], b)
169
+ })
170
+
171
+ test('should freeze the result', () => {
172
+ const result = freezeDedupe([1, 2, 3])
173
+ assert.ok(Object.isFrozen(result))
174
+ })
175
+
176
+ test('should preserve order (first occurrence)', () => {
177
+ const result = freezeDedupe([3, 1, 2, 1, 3])
178
+ assert.deepStrictEqual([...result], [3, 1, 2])
179
+ })
180
+ })
181
+
182
+ describe('closeWireServices', () => {
183
+ test('should call close on services that have it', async () => {
184
+ let closeCalled = false
185
+ const mockLogger = {
186
+ info: () => {},
187
+ warn: () => {},
188
+ error: () => {},
189
+ debug: () => {},
190
+ }
191
+ const wireServices = {
192
+ serviceA: {
193
+ close: async () => {
194
+ closeCalled = true
195
+ },
196
+ },
197
+ serviceB: {},
198
+ }
199
+
200
+ await closeWireServices(mockLogger as any, wireServices as any)
201
+
202
+ assert.strictEqual(closeCalled, true)
203
+ })
204
+
205
+ test('should handle errors during close gracefully', async () => {
206
+ let errorLogged = false
207
+ const mockLogger = {
208
+ info: () => {},
209
+ warn: () => {},
210
+ error: () => {
211
+ errorLogged = true
212
+ },
213
+ debug: () => {},
214
+ }
215
+ const wireServices = {
216
+ serviceA: {
217
+ close: async () => {
218
+ throw new Error('close failed')
219
+ },
220
+ },
221
+ }
222
+
223
+ await closeWireServices(mockLogger as any, wireServices as any)
224
+
225
+ assert.strictEqual(errorLogged, true)
226
+ })
227
+
228
+ test('should skip services without close method', async () => {
229
+ const mockLogger = {
230
+ info: () => {},
231
+ warn: () => {},
232
+ error: () => {},
233
+ debug: () => {},
234
+ }
235
+ const wireServices = {
236
+ serviceA: { doStuff: () => {} },
237
+ serviceB: null,
238
+ }
239
+
240
+ await closeWireServices(mockLogger as any, wireServices as any)
241
+ // Should not throw
242
+ })
243
+ })
244
+
245
+ describe('stopSingletonServices', () => {
246
+ test('should stop addon services first, then parent services', async () => {
247
+ const order: string[] = []
248
+ const mockServices = {
249
+ logger: {
250
+ info: () => {},
251
+ warn: () => {},
252
+ error: () => {},
253
+ debug: () => {},
254
+ },
255
+ serviceA: {
256
+ stop: async () => {
257
+ order.push('parent-serviceA')
258
+ },
259
+ },
260
+ }
261
+ pikkuState(null, 'package', 'singletonServices', mockServices as any)
262
+
263
+ initializePikkuState('@addon/pkg')
264
+ const addonServices = {
265
+ addonService: {
266
+ stop: async () => {
267
+ order.push('addon-addonService')
268
+ },
269
+ },
270
+ }
271
+ pikkuState(
272
+ '@addon/pkg',
273
+ 'package',
274
+ 'singletonServices',
275
+ addonServices as any
276
+ )
277
+
278
+ await stopSingletonServices()
279
+
280
+ assert.ok(
281
+ order.indexOf('addon-addonService') < order.indexOf('parent-serviceA')
282
+ )
283
+ })
284
+
285
+ test('should handle services without stop method', async () => {
286
+ const mockServices = {
287
+ logger: {
288
+ info: () => {},
289
+ warn: () => {},
290
+ error: () => {},
291
+ debug: () => {},
292
+ },
293
+ noStop: {},
294
+ }
295
+ pikkuState(null, 'package', 'singletonServices', mockServices as any)
296
+
297
+ await stopSingletonServices()
298
+ // Should not throw
299
+ })
300
+
301
+ test('should handle errors during stop gracefully', async () => {
302
+ let errorLogged = false
303
+ const mockServices = {
304
+ logger: {
305
+ info: () => {},
306
+ warn: () => {},
307
+ error: () => {
308
+ errorLogged = true
309
+ },
310
+ debug: () => {},
311
+ },
312
+ failing: {
313
+ stop: async () => {
314
+ throw new Error('stop failed')
315
+ },
316
+ },
317
+ }
318
+ pikkuState(null, 'package', 'singletonServices', mockServices as any)
319
+
320
+ await stopSingletonServices()
321
+
322
+ assert.strictEqual(errorLogged, true)
323
+ })
324
+
325
+ test('should clear addon singleton services after stopping', async () => {
326
+ const mockServices = {
327
+ logger: {
328
+ info: () => {},
329
+ warn: () => {},
330
+ error: () => {},
331
+ debug: () => {},
332
+ },
333
+ }
334
+ pikkuState(null, 'package', 'singletonServices', mockServices as any)
335
+
336
+ initializePikkuState('@addon/pkg')
337
+ pikkuState('@addon/pkg', 'package', 'singletonServices', {
338
+ s: { stop: async () => {} },
339
+ } as any)
340
+
341
+ await stopSingletonServices()
342
+
343
+ const addonServices = pikkuState(
344
+ '@addon/pkg',
345
+ 'package',
346
+ 'singletonServices'
347
+ )
348
+ assert.strictEqual(addonServices, null)
349
+ })
350
+ })
package/src/utils.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { Logger } from './services/logger.js'
2
- import { CoreSingletonServices, WireServices } from './types/core.types.js'
1
+ import type { Logger } from './services/logger.js'
2
+ import type { WireServices } from './types/core.types.js'
3
+ import { getSingletonServices, getAllPackageStates } from './pikku-state.js'
3
4
 
4
5
  export const closeWireServices = async (
5
6
  logger: Logger,
@@ -18,9 +19,9 @@ export const closeWireServices = async (
18
19
  )
19
20
  }
20
21
 
21
- export const createWeakUID = () => {
22
- return Date.now().toString(36) + Math.random().toString(36).substring(2, 10)
23
- }
22
+ const _uidPrefix = Date.now().toString(36)
23
+ let _uidCounter = 0
24
+ export const createWeakUID = () => `${_uidPrefix}-${++_uidCounter}`
24
25
 
25
26
  export const isSerializable = (data: any): boolean => {
26
27
  return !(
@@ -93,19 +94,19 @@ const stopService = async (
93
94
  }
94
95
 
95
96
  /**
96
- * Stop all singleton services, including external package services.
97
- * External package services are stopped first, then the parent services.
97
+ * Stop all singleton services, including addon package services.
98
+ * Addon package services are stopped first, then the parent services.
98
99
  *
99
100
  * @param singletonServices - The parent singleton services to stop
100
101
  */
101
- export const stopSingletonServices = async (
102
- singletonServices: CoreSingletonServices
103
- ): Promise<void> => {
102
+ export const stopSingletonServices = async (): Promise<void> => {
103
+ const singletonServices = getSingletonServices()
104
104
  const logger = singletonServices.logger
105
105
 
106
- // First, stop all external package singleton services
107
- if (globalThis.pikkuState) {
108
- for (const [packageName, packageState] of globalThis.pikkuState) {
106
+ // Stop all addon package singleton services
107
+ const stateMap = getAllPackageStates()
108
+ if (stateMap.size > 0) {
109
+ for (const [packageName, packageState] of stateMap) {
109
110
  // Skip main package - we handle it separately
110
111
  if (packageName === '__main__') continue
111
112