@holoscript/plugin-government-civic 2.0.1

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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # @holoscript/plugin-government-civic
2
+
3
+ ## 2.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [c64fc1a]
8
+ - @holoscript/core@8.0.6
9
+
10
+ ## 2.0.0
11
+
12
+ ### Patch Changes
13
+
14
+ - @holoscript/core@6.1.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-2026 HoloScript Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@holoscript/plugin-government-civic",
3
+ "version": "2.0.1",
4
+ "description": "HoloScript domain plugin for government and civic services — permits, public meetings, service requests, voting records, and ADA/FOIA compliance.",
5
+ "main": "src/index.ts",
6
+ "peerDependencies": {
7
+ "@holoscript/core": "8.0.6"
8
+ },
9
+ "license": "MIT",
10
+ "scripts": {
11
+ "test": "vitest run --passWithNoTests",
12
+ "test:coverage": "vitest run --coverage --passWithNoTests"
13
+ }
14
+ }
@@ -0,0 +1,360 @@
1
+ /**
2
+ * Government & civic solver tests — government-civic-plugin
3
+ *
4
+ * Reference values verified against:
5
+ * - Polsby D, Popper R (1991) 9 Yale L. & Pol'y Rev. 301
6
+ * - Rice S (1928) Am. J. Sociology 33:688-708
7
+ * - GFOA Budget Variance Standards
8
+ */
9
+
10
+ import { describe, it, expect } from 'vitest';
11
+ import {
12
+ permitScoring,
13
+ mcdaAnalysis,
14
+ quorumCalculator,
15
+ votingCohesion,
16
+ polsbyPopper,
17
+ budgetVariance,
18
+ buildGovtReceipt,
19
+ } from '../civicsolver';
20
+
21
+ // ─── Permit Scoring ───────────────────────────────────────────────────────────
22
+
23
+ describe('permitScoring', () => {
24
+ it('compositeScore = weighted sum of criterion scores', () => {
25
+ const criteria = [
26
+ { name: 'safety', weight: 0.40, score: 80 },
27
+ { name: 'zoning', weight: 0.35, score: 90 },
28
+ { name: 'env', weight: 0.25, score: 70 },
29
+ ];
30
+ const r = permitScoring(criteria);
31
+ const expected = 0.40 * 80 + 0.35 * 90 + 0.25 * 70;
32
+ expect(r.compositeScore).toBeCloseTo(expected, 4);
33
+ });
34
+
35
+ it('approved=true when compositeScore ≥ threshold', () => {
36
+ const criteria = [{ name: 'safety', weight: 1.0, score: 85 }];
37
+ const r = permitScoring(criteria, 70);
38
+ expect(r.approved).toBe(true);
39
+ });
40
+
41
+ it('approved=false when compositeScore < threshold', () => {
42
+ const criteria = [{ name: 'safety', weight: 1.0, score: 60 }];
43
+ const r = permitScoring(criteria, 70);
44
+ expect(r.approved).toBe(false);
45
+ });
46
+
47
+ it('breakdown includes contribution = score × weight', () => {
48
+ const criteria = [
49
+ { name: 'env', weight: 0.60, score: 75 },
50
+ { name: 'traffic', weight: 0.40, score: 50 },
51
+ ];
52
+ const r = permitScoring(criteria);
53
+ expect(r.breakdown[0].contribution).toBeCloseTo(0.60 * 75, 4);
54
+ expect(r.breakdown[1].contribution).toBeCloseTo(0.40 * 50, 4);
55
+ });
56
+
57
+ it('throws when weights do not sum to 1.0', () => {
58
+ const criteria = [
59
+ { name: 'a', weight: 0.5, score: 80 },
60
+ { name: 'b', weight: 0.3, score: 70 },
61
+ ]; // sum = 0.8
62
+ expect(() => permitScoring(criteria)).toThrow();
63
+ });
64
+
65
+ it('throws for empty criteria', () => {
66
+ expect(() => permitScoring([])).toThrow();
67
+ });
68
+ });
69
+
70
+ // ─── MCDA Analysis ────────────────────────────────────────────────────────────
71
+
72
+ describe('mcdaAnalysis', () => {
73
+ const candidates = [
74
+ { id: 'project-A', scores: [90, 60, 40] },
75
+ { id: 'project-B', scores: [70, 80, 90] },
76
+ { id: 'project-C', scores: [50, 90, 70] },
77
+ ];
78
+ const criteria = [
79
+ { name: 'cost-effectiveness', weight: 0.50, isBenefit: true },
80
+ { name: 'community-impact', weight: 0.30, isBenefit: true },
81
+ { name: 'implementation-ease',weight: 0.20, isBenefit: true },
82
+ ];
83
+
84
+ it('winner is a valid candidate id', () => {
85
+ const r = mcdaAnalysis(candidates, criteria);
86
+ const ids = candidates.map(c => c.id);
87
+ expect(ids).toContain(r.winner);
88
+ });
89
+
90
+ it('ranking has all candidates', () => {
91
+ const r = mcdaAnalysis(candidates, criteria);
92
+ expect(r.ranking).toHaveLength(candidates.length);
93
+ });
94
+
95
+ it('TOPSIS scores in [0, 1]', () => {
96
+ const r = mcdaAnalysis(candidates, criteria);
97
+ for (const item of r.ranking) {
98
+ expect(item.topsisScore).toBeGreaterThanOrEqual(0);
99
+ expect(item.topsisScore).toBeLessThanOrEqual(1);
100
+ }
101
+ });
102
+
103
+ it('ranks are 1-indexed and sequential', () => {
104
+ const r = mcdaAnalysis(candidates, criteria);
105
+ const ranks = r.ranking.map(x => x.rank).sort((a, b) => a - b);
106
+ expect(ranks).toEqual([1, 2, 3]);
107
+ });
108
+
109
+ it('dominant candidate wins when clearly superior', () => {
110
+ // project-X dominates on all criteria vs project-Y
111
+ const dom = [
112
+ { id: 'project-X', scores: [100, 100, 100] },
113
+ { id: 'project-Y', scores: [10, 10, 10] },
114
+ ];
115
+ const r = mcdaAnalysis(dom, criteria);
116
+ expect(r.winner).toBe('project-X');
117
+ });
118
+
119
+ it('throws for empty candidates', () => {
120
+ expect(() => mcdaAnalysis([], criteria)).toThrow();
121
+ });
122
+
123
+ it('throws when scores.length ≠ criteria.length', () => {
124
+ const bad = [{ id: 'x', scores: [1, 2] }];
125
+ expect(() => mcdaAnalysis(bad, criteria)).toThrow();
126
+ });
127
+ });
128
+
129
+ // ─── Quorum Calculator ────────────────────────────────────────────────────────
130
+
131
+ describe('quorumCalculator', () => {
132
+ /**
133
+ * 15 member board, 10 present, 7 yes, 2 no, 1 abstain
134
+ * Quorum required: 8 → 10 present → met
135
+ * Simple majority of 7+2=9 votes → 7/9 = 77.8% > 50% → passes
136
+ */
137
+ it('quorumMet=true when present ≥ strict majority', () => {
138
+ const r = quorumCalculator(15, 10, 7, 2, 1);
139
+ expect(r.quorumMet).toBe(true);
140
+ });
141
+
142
+ it('motionPassed=true for simple majority with quorum', () => {
143
+ const r = quorumCalculator(15, 10, 7, 2, 1, 'simple');
144
+ expect(r.motionPassed).toBe(true);
145
+ });
146
+
147
+ it('quorumMet=false when present < strict majority', () => {
148
+ const r = quorumCalculator(20, 9, 7, 2, 0);
149
+ // Required: 11; present: 9 → not met
150
+ expect(r.quorumMet).toBe(false);
151
+ });
152
+
153
+ it('motionPassed=false when quorum not met', () => {
154
+ const r = quorumCalculator(20, 9, 7, 2, 0);
155
+ expect(r.motionPassed).toBe(false);
156
+ });
157
+
158
+ it('supermajority requires > 2/3 yes', () => {
159
+ // 6 yes, 4 no out of 10 → 60% < 66.7% → fails supermajority
160
+ const r = quorumCalculator(10, 10, 6, 4, 0, 'supermajority');
161
+ expect(r.motionPassed).toBe(false);
162
+ });
163
+
164
+ it('supermajority passes with 7/9 yes', () => {
165
+ const r = quorumCalculator(10, 10, 7, 2, 1, 'supermajority');
166
+ expect(r.motionPassed).toBe(true);
167
+ });
168
+
169
+ it('passThreshold=0.5 for simple, 2/3 for supermajority', () => {
170
+ const simple = quorumCalculator(10, 8, 5, 3, 0, 'simple');
171
+ const super_ = quorumCalculator(10, 8, 5, 3, 0, 'supermajority');
172
+ expect(simple.passThreshold).toBeCloseTo(0.5, 4);
173
+ expect(super_.passThreshold).toBeCloseTo(2 / 3, 4);
174
+ });
175
+
176
+ it('throws for presentMembers > totalMembers', () => {
177
+ expect(() => quorumCalculator(10, 15, 5, 3, 0)).toThrow();
178
+ });
179
+ });
180
+
181
+ // ─── Voting Cohesion ──────────────────────────────────────────────────────────
182
+
183
+ describe('votingCohesion', () => {
184
+ /**
185
+ * Party A: 3 yes, 0 no → Rice = |3-0|/3 = 1.0
186
+ * Party B: 1 yes, 2 no → Rice = |1-2|/3 = 0.33
187
+ */
188
+ const votes = [
189
+ { memberId: 'A1', partyId: 'A', vote: 'yes' as const },
190
+ { memberId: 'A2', partyId: 'A', vote: 'yes' as const },
191
+ { memberId: 'A3', partyId: 'A', vote: 'yes' as const },
192
+ { memberId: 'B1', partyId: 'B', vote: 'yes' as const },
193
+ { memberId: 'B2', partyId: 'B', vote: 'no' as const },
194
+ { memberId: 'B3', partyId: 'B', vote: 'no' as const },
195
+ ];
196
+
197
+ it('unanimous party cohesion = 1.0', () => {
198
+ const r = votingCohesion(votes);
199
+ const partyA = r.partyCohesion.find(p => p.partyId === 'A');
200
+ expect(partyA!.cohesion).toBeCloseTo(1.0, 4);
201
+ });
202
+
203
+ it('split party cohesion < 1', () => {
204
+ const r = votingCohesion(votes);
205
+ const partyB = r.partyCohesion.find(p => p.partyId === 'B');
206
+ expect(partyB!.cohesion).toBeGreaterThanOrEqual(0);
207
+ expect(partyB!.cohesion).toBeLessThan(1);
208
+ });
209
+
210
+ it('chamberCohesion in [0, 1]', () => {
211
+ const r = votingCohesion(votes);
212
+ expect(r.chamberCohesion).toBeGreaterThanOrEqual(0);
213
+ expect(r.chamberCohesion).toBeLessThanOrEqual(1);
214
+ });
215
+
216
+ it('majority=yes when more yes than no', () => {
217
+ const r = votingCohesion(votes); // 4 yes, 2 no
218
+ expect(r.majority).toBe('yes');
219
+ });
220
+
221
+ it('majority=no when more no than yes', () => {
222
+ const noMajority = votes.map(v => ({ ...v, vote: (v.partyId === 'A' ? 'no' : v.vote) as 'yes' | 'no' | 'abstain' }));
223
+ const r = votingCohesion(noMajority); // 1 yes, 5 no
224
+ expect(r.majority).toBe('no');
225
+ });
226
+
227
+ it('throws for empty votes', () => {
228
+ expect(() => votingCohesion([])).toThrow();
229
+ });
230
+ });
231
+
232
+ // ─── Polsby-Popper ────────────────────────────────────────────────────────────
233
+
234
+ describe('polsbyPopper', () => {
235
+ /**
236
+ * Perfect circle: PP = 4π×A / P² = 1.0
237
+ * Circle with A=π, P=2π → PP = 4π×π / (2π)² = 4π²/4π² = 1.0
238
+ */
239
+ it('circle → compactnessScore ≈ 1.0', () => {
240
+ const A = Math.PI; // π km²
241
+ const P = 2 * Math.PI; // 2π km
242
+ const r = polsbyPopper(A, P);
243
+ expect(r.compactnessScore).toBeCloseTo(1.0, 4);
244
+ });
245
+
246
+ it('compact classification for score ≥ 0.50', () => {
247
+ const r = polsbyPopper(Math.PI, 2 * Math.PI);
248
+ expect(r.classification).toBe('compact');
249
+ });
250
+
251
+ it('gerrymandered classification for very elongated shape', () => {
252
+ // Very elongated: narrow strip, A=1, P=100 → PP = 4π/10000 ≈ 0.00125
253
+ const r = polsbyPopper(1, 100);
254
+ expect(r.classification).toBe('gerrymandered');
255
+ expect(r.compactnessScore).toBeLessThan(0.20);
256
+ });
257
+
258
+ it('moderate classification for 0.20 ≤ score < 0.50', () => {
259
+ // Rectangle 2×1: A=2, P=6 → PP = 4π×2/36 ≈ 0.698... wait that's >0.5
260
+ // Rectangle 4×1: A=4, P=10 → PP = 4π×4/100 = 0.503 still >0.5
261
+ // Rectangle 10×1: A=10, P=22 → PP = 4π×10/484 ≈ 0.259 → moderate
262
+ const r = polsbyPopper(10, 22);
263
+ expect(r.classification).toBe('moderate');
264
+ });
265
+
266
+ it('compactnessScore = 4π × area / perimeter²', () => {
267
+ const A = 5, P = 15;
268
+ const r = polsbyPopper(A, P);
269
+ expect(r.compactnessScore).toBeCloseTo((4 * Math.PI * A) / (P ** 2), 6);
270
+ });
271
+
272
+ it('throws for non-positive area', () => {
273
+ expect(() => polsbyPopper(0, 10)).toThrow();
274
+ });
275
+ });
276
+
277
+ // ─── Budget Variance ──────────────────────────────────────────────────────────
278
+
279
+ describe('budgetVariance', () => {
280
+ const lines = [
281
+ { category: 'Roads', budgeted: 100_000, actual: 95_000 }, // under → favorable
282
+ { category: 'Parks', budgeted: 50_000, actual: 60_000 }, // over → unfavorable
283
+ { category: 'Admin', budgeted: 75_000, actual: 75_500 }, // near → on-target
284
+ ];
285
+
286
+ it('variance = actual - budgeted', () => {
287
+ const r = budgetVariance(lines);
288
+ expect(r.lines[0].variance).toBe(95_000 - 100_000);
289
+ expect(r.lines[1].variance).toBe(60_000 - 50_000);
290
+ });
291
+
292
+ it('variancePct = variance / budgeted', () => {
293
+ const r = budgetVariance(lines);
294
+ expect(r.lines[0].variancePct).toBeCloseTo(-5_000 / 100_000, 4);
295
+ expect(r.lines[1].variancePct).toBeCloseTo(10_000 / 50_000, 4);
296
+ });
297
+
298
+ it('under-budget is favorable', () => {
299
+ const r = budgetVariance(lines);
300
+ expect(r.lines[0].status).toBe('favorable');
301
+ });
302
+
303
+ it('over-budget is unfavorable', () => {
304
+ const r = budgetVariance(lines);
305
+ expect(r.lines[1].status).toBe('unfavorable');
306
+ });
307
+
308
+ it('flaggedLines contains categories with |variance%| > threshold', () => {
309
+ const r = budgetVariance(lines, 0.10); // 10% threshold
310
+ // Parks: 20% over → flagged. Roads: 5% under → not flagged (below 10%)
311
+ expect(r.flaggedLines).toContain('Parks');
312
+ expect(r.flaggedLines).not.toContain('Roads');
313
+ });
314
+
315
+ it('totalBudgeted and totalActual correct', () => {
316
+ const r = budgetVariance(lines);
317
+ expect(r.totalBudgeted).toBe(225_000);
318
+ expect(r.totalActual).toBe(230_500);
319
+ });
320
+
321
+ it('throws for empty lines', () => {
322
+ expect(() => budgetVariance([])).toThrow();
323
+ });
324
+ });
325
+
326
+ // ─── Receipt ─────────────────────────────────────────────────────────────────
327
+
328
+ describe('buildGovtReceipt', () => {
329
+ it('plugin=government-civic and CAEL event correct', () => {
330
+ const permit = permitScoring([{ name: 'safety', weight: 1.0, score: 85 }]);
331
+ const receipt = buildGovtReceipt({ permit, converged: true });
332
+ expect(receipt.plugin).toBe('government-civic');
333
+ expect(receipt.cael.event).toBe('government_civic.civic_analysis');
334
+ expect(receipt.payloadHash).toBeTruthy();
335
+ });
336
+
337
+ it('accepted=true for approved permit', () => {
338
+ const permit = permitScoring([{ name: 'safety', weight: 1.0, score: 85 }]);
339
+ const receipt = buildGovtReceipt({ permit, converged: true });
340
+ expect(receipt.acceptance.accepted).toBe(true);
341
+ });
342
+
343
+ it('accepted=false for denied permit', () => {
344
+ const permit = permitScoring([{ name: 'safety', weight: 1.0, score: 55 }]);
345
+ const receipt = buildGovtReceipt({ permit, converged: true });
346
+ expect(receipt.acceptance.accepted).toBe(false);
347
+ expect(receipt.acceptance.violations.length).toBeGreaterThan(0);
348
+ });
349
+
350
+ it('accepted=false for gerrymandered district', () => {
351
+ const compactness = polsbyPopper(1, 200); // very elongated
352
+ const receipt = buildGovtReceipt({ compactness, converged: true });
353
+ expect(receipt.acceptance.accepted).toBe(false);
354
+ });
355
+
356
+ it('uses provided runId', () => {
357
+ const receipt = buildGovtReceipt({ converged: true }, { runId: 'gov-run-42' });
358
+ expect(receipt.runId).toBe('gov-run-42');
359
+ });
360
+ });