-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharticle-types.test.ts
More file actions
300 lines (270 loc) · 11.3 KB
/
article-types.test.ts
File metadata and controls
300 lines (270 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Validation tests for analysis/article-types.json — the single source of
* truth for every Riksdagsmonitor agentic news article type.
*
* Asserts:
* - JSON parseability and repository-specific structural invariants
* - Each type has a matching .github/workflows/news-*.md source file
* - Each news-*.md workflow source has a matching registry entry (no drift)
* - Each type has a matching reference-quality-thresholds.json block
* - Election cycle dates are coherent (current.end == next.start, etc.)
* - Long-horizon types declare longHorizonRules
*
* Note: Full JSON Schema (schemas/article-types.schema.json) validation via
* Ajv is planned follow-up work; this file currently performs ad-hoc
* structural checks only.
*
* If this file fails after a workflow / registry / thresholds change, the
* single-source-of-truth invariant has been broken and must be repaired
* before merging.
*
* @author Hack23 AB
* @license Apache-2.0
*/
import { describe, it, expect } from 'vitest';
import { readFileSync, readdirSync, existsSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const repoRoot = resolve(__dirname, '..');
interface ArticleType {
id: string;
family: 'single-type' | 'tier-c-aggregation' | 'long-horizon-forecast';
horizonDays: number;
lookbackDays: number;
subfolder: string;
tierCMultiplier: number;
articleWordFloor: number;
scenarioHorizonYears: number;
forwardIndicatorHorizons: string[];
electionCycleAnchor: 'current' | 'next' | 'both' | 'none';
imfPolicy: string;
icon: string;
label: string;
workflow: string;
cronExpression?: string;
dispatchOnly?: boolean;
coreLanguages: string[];
extraArtifacts: string[];
longHorizonRules?: Record<string, unknown>;
[key: string]: unknown;
}
interface Registry {
version: string;
effectiveDate: string;
owner: string;
classification: string;
types: ArticleType[];
electionCycles: {
current: { id: string; start: string; end: string };
next: { id: string; start: string; end: string };
};
horizonBands: Record<string, { days: number; wepFloor: string }>;
}
const registryPath = resolve(repoRoot, 'analysis/article-types.json');
const registry: Registry = JSON.parse(readFileSync(registryPath, 'utf8'));
describe('article-types registry — structural validity', () => {
it('parses as JSON and declares a semantic version', () => {
expect(registry.version).toMatch(/^\d+\.\d+(\.\d+)?$/);
expect(registry.effectiveDate).toMatch(/^\d{4}-\d{2}-\d{2}$/);
expect(registry.classification).toBe('Public');
});
it('declares at least the 13 known article types', () => {
expect(registry.types.length).toBeGreaterThanOrEqual(13);
const ids = registry.types.map((t) => t.id);
const expected = [
'propositions',
'motions',
'committee-reports',
'interpellations',
'realtime-monitor',
'evening-analysis',
'week-ahead',
'month-ahead',
'quarter-ahead',
'year-ahead',
'election-cycle',
'weekly-review',
'monthly-review',
];
for (const id of expected) {
expect(ids, `missing article type ${id}`).toContain(id);
}
});
it('every type id is unique', () => {
const ids = registry.types.map((t) => t.id);
const set = new Set(ids);
expect(set.size).toBe(ids.length);
});
it('every subfolder is unique', () => {
const subs = registry.types.map((t) => t.subfolder);
const set = new Set(subs);
expect(set.size).toBe(subs.length);
});
});
describe('article-types registry — workflow ↔ registry parity', () => {
const workflowsDir = resolve(repoRoot, '.github/workflows');
const newsWorkflowFiles = readdirSync(workflowsDir).filter(
(f) => f.startsWith('news-') && f.endsWith('.md') && !f.endsWith('.lock.yml'),
);
const newsTranslate = 'news-translate.md';
it('every type with a workflow points to a real news-*.md source', () => {
for (const type of registry.types) {
const workflowPath = resolve(workflowsDir, type.workflow);
expect(existsSync(workflowPath), `${type.id}: missing workflow ${type.workflow}`).toBe(true);
}
});
it('every news-*.md (except news-translate) has a registry entry', () => {
const registeredWorkflows = new Set(registry.types.map((t) => t.workflow));
for (const wf of newsWorkflowFiles) {
if (wf === newsTranslate) continue;
expect(
registeredWorkflows.has(wf),
`news workflow ${wf} has no registry entry in analysis/article-types.json`,
).toBe(true);
}
});
});
describe('article-types registry — long-horizon rules', () => {
it('every long-horizon-forecast declares longHorizonRules', () => {
const longHorizon = registry.types.filter((t) => t.family === 'long-horizon-forecast');
expect(longHorizon.length).toBeGreaterThanOrEqual(5); // week, month, quarter, year, cycle
for (const t of longHorizon) {
expect(t.longHorizonRules, `${t.id}: missing longHorizonRules`).toBeDefined();
}
});
it('quarter / year / cycle have ascending tier-C multipliers', () => {
const get = (id: string) => registry.types.find((t) => t.id === id);
const week = get('week-ahead')!;
const month = get('month-ahead')!;
const quarter = get('quarter-ahead')!;
const year = get('year-ahead')!;
const cycle = get('election-cycle')!;
expect(week.tierCMultiplier).toBeLessThan(month.tierCMultiplier);
expect(month.tierCMultiplier).toBeLessThan(quarter.tierCMultiplier);
expect(quarter.tierCMultiplier).toBeLessThan(year.tierCMultiplier);
expect(year.tierCMultiplier).toBeLessThan(cycle.tierCMultiplier);
});
it('cycle horizon is at least 4 years (1460 days)', () => {
const cycle = registry.types.find((t) => t.id === 'election-cycle')!;
expect(cycle.horizonDays).toBeGreaterThanOrEqual(1460);
expect(cycle.electionCycleAnchor).toBe('both');
expect(cycle.dispatchOnly).toBe(true);
});
it('election-cycle declares cycle-trajectory.md as an extra artifact', () => {
const cycle = registry.types.find((t) => t.id === 'election-cycle')!;
expect(cycle.extraArtifacts).toContain('cycle-trajectory.md');
});
it('year-ahead and election-cycle mandate PESTLE', () => {
const year = registry.types.find((t) => t.id === 'year-ahead')!;
const cycle = registry.types.find((t) => t.id === 'election-cycle')!;
expect((year.longHorizonRules as { pestleMandatory?: boolean }).pestleMandatory).toBe(true);
expect((cycle.longHorizonRules as { pestleMandatory?: boolean }).pestleMandatory).toBe(true);
});
});
describe('article-types registry — election cycle coherence', () => {
it('current cycle ends where next cycle begins', () => {
expect(registry.electionCycles.current.end).toBe(registry.electionCycles.next.start);
});
it('current cycle covers Tidö mandate (2022-09-11 → 2026-09-13)', () => {
expect(registry.electionCycles.current.start).toBe('2022-09-11');
expect(registry.electionCycles.current.end).toBe('2026-09-13');
});
it('next cycle ends 2030-09-08', () => {
expect(registry.electionCycles.next.end).toBe('2030-09-08');
});
});
describe('article-types registry — horizon bands', () => {
it('declares the canonical seven horizon bands', () => {
const bands = Object.keys(registry.horizonBands);
for (const band of ['72h', 'week', 'month', 'quarter', 'year', 'cycle', 'election']) {
expect(bands, `missing horizon band ${band}`).toContain(band);
}
});
it('each band has ascending or equal day-count by horizon', () => {
const order = ['72h', 'week', 'month', 'quarter', 'year', 'cycle'];
let prev = 0;
for (const b of order) {
const days = registry.horizonBands[b].days;
expect(days).toBeGreaterThanOrEqual(prev);
prev = days;
}
});
});
describe('article-types registry — reference-quality-thresholds parity', () => {
const thresholdsPath = resolve(
repoRoot,
'analysis/methodologies/reference-quality-thresholds.json',
);
const thresholds = JSON.parse(readFileSync(thresholdsPath, 'utf8')) as {
thresholds: Record<string, unknown>;
};
it('quarter-ahead, year-ahead, election-cycle have threshold blocks', () => {
for (const id of ['quarter-ahead', 'year-ahead', 'election-cycle']) {
expect(
thresholds.thresholds[id],
`reference-quality-thresholds.json missing block for ${id}`,
).toBeDefined();
}
});
});
describe('article-types registry — forward-indicator horizon band counts', () => {
const fixturePath = resolve(repoRoot, 'tests/fixtures/forward-indicators-horizon-bands.json');
const fixture = JSON.parse(readFileSync(fixturePath, 'utf8')) as {
scenarios: Array<{
name: string;
articleTypeId: string;
horizonDays: number;
expectedBands: string[];
expectedBandCount: number;
minIndicators: number;
wepCeilings: Record<string, string>;
}>;
};
for (const scenario of fixture.scenarios) {
it(`${scenario.articleTypeId}: forwardIndicatorHorizons matches fixture (${scenario.expectedBandCount} bands)`, () => {
const type = registry.types.find((t) => t.id === scenario.articleTypeId)!;
expect(type, `article type ${scenario.articleTypeId} not found`).toBeDefined();
expect(type.horizonDays).toBe(scenario.horizonDays);
expect(type.forwardIndicatorHorizons).toEqual(scenario.expectedBands);
expect(type.forwardIndicatorHorizons.length).toBe(scenario.expectedBandCount);
});
it(`${scenario.articleTypeId}: every declared band exists in horizonBands registry`, () => {
const type = registry.types.find((t) => t.id === scenario.articleTypeId)!;
for (const band of type.forwardIndicatorHorizons) {
expect(
registry.horizonBands[band],
`band "${band}" in ${scenario.articleTypeId}.forwardIndicatorHorizons not found in horizonBands`,
).toBeDefined();
}
});
it(`${scenario.articleTypeId}: WEP ceilings in fixture match horizonBands registry`, () => {
for (const [band, expectedWep] of Object.entries(scenario.wepCeilings)) {
expect(
registry.horizonBands[band]?.wepFloor,
`WEP mismatch for band "${band}"`,
).toBe(expectedWep);
}
});
}
it('six-band coverage: month-ahead declares all 6 bands (72h, week, month, quarter, year, election)', () => {
const monthAhead = registry.types.find((t) => t.id === 'month-ahead')!;
expect(monthAhead.forwardIndicatorHorizons.length).toBe(6);
expect(monthAhead.forwardIndicatorHorizons).toContain('72h');
expect(monthAhead.forwardIndicatorHorizons).toContain('week');
expect(monthAhead.forwardIndicatorHorizons).toContain('month');
expect(monthAhead.forwardIndicatorHorizons).toContain('quarter');
expect(monthAhead.forwardIndicatorHorizons).toContain('year');
expect(monthAhead.forwardIndicatorHorizons).toContain('election');
});
it('back-compat: single-type articles retain 4-band legacy schema', () => {
const singleTypes = registry.types.filter((t) => t.family === 'single-type');
for (const t of singleTypes) {
expect(
t.forwardIndicatorHorizons,
`${t.id} should have legacy 4-band schema`,
).toEqual(['72h', 'week', 'month', 'election']);
}
});
});