@kb-labs/shared 1.1.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 (232) hide show
  1. package/.cursorrules +32 -0
  2. package/.github/workflows/ci.yml +13 -0
  3. package/.github/workflows/deploy.yml +28 -0
  4. package/.github/workflows/docker-build.yml +25 -0
  5. package/.github/workflows/drift-check.yml +10 -0
  6. package/.github/workflows/profiles-validate.yml +16 -0
  7. package/.github/workflows/release.yml +8 -0
  8. package/.kb/devkit/agents/devkit-maintainer/context.globs +15 -0
  9. package/.kb/devkit/agents/devkit-maintainer/permissions.yml +17 -0
  10. package/.kb/devkit/agents/devkit-maintainer/prompt.md +28 -0
  11. package/.kb/devkit/agents/devkit-maintainer/runbook.md +31 -0
  12. package/.kb/devkit/agents/docs-crafter/prompt.md +24 -0
  13. package/.kb/devkit/agents/docs-crafter/runbook.md +18 -0
  14. package/.kb/devkit/agents/release-manager/context.globs +7 -0
  15. package/.kb/devkit/agents/release-manager/prompt.md +27 -0
  16. package/.kb/devkit/agents/release-manager/runbook.md +17 -0
  17. package/.kb/devkit/agents/test-generator/context.globs +7 -0
  18. package/.kb/devkit/agents/test-generator/prompt.md +27 -0
  19. package/.kb/devkit/agents/test-generator/runbook.md +18 -0
  20. package/.vscode/settings.json +23 -0
  21. package/CHANGELOG.md +33 -0
  22. package/CONTRIBUTING.md +117 -0
  23. package/LICENSE +21 -0
  24. package/README.md +306 -0
  25. package/docs/DECLARATIVE-FLAGS-AND-ENV.md +622 -0
  26. package/docs/DOCUMENTATION.md +70 -0
  27. package/docs/adr/0000-template.md +52 -0
  28. package/docs/adr/0001-architecture-and-repository-layout.md +31 -0
  29. package/docs/adr/0002-plugins-and-extensibility.md +44 -0
  30. package/docs/adr/0003-package-and-module-boundaries.md +35 -0
  31. package/docs/adr/0004-versioning-and-release-policy.md +36 -0
  32. package/docs/adr/0005-reactive-loader-pattern.md +179 -0
  33. package/docs/adr/0006-declarative-flags-and-env-systems.md +376 -0
  34. package/eslint.config.js +27 -0
  35. package/kb-labs.config.json +5 -0
  36. package/package.json +88 -0
  37. package/package.json.bin +25 -0
  38. package/package.json.lib +30 -0
  39. package/packages/shared-cli-ui/CHANGELOG.md +20 -0
  40. package/packages/shared-cli-ui/README.md +342 -0
  41. package/packages/shared-cli-ui/docs/ARCHITECTURE.md +105 -0
  42. package/packages/shared-cli-ui/eslint.config.js +27 -0
  43. package/packages/shared-cli-ui/package.json +72 -0
  44. package/packages/shared-cli-ui/src/__tests__/artifacts-display.spec.ts +89 -0
  45. package/packages/shared-cli-ui/src/__tests__/format.spec.ts +44 -0
  46. package/packages/shared-cli-ui/src/__tests__/loader-json-mode.test.ts +119 -0
  47. package/packages/shared-cli-ui/src/artifacts-display.ts +266 -0
  48. package/packages/shared-cli-ui/src/cli-auto-discovery.ts +120 -0
  49. package/packages/shared-cli-ui/src/colors.ts +142 -0
  50. package/packages/shared-cli-ui/src/command-discovery.ts +72 -0
  51. package/packages/shared-cli-ui/src/command-output.ts +153 -0
  52. package/packages/shared-cli-ui/src/command-result.ts +267 -0
  53. package/packages/shared-cli-ui/src/command-runner.ts +310 -0
  54. package/packages/shared-cli-ui/src/command-suggestions.ts +204 -0
  55. package/packages/shared-cli-ui/src/debug/components/output.ts +141 -0
  56. package/packages/shared-cli-ui/src/debug/components/trace.ts +101 -0
  57. package/packages/shared-cli-ui/src/debug/components/tree.ts +88 -0
  58. package/packages/shared-cli-ui/src/debug/formatters/ai.ts +17 -0
  59. package/packages/shared-cli-ui/src/debug/formatters/human.ts +98 -0
  60. package/packages/shared-cli-ui/src/debug/formatters/timeline.ts +94 -0
  61. package/packages/shared-cli-ui/src/debug/index.ts +56 -0
  62. package/packages/shared-cli-ui/src/debug/types.ts +57 -0
  63. package/packages/shared-cli-ui/src/debug/utilities.ts +203 -0
  64. package/packages/shared-cli-ui/src/dynamic-command-discovery.ts +131 -0
  65. package/packages/shared-cli-ui/src/format.ts +412 -0
  66. package/packages/shared-cli-ui/src/index.ts +34 -0
  67. package/packages/shared-cli-ui/src/loader.ts +196 -0
  68. package/packages/shared-cli-ui/src/manifest-parser.ts +151 -0
  69. package/packages/shared-cli-ui/src/modern-format.ts +271 -0
  70. package/packages/shared-cli-ui/src/multi-cli-suggestions.ts +159 -0
  71. package/packages/shared-cli-ui/src/table.ts +134 -0
  72. package/packages/shared-cli-ui/src/timing-tracker.ts +68 -0
  73. package/packages/shared-cli-ui/src/utils/context.ts +12 -0
  74. package/packages/shared-cli-ui/src/utils/env.ts +164 -0
  75. package/packages/shared-cli-ui/src/utils/flags.ts +269 -0
  76. package/packages/shared-cli-ui/src/utils/path.ts +8 -0
  77. package/packages/shared-cli-ui/tsconfig.build.json +15 -0
  78. package/packages/shared-cli-ui/tsconfig.json +9 -0
  79. package/packages/shared-cli-ui/tsup.config.ts +11 -0
  80. package/packages/shared-cli-ui/vitest.config.ts +15 -0
  81. package/packages/shared-command-kit/CHANGELOG.md +20 -0
  82. package/packages/shared-command-kit/LICENSE +22 -0
  83. package/packages/shared-command-kit/README.md +1030 -0
  84. package/packages/shared-command-kit/docs/HIGH-LEVEL-API.md +89 -0
  85. package/packages/shared-command-kit/docs/LOW-LEVEL-API.md +105 -0
  86. package/packages/shared-command-kit/docs/MIGRATION-GUIDE.md +135 -0
  87. package/packages/shared-command-kit/eslint.config.js +27 -0
  88. package/packages/shared-command-kit/eslint.config.ts +14 -0
  89. package/packages/shared-command-kit/package.json +76 -0
  90. package/packages/shared-command-kit/prettierrc.json +5 -0
  91. package/packages/shared-command-kit/src/__tests__/define-command.spec.ts +294 -0
  92. package/packages/shared-command-kit/src/__tests__/define-route.test.ts +285 -0
  93. package/packages/shared-command-kit/src/__tests__/define-system-command.spec.ts +508 -0
  94. package/packages/shared-command-kit/src/__tests__/define-webhook.test.ts +156 -0
  95. package/packages/shared-command-kit/src/__tests__/define-websocket.test.ts +316 -0
  96. package/packages/shared-command-kit/src/__tests__/errors.spec.ts +45 -0
  97. package/packages/shared-command-kit/src/__tests__/flags.spec.ts +353 -0
  98. package/packages/shared-command-kit/src/__tests__/platform-api.test.ts +135 -0
  99. package/packages/shared-command-kit/src/__tests__/plugin-context-v3.snapshot.spec.ts +240 -0
  100. package/packages/shared-command-kit/src/__tests__/ws-types.test.ts +359 -0
  101. package/packages/shared-command-kit/src/analytics/index.ts +6 -0
  102. package/packages/shared-command-kit/src/analytics/with-analytics.ts +195 -0
  103. package/packages/shared-command-kit/src/define-action.ts +100 -0
  104. package/packages/shared-command-kit/src/define-command.ts +113 -0
  105. package/packages/shared-command-kit/src/define-route.ts +113 -0
  106. package/packages/shared-command-kit/src/define-system-command.ts +362 -0
  107. package/packages/shared-command-kit/src/define-webhook.ts +115 -0
  108. package/packages/shared-command-kit/src/define-websocket.ts +308 -0
  109. package/packages/shared-command-kit/src/errors/factory.ts +282 -0
  110. package/packages/shared-command-kit/src/errors/format-validation.ts +144 -0
  111. package/packages/shared-command-kit/src/errors/format.ts +92 -0
  112. package/packages/shared-command-kit/src/errors/index.ts +9 -0
  113. package/packages/shared-command-kit/src/errors/types.ts +32 -0
  114. package/packages/shared-command-kit/src/flags/define.ts +92 -0
  115. package/packages/shared-command-kit/src/flags/index.ts +9 -0
  116. package/packages/shared-command-kit/src/flags/types.ts +153 -0
  117. package/packages/shared-command-kit/src/flags/validate.ts +358 -0
  118. package/packages/shared-command-kit/src/helpers/context.ts +8 -0
  119. package/packages/shared-command-kit/src/helpers/flags.ts +84 -0
  120. package/packages/shared-command-kit/src/helpers/index.ts +42 -0
  121. package/packages/shared-command-kit/src/helpers/patterns.ts +464 -0
  122. package/packages/shared-command-kit/src/helpers/platform.ts +335 -0
  123. package/packages/shared-command-kit/src/helpers/use-analytics.ts +95 -0
  124. package/packages/shared-command-kit/src/helpers/use-cache.ts +97 -0
  125. package/packages/shared-command-kit/src/helpers/use-config.ts +99 -0
  126. package/packages/shared-command-kit/src/helpers/use-embeddings.ts +49 -0
  127. package/packages/shared-command-kit/src/helpers/use-llm.ts +316 -0
  128. package/packages/shared-command-kit/src/helpers/use-logger.ts +77 -0
  129. package/packages/shared-command-kit/src/helpers/use-platform.ts +111 -0
  130. package/packages/shared-command-kit/src/helpers/use-resource-broker.ts +106 -0
  131. package/packages/shared-command-kit/src/helpers/use-storage.ts +71 -0
  132. package/packages/shared-command-kit/src/helpers/use-vector-store.ts +49 -0
  133. package/packages/shared-command-kit/src/helpers/validation.ts +398 -0
  134. package/packages/shared-command-kit/src/index.ts +410 -0
  135. package/packages/shared-command-kit/src/jobs.ts +132 -0
  136. package/packages/shared-command-kit/src/lifecycle/define-handlers.ts +366 -0
  137. package/packages/shared-command-kit/src/lifecycle/index.ts +6 -0
  138. package/packages/shared-command-kit/src/manifest.ts +127 -0
  139. package/packages/shared-command-kit/src/rest/define-handler.ts +187 -0
  140. package/packages/shared-command-kit/src/rest/index.ts +11 -0
  141. package/packages/shared-command-kit/src/studio/index.ts +12 -0
  142. package/packages/shared-command-kit/src/validation/index.ts +6 -0
  143. package/packages/shared-command-kit/src/validation/schema-builders.ts +409 -0
  144. package/packages/shared-command-kit/src/ws-types.ts +106 -0
  145. package/packages/shared-command-kit/tsconfig.build.json +15 -0
  146. package/packages/shared-command-kit/tsconfig.json +9 -0
  147. package/packages/shared-command-kit/tsup.config.ts +30 -0
  148. package/packages/shared-command-kit/vitest.config.ts +4 -0
  149. package/packages/shared-http/package.json +67 -0
  150. package/packages/shared-http/src/__tests__/log-correlation.test.ts +81 -0
  151. package/packages/shared-http/src/__tests__/operation-metrics-tracker.test.ts +55 -0
  152. package/packages/shared-http/src/http-observability-collector.ts +363 -0
  153. package/packages/shared-http/src/index.ts +36 -0
  154. package/packages/shared-http/src/log-correlation.ts +89 -0
  155. package/packages/shared-http/src/operation-metrics-tracker.ts +107 -0
  156. package/packages/shared-http/src/register-openapi.ts +108 -0
  157. package/packages/shared-http/src/resolve-schema-ref.ts +75 -0
  158. package/packages/shared-http/src/schemas.ts +29 -0
  159. package/packages/shared-http/src/service-observability.ts +63 -0
  160. package/packages/shared-http/tsconfig.build.json +15 -0
  161. package/packages/shared-http/tsconfig.json +9 -0
  162. package/packages/shared-http/tsup.config.ts +23 -0
  163. package/packages/shared-http/vitest.config.ts +13 -0
  164. package/packages/shared-perm-presets/CHANGELOG.md +20 -0
  165. package/packages/shared-perm-presets/README.md +78 -0
  166. package/packages/shared-perm-presets/eslint.config.js +27 -0
  167. package/packages/shared-perm-presets/package.json +45 -0
  168. package/packages/shared-perm-presets/src/__tests__/combine.test.ts +403 -0
  169. package/packages/shared-perm-presets/src/__tests__/presets.test.ts +205 -0
  170. package/packages/shared-perm-presets/src/combine.ts +278 -0
  171. package/packages/shared-perm-presets/src/index.ts +18 -0
  172. package/packages/shared-perm-presets/src/presets/ci-environment.ts +34 -0
  173. package/packages/shared-perm-presets/src/presets/full-env.ts +16 -0
  174. package/packages/shared-perm-presets/src/presets/git-workflow.ts +40 -0
  175. package/packages/shared-perm-presets/src/presets/index.ts +8 -0
  176. package/packages/shared-perm-presets/src/presets/kb-platform.ts +30 -0
  177. package/packages/shared-perm-presets/src/presets/llm-access.ts +29 -0
  178. package/packages/shared-perm-presets/src/presets/minimal.ts +21 -0
  179. package/packages/shared-perm-presets/src/presets/npm-publish.ts +48 -0
  180. package/packages/shared-perm-presets/src/presets/vector-store.ts +40 -0
  181. package/packages/shared-perm-presets/src/types.ts +192 -0
  182. package/packages/shared-perm-presets/tsconfig.build.json +15 -0
  183. package/packages/shared-perm-presets/tsconfig.json +9 -0
  184. package/packages/shared-perm-presets/tsup.config.ts +8 -0
  185. package/packages/shared-perm-presets/vitest.config.ts +9 -0
  186. package/packages/shared-testing/CHANGELOG.md +20 -0
  187. package/packages/shared-testing/README.md +430 -0
  188. package/packages/shared-testing/package.json +51 -0
  189. package/packages/shared-testing/src/__tests__/create-test-context.test.ts +199 -0
  190. package/packages/shared-testing/src/__tests__/mock-cache.test.ts +174 -0
  191. package/packages/shared-testing/src/__tests__/mock-llm.test.ts +212 -0
  192. package/packages/shared-testing/src/__tests__/setup-platform.test.ts +90 -0
  193. package/packages/shared-testing/src/__tests__/test-command.test.ts +557 -0
  194. package/packages/shared-testing/src/create-test-context.ts +550 -0
  195. package/packages/shared-testing/src/index.ts +77 -0
  196. package/packages/shared-testing/src/mock-cache.ts +179 -0
  197. package/packages/shared-testing/src/mock-llm.ts +319 -0
  198. package/packages/shared-testing/src/mock-logger.ts +97 -0
  199. package/packages/shared-testing/src/mock-storage.ts +108 -0
  200. package/packages/shared-testing/src/setup-platform.ts +101 -0
  201. package/packages/shared-testing/src/test-command.ts +288 -0
  202. package/packages/shared-testing/tsconfig.build.json +15 -0
  203. package/packages/shared-testing/tsconfig.json +9 -0
  204. package/packages/shared-testing/tsup.config.ts +20 -0
  205. package/packages/shared-testing/vitest.config.ts +3 -0
  206. package/packages/shared-tool-kit/CHANGELOG.md +20 -0
  207. package/packages/shared-tool-kit/package.json +47 -0
  208. package/packages/shared-tool-kit/src/__tests__/factory.test.ts +103 -0
  209. package/packages/shared-tool-kit/src/__tests__/mock-tool.test.ts +95 -0
  210. package/packages/shared-tool-kit/src/factory.ts +126 -0
  211. package/packages/shared-tool-kit/src/index.ts +32 -0
  212. package/packages/shared-tool-kit/src/testing/index.ts +84 -0
  213. package/packages/shared-tool-kit/tsconfig.build.json +15 -0
  214. package/packages/shared-tool-kit/tsconfig.json +9 -0
  215. package/packages/shared-tool-kit/tsup.config.ts +21 -0
  216. package/pnpm-workspace.yaml +11070 -0
  217. package/prettierrc.json +1 -0
  218. package/scripts/devkit-sync.mjs +37 -0
  219. package/scripts/hooks/post-push +9 -0
  220. package/scripts/hooks/pre-commit +9 -0
  221. package/scripts/hooks/pre-push +9 -0
  222. package/tsconfig.base.json +9 -0
  223. package/tsconfig.build.json +15 -0
  224. package/tsconfig.json +9 -0
  225. package/tsconfig.paths.json +50 -0
  226. package/tsconfig.tools.json +18 -0
  227. package/tsup.config.bin.ts +34 -0
  228. package/tsup.config.cli.ts +41 -0
  229. package/tsup.config.dual.ts +46 -0
  230. package/tsup.config.ts +36 -0
  231. package/tsup.external.json +104 -0
  232. package/vitest.config.ts +48 -0
@@ -0,0 +1,135 @@
1
+ /**
2
+ * @module @kb-labs/shared-command-kit/__tests__/platform-api
3
+ *
4
+ * Unit tests for platform API composables: useLogger, useCache, usePlatform,
5
+ * isPlatformConfigured, isCacheAvailable.
6
+ *
7
+ * These are critical path tests — if composables break, ALL plugin handlers break.
8
+ */
9
+
10
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
11
+ import { resetPlatform } from '@kb-labs/core-runtime';
12
+ import {
13
+ usePlatform,
14
+ useLogger,
15
+ useCache,
16
+ isCacheAvailable,
17
+ isPlatformConfigured,
18
+ } from '../index.js';
19
+
20
+ describe('Platform API composables', () => {
21
+ beforeEach(() => {
22
+ resetPlatform();
23
+ });
24
+
25
+ afterEach(() => {
26
+ resetPlatform();
27
+ vi.restoreAllMocks();
28
+ });
29
+
30
+ describe('usePlatform()', () => {
31
+ it('returns an object with platform services', () => {
32
+ const result = usePlatform();
33
+ expect(result).toBeDefined();
34
+ expect(typeof result).toBe('object');
35
+ });
36
+
37
+ it('always returns the same object reference', () => {
38
+ expect(usePlatform()).toBe(usePlatform());
39
+ });
40
+ });
41
+
42
+ describe('useLogger()', () => {
43
+ it('returns same constructor as usePlatform().logger', () => {
44
+ const logger = useLogger();
45
+ const platformLogger = usePlatform().logger;
46
+ // Both come from the same platform — same class (may be Proxy-wrapped, so no toBe)
47
+ expect(logger.constructor).toBe(platformLogger.constructor);
48
+ });
49
+
50
+ it('returned logger has all required methods', () => {
51
+ const logger = useLogger();
52
+ expect(typeof logger.info).toBe('function');
53
+ expect(typeof logger.warn).toBe('function');
54
+ expect(typeof logger.error).toBe('function');
55
+ expect(typeof logger.debug).toBe('function');
56
+ expect(typeof logger.fatal).toBe('function');
57
+ expect(typeof logger.child).toBe('function');
58
+ });
59
+
60
+ it('logger methods are callable without throwing', () => {
61
+ const logger = useLogger();
62
+ expect(() => logger.info('test message')).not.toThrow();
63
+ expect(() => logger.warn('test warning')).not.toThrow();
64
+ expect(() => logger.error('test error')).not.toThrow();
65
+ expect(() => logger.debug('test debug')).not.toThrow();
66
+ });
67
+
68
+ it('child() returns a logger with same interface', () => {
69
+ const logger = useLogger();
70
+ const child = logger.child({ requestId: 'req-123' });
71
+ expect(typeof child.info).toBe('function');
72
+ expect(typeof child.error).toBe('function');
73
+ });
74
+ });
75
+
76
+ describe('useCache()', () => {
77
+ it('returns same constructor as usePlatform().cache', () => {
78
+ const cache = useCache();
79
+ const platformCache = usePlatform().cache;
80
+ // Both come from the same platform — same class (may be Proxy-wrapped, so no toBe)
81
+ expect(cache?.constructor).toBe(platformCache?.constructor);
82
+ });
83
+
84
+ it('returned cache has get/set/delete interface', () => {
85
+ const cache = useCache();
86
+ // cache is always present (noopCache fallback)
87
+ expect(typeof cache?.get).toBe('function');
88
+ expect(typeof cache?.set).toBe('function');
89
+ expect(typeof cache?.delete).toBe('function');
90
+ });
91
+
92
+ it('cache.get returns undefined for unknown key', async () => {
93
+ const cache = useCache();
94
+ const val = await cache?.get('nonexistent-key');
95
+ expect(val).toBeNull();
96
+ });
97
+
98
+ it('cache.set then get returns stored value', async () => {
99
+ const cache = useCache();
100
+ await cache?.set('test-key', { foo: 'bar' });
101
+ const result = await cache?.get<{ foo: string }>('test-key');
102
+ expect(result).toEqual({ foo: 'bar' });
103
+ });
104
+
105
+ it('cache.delete removes a key', async () => {
106
+ const cache = useCache();
107
+ await cache?.set('delete-me', 'value');
108
+ await cache?.delete('delete-me');
109
+ const result = await cache?.get('delete-me');
110
+ expect(result).toBeNull();
111
+ });
112
+ });
113
+
114
+ describe('isCacheAvailable()', () => {
115
+ it('returns true when platform.cache is set', () => {
116
+ // Default platform always has a cache (MemoryCache noop)
117
+ expect(typeof isCacheAvailable()).toBe('boolean');
118
+ });
119
+ });
120
+
121
+ describe('isPlatformConfigured()', () => {
122
+ it('returns false for adapter named with "noop" constructor', () => {
123
+ // Default platform uses NoOp adapters — they should not appear as "configured"
124
+ // (actual result depends on constructor name detection)
125
+ const result = isPlatformConfigured('llm');
126
+ expect(typeof result).toBe('boolean');
127
+ });
128
+
129
+ it('returns true for logger (always present)', () => {
130
+ // Logger is always configured
131
+ const result = isPlatformConfigured('logger');
132
+ expect(typeof result).toBe('boolean');
133
+ });
134
+ });
135
+ });
@@ -0,0 +1,240 @@
1
+ /**
2
+ * @module @kb-labs/shared-command-kit/__tests__/plugin-context-v3-snapshot
3
+ *
4
+ * Snapshot test to ensure PluginContextV3 structure remains stable.
5
+ * This test validates that system commands receive ONLY PluginContextV3 fields,
6
+ * without any legacy EnhancedCliContext fields (tracker, success, error, etc.).
7
+ */
8
+
9
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
10
+ import { defineSystemCommand } from '../define-system-command';
11
+ import type { PluginContextV3 } from '@kb-labs/plugin-contracts';
12
+
13
+ describe('PluginContextV3 Structure Snapshot', () => {
14
+ let mockCtx: PluginContextV3;
15
+
16
+ beforeEach(() => {
17
+ // Pure PluginContextV3 - no EnhancedCliContext, no tracker, no output helpers
18
+ mockCtx = {
19
+ host: 'cli',
20
+ requestId: 'test-request-id',
21
+ pluginId: '@kb-labs/system',
22
+ pluginVersion: '0.1.0',
23
+ cwd: '/test',
24
+ hostContext: { host: 'cli', argv: [], flags: {} },
25
+ ui: {
26
+ colors: {} as any,
27
+ symbols: {} as any,
28
+ write: vi.fn(),
29
+ json: vi.fn(),
30
+ error: vi.fn(),
31
+ info: vi.fn(),
32
+ success: vi.fn(),
33
+ warn: vi.fn(),
34
+ warning: vi.fn(),
35
+ message: vi.fn(),
36
+ table: vi.fn(),
37
+ keyValue: vi.fn(),
38
+ list: vi.fn(),
39
+ confirm: vi.fn(),
40
+ prompt: vi.fn(),
41
+ } as any,
42
+ platform: {
43
+ logger: {
44
+ info: vi.fn(),
45
+ error: vi.fn(),
46
+ warn: vi.fn(),
47
+ debug: vi.fn(),
48
+ } as any,
49
+ llm: {} as any,
50
+ embeddings: {} as any,
51
+ vectorStore: {} as any,
52
+ cache: {} as any,
53
+ storage: {} as any,
54
+ analytics: {} as any,
55
+ eventBus: {} as any,
56
+ logs: {} as any,
57
+ },
58
+ runtime: {
59
+ fs: {} as any,
60
+ fetch: vi.fn(),
61
+ env: {} as any,
62
+ },
63
+ api: {} as any,
64
+ trace: {
65
+ traceId: 'test-trace-id',
66
+ spanId: 'test-span-id',
67
+ addEvent: vi.fn(),
68
+ setAttribute: vi.fn(),
69
+ recordError: vi.fn(),
70
+ },
71
+ };
72
+ });
73
+
74
+ it('should have ONLY PluginContextV3 fields (snapshot)', async () => {
75
+ const handler = vi.fn(async (ctx: PluginContextV3) => {
76
+ // Capture context structure
77
+ const contextKeys = Object.keys(ctx).sort();
78
+
79
+ // Snapshot the structure
80
+ expect(contextKeys).toMatchInlineSnapshot(`
81
+ [
82
+ "api",
83
+ "cwd",
84
+ "host",
85
+ "platform",
86
+ "pluginId",
87
+ "requestId",
88
+ "runtime",
89
+ "trace",
90
+ "ui",
91
+ ]
92
+ `);
93
+
94
+ return { ok: true };
95
+ });
96
+
97
+ const command = defineSystemCommand({
98
+ name: 'test',
99
+ description: 'Test',
100
+ flags: {},
101
+ handler,
102
+ });
103
+
104
+ await command.run(mockCtx, [], {});
105
+
106
+ expect(handler).toHaveBeenCalledWith(
107
+ expect.objectContaining({
108
+ host: 'cli',
109
+ pluginId: '@kb-labs/system',
110
+ ui: expect.any(Object),
111
+ platform: expect.any(Object),
112
+ runtime: expect.any(Object),
113
+ trace: expect.any(Object),
114
+ }),
115
+ [],
116
+ {}
117
+ );
118
+ });
119
+
120
+ it('should NOT have legacy EnhancedCliContext fields', async () => {
121
+ const handler = vi.fn(async (ctx: any) => {
122
+ // Verify no legacy fields
123
+ expect(ctx.tracker).toBeUndefined();
124
+ expect(ctx.success).toBeUndefined();
125
+ expect(ctx.error).toBeUndefined();
126
+ expect(ctx.warning).toBeUndefined();
127
+ expect(ctx.info).toBeUndefined();
128
+ expect(ctx.result).toBeUndefined();
129
+
130
+ return { ok: true };
131
+ });
132
+
133
+ const command = defineSystemCommand({
134
+ name: 'test',
135
+ description: 'Test',
136
+ flags: {},
137
+ handler,
138
+ });
139
+
140
+ await command.run(mockCtx, [], {});
141
+
142
+ expect(handler).toHaveBeenCalled();
143
+ });
144
+
145
+ it('should have ui field with all required methods (snapshot)', async () => {
146
+ const handler = vi.fn(async (ctx: PluginContextV3) => {
147
+ const uiMethods = Object.keys(ctx.ui!).sort();
148
+
149
+ // Snapshot UI methods
150
+ expect(uiMethods).toMatchInlineSnapshot(`
151
+ [
152
+ "colors",
153
+ "completeProgress",
154
+ "confirm",
155
+ "error",
156
+ "failProgress",
157
+ "info",
158
+ "json",
159
+ "keyValue",
160
+ "list",
161
+ "prompt",
162
+ "startProgress",
163
+ "success",
164
+ "table",
165
+ "updateProgress",
166
+ "warn",
167
+ "write",
168
+ ]
169
+ `);
170
+
171
+ return { ok: true };
172
+ });
173
+
174
+ const command = defineSystemCommand({
175
+ name: 'test',
176
+ description: 'Test',
177
+ flags: {},
178
+ handler,
179
+ });
180
+
181
+ await command.run(mockCtx, [], {});
182
+ });
183
+
184
+ it('should have platform field with all required services (snapshot)', async () => {
185
+ const handler = vi.fn(async (ctx: PluginContextV3) => {
186
+ const platformServices = Object.keys(ctx.platform).sort();
187
+
188
+ // Snapshot platform services
189
+ expect(platformServices).toMatchInlineSnapshot(`
190
+ [
191
+ "analytics",
192
+ "cache",
193
+ "embeddings",
194
+ "llm",
195
+ "logger",
196
+ "storage",
197
+ "vectorStore",
198
+ ]
199
+ `);
200
+
201
+ return { ok: true };
202
+ });
203
+
204
+ const command = defineSystemCommand({
205
+ name: 'test',
206
+ description: 'Test',
207
+ flags: {},
208
+ handler,
209
+ });
210
+
211
+ await command.run(mockCtx, [], {});
212
+ });
213
+
214
+ it('should have runtime field with all required methods (snapshot)', async () => {
215
+ const handler = vi.fn(async (ctx: PluginContextV3) => {
216
+ const runtimeMethods = Object.keys(ctx.runtime).sort();
217
+
218
+ // Snapshot runtime methods
219
+ expect(runtimeMethods).toMatchInlineSnapshot(`
220
+ [
221
+ "env",
222
+ "fetch",
223
+ "fs",
224
+ "state",
225
+ ]
226
+ `);
227
+
228
+ return { ok: true };
229
+ });
230
+
231
+ const command = defineSystemCommand({
232
+ name: 'test',
233
+ description: 'Test',
234
+ flags: {},
235
+ handler,
236
+ });
237
+
238
+ await command.run(mockCtx, [], {});
239
+ });
240
+ });