@elevasis/core 0.21.0 → 0.23.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 (132) hide show
  1. package/dist/index.d.ts +2518 -2169
  2. package/dist/index.js +2495 -1095
  3. package/dist/knowledge/index.d.ts +706 -1044
  4. package/dist/knowledge/index.js +9 -9
  5. package/dist/organization-model/index.d.ts +2518 -2169
  6. package/dist/organization-model/index.js +2495 -1095
  7. package/dist/test-utils/index.d.ts +826 -1014
  8. package/dist/test-utils/index.js +1894 -1032
  9. package/package.json +3 -3
  10. package/src/__tests__/template-core-compatibility.test.ts +11 -79
  11. package/src/_gen/__tests__/__snapshots__/contracts.md.snap +852 -397
  12. package/src/auth/multi-tenancy/permissions.ts +20 -8
  13. package/src/business/README.md +2 -2
  14. package/src/business/acquisition/api-schemas.test.ts +175 -2
  15. package/src/business/acquisition/api-schemas.ts +132 -16
  16. package/src/business/acquisition/build-templates.test.ts +4 -4
  17. package/src/business/acquisition/build-templates.ts +72 -30
  18. package/src/business/acquisition/crm-state-actions.test.ts +13 -11
  19. package/src/business/acquisition/index.ts +12 -0
  20. package/src/business/acquisition/types.ts +7 -3
  21. package/src/business/clients/api-schemas.test.ts +115 -0
  22. package/src/business/clients/api-schemas.ts +158 -0
  23. package/src/business/clients/index.ts +1 -0
  24. package/src/business/deals/api-schemas.ts +8 -0
  25. package/src/business/index.ts +5 -2
  26. package/src/business/projects/types.ts +19 -0
  27. package/src/execution/engine/__tests__/fixtures/test-agents.ts +10 -8
  28. package/src/execution/engine/agent/core/__tests__/agent.test.ts +16 -12
  29. package/src/execution/engine/agent/core/__tests__/error-passthrough.test.ts +4 -3
  30. package/src/execution/engine/agent/core/types.ts +25 -15
  31. package/src/execution/engine/agent/index.ts +6 -4
  32. package/src/execution/engine/agent/reasoning/__tests__/request-builder.test.ts +24 -18
  33. package/src/execution/engine/index.ts +3 -0
  34. package/src/execution/engine/workflow/types.ts +9 -2
  35. package/src/knowledge/README.md +8 -7
  36. package/src/knowledge/__tests__/queries.test.ts +74 -73
  37. package/src/knowledge/format.ts +10 -9
  38. package/src/knowledge/index.ts +1 -1
  39. package/src/knowledge/published.ts +1 -1
  40. package/src/knowledge/queries.ts +26 -25
  41. package/src/organization-model/README.md +73 -26
  42. package/src/organization-model/__tests__/content-kinds-registry.test.ts +210 -0
  43. package/src/organization-model/__tests__/defaults.test.ts +76 -96
  44. package/src/organization-model/__tests__/domains/actions.test.ts +56 -0
  45. package/src/organization-model/__tests__/domains/customers.test.ts +299 -295
  46. package/src/organization-model/__tests__/domains/entities.test.ts +56 -0
  47. package/src/organization-model/__tests__/domains/goals.test.ts +493 -479
  48. package/src/organization-model/__tests__/domains/identity.test.ts +280 -279
  49. package/src/organization-model/__tests__/domains/navigation.test.ts +268 -212
  50. package/src/organization-model/__tests__/domains/offerings.test.ts +414 -419
  51. package/src/organization-model/__tests__/domains/policies.test.ts +323 -0
  52. package/src/organization-model/__tests__/domains/resource-mappings.test.ts +271 -271
  53. package/src/organization-model/__tests__/domains/resources.test.ts +310 -0
  54. package/src/organization-model/__tests__/domains/roles.test.ts +463 -347
  55. package/src/organization-model/__tests__/domains/statuses.test.ts +246 -243
  56. package/src/organization-model/__tests__/domains/systems.test.ts +209 -0
  57. package/src/organization-model/__tests__/flatten-additive-merge.test.ts +361 -0
  58. package/src/organization-model/__tests__/foundation.test.ts +74 -102
  59. package/src/organization-model/__tests__/get-resources-for-system.test.ts +144 -0
  60. package/src/organization-model/__tests__/graph.test.ts +899 -71
  61. package/src/organization-model/__tests__/knowledge.test.ts +209 -49
  62. package/src/organization-model/__tests__/lookup-helpers.test.ts +438 -0
  63. package/src/organization-model/__tests__/migration-helpers.test.ts +591 -0
  64. package/src/organization-model/__tests__/prospecting-ssot.test.ts +36 -27
  65. package/src/organization-model/__tests__/recursive-system-schema.test.ts +520 -0
  66. package/src/organization-model/__tests__/resolve.test.ts +174 -23
  67. package/src/organization-model/__tests__/schema.test.ts +291 -114
  68. package/src/organization-model/__tests__/surface-projection.test.ts +207 -97
  69. package/src/organization-model/catalogs/lead-gen.ts +144 -0
  70. package/src/organization-model/content-kinds/config.ts +36 -0
  71. package/src/organization-model/content-kinds/index.ts +74 -0
  72. package/src/organization-model/content-kinds/pipeline.ts +68 -0
  73. package/src/organization-model/content-kinds/registry.ts +44 -0
  74. package/src/organization-model/content-kinds/status.ts +71 -0
  75. package/src/organization-model/content-kinds/template.ts +83 -0
  76. package/src/organization-model/content-kinds/types.ts +117 -0
  77. package/src/organization-model/contracts.ts +13 -3
  78. package/src/organization-model/defaults.ts +499 -86
  79. package/src/organization-model/domains/actions.ts +239 -0
  80. package/src/organization-model/domains/customers.ts +78 -75
  81. package/src/organization-model/domains/entities.ts +144 -0
  82. package/src/organization-model/domains/goals.ts +83 -80
  83. package/src/organization-model/domains/knowledge.ts +76 -17
  84. package/src/organization-model/domains/navigation.ts +107 -384
  85. package/src/organization-model/domains/offerings.ts +71 -66
  86. package/src/organization-model/domains/policies.ts +102 -0
  87. package/src/organization-model/domains/projects.ts +14 -48
  88. package/src/organization-model/domains/prospecting.ts +62 -181
  89. package/src/organization-model/domains/resources.ts +145 -0
  90. package/src/organization-model/domains/roles.ts +96 -55
  91. package/src/organization-model/domains/sales.ts +10 -219
  92. package/src/organization-model/domains/shared.ts +57 -57
  93. package/src/organization-model/domains/statuses.ts +339 -130
  94. package/src/organization-model/domains/systems.ts +203 -0
  95. package/src/organization-model/foundation.ts +54 -67
  96. package/src/organization-model/graph/build.ts +682 -54
  97. package/src/organization-model/graph/link.ts +1 -1
  98. package/src/organization-model/graph/schema.ts +24 -9
  99. package/src/organization-model/graph/types.ts +20 -7
  100. package/src/organization-model/helpers.ts +231 -26
  101. package/src/organization-model/icons.ts +1 -0
  102. package/src/organization-model/index.ts +118 -5
  103. package/src/organization-model/migration-helpers.ts +249 -0
  104. package/src/organization-model/organization-graph.mdx +16 -15
  105. package/src/organization-model/organization-model.mdx +111 -44
  106. package/src/organization-model/published.ts +172 -19
  107. package/src/organization-model/resolve.ts +117 -54
  108. package/src/organization-model/schema.ts +654 -112
  109. package/src/organization-model/surface-projection.ts +116 -122
  110. package/src/organization-model/types.ts +146 -20
  111. package/src/platform/api/types.ts +38 -35
  112. package/src/platform/constants/versions.ts +1 -1
  113. package/src/platform/registry/__tests__/command-view.test.ts +6 -8
  114. package/src/platform/registry/__tests__/resource-link.test.ts +13 -8
  115. package/src/platform/registry/__tests__/resource-registry.integration.test.ts +16 -31
  116. package/src/platform/registry/__tests__/resource-registry.nested-systems.test.ts +245 -0
  117. package/src/platform/registry/__tests__/resource-registry.test.ts +2053 -2005
  118. package/src/platform/registry/__tests__/validation.test.ts +1347 -1086
  119. package/src/platform/registry/index.ts +14 -0
  120. package/src/platform/registry/resource-registry.ts +52 -2
  121. package/src/platform/registry/serialization.ts +241 -202
  122. package/src/platform/registry/serialized-types.ts +1 -0
  123. package/src/platform/registry/types.ts +411 -361
  124. package/src/platform/registry/validation.ts +745 -513
  125. package/src/projects/api-schemas.ts +290 -267
  126. package/src/reference/_generated/contracts.md +853 -397
  127. package/src/reference/glossary.md +23 -18
  128. package/src/supabase/database.types.ts +181 -0
  129. package/src/test-utils/test-utils.test.ts +1 -6
  130. package/src/organization-model/__tests__/domains/operations.test.ts +0 -203
  131. package/src/organization-model/domains/features.ts +0 -31
  132. package/src/organization-model/domains/operations.ts +0 -85
@@ -0,0 +1,209 @@
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import {
3
+ DEFAULT_ORGANIZATION_MODEL_SYSTEMS,
4
+ SystemEntrySchema,
5
+ SystemKindSchema,
6
+ SystemStatusSchema,
7
+ SystemsDomainSchema
8
+ } from '../../domains/systems'
9
+ import { resolveOrganizationModel } from '../../resolve'
10
+
11
+ const VALID_SYSTEM = {
12
+ id: 'sys.lead-gen',
13
+ order: 10,
14
+ title: 'Lead Generation Pipeline',
15
+ description: 'Coordinates prospecting, enrichment, qualification, and outreach preparation.',
16
+ kind: 'operational' as const
17
+ }
18
+
19
+ describe('SystemEntrySchema - positive parse', () => {
20
+ it('accepts a minimal tenant-defined system', () => {
21
+ const result = SystemEntrySchema.safeParse(VALID_SYSTEM)
22
+
23
+ expect(result.success).toBe(true)
24
+ if (result.success) {
25
+ expect(result.data.governedByKnowledge).toEqual([])
26
+ expect(result.data.drivesGoals).toEqual([])
27
+ }
28
+ })
29
+
30
+ it('accepts all optional governance references when present', () => {
31
+ const result = SystemEntrySchema.safeParse({
32
+ ...VALID_SYSTEM,
33
+ responsibleRoleId: 'role.sales-ops',
34
+ governedByKnowledge: ['knowledge.lead-gen-playbook'],
35
+ drivesGoals: ['goal.pipeline-coverage']
36
+ })
37
+
38
+ expect(result.success).toBe(true)
39
+ })
40
+
41
+ it('trims id, title, and description', () => {
42
+ const result = SystemEntrySchema.safeParse({
43
+ ...VALID_SYSTEM,
44
+ id: ' sys.crm ',
45
+ title: ' CRM ',
46
+ description: ' Owns relationship and deal pipeline operations. '
47
+ })
48
+
49
+ expect(result.success).toBe(true)
50
+ if (result.success) {
51
+ expect(result.data.id).toBe('sys.crm')
52
+ expect(result.data.title).toBe('CRM')
53
+ expect(result.data.description).toBe('Owns relationship and deal pipeline operations.')
54
+ }
55
+ })
56
+
57
+ it('accepts deprecated status and maps it to lifecycle for one-cycle compatibility', () => {
58
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
59
+
60
+ const result = SystemEntrySchema.safeParse({
61
+ ...VALID_SYSTEM,
62
+ status: 'deprecated'
63
+ })
64
+
65
+ expect(result.success).toBe(true)
66
+ if (result.success) {
67
+ expect(result.data.status).toBe('deprecated')
68
+ expect(result.data.lifecycle).toBe('deprecated')
69
+ }
70
+ expect(warn).toHaveBeenCalledWith('[organization-model] System.status is deprecated; use System.lifecycle instead.')
71
+
72
+ warn.mockRestore()
73
+ })
74
+ })
75
+
76
+ describe('SystemEntrySchema - negative parse', () => {
77
+ it('rejects missing required fields', () => {
78
+ expect(SystemEntrySchema.safeParse({ id: 'sys.missing' }).success).toBe(false)
79
+ })
80
+
81
+ it('rejects an unknown system kind', () => {
82
+ expect(SystemEntrySchema.safeParse({ ...VALID_SYSTEM, kind: 'sales' }).success).toBe(false)
83
+ })
84
+
85
+ it('rejects an unknown deprecated system status', () => {
86
+ expect(SystemEntrySchema.safeParse({ ...VALID_SYSTEM, status: 'paused' }).success).toBe(false)
87
+ })
88
+
89
+ it('rejects IDs that do not match the OM model-id format', () => {
90
+ expect(SystemEntrySchema.safeParse({ ...VALID_SYSTEM, id: 'Sys Lead Gen' }).success).toBe(false)
91
+ })
92
+ })
93
+
94
+ describe('SystemsDomainSchema', () => {
95
+ it('accepts an empty systems record', () => {
96
+ expect(SystemsDomainSchema.safeParse({}).success).toBe(true)
97
+ })
98
+
99
+ it('defaults systems to an empty record when omitted', () => {
100
+ const result = SystemsDomainSchema.safeParse({})
101
+
102
+ expect(result.success).toBe(true)
103
+ if (result.success) {
104
+ expect(result.data).toEqual(DEFAULT_ORGANIZATION_MODEL_SYSTEMS)
105
+ }
106
+ })
107
+ })
108
+
109
+ describe('SystemKindSchema', () => {
110
+ it.each(['product', 'operational', 'platform', 'diagnostic'] as const)('accepts kind "%s"', (kind) => {
111
+ expect(SystemKindSchema.safeParse(kind).success).toBe(true)
112
+ })
113
+ })
114
+
115
+ describe('SystemStatusSchema', () => {
116
+ it.each(['active', 'deprecated', 'archived'] as const)('accepts deprecated status "%s"', (status) => {
117
+ expect(SystemStatusSchema.safeParse(status).success).toBe(true)
118
+ })
119
+ })
120
+
121
+ describe('resolveOrganizationModel - systems domain integration', () => {
122
+ it('omitting systems keeps default system entries', () => {
123
+ const model = resolveOrganizationModel({})
124
+
125
+ expect(Object.keys(model.systems).length).toBeGreaterThan(Object.keys(DEFAULT_ORGANIZATION_MODEL_SYSTEMS).length)
126
+ expect(model.systems['sales.crm']?.path).toBe('/crm')
127
+ })
128
+
129
+ it('accepts valid responsible role, knowledge, and goal references', () => {
130
+ expect(() =>
131
+ resolveOrganizationModel({
132
+ roles: {
133
+ 'role.sales-ops': { id: 'role.sales-ops', order: 10, title: 'Sales Ops' }
134
+ },
135
+ goals: {
136
+ 'goal.pipeline-coverage': {
137
+ id: 'goal.pipeline-coverage',
138
+ order: 10,
139
+ description: 'Improve sales pipeline coverage',
140
+ periodStart: '2026-01-01',
141
+ periodEnd: '2026-12-31'
142
+ }
143
+ },
144
+ knowledge: {
145
+ 'knowledge.lead-gen-playbook': {
146
+ id: 'knowledge.lead-gen-playbook',
147
+ kind: 'playbook',
148
+ title: 'Lead Gen Playbook',
149
+ summary: 'How lead generation is governed.',
150
+ body: '## Lead Gen',
151
+ updatedAt: '2026-05-08'
152
+ }
153
+ },
154
+ systems: {
155
+ 'sys.lead-gen': {
156
+ ...VALID_SYSTEM,
157
+ responsibleRoleId: 'role.sales-ops',
158
+ governedByKnowledge: ['knowledge.lead-gen-playbook'],
159
+ drivesGoals: ['goal.pipeline-coverage']
160
+ }
161
+ }
162
+ })
163
+ ).not.toThrow()
164
+ })
165
+
166
+ it('throws when system IDs are duplicated', () => {
167
+ expect(() =>
168
+ resolveOrganizationModel({
169
+ systems: {
170
+ 'sys.lead-gen': VALID_SYSTEM,
171
+ 'sys.lead-gen-dup': {
172
+ ...VALID_SYSTEM,
173
+ id: 'sys.lead-gen'
174
+ }
175
+ }
176
+ })
177
+ ).toThrow()
178
+ })
179
+
180
+ it('throws when responsibleRoleId references an unknown role', () => {
181
+ expect(() =>
182
+ resolveOrganizationModel({
183
+ systems: {
184
+ 'sys.lead-gen': { ...VALID_SYSTEM, responsibleRoleId: 'role.missing' }
185
+ }
186
+ })
187
+ ).toThrow(/unknown responsibleRoleId \\"role\.missing\\"/)
188
+ })
189
+
190
+ it('throws when governedByKnowledge references an unknown knowledge node', () => {
191
+ expect(() =>
192
+ resolveOrganizationModel({
193
+ systems: {
194
+ 'sys.lead-gen': { ...VALID_SYSTEM, governedByKnowledge: ['knowledge.missing'] }
195
+ }
196
+ })
197
+ ).toThrow(/unknown knowledge node \\"knowledge\.missing\\"/)
198
+ })
199
+
200
+ it('throws when drivesGoals references an unknown goal', () => {
201
+ expect(() =>
202
+ resolveOrganizationModel({
203
+ systems: {
204
+ 'sys.lead-gen': { ...VALID_SYSTEM, drivesGoals: ['goal.missing'] }
205
+ }
206
+ })
207
+ ).toThrow(/unknown goal \\"goal\.missing\\"/)
208
+ })
209
+ })
@@ -0,0 +1,361 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { DEFAULT_ORGANIZATION_MODEL } from '../defaults'
3
+ import { listDomain } from '../helpers'
4
+ import { resolveOrganizationModel } from '../resolve'
5
+ import type { DeepPartial, OrganizationModel } from '../types'
6
+
7
+ // ---------------------------------------------------------------------------
8
+ // Step 5 of om-flatten-single-array-domains: lock in additive-by-id merge
9
+ // across all 9 flattened domains.
10
+ //
11
+ // Before the flatten, every domain was `{ field: Entry[] }` and deepMerge's
12
+ // array branch (`Array.isArray(base) -> return override`) caused tenant
13
+ // overrides to wholesale-replace the array. The systems domain needed a
14
+ // special-case mergeSystemsById helper (sidebar-regression-fix.mdx OD1) to
15
+ // produce additive behavior.
16
+ //
17
+ // After the flatten, every domain is `Record<string, Entry>` keyed by id, so
18
+ // deepMerge's object branch (`isPlainObject(base) && isPlainObject(override)
19
+ // -> deepMerge per key`) gives additive-by-id semantics for free.
20
+ //
21
+ // These tests pin that semantic in place. If anyone re-introduces array shape
22
+ // on these domains, or special-cases merge logic, these tests will surface it.
23
+ // ---------------------------------------------------------------------------
24
+
25
+ describe('flatten: deepMerge produces additive-by-id behavior across all 9 flattened domains', () => {
26
+ // Phase 4 (D1): 'statuses' top-level field removed from OrganizationModel.
27
+ // Status data moved into system.content as (schema:status-flow) / (schema:status) nodes.
28
+ // The remaining 8 domains are all Record<string, Entry> after the flatten.
29
+ const FLATTENED_DOMAINS = [
30
+ 'systems',
31
+ 'roles',
32
+ 'goals',
33
+ 'customers',
34
+ 'actions',
35
+ 'offerings',
36
+ 'resources',
37
+ 'policies'
38
+ ] as const
39
+
40
+ it.each(FLATTENED_DOMAINS)('%s domain shape is Record<string, Entry>, not an array wrapper', (domain) => {
41
+ const value = DEFAULT_ORGANIZATION_MODEL[domain]
42
+ expect(Array.isArray(value)).toBe(false)
43
+ expect(typeof value).toBe('object')
44
+ expect(value).not.toBeNull()
45
+ })
46
+
47
+ it('adds a tenant system to the merged record without dropping defaults', () => {
48
+ const baseSize = Object.keys(DEFAULT_ORGANIZATION_MODEL.systems).length
49
+
50
+ const model = resolveOrganizationModel({
51
+ systems: {
52
+ 'tenant.workspace': {
53
+ id: 'tenant.workspace',
54
+ order: 10000,
55
+ label: 'Tenant Workspace',
56
+ lifecycle: 'active',
57
+ path: '/tenant'
58
+ }
59
+ }
60
+ })
61
+
62
+ expect(Object.keys(model.systems)).toHaveLength(baseSize + 1)
63
+ expect(model.systems['tenant.workspace']?.label).toBe('Tenant Workspace')
64
+ // Default systems survive
65
+ expect(model.systems['dashboard']).toBeDefined()
66
+ expect(model.systems['operations']).toBeDefined()
67
+ })
68
+
69
+ it('merges a tenant role additively without clobbering default roles', () => {
70
+ const baseSize = Object.keys(DEFAULT_ORGANIZATION_MODEL.roles).length
71
+
72
+ const model = resolveOrganizationModel({
73
+ roles: {
74
+ 'role.tenant-lead': {
75
+ id: 'role.tenant-lead',
76
+ order: 10000,
77
+ title: 'Tenant Lead'
78
+ }
79
+ }
80
+ })
81
+
82
+ expect(Object.keys(model.roles)).toHaveLength(baseSize + 1)
83
+ expect(model.roles['role.tenant-lead']?.title).toBe('Tenant Lead')
84
+ })
85
+
86
+ it('merges a tenant action additively while preserving the default 13 lead-gen actions', () => {
87
+ const baseSize = Object.keys(DEFAULT_ORGANIZATION_MODEL.actions).length
88
+ expect(baseSize).toBe(13)
89
+
90
+ const model = resolveOrganizationModel({
91
+ actions: {
92
+ 'tenant.custom-action': {
93
+ id: 'tenant.custom-action',
94
+ order: 10000,
95
+ label: 'Custom Action',
96
+ scope: 'global',
97
+ resourceId: 'tenant-workflow'
98
+ }
99
+ }
100
+ })
101
+
102
+ expect(Object.keys(model.actions)).toHaveLength(baseSize + 1)
103
+ expect(model.actions['tenant.custom-action']?.label).toBe('Custom Action')
104
+ expect(model.actions['lead-gen.company.source']).toBeDefined()
105
+ })
106
+
107
+ it('overrides an existing system by id (last write wins on the same map key)', () => {
108
+ const model = resolveOrganizationModel({
109
+ systems: {
110
+ dashboard: {
111
+ id: 'dashboard',
112
+ order: 10,
113
+ label: 'Tenant Dashboard',
114
+ lifecycle: 'active',
115
+ path: '/'
116
+ }
117
+ }
118
+ })
119
+
120
+ expect(model.systems['dashboard']?.label).toBe('Tenant Dashboard')
121
+ // Other defaults remain
122
+ expect(model.systems['operations']).toBeDefined()
123
+ })
124
+
125
+ it('merges nested fields within a single entry deeply (not replacing the whole entry)', () => {
126
+ // Overriding only `label` on a default system preserves its other fields.
127
+ const defaultDashboard = DEFAULT_ORGANIZATION_MODEL.systems['dashboard']
128
+ expect(defaultDashboard).toBeDefined()
129
+
130
+ const model = resolveOrganizationModel({
131
+ systems: {
132
+ dashboard: {
133
+ id: 'dashboard',
134
+ order: defaultDashboard!.order,
135
+ label: 'Renamed'
136
+ }
137
+ }
138
+ })
139
+
140
+ const merged = model.systems['dashboard']
141
+ expect(merged?.label).toBe('Renamed')
142
+ // Path from defaults survives because object merge is deep per-key.
143
+ expect(merged?.path).toBe(defaultDashboard!.path)
144
+ })
145
+
146
+ it('confirms no special-case mergeSystemsById helper exists in resolve.ts', async () => {
147
+ // The OD1 special case from sidebar-regression-fix.mdx is superseded by
148
+ // this flatten. If anyone reintroduces it under any of these names,
149
+ // surface that drift.
150
+ const source = await import('node:fs/promises').then((fs) =>
151
+ fs.readFile(new URL('../resolve.ts', import.meta.url), 'utf-8')
152
+ )
153
+
154
+ expect(source).not.toMatch(/mergeSystemsById/)
155
+ expect(source).not.toMatch(/mergeById/)
156
+ expect(source).not.toMatch(/mergeRecordById/)
157
+ })
158
+
159
+ it('multiple flattened domains receive independent additive merges in one call', () => {
160
+ const baseSystems = Object.keys(DEFAULT_ORGANIZATION_MODEL.systems).length
161
+ const baseRoles = Object.keys(DEFAULT_ORGANIZATION_MODEL.roles).length
162
+ const baseActions = Object.keys(DEFAULT_ORGANIZATION_MODEL.actions).length
163
+
164
+ const model = resolveOrganizationModel({
165
+ systems: {
166
+ 'tenant.a': { id: 'tenant.a', order: 10000, label: 'A', lifecycle: 'active', path: '/a' }
167
+ },
168
+ roles: {
169
+ 'role.a': { id: 'role.a', order: 10000, title: 'A' }
170
+ },
171
+ actions: {
172
+ 'tenant.a': {
173
+ id: 'tenant.a',
174
+ order: 10000,
175
+ label: 'A Action',
176
+ scope: 'global',
177
+ resourceId: 'a-workflow'
178
+ }
179
+ }
180
+ })
181
+
182
+ expect(Object.keys(model.systems)).toHaveLength(baseSystems + 1)
183
+ expect(Object.keys(model.roles)).toHaveLength(baseRoles + 1)
184
+ expect(Object.keys(model.actions)).toHaveLength(baseActions + 1)
185
+ })
186
+
187
+ // ---------------------------------------------------------------------------
188
+ // Override merge edge cases
189
+ // ---------------------------------------------------------------------------
190
+
191
+ it('order override on existing entry takes precedence and re-sorts the listed domain', () => {
192
+ const override: DeepPartial<OrganizationModel> = {
193
+ systems: {
194
+ dashboard: {
195
+ id: 'dashboard',
196
+ order: 1000,
197
+ label: 'Dashboard',
198
+ lifecycle: 'active',
199
+ path: '/'
200
+ }
201
+ }
202
+ }
203
+
204
+ const resolved = resolveOrganizationModel(override)
205
+
206
+ expect(resolved.systems['dashboard']!.order).toBe(1000)
207
+
208
+ const listed = listDomain(resolved.systems)
209
+ const dashboardIndex = listed.findIndex((s) => s.id === 'dashboard')
210
+ const systemsBefore = listed.filter((s) => s.order < 1000)
211
+ expect(dashboardIndex).toBeGreaterThan(systemsBefore.length - 1)
212
+ // All entries with order < 1000 must appear before dashboard in the sorted list
213
+ systemsBefore.forEach((s) => {
214
+ expect(listed.indexOf(s)).toBeLessThan(dashboardIndex)
215
+ })
216
+ })
217
+
218
+ it('override entry omitting order preserves the base order value', () => {
219
+ const baseOrder = DEFAULT_ORGANIZATION_MODEL.systems['dashboard']!.order
220
+
221
+ const override: DeepPartial<OrganizationModel> = {
222
+ systems: {
223
+ dashboard: {
224
+ id: 'dashboard',
225
+ label: 'Renamed Dashboard'
226
+ }
227
+ }
228
+ }
229
+
230
+ const resolved = resolveOrganizationModel(override)
231
+
232
+ expect(resolved.systems['dashboard']!.order).toBe(baseOrder)
233
+ expect(resolved.systems['dashboard']!.label).toBe('Renamed Dashboard')
234
+ })
235
+
236
+ it('throws when system map key does not match the entry id', () => {
237
+ const badOverride: DeepPartial<OrganizationModel> = {
238
+ systems: {
239
+ 'wrong-key': {
240
+ id: 'different-id',
241
+ order: 10,
242
+ label: 'Mismatch',
243
+ lifecycle: 'active',
244
+ path: '/mismatch'
245
+ }
246
+ }
247
+ }
248
+
249
+ expect(() => resolveOrganizationModel(badOverride)).toThrow(/map key/)
250
+ })
251
+
252
+ it('replaces nested arrays rather than merging them', () => {
253
+ // sales.lead-gen has a top-level `actions` array field on the entry.
254
+ // deepMerge hits Array.isArray(existing) => returns override value directly.
255
+ const singleRef = [{ actionId: 'lead-gen.company.source', intent: 'exposes' as const }]
256
+
257
+ const override: DeepPartial<OrganizationModel> = {
258
+ systems: {
259
+ 'sales.lead-gen': {
260
+ id: 'sales.lead-gen',
261
+ order: DEFAULT_ORGANIZATION_MODEL.systems['sales.lead-gen']!.order,
262
+ label: 'Lead Gen',
263
+ actions: singleRef
264
+ }
265
+ }
266
+ }
267
+
268
+ const resolved = resolveOrganizationModel(override)
269
+ const entry = resolved.systems['sales.lead-gen']!
270
+
271
+ expect(entry.actions).toHaveLength(1)
272
+ expect(entry.actions).toEqual(singleRef)
273
+ })
274
+
275
+ it('empty systems override produces same systems domain as no override', () => {
276
+ const withEmpty = resolveOrganizationModel({ systems: {} })
277
+ const withNone = resolveOrganizationModel()
278
+
279
+ expect(withEmpty.systems).toEqual(withNone.systems)
280
+ })
281
+
282
+ it('field set to undefined in override is skipped and retains the base value', () => {
283
+ const basePath = DEFAULT_ORGANIZATION_MODEL.systems['dashboard']!.path
284
+
285
+ const override: DeepPartial<OrganizationModel> = {
286
+ systems: {
287
+ dashboard: {
288
+ id: 'dashboard',
289
+ order: DEFAULT_ORGANIZATION_MODEL.systems['dashboard']!.order,
290
+ label: 'Dashboard',
291
+ path: undefined
292
+ }
293
+ }
294
+ }
295
+
296
+ const resolved = resolveOrganizationModel(override)
297
+
298
+ expect(resolved.systems['dashboard']!.path).toBe(basePath)
299
+ })
300
+
301
+ it('simultaneous override of systems, roles, and actions merges each domain independently without cross-contamination', () => {
302
+ const baseSystems = Object.keys(DEFAULT_ORGANIZATION_MODEL.systems).length
303
+ const baseRoles = Object.keys(DEFAULT_ORGANIZATION_MODEL.roles).length
304
+ const baseActions = Object.keys(DEFAULT_ORGANIZATION_MODEL.actions).length
305
+ const baseCustomers = DEFAULT_ORGANIZATION_MODEL.customers
306
+ const baseGoals = DEFAULT_ORGANIZATION_MODEL.goals
307
+
308
+ const model = resolveOrganizationModel({
309
+ systems: {
310
+ 'multi.sys': { id: 'multi.sys', order: 50000, label: 'Multi Sys', lifecycle: 'active', path: '/multi-sys' }
311
+ },
312
+ roles: {
313
+ 'multi.role': { id: 'multi.role', order: 50000, title: 'Multi Role' }
314
+ },
315
+ actions: {
316
+ 'multi.action': {
317
+ id: 'multi.action',
318
+ order: 50000,
319
+ label: 'Multi Action',
320
+ scope: 'global',
321
+ resourceId: 'multi-workflow'
322
+ }
323
+ }
324
+ })
325
+
326
+ expect(Object.keys(model.systems)).toHaveLength(baseSystems + 1)
327
+ expect(model.systems['multi.sys']).toBeDefined()
328
+
329
+ expect(Object.keys(model.roles)).toHaveLength(baseRoles + 1)
330
+ expect(model.roles['multi.role']).toBeDefined()
331
+
332
+ expect(Object.keys(model.actions)).toHaveLength(baseActions + 1)
333
+ expect(model.actions['multi.action']).toBeDefined()
334
+
335
+ expect(model.customers).toEqual(baseCustomers)
336
+ expect(model.goals).toEqual(baseGoals)
337
+ })
338
+
339
+ it('override sets top-level array field on entry to single-element array replacing the original', () => {
340
+ // The `invocations` field on an action entry is a top-level array.
341
+ // deepMerge sees Array.isArray(existing) and returns override directly.
342
+ const replacementInvocations = [{ kind: 'slash-command' as const, command: '/custom-source' }]
343
+
344
+ const override: DeepPartial<OrganizationModel> = {
345
+ actions: {
346
+ 'lead-gen.company.source': {
347
+ id: 'lead-gen.company.source',
348
+ order: DEFAULT_ORGANIZATION_MODEL.actions['lead-gen.company.source']!.order,
349
+ label: 'Source companies',
350
+ invocations: replacementInvocations
351
+ }
352
+ }
353
+ }
354
+
355
+ const resolved = resolveOrganizationModel(override)
356
+ const entry = resolved.actions['lead-gen.company.source']!
357
+
358
+ expect(entry.invocations).toHaveLength(1)
359
+ expect(entry.invocations).toEqual(replacementInvocations)
360
+ })
361
+ })