|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `scoreMetadata` must never publish a clean verdict for a rubric that did not |
| 5 | + * run. |
| 6 | + * |
| 7 | + * ## Why the linter is mocked here rather than driven |
| 8 | + * |
| 9 | + * The crash IS reachable on a schema-valid stack — a localized `label` |
| 10 | + * (`{ en: …, 'zh-CN': … }`) on an app, or on a view's `list`, parses clean and |
| 11 | + * makes the label-case rule throw a `TypeError`. That is a defect in the rule, |
| 12 | + * filed on its own; pinning it here would make this suite depend on a bug |
| 13 | + * staying unfixed, and the day someone repairs the rule these assertions would |
| 14 | + * go green for the wrong reason — or be deleted to make them pass. |
| 15 | + * |
| 16 | + * What this file pins is the SCORER's contract, which holds for any throw from |
| 17 | + * any rule: a crash is recorded, never swallowed into `issues: []`. So the |
| 18 | + * linter is replaced by one that throws on demand, and the control below runs |
| 19 | + * the same harness with a linter that returns cleanly — a mock that always |
| 20 | + * failed would satisfy every assertion here for no reason at all. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, expect, it, vi } from 'vitest'; |
| 24 | + |
| 25 | +const lint = vi.hoisted(() => ({ |
| 26 | + /** When set, the stand-in `lintConfig` throws this instead of returning. */ |
| 27 | + throws: null as unknown, |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock('../src/commands/lint.js', async (importOriginal) => { |
| 31 | + const actual = await importOriginal<typeof import('../src/commands/lint.js')>(); |
| 32 | + return { |
| 33 | + ...actual, |
| 34 | + lintConfig: () => { |
| 35 | + if (lint.throws !== null) throw lint.throws; |
| 36 | + return []; |
| 37 | + }, |
| 38 | + }; |
| 39 | +}); |
| 40 | + |
| 41 | +const { scoreMetadata, LINT_CRASHED_RULE, SCORE_WEIGHTS } = await import('../src/lint/score.js'); |
| 42 | +const { runMetadataEval } = await import('../src/lint/metadata-eval.js'); |
| 43 | + |
| 44 | +/** Schema-valid and, to the stand-in linter, clean. */ |
| 45 | +const STACK = { |
| 46 | + objects: [ |
| 47 | + { |
| 48 | + name: 'invoice', |
| 49 | + label: 'Invoice', |
| 50 | + sharingModel: 'private', |
| 51 | + fields: { name: { type: 'text', label: 'Invoice Number', required: true } }, |
| 52 | + }, |
| 53 | + ], |
| 54 | +}; |
| 55 | + |
| 56 | +/** Schema-INVALID (`namespace` fails its pattern), so the parse half has a verdict. */ |
| 57 | +const SCHEMA_INVALID_STACK = { |
| 58 | + manifest: { id: 'bad', namespace: 'X', version: '1.0.0', name: 'Bad', type: 'app' as const }, |
| 59 | +}; |
| 60 | + |
| 61 | +function withLinterThrowing<T>(thrown: unknown, fn: () => T): T { |
| 62 | + lint.throws = thrown; |
| 63 | + try { |
| 64 | + return fn(); |
| 65 | + } finally { |
| 66 | + lint.throws = null; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * The async twin. ⚠️ Not a stylistic variant of the above: the sync helper |
| 72 | + * restores `lint.throws` when `fn` RETURNS, which for an async `fn` is the |
| 73 | + * moment it hands back a pending promise — before a single line of the work |
| 74 | + * being measured has run. Awaiting inside the `try` is what keeps the stand-in |
| 75 | + * throwing for the whole run. |
| 76 | + */ |
| 77 | +async function withLinterThrowingAsync<T>(thrown: unknown, fn: () => Promise<T>): Promise<T> { |
| 78 | + lint.throws = thrown; |
| 79 | + try { |
| 80 | + return await fn(); |
| 81 | + } finally { |
| 82 | + lint.throws = null; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +describe('scoreMetadata — when the linter crashes', () => { |
| 87 | + it('CONTROL: the same harness with a linter that returns cleanly still scores 100 / A', () => { |
| 88 | + const r = scoreMetadata(STACK); |
| 89 | + expect(r.lintError).toBeUndefined(); |
| 90 | + expect(r.score).toBe(100); |
| 91 | + expect(r.grade).toBe('A'); |
| 92 | + expect(r.valid).toBe(true); |
| 93 | + expect(r.counts.errors).toBe(0); |
| 94 | + expect(r.issues).toEqual([]); |
| 95 | + }); |
| 96 | + |
| 97 | + it('refuses the verdict instead of scoring 100 / A / valid', () => { |
| 98 | + const r = withLinterThrowing(new TypeError('boom'), () => scoreMetadata(STACK)); |
| 99 | + |
| 100 | + // The headline: nothing about this may read like a clean stack. |
| 101 | + expect(r.score).toBe(0); |
| 102 | + expect(r.grade).toBe('F'); |
| 103 | + expect(r.valid).toBe(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it('records the crash in every carrier a consumer might read', () => { |
| 107 | + const r = withLinterThrowing(new TypeError('boom'), () => scoreMetadata(STACK)); |
| 108 | + |
| 109 | + expect(r.lintError).toBe('boom'); |
| 110 | + expect(r.counts.errors).toBe(1); |
| 111 | + expect(r.issues).toHaveLength(1); |
| 112 | + expect(r.issues[0]).toMatchObject({ severity: 'error', rule: LINT_CRASHED_RULE }); |
| 113 | + // The message must say the rubric did not RUN — "no issues found" is the |
| 114 | + // exact reading this whole change exists to prevent. |
| 115 | + expect(r.issues[0].message).toContain('did not run'); |
| 116 | + expect(r.issues[0].message).toContain('boom'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('keeps the schema verdict, which the crash must not mask', () => { |
| 120 | + const r = withLinterThrowing(new TypeError('boom'), () => scoreMetadata(SCHEMA_INVALID_STACK)); |
| 121 | + |
| 122 | + expect(r.counts.schemaErrors).toBeGreaterThan(0); |
| 123 | + expect(r.schemaErrors.some((m) => m.includes('namespace'))).toBe(true); |
| 124 | + expect(r.lintError).toBe('boom'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('stringifies a non-Error throw rather than reporting "undefined"', () => { |
| 128 | + const r = withLinterThrowing('plain string failure', () => scoreMetadata(STACK)); |
| 129 | + |
| 130 | + expect(r.lintError).toBe('plain string failure'); |
| 131 | + expect(r.issues[0].message).toContain('plain string failure'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('is not a lint error in disguise: a real lint error scores by the rubric and sets no lintError', () => { |
| 135 | + // One `error`-severity issue costs exactly its weight — the crash path is a |
| 136 | + // different claim from "the linter found one error", and the two must not |
| 137 | + // land on the same output. |
| 138 | + const r = scoreMetadata({ |
| 139 | + objects: [{ name: 'BadName', label: 'Bad', fields: { name: { type: 'text', label: 'Name' } } }], |
| 140 | + }); |
| 141 | + expect(r.lintError).toBeUndefined(); |
| 142 | + expect(r.score).toBeGreaterThan(100 - SCORE_WEIGHTS.error * 2); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe('the eval harness reads the refusal without a new field', () => { |
| 147 | + const CORPUS = [{ id: 'crashing_case', prompt: 'anything', fixture: STACK }]; |
| 148 | + |
| 149 | + it('CONTROL: the same case passes when the linter returns cleanly', async () => { |
| 150 | + const report = await runMetadataEval(CORPUS, { minScore: 75 }); |
| 151 | + expect(report.results[0].passed).toBe(true); |
| 152 | + expect(report.ok).toBe(true); |
| 153 | + }); |
| 154 | + |
| 155 | + it('fails the case whose linter crashed, and contributes 0 to the mean', async () => { |
| 156 | + const report = await withLinterThrowingAsync(new TypeError('boom'), () => |
| 157 | + runMetadataEval(CORPUS, { minScore: 75 }), |
| 158 | + ); |
| 159 | + |
| 160 | + expect(report.results[0].passed).toBe(false); |
| 161 | + expect(report.results[0].score.lintError).toBe('boom'); |
| 162 | + expect(report.meanScore).toBe(0); |
| 163 | + expect(report.ok).toBe(false); |
| 164 | + }); |
| 165 | + |
| 166 | + it('still fails it when the score bar is lowered to 0 — the synthetic error is what holds', async () => { |
| 167 | + // `passed` reads `score >= minScore && counts.errors === 0 && |
| 168 | + // counts.schemaErrors === 0`. At `--eval-min 0` the score half stops |
| 169 | + // discriminating, so the crash has to be an `error` in `counts` or the case |
| 170 | + // passes again. This is why the refusal is not carried by the number alone. |
| 171 | + const report = await withLinterThrowingAsync(new TypeError('boom'), () => |
| 172 | + runMetadataEval(CORPUS, { minScore: 0 }), |
| 173 | + ); |
| 174 | + |
| 175 | + expect(report.results[0].score.score).toBeGreaterThanOrEqual(0); |
| 176 | + expect(report.results[0].score.counts.errors).toBe(1); |
| 177 | + expect(report.results[0].passed).toBe(false); |
| 178 | + expect(report.ok).toBe(false); |
| 179 | + }); |
| 180 | +}); |
0 commit comments