@apexops/capabilities 0.10.0-next.5 → 0.11.0-next.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 (92) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/archetype-source.d.ts +49 -0
  3. package/dist/archetype-source.d.ts.map +1 -0
  4. package/dist/archetype-source.js +421 -0
  5. package/dist/archetype-source.js.map +1 -0
  6. package/dist/bound-envelope-transport.js +1 -1
  7. package/dist/bound-envelope-transport.js.map +1 -1
  8. package/dist/executable-resolution.d.ts +21 -0
  9. package/dist/executable-resolution.d.ts.map +1 -0
  10. package/dist/executable-resolution.js +87 -0
  11. package/dist/executable-resolution.js.map +1 -0
  12. package/dist/governance-baseline.d.ts +87 -0
  13. package/dist/governance-baseline.d.ts.map +1 -0
  14. package/dist/governance-baseline.js +639 -0
  15. package/dist/governance-baseline.js.map +1 -0
  16. package/dist/governance-setup.d.ts +13 -0
  17. package/dist/governance-setup.d.ts.map +1 -0
  18. package/dist/governance-setup.js +231 -0
  19. package/dist/governance-setup.js.map +1 -0
  20. package/dist/iac-generation.d.ts.map +1 -1
  21. package/dist/iac-generation.js +56 -20
  22. package/dist/iac-generation.js.map +1 -1
  23. package/dist/iac-normalizers.d.ts.map +1 -1
  24. package/dist/iac-normalizers.js +19 -1
  25. package/dist/iac-normalizers.js.map +1 -1
  26. package/dist/iac.d.ts +30 -2
  27. package/dist/iac.d.ts.map +1 -1
  28. package/dist/iac.js.map +1 -1
  29. package/dist/index.d.ts +6 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +6 -0
  32. package/dist/index.js.map +1 -1
  33. package/dist/native-iac-providers.d.ts +20 -4
  34. package/dist/native-iac-providers.d.ts.map +1 -1
  35. package/dist/native-iac-providers.js +591 -27
  36. package/dist/native-iac-providers.js.map +1 -1
  37. package/dist/pack-manager.d.ts.map +1 -1
  38. package/dist/pack-manager.js +15 -14
  39. package/dist/pack-manager.js.map +1 -1
  40. package/dist/policy-validation.d.ts +68 -0
  41. package/dist/policy-validation.d.ts.map +1 -0
  42. package/dist/policy-validation.js +653 -0
  43. package/dist/policy-validation.js.map +1 -0
  44. package/dist/process-runner.d.ts.map +1 -1
  45. package/dist/process-runner.js +10 -1
  46. package/dist/process-runner.js.map +1 -1
  47. package/dist/rename-retry.d.ts +12 -0
  48. package/dist/rename-retry.d.ts.map +1 -0
  49. package/dist/rename-retry.js +27 -0
  50. package/dist/rename-retry.js.map +1 -0
  51. package/dist/repository-publish.d.ts +17 -0
  52. package/dist/repository-publish.d.ts.map +1 -0
  53. package/dist/repository-publish.js +124 -0
  54. package/dist/repository-publish.js.map +1 -0
  55. package/dist/terraform-provider-schema.d.ts.map +1 -1
  56. package/dist/terraform-provider-schema.js +6 -2
  57. package/dist/terraform-provider-schema.js.map +1 -1
  58. package/dist/test/archetype-source.test.d.ts +2 -0
  59. package/dist/test/archetype-source.test.d.ts.map +1 -0
  60. package/dist/test/archetype-source.test.js +258 -0
  61. package/dist/test/archetype-source.test.js.map +1 -0
  62. package/dist/test/executable-resolution.test.d.ts +2 -0
  63. package/dist/test/executable-resolution.test.d.ts.map +1 -0
  64. package/dist/test/executable-resolution.test.js +87 -0
  65. package/dist/test/executable-resolution.test.js.map +1 -0
  66. package/dist/test/governance-baseline.test.d.ts +2 -0
  67. package/dist/test/governance-baseline.test.d.ts.map +1 -0
  68. package/dist/test/governance-baseline.test.js +841 -0
  69. package/dist/test/governance-baseline.test.js.map +1 -0
  70. package/dist/test/governance-setup.test.d.ts +2 -0
  71. package/dist/test/governance-setup.test.d.ts.map +1 -0
  72. package/dist/test/governance-setup.test.js +160 -0
  73. package/dist/test/governance-setup.test.js.map +1 -0
  74. package/dist/test/iac-generation.test.js +203 -14
  75. package/dist/test/iac-generation.test.js.map +1 -1
  76. package/dist/test/native-iac-providers.test.js +1482 -5
  77. package/dist/test/native-iac-providers.test.js.map +1 -1
  78. package/dist/test/policy-validation.test.d.ts +2 -0
  79. package/dist/test/policy-validation.test.d.ts.map +1 -0
  80. package/dist/test/policy-validation.test.js +1061 -0
  81. package/dist/test/policy-validation.test.js.map +1 -0
  82. package/dist/test/rename-retry.test.d.ts +2 -0
  83. package/dist/test/rename-retry.test.d.ts.map +1 -0
  84. package/dist/test/rename-retry.test.js +25 -0
  85. package/dist/test/rename-retry.test.js.map +1 -0
  86. package/dist/test/repository-publish.test.d.ts +2 -0
  87. package/dist/test/repository-publish.test.d.ts.map +1 -0
  88. package/dist/test/repository-publish.test.js +123 -0
  89. package/dist/test/repository-publish.test.js.map +1 -0
  90. package/dist/test/terraform-provider-schema.test.js +12 -6
  91. package/dist/test/terraform-provider-schema.test.js.map +1 -1
  92. package/package.json +3 -3
@@ -0,0 +1,841 @@
1
+ import assert from "node:assert/strict";
2
+ import { readFileSync } from "node:fs";
3
+ import { test } from "node:test";
4
+ import { Ajv2020 } from "ajv/dist/2020.js";
5
+ import { GovernanceConstraintsV1Schema, PolicyPropertyMapV1Schema } from "@apexops/contracts";
6
+ import { GovernanceBaselineError, importGovernanceBaseline, importGovernanceReference, inspectGovernanceBaseline, } from "../index.js";
7
+ const validate = new Ajv2020({ strict: false }).compile(JSON.parse(readFileSync(new URL("../../../../tools/schemas/governance-baseline.schema.json", import.meta.url), "utf8")));
8
+ const subscriptionId = "11111111-1111-1111-1111-111111111111";
9
+ const otherId = "22222222-2222-2222-2222-222222222222";
10
+ const discoveredAt = "2026-09-16T00:00:00Z";
11
+ const options = { subscriptionId, now: "2026-09-16T01:00:00Z" };
12
+ const managementScope = "/providers/Microsoft.Management/managementGroups/root";
13
+ function envelope(id = subscriptionId) {
14
+ return {
15
+ schema_version: "governance-constraints-v1",
16
+ subscription_id: id,
17
+ discovered_at: discoveredAt,
18
+ source: "github-actions-baseline",
19
+ discovery_status: "COMPLETE",
20
+ discovery_metadata: {
21
+ discovery_status: "COMPLETE",
22
+ discovered_at: discoveredAt,
23
+ scope: { subscription_id: id, management_groups: [] },
24
+ api_versions: {
25
+ policyAssignments: "2022-06-01",
26
+ policyDefinitions: "2021-06-01",
27
+ policyExemptions: "2022-07-01-preview",
28
+ },
29
+ page_counts: { policyAssignments: 0, policyDefinitions: 0, policyExemptions: 0 },
30
+ completeness_signature: "",
31
+ ttl_days: 7,
32
+ },
33
+ discovery_summary: {
34
+ assignment_total: 0,
35
+ assignment_kept: 0,
36
+ defender_auto_filtered: 0,
37
+ subscription_scope_count: 0,
38
+ management_group_inherited_count: 0,
39
+ blocker_count: 0,
40
+ auto_remediate_count: 0,
41
+ informational_count: 0,
42
+ audit_count: 0,
43
+ disabled_count: 0,
44
+ exempted_count: 0,
45
+ },
46
+ assignment_inventory: [],
47
+ findings: [],
48
+ tags_required: [],
49
+ allowed_locations: [],
50
+ };
51
+ }
52
+ function baseline() {
53
+ return {
54
+ schema_version: "governance-baseline-v2",
55
+ management_group_id: "root",
56
+ coverage_status: "COMPLETE",
57
+ subscriptions_discovered: 1,
58
+ subscriptions_processed: 1,
59
+ subscriptions_skipped: [],
60
+ subscriptions_excluded: [],
61
+ summary: { total_findings: 0, total_blockers: 0, total_auto_remediate: 0, subscriptions_complete: 1 },
62
+ subscriptions: { [subscriptionId]: envelope() },
63
+ };
64
+ }
65
+ function rejected(input, code, override = options) {
66
+ assert.throws(() => importGovernanceBaseline(input, override, validate), (error) => error instanceof GovernanceBaselineError && error.code === code);
67
+ }
68
+ test("selects only the active subscription and produces runtime-compatible compact constraints", () => {
69
+ const source = baseline();
70
+ source.subscriptions[otherId] = envelope(otherId);
71
+ source.subscriptions_discovered = source.subscriptions_processed = source.summary.subscriptions_complete = 2;
72
+ const selected = importGovernanceBaseline(source, options, validate);
73
+ assert.equal(JSON.stringify(selected).includes(otherId), false);
74
+ assert.deepEqual(selected.snapshot.findings, []);
75
+ assert.equal(selected.snapshot.reconciliationRequired, true);
76
+ assert.equal(selected.snapshot.provenance.signatureStatus, "unverified");
77
+ assert.equal(selected.constraints.expiresAt, "2026-10-16T00:00:00.000Z");
78
+ const validateConstraints = new Ajv2020({ strict: false, validateFormats: false }).compile(GovernanceConstraintsV1Schema);
79
+ assert.ok(validateConstraints({
80
+ ...selected.constraints,
81
+ source: "collected",
82
+ projectId: "example",
83
+ runId: "run-1",
84
+ constraintsRef: {
85
+ mediaType: "application/json",
86
+ uri: "evidence/selected.json",
87
+ digest: "a".repeat(64),
88
+ bytes: 1,
89
+ },
90
+ }));
91
+ assert.deepEqual(importGovernanceBaseline(Buffer.from(JSON.stringify(source)), options, validate), selected);
92
+ assert.deepEqual(importGovernanceBaseline(JSON.stringify(source), options, validate), selected);
93
+ });
94
+ test("preserves legacy management-group provenance and accepts an exclusive standalone root", () => {
95
+ const { management_group_id, ...source } = baseline();
96
+ assert.equal(management_group_id, "root");
97
+ const standalone = { ...source, subscription_id: subscriptionId };
98
+ assert.deepEqual(importGovernanceBaseline(standalone, options, validate).snapshot.provenance.root, {
99
+ kind: "subscription",
100
+ subscriptionId,
101
+ });
102
+ rejected({ ...standalone, management_group_id: "root" }, "invalid-input");
103
+ rejected(source, "invalid-input");
104
+ rejected({ ...standalone, subscription_id: otherId }, "target-mismatch");
105
+ });
106
+ test("rejects incomplete empty evidence, errors and inconsistent counts", () => {
107
+ const mutations = [
108
+ (source) => {
109
+ source.coverage_status = "PARTIAL";
110
+ },
111
+ (source) => {
112
+ source.subscriptions_skipped.push(otherId);
113
+ },
114
+ (source) => {
115
+ source.subscriptions[subscriptionId].discovery_status = "PARTIAL";
116
+ },
117
+ (source) => {
118
+ source.subscriptions[subscriptionId].discovery_metadata.discovery_status = "FAILED";
119
+ },
120
+ (source) => {
121
+ source.subscriptions_processed = 2;
122
+ },
123
+ (source) => {
124
+ source.subscriptions_discovered = 2;
125
+ },
126
+ (source) => {
127
+ source.summary.subscriptions_complete = 0;
128
+ },
129
+ (source) => {
130
+ source.summary.total_findings = 1;
131
+ },
132
+ (source) => {
133
+ source.subscriptions[subscriptionId].discovery_summary.assignment_kept = 1;
134
+ },
135
+ (source) => {
136
+ Object.assign(source.subscriptions[subscriptionId], { error: { message: "secret" } });
137
+ },
138
+ (source) => {
139
+ source.subscriptions_excluded.push({ subscription_id: otherId, reason: "processing_error" });
140
+ },
141
+ ];
142
+ for (const mutate of mutations) {
143
+ const source = baseline();
144
+ mutate(source);
145
+ rejected(source, "incomplete");
146
+ }
147
+ const source = baseline();
148
+ delete source.subscriptions[subscriptionId].discovery_metadata;
149
+ rejected(source, "invalid-input");
150
+ });
151
+ test("inspection admits old metadata only while preserving all non-age validation", () => {
152
+ const old = { ...options, now: new Date(Date.parse(discoveredAt) + 30 * 86_400_000).toISOString() };
153
+ assert.deepEqual(inspectGovernanceBaseline(baseline(), old, validate), {
154
+ observedAt: new Date(discoveredAt).toISOString(),
155
+ refreshRequired: true,
156
+ });
157
+ assert.deepEqual(inspectGovernanceBaseline(baseline(), options, validate), {
158
+ observedAt: new Date(discoveredAt).toISOString(),
159
+ refreshRequired: false,
160
+ });
161
+ assert.throws(() => importGovernanceBaseline(baseline(), old, validate), /stale/);
162
+ assert.throws(() => inspectGovernanceBaseline(baseline(), { ...options, now: "2026-09-15T00:00:00Z" }, validate), /stale/);
163
+ assert.throws(() => inspectGovernanceBaseline(baseline(), { ...old, subscriptionId: otherId }, validate), /target-mismatch/);
164
+ const partial = baseline();
165
+ partial.coverage_status = "PARTIAL";
166
+ assert.throws(() => inspectGovernanceBaseline(partial, old, validate), /incomplete|invalid-input/);
167
+ assert.throws(() => inspectGovernanceBaseline("{", old, validate), /invalid-input/);
168
+ });
169
+ test("allows reuse below 30 days and requires refresh at exactly 30 days independent of legacy TTL", () => {
170
+ for (const ttl of [1, 7, 30, 90]) {
171
+ const source = baseline();
172
+ source.subscriptions[subscriptionId].discovery_metadata.ttl_days = ttl;
173
+ const selected = importGovernanceBaseline(source, { ...options, now: "2026-10-15T23:59:59.999Z" }, validate);
174
+ assert.equal(selected.constraints.expiresAt, "2026-10-16T00:00:00.000Z");
175
+ rejected(source, "stale", { ...options, now: "2026-10-16T00:00:00Z" });
176
+ rejected(source, "stale", { ...options, now: "2026-10-17T00:00:00Z" });
177
+ }
178
+ });
179
+ test("rejects future and invalid timestamps using explicit time", () => {
180
+ rejected(baseline(), "stale", { ...options, now: "2026-09-15T23:59:59Z" });
181
+ rejected(baseline(), "invalid-options", { ...options, now: "2026-02-30T00:00:00Z" });
182
+ const source = baseline();
183
+ source.subscriptions[subscriptionId].discovery_metadata.discovered_at = "2026-09-16T00:01:00Z";
184
+ rejected(source, "incomplete");
185
+ });
186
+ test("successful unchanged observation renews age without changing selected policy content", () => {
187
+ const previous = baseline();
188
+ const refreshed = baseline();
189
+ refreshed.subscriptions[subscriptionId].discovered_at = "2026-09-17T00:00:00Z";
190
+ refreshed.subscriptions[subscriptionId].discovery_metadata.discovered_at = "2026-09-17T00:00:00Z";
191
+ const at = { ...options, now: "2026-09-17T01:00:00Z" };
192
+ const before = importGovernanceBaseline(previous, at, validate);
193
+ const after = importGovernanceBaseline(refreshed, at, validate);
194
+ assert.deepEqual(before.snapshot, after.snapshot);
195
+ assert.notEqual(before.constraints.discoveredAt, after.constraints.discoveredAt);
196
+ assert.equal(Date.parse(after.constraints.expiresAt) - Date.parse(before.constraints.expiresAt), 86_400_000);
197
+ });
198
+ test("rejects absent, mismatched and malformed other subscriptions", () => {
199
+ rejected(baseline(), "target-mismatch", { ...options, subscriptionId: otherId });
200
+ const source = baseline();
201
+ source.subscriptions[subscriptionId].discovery_metadata.scope.subscription_id = otherId;
202
+ rejected(source, "target-mismatch");
203
+ source.subscriptions[subscriptionId] = envelope();
204
+ source.subscriptions[otherId] = envelope(otherId);
205
+ source.subscriptions_discovered = source.subscriptions_processed = source.summary.subscriptions_complete = 2;
206
+ source.subscriptions[otherId].findings.push({ arbitrary: "malformed" });
207
+ rejected(source, "invalid-input");
208
+ });
209
+ test("rejects duplicate IDs and stale or incomplete nonselected subscriptions", () => {
210
+ const duplicate = baseline();
211
+ duplicate.subscriptions_excluded.push({ subscription_id: subscriptionId, reason: "disabled" });
212
+ rejected(duplicate, "target-mismatch");
213
+ const source = baseline();
214
+ source.subscriptions[otherId] = envelope(otherId);
215
+ source.subscriptions_discovered = source.subscriptions_processed = source.summary.subscriptions_complete = 2;
216
+ source.subscriptions[otherId].discovery_status = "PARTIAL";
217
+ rejected(source, "incomplete");
218
+ source.subscriptions[otherId].discovery_status = "COMPLETE";
219
+ source.subscriptions[otherId].discovered_at = "2026-08-01T00:00:00Z";
220
+ source.subscriptions[otherId].discovery_metadata.discovered_at = "2026-08-01T00:00:00Z";
221
+ rejected(source, "stale");
222
+ });
223
+ function inheritedBaseline() {
224
+ const source = baseline();
225
+ const entry = source.subscriptions[subscriptionId];
226
+ entry.discovery_metadata.scope.management_groups = ["root"];
227
+ entry.discovery_metadata.page_counts = { policyAssignments: 1, policyDefinitions: 2, policyExemptions: 1 };
228
+ entry.assignment_inventory = [
229
+ {
230
+ displayName: "Inherited initiative",
231
+ scope: managementScope,
232
+ assignmentType: "management-group",
233
+ policyDefinitionId: "initiative",
234
+ },
235
+ ];
236
+ entry.findings = ["policy-b", "policy-a"].map((policyId) => ({
237
+ policy_id: policyId,
238
+ display_name: policyId,
239
+ effect: "deny",
240
+ scope: managementScope,
241
+ assignment_id: `${managementScope}/providers/Microsoft.Authorization/policyAssignments/inherited`,
242
+ classification: "informational",
243
+ resource_types: ["Microsoft.Storage/storageAccounts"],
244
+ exemption: { category: "Waiver", policyDefinitionReferenceIds: ["member-b", "member-a"] },
245
+ }));
246
+ Object.assign(entry.discovery_summary, {
247
+ assignment_total: 1,
248
+ assignment_kept: 1,
249
+ management_group_inherited_count: 1,
250
+ informational_count: 2,
251
+ exempted_count: 2,
252
+ });
253
+ source.summary.total_findings = 2;
254
+ return source;
255
+ }
256
+ test("enforcementMode preserves supported modes without weakening desired compliance", () => {
257
+ for (const mode of ["Default", "DoNotEnforce"]) {
258
+ const source = inheritedBaseline();
259
+ const entry = source.subscriptions[subscriptionId];
260
+ entry.assignment_inventory[0].enforcementMode = mode;
261
+ for (const item of entry.findings)
262
+ Object.assign(item, { enforcementMode: mode, classification: "blocker", exemption: null });
263
+ Object.assign(entry.discovery_summary, { blocker_count: 2, informational_count: 0, exempted_count: 0 });
264
+ source.summary.total_blockers = 2;
265
+ const selected = importGovernanceBaseline(source, options, validate);
266
+ assert.equal(selected.constraints.summary.denyCount, 2);
267
+ for (const item of selected.snapshot.findings) {
268
+ assert.deepEqual(item, {
269
+ ...item,
270
+ enforcementMode: mode,
271
+ effect: "deny",
272
+ classification: "blocker",
273
+ exemption: null,
274
+ });
275
+ }
276
+ assert.deepEqual(importGovernanceBaseline(JSON.stringify(source), options, validate), selected);
277
+ }
278
+ });
279
+ test("enforcementMode leaves legacy absence unknown and does not guess inventory identities", () => {
280
+ const source = inheritedBaseline();
281
+ const entry = source.subscriptions[subscriptionId];
282
+ entry.assignment_inventory[0].enforcementMode = "Default";
283
+ entry.findings[0].enforcementMode = "DoNotEnforce";
284
+ const findings = importGovernanceBaseline(source, options, validate).snapshot.findings;
285
+ assert.equal(Object.hasOwn(findings.find((item) => item.policyId === "policy-a"), "enforcementMode"), false);
286
+ const explicit = findings.find((item) => item.policyId === "policy-b");
287
+ assert.deepEqual(explicit, { ...explicit, enforcementMode: "DoNotEnforce" });
288
+ assert.ok(importGovernanceBaseline(inheritedBaseline(), options, validate).snapshot.findings.every((item) => !Object.hasOwn(item, "enforcementMode")));
289
+ });
290
+ test("enforcementMode rejects unsupported and malformed explicit values in inventory and findings", () => {
291
+ for (const field of ["assignment_inventory", "findings"]) {
292
+ for (const mode of [
293
+ "Enroll",
294
+ "unknown",
295
+ "default",
296
+ "donotenforce",
297
+ "Default ",
298
+ "",
299
+ null,
300
+ false,
301
+ 0,
302
+ [],
303
+ ["Default"],
304
+ {},
305
+ ]) {
306
+ const source = inheritedBaseline();
307
+ const entry = source.subscriptions[subscriptionId];
308
+ entry[field][0].enforcementMode = mode;
309
+ rejected(source, "invalid-input");
310
+ }
311
+ }
312
+ });
313
+ test("enforcementMode rejects conflicting explicit modes for the same exact assignment ID", () => {
314
+ const source = inheritedBaseline();
315
+ const entry = source.subscriptions[subscriptionId];
316
+ entry.findings[0].enforcementMode = "Default";
317
+ entry.findings[1].enforcementMode = "DoNotEnforce";
318
+ rejected(source, "incomplete");
319
+ entry.findings[1].assignment_id = String(entry.findings[1].assignment_id).toUpperCase();
320
+ rejected(source, "incomplete");
321
+ entry.findings[1].assignment_id = `${managementScope}/providers/Microsoft.Authorization/policyAssignments/different`;
322
+ entry.assignment_inventory.push({ ...entry.assignment_inventory[0] });
323
+ entry.discovery_metadata.page_counts.policyAssignments = 2;
324
+ Object.assign(entry.discovery_summary, {
325
+ assignment_total: 2,
326
+ assignment_kept: 2,
327
+ management_group_inherited_count: 2,
328
+ });
329
+ assert.equal(importGovernanceBaseline(source, options, validate).snapshot.findings.length, 2);
330
+ });
331
+ test("preserves inherited findings and reported exemptions deterministically without granting mappings", () => {
332
+ const source = inheritedBaseline();
333
+ const selected = importGovernanceBaseline(source, options, validate);
334
+ assert.deepEqual(selected.snapshot.findings.map((item) => item.policyId), ["policy-a", "policy-b"]);
335
+ assert.equal(selected.constraints.summary.assignmentCount, 1);
336
+ assert.equal(selected.constraints.summary.exemptionCount, 1);
337
+ assert.equal(selected.constraints.summary.denyCount, 2);
338
+ assert.ok(selected.snapshot.findings.every((item) => item.mappingStatus === "unmapped" &&
339
+ item.scope === managementScope &&
340
+ item.exemption?.verificationStatus === "unverified"));
341
+ const entry = source.subscriptions[subscriptionId];
342
+ entry.findings.reverse();
343
+ const reordered = importGovernanceBaseline(source, options, validate);
344
+ assert.deepEqual(reordered.snapshot.findings, selected.snapshot.findings);
345
+ assert.notEqual(reordered.snapshot.contentHash, selected.snapshot.contentHash);
346
+ assert.deepEqual(source.subscriptions[subscriptionId].findings[0].exemption
347
+ .policyDefinitionReferenceIds, ["member-b", "member-a"]);
348
+ });
349
+ function descendantsBaseline(assignmentName = "policy") {
350
+ const source = baseline();
351
+ const entry = source.subscriptions[subscriptionId];
352
+ Object.assign(entry.discovery_metadata.scope, {
353
+ coverage: "subscription-and-descendants-v1",
354
+ management_groups: ["root"],
355
+ });
356
+ const targetScope = `/subscriptions/${subscriptionId}/resourceGroups/target`;
357
+ const scopes = [
358
+ managementScope,
359
+ `/subscriptions/${subscriptionId}`,
360
+ targetScope,
361
+ `${targetScope}/providers/Microsoft.Storage/storageAccounts/child`,
362
+ `/subscriptions/${subscriptionId}/resourceGroups/target-other`,
363
+ ];
364
+ entry.findings = scopes.map((scope, index) => ({
365
+ policy_id: `policy-${index}`,
366
+ display_name: `Policy ${index}`,
367
+ effect: "deny",
368
+ scope,
369
+ assignment_id: `${scope}/providers/Microsoft.Authorization/policyAssignments/${assignmentName}`,
370
+ classification: "blocker",
371
+ resource_types: ["Microsoft.Storage/storageAccounts"],
372
+ exemption: null,
373
+ reported_exemptions: [],
374
+ }));
375
+ entry.assignment_inventory = scopes.map((scope) => ({
376
+ scope,
377
+ assignmentId: `${scope}/providers/Microsoft.Authorization/policyAssignments/${assignmentName}`,
378
+ displayName: "Policy",
379
+ policyDefinitionId: "policy",
380
+ assignmentType: scope === managementScope ? "management-group" : "subscription",
381
+ }));
382
+ Object.assign(entry.discovery_summary, {
383
+ assignment_total: 5,
384
+ assignment_kept: 5,
385
+ subscription_scope_count: 4,
386
+ management_group_inherited_count: 1,
387
+ blocker_count: 5,
388
+ classified_policy_count: 5,
389
+ other_effect_count: 0,
390
+ });
391
+ entry.discovery_metadata.page_counts.policyAssignments = 5;
392
+ Object.assign(source.summary, { total_findings: 5, total_blockers: 5 });
393
+ return { source, entry, targetScope, scopes };
394
+ }
395
+ test("target projection retains inherited and child policies only after full cache validation", () => {
396
+ const { source, entry, targetScope, scopes } = descendantsBaseline();
397
+ const selected = importGovernanceBaseline(source, { ...options, targetScope }, validate);
398
+ assert.equal(selected.snapshot.schemaVersion, "governance-baseline-selection-v2");
399
+ assert.equal(selected.snapshot.targetScope, targetScope.toLowerCase());
400
+ assert.equal(selected.snapshot.findings.length, 4);
401
+ assert.ok(selected.snapshot.findings.some((item) => item.scope.endsWith("/child")));
402
+ assert.equal(selected.constraints.summary.assignmentCount, 4);
403
+ assert.equal(entry.findings.length, 5);
404
+ const other = importGovernanceBaseline(source, { ...options, targetScope: scopes[4] }, validate);
405
+ assert.notEqual(selected.snapshot.contentHash, other.snapshot.contentHash);
406
+ const scoped = structuredClone(source);
407
+ const scopedEntry = scoped.subscriptions[subscriptionId];
408
+ const reported = [targetScope, scopes[4]].map((scope) => ({
409
+ id: `${scope}/providers/Microsoft.Authorization/policyExemptions/reported`,
410
+ scope,
411
+ expiresOn: null,
412
+ category: "Waiver",
413
+ policyDefinitionReferenceIds: null,
414
+ }));
415
+ scopedEntry.findings[1].reported_exemptions = reported;
416
+ Object.assign(scopedEntry.findings[2], { effect: "auditIfNotExists", classification: "informational" });
417
+ Object.assign(scopedEntry.discovery_summary, { blocker_count: 4, informational_count: 1, audit_count: 1 });
418
+ scopedEntry.discovery_metadata.page_counts.policyExemptions = 2;
419
+ scoped.summary.total_blockers = 4;
420
+ const projected = importGovernanceBaseline(scoped, { ...options, targetScope }, validate);
421
+ assert.equal(projected.constraints.summary.auditCount, 1);
422
+ assert.equal(projected.constraints.summary.exemptionCount, 1);
423
+ assert.equal(projected.constraints.summary.denyCount, 3);
424
+ assert.equal(projected.snapshot.findings.find((item) => item.policyId === "policy-1").reportedExemptions[0].scope, targetScope);
425
+ assert.ok(projected.snapshot.findings.every((item) => item.exemption === null));
426
+ for (const mutate of [
427
+ (value) => {
428
+ value.subscriptions[subscriptionId].assignment_inventory[2].scope = scopes[4];
429
+ },
430
+ (value) => {
431
+ value.subscriptions[subscriptionId].discovery_summary.audit_count = 2;
432
+ },
433
+ (value) => {
434
+ value.subscriptions[subscriptionId].findings[2].scope = `${targetScope}/../other`;
435
+ },
436
+ (value) => {
437
+ value.subscriptions[subscriptionId].findings[2].reported_exemptions = reported;
438
+ },
439
+ ]) {
440
+ const invalid = structuredClone(scoped);
441
+ mutate(invalid);
442
+ assert.throws(() => importGovernanceBaseline(invalid, { ...options, targetScope }, validate), GovernanceBaselineError);
443
+ }
444
+ for (const target of [`/subscriptions/${otherId}`, `${targetScope}/../other`, `${targetScope}?query`]) {
445
+ assert.throws(() => importGovernanceBaseline(source, { ...options, targetScope: target }, validate), GovernanceBaselineError);
446
+ }
447
+ const partialOther = structuredClone(source);
448
+ partialOther.subscriptions[otherId] = envelope(otherId);
449
+ partialOther.subscriptions[otherId].discovery_status = "PARTIAL";
450
+ partialOther.subscriptions_discovered =
451
+ partialOther.subscriptions_processed =
452
+ partialOther.summary.subscriptions_complete =
453
+ 2;
454
+ assert.throws(() => importGovernanceBaseline(partialOther, { ...options, targetScope }, validate), GovernanceBaselineError);
455
+ const legacy = structuredClone(source);
456
+ delete legacy.subscriptions[subscriptionId].discovery_metadata.scope.coverage;
457
+ assert.throws(() => importGovernanceBaseline(legacy, { ...options, targetScope }, validate), GovernanceBaselineError);
458
+ entry.discovery_summary.blocker_count--;
459
+ assert.throws(() => importGovernanceBaseline(source, { ...options, targetScope }, validate), GovernanceBaselineError);
460
+ });
461
+ test("preserves scoped exemption provenance without granting verification", () => {
462
+ const source = inheritedBaseline();
463
+ const entry = source.subscriptions[subscriptionId];
464
+ const exemptionScope = `/subscriptions/${subscriptionId}`;
465
+ const provenance = {
466
+ id: `${exemptionScope}/providers/Microsoft.Authorization/policyExemptions/waiver`,
467
+ scope: exemptionScope,
468
+ expiresOn: "2027-01-01T00:00:00Z",
469
+ };
470
+ for (const item of entry.findings)
471
+ Object.assign(item.exemption, provenance);
472
+ const selected = importGovernanceBaseline(source, options, validate);
473
+ assert.deepEqual(selected.snapshot.findings[0].exemption, {
474
+ ...provenance,
475
+ category: "Waiver",
476
+ policyDefinitionReferenceIds: ["member-a", "member-b"],
477
+ verificationStatus: "unverified",
478
+ });
479
+ const legacy = importGovernanceBaseline(inheritedBaseline(), options, validate);
480
+ assert.equal(legacy.snapshot.findings[0].exemption?.scope, undefined);
481
+ assert.notEqual(selected.snapshot.contentHash, legacy.snapshot.contentHash);
482
+ const management = structuredClone(source);
483
+ for (const item of management.subscriptions[subscriptionId].findings) {
484
+ Object.assign(item.exemption, {
485
+ id: `${managementScope}/providers/Microsoft.Authorization/policyExemptions/waiver`,
486
+ scope: managementScope,
487
+ expiresOn: null,
488
+ });
489
+ }
490
+ const inherited = importGovernanceBaseline(management, options, validate);
491
+ assert.equal(inherited.snapshot.findings[0].exemption?.scope, managementScope);
492
+ assert.notEqual(inherited.snapshot.contentHash, selected.snapshot.contentHash);
493
+ for (const changes of [
494
+ { id: `${provenance.id}/child` },
495
+ { id: `${provenance.id}?scope=other` },
496
+ { scope: `${exemptionScope}/resourceGroups/child` },
497
+ { id: provenance.id.replace(subscriptionId, otherId) },
498
+ { expiresOn: "not-a-date" },
499
+ { expiresOn: "2027-01-01T00:00:00" },
500
+ { expiresOn: "2027-02-30T00:00:00Z" },
501
+ { id: undefined },
502
+ ]) {
503
+ const invalid = structuredClone(source);
504
+ Object.assign(invalid.subscriptions[subscriptionId].findings[0].exemption, changes);
505
+ assert.throws(() => importGovernanceBaseline(invalid, options, validate), GovernanceBaselineError);
506
+ }
507
+ });
508
+ test("rejects malformed assignment resource identities", () => {
509
+ for (const suffix of [
510
+ "",
511
+ "name/child",
512
+ "name?query",
513
+ "name#fragment",
514
+ "name%2F",
515
+ " leading",
516
+ "trailing ",
517
+ "tab\tname",
518
+ "line\nbreak",
519
+ "\u0007",
520
+ "\u0007name",
521
+ "name\u0007",
522
+ "na\u0007me",
523
+ ]) {
524
+ const source = inheritedBaseline();
525
+ source.subscriptions[subscriptionId].findings[0].assignment_id =
526
+ `${managementScope}/providers/Microsoft.Authorization/policyAssignments/${suffix}`;
527
+ rejected(source, "target-mismatch");
528
+ }
529
+ });
530
+ test("accepts assignment names with inner spaces, as Azure allows", () => {
531
+ const name = "(ArcBox) Tag resources-Solution";
532
+ const inherited = inheritedBaseline();
533
+ const assignmentId = `${managementScope}/providers/Microsoft.Authorization/policyAssignments/${name}`;
534
+ const inheritedEntry = inherited.subscriptions[subscriptionId];
535
+ const originalId = inheritedEntry.findings[0].assignment_id;
536
+ for (const item of inheritedEntry.findings)
537
+ if (item.assignment_id === originalId)
538
+ item.assignment_id = assignmentId;
539
+ assert.ok(importGovernanceBaseline(inherited, options, validate).snapshot.findings.some((item) => item.assignmentId === assignmentId));
540
+ const { source, targetScope } = descendantsBaseline(name);
541
+ const selected = importGovernanceBaseline(source, { ...options, targetScope }, validate);
542
+ assert.ok(selected.snapshot.findings.some((item) => item.assignmentId === `${targetScope}/providers/Microsoft.Authorization/policyAssignments/${name}`));
543
+ });
544
+ test("retains effective constraints while excluding arbitrary metadata and collector mapping guesses", () => {
545
+ const source = inheritedBaseline();
546
+ const entry = source.subscriptions[subscriptionId];
547
+ Object.assign(entry, { unknown: { instruction: "MALICIOUS_PAYLOAD" } });
548
+ Object.assign(entry.findings[0], {
549
+ unknown: "MALICIOUS_PAYLOAD",
550
+ required_value: { nested: [false, 3, null, "enforced"] },
551
+ assignment_parameters: { mode: "enforced", ports: [443] },
552
+ azurePropertyPath: "MALICIOUS_PAYLOAD",
553
+ policyDefinitionReferenceId: "member-b",
554
+ });
555
+ entry.tags_required = [
556
+ { name: "owner", allowed_values: ["team-b", "team-a"], default: "team-a", unknown: "MALICIOUS_PAYLOAD" },
557
+ ];
558
+ entry.allowed_locations = ["swedencentral", "germanywestcentral"];
559
+ const result = importGovernanceBaseline(source, options, validate);
560
+ assert.equal(JSON.stringify(result).includes("MALICIOUS_PAYLOAD"), false);
561
+ assert.equal(JSON.stringify(result).includes("policies"), false);
562
+ const selected = result.snapshot.findings.find((item) => item.policyId === "policy-b");
563
+ assert.deepEqual(selected.requiredValue, { nested: [false, 3, null, "enforced"] });
564
+ assert.deepEqual(selected.effectiveParameters, { mode: "enforced", ports: [443] });
565
+ assert.equal(selected.policyDefinitionReferenceId, "member-b");
566
+ assert.deepEqual(result.snapshot.tagsRequired, [
567
+ { name: "owner", allowedValues: ["team-a", "team-b"], default: "team-a" },
568
+ ]);
569
+ assert.deepEqual(result.snapshot.allowedLocations, ["germanywestcentral", "swedencentral"]);
570
+ assert.deepEqual(importGovernanceBaseline(JSON.stringify(source), options, validate), result);
571
+ });
572
+ test("different enforced values remain distinct and absent values differ from null", () => {
573
+ for (const field of ["required_value", "assignment_parameters"]) {
574
+ const source = inheritedBaseline();
575
+ const entry = source.subscriptions[subscriptionId];
576
+ const original = importGovernanceBaseline(source, options, validate);
577
+ entry.findings[0][field] = field === "required_value" ? null : { mode: "deny" };
578
+ const first = importGovernanceBaseline(source, options, validate);
579
+ assert.notDeepEqual(first, original);
580
+ entry.findings[0][field] = field === "required_value" ? false : { mode: "audit" };
581
+ assert.notDeepEqual(importGovernanceBaseline(source, options, validate), first);
582
+ }
583
+ });
584
+ test("content digest covers the entire selected raw envelope and root except observation timestamps and TTL", () => {
585
+ const source = inheritedBaseline();
586
+ const original = importGovernanceBaseline(source, options, validate);
587
+ const refreshed = structuredClone(source);
588
+ const entry = refreshed.subscriptions[subscriptionId];
589
+ entry.discovered_at = entry.discovery_metadata.discovered_at = options.now;
590
+ entry.discovery_metadata.ttl_days = 90;
591
+ Object.assign(entry, { ttl_days: 30 });
592
+ assert.equal(importGovernanceBaseline(refreshed, options, validate).snapshot.contentHash, original.snapshot.contentHash);
593
+ for (const field of ["assignment_inventory", "findings"]) {
594
+ const hashes = [original.snapshot.contentHash];
595
+ for (const mode of ["Default", "DoNotEnforce"]) {
596
+ const renewed = structuredClone(refreshed);
597
+ const renewedEntry = renewed.subscriptions[subscriptionId];
598
+ for (const item of renewedEntry[field])
599
+ item.enforcementMode = mode;
600
+ hashes.push(importGovernanceBaseline(renewed, options, validate).snapshot.contentHash);
601
+ }
602
+ assert.equal(new Set(hashes).size, 3, `${field} mode changes must not become observation-only renewal`);
603
+ }
604
+ const mutations = [
605
+ (value) => {
606
+ value.management_group_id = "different-root";
607
+ },
608
+ (value) => {
609
+ value.subscriptions[subscriptionId].assignment_inventory[0].displayName = "changed";
610
+ },
611
+ (value) => {
612
+ Object.assign(value.subscriptions[subscriptionId].discovery_summary, { audit_notes: "changed" });
613
+ },
614
+ (value) => {
615
+ Object.assign(value.subscriptions[subscriptionId].discovery_metadata, { collector: "changed" });
616
+ },
617
+ (value) => {
618
+ Object.assign(value.subscriptions[subscriptionId], { unrelated: "changed" });
619
+ },
620
+ (value) => {
621
+ value.subscriptions[subscriptionId].findings[0].required_value = false;
622
+ },
623
+ (value) => {
624
+ value.subscriptions[subscriptionId].findings[0].audit = { discovered_at: "not-a-collection-time", ttl_days: 2 };
625
+ },
626
+ ];
627
+ for (const mutate of mutations) {
628
+ const changed = structuredClone(source);
629
+ mutate(changed);
630
+ assert.notEqual(importGovernanceBaseline(changed, options, validate).snapshot.contentHash, original.snapshot.contentHash);
631
+ }
632
+ const reorderedKeys = JSON.parse(JSON.stringify(source, (_key, value) => value !== null && typeof value === "object" && !Array.isArray(value)
633
+ ? Object.fromEntries(Object.entries(value).reverse())
634
+ : value));
635
+ assert.equal(importGovernanceBaseline(reorderedKeys, options, validate).snapshot.contentHash, original.snapshot.contentHash);
636
+ });
637
+ test("rejects nonfinite and oversized effective JSON constraints", () => {
638
+ const nested = Array.from({ length: 17 }).reduce((value) => ({ nested: value }), null);
639
+ for (const field of ["required_value", "assignment_parameters"]) {
640
+ for (const value of [Infinity, NaN, nested, "x".repeat(65_537), Array.from({ length: 4097 }, () => 0)]) {
641
+ const source = inheritedBaseline();
642
+ const entry = source.subscriptions[subscriptionId];
643
+ entry.findings[0][field] = field === "assignment_parameters" ? { value } : value;
644
+ rejected(source, "invalid-input");
645
+ }
646
+ const source = inheritedBaseline();
647
+ const entry = source.subscriptions[subscriptionId];
648
+ entry.findings[0][field] = { value: "OVERFLOW_NUMBER" };
649
+ rejected(JSON.stringify(source).replaceAll('"OVERFLOW_NUMBER"', "1e400"), "invalid-input");
650
+ }
651
+ const source = inheritedBaseline();
652
+ const entry = source.subscriptions[subscriptionId];
653
+ entry.findings[0].assignment_parameters = ["not-an-object"];
654
+ rejected(source, "invalid-input");
655
+ });
656
+ test("fails closed on malformed JSON, invalid UTF-8 and full-schema violations", () => {
657
+ rejected("{", "invalid-input");
658
+ rejected(new Uint8Array([0xff]), "invalid-input");
659
+ rejected({ ...baseline(), unknown: "secret" }, "invalid-input");
660
+ rejected(null, "invalid-input");
661
+ const obsolete = inheritedBaseline();
662
+ Object.assign(obsolete.subscriptions[subscriptionId], {
663
+ policies: structuredClone(obsolete.subscriptions[subscriptionId].findings),
664
+ });
665
+ rejected(obsolete, "invalid-input");
666
+ rejected({ ...inheritedBaseline(), schema_version: "governance-baseline-v1" }, "invalid-input");
667
+ });
668
+ test("distinguishes repeated initiative policies by member reference and rejects duplicate identities", () => {
669
+ const source = inheritedBaseline();
670
+ const entry = source.subscriptions[subscriptionId];
671
+ entry.findings = ["member-a", "member-b"].map((memberId) => ({
672
+ ...entry.findings[0],
673
+ policyDefinitionReferenceId: memberId,
674
+ }));
675
+ const selected = importGovernanceBaseline(source, options, validate);
676
+ assert.deepEqual(selected.snapshot.findings.map((item) => item.policyDefinitionReferenceId), ["member-a", "member-b"]);
677
+ entry.findings[1].policyDefinitionReferenceId = "member-a";
678
+ rejected(source, "incomplete");
679
+ for (const item of entry.findings)
680
+ delete item.policyDefinitionReferenceId;
681
+ rejected(source, "incomplete");
682
+ });
683
+ test("fails closed on unclassified kept assignments but accepts audit and disabled members", () => {
684
+ const source = inheritedBaseline();
685
+ const entry = source.subscriptions[subscriptionId];
686
+ entry.findings = [];
687
+ entry.discovery_summary.informational_count = entry.discovery_summary.exempted_count = 0;
688
+ source.summary.total_findings = 0;
689
+ rejected(source, "incomplete");
690
+ entry.discovery_summary.audit_count = 2;
691
+ assert.equal(importGovernanceBaseline(source, options, validate).constraints.summary.auditCount, 2);
692
+ entry.discovery_summary.audit_count = 0;
693
+ entry.discovery_summary.disabled_count = 2;
694
+ assert.deepEqual(importGovernanceBaseline(source, options, validate).snapshot.findings, []);
695
+ const uncovered = inheritedBaseline();
696
+ const uncoveredEntry = uncovered.subscriptions[subscriptionId];
697
+ uncoveredEntry.assignment_inventory.push({ ...uncoveredEntry.assignment_inventory[0] });
698
+ Object.assign(uncoveredEntry.discovery_summary, {
699
+ assignment_total: 2,
700
+ assignment_kept: 2,
701
+ management_group_inherited_count: 2,
702
+ });
703
+ uncoveredEntry.discovery_metadata.page_counts.policyAssignments = 2;
704
+ rejected(uncovered, "incomplete");
705
+ });
706
+ test("validates optional explicit classification coverage without equating members to assignments", () => {
707
+ const source = inheritedBaseline();
708
+ const entry = source.subscriptions[subscriptionId];
709
+ const coverage = {
710
+ assignment_count: 1,
711
+ member_count: 2,
712
+ finding_count: 2,
713
+ audit_count: 0,
714
+ disabled_count: 0,
715
+ unclassified_count: 0,
716
+ };
717
+ Object.assign(entry.discovery_summary, { classification_coverage: coverage });
718
+ assert.equal(importGovernanceBaseline(source, options, validate).snapshot.findings.length, 2);
719
+ for (const key of Object.keys(coverage)) {
720
+ coverage[key]++;
721
+ rejected(source, "incomplete");
722
+ coverage[key]--;
723
+ }
724
+ Object.assign(entry.discovery_summary, { classification_coverage: {} });
725
+ rejected(source, "invalid-input");
726
+ });
727
+ test("includes not-scope exclusions exactly in assignment arithmetic and round trips", () => {
728
+ const source = inheritedBaseline();
729
+ const entry = source.subscriptions[subscriptionId];
730
+ Object.assign(entry.discovery_summary, { assignment_total: 4, defender_auto_filtered: 1, not_scope_excluded: 2 });
731
+ entry.discovery_metadata.page_counts.policyAssignments = 4;
732
+ const selected = importGovernanceBaseline(source, options, validate);
733
+ assert.equal(selected.constraints.summary.assignmentCount, 4);
734
+ assert.deepEqual(importGovernanceBaseline(JSON.stringify(source), options, validate), selected);
735
+ assert.deepEqual(importGovernanceBaseline(Buffer.from(JSON.stringify(source)), options, validate), selected);
736
+ Object.assign(entry.discovery_summary, { not_scope_excluded: 1 });
737
+ rejected(source, "incomplete");
738
+ Object.assign(entry.discovery_summary, { not_scope_excluded: -1 });
739
+ rejected(source, "invalid-input");
740
+ Object.assign(entry.discovery_summary, { not_scope_excluded: null });
741
+ rejected(source, "invalid-input");
742
+ });
743
+ test("round trips allowed excluded subscriptions without retaining their metadata", () => {
744
+ const source = baseline();
745
+ source.subscriptions_discovered = 2;
746
+ for (const reason of ["disabled", "AAD_quota"]) {
747
+ source.subscriptions_excluded = [{ subscription_id: otherId, reason, detail: "EXCLUDED_METADATA" }];
748
+ const selected = importGovernanceBaseline(source, options, validate);
749
+ assert.deepEqual(importGovernanceBaseline(JSON.stringify(source), options, validate), selected);
750
+ assert.equal(JSON.stringify(selected).includes("EXCLUDED_METADATA"), false);
751
+ }
752
+ });
753
+ test("mapping contract accepts optional definition and member identities without loosening other fields", () => {
754
+ const validateMap = new Ajv2020({ strict: false, validateFormats: false }).compile(PolicyPropertyMapV1Schema);
755
+ const mapping = {
756
+ policyAssignmentId: "assignment",
757
+ effect: "deny",
758
+ logicalResourceId: "storage",
759
+ propertyPath: "httpsOnly",
760
+ disposition: "planned",
761
+ };
762
+ const document = {
763
+ schemaVersion: "1.0.0",
764
+ projectId: "example",
765
+ runId: "run-1",
766
+ governanceHash: "a".repeat(64),
767
+ mappings: [mapping],
768
+ };
769
+ assert.ok(validateMap(document));
770
+ for (const identities of [
771
+ { policyDefinitionId: "policy" },
772
+ { policyDefinitionReferenceId: "member" },
773
+ { policyDefinitionId: "policy", policyDefinitionReferenceId: "member" },
774
+ ]) {
775
+ assert.ok(validateMap({ ...document, mappings: [{ ...mapping, ...identities }] }));
776
+ }
777
+ for (const invalid of [{ policyDefinitionId: "" }, { policyDefinitionReferenceId: 1 }, { verified: true }]) {
778
+ assert.equal(validateMap({ ...document, mappings: [{ ...mapping, ...invalid }] }), false);
779
+ }
780
+ });
781
+ test("rejects excessive nesting, hidden errors and missing inherited ancestry", () => {
782
+ rejected("[".repeat(66) + "0" + "]".repeat(66), "invalid-input");
783
+ rejected(baseline(), "invalid-options", { ...options, now: "2026-09-16T24:00:00Z" });
784
+ const source = baseline();
785
+ Object.assign(source.subscriptions[subscriptionId].discovery_metadata, { errors: ["failure"] });
786
+ rejected(source, "incomplete");
787
+ const inherited = inheritedBaseline();
788
+ inherited.subscriptions[subscriptionId].discovery_metadata.scope.management_groups = [];
789
+ rejected(inherited, "target-mismatch");
790
+ });
791
+ function reference(policies) {
792
+ return {
793
+ schema_version: "governance-reference-v1",
794
+ source: "alz-corp-reference",
795
+ library: { release: "platform/alz/2026.08.1", commit: "c".repeat(40) },
796
+ policies,
797
+ };
798
+ }
799
+ function referencePolicy(name, effect, resourceTypes) {
800
+ const scope = "/providers/Microsoft.Management/managementGroups/corp";
801
+ return {
802
+ policy_id: `/providers/Microsoft.Authorization/policyDefinitions/${name}`,
803
+ display_name: name,
804
+ effect,
805
+ scope,
806
+ assignment_display_name: name,
807
+ assignment_id: `${scope}/providers/Microsoft.Authorization/policyAssignments/${name}`,
808
+ classification: effect === "deny" ? "blocker" : "informational",
809
+ resource_types: resourceTypes,
810
+ exemption: null,
811
+ };
812
+ }
813
+ test("imports the ALZ Corp reference for any target without subscription binding", () => {
814
+ const source = reference([
815
+ referencePolicy("deny-public-ip", "deny", ["Microsoft.Network/publicIPAddresses"]),
816
+ referencePolicy("audit-tags", "audit", []),
817
+ ]);
818
+ for (const targetScope of ["local", `/subscriptions/${subscriptionId}/resourceGroups/rg-demo`]) {
819
+ const selected = importGovernanceReference(source, { targetScope, now: options.now });
820
+ assert.equal(selected.snapshot.provenance.source, "alz-corp-reference");
821
+ assert.deepEqual(selected.snapshot.provenance.reference, {
822
+ release: "platform/alz/2026.08.1",
823
+ commit: "c".repeat(40),
824
+ });
825
+ assert.equal(selected.snapshot.targetScope, targetScope.toLowerCase());
826
+ assert.deepEqual(selected.snapshot.findings.map(({ effect, resourceTypes }) => [effect, resourceTypes]), [
827
+ ["deny", ["Microsoft.Network/publicIPAddresses"]],
828
+ ["audit", []],
829
+ ]);
830
+ assert.deepEqual(selected.constraints.summary, {
831
+ assignmentCount: 2,
832
+ denyCount: 1,
833
+ modifyCount: 0,
834
+ auditCount: 1,
835
+ exemptionCount: 0,
836
+ });
837
+ }
838
+ assert.throws(() => importGovernanceReference({ ...source, source: "github-actions-baseline" }, { targetScope: "local", now: options.now }), GovernanceBaselineError);
839
+ assert.throws(() => importGovernanceReference(reference([]), { targetScope: "local", now: options.now }), GovernanceBaselineError);
840
+ });
841
+ //# sourceMappingURL=governance-baseline.test.js.map