An open source design system that's fully customizable and agent ready
TypeScript
visual gate: the probe cover starts from the baseline, so the plan moves only when a target does (#10626)
Cindy Zhang4c1d64f5fc1c
diff --git a/.github/scripts/visual-gate/lib/plan.mjs b/.github/scripts/visual-gate/lib/plan.mjsindex ded161c901..61c31ff0bc 100644--- a/.github/scripts/visual-gate/lib/plan.mjs+++ b/.github/scripts/visual-gate/lib/plan.mjs@@ -22,8 +22,10 @@** surface — one representative story per Core component, plus explicit* visual-baseline / visual-theme-matrix stories, in Neutral light and dark.- * probe — the smallest story set that renders every theming target, in the- * generated Probe fixture light and dark.+ * probe — a small story set that renders every theming target, in the+ * generated Probe fixture light and dark. It keeps the stories the+ * accepted baseline already holds while they still render a target no+ * other kept story does, so it plans itself again from its own baseline.** `theme-matrix` remains available for focused theme evidence, but it does not* own permanent baseline keys. `full` widens `surface` to every story for an@@ -545,7 +547,8 @@ export function acceptedVisualThemes(manifest, themes, defaultTheme) {/*** Ids of the stories the accepted baseline holds probe-theme shots for. The- * probe cover keeps them when another story would cover as many targets.+ * probe cover keeps them first while they still render a target no other kept+ * story does.* @param {{shots?: Record<string, {storyId?: string, theme?: string}>}} manifest* @param {string} probeTheme* @returns {Set<string>}@@ -774,10 +777,16 @@ export function buildPlan({* verifies all of them at once. Without observations there is nothing to aim* at — the probe tier needs the scout.*- * Between stories that would cover the same number of targets, the cover keeps- * a story the accepted baseline already holds, then takes the smallest story- * id. So the choice does not move when an unrelated story is added or the- * story index changes order, and a baseline sync does not chase it.+ * The cover starts from the accepted baseline: it keeps each story the+ * baseline already holds probe shots for while that story still renders a+ * target no kept story covers, widest first. Only the targets left after that+ * get a greedy fill, and then each story the rest of the plan already covers+ * is dropped, narrowest first. A visual-theme-matrix story plans its probe+ * shots anyway, so its targets count as covered from the start. The plan is a+ * fixed point: fed back as the+ * baseline, it plans the same stories, so a baseline sync settles in one step+ * and the plan moves again only when a target is gained or lost. Between+ * stories that cover the same number of targets, the smaller story id wins.** @param {object} options* @param {ReturnType<typeof readStoryIndex>} options.stories@@ -790,47 +799,74 @@ function probeShots({stories, targets, observations, probeTheme, baselineStoriesif (!observations) return [];const wanted = new Set(targets.map(target => target.key));+ // A visual-theme-matrix story always plans its probe shots (probe:opt-in),+ // so the targets it renders are covered before the cover starts, and it is+ // never picked or dropped here.+ const optIn = new Set(+ stories+ .filter(story => (story.tags ?? []).includes(VISUAL_THEME_MATRIX_TAG))+ .map(story => story.id),+ );const byStory = new Map();+ const renders = new Map();for (const story of stories) {const rendered = Object.keys(observations[story.id] ?? {}).filter(key => wanted.has(key));- if (rendered.length > 0) byStory.set(story, new Set(rendered));+ if (rendered.length > 0) {+ renders.set(story.id, new Set(rendered));+ if (!optIn.has(story.id)) byStory.set(story, new Set(rendered));+ }}- // Greedy set cover: fewest stories that between them render every target.const planned = [];const uncovered = new Set(wanted);- while (uncovered.size > 0) {- let best = null;- let bestCount = 0;- for (const [story, rendered] of byStory) {- const count = [...rendered].filter(key => uncovered.has(key)).length;- if (count > bestCount || (count > 0 && count === bestCount && keepsTie(story, best, baselineStories))) {- best = story;- bestCount = count;+ for (const id of optIn) for (const key of renders.get(id) ?? []) uncovered.delete(key);+ /** @param {(story: {id: string}) => boolean} eligible */+ const cover = eligible => {+ while (uncovered.size > 0) {+ let best = null;+ let bestCount = 0;+ for (const [story, rendered] of byStory) {+ if (!eligible(story)) continue;+ const count = [...rendered].filter(key => uncovered.has(key)).length;+ if (count > bestCount || (count > 0 && count === bestCount && story.id < best.id)) {+ best = story;+ bestCount = count;+ }}+ if (!best) return;+ for (const key of byStory.get(best)) uncovered.delete(key);+ for (const mode of MODES) {+ planned.push({shot: {...toShotBase(best), theme: probeTheme, mode}, reason: 'probe'});+ }+ byStory.delete(best);}- if (!best) break;- for (const key of byStory.get(best)) uncovered.delete(key);- for (const mode of MODES) {- planned.push({shot: {...toShotBase(best), theme: probeTheme, mode}, reason: 'probe'});- }- byStory.delete(best);+ };+ // Keep the baseline's stories first, then a greedy set cover for the rest.+ cover(story => baselineStories.has(story.id));+ cover(() => true);+ // Drop each story the rest of the plan already covers, narrowest first, so+ // the plan, fed back as the baseline, plans itself again.+ const kept = [...new Set(planned.map(entry => entry.shot.storyId))];+ const rendersOf = id => [...(renders.get(id) ?? [])];+ for (;;) {+ const redundant = kept+ .filter(id =>+ rendersOf(id).every(+ key =>+ kept.some(other => other !== id && renders.get(other)?.has(key)) ||+ [...optIn].some(other => renders.get(other)?.has(key)),+ ),+ )+ .sort(+ (a, b) =>+ rendersOf(a).length - rendersOf(b).length ||+ Number(baselineStories.has(a)) - Number(baselineStories.has(b)) ||+ (a < b ? 1 : -1),+ );+ if (redundant.length === 0) break;+ kept.splice(kept.indexOf(redundant[0]), 1);}- return planned;-}--/**- * Whether `story` wins a tie in the probe cover over `current`: a story the- * baseline already holds first, then the smaller story id.- * @param {{id: string}} story- * @param {{id: string}} current- * @param {Set<string>} baselineStories- * @returns {boolean}- */-function keepsTie(story, current, baselineStories) {- const held = baselineStories.has(story.id);- if (held !== baselineStories.has(current.id)) return held;- return story.id < current.id;+ return planned.filter(entry => kept.includes(entry.shot.storyId));}/**diff --git a/.github/scripts/visual-gate/lib/plan.test.mjs b/.github/scripts/visual-gate/lib/plan.test.mjsindex 5129bbad1e..cde4aeb395 100644--- a/.github/scripts/visual-gate/lib/plan.test.mjs+++ b/.github/scripts/visual-gate/lib/plan.test.mjs@@ -350,6 +350,146 @@ describe('buildPlan', () => {]);});+ describe('the probe cover is a fixed point', () => {+ const target = key => ({key, className: `astryx-${key}`, component: 'Badge', props: [], states: []});+ const badge = (id, name) => story({id, title: 'Core/Badge', name, component: 'Badge'});+ const plannedStories = ({stories: list, targets: wanted, observations, baselineStories}) => [+ ...new Set(+ buildPlan({+ stories: list,+ targets: wanted,+ themeOverrides: {},+ observations,+ defaultTheme: 'neutral',+ tiers: ['probe'],+ probeTheme: 'probe',+ baselineStories,+ }).map(shot => shot.storyId),+ ),+ ].sort();+ const cases = {+ 'a baselined story and a wider one a new target needs': {+ stories: [badge('core-badge--solid', 'Solid'), badge('core-badge--wide', 'Wide')],+ targets: ['badge', 'tooltip'].map(target),+ observations: {+ 'core-badge--solid': {badge: ['base']},+ 'core-badge--wide': {badge: ['base'], tooltip: ['base']},+ },+ baselineStories: new Set(['core-badge--solid']),+ },+ 'two baselined stories and a wider newcomer': {+ stories: ['first', 'second', 'wider'].map(name => badge(`core-badge--${name}`, name)),+ targets: ['a', 'b', 'c', 'd'].map(target),+ observations: {+ 'core-badge--first': {a: ['base'], b: ['base']},+ 'core-badge--second': {c: ['base'], d: ['base']},+ 'core-badge--wider': {a: ['base'], b: ['base'], c: ['base']},+ },+ baselineStories: new Set(['core-badge--first', 'core-badge--second']),+ },+ 'a baselined cover and a story newly tagged into the theme matrix': {+ stories: [+ badge('core-badge--first', 'First'),+ badge('core-badge--second', 'Second'),+ {...badge('core-badge--matrix', 'Matrix'), tags: ['visual-theme-matrix']},+ ],+ targets: ['a', 'b', 'c'].map(target),+ observations: {+ 'core-badge--first': {a: ['base'], b: ['base']},+ 'core-badge--second': {c: ['base']},+ 'core-badge--matrix': {a: ['base'], b: ['base'], c: ['base']},+ },+ baselineStories: new Set(['core-badge--first', 'core-badge--second']),+ },+ 'no baseline': {+ stories: ['first', 'second', 'wider'].map(name => badge(`core-badge--${name}`, name)),+ targets: ['a', 'b', 'c', 'd'].map(target),+ observations: {+ 'core-badge--first': {a: ['base'], b: ['base']},+ 'core-badge--second': {c: ['base'], d: ['base']},+ 'core-badge--wider': {a: ['base'], b: ['base'], c: ['base']},+ },+ baselineStories: new Set(),+ },+ };++ it.each(Object.entries(cases))(+ 'plans the same stories when its own plan is the baseline: %s',+ (_label, fixture) => {+ const once = plannedStories(fixture);+ const again = plannedStories({...fixture, baselineStories: new Set(once)});+ expect(again).toEqual(once);+ },+ );+ });++ describe('the probe cover keeps the baseline it already has', () => {+ const target = key => ({key, className: `astryx-${key}`, component: 'Badge', props: [], states: []});+ const four = ['a', 'b', 'c', 'd'].map(target);+ const badge = (id, name) => story({id, title: 'Core/Badge', name, component: 'Badge'});+ const first = badge('core-badge--first', 'First');+ const second = badge('core-badge--second', 'Second');+ const wider = badge('core-badge--wider', 'Wider');+ const coverOf = ({stories: list, observations, baselineStories}) => [+ ...new Set(+ buildPlan({+ stories: list,+ targets: four,+ themeOverrides: {},+ observations,+ defaultTheme: 'neutral',+ tiers: ['probe'],+ probeTheme: 'probe',+ baselineStories,+ }).map(shot => shot.storyId),+ ),+ ].sort();++ it('keeps the baselined stories when a wider story arrives that they still cover', () => {+ // The baseline covers a+b and c+d. A new story renders a, b and c: the+ // greedy cover alone would take it first and move the plan.+ const observations = {+ 'core-badge--first': {a: ['base'], b: ['base']},+ 'core-badge--second': {c: ['base'], d: ['base']},+ 'core-badge--wider': {a: ['base'], b: ['base'], c: ['base']},+ };+ expect(+ coverOf({+ stories: [first, second, wider],+ observations,+ baselineStories: new Set(['core-badge--first', 'core-badge--second']),+ }),+ ).toEqual(['core-badge--first', 'core-badge--second']);+ });++ it('drops a baselined story that renders no target and fills what it lost', () => {+ const observations = {+ 'core-badge--first': {},+ 'core-badge--second': {c: ['base'], d: ['base']},+ 'core-badge--wider': {a: ['base'], b: ['base'], c: ['base']},+ };+ expect(+ coverOf({+ stories: [first, second, wider],+ observations,+ baselineStories: new Set(['core-badge--first', 'core-badge--second']),+ }),+ ).toEqual(['core-badge--second', 'core-badge--wider']);+ });++ it('takes the widest story first when there is no baseline', () => {+ const observations = {+ 'core-badge--first': {a: ['base'], b: ['base']},+ 'core-badge--second': {c: ['base'], d: ['base']},+ 'core-badge--wider': {a: ['base'], b: ['base'], c: ['base']},+ };+ expect(coverOf({stories: [first, second, wider], observations})).toEqual([+ 'core-badge--second',+ 'core-badge--wider',+ ]);+ });+ });+describe('the probe cover between stories that tie', () => {const badgeTarget = [{key: 'badge', className: 'astryx-badge', component: 'Badge', props: [], states: []}];const solid = story({id: 'core-badge--solid', title: 'Core/Badge', name: 'Solid', component: 'Badge'});@@ -397,7 +537,7 @@ describe('buildPlan', () => {expect(probeBaselineStories({}, 'probe')).toEqual(new Set());});- it('still prefers the story that covers more targets over a baselined one', () => {+ it('lets a wider story a new target needs replace the baselined story it makes redundant', () => {const wide = story({id: 'core-badge--wide', title: 'Core/Badge', name: 'Wide', component: 'Badge'});const keys = buildPlan({stories: [solid, wide],