File size: 9,084 Bytes
ce8f04a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
 * Node built-in test runner for pure GenerationReadinessPanel helpers.
 * Mirrors pure exports in GenerationReadinessPanel.tsx (kept isolated — no React).
 * Run: node --test src/components/project/GenerationReadinessPanel.node-test.mjs
 */
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';

const MSP_LABELS = {
  mikro: 'Mikro',
  mala: 'Mała',
  srednia: 'Średnia',
  duza: 'Duża',
  unknown: 'Nieustalony',
};

function formatMspLabel(status) {
  const key = String(status || 'unknown').toLowerCase().trim();
  return MSP_LABELS[key] || MSP_LABELS.unknown;
}

function formatEur(value) {
  if (value === null || value === undefined || Number.isNaN(Number(value))) {
    return 'brak danych';
  }
  return `${Number(value).toLocaleString('pl-PL', { maximumFractionDigits: 0 })} EUR`;
}

function shouldShowEligibilitySection(readiness) {
  if (!readiness) return false;
  if (readiness.eligibility && typeof readiness.eligibility === 'object') return true;
  const missing = readiness.missing_fields || [];
  return missing.some((m) => m.category === 'eligibility');
}

function isEligibilitySpineEnforced(readiness) {
  if (!readiness) return true;
  const elig = readiness.eligibility;
  if (readiness.eligibility_spine_enforced === false) return false;
  if (elig && typeof elig === 'object' && elig.spine_enabled === false) return false;
  return true;
}

function isEligibilityBlocking(readiness) {
  if (!readiness) return true;
  const elig = readiness.eligibility;
  if (!elig || typeof elig !== 'object') {
    return (readiness.missing_fields || []).some((m) => m.category === 'eligibility');
  }

  if (Array.isArray(elig.blockers) && elig.blockers.length > 0) return true;

  if (!isEligibilitySpineEnforced(readiness)) {
    return false;
  }

  if (elig.ready_for_full_autopilot === true) return false;
  if (elig.ready_for_full_autopilot === false) return true;

  const mspStatus = String(elig.msp?.status || 'unknown').toLowerCase().trim();
  if (mspStatus === 'unknown' || Boolean(elig.msp?.is_large)) return true;
  if (Boolean(elig.de_minimis?.exhausted)) return true;
  if (elig.de_minimis?.used_eur === null || elig.de_minimis?.used_eur === undefined) {
    return true;
  }
  return false;
}

function eligibilityBannerMessage(readiness) {
  if (isEligibilityBlocking(readiness)) {
    return {
      tone: 'blocked',
      text:
        'Full Autopilot zablokowany do czasu weryfikacji kwalifikowalności (fail-closed). ' +
        'Brak wiarygodnego statusu MŚP lub de minimis nie jest traktowany jako „OK”.',
    };
  }
  if (!isEligibilitySpineEnforced(readiness)) {
    return {
      tone: 'info',
      text:
        'Spine kwalifikowalności w trybie informacyjnym (nie egzekwowany) — nie bramkuje Full Autopilot. ' +
        'Status MŚP / de minimis poniżej tylko do wglądu.',
    };
  }
  return {
    tone: 'ok',
    text: 'Spine kwalifikowalności OK — MŚP i de minimis nie blokują Full Autopilot.',
  };
}

describe('formatMspLabel', () => {
  it('maps known statuses', () => {
    assert.equal(formatMspLabel('mikro'), 'Mikro');
    assert.equal(formatMspLabel('duza'), 'Duża');
    assert.equal(formatMspLabel('unknown'), 'Nieustalony');
  });

  it('fails closed on empty', () => {
    assert.equal(formatMspLabel(null), 'Nieustalony');
    assert.equal(formatMspLabel(''), 'Nieustalony');
    assert.equal(formatMspLabel('weird'), 'Nieustalony');
  });
});

describe('formatEur', () => {
  it('formats numbers', () => {
    assert.match(formatEur(45000), /45\s?000/);
    assert.match(formatEur(0), /0/);
  });

  it('fails closed on null/undefined/NaN', () => {
    assert.equal(formatEur(null), 'brak danych');
    assert.equal(formatEur(undefined), 'brak danych');
    assert.equal(formatEur(Number.NaN), 'brak danych');
  });
});

describe('shouldShowEligibilitySection', () => {
  it('false when no readiness', () => {
    assert.equal(shouldShowEligibilitySection(null), false);
    assert.equal(shouldShowEligibilitySection({}), false);
  });

  it('true when eligibility report present', () => {
    assert.equal(
      shouldShowEligibilitySection({ eligibility: { blockers: [] } }),
      true,
    );
  });

  it('true when missing_fields category eligibility', () => {
    assert.equal(
      shouldShowEligibilitySection({
        missing_fields: [{ field: 'msp_status', label: 'MŚP', category: 'eligibility' }],
      }),
      true,
    );
  });

  it('false when only other missing categories', () => {
    assert.equal(
      shouldShowEligibilitySection({
        missing_fields: [{ field: 'title', label: 'Tytuł', category: 'project' }],
      }),
      false,
    );
  });
});

describe('isEligibilityBlocking (fail-closed)', () => {
  it('blocks when readiness null', () => {
    assert.equal(isEligibilityBlocking(null), true);
  });

  it('blocks on eligibility missing_fields without report', () => {
    assert.equal(
      isEligibilityBlocking({
        missing_fields: [{ field: 'nip', label: 'NIP', category: 'eligibility' }],
      }),
      true,
    );
  });

  it('does not block on unrelated missing fields without report', () => {
    assert.equal(
      isEligibilityBlocking({
        missing_fields: [{ field: 'title', label: 'Tytuł', category: 'project' }],
      }),
      false,
    );
  });

  it('blocks on explicit blockers always', () => {
    assert.equal(
      isEligibilityBlocking({
        eligibility_spine_enforced: false,
        eligibility: {
          spine_enabled: false,
          ready_for_full_autopilot: true,
          blockers: ['Status duża firma'],
          msp: { status: 'duza', is_large: true },
          de_minimis: { used_eur: 0 },
        },
      }),
      true,
    );
  });

  it('does not claim FA blocked when spine not enforced (no blockers)', () => {
    assert.equal(
      isEligibilityBlocking({
        eligibility_spine_enforced: false,
        eligibility: {
          spine_enabled: false,
          ready_for_full_autopilot: false,
          blockers: [],
          msp: { status: 'unknown' },
          de_minimis: {},
        },
      }),
      false,
    );
  });

  it('honors ready_for_full_autopilot when enforced', () => {
    assert.equal(
      isEligibilityBlocking({
        eligibility_spine_enforced: true,
        eligibility: {
          ready_for_full_autopilot: true,
          blockers: [],
          msp: { status: 'mikro' },
          de_minimis: { used_eur: 10_000, exhausted: false },
        },
      }),
      false,
    );
    assert.equal(
      isEligibilityBlocking({
        eligibility_spine_enforced: true,
        eligibility: {
          ready_for_full_autopilot: false,
          blockers: [],
          msp: { status: 'mikro' },
          de_minimis: { used_eur: 10_000 },
        },
      }),
      true,
    );
  });

  it('fail-closed heuristics when ready flag absent', () => {
    assert.equal(
      isEligibilityBlocking({
        eligibility: {
          blockers: [],
          msp: { status: 'unknown' },
          de_minimis: { used_eur: 0 },
        },
      }),
      true,
    );
    assert.equal(
      isEligibilityBlocking({
        eligibility: {
          blockers: [],
          msp: { status: 'mikro', is_large: true },
          de_minimis: { used_eur: 0 },
        },
      }),
      true,
    );
    assert.equal(
      isEligibilityBlocking({
        eligibility: {
          blockers: [],
          msp: { status: 'mikro' },
          de_minimis: { used_eur: 300_000, exhausted: true },
        },
      }),
      true,
    );
    assert.equal(
      isEligibilityBlocking({
        eligibility: {
          blockers: [],
          msp: { status: 'mikro' },
          de_minimis: { used_eur: null },
        },
      }),
      true,
    );
  });

  it('treats used_eur 0 as known (not missing)', () => {
    assert.equal(
      isEligibilityBlocking({
        eligibility: {
          blockers: [],
          msp: { status: 'srednia' },
          de_minimis: { used_eur: 0, remaining_eur: 300_000, exhausted: false },
        },
      }),
      false,
    );
  });
});

describe('eligibilityBannerMessage', () => {
  it('blocked tone on blockers', () => {
    const b = eligibilityBannerMessage({
      eligibility: { blockers: ['x'], msp: { status: 'duza' } },
    });
    assert.equal(b.tone, 'blocked');
    assert.match(b.text, /fail-closed/i);
  });

  it('info tone when spine not enforced', () => {
    const b = eligibilityBannerMessage({
      eligibility_spine_enforced: false,
      eligibility: {
        spine_enabled: false,
        blockers: [],
        msp: { status: 'unknown' },
      },
    });
    assert.equal(b.tone, 'info');
    assert.match(b.text, /informacyjnym/i);
  });

  it('ok tone when ready', () => {
    const b = eligibilityBannerMessage({
      eligibility_spine_enforced: true,
      eligibility: {
        ready_for_full_autopilot: true,
        blockers: [],
        msp: { status: 'mikro' },
        de_minimis: { used_eur: 1 },
      },
    });
    assert.equal(b.tone, 'ok');
  });
});