|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +// `field/option-color-renders-neutral`: a select option `color` the option |
| 4 | +// badge does not paint (outside the nine palette families and the `#rgb` / |
| 5 | +// `#rrggbb` hex) is warned at author time and at the save door, instead of |
| 6 | +// rendering neutral grey with no signal. A declared family name and a hex are |
| 7 | +// not warned. Advisory only: nothing is refused. |
| 8 | + |
| 9 | +import { describe, expect, it } from 'vitest'; |
| 10 | + |
| 11 | +import { authoringRulesFor } from './authoring-rules.js'; |
| 12 | +import { |
| 13 | + FIELD_OPTION_COLOR_RENDERS_NEUTRAL, |
| 14 | + OPTION_COLOR_FAMILIES, |
| 15 | + isBadgePaintableOptionColor, |
| 16 | + lintOptionColors, |
| 17 | +} from './data-model-rules.js'; |
| 18 | +import * as indexBarrel from './index.js'; |
| 19 | +import { explainRule } from './rule-explanations.js'; |
| 20 | +import { runRuntimeAuthoringRules, runtimeAuthoringRulesFor } from './runtime-gate.js'; |
| 21 | + |
| 22 | +const stageField = (colors: readonly (string | undefined)[]) => ({ |
| 23 | + type: 'select', |
| 24 | + options: colors.map((color, i) => ({ label: `S${i}`, value: `s${i}`, ...(color === undefined ? {} : { color }) })), |
| 25 | +}); |
| 26 | + |
| 27 | +const objectWith = (colors: readonly (string | undefined)[]) => [ |
| 28 | + { name: 'acme_deal', fields: { stage: stageField(colors) } }, |
| 29 | +]; |
| 30 | + |
| 31 | +const hits = (colors: readonly (string | undefined)[]) => |
| 32 | + lintOptionColors(objectWith(colors)).filter((i) => i.rule === FIELD_OPTION_COLOR_RENDERS_NEUTRAL); |
| 33 | + |
| 34 | +describe('field/option-color-renders-neutral', () => { |
| 35 | + it('is published from the root barrel under its constant, with an `os explain` entry', () => { |
| 36 | + expect(FIELD_OPTION_COLOR_RENDERS_NEUTRAL).toBe('field/option-color-renders-neutral'); |
| 37 | + expect(indexBarrel.FIELD_OPTION_COLOR_RENDERS_NEUTRAL).toBe(FIELD_OPTION_COLOR_RENDERS_NEUTRAL); |
| 38 | + expect(explainRule(FIELD_OPTION_COLOR_RENDERS_NEUTRAL)?.rule).toBe(FIELD_OPTION_COLOR_RENDERS_NEUTRAL); |
| 39 | + }); |
| 40 | + |
| 41 | + it('an out-of-family colour name is warned, located at the option, and the fix names the families', () => { |
| 42 | + const found = hits(['teal']); |
| 43 | + expect(found).toHaveLength(1); |
| 44 | + const [issue] = found; |
| 45 | + expect(issue).toMatchObject({ |
| 46 | + severity: 'warning', |
| 47 | + rule: FIELD_OPTION_COLOR_RENDERS_NEUTRAL, |
| 48 | + path: 'objects[0].fields.stage.options[0].color', |
| 49 | + where: `object "acme_deal" · field 'stage' · option 's0'`, |
| 50 | + }); |
| 51 | + expect(issue!.message).toContain("color: 'teal'"); |
| 52 | + for (const family of OPTION_COLOR_FAMILIES) expect(issue!.fix).toContain(family); |
| 53 | + }); |
| 54 | + |
| 55 | + it('a declared family name is not warned, nor is a #rgb / #rrggbb hex', () => { |
| 56 | + expect(OPTION_COLOR_FAMILIES).toHaveLength(9); |
| 57 | + expect(hits([...OPTION_COLOR_FAMILIES])).toEqual([]); |
| 58 | + expect(hits(['#14b8a6', '#ABC', '#14B8A6'])).toEqual([]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('an option with no colour, or an empty one, declares nothing and is not judged', () => { |
| 62 | + expect(hits([undefined, '', ' '])).toEqual([]); |
| 63 | + }); |
| 64 | + |
| 65 | + it('every spelling the badge renders neutral is warned, each with the fix its value points at', () => { |
| 66 | + const fixOf = (color: string) => hits([color])[0]?.fix; |
| 67 | + expect(fixOf('Red')).toBe("Write the family in lowercase: color: 'red'."); |
| 68 | + expect(fixOf('14b8a6')).toBe("Write the hex with a leading #: color: '#14b8a6'."); |
| 69 | + expect(fixOf('#14b8a6cc')).toContain("color: '#14b8a6'"); |
| 70 | + for (const color of ['amber', 'rgb(20, 184, 166)', 'hsl(173 80% 40%)', ' #14b8a6', '#14b8a']) { |
| 71 | + expect(isBadgePaintableOptionColor(color), color).toBe(false); |
| 72 | + expect(hits([color]), color).toHaveLength(1); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it('judges object-extension fields and picklists where they are written', () => { |
| 77 | + const issues = lintOptionColors( |
| 78 | + [], |
| 79 | + [{ target: 'acme_deal', path: 'objectExtensions[0]', fields: [{ name: 'tier', ...stageField(['amber']) }] }], |
| 80 | + [{ name: 'deal_stage', path: 'picklists[0]', where: 'picklist "deal_stage"', options: [{ label: 'A', value: 'a', color: 'teal' }, { label: 'B', value: 'b', color: 'blue' }] }], |
| 81 | + ); |
| 82 | + expect(issues.map((i) => i.path)).toEqual([ |
| 83 | + 'objectExtensions[0].fields.tier.options[0].color', |
| 84 | + 'picklists[0].options[0].color', |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('runs on all three commands, picklists included, from the registry', () => { |
| 89 | + for (const command of ['validate', 'build', 'lint'] as const) { |
| 90 | + expect(authoringRulesFor(command).map((r) => r.name), command).toContain('lintOptionColors'); |
| 91 | + } |
| 92 | + const entry = authoringRulesFor('validate').find((r) => r.name === 'lintOptionColors')!; |
| 93 | + const findings = entry.run( |
| 94 | + { |
| 95 | + objects: objectWith(['teal']), |
| 96 | + picklists: [{ name: 'deal_stage', label: 'Stage', options: [{ label: 'A', value: 'a', color: 'amber' }] }], |
| 97 | + picklistExtensions: [{ extend: 'deal_stage', options: [{ label: 'C', value: 'c', color: 'Red' }] }], |
| 98 | + }, |
| 99 | + {}, |
| 100 | + ); |
| 101 | + expect(findings.map((f) => [f.severity, f.path])).toEqual([ |
| 102 | + ['warning', 'objects[0].fields.stage.options[0].color'], |
| 103 | + ['warning', 'picklists[0].options[0].color'], |
| 104 | + ['warning', 'picklistExtensions[0].options[0].color'], |
| 105 | + ]); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('at the save door (object writes)', () => { |
| 109 | + const write = (color: string) => ({ |
| 110 | + name: 'acme_deal', |
| 111 | + label: 'Deal', |
| 112 | + sharingModel: 'private', |
| 113 | + fields: { stage: stageField([color]) }, |
| 114 | + }); |
| 115 | + |
| 116 | + it('an out-of-family colour name rides back as an advisory and refuses nothing', () => { |
| 117 | + expect(runtimeAuthoringRulesFor('object').map((r) => r.name)).toContain('lintOptionColors'); |
| 118 | + const result = runRuntimeAuthoringRules({ type: 'object', item: write('teal'), context: { objects: [] } }); |
| 119 | + expect(result.errors).toEqual([]); |
| 120 | + const advisories = result.advisories.filter((f) => f.rule === FIELD_OPTION_COLOR_RENDERS_NEUTRAL); |
| 121 | + expect(advisories).toHaveLength(1); |
| 122 | + expect(advisories[0]).toMatchObject({ severity: 'warning' }); |
| 123 | + expect(advisories[0]!.hint.length).toBeGreaterThan(0); |
| 124 | + }); |
| 125 | + |
| 126 | + it('a declared family name and a hex ride back with no advisory', () => { |
| 127 | + for (const color of ['red', '#14b8a6']) { |
| 128 | + const result = runRuntimeAuthoringRules({ type: 'object', item: write(color), context: { objects: [] } }); |
| 129 | + expect(result.advisories.filter((f) => f.rule === FIELD_OPTION_COLOR_RENDERS_NEUTRAL), color).toEqual([]); |
| 130 | + } |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments